<?php
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
session_start();

	
$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
	
function getInvoiceGrid ($invFromdate, $invTodate, $orderby, $startrange, $perpage, $connection){
		
		
		if ($startrange=='')
			$startrange = 0;
		if ($perpage=='')
			$perpage = 20;
		
		$ndate = explode('/',$invFromdate);
		$invFromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
		
		$ndate = explode('/',$invTodate);
		$invTodate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
				
		if ($orderby == '1') $order = " delivery_reference ";
		else if ($orderby == '2') $order = " delivery_date ";
		
		$query = "select delivery_reference, date_format(delivery_date,'%d-%m-%Y'), client_code, ledger_name, project_name, costcent_name, delivery_id
				from deliveries
				inner join clients on clients.ledger_number = deliveries.client_code
				left join project on project.Project_Code = deliveries.project_code
				left join costcent on costcent.Costcent_code = deliveries.Costcent_code
				where date_format(delivery_date,'%Y-%m-%d') between '$invFromdate' and '$invTodate'
				order by $order desc ";
			
		$res = mysql_query($query, $connection);	
		$totalrows = mysql_num_rows($res);
		
		$query = $query." limit ".$startrange.",".$perpage;	
		$res = mysql_query($query, $connection);	
		$totalrow = mysql_num_rows($res);
		
		echo "<table align='center' cellpadding=2 cellspacing=2 width=1000px>";
		echo "<col width=50px><col width=100px><col width=120px><col width=330px><col width=200px><col width=200px>";
		// Pagination
		echo "<tr><td></td><td align='right' colspan='2'><span class='style8'>Per Page </span>
				<input type='hidden' name='startrange' id='startrange' value='$startrange'>
				<span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage();\" style='border-radius: 5px; font-size: 11px;'>";
		if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';
		echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>
			 <td  style="border-right:1px solid #000000;text-align:right;">';
		if($startrange > 0)
			echo"<img src='img/firstpage.png' height='15px' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

		$diff= $startrange - $perpage;
			
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"&nbsp;&nbsp;<img src='img/arrowleft.png' height='15px' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";	
		echo"		</td><td  align='left'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' height='15px' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' height='15px' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
		echo"		</td><td>&nbsp;</td>";
		echo "</tr>";
		echo "<tr><td align='center'><input type='checkbox' name='allIds' id='allIds' onclick=\"selectAll();\" title='select all'></td>
				<td align='center'><span class='style8'>Ref.</span></td><td align='center'><span class='style8'>Date</span></td>
				<td align='center'><span class='style8'>Client</span></td><td align='center'><span class='style8'>Project</span></td><td align='center'><span class='style8'>Costcent</span></td></tr>";
		echo "<tr><td colspan='6'><hr></td></tr>";
		
		$i=0;
		while ($rw = mysql_fetch_array($res)){
			echo "<tr>
					<td align='center'><input type='checkbox' name='delId$i' id='delId$i' value='$rw[6]' onclick=\"addDelivery();\"></td>
					<td align='center' style='font-size:11px;'>$rw[0]</td>
					<td align='center' style='font-size:11px;'>$rw[1]</td>
					<td align='left' style='font-size:11px;'>$rw[2] $rw[3]</td>
					<td align='left' style='font-size:11px;'>$rw[4]</td>
					<td align='left' style='font-size:11px;'>$rw[5]</td>
				  </tr>";
		
			$i++;
		}
		echo "<input type='hidden' name='delIds' id='delIds' value='$delIds'>";
		echo "<input type='hidden' name='totalrow' id='totalrow' value='$totalrow'>";
		echo "</table>";
	}


