<?php

 date_default_timezone_set('Asia/Karachi');
 include('db_con.php');
 
 
 $p_id = $_GET['id'];
 if($p_id == 0)
 {
  $sql = "select max(pr_id) from pr";
  $statement = $connect->prepare($sql);
  $statement->execute();
  $p_id = $statement->fetchColumn();
 }
 $tot_qty = 0;
 $tot_amt = 0;
 $tot_lqty = 0;
 $discount =0;
 $freight = 0;
 $pre_balance = 0;
  
 
  $heading1 = 'Purchase Return';
  $heading2 = '-';
  $heading3 = '-';
 
 

  
 

 
  ?>
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>Purchase Return</title>
     <style>
    * {
      font-family: arial;

      padding: 3px;
      font-weight: bold;

    }
    

    p {
      font-size: 11px;
    }

    .inv_title {
      font-size: 20px;
      text-align: center;
    }

    .main_header {
      text-align: center;
      padding: 2px;
      line-height: 0px;
    }

    .font-bold {
      font-weight: bold;
    }

    thead td {
      font-weight: bold;
      padding: 3px;
      border-bottom: 2px solid black;
      border-top: 2px solid black;
      background-color: #f4f4f4;
    }
    tbody td {
      
      padding: 3px;
      border-bottom: 0.5px solid black;
      
    }
    .font-bold {
      font-weight: bold;
    }
    .rightside {
      text-align: right;
    }
    table.table_head td{
      border: none;
    }
    .smallfont {
      font-size: 12px;
      font-weight: bold;
    }

    .no-border {
      border: none;
    }
   

  .bottom-text {
    width: 50%;
    margin: 0 auto;
  }
  @page {
  margin: 1cm;
}

.danger {
  color: #d9534f;
}

