<?php
require('validate.php');
require('functions.php');
$getpage =$_SERVER['QUERY_STRING']; 
//echo $getpage;
$explode= explode('=',$getpage);
$refno=$explode[1];

$query="SELECT order_,tran_date,ov_amount,trans_no,type FROM ".$prefix."debtor_trans WHERE reference='$refno'";
//echo $query;exit;
$result=mysqli_query($linkid,$query);
$myorder=mysqli_fetch_row($result);
$orderno=$myorder[0];
$otransno=$myorder[3];
$otype=$myorder[4];
$dateval=explode("-",$myorder[1]);
$orderdate=date("d-m-Y",mktime(0,0,0,$dateval[1],$dateval[2],$dateval[0]));


$query="SELECT order_no,reference,ord_date,contact_email,deliver_to,delivery_date,total,a.payment_terms,b.debtor_no,b.debtor_ref AS phone,b.name,b.address,b.notes FROM ".$prefix."sales_orders AS a,".$prefix."debtors_master AS b WHERE a.debtor_no=b.debtor_no  AND a.order_no=$orderno";
//echo $query;exit;
$resultType=mysqli_query($linkid,$query);
$orderData=mysqli_fetch_array($resultType);
$debtor=$orderData['debtor_no'];
$salesperson=$orderData['payment_terms'];

$dateval=explode("-",$orderData['delivery_date']);
$deliverydate=date("d-m-Y",mktime(0,0,0,$dateval[1],$dateval[2],$dateval[0]));

$query="SELECT  DISTINCT  terms FROM ".$prefix."sales_orders AS a,".$prefix."payment_terms AS b WHERE a.payment_terms=b.terms_indicator AND a.payment_terms=$salesperson";
//echo $query;exit; 
$resultType=mysqli_query($linkid,$query);
$salesData=mysqli_fetch_array($resultType);
$salesman=$salesData['terms'];

//$query="SELECT stk_code,unit_price,quantity,discount_percent AS discount,long_description FROM ".$prefix."sales_order_details AS a,".$prefix."stock_master AS b WHERE a.stk_code=b.stock_id AND a.order_no=$orderno";
$query="SELECT  stock_id,description,unit_price,quantity,discount_percent AS discount FROM ".$prefix."debtor_trans_details WHERE debtor_trans_no='$otransno' AND debtor_trans_type='$otype' AND quantity > 0";
$resultOrder=mysqli_query($linkid,$query);
$resultDiscount=mysqli_query($linkid,$query);
$getDiscount=mysqli_fetch_row($resultDiscount);