function exportGrid($base1, $base2, $projectcode, $costcentcode, $connection){
	$bottom = "border-bottom: 1px solid #000000a1";
	$left   = "border-left: 1px solid #000000a1";
	$top    = "border-top: 1px solid #000000a1";
	$right  = "border-right: 1px solid #000000a1";
	
	$query = "select currency_code from currency order by currency_code";
	
	
	echo "<table width=100% cellpadding=0 cellspacing=0>
		<col width='2.5%'><col width='2.5%'><col width='5%'><col width='5%'><col width='18%'><col width='22%'><col width='13%'><col width='13%'><col width='4%'><col width='5%'><col width='10%'>";
	echo "<tr height='30px'>
				<td style='$bottom;$left;$top;'></td><td style='$bottom;$left;$top;'></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Line NB</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Column NB</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Account</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Name</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Project</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Costcenter</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>D/C</span></td>
				<td style='$bottom;$left;$top;'><span class='style8'>Curr.</span></td>
				<td style='$bottom;$left;$top;$right;'><span class='style8'>Only...0 Balance</span></td>
		</tr>";
	$i = 0;
	$queryall = "select exp_id, linenb, colnb, ledgerexport.ledger_number, ledger_name, ledgerexport.project_code, ledgerexport.costcent_code, dbcd, currency, balance
				from ledgerexport
				inner join ledger on ledger.ledger_number = ledgerexport.ledger_number
				order by linenb, colnb ";
	$resall = mysql_query($queryall, $connection);			
	while ($rowall = mysql_fetch_array($resall)){
		
		echo "<tr height='30px'>
				<td style='$bottom;$left;'><img src='img/delete.png' width='20' height='20' border='0' onclick=\"deleteRecord('$rowall[0]');\" onmouseover=\"style.cursor='pointer'\" style='vertical-align: bottom; cursor: pointer;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='button' id='imgSubmit[$i]' onclick=\"submitRecord('$i','$rowall[0]')\" style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png);'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='linenb[$i]' id='linenb[$i]' value='$rowall[1]' style='width:95%;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='colnb[$i]' id='colnb[$i]' value='$rowall[2]' style='width:95%;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='ledgernumber[$i]' id='ledgernumber[$i]' value='$rowall[3]' style='width:80%;'>
					<img src='img/btnopen.jpg' title='Select Ledger' width='15' height='17' border='0' onmouseover=\"style.cursor='hand';\" onclick=\"showlistLedger('$i'); return false;\">
				</td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='ledgername[$i]' id='ledgername[$i]' value='$rowall[4]' style='width:98%;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='projectcode[$i]' id='projectcode[$i]' value='$rowall[5]' style='width:80%;'>
					<img src='img/btnopen.jpg' title='Select Project' width='15' height='17' border='0' onmouseover=\"style.cursor='hand';\" onclick=\"showListProject('$i'); return false;\"></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='costcentcode[$i]' id='costcentcode[$i]' value='$rowall[6]' style='width:80%;'>
					<img src='img/btnopen.jpg' title='Select Project' width='15' height='17' border='0' onmouseover=\"style.cursor='hand';\" onclick=\"showListCostcent('$i'); return false;\"></td>
				<td style='$bottom;$left;' valign='middle' align='center'><select name='dbcd[$i]' id='dbcd[$i]' style='width:95%;'>
					<option value='' ".($rowall[7]==''?'selected':'')."></option>
					<option value='D' ".($rowall[7]=='D'?'selected':'').">D</option>
					<option value='C' ".($rowall[7]=='C'?'selected':'').">C</option>
				</select></td>
				<td style='$bottom;$left;' valign='middle' align='center'><select name='currencycode[$i]' id='currencycode[$i]' style='width:95%;'>";
			echo "<option value='' ".($rowall[8]==''?'selected':'')."></option>
				  <option value='$base1' ".($rowall[8]==$base1?'selected':'').">$base1</option>
				  <option value='$base2' ".($rowall[8]==$base2?'selected':'').">$base2</option>";		
		/* $res = mysql_query($query, $connection);
		while($row = mysql_fetch_array($res)){
			echo "<option value='$row[0]' ".($rowall[8]==$row[0]?'selected':'').">$row[0]</option>";
		} */
		echo "	</select></td>
				<td style='$bottom;$left;$right;' valign='middle' align='center'><select name='bal[$i]' id='bal[$i]' style='width:70%;'>
					<option value='0' ".($rowall[9]=='0'?'selected':'')."></option>
					<option value='1' ".($rowall[9]=='1'?'selected':'')."> > </option>
					<option value='2' ".($rowall[9]=='2'?'selected':'')."> < </option>
				</select></td>
		</tr>";
		
		$i++;
		$projectcode  = $rowall[5];
		$costcentcode = $rowall[6];
	}
	
	echo "<tr height='30px' class='bgcolor1'>
				<td style='$bottom;$left;' valign='middle' align='center'><img src='img/undo-1.png' width='20' height='20' border='0' onclick=\"clearRecord('$i');\" onmouseover=\"style.cursor='pointer'\" style='vertical-align: bottom; cursor: pointer;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='button' id='imgSubmit[$i]' onclick=\"submitRecord('$i','');\" style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png);'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='linenb[$i]' id='linenb[$i]' value='' style='width:95%;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='colnb[$i]' id='colnb[$i]' value='' style='width:95%;'></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='ledgernumber[$i]' id='ledgernumber[$i]' value='' style='width:80%;' readonly>
					<img src='img/btnopen.jpg' title='Select Ledger' width='15' height='17' border='0' onmouseover=\"style.cursor='hand';\" onclick=\"showlistLedger('$i'); return false;\">
				</td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='ledgername[$i]' id='ledgername[$i]' value='' style='width:98%;' readonly></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='projectcode[$i]' id='projectcode[$i]' value='$projectcode' style='width:80%;'>
					<img src='img/btnopen.jpg' title='Select Project' width='15' height='17' border='0' onmouseover=\"style.cursor='hand';\" onclick=\"showListProject('$i'); return false;\"></td>
				<td style='$bottom;$left;' valign='middle' align='center'><input type='text' name='costcentcode[$i]' id='costcentcode[$i]' value='$costcentcode' style='width:80%;'>
					<img src='img/btnopen.jpg' title='Select Project' width='15' height='17' border='0' onmouseover=\"style.cursor='hand';\" onclick=\"showListCostcent('$i'); return false;\"></td>
				<td style='$bottom;$left;' valign='middle' align='center'><select name='dbcd[$i]' id='dbcd[$i]' style='width:95%;'>
					<option value=''></option>
					<option value='D'>D</option>
					<option value='C'>C</option>
				</select></td>
				<td style='$bottom;$left;' valign='middle' align='center'><select name='currencycode[$i]' id='currencycode[$i]' style='width:95%;'>";
			echo "<option value=''></option>
				  <option value='$base1'>$base1</option>
				  <option value='$base2'>$base2</option>";		
		/* $res = mysql_query($query, $connection);
		while($row = mysql_fetch_array($res)){
			echo "<option value='$row[0]'>$row[0]</option>";
		} */
	echo "		</select></td>
				<td style='$bottom;$left;$right;' valign='middle' align='center'><select name='bal[$i]' id='bal[$i]' style='width:70%;'>
					<option value='0'></option>
					<option value='1'> > </option>
					<option value='2'> < </option>
				</select></td>
		</tr>";
	
	echo "</table>";
}
	  
?>