<?php require "_header.php";?>
<div id="main-report" align="center">
<center><h4>Customer Invoice </h4></center>
<?php 

$customerinvoice_id = Escape($_POST["customerinvoice_id"]);
$customer_id = Escape($_POST["customer_id"]);



if($customerinvoice_id =="" )
{
?>
	<script language="javascript" type="text/javascript">
		location.href='addedit-customerinvoice.php';
	</script>
<?php
}
elseif($_SESSION['cCompany_id']!="" && isset($_SESSION['cCompany_id'])) 
{
	$sno=1;
	$totalproduct=0;
	 $query="Select *
			 FROM lms_companymaster
			 WHERE
			 company_id='".$_SESSION['cCompany_id']."'";
			
	ob_start(); 
	$out = dbQuery($query);
	while($row=dbArray($out)){
	//print_r($row);	
	?>
    <table width="80%"  class="report">
  <tr>
    <td width="60%" colspan="3" rowspan="3">
    <b>
<?php echo $row["company_name"]; ?>
</b>
<br/>
<?php echo $row["company_address"]; ?>
<br/>
Phone:<?php echo $row["company_phone"]; ?>
<br/>
TIN No:<?php echo $row["company_tinno"]; ?>
<br/>
E-mail:<?php echo $row["company_email"]; ?>
</td>
</td>
    <td width="20%" style="text-align:center">Invoice No<br/><b><?php echo $_POST['customerinvoice_no'];?></b>
    </td>
    <td width="20%"style="text-align:center">Date<br/><b><?php echo $_POST['customerinvoice_date']; ?></b></td>
  </tr>
  <tr>
    <td style="text-align:center">Despatched through<br/><b><?php echo "ABCD";?></b></td>
    <td style="text-align:center">Terms of Payment<br/><b><?php echo $_POST['customerinvoice_paymenttype'];?> Day</b></td>
  </tr>
  <tr>
    <td style="text-align:center">Vat No<br/><b><?php echo $row["company_tinno"];?></b></td>
    <td style="text-align:center">CST NO<br/><b><?php echo $row["company_cstno"];?></b></td>
  </tr>
  <?php  $query="Select *
			 FROM lms_customer
			 WHERE
			 customer_id='".$customer_id."'";
			
			
	$out1 = dbQuery($query);
	while($row1=dbArray($out1)){ 
	//print_r($row1);
	
?>
  <tr>
    <td colspan="3" rowspan="3">
      Buyer
      <br/>
      <b>
      <?php echo $row1["customer_orgname"]; ?>
      </b>
      <br/>
      <?php echo $row1["customer_address"]; ?>
      <br/>
      Phone:<?php echo $row1["customer_phone1"]; ?>
      <br/>
    </td>
    <td style="text-align:center">Service Tax No<br/><b><?php echo $row["company_servicetaxno"];?></b></td>
    <td style="text-align:center">PAN No<br/><b><?php echo $row["company_panno"];?></b></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="10%" style="text-align:center">Sr.No</td>
    <td width="60%" style="text-align:center">Description of Goods</td>
    <td width="10%" style="text-align:center">Quantity</td>
    <td width="10%" style="text-align:center">Rate</td>
    <td width="10%" style="text-align:center">Amount</td>
  </tr>
   <?php
     $qry="Select lcp.product_id,lcp.product_qty,lcp.product_rate,lp.product_name
		from
			lms_customerinvoiceproduct lcp,
			lms_productmaster lp
		Where
			lcp.product_id=lp.product_id
		And
		customerinvoice_id='".$customerinvoice_id."'";
	
	    $finalout = dbQuery($qry);
	    while($finalrow=dbArray($finalout)){
		//print_r($finalrow); 
		$Amount=$finalrow["product_qty"] * $finalrow["product_rate"];
		$totalproduct+=$Amount;
   ?>
  <tr>
    <td style="text-align:center"><?php echo $sno;?></td>
    <td ><?php echo $finalrow["product_name"];?></td>
    <td style="text-align:center"><?php echo $finalrow["product_qty"];?></td>
    <td style="text-align:center"><?php echo $finalrow["product_rate"];?></td>
    <td style="text-align:right"><?php echo number_format($Amount,2);?></td>
  </tr>
  <?php $sno++; } ?>
   <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td style="text-align:right"><?php echo number_format($totalproduct,2);?></td>
  </tr>
  <?php
   
   $qry="Select lt.tax_name,lct.tax_percentage
	 	FROM
			lms_customerinvoicetax lct,
			lms_taxmaster lt
		WHERE
			lt.tax_id= lct.tax_id
		AND
			customerinvoice_id='".$customerinvoice_id."'";
			
			 $finalTax = dbQuery($qry);
	    while($finalTaxrow=dbArray($finalTax)){
		//print_r($finalTaxrow); 
		$taxamount=(($totalproduct * $finalTaxrow["tax_percentage"])/100);
		$totaltax+=$taxamount;
		?>
        <tr>
        <td></td>
        <td style="text-align:right" ><?php echo $finalTaxrow["tax_name"];?></td>
        <td></td>
        <td style="text-align:right" ><?php echo $finalTaxrow["tax_percentage"];?>%</td>
        <td style="text-align:right" ><?php echo number_format($taxamount,2);?></td>
      </tr>
        <?php
		} $grandtotal= $totaltax + $totalproduct;?>
        <tr>
        <td></td>
        <td>&nbsp;</td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
         <tr>
        <td></td>
        <td style="text-align:right">Total</td>
        <td></td>
        <td></td>
        <td style="text-align:right"><?php echo number_format($grandtotal,2);?></td>
      </tr>
  
</table>
    
	<?php } }
/*	 
 }else
	{
		echo "<b>No Data Found</b>";
	}*/
	
 } else{
         echo "Session Expired"; 
       }
?>