<?php















 include('function.php');







 







 include('db_con.php');







 //stock_update($connect);







 if (isset($_SESSION['user_id'])) {















        $user_type = $_SESSION['user_type'];







        $user_id = $_SESSION['user_id'];







        $user_name = $_SESSION['user_name'];







        $branch_id = $_SESSION['branch_id'];







        $branch_name = $_SESSION['branch_name'];







        $store_id = $_SESSION['store_id'];







        $store_name = $_SESSION['store_name'];







}else{







    header('Location: login');







    die();







}







 $html = '';







 







 $count = 0;















 $gtot_qty = 0;







 $gtot_ctn = 0;







 $gtot_amount  = 0;























 















  $fdate = $_GET['fdate'];







  $tdate = $_GET['tdate'];







  















$sv_id = $_GET['sv_id'];















$dated= date('Y-m-d');







































    















$acc_name = 'ALL';







































  $statement = $connect->prepare("select m.p_id,m.inv_no,DATE_FORMAT(m.dated,'%d-%m-%Y') dated ,v.sv_name name,m.discount,m.freight,m.gst_pr,m.discount_dsc,m.freight_dsc from purchase m , sub_vendors v 







    where v.sv_id=m.sv_id and m.dated between :fdate and :tdate and m.sv_id LIKE '%".$sv_id."%' 







    order by p_id");







 $statement->execute(







  array(







   







   ':fdate'       =>  $fdate,







   ':tdate'       =>  $tdate







  )







 );















$result = $statement->fetchAll();















 ?>















<!DOCTYPE html>







<html>







<head>







  <title>







  







  </title>







  <style>







  .rightside {text-align: right;}







  @media print {







  @page { margin: 0; }







  body { margin: 1.6cm; }







}







  table td{







    







    border-bottom: 1px solid gray;















  }







  .head_row {







    font-weight: bold;







  }







  thead td {







    font-weight: bold;







     border-bottom: 1px solid gray;







      border-top: 1px solid gray;







       background-color: :  gray;















  }







  </style>







</head>







<body>















 















  







  <table id="main" width="100%">







  







  <thead>







<tr>







  <td width="5%" class="font-bold">Sr</td>







  







  <td width="46%" class="font-bold">Item Name</td>







  <td width="10%" class="font-bold">UOM</td>







  







  <td width="7%" class="rightside font-bold">Ctn</td>







  <td width="10%" class="rightside font-bold">Qty</td>







  







  <td width="10%"class="rightside font-bold">Rate</td>







  <td colspan="2" width="10%"class="rightside font-bold">Discount</td>







  <td width="10%" class="rightside font-bold">Amount</td>







 































  </tr>







  </thead>







  















<tbody>















  







<?php















foreach($result as $row)







 {























$p_id = $row["p_id"];















   $count = $count+1;















          $detail_statement = $connect->prepare("select d.item_no,CONCAT(i.item_name,' ',l.color_name,' ',b.brand_name) as item_name,u.uom_name,d.ctn,d.qty,d.rate,d.dis_pr,d.dis_rate,0 as amount from  items i,item_brand b,item_type t,item_color l, purchase_detail d,purchase m,item_uom u,







    sub_vendors v where m.p_id=d.p_id and i.brand_id=b.brand_id and i.color_id=l.color_id and t.type_id=i.type_id and d.item_no=i.item_id and i.uom=u.uom_id and v.sv_id=m.sv_id and







    m.p_id = '".$p_id."'");







         $detail_statement->execute(







                   );







         $detail_result = $detail_statement->fetchAll();























 $sql = "SELECT sum(qty*rate) from purchase_detail where p_id = '".$p_id."'";







           $statement = $connect->prepare($sql);







           $statement->execute(); // no need to add `$sql` here, you can take that out







           $tot_amount = $statement->fetchColumn();







           $sql = "SELECT sum(dis_rate*qty)  from purchase_detail where p_id = '".$p_id."'";







           $statement = $connect->prepare($sql);







           $statement->execute(); // no need to add `$sql` here, you can take that out







           $tot_dis_amount = $statement->fetchColumn();















$sql = "SELECT sum(qty) from purchase_detail where p_id = '".$p_id."'";







           $statement = $connect->prepare($sql);







           $statement->execute(); // no need to add `$sql` here, you can take that out







           $tot_qty = $statement->fetchColumn();















 







  







  echo '







  







<tr class="head_row">







<b>







<td width="5%">'.$count.'</td>







<td colspan="8" width="95%">'.$row["dated"].' , P_ID. '.$row["p_id"].', '.$row["name"].'</td>







  















































</b>







</tr>























   ';







   







 























   $sr = 0;







   $tot_qty = 0;







   $tot_ctn = 0;







   $tot_amount = 0;







   $gst_amount = 0;







   $discount = 0;







   $freight  = 0;







   //$tot_amount = 0;







   //$tot_qty = 0;







   //$tot_dis_amount = 0;







                foreach($detail_result as $detail_row)







                  {















                  //  $gst_pr = $detail_row["gst_pr"];















  $qty = $detail_row["qty"];







  $ctn = $detail_row["ctn"];







  $tot_qty = $tot_qty + $qty;







  $tot_ctn = $tot_ctn + $ctn;







  $rate = $detail_row["rate"];







  $dis_rate = $detail_row["dis_rate"];







  $dis_pr = $detail_row["dis_pr"];







  if($dis_rate > 0)







  {







    $dis_item = $qty * $dis_rate; 







    $dis_type = "PP";







    $dis_given = $dis_rate;







  }else{















    $dis_rate = $rate * $dis_pr / 100;







    $dis_item = $qty * $dis_rate;







    $dis_given = $dis_pr;







    $dis_type="%";}















                      $sr = $sr+1;







                    







                      $dis_amount = $detail_row["dis_rate"];







                      $amount = $qty * $rate - $dis_item;















                      $tot_amount = $tot_amount + $amount;















                     







                      //$tot_amount = $tot_amount + $amount;







                     // $tot_qty = $tot_qty + $qty;







                     // $tot_dis_amount = $tot_dis_amount + $dis_amount;















                echo '







                      <tr>







                       







                    <td width="5%" class="small-font">'.$sr.'</td>  







                    <td width="39%" class="small-font">'.$detail_row["item_name"].'</td>







<td width="7%" class="small-font">'.$detail_row["uom_name"].'</td>







<td width="7%" class="rightside small-font">'.$detail_row["ctn"].'</td>







<td width="8%" class="rightside small-font">'.number_format($qty, 2).'</td>







   <td width="10%" class="rightside small-font">'.number_format($rate, 2).'</td>







   <td width="7%" class="rightside small-font">'.number_format($dis_given, 2).'</td>







   <td width="3%" class="rightside small-font">'.$dis_type.'</td>







   <td width="14%" class="rightside small-font">'.number_format($amount, 2).'</td>







  







   







    







   </tr>';







   }















   $gst_pr  = $row['gst_pr'];







   $discount = $row['discount'];







   $freight = $row['freight'];







   $net_amount = $tot_amount + $gst_amount - $discount + $freight;







   if($gst_pr > 0){$gst_amount = $tot_amount * $gst_pr / 100;}







   echo '







     <tr>







                       







                    







<td colspan="3" width="44%" class="rightside small-font">Total :</td>







<td width="7%" class="rightside small-font">'.number_format($tot_ctn, 0).'</td>







<td width="8%" class="rightside small-font">'.number_format($tot_qty, 2).'</td>







   <td width="10%" class="rightside small-font"></td>







   <td width="7%" class="rightside small-font"></td>







   <td width="3%" class="rightside small-font"></td>







   <td width="14%" class="rightside small-font">'.number_format($tot_amount, 2).'</td>







  







   







    







   </tr>







    <tr>







                       







                    















   <td colspan="8" width="90%" class="rightside small-font">GST Amount : '.$gst_pr.' (%)</td>







   <td width="10%" class="rightside small-font">'.number_format($gst_amount, 2).'</td>







  







   







    







   </tr>







    <tr>







                       







                    















   <td colspan="8" width="90%" class="rightside small-font">'.$row["discount_dsc"].'</td>







   <td width="10%" class="rightside small-font">'.number_format($discount, 2).'</td>







  







   







    







   </tr>







   <tr>







                       







                    















   <td colspan="8" width="90%" class="rightside small-font">'.$row["freight_dsc"].'</td>







   <td width="10%" class="rightside small-font">'.number_format($freight, 2).'</td>







  







   







    







   </tr>







   <tr>







                       







                    















   <td colspan="8" width="90%" class="rightside small-font">Net Amount :</td>







   <td width="10%" class="rightside small-font">'.number_format($net_amount, 2).'</td>







  







   







    







   </tr>







   ';















 }























?>















</tbody>







  







  







</tfoot>







 







 </table><!--end main table-->















    







  







 </body>







 </html>