<?php
$bill_no  = $_POST['bill_no'];
include('../config.php');
$bill             =  "SELECT * FROM `billing` WHERE `invoice_no` = '".$bill_no."'";
$bill_res         = mysqli_query($conn,$bill);
$bill_row         = mysqli_fetch_assoc($bill_res);

$vendor           = "SELECT * FROM `vendor_master` WHERE `id` = '".$bill_row['vendor_id']."'";
$vendor_res       = mysqli_query($conn,$vendor);
$vendor_row       = mysqli_fetch_array($vendor_res);

$state_code       = substr($vendor_row['gstin'],0,2);

$lr     = "SELECT * FROM `billing` WHERE invoice_no = '".$bill_no."'";
  $lr_res = mysqli_query($conn,$lr);
  $num    = mysqli_num_rows($lr_res);
  $lrs = array();
  while ($lr_row = mysqli_fetch_assoc($lr_res)) { 
    $lrs[] = $lr_row;
    $common_remarks = $lr_row['remarks'];
  }

  $gst_paid         = "SELECT * FROM lr_mst WHERE lr_no = '".$bill_row['lr_no']."'";
  $gst_paid_res     = mysqli_query($conn,$gst_paid);
  $gst_paid_row     = mysqli_fetch_assoc($gst_paid_res);

  $bank_det         = "SELECT * FROM company_mst WHERE id = '".$_SESSION['company_id']."'";
  $bank_det_res     = mysqli_query($conn,$bank_det);
  $bank_det_rows    = mysqli_fetch_assoc($bank_det_res);

  $bill_add     = "SELECT * FROM `billing_details` WHERE `type` = 'add' AND `bill_no` =  '".$bill_no."' ORDER BY id DESC";
  $bill_add_res = mysqli_query($conn,$bill_add);

  $bill_add = array();
  $total_add  = 0;
  while ($bill_add_row = mysqli_fetch_assoc($bill_add_res)) {
    //$addition = $lhc_details_row['descr'];
    $bill_add[] = array('title' => $bill_add_row['tax_mst_id'], 'amount' => $bill_add_row['amount']);
    $total_add += $bill_add_row['amount'];
  }
  $master_tax = combine(getAllRecords('tax_mst'), 'id', 'descr');

  if( isset($bill_add[0]['amount']) && !empty($bill_add[0]['amount']) ) {
    $bill_amnt_0 = customFormatMoney($bill_add[0]['amount']); 
  }else{
    $bill_amnt_0 = '';
  }

  if( isset($bill_add[1]['amount']) && !empty($bill_add[1]['amount']) ) {
    $bill_amnt_1 = customFormatMoney($bill_add[1]['amount']); 
  }else{
    $bill_amnt_1 = '';
  }

  if( isset($bill_add[2]['amount']) && !empty($bill_add[2]['amount']) ) {
    $bill_amnt_2 = customFormatMoney($bill_add[2]['amount']);
  }else{
    $bill_amnt_2 = '';
  }

  if( isset($bill_add[3]['amount']) && !empty($bill_add[3]['amount']) ) {
    $bill_amnt_3 = customFormatMoney($bill_add[3]['amount']);
  }else{
    $bill_amnt_3 = '';
  } 


  $lhc_sub     = "SELECT * FROM `billing_details` WHERE `type` = 'sub' AND `bill_no` =  '".$bill_no."' ORDER BY id DESC";
  $lhc_sub_res = mysqli_query($conn,$lhc_sub);
  $sub = array();
  $total_sub = 0;
  while ($lhc_sub_row = mysqli_fetch_assoc($lhc_sub_res)) {
    $sub[] = array('title' => $lhc_sub_row['tax_mst_id'], 'amount' => $lhc_sub_row['amount']);
    $total_sub += $lhc_sub_row['amount'];
  }

  if( isset($sub[0]['amount']) && !empty($sub[0]['amount']) ) {
    $sub_0 = customFormatMoney($sub[0]['amount']);
    $style = ''; 
  }else{
    $sub_0 = '';
    $style = "height:17px";
  }

  if( isset($sub[1]['amount']) && !empty($sub[1]['amount']) ) {
    $sub_1 = customFormatMoney($sub[1]['amount']);
    $style = '';  
  }else{
    $sub_1 = '';
    $style = "height:17px";
  }

  if( isset($sub[2]['amount']) && !empty($sub[2]['amount']) ) {
    $sub_2 = customFormatMoney($sub[2]['amount']);
    $style = '';  
  }else{
    $sub_2 = '';
    $style = "height:17px";
  }

  if( isset($sub[3]['amount']) && !empty($sub[3]['amount']) ) {
    $sub_3 = customFormatMoney($sub[3]['amount']);
    $style = '';  
  }else{
    $sub_3 = '';
    $style = "height:17px";
  }

  $branch     = "SELECT * FROM branch_mst WHERE id = '".$_SESSION['branch_id']."'";
  $branch_mst = mysqli_query($conn,$branch);
  $branch_row = mysqli_fetch_assoc($branch_mst);
