<?php
    session_start();
    include ('../common/library.php');  
    include ('../common/helper.php'); 
    $sale_no = $_REQUEST['sale_no'];
    
    $sql = $conn->query("select * from sales_master where group_for=".$_SESSION['group_for']."  and s_no='".$sale_no."'");
    $s_data = $sql->fetch_assoc();
    
    if($s_data['dealer_code'] > 0){
        $dealer_name=find_a_field('customer_info','business_name','id='.$s_data['dealer_code']);
        $dealer_mobile=find_a_field('customer_info','mobile_no','id='.$s_data['dealer_code']); 
        $dealer_address=find_a_field('customer_info','customer_address','id='.$s_data['dealer_code']);
    }
    else {
        $dealer_name=$s_data['dealer_name'];
        $dealer_mobile=$s_data['dealer_address'];
        $dealer_address=$s_data['contact'];
    }
    
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Busicus Demo</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <link rel="stylesheet" href="../assets/css/bootstrap.min.css">
  <link rel="stylesheet" href="../assets/css/atlantis.min.css">
  

  
  <style>
      body{
          background-color:#fff;
          color:#000;
      }
      .logo img{
          width:100%;
      }
  </style>
  <script>
		
		function printpage() {
       
        var printButton = document.getElementById("printpagebutton");
       
        printButton.style.visibility = 'hidden';
        
        window.print()
        printButton.style.visibility = 'visible';
    }
		
			
		
	</script>
  
</head>
<body>
    <div class="container mt-1">
        <div class="">
		<div class="col text-center">
			<input  id="printpagebutton" type="button" value="print" onClick="printpage()"/>
		</div>
	</div>
        <div class="row mb-4">
            <div class="col-6">
                <div class="logo">
				
			
                <img src="../images/logo.png" style="height: 80px;
    width: 235px;
    padding: 6px;"  alt="Busicus" class="navbar-brand"><br>
                <address>
                819/A, Section 2<br>
                Mirpur, Dhaka - 1216.<br>
                Contact : +8801620615052
                </address>
				
				
			</div>
            </div>
            <div class="col-6 text-right">
			
			    <div>			
				    <h1>INVOICE</h1>					
			    </div>
			
			</div>
        </div>
        <div class="row">
            <div class="col-6">
                <div class="row">
                    <div class="col-6">
                        <b>Customer Name:</b>
                    </div>
                    <div class="col-6"><p><?php echo $dealer_name;?></p></div>
                </div>
                <div class="row">
                    <div class="col-6">
                        <b>Address:</b>
                    </div>
                    <div class="col-6"><p><?php echo $dealer_address;?></p></div>
                </div>
                
				
            </div>
            <div class="col-6 text-right">
                <div class="row">
				
                    <div class="col-12">
                        <p> <b>Invoice No : <?php echo $s_data['s_no'];?></b></p>
                </div>
            </div>
			<div class="row">
				
                    <div class="col-12">
                        <p> <b>Invoice Date : <?php echo $s_data['sales_date'];?></b></p>
                </div>
            </div>
			<div class="row">
				
                    <div class="col-12">
                        <p> <b>Delivery Date : <?php echo $s_data['sales_date'];?></b></p>
                </div>
            </div>
			
        </div>
    </div>
    <div class="row mb-2">
        <table border="1" width="100%" style="margin:0 auto" cellpadding="5">
            <thead>
                <tr>
                    <th>Item Code</th>
                    <th>Item Description</th>
                    <th>Rate</th>
                    <th>Qty</th>
                    <th class="text-right">Amount</th>
                </tr>
            </thead>
            <tbody>
                
            <? $s_details = $conn->query("select * from sales_order where group_for=".$_SESSION['group_for']."  and  s_no=".$sale_no."");
                while($data = $s_details->fetch_assoc()){
                    
                ?>
                <tr>
                <td><?php echo $data['item_id']?></td>
                    <td><?php echo find_a_field('item_info','item_name','item_id='.$data['item_id']) ?></td>
                    <td><?php echo $data['rate']?></td>
                    
                    <td><?php echo $data['qty']?></td>
                    
                    <td class="text-right"><?php echo $data['amount']; $tot_amt += $data['amount'];  ?></td>
                </tr>
                <?php } ?>
            </tbody>
            <tfoot class="text-right">
                <tr>
                    <td colspan="4">Sub Total</td>
                    <td><?php echo number_format($tot_amt,2);?></td>
                </tr>   
                <tr>
                    <td colspan="4">Discount</td>
                    
                    <td><?php
                        $discount_amt = find_a_field('sales_master','discount','s_no='.$sale_no);
                    echo number_format($discount_amt,2);?></td>
                </tr>
                <tr>
                    <td colspan="4">Total</td>
                    <td><?php echo number_format($sales_amt = $tot_amt-$discount_amt,2);?></td>
                </tr>
            </tfoot>
        </table>
    </div>
    <div class="row mb-2">
        <div class="col text-center">
            <h3><u>Payment Details</u></h3>
        </div>
    </div>
    <div class="row mb-5">
        <table border="1" width="100%" style="margin:0 auto"cellpadding="5">
            <thead>
                <tr>
                    <th>Collection Amount</th>

                    <th>Type</th>
                    <th class="text-right">Amount</th>
                </tr>
            </thead>
            <tbody>
                
            <?php
					$select =$conn->query("select a.ledger_id,b.discount from customer_info a, sales_master b where a.id=b.dealer_code and s_no='".$sale_no."'");
                                                $i = 1;
											 while($c_data = $select->fetch_assoc()){

						//$sales_amount = find_a_field('journal','sum(dr_amt)','ledger_id='.$c_data['ledger_id']);
						
                        //$collection_amount = find_a_field('journal','sum(cr_amt)','ledger_id='.$c_data['ledger_id']);
                        $collection_amount = $s_data['collection'];
                        //$due_amount = ($sales_amount-$collection_amount);
                        
                        $due_amount = $s_data['dues'];

                        if($due_amount>0){
											 ?>
                <tr>
                    <td><?=$collection_amount;?></td>
                    <td></td>
                    <td class="text-right"><?=$due_amount;?></td>
                </tr>
                <?php } }?>
            </tbody>
            <tfoot class="text-right">
              
                <tr>
                    <td colspan="2">Due Amount</td>
                    <td><?php echo  number_format($due_amount,2); ?></td>
                </tr>
            </tfoot>
        </table>
    </div>
    <hr>
						<div>
<p><strong>TERMS & CONDITION:</strong></p>
<p style="font-size:12px">Advance Payment Non-refundable<br>
</div>
    <div class="row pt-5 text-center">
					<div class="col-4">
						<br><br><br><br><br>
						<hr class="w-50">
						<p>Received By</p>
					</div>
					<div class="col-4">
						
					</div>
					<div class="col-4">
						
						<br><br><br><br><br>
						<hr class="w-50">
						<p>Authorized By</p>
					</div>
				</div>
</body>
</html>
