<?php
function drawTempInvoicingGrid($connection, $date, $headerinvoicing_id){
	echo "<table width=1100 style='border-collapse: collapse'>";
	echo "<tr>
	<th width=120 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Client Code</th>
	<th width=180 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Client Name</th>
	<th width=100 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Ref. #</th>
	<th width=75 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Curreny</th>
	<th width=100 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Amount</th>
	<th width=120 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Value Date</th>
	<th width=180 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'>Status</th>
	<th width=25 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'></th>
	<th width=25 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'></th>
	<th width=25 style='font-size: 15px; font-weight: bold; padding: 5px; background: #bebebe; bordeR: solid 1.5px #000000'></th>
	</tr>";
	echo "<tr>
	<td width=120><input type='text' name='Client_Code' id='Client_Code' style='width: 75%' onkeyup=\"goToNextField('Client_Name', event); chkClientCode(event)\" autofocus='autofocus' /> <img src='img/btnopen.jpg' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListCustomers()\" /></td>
	<td width=180><input type='text' name='Client_Name' id='Client_Name' style='width: 100%' onkeyup=\"goToNextField('Invoicing_Reference', event)\" /></td>
	<td width=100><input type='text' name='Invoicing_Reference' id='Invoicing_Reference' style='width: 100%' onkeyup=\"goToNextField('Currency_Code', event)\" /></td>
	<td width=75><select name='Currency_Code' id='Currency_Code' style='width: 100%' onchange=\"goToAmount()\" style='vertical-align:top;'>
	<option value=''></option>";
	$query_currency  = "select Currency_Code as CurrencyCode from currency";
	$result_currency = mysql_query($query_currency, $connection); 
	while($row_currency = mysql_fetch_array($result_currency)){
		extract($row_currency);
		echo "<option value='$CurrencyCode'>$CurrencyCode</option>";
	}
	echo"</select></td>
	<td width=100><input type='text' name='Invoicing_Amount' id='Invoicing_Amount' style='width:100%' onkeyup=\"submitInsert(event)\" /></td>
	<td width=120><input type='text' name='Invoicing_ValueDate' id='Invoicing_ValueDate' style='width:100%' readonly /></td>
	<td width=180 id='client_status' style='text-align: center; font-size: 14px; font-weight: bold'></td>
	<td width=25></td>
	<td width=25 style='text-align: center'><img src='img/confirm.png' width=20 style='cursor: pointer' onclick=\"insertInvoicing()\" /></td>
	<td width=25 style='text-align: center'></td>
	</tr>";
	$query_invoicing = "select invoicing.Invoicing_Id as InvoicingId, invoicing.Client_Code as ClientCode, clients.ledger_name as ClientName, invoicing.Invoicing_Reference as RefNo, invoicing.Currency_Code as CurrencyCode, format(Invoicing_Amount, 2) as Invoicing_Amount, date_format(Invoicing_ValueDate, '%d/%m/%Y') as ValueDate, clients.Client_EndofMonthPay, clients.Client_PaymentTerms, clients.Client_PaymentMode, clients.Client_paymentTerms2, CLIENT_CASHMODE as CashMode
	                    from temp_invoicing as invoicing inner join clients on clients.ledger_number = invoicing.client_code
						where 1=1";
	if($headerinvoicing_id == "" || $headerinvoicing_id == "null"){
		$query_invoicing = $query_invoicing." and invoicing.headerinvoicing_id is null";
	}
	else{
		$query_invoicing = $query_invoicing." and invoicing.headerinvoicing_id = '$headerinvoicing_id'";
	}
	$query_invoicing = $query_invoicing." order by invoicing_id desc";
	//echo $query_invoicing;
	$result_invoicing = mysql_query($query_invoicing, $connection);
	$number_invoicing = mysql_num_rows($result_invoicing);
	echo "<input type='hidden' id='num_invoicing' name='num_invoicing' value='$number_invoicing' />";
	while($row_invoicing = mysql_fetch_array($result_invoicing)){
		extract($row_invoicing);
		echo "<tr id='$InvoicingId'>
	<td width=120><input type='text' name='Client_Code' id='Client_Code_$InvoicingId' style='width: 75%' value='$ClientCode' /> <img src='img/btnopen.jpg' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListCustomers2('$InvoicingId')\" /></td>
	<td width=280><input type='text' name='Client_Name' id='Client_Name_$InvoicingId' style='width: 100%' value='$ClientName' /></td>
	<td width=100><input type='text' name='Invoicing_Reference_$InvoicingId' id='Invoicing_Reference_$InvoicingId' value='$RefNo' style='width: 100%' /></td>
	<td width=75><select name='Currency_Code_$InvoicingId' id='Currency_Code_$InvoicingId' style='width: 100%'>
	<option value=''></option>";
	$query_currency  = "select Currency_Code as CurrencyCodeX from currency";
	$result_currency = mysql_query($query_currency, $connection); 
	while($row_currency = mysql_fetch_array($result_currency)){
		extract($row_currency);
		if($CurrencyCode == $CurrencyCodeX){
			$selected = "selected = 'selected'";
		}
		else{
			$selected = "";
		}
		echo "<option value='$CurrencyCodeX' $selected>$CurrencyCodeX</option>";
	}
	echo"</select></td>
	<td width=100><input type='text' name='Invoicing_Amount_$InvoicingId' id='Invoicing_Amount_$InvoicingId' value='".str_replace(",", "", $Invoicing_Amount)."' style='width:100%' /></td>
	<td width=120><input type='text' name='Invoicing_ValueDate_$InvoicingId' id='Invoicing_ValueDate_$InvoicingId' value='$ValueDate' style='width:100%' readonly /></td>
	<td width=180 style='font-size: 14px; font-weight: bold; text-align: center' id='client_status_$InvoicingId'>";
	if($Client_EndofMonthPay == 1){
		echo "End of Month";
	}
	else
		if($Client_PaymentTerms > 0){
			echo "Payment Term";
	}
	else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				echo "Weekly";
			}
			else
				if($CashMode == 1){
					echo "Cash Mode";
				}
	echo"</td>
	<td width=25 style='text-align: center'><img src='img/btnopen.jpg' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListInvoicingDt('$InvoicingId')\" /></td>
	<td width=25 style='text-align: center'><img src='img/confirm.png' width=20 style='cursor: pointer' onclick=\"editInvoicing('$InvoicingId')\" /></td>
	<td width=25 style='text-align: center'><img src='img/delete2.png' width=20 style='cursor: pointer' onclick=\"deleteInvoicing('$InvoicingId')\" /></td>
	</tr>";
	$listinvoicingid = $listinvoicingid."$InvoicingId,";
	}
	echo "</table>";
	echo "<input type='hidden' id='list_invoicing_id' name='list_invoicing_id' value='$listinvoicingid'>";
}
function drawInvoicingDt($connection, $invoicing_id){
	echo "<table width=800 style='margin: auto;'>
	<tr>
	<th width=45% style='background-color: #e5e5e5; padding: 5px; font-size: 14px; font-family: Arial'>Amount</th>
	<th width=45% style='background-color: #e5e5e5; padding: 5px; font-size: 14px; font-family: Arial'>Due Date</th>
	<th width=5% style='background-color: #e5e5e5; padding: 5px; font-size: 14px; font-family: Arial'></th>
	<th width=5% style='background-color: #e5e5e5; padding: 5px; font-size: 14px; font-family: Arial'></th>
	</tr>";
	echo "<tr>
		<td><input type='text' name='InvoicingDt_Amount' id='InvoicingDt_Amount' style='width: 100%' value='".str_replace(",", "", $InvoicingDt_Amount)."' /></td>
		<td><A HREF='#' onClick=\"displayCalendar(document.getElementById('InvoicingDt_DueDate'),'dd/mm/yyyy',this);  return false; \" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A>&nbsp;<input type='text' name='InvoicingDt_DueDate' id='InvoicingDt_DueDate' style='width: 93%' value='' /></td>
		<td style='text-align: center'><img src='img/confirm.png' width=20 style='cursor: pointer' onclick=\"insertInvoicingDt()\" /></td>
		<td></td>
		</tr>";
	$query_invoicingdt  = "select InvoicingDt_Id, date_format(InvoicingDt_DueDate, '%d/%m/%Y') as InvoicingDt_DueDate, InvoicingDt_DueDate as InvoicingDt_DueDate2, format(InvoicingDt_Amount, 2) as InvoicingDt_Amount
	                       from temp_invoicingdt
						   where invoicing_id = '$invoicing_id' order by InvoicingDt_DueDate2 asc";
	//echo $query_invoicingdt;
	$result_invoicingdt = mysql_query($query_invoicingdt, $connection);
	while($row_invoicingdt = mysql_fetch_array($result_invoicingdt)){
		extract($row_invoicingdt);
		echo "<tr>
		<td><input type='text' name='InvoicingDt_Amount_$InvoicingDt_Id' id='InvoicingDt_Amount_$InvoicingDt_Id' style='width: 100%' value='".str_replace(",", "", $InvoicingDt_Amount)."' /></td>
		<td><A HREF='#' onClick=\"displayCalendar(document.getElementById('InvoicingDt_DueDate_$InvoicingDt_Id'),'dd/mm/yyyy',this);  return false; \" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A>&nbsp;<input type='text' name='InvoicingDt_DueDate_$InvoicingDt_Id' id='InvoicingDt_DueDate_$InvoicingDt_Id' style='width: 93%' value='$InvoicingDt_DueDate' /></td>
		<td style='text-align: center'><img src='img/confirm.png' width=20 style='cursor: pointer' onclick=\"editInvoicingDt('$InvoicingDt_Id')\" /></td>
		<td style='text-align: center'><img src='img/delete2.png' width=20 style='cursor: pointer' onclick=\"deleteInvoicingDt('$InvoicingDt_Id')\" /></td>
		</tr>";
	}
	echo"</table>";
}
function drawListInvoicing($connection, $search_id, $search_date){
	
	echo "<table width=100%>
	<tr>
	<th style='width: 25px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'></th>
	<th style='width: 25px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'></th>
	<th style='width: 25px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'></th>
	<th style='width: 100px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'>ID</th>
	<th style='background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'>Date</th>
	</tr>";
	$query_invoicing = "select headerinvoicing_id as HeaderId, date_format(headerinvoicing_date, '%d/%m/%Y') as HeaderDate
	                    from headerinvoicing
						where 1=1";
	if($search_id != ""){
		$query_invoicing = $query_invoicing." and headerinvoicing_id = '$search_id'";
	}
	if($search_date != ""){
		$search_date = explode("/", $search_date);
		$search_date = $search_date[2]."-".$search_date[1]."-".$search_date[0];
		$query_invoicing = $query_invoicing." and headerinvoicing_date between '$search_date' and '$search_date 23:59:59'";
	}
	$result_invoicing = mysql_query($query_invoicing, $connection);
	$count = 0;
	while($row_invoicing = mysql_fetch_array($result_invoicing)){
		extract($row_invoicing);
		$count++;
		$query_chkexistintemp  = "select count(*) as ExistInTemp from temp_headerinvoicing where headerinvoicing_id = '$HeaderId'";
		$result_chkexistintemp = mysql_query($query_chkexistintemp, $connection);
        $row_chkexistintemp    = mysql_fetch_array($result_chkexistintemp);
        extract($row_chkexistintemp);		
		if($count %2 != 0){
			$color = "#e6e6e6";
		}
		else{
			$color = "#f7f7f7";
		}
		echo "<tr style='background-color: $color'>
		<th><img src='img/delete.png' width=20 style='cursor: pointer' onclick=\"deleteInvoicingHeader('$HeaderId')\" /></th>
		<th>";
		if($ExistInTemp == 0){
		echo"<img src='img/edit.png' width=20 style='cursor: pointer' onclick=\"editInvoicingHeader('$HeaderId')\" />";
		}
		echo"</th>
		<th><img src='img/details.gif' width=15 style='cursor: pointer' onclick=\"viewDetails('$HeaderId')\" /></th>
		<th style='font-family: Arial; font-size: 16px; font-weight: bold; '>$HeaderId</th>
		<th style='font-family: Arial; font-size: 16px; font-weight: bold; '>$HeaderDate</th>
		</tr>";
		echo "<t>
		<th colspan=5 style='background: $color'><div id='list_invoicing_$HeaderId' style='padding: 5px; font-size: 14px; font-family: Arial; background: #ccffff; border: solid 1px #33ccff; width: 90%; margin: auto; display: none;'>
		
		</div></th>
		</tr>";
	}
	echo"</table>";
}
function drawListTempInvoicing($connection, $search_id, $search_date){
	
	echo "<table width=100%>
	<tr>
	<th style='width: 25px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'></th>
	<th style='width: 25px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'></th>
	<th style='width: 25px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'></th>
	<th style='width: 100px; background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'>ID</th>
	<th style='background: #1e87a5; font-family: Arial; color: #FFFFFF; font-weight: bold; font-size: 18px;'>Date</th>
	</tr>";
	$query_invoicing = "select headerinvoicing_id as HeaderId, date_format(headerinvoicing_date, '%d/%m/%Y') as HeaderDate
	                    from temp_headerinvoicing
						where 1=1";
	if($search_id != ""){
		$query_invoicing = $query_invoicing." and headerinvoicing_id = '$search_id'";
	}
	if($search_date != ""){
		$search_date = explode("/", $search_date);
		$search_date = $search_date[2]."-".$search_date[1]."-".$search_date[0];
		$query_invoicing = $query_invoicing." and headerinvoicing_date between '$search_date' and '$search_date 23:59:59'";
	}
	//echo $query_invoicing;
	$result_invoicing = mysql_query($query_invoicing, $connection);
	$count = 0;
	while($row_invoicing = mysql_fetch_array($result_invoicing)){
		extract($row_invoicing);
		$count++;	
		if($count %2 != 0){
			$color = "#e6e6e6";
		}
		else{
			$color = "#f7f7f7";
		}
		echo "<tr style='background-color: $color'>
		<th><img src='img/delete.png' width=20 style='cursor: pointer' onclick=\"deleteTempInvoicingHeader('$HeaderId')\" /></th>
		<th>";
		echo"<img src='img/edit.png' width=20 style='cursor: pointer' onclick=\"editTempInvoicingHeader('$HeaderId')\" />";
		echo"</th>
		<th><img src='img/details.gif' width=15 style='cursor: pointer' onclick=\"viewDetails('$HeaderId')\" /></th>
		<th style='font-family: Arial; font-size: 16px; font-weight: bold; '>$HeaderId</th>
		<th style='font-family: Arial; font-size: 16px; font-weight: bold; '>$HeaderDate</th>
		</tr>";
		echo "<t>
		<th colspan=5 style='background: $color'><div id='list_invoicing_$HeaderId' style='padding: 5px; font-size: 14px; font-family: Arial; background: #ccffff; border: solid 1px #33ccff; width: 90%; margin: auto; display: none;'>
		
		</div></th>
		</tr>";
	}
	echo"</table>";
}
function drawListInvoicingSearch($connection, $headerinvoicing_id){
	echo "<table width=100%>
	<tr>
	<th style='font-size: 12px; font-family: Arial' width=14%>Client Code</th>
	<th style='font-size: 12px; font-family: Arial' width=25%>Client Name</th>
	<th style='font-size: 12px; font-family: Arial' width=8%>Reference No.</th>
	<th style='font-size: 12px; font-family: Arial' width=10%>Amount</th>
	<th style='font-size: 12px; font-family: Arial' width=8%>Currency</th>
	<th style='font-size: 12px; font-family: Arial' width=10%>Value Date</th>
	<th style='font-size: 12px; font-family: Arial' width=25%>Status</th>
	</tr>";
	$query_wholeinvoicing  = "select invoicing.client_code as ClientCode, clients.ledger_name as ClientName, invoicing.Invoicing_Reference as RefNo, format(Invoicing_Amount, 2) as InvoicingAmount, invoicing.currency_code as CurrencyCode, date_format(Invoicing_ValueDate, '%d/%m.%Y') as ValueDate, clients.Client_EndofMonthPay, clients.Client_PaymentTerms, clients.Client_PaymentMode, clients.Client_paymentTerms2, clients.CLIENT_CASHMODE as CashMode
	                          from invoicing inner join clients on invoicing.client_code = clients.ledger_number
							  where headerinvoicing_id = '$headerinvoicing_id'";
	//echo $query_wholeinvoicing;
	$result_wholeinvoicing = mysql_query($query_wholeinvoicing, $connection);
	while($row_wholeinvoicing = mysql_fetch_array($result_wholeinvoicing)){
		extract($row_wholeinvoicing);
		echo "<tr>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: left' width=14%>$ClientCode</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: left' width=30%>$ClientName</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=8%>$RefNo</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: right' width=10%>$InvoicingAmount</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=8%>$CurrencyCode</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=10%>$ValueDate</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=20%>";
		if($Client_EndofMonthPay == 1){
		echo "End of Month";
	}
	else
		if($Client_PaymentTerms > 0){
			echo "Payment Term";
	}
	else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				echo "Weekly";
			}
			else
				if($CashMode == 1){
					echo "Cash Mode";
				}
		echo"</td>
		</tr>";
	}
	echo"</table>";
}
function drawListTempInvoicingSearch($connection, $headerinvoicing_id){
	echo "<table width=100%>
	<tr>
	<th style='font-size: 12px; font-family: Arial' width=14%>Client Code</th>
	<th style='font-size: 12px; font-family: Arial' width=25%>Client Name</th>
	<th style='font-size: 12px; font-family: Arial' width=8%>Reference No.</th>
	<th style='font-size: 12px; font-family: Arial' width=10%>Amount</th>
	<th style='font-size: 12px; font-family: Arial' width=8%>Currency</th>
	<th style='font-size: 12px; font-family: Arial' width=10%>Value Date</th>
	<th style='font-size: 12px; font-family: Arial' width=25%>Status</th>
	</tr>";
	$query_wholeinvoicing  = "select invoicing.client_code as ClientCode, clients.ledger_name as ClientName, invoicing.Invoicing_Reference as RefNo, format(Invoicing_Amount, 2) as InvoicingAmount, invoicing.currency_code as CurrencyCode, date_format(Invoicing_ValueDate, '%d/%m.%Y') as ValueDate, clients.Client_EndofMonthPay, clients.Client_PaymentTerms, clients.Client_PaymentMode, clients.Client_paymentTerms2, clients.CLIENT_CASHMODE as CashMode
	                          from temp_invoicing as invoicing inner join clients on invoicing.client_code = clients.ledger_number
							  where headerinvoicing_id = '$headerinvoicing_id'";
	//echo $query_wholeinvoicing;
	$result_wholeinvoicing = mysql_query($query_wholeinvoicing, $connection);
	while($row_wholeinvoicing = mysql_fetch_array($result_wholeinvoicing)){
		extract($row_wholeinvoicing);
		echo "<tr>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: left' width=14%>$ClientCode</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: left' width=30%>$ClientName</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=8%>$RefNo</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: right' width=10%>$InvoicingAmount</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=8%>$CurrencyCode</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=10%>$ValueDate</td>
		<td style='font-family: Arial; font-size: 11px; color: #186194; text-align: center' width=20%>";
		if($Client_EndofMonthPay == 1){
		echo "End of Month";
	}
	else
		if($Client_PaymentTerms > 0){
			echo "Payment Term";
	}
	else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				echo "Weekly";
			}
			else
				if($CashMode == 1){
					echo "Cash Mode";
				}
		echo"</td>
		</tr>";
	}
	echo"</table>";
}
?>