<?php



require_once 'pdf.php';

 

 include('db_con.php');

 $html = '';

 $fdate = $_GET['fdate'];

 

 $tdate = $_GET['tdate'];



 $item_no = $_GET['item_no'];

 $branch_id = $_GET['branch_id'];

 $sm_id = $_GET['sm_id'];

 

 

 $count = 0;



 $tot_amount = 0;

 $tot_discount = 0;

 $tot_cost = 0;

 $tot_profit = 0;

 $item_name = '';

 $tot_qty = 0;



 

   $sql = "SELECT item_name FROM items where item_no ='".$item_no."'";

                 

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

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

                $item_name = $statement->fetchColumn();







if($branch_id == 0)

{







  $statement = $connect->prepare("select m.s_id as id,m.inv_no,m.dated,c.name,b.branch_name,d.qty,sum(d.qty*d.rate) as amount,sum(d.qty*d.cost)as cost,sum(d.dis_amount) as discount

    from sale m,sale_detail d,customers c,branch b 

    where m.s_id=d.s_id and m.branch_id=b.branch_id and m.cust_id=c.cust_id and d.item_no = :item_no and m.dated between :fdate and :tdate group by id,dated,name,branch_name  order by dated,id");

  if($sm_id > 0)

  {

    $statement = $connect->prepare("select m.s_id as id,m.inv_no,m.dated,c.name,b.branch_name,d.qty,sum(d.qty*d.rate) as amount,sum(d.qty*d.cost)as cost,sum(d.dis_amount) as discount

    from sale m,sale_detail d,customers c,branch b 

    where m.s_id=d.s_id and m.branch_id=b.branch_id and m.cust_id=c.cust_id and d.item_no = :item_no and m.sm_id = '".$sm_id."' and m.dated between :fdate and :tdate group by id,dated,name,branch_name  order by dated,id");

  

  }



 $statement->execute(

  array(

   ':item_no'     => $item_no,

   ':fdate'       =>  $fdate,

   ':tdate'       =>  $tdate

  )

 );

} 







if($branch_id > 0)

{

  $statement = $connect->prepare("select m.s_id as id,m.inv_no,m.dated,c.name,b.branch_name,d.qty,sum(d.qty*d.rate) as amount,sum(d.qty*d.cost)as cost,sum(d.dis_amount) as discount

    from sale m,sale_detail d,customers c,branch b 

    where m.s_id=d.s_id and m.branch_id=b.branch_id and m.cust_id=c.cust_id and m.branch_id = '".$branch_id."' and d.item_no = :item_no and m.dated between :fdate and :tdate group by id,dated,name,branch_name  order by dated,id");

  

  if($sm_id > 0)

  {

    $statement = $connect->prepare("select m.s_id as id,m.inv_no,m.dated,c.name,b.branch_name,d.qty,sum(d.qty*d.rate) as amount,sum(d.qty*d.cost)as cost,sum(d.dis_amount) as discount

    from sale m,sale_detail d,customers c,branch b 

    where m.s_id=d.s_id and m.branch_id=b.branch_id and m.cust_id=c.cust_id and m.branch_id = '".$branch_id."' and m.sm_id='".$sm_id."' and d.item_no = :item_no and m.dated between :fdate and :tdate group by id,dated,name,branch_name  order by dated,id");

  

  }

  $statement->execute(

  array(

   ':item_no'     => $item_no,

   ':fdate'       =>  $fdate,

   ':tdate'       =>  $tdate

   )

 );



}











             

 $result = $statement->fetchAll(); 



 $html .=' <style>

    @page { margin:80px 20px; }

    @page {margin-bottom: 40px;}

    #header { position: fixed; left: 0px; top: -60px; right: 0px; height: 60px; text-align: center; }

    #footer { position: fixed; left: 0px; bottom: -20px; right: 0px; height: 50px; background-color: #fff; }

    #content {margin:0;font-size:13px;font-weight:normal;}

    .rightside{text-align:right;font-size:12px;}

    #footer .page:after { content: counter(page); }

    

    #table_header th {

    border-top:1px solid black;

    border-bottom:1px solid black;

  }

  .smallfont{font-size:12px;text-align:right;}

  </style>



  <div id="header">



  <table width="100%">

  <tr>

  <td width="15%">Sale Report of item </td><td width="35%">'.$item_name.'</td>

  <td width="5%" class="rightside">From :</td><td width="12%" class="rightside">'.$fdate.'</td>

  <td width="10%"></td><td width="8%" class="rightside">To :</td><td width="15%" class="rightside">'.$tdate.'</td>

  </tr>

  

  </table>

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



  <tr>

  <th width="2%">#</th>

  <th width="4%">ID</th>

  <th width="4%">InvNo</th>

  <th width="8%">Date</th> 

  <th width="10%">Branch</th> 

  <th width="42%">Customer</th>

  

  <th width="6%" class="rightside">Qty</th>

  <th width="6%" class="rightside">Amount</th>

  <th width="6%" class="rightside">Discount</th>

  <th width="6%" class="rightside">Cost</th>

  <th width="6%" class="rightside">Profit</th>







  </tr>



  </table>



  </div>

  

';

foreach($result as $row)

 {

  $amount = $row["amount"];

  $discount = $row["discount"];

  $cost = $row["cost"];

  $qty = $row["qty"];

  $profit = $amount - $discount -$cost;



  $count = $count+1;



  $tot_amount = $tot_amount + $amount;



  $tot_discount = $tot_discount + $discount;



  $tot_cost = $tot_cost + $cost;



  $tot_profit = $tot_amount - $tot_discount - $tot_cost;



  $tot_qty = $tot_qty +$qty;







 

  

    

  

  



$html .= '

  <div id="content">

  

  <table width="100%">

<tr>

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

  <td width="4%">'.$row["id"].'</td>

  <td width="4%">'.$row["inv_no"].'</td>

<td width="8%">'.$row["dated"].'</td>

<td width="10%">'.$row["branch_name"].'</td>

<td width="42%">'.$row["name"].'</td>

<td width="6%" class="rightside">'.number_format($qty, 2).'</td>

<td width="6%" class="rightside">'.number_format($amount, 2).'</td>

   <td width="6%" class="rightside">'.number_format($discount, 2).'</td>

    <td width="6%" class="rightside">'.number_format($cost, 2).'</td>

    <td width="6%" class="rightside">'.number_format($profit, 2).'</td>

  

   

    

   </tr>

   

  </table>





  </div>



  <div id="footer">

  <hr />

  <table width="100%">

  <tr>

  <td width="70%" class="rightside">Total : </td>

  <td width="6%" class="rightside">'.number_format($tot_qty, 2).'</td>

  <td width="6%" class="rightside">'.number_format($tot_amount, 3).'</td>

  <td width="6%" class="rightside">'.number_format($tot_discount, 3).'</td>

  <td width="6%" class="rightside">'.number_format($tot_cost, 3).'</td>

  <td width="6%" class="rightside">'.number_format($tot_profit, 3).'</td>

  </tr>

  </table>

    

  </div>



        ';



}

 $pdf = new Pdf();



 $file_name = 'Sale_Report_of_item.pdf';

 $pdf->setPaper('A4', 'landscape');



 $pdf->loadHtml($html);

 $pdf->render();

 $pdf->stream($file_name, array("Attachment" => false));



?>