?>
<!DOCTYPE html>
<html>
<head>
  <title>BILL</title>
<link rel="stylesheet" href="../assets/pdf_formate.css" media="all" />
</head>
<body onload="printpage();">
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <?php 
      if( isset($_POST['include_header']) && !empty($_POST['include_header']) ) { ?>
        <table class="table no-top-border-table no-bottom hitz-top">
          <tr>
            <td width="5%" align="left">
              <img src="../assets/letter_logo.png" class="img-height">
            </td>
            <td width="75%" align="center">
              <label class="invoice">INVOICE</label>
              <h1>PIYUSH LOGISTIC</h1>
              <span class="header-small">FF 04, SUNGOLD COMPLEX, OPP.SNT. BASIL SCHOOL,NR.JAMBUWA BRIDGE, VADODARA-14</span><br/><br/>
              <span class="header-small"><b>EMAIL</b>:PIYUSHLOGISTIC@GMAIL.COM&nbsp;<b>WEB&nbsp;</b>: WWW.PIYUSHLOGISTIC.COM</span>
            </td>
            <td width="20%" align="right">
              <span>MOBILE : 9377320515</span><br/>
              <span>9408000031</span>
            </td>
          </tr>
        </table>
      <?php }else{ ?>
      <table class="table no-top-border-table no-bottom hitz-top">
        <tbody>
          <?php for ($j=1; $j <=10 ; $j++) { ?>
            <tr>
              <td>&nbsp;</td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
            </tr>  
          <?php } ?>
        </tbody>
      </table>
      <?php } ?>
      
    </div><hr class="hr-custom">
    <div class="col-md-12">
      <table class="table no-top-border-table no-bottom hitz-top">
        <tbody>
          <tr>
            <td width="70%" class="text-left"><label class="control-label bold">M/S.</label></td>
            <td width="30%" class="text-left">
              <label class="control-label"><strong style="width: 106px;display: inline-block;margin-right: 0px;text-align: left;">Bill No</strong> : <b><?php echo $bill_row['invoice_no'];?></b>
            </td>
          </tr>
          <tr>
            <td width="70%" class="text-left"><label class="control-label"><span class="bold"><?php echo $vendor_row['name'];?></span><br/><?php echo $vendor_row['address'];?>,<?php echo $vendor_row['city'];?>,<?php echo $vendor_row['state'];?> <?php echo $vendor_row['pincode'];?>.</label></td>
            <td width="30%" class="text-left">
              <label class="control-label"><strong style="width: 106px;display: inline-block;margin-right: 0px;text-align: left;">BILL Date</strong> : <b><?php echo risFormatDate($bill_row['bill_date']);?></b>
            </td>
          </tr>
          <tr>
            <td width="70%" class="text-left"><label class="control-label bold">GSTIN NO. <?php echo $vendor_row['gstin'];?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="control-label bold">STATE CODE. <?php echo $state_code;?></label> </td>
            <td width="30%" class="text-left"></td>
            
          </tr>
        </tbody>
      </table>
      <table class="table no-top-border-table width100">
        <tbody>
            <tr>
              <th width="1%">Sr.No.</th>
              <th width="15%">Date</th>
              <th width="15%">LR No.</th>
              <th colspan="2" width="10%">From</th>
              <th colspan="2" width="10%">To</th>
              <th width="10%">Amount</th>
              <th width="10%">Other</th>
              <th width="15%">Net Amount</th>
            </tr>
            <?php
            $i = 1; 
            $net = 0;
            foreach ($lrs as $i => $lrs) { 
            $net += $lrs['lr_amount'] + $lrs['other_charges'];
            $net_amnt = $lrs['lr_amount'] + $lrs['other_charges'];
            ?>
            <tr class="table-boder-bottom  border-bottom" align="center">
              <td class="border-right padding10 border-bottom"><?php echo ++$i;?></td>
              <td class="border-right padding10 border-bottom"><?php echo risFormatDate($lrs['bill_date']);?></td>
              <td class="border-right padding10 border-bottom"><?php echo $lrs['lr_no'];?></td>
              <td colspan="2" class="border-right padding10 border-bottom"><?php echo $lrs['lr_from'];?></td>
              <td colspan="2" class="border-right padding10 border-bottom"><?php echo $lrs['lr_to'];?></td>
              <td class="border-right padding10 border-bottom"><?php echo $lrs['lr_amount'];?></td>
              
              <td class="table-border border-right padding10 border-bottom"><?php echo customFormatMoney($lrs['other_charges']);?></td>
              <td class="table-border border-right padding10 border-bottom"><?php echo customFormatMoney($net_amnt);?></td>
            </tr>
            <?php } ?>
           <?php 
            if( $num <  6) { 
              for ($j=1; $j <= ( 6 - $num ); $j++) {  ?>
                  <tr>
                    <td class="table-border padding10">&nbsp;</td>
                    <td class="table-border padding10"></td>
                    <td class="table-border padding10"></td>
                    <td class="table-border padding10"></td>
                    <td class="table-border padding10"></td>
                    <td class="table-border padding10"></td>
                    <td class="table-border padding10" colspan="2"></td>
                    <td class="table-border padding10"></td>
                    <td class="table-border padding10"></td>
                  </tr>
            <?php   } 
            } ?>
            
            <tr class="table-boder-bottom  border-top border-bottom" align="center" style="width: 100%">
              <td colspan="10" class="padding10"><b>OUR BANK DETAILS</b></td>
            </tr>
            <tr>
              <td colspan="10" class="right-border-none" align="left" style="border-right: 1px solid black"><label style="position: relative;left: 11px;"><b>BANK NAME</b> &nbsp; : <b>KOTAK MAHINDRA BANK</b></label></td>
              
            </tr>
            <tr>
              <td colspan="10" class="right-border-none" align="left" style="border-right: 1px solid black"><label style="position: relative;left: 11px;"><b>A/c No</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <b>9911683715</b></label></td>
              
            </tr>

            <tr>
              <td colspan="10" class="right-border-none" align="left" style="border-right: 1px solid black"><label style="position: relative;left: 11px;"><b>IFSC CODE</b> &nbsp;&nbsp;&nbsp; : <b>KKBK0000843</b></label></td>
              
            </tr>

            <tr>
              <td colspan="10" class="right-border-none" align="left" style="border-right: 1px solid black"><label style="position: relative;left: 11px;"><b>Branch</b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <b>TARSALI, VADODARA</b></label></td>
              
            </tr>

            
            
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 border-top none-border-right" colspan="6"><label class="control-label bold">GST PAID BY &nbsp;&nbsp;&nbsp;&nbsp;</label> <b><?php echo $gst_paid_row['gst_paid_by'];?></b></td>
              
              <td class="border-right padding7 border-top bold none-border-right" colspan="3" align="left">Total</td>
              <td class="table-border border-right padding7 border-top" align="right"><?php echo customFormatMoney($net);?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold">SEC CODE 996511</td>
              
              <td class="border-right padding7 border-top bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$bill_add[0]['title']];?></td>
              <td class="table-border border-right padding7 border-top" align="right" style="<?php echo $style;?>"><?php echo $bill_amnt_0;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold">REMARK</td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$bill_add[1]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $bill_amnt_1;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label"><b><?php echo $common_remarks?></b></td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$bill_add[2]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $bill_amnt_2;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$bill_add[3]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $bill_amnt_3;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$sub[0]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $sub_0;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$sub[1]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $sub_1;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$sub[2]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $sub_2;?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
              <td class="border-right padding7 bold none-border-right" colspan="3" align="left"><?php echo @$master_tax[$sub[3]['title']];?></td>
              <td class="table-border border-right padding7" align="right" style="<?php echo $style;?>"><?php echo $sub_3;?></td>
            </tr>
            <?php if($bill_row['sgst_per'] > 0 && $bill_row['cgst_per'] > 0 ){ ?>
              <tr class="table-boder-bottom no-bottom">
                <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
                <td class="border-right padding7 border-top bold none-border-right" colspan="3" align="left">SGST &nbsp;<?php echo $bill_row['sgst_per']?>%</td>
                <td class="table-border border-right padding7 border-top" align="right"><?php echo customFormatMoney($bill_row['sgst_amount'])?></td>
              </tr>
              <tr class="table-boder-bottom no-bottom">
                <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
                <td class="border-right padding7 border-top bold none-border-right" colspan="3" align="left">CGST &nbsp;<?php echo $bill_row['cgst_per']?>%</td>
                <td class="table-border border-right padding7 border-top" align="right"><?php echo customFormatMoney($bill_row['cgst_amount'])?></td>
              </tr>
            <?php } ?>
            <?php if($bill_row['igst_per'] > 0 ){ ?>
              <tr class="table-boder-bottom no-bottom">
                <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold"></td>
              
                <td class="border-right padding7 border-top bold none-border-right" colspan="3" align="left">IGST &nbsp;<?php echo $bill_row['igst_per']?>%</td>
                <td class="table-border border-right padding7 border-top" align="right"><?php echo customFormatMoney($bill_row['igst_amount'])?></td>
              </tr>
            <?php } ?>

            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold">Total Amount In Words</td>
              
              <td class="border-right padding7 border-top bold none-border-right" colspan="3" align="left">Taxable Amount</td>
              <td class="table-border border-right padding7 border-top" align="right"><?php 
              $taxable_amount = $net + ($total_add - $total_sub);
              echo customFormatMoney($taxable_amount);?></td>
            </tr>

            <tr class="table-boder-bottom no-bottom">
              <td class="border-right padding7 none-border-right" colspan="6"><label class="control-label bold" style="text-transform: capitalize;"><?php echo convertNumber2Words($taxable_amount)?></label></td>
              
              <td class="border-right padding7 bold none-border-right border-top" colspan="3" align="left">Net Amount</td>
              <td class="table-border border-right padding7 border-top" align="right"><?php echo customFormatMoney($taxable_amount + $bill_row['sgst_amount'] + $bill_row['cgst_amount'] + $bill_row['igst_amount']);?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right cust-padding border-top none-border-right" colspan="6"><label class="control-label"><strong class="col-cust">GSTIN NO</strong>: <?php echo $bank_det_rows['gst'];?></td>
              
              <td class="border-right cust-padding border-top bold none-border-right" colspan="4" align="right" rowspan="2">FOR,<?php echo $bank_det_rows['name'];?></td>
              
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right cust-padding none-border-right" colspan="6"><label class="control-label"><strong class="col-cust">STATE CODE</strong>: <?php echo substr($bank_det_rows['gst'],0,2);?></td>
            </tr>
            <tr class="table-boder-bottom no-bottom">
              <td class="border-right cust-padding none-border-right" colspan="6"><label class="control-label"><strong class="col-cust">PAN</strong>: <?php echo $bank_det_rows['panno'];?></td>
              
              <td class="border-right cust-padding bold none-border-right" colspan="4" align="right">AUTH. SIGNATURE</td>
              
            </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
<script type="text/javascript">
function printpage(){
    window.print();
    setTimeout(function () { window.close(); }, 100);
}
</script>
</body>
</html>