$cphone=$orderData['phone'];
if($cphone!="" || !empty($cphone)){
$query="SELECT phone2 FROM ".$prefix."crm_persons WHERE name='$cphone'";
//echo $query;exit;
$result=mysqli_query($linkid,$query);
$getPhone=mysqli_fetch_row($result);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>&nbsp;&nbsp;&nbsp;Delivery Challan</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Bootstrap framework -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="bootstrap/css/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />
<link rel="shortcut icon" href="img/favicon.ico" />
<style type="text/css">
/*
.tableborder { border-collapse: collapse; border : 1px solid #CCC; }

.tableborder td,th { border-left: 1px solid #CCC; }

.tableborder th { border-bottom: 1px solid #CCC; }

.tableborder td:first-child { border-left: none; }*/

</style>
</head>
<body>
<table width="65%" border="1" align="left" style="border:1px #999 hidden;font-size:11px">
  <tr>
    <td><table width="100%" border="0" align="left">
        <tr>
          <td width="65%" align="left"><table width="100%" border="0" align="left">
              <!--<tr>
                <td colspan="2" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><?php echo "Premium-".$refno;?></strong>&nbsp;&nbsp;&nbsp;</td>
                <td width="65%" style="padding-left:80px;"><strong>Date :</strong> <?php echo $orderdate;?></td>
              </tr>-->
			   <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
			  <tr>
                <td><strong>No: <?php echo "PREM-".$refno;?></strong></td>
                <td align="left">:</td>
                <td>Date: <?php echo $orderdate;?></td>
              </tr>
              <tr>
               <td><strong>Customer Name<strong></td>
                <td align="left">:</td>
                <td><?php echo $orderData['name'];?></td>
              </tr>
              <tr>
                 <td><strong>Address</strong></td>
                <td align="left">:</td>
                <td><?php echo $orderData['address'];?></td>
              </tr>
              <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
              
			  <tr>
                <td><strong>Contact No</strong> </td>
                <td>:</td>
                <td><?php echo $orderData['phone'];?></td>
              </tr>
              <tr>
                <td><strong>Sales Person</strong></td>
                <td>:</td>
                <td><?php echo $salesman;?></td>
              </tr>      
            </table></td>
          <td width="40%" align="right">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2" width="100%" valign="top" style="height:438px;min-height:280px; margin-bottom:6px position:relative;">
		  <table width="100%" cellpadding="6" cellspacing="6">
              <tr>
                <th width="5%" align="center">SL.</strong></th>
                <th width="41%" align="left"><strong>Article &amp; description </strong></th>
                <th width="12%" align="center"><strong>Net Price</strong></th>
                <th width="12%" align="right"><strong>PKT</strong></th>
				<th width="12%" align="center"><strong>Qty.</strong></th>
				<?php if($getDiscount[4] > 0){?>
				<th width="6%" align="center"><strong>Disc.</strong></th>
				<?php
				}
				?>	
                <th width="12%" align="right"><strong>Amount(Tk.)</strong></th>
              </tr>
			  <?php

			  if($resultOrder)

			  { $i=0;

			  while($mydata=mysqli_fetch_assoc($resultOrder))

			  {		  

			  $i+=1;
			   $netorder=$mydata['unit_price'] * $mydata['quantity'];
			   $discount= $netorder * $mydata['discount'];
			   $totalOrder=$netorder-$discount;	
			  
			  ?>
              <tr>
                <td align="center" valign="top"><?php echo $i;?></td>
                <td align="left" valign="top" ><?php echo $mydata['stock_id'];?><br>
                  <small><?php echo $mydata['description'];?></small></td>
                <td align="center" valign="top" ><?php echo $mydata['unit_price'];?></td>
                <td align="right" valign="top" >&nbsp;</td>
				<td align="center" valign="top" ><?php echo $mydata['quantity'];?></td>
				<?php if($getDiscount[4] > 0){?>
				<td align="center" valign="top" ><?php echo $mydata['discount']*100;?>%</td>
				<?php
				}
				?>	
                <td align="right"  valign="top"><?php echo number_format($totalOrder,2);?></td>
				
              </tr>
              <?php

			  }

			  }

			  ?>
              <tr>
			   <?php if($getDiscount[4] > 0){?>
                <td align="right" colspan="6"><b>Amount(VAT Inclusive)</b></td>
				<?php 
				}else{
				?>
				<td align="right" colspan="5"><b>Amount(VAT Inclusive)</b></td>
				<?php 
				}
				?>
                
                <td align="right"><strong><?php echo number_format($myorder[2],2);?></strong></td>
              </tr>
              <tr>
			   <?php if($getDiscount[4] > 0){?>
                <td align="right" colspan="6"><b>Invoice Amount</b></td>
				<?php 
				}else{
				?>
				<td align="right" colspan="5"><b>Invoice Amount</b></td>
				<?php 
				}
				?>
                
                <td align="right"><strong><?php echo number_format($myorder[2],2);?></strong></td>
              </tr>
            </table></td>
        </tr>
       <!-- <tr>
          <td colspan="2" align="left">
		  <div style="min-height:280px;">&nbsp;</div>
		  </td>
        </tr>  -->  
	    <tr>
          <td colspan="2" align="left" valign="top"> <b> Amount In Words :</b>
            <?php  $num =$myorder[2];
			 $test = convertNumber($num);
			 echo ucwords($test)." Only";?>
          </td>
        </tr>
        <tr>
          <td colspan="2" valign="top"><table width="100%" border="0"  style="line-height:15px;">
              <tr>
                <td colspan="2" width="100%"><table width="80%" border="0">
                    <tr>
                      <td width="30%"><strong>Delivery Place</strong> : </td>
                      <td width="70%" align="left"><?php echo $orderData['deliver_to'];?></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                        <td><?php if($orderData['notes']=="")
					  echo $orderData['address'];
					  else
					  echo $orderData['notes'];?></td>
                    </tr>
					 <tr>
                      <td>&nbsp;</td>
                      <td><?php echo $orderData['phone'];?><?php if($getPhone[0]!="") echo ", ".$getPhone[0];?> </td>
                    </tr>
                  </table></td>
              </tr>
               <tr>
                   <td valign="top" style="font-size:10px">Transporter : </td>
                   <td valign="top" style="font-size:10px">Truck/vehicle no: </td>
              </tr>
			  <tr>
                <td colspan="2" valign="top" style="font-size:9px">
                  <strong>Terms & Conditions</strong>: Payment to be made in Cash/DD/TT or by A/C payee  Cheque/Payorder. Goods to be delivered on realization of Cheque/Payorder. Goods once sold cannot be returned/exchanged except for manufacturing defects. Which must be brought to our notice within 7 days from the date of delivery. Please preserve this document to get any kind of after sales service.</td>
              </tr>
              <tr>
                <td colspan="2" valign="top"><table width="100%" border="0" style="font-size:10px">
                    <tr>
                      <td>
                        ....................................................<br>
                        <strong>Customer's Signature</strong></td>
                      <td align="center">
                        ....................................................<br>
                        <strong>Checked by</strong></td>
                      <td align="center">
                        ....................................................<br>
                        <strong>Approved by</strong></td>
						<td align="center">
                        ....................................................<br>
                        <strong>Security</strong></td>
                    </tr>
                  </table></td>
              </tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>