.primary {
  color: #0275d8;
}
.nobor {
  border:none;
}
.dataline {
  font-size: 12px;
}
.total {
  font-size: 14px !important;
}
  </style>


  </head>
  <body onload="window.print();">
    <div class="main_header">
       <span class="inv_title"><?php echo $heading1;?></span>
      <p><?php echo $heading2;?></p>
      <p><?php echo $heading3;?></p>
    </div>
 <table width="100%" class="table_head">
  <?php 
  $statement = $connect->prepare("select m.pr_id p_id,m.inv_no,m.v_id,m.po_no,m.po_no,m.due_day,DATE_FORMAT(m.dated,'%d-%m-%Y') as dated,m.cur_stamp,m.description,'Credit' inv_type,m.discount,m.discount_dsc,m.freight_dsc,m.gst_pr, m.fst_pr, m.ast_pr, v.phone,m.freight,m.gst_pr,0 pre_balance,v.name,v.address,v.phone from pr m, vendors v where m.v_id=v.v_id and m.pr_id= :p_id
    
  ");
 $statement->execute(
  array(  
   ':p_id'       =>  $p_id
  )
 );
  $result = $statement->fetchAll();

  foreach($result as $row)
 {
 $v_id = $row['v_id'];
  $acc_no = '201'.$v_id;
  $gst_pr = $row["gst_pr"];
    $ast_pr = $row["ast_pr"];
    $fst_pr = $row["fst_pr"];
 if($row['inv_type']=='Cash')
  {
    
    $contact = $row['phone'];
   
    
    $name = $row['name'];
  }else{$contact = $row['phone'];$name=$row['name'];}
  $discount = $row['discount'];
  $freight = $row['freight'];
  $discount_dsc = $row["discount_dsc"];
  $freight_dsc = $row["freight_dsc"];
  $p_id = $row['p_id'];
  $cur_stamp = $row['cur_stamp'];
  $pre_balance = $row['pre_balance'];

 echo '
  <tr>

  <td width="10%"><b>M/S :</b></td><td width = "50%" class="danger">'.$name.'</td><td width="15%"><b>Inv # :</b></td><td width = "25%">'.$row['inv_no'].'</td>
  </tr>
    <td width="10%"><b>Address:</b></td><td width = "50%" >'.$row['address'].'</td><td width="15%"><b>Inv Type:</b></td><td width = "25%">'.$row['inv_type'].'</td>
  <tr>
  <td width="10%" class="font-bold"><b>Ph:</b></td><td width = "50%">'.$contact.'</td><td width="40%" colspan="2"><b>Date:</b><span class="smallfont">'.$cur_stamp.'</span></td>
  </tr>
  ';
}
  ?> 
  </table>
  <table width="100%" id="table_header">
  <thead>

  <tr>
  
  
  <td width="3%" class="font-bold">#</td>
  <td width="58%"class="font-bold">Description</td>
  
  <td width="10%" class="font-bold rightside">UOM</td>
<td width="7%" class="font-bold rightside">Qty</td>
  <td width="9%" class="rightside font-bold">Rate</td> 
    <td width="12%" class="rightside font-bold">Amount</td> 
  
  </tr>
  
</thead>

<tbody>
<?php

$sql = "select 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, pr_detail d,item_uom u    where t.type_id=i.type_id and d.item_no=i.item_id and i.uom=u.uom_id and
    d.pr_id= :p_id";
$statement = $connect->prepare($sql);
$statement->execute(
  array(  
   ':p_id'       =>  $p_id
  )
 );
$result = $statement->fetchAll();
$count = 0;
foreach($result as $row)
 {

$amount = 0;

$tot_balance = 0;

  $qty = $row["qty"];
  $ctn = $row["ctn"];
  
  $rate = $row["rate"];
   $count++;
  
  $amount = $qty * $rate;
  $tot_qty = $tot_qty + $qty;
  $tot_amt = $tot_amt + $amount;
  

  
echo '
  
<tr  class="dataline">
  
   <td>'.number_format($count, 0).'</td>
   <td width="53%">'.$row['item_name'].'</td>
   

<td class="rightside">'.$row['uom_name'].'</td>

<td class="rightside">'.number_format($qty, 0).'</td>
<td  class="rightside ">'.number_format($rate, 0).'</td>
<td class="rightside ">'.number_format($amount, 0).'</td>
   </tr>
   ';
   
  
  
  

}
$tot_balance =  $tot_amt;


echo '

<tr  class="total">
<td colspan="3" class="rightside"> Total : </td>
<td class="total_line rightside small-font">'.number_format($tot_qty, 0).'</td>
<td class="total_line rightside small-font"></td>
<td class="total_line rightside small-font">'.number_format($tot_amt, 0).'</td>
 
</tr>';

if($discount > 0 )
{
  echo '

<tr class="dataline">
<td colspan="5" class="rightside nobor"> '.$discount_dsc.' : </td>
<td class="total_line rightside">- '.number_format($discount, 2).'</td>
 
</tr>';
}
if($freight > 0 )
{
  echo '

<tr class="dataline">
<td colspan="5" class="rightside nobor"> '.$freight_dsc.' : </td>
<td class="total_line rightside">'.number_format($freight, 2).'</td>
 
</tr>';
}
if($discount > 0 || $freight > 0 )
{
  $net_amt = $tot_amt + $freight - $discount;
  echo '

<tr  class="total">
<td colspan="5" class="rightside nobor"> Invoice Total : </td>
<td class="total_line rightside">'.number_format($net_amt, 2).'</td>
 
</tr>';
}
$gst_amount = 0;
$ast_amount = 0;
$fst_amount = 0;
if($gst_pr > 0)
{
  $gst_amount = $tot_amt * $gst_pr / 100;
   echo '

<tr class="dataline">
<td colspan="5" class="rightside nobor"> GST '.$gst_pr.'%: </td>
<td class="total_line rightside ">'.number_format($gst_amount, 2).'</td>
 
</tr>';
}
if($fst_pr > 0)
{
  $fst_amount = $tot_amt * $fst_pr / 100;
   echo '

<tr class="dataline">
<td colspan="5" class="rightside nobor"> Further Tax '.$fst_pr.'%: </td>
<td class="total_line rightside ">'.number_format($fst_amount, 2).'</td>
 
</tr>';
}
if($ast_pr > 0)
{
  $ast_amount = $tot_amt * $ast_pr / 100;
   echo '

<tr class="dataline">
<td colspan="5" class="rightside nobor"> Advance Tax: '.$ast_pr.'%: </td>
<td class="total_line rightside ">'.number_format($ast_amount, 2).'</td>
 
</tr>';
}
if($gst_pr > 0 || $ast_pr > 0 || $fst_pr > 0)
{
  $net_amount = $tot_amt + $gst_amount + $ast_amount + $fst_amount;
  echo '

<tr class="total">
<td colspan="5" class="rightside nobor"> Net Total: </td>
<td class="total_line rightside ">'.number_format($net_amount, 2).'</td>
 
</tr>';
}
?>


</table>

</body>
</html>