<?php


require_once 'pdf.php';


 


 include('db_con.php');


 $html = '';


 


 $s_id = $_GET['id'];


 $tot_qty = 0;


 $tot_ctn = 0;


 $tot_amt = 0;


 $discount = 0;


 $gst_amt = 0;


 $sql = "select title_id from sr where sr_id = '".$s_id."'";


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


                      $statement->execute(); 


                      $title_id = $statement->fetchColumn();


$statement = $connect->prepare("select * from titles where  id= :title_id


    


  ");


 $statement->execute(


  array(  


   ':title_id'       =>  $title_id


  )


 );


 $result = $statement->fetchAll();


 


  foreach($result as $row)


 {


  $heading1 = $row['heading1'];


  $heading2 = $row['heading2'];


  $heading3 = $row['heading3'];


 }


        


 $statement = $connect->prepare("select m.sr_id,m.inv_no,m.chal_no,m.po_no,m.bilty_no,m.po_no,m.due_day,DATE_FORMAT(m.due_dated,'%d-%m-%Y') as due_dated,DATE_FORMAT(m.dated,'%d-%m-%Y') as dated,m.cur_stamp,m.description,m.inv_type,m.discount,m.discount_dsc,m.freight_dsc,m.cust_name,m.contact,m.freight,m.gst_pr,m.pre_balance,c.name,c.phone,o.user_name,e.first_name,s.sname,


    d.item_no,CONCAT(t.type_name,' - ', i.item_name) as item_name,u.uom_name,d.ctn,d.qty,d.rate from  items i,item_type t, sr_detail d,sr m,item_uom u,user_detail o,


    customers c,sales_man s,employees e where m.sr_id=d.sr_id and t.type_id=i.type_id and m.emp_id=e.emp_id and s.sm_id=m.sm_id and o.user_id=m.user_id and d.item_no=i.item_id and i.uom=u.uom_id and c.cust_id=m.cust_id and


    m.sr_id= :s_id


    


  ");


 $statement->execute(


  array(  


   ':s_id'       =>  $s_id


  )


 );


 $result = $statement->fetchAll();


 


  foreach($result as $row)


 {


  if($row['inv_type']=='Cash')


  {


    $contact = $row['contact'];


    $name = $row['cust_name'];


  }else{$contact = $row['phone'];$name=$row['name'];}


 


  $html .=' <style>


 * {


 font-family : Helvetica;


}


    @page { margin:180px 20px; }


    .first_page { position: fixed; left: 0px; top: -180px; right: 0px; height: 180px; text-align: center;background-color: #fff; }


    


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


    #content {margin:0;}


    .rightside{text-align:right;}


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


    


    #table_header th {


    border-top:1px solid black;


    border-bottom:1px solid black;


  }


  .small-font {


  font-size:10px;


  font-weight:normal;


  font-family:Helvetica;


}


.font-bold {


  font-size:13px;


  font-weight:normal;


  font-family:Helvetica;


}


.signtable {


  text-align:center;


}


#heading {


  text-align:center;


}


.topline {


  border-top:0.1px solid black;


}


.brline


{


  border-top:0.1px solid black;


}


 h5, h6{


  margin: 0;


  padding: 0;


}


#main-header {


  font-size:12px;


}


  </style>


  ';


  $html .='


<div class="first_page">


  <div id="header">


    <div id="heading">


    <br />


  <h5>'.$heading1.'</h5>


  <h6>'.$heading2.'</h6>


  <h6>'.$heading3.'</h6>


  </div>


  


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


  <tr>


  <td width="15%">Bill No. : '.$row['sr_id'].'</td><td width = "50%">Inv No. : '.$row["inv_no"].'</td><td width="15%">Date :</td><td width = "20%">'.$row["dated"].'</td>


  </tr>


  <tr>


  <td width="15%">M/S :</td><td width = "50%">'.$name.'</td><td width="15%">Credit Days :</td><td width = "20%">'.$row["due_day"].'</td>


  </tr>


  <tr>


  <td width="15%">Phone No. :</td><td width = "50%">'.$contact.'</td><td width="15%">Due Date :</td><td width = "20%">'.$row["due_dated"].'</td>


  </tr>


  <tr>


  <td width="15%">Delivered By :</td><td width = "50%">'.$row["first_name"].' </td><td width="15%">Bilty No. :</td><td width = "20%">'.$row["bilty_no"].'</td>


  </tr>


  <tr>


  <td width="15%"><b>PO No. :</b></td><td width = "50%">'.$row["po_no"].'</td><td width="15%">Chal No. :</td><td width = "20%">'.$row["chal_no"].'</td>


  </tr>


  


  </table>


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


  <tr>


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


  


  <th width="58%" class="font-bold">Item Name</th>


  <th width="7%" class="font-bold">UOM</th>


  


  


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


  


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


  


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


 


  </tr>


  </table>


  </div>


  


';


}


$count = 0;


$amount = 0;


$discount = $row["discount"];


$freight = $row["freight"];


$gst_pr = $row["gst_pr"];


foreach($result as $row)


 {


  $qty = $row["qty"];


  $rate = $row["rate"];


  


  


  


   $count = $count +1;


  


  $amount = $qty * $rate;


  $tot_qty = $tot_qty + $qty;


  


  $tot_amt = $tot_amt + $amount;


$html .= '


  <div id="content">


  


  <table width="100%">


<tr>


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


 


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


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





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


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





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


  


   


    


   </tr>


   


  </table>


  </div>


  <div id="footer">


 


  <table width="100%">


  <tr>


  <td width="70%" class="rightside"> </td><td width="30%" class="rightside"></td>


  </tr>


  </table>


  


  </div>


 


        ';


}


 if($gst_pr > 0)


    {


      $gst_amt = $tot_amt * $gst_pr / 100;


    }


  $net_amt = $tot_amt - $discount + $freight + $gst_amt;


