<?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];



$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";
$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";
//echo $query;exit;
$resultOrder=mysqli_query($linkid,$query);
$resultOrder2=mysqli_query($linkid,$query);
$resultDiscount=mysqli_query($linkid,$query);
$getDiscount=mysqli_fetch_row($resultDiscount);

$query="SELECT  SUM(unit_price * quantity),SUM(quantity),SUM((unit_price * quantity *.075)/1.075),SUM(unit_price * quantity * discount_percent) FROM ".$prefix."debtor_trans_details WHERE debtor_trans_no='$otransno' AND debtor_trans_type='$otype' AND quantity > 0";
//echo $query;exit;
$resultSUM=mysqli_query($linkid,$query);
$getSumRows=mysqli_fetch_row($resultSUM);

$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);
}

$query="SELECT reference,challan_date FROM ".$prefix2."order_vat_challan WHERE invoiceid='$refno'";
$result=mysqli_query($linkid,$query);
$getchallan=mysqli_fetch_array($result);
$dateval=explode("-",$getchallan['challan_date']);
$transdate=date("d-M-Y",mktime(0,0,0,$dateval[1],$dateval[2],$dateval[0]));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>&nbsp;&nbsp;&nbsp;</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="100%" border="1" align="left" style="border:1px #999 hidden;font-size:13px">
  <tr>
    <td><table width="100%" border="0" align="left">
        <tr>
          <td width="80%" align="left"><table width="100%" border="0" align="left">
              <tr>
                <td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                <td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
              </tr>
              <tr>
                <td colspan="3" align="right">&nbsp;&nbsp;</td>
              </tr>
              <tr>
                <td width="25%">&nbsp;</td>
                <td width="4%">&nbsp;</td>
                <td align="right" style="font-size:16px;"><strong>ডিস্ট্রিবিউটর</strong></td>
              </tr>
              <tr>
                <td width="25%">&nbsp;</td>
                <td width="4%">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
			  <tr>
                <td width="25%">&nbsp;</td>
                <td width="4%">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td align="left"><?php echo $orderData['name'];?></td>
              </tr>
			  <tr>
                <td width="25%">&nbsp;</td>
                <td width="4%">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td align="left"><?php echo $orderData['address'];?></td>
              </tr>
              <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
			  <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
			  <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
			  <tr>
                <td colspan="3">&nbsp;</td>
              </tr>			  
              <tr>
                <td align="left"><strong>Sales Person</strong></td>
                <td>:</td>
                <td align="left"><?php echo $salesman;?></td>
              </tr>
			  <tr>
                <td colspan="3">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="3">&nbsp;</td>
              </tr>              
            </table></td>
          <td width="20%" align="right" valign="top"><table width="100%" border="0" align="right">
              <tr>
                <td align="right">&nbsp;</td>
				<td align="left"><strong>VAT CHALLAN</strong></td>
              </tr>
              <tr>
			    <td align="right">&nbsp;</td>
                <td align="left" style="font-size:16px;"><strong>মূসক - ৬.৩</strong></td>
              </tr>
			  <tr>
			    <td align="right" style="font-size:16px;"><strong>:</strong></td>
                <td align="left"  style="font-size:16px;"><strong>প্রিমিয়াম কর্পোরেশন</strong></td>
              </tr>
          
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2">&nbsp;</td>
              </tr>
             <!-- <tr>
                <td colspan="2" align="left"><b>19121065032</b></td>
              </tr>-->
              <tr>
                <td colspan="2" align="left"><?php echo $getchallan['reference'];?></td>
              </tr>
              <tr>
                <td colspan="2" align="left"><?php echo $transdate;?></td>
              </tr>
              <tr>
                <td colspan="2" align="left"><?php echo date("h:i A");?></td>
              </tr>
            </table></td>
        </tr>
        <tr>
          <td colspan="2" valign="top" style="height:400px;min-height:230px; margin-bottom:10px position:relative;"><table width="100%" cellpadding="10" cellspacing="10">
              <tr>
                <td colspan="8">&nbsp;</td>
              </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;	
			  
			   $getvat=($totalOrder *.075)/1.075;
//$getvat=($totalOrder *.063)/1.063;
			   $getbalance=$totalOrder - $getvat;
			  ?>
              <tr style="line-height:12px;">
                <td align="center"  valign="top" width="5%"><?php echo $i;?></td>
                <td align="left" valign="top" width="35%" ><?php echo $mydata['stock_id'];?><br>
                  <small><?php echo $mydata['description'];?></small></td>
                <td align="right" valign="top" width="12%"><?php echo $mydata['quantity'];?></td>
                <td align="right" valign="top" width="10%" ><?php echo $totalOrder;?></td>
                <td align="right" valign="top" width="5%">&nbsp;</td>
                <td align="right" valign="top" width="10%"><?php echo number_format($getbalance,2);?></td>
                <td align="right" valign="top" width="8%"><?php echo number_format($getvat,2);?></td>
                <td align="right"  valign="top" width="15%"><?php echo number_format($totalOrder,2);?></td>
              </tr>
              <?php

			  }

			  }

			  ?>
            </table></td>
        </tr>
        <tr>
          <td colspan="2" valign="top"><table width="100%" >
              <tr>
                <td align="center"  valign="top" width="5%">&nbsp;</td>
                <td align="left" valign="top" width="35%" >&nbsp;</td>
                <td align="right" valign="top" width="12%"><?php echo $getSumRows[1];?></td>
                <td align="right" valign="top" width="10%"><?php echo $getSumRows[0] - $getSumRows[3];?></td>
                <td align="right" valign="top" width="5%">&nbsp;</td>
                <td align="right" valign="top" width="10%"><?php echo number_format(($getSumRows[0] - $getSumRows[3])- (($getSumRows[0] - $getSumRows[3])*0.075)/1.075,2);?></td>
                <td align="right" valign="top" width="8%"><?php echo number_format((($getSumRows[0] - $getSumRows[3])*0.075)/1.075,2);?></td>
                <td align="right"  valign="top" width="15%"><?php echo number_format($getSumRows[0] - $getSumRows[3],2);?></td>
              </tr>
            </table></td>
        </tr>
        <tr>
          <td>&nbsp; </td>
        </tr>
		<tr>
          <td colspan="2" align="left" valign="top" style="padding-left:150px;">
            <?php  $num =$myorder[2];
			 $test = convertNumber($num);
			 echo ucwords($test)." Only";?>
          </td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>