<?php



require_once 'pdf.php';

 

 include('db_con.php');

 $html = '';

 $fdate = $_GET['fdate'];

 

 $tdate = $_GET['tdate'];



 $branch_id = $_GET['branch_id'];



  $sm_id = $_GET['sm_id'];

 

 

 $count = 0;



 $tot_sale = 0;

 $tot_return = 0;

 $total = 0;



 $sm_name = '';



 $sql = "select sname from sales_man where sm_id = '".$sm_id."'";



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



 $statement->execute();

  $sm_name = $statement->fetchColumn();







 













 



 $statement = $connect->prepare("

   select a.id,a.dated,a.branch_id,a.name,a.sale,a.sreturn from(

  select m.s_id as id, m.dated, m.branch_id,c.name,sum(d.qty) as sale, 0 as sreturn From

  sale m,sale_detail d,customers c where m.s_id=d.s_id and m.cust_id=c.cust_id and m.dated between :fdate and :tdate and m.branch_id = :branch_id and m.sm_id = :sm_id group by id, dated, name 

  union

 select m.sr_id as id, m.dated, m.branch_id, c.name,0 as sale, sum(d.qty) as sreturn From

  sr m,sr_detail d,customers c where m.sr_id=d.sr_id and m.cust_id=c.cust_id and m.dated between :fdate and :tdate and m.branch_id = :branch_id and m.sm_id = :sm_id group by id, dated, name 

 

  )a order by dated;");

 $statement->execute(

  array(

   ':branch_id'       =>  $branch_id,

   ':sm_id'       =>  $sm_id,

   ':fdate'       =>  $fdate,

   ':tdate'       =>  $tdate

  )

 );





 if($branch_id ==0)

 {

  $statement = $connect->prepare("

   select a.id,a.dated,a.branch_id,a.name,a.sale,a.sreturn from(

  select m.s_id as id, m.dated,m.branch_id, c.name,sum(d.qty) as sale, 0 as sreturn From

  sale m,sale_detail d,customers c where m.s_id=d.s_id and m.cust_id=c.cust_id and m.dated between :fdate and :tdate and m.sm_id = :sm_id group by id, dated, name 

  union

 select m.sr_id as id, m.dated, m.branch_id, c.name,0 as sale, sum(d.qty) as sreturn From

  sr m,sr_detail d,customers c where m.sr_id=d.sr_id and m.cust_id=c.cust_id and m.dated between :fdate and :tdate  and m.sm_id = :sm_id group by id, dated, name 

 

  )a order by dated;");

 $statement->execute(

  array(

   

   ':sm_id'       =>  $sm_id,

   ':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:10px;text-align:right;}

  </style>



  <div id="header">



  <table width="100%">

  <tr>

  <td width="20%">Sales Man Report</td>

  <td width="15%">Sales Man :</td><td width="20%" class="smallfont">'.$sm_name.'</td>

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

  <td width="7%"></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="4%">#</th>

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

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

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

  

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

  

  

  <th width="10%" class="rightside">Sale</th>

  <th width="10%" class="rightside">Return</th>

  <th width="10%" class="rightside">Total</th>







  </tr>



  </table>



  </div>

  

';

foreach($result as $row)

 {

  $sale = $row["sale"];

  $return = $row["sreturn"];

  

  $count = $count+1;

  $tot_sale = $tot_sale + $sale;

  $tot_return = $tot_return + $return;

  $total = $tot_sale - $tot_return;





 

  

    

  

  



$html .= '

  <div id="content">

  

  <table width="100%">

<tr>

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

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

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

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



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



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

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

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

  

   

    

   </tr>

   

  </table>





  </div>



  <div id="footer">

  <hr />

  <table width="100%">

  <tr>

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

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

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

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

  

  

  </tr>

  </table>

    

  </div>



        ';



}

 $pdf = new Pdf();



 $file_name = 'Sales_Man_Report.pdf';

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



 $pdf->loadHtml($html);

 $pdf->render();

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



?>