$html .='<div class="brline"></div>


<table width="100%">


<tr>


<td width="60%" class="small-font">Description: '.$row["description"].' </td><td class="rightside small-font">Total:</td>





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


<td width="10%"></td>


<td width="10%"></td>


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


</tr>


</table>


<table width="100%">


<tr>


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


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


</tr>


<tr>


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


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


</tr>


<tr>


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


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


</tr>


<tr>


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


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


</tr>


</table>


<div class="brline"></div>


<table width="100%">


<tr>


<td class="small-font" width="90%">Printed at : '.$row['cur_stamp'].' - Previous Balance : '.$row['pre_balance'].' - Created By : '.$row['user_name'].' - Sales Officer:'.$row['sname'].'</td>


</tr>


</tr>


<tr>


</tr>


</table>


<br />


<br />


<table width="100%" class="signtable">


<tr>


<td width="15%"></td><td class="small-font topline" width="20%">Prepared By</td><td width="20%"></td><td width="20%" class="small-font topline">Received By</td><td width="25%"></td>


<td width="20%" class="small-font topline">Checked By</td><td width="25%"></td>


</tr>


</table>


</div><!--end first_page-->'


;


$tot_qty = 0;


 $tot_amt = 0;


 $discount = 0;


 $gst_amt = 0;


 $html .='


<div class="second_page">


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


<br />


  <div id="header">


    <div id="heading">


    <br />


  <h5>'.$heading1.'</h5>


  <h6>'.$heading2.'</h6>


  <h6>'.$heading3.'</h6>


  </div>


  


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


  <tr>


  <td width="15%">Bill No. : '.$row['sr_id'].'</td><td width = "50%">Inv No. : '.$row["inv_no"].'</td><td width="15%">Date :</td><td width = "20%">'.$row["dated"].'</td>


  </tr>


  <tr>


  <td width="15%">M/S :</td><td width = "50%">'.$name.'</td><td width="15%">Credit Days :</td><td width = "20%">'.$row["due_day"].'</td>


  </tr>


  <tr>


  <td width="15%">Phone No. :</td><td width = "50%">'.$contact.'</td><td width="15%">Due Date :</td><td width = "20%">'.$row["due_dated"].'</td>


  </tr>


  <tr>


  <td width="15%">Delivered By :</td><td width = "50%">'.$row["first_name"].'</td><td width="15%">Bilty No. :</td><td width = "20%">'.$row["bilty_no"].'</td>


  </tr>


  <tr>


  <td width="15%"><b>PO No. :</b></td><td width = "50%">'.$row["po_no"].'</td><td width="15%">Chal No. :</td><td width = "20%">'.$row["chal_no"].'</td>


  </tr>


  


  </table>


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


  <tr>


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


  


  <th width="58%" class="font-bold">Item Name</th>


  <th width="7%" class="font-bold">UOM</th>


  


  


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


  


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


  


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


 


  </tr>


  </table>


  </div>


  


';


$count = 0;


$amount = 0;


$discount = $row["discount"];


$freight = $row["freight"];


$gst_pr = $row["gst_pr"];


foreach($result as $row)


 {


  $qty = $row["qty"];


  


  $rate = $row["rate"];


  


   $count = $count +1;


  


  $amount = $qty * $rate;


  $tot_qty = $tot_qty + $qty;


  $tot_amt = $tot_amt + $amount;


$html .= '


  <div id="content">


  


  <table width="100%">


<tr>


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


 


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


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





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


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





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


  


   


    


   </tr>


   


  </table>


  </div>


  <div id="footer">


 


  <table width="100%">


  <tr>


  <td width="70%" class="rightside"> </td><td width="30%" class="rightside"></td>


  </tr>


  </table>


  


  </div>


 


        ';


}


 if($gst_pr > 0)


    {


      $gst_amt = $tot_amt * $gst_pr / 100;


    }


  $net_amt = $tot_amt - $discount + $freight + $gst_amt;


$html .='<div class="brline"></div>


<table width="100%">


<tr>


<td width="60%" class="small-font">Description: '.$row["description"].' </td><td class="rightside small-font">Total:</td>





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


<td width="10%"></td>


<td width="10%"></td>


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


</tr>


</table>


<table width="100%">


<tr>


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


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


</tr>


<tr>


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


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


</tr>


<tr>


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


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


</tr>


<tr>


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


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


</tr>


</table>


<div class="brline"></div>


<table width="100%">


<tr>


<td class="small-font" width="90%">Printed at : '.$row['cur_stamp'].' - Previous Balance : '.$row['pre_balance'].' - Created By : '.$row['user_name'].' - Sales Officer:'.$row['sname'].'</td>


</tr>


</tr>


<tr>


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


</tr>


</table>


<br />


<br />


<table width="100%" class="signtable">


<tr>


<td width="15%"></td><td class="small-font topline" width="20%">Prepared By</td><td width="20%"></td><td width="20%" class="small-font topline">Received By</td><td width="25%"></td>


<td width="20%" class="small-font topline">Checked By</td><td width="25%"></td>


</tr>


</table>


</div><!--end first_page-->'


;


 $pdf = new Pdf();


 $file_name = 'Sale_InvoiceA5.pdf';


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


 $pdf->loadHtml($html);


 $pdf->render();


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


?>