<?php include("sass/header_links_only.php");?>
          
        
      <?php

$invoice_no =$_REQUEST['inv'];
$query_invoice = "SELECT * FROM sale_taka where invoice_no ='$invoice_no'";
                                        $result_invoice = mysqli_query($connection,$query_invoice);
                                        
										
										
                                        while($row_invoice=mysqli_fetch_array($result_invoice))
                                        {
                                        
										
                                        $have_to_pay = $row_invoice['have_to_pay'];
										$payment = $row_invoice['payment'];
										
										$beforedue = $row_invoice['beforedue'];
										$cash = $row_invoice['cash'];
										$bank = $row_invoice['bank'];
										$vehicle_cost = $row_invoice['vehicle_cost'];
										$beton = $row_invoice['beton'];
										$discount = $row_invoice['discount'];
										$due = $row_invoice['due'];
										$tax = $row_invoice['tax'];
										$cheque_no = $row_invoice['cheque_no'];
										$cheque_date = $row_invoice['cheque_date'];
										$date = $row_invoice['date'];
										$customer_id = $row_invoice['customer_id'];
										
										$v_plus_minus = $row_invoice['v_plus_minus'];
										$posting_time_date = $row_invoice['posting_time_date'];
										
										if (empty($customer_id)) {
										
										$name = $row_invoice['retail_name'];
										$address = $row_invoice['retail_address'];
										$district = "";
											$cl_mobile_no = "";
										
										}
										
										else {
										$result_for_cus_name = mysqli_query($connection,"SELECT * FROM customer_setup WHERE customer_id  = $customer_id");
		$test_for_cus_name = mysqli_fetch_array($result_for_cus_name);
				
				$name=$test_for_cus_name['customer_name'];
				$address=$test_for_cus_name['address'];
				$cl_mobile_no=$test_for_cus_name['mobile_no'];
				$district=$test_for_cus_name['district'];
				
				$district = ", $district";
				
				/*$opening_due = $test_for_cus_name['opening_due'];
				
				
				$result_for_supplier = mysqli_query($connection,"SELECT SUM(due) AS due FROM sale_taka WHERE customer_id  = '$customer_id'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				
				$beforedue=$test_for_supplier['due'];
				$beforedue= $beforedue + $opening_due ;
				$beforedue = $beforedue - $due ;*/
				
				
										}
										
						$user_id_t = $_SESSION['user_id'] ;
						
				$result_for_supplier = mysqli_query($connection,"SELECT branch_id FROM users WHERE users_id = '$user_id_t'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				$branch_id=$test_for_supplier['branch_id'];	
						
			$result_for_supplier = mysqli_query($connection,"SELECT * FROM branch WHERE branch_id  = '$branch_id'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				$branch_name=$test_for_supplier['branch_name'];	
				$email=$test_for_supplier['email'];			
				$mobile2=$test_for_supplier['mobile'];			
				$address2=$test_for_supplier['address'];		
				$website=$test_for_supplier['website'];								
										
?>  
<style>
body
{
margin:0;
padding:2;
}
.header
{


}

.logo
{
background-color: buttonface;
    float: left;
    height: 200px;
    width: 200px;
}
.right_site
{

}

td
{
font-size:15px;

}
@media print{

#notprintableArea { visibility: hidden; }

}
</style>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />


<div style="width: 100%;font-size: 15px; text-align:center"> 
<div class="">
<div style="font-size:20px;text-align:center"><span style="background:#834042;color:#fff;border:2px solid #834042;border-radius:50px;padding:5px;">INVOICE</span></div>
<div style="font-size:25px;text-align:center"><img style="margin-right:5px;" src="img/scpfc.png" alt="" /> <?php echo $branch_name; ?> <img class="img-circle" src="img/chicks.jpg" alt="" /></div>
<div style="font-size:15;margin-bottom:4px">
<?php echo $address2; ?>,
 Bangladesh 
</div>
<div style="font-size:16px;text-align:center"><span style="margin-bottom:4px;border:2px solid #BB8889;border-radius:50px;padding:3px;"> Call : <?php echo $mobile2; ?> Email: <?php echo $email; ?></span></b> <br>
Website: <?php echo $website; ?>
</div>
<div> Invoice no : <?php echo $invoice_no; ?>&nbsp; &nbsp; Date :
<?php
echo  ($date) ; ?> </div>

   </div> 
<table align="center" width="70%">
<tr>
<td>
Buyer's Name : <?php echo $name ; ?>
</td>
<td>
Mob : <?php echo $cl_mobile_no ; ?>
</td>
<td>
  Address :  <?php echo $address  ; ?> <?php echo $district  ; ?>
</td>
</tr> 
</table>  
<hr />
<table border="1" align="center" style="width:80%">
<tr>
<th style="text-align:center;">
Product name
</th>
<th style="text-align:center;">
Quantity
</th>
<th style="text-align:center;">
Per unit price
</th>
<th style="text-align:right;">
Price
</th>
</tr>
<?php 
$query_sale = "SELECT * FROM sale where invoice_no = '$invoice_no'";
                                        $result_sale = mysqli_query($connection,$query_sale);
										$i=0;
										$grand_total = 0 ;
                                        while($row_sale=mysqli_fetch_array($result_sale))
                                        {
										$i++;
                                        $product_id = $row_sale['product_id'];
                                        $posted_by_id = $row_sale['user_id'];
										$quantity = $row_sale['quantity'];
										$quantity_show = $quantity ;
										$carton = $row_sale['carton'];
										$packate = $row_sale['packate'];
										if($carton <> 0 || $packate <> 0) {
											if($carton <> 0 && $packate == 0) {
												$when_carton = "$carton sack(s)";
												}
												elseif($carton == 0 && $packate <> 0) {
												$when_carton = "$packate Packet";
												}
												else {
												$when_carton = "$carton Carton $packate Packet";
												}
										}
										$result_for_product = mysqli_query($connection,"SELECT * FROM product_setup WHERE product_id  = $product_id");
										$test_for_product = mysqli_fetch_array($result_for_product);
										$product_name_to=$test_for_product['product_name'];
										$unit_type_id = $test_for_product['unit_type_id'];
										$selling_price = $row_sale['selling_price'];
                                        $per_total = $quantity * $selling_price ;
										$grand_total = $grand_total + $per_total ;
										$transport_cost = $row_sale['transport_cost'];
										if ($v_plus_minus == "p") {
$grand_total = $grand_total + $vehicle_cost ;
}
else {
$grand_total = $grand_total - $vehicle_cost ;
}
										
									$result_unit_name = mysqli_query($connection,"SELECT * FROM unit_type WHERE unit_type_id  = $unit_type_id");
		$test_unit_name = mysqli_fetch_array($result_unit_name);
				
				$unit_type_name=$test_unit_name['unit_type_name'];

?>
<tr>
<td>
<?php echo $product_name_to; ?>
</td>
<td>
<?php 
if($carton <> 0 || $packate <> 0) { 
echo ($when_carton); 
}
else {
echo ($quantity_show); 

echo " $unit_type_name";
}
?>
</td>
<td style="text-align: right;">
<?php echo ($selling_price) ; ?>/-
</td>
<td style="text-align: right;">
<?php echo ($per_total) ; ?>/-
</td>

</tr>
<?php 
$grand_total ;
}



	$result_for_previous = mysqli_query($connection,"SELECT SUM(debit) as cusomer_due FROM transaction WHERE customer_id ='$customer_id' AND ledger='9' and payment_type='due' ");
			
		$test_for_previous = mysqli_fetch_array($result_for_previous);
				
				$cusomer_due=$test_for_previous['cusomer_due'];
				
			
				
				
					$result_for_previous = mysqli_query($connection,"SELECT SUM(debit) as cusomer_due_payment FROM transaction WHERE customer_id ='$customer_id' AND ledger='7' ");
			
		$test_for_previous = mysqli_fetch_array($result_for_previous);
				
				$cusomer_due_payment=$test_for_previous['cusomer_due_payment'];
				
				$this_customer_due = $cusomer_due - $cusomer_due_payment ;
				
				
				
		$final_grand_total = (($grand_total+$transport_cost+$tax) - $discount);	
		
		$today_due = ($final_grand_total-$payment);
		
		$beforedue = $this_customer_due - $today_due ;
				
 ?>
  <tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-bottom:hidden;">
 Total: 
 </th>
 <th style="text-align: right;">
 <?php echo ($grand_total); ?>/-
 </th>
 </tr>
  <tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-bottom:hidden;">
 Today's Due : 
 </th>
 <th style="text-align: right;">
 <?php echo $today_due; ?>/-
 </th>
 </tr>
 <tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-bottom:hidden;">
 Previous Due: 
 </th>
 <th style="text-align: right;">
 <?php echo ($beforedue); ?>/-
 </th>
 </tr>

  <tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-bottom:hidden;">
 Discount: 
 </th>
 <th style="text-align: right;">
 <?php echo $discount; ?>/-
 </th>
 </tr>
 <tr>
 <th colspan="3" style="text-align: right; border-left:hidden;">
 Grand Total : 
 </th>
 <th style="text-align: right;">
 <?php echo $final_grand_total + $beforedue ; ?>/-
 </th>
 </tr>
 <tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-top:hidden; border-bottom:hidden;">
  Payment: 
 </th>
 <th style="text-align: right;">
 <?php echo ($payment) ; ?> /-
 </th>
 </tr>
 <tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-top:hidden; border-bottom:hidden;">
  Total Due: 
 </th>
 <th style="text-align: right;">
 <?php echo ($this_customer_due) ; ?> /-
 </th>
 </tr>

 <!--<tr>
 <th colspan="3" style="text-align: right; border-left:hidden; border-bottom:hidden;">
 Total Due : 
 </th>
 <th style="text-align: right;">
 <?php //echo ($due + $beforedue); ?>/-
 </th>
 </tr>--->
</table>
<!--<b style="font-size: 35px;">----------------</b>-->
<?php
}
	$result_for_supplier = mysqli_query($connection,"SELECT user_name FROM users WHERE users_id = '$posted_by_id'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				$posted_by=$test_for_supplier['user_name'];	
?>
</td>
</tr>
</table>
					
<!--Exchange to be made within 3 days-->
<br/>
You have to bring this invoice for any furthur query.
<br/>
Thank you for your co-operation.
<table style="width:100%;text-align:center;font-size:12px;">
    	<tr>
					<td colspan="3" style=""><br>
Time: <?php echo $posting_time_date;?>
<br/>
You have to bring this invoice for any furthur query.
<br/>
Thank you for your co-operation.
					</td>
					</tr>
					
					<tr>
					<td style="padding-left:20px;"><br>
					Prepared By:<?php echo $posted_by ;?>
					_______________________<br>
					
					</td>
					<td><br>Authorised Signature
					_______________________<br>
					
					</td>
					<td style="padding-right:20px;"><br>Client Signature
					_______________________<br>
					
					</td>
					
					</tr>
					
				
					
					</table>
</div> 

<script>
window.print()
</script>
		
	<script>
   opener.location.href = opener.location.href;
</script>
		


