<?php 
error_reporting('0');
include("../config.php");
$report     = "SELECT * FROM report_setting WHERE company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
$report_res = mysqli_query($conn,$report);
$report_row = mysqli_fetch_assoc($report_res);
$id = $_POST['id'];

$master_tax       = combine(getAllRecords('tax_mst'), 'id', 'descr');
$master_accounts  = combine(getAllRecords('act_master'), 'id', 'ac_name');

$bill             =  "SELECT * FROM `billing` WHERE `invoice_no` = '".$id."'";
$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);

$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` =  '".$id."' 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'];
}

$lhc_sub     = "SELECT * FROM `billing_details` WHERE `type` = 'sub' AND `bill_no` =  '".$id."' 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'];
}
//addition
if($bill_add[0]['title'] == ''){
  $style = "height:20px";
}else if($bill_add[1]['title'] == ''){
  $style = "height:20px";
}else if($bill_add[2]['title'] == ''){
  $style = "height:20px";
}else if($bill_add[3]['title'] == ''){
  $style = "height:31px";
}else{
  $style = "";
}


//deletion
if($sub[0]['title'] == ''){
  $style = "height:20px";
}else if($sub[1]['title'] == ''){
  $style = "height:20px";
}else if($sub[2]['title'] == ''){
  $style = "height:20px";
}else if($sub[3]['title'] == ''){
  $style = "height:20px";
}else{
  $style = "";
}
$master_tax       = combine(getAllRecords('tax_mst'), 'id', 'descr');

$branch     = "SELECT * FROM branch_mst WHERE id = '".$_SESSION['branch_id']."'";
$branch_mst = mysqli_query($conn,$branch);
$branch_row = mysqli_fetch_assoc($branch_mst);

?>
<style type="text/css">
  .clickable{
    display: none;
  }
  
</style>
<div class="hide-header-blank">
<?php echo $report_row['header_data']; ?>
</div>

<div class="show-header-blank clickable">
  <table width="100%">
    <tbody>
    <?php for ($j=1; $j <= ( 5 - $num ); $j++) { ?> 
      <tr>
        <td>&nbsp;</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
</div>
<div class="col-md-12 hidden-print text-right">
  <form method="post" action="bill/bill_print.php" target="_blank">
    <label class="radio-inline fontradiobuttion">
    <input type="checkbox" name="include_header" ata-checkbox="icheckbox_flat" class="icheck" id="header-checkbox" checked="" value="IH"> <span class="label-text">INCLUDE HEADER</span>
    <input type="hidden" name="bill_no" value="<?php echo $id;?>">
    </label>
    <!-- <button type="button" onclick="window.print();" class="btn btn-primary">PRINT</button> -->
     <button type="submit" class="btn btn-primary">PRINT</button>  
    <button type="button" class="btn btn-danger" data-dismiss="modal">CLOSE</button>
  </form>
</div>
<table class="table no-top-border-table">
  <tr>
      <td width="95%" colspan="3" class="text-left"><label class="control-label">M/s. <br/> <?php echo $vendor_row['name']?> <br/><?php echo $vendor_row['address']?></label></td>
      
      <td width="5%" class="text-left" colspan="2">
        
        <label class="control-label"><strong style="width: 125px;display: inline-block;margin-right: 5px;text-align: left;">Bill No</strong> : <?php echo $bill_row['invoice_no']?></label><br/>

        <label class="control-label"><strong style="width: 125px;display: inline-block;margin-right: 5px;text-align: left;">BILLDate</strong> : <?php echo risFormatDate($bill_row['bill_date'])?></label>

      </td>    
  </tr>
  <tr>
      <td width="55%" colspan="3" class="text-left"><label class="control-label"><strong>GSTIN No</strong> : <?php echo $vendor_row['gstin']?></label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label class="control-label"><strong>State Code</strong> : <?php echo $state_code?></label></td>
  </tr>
</table>
<table class="table table-bordered table-striped print-bill-bottom">
  <tbody>
    <tr class="text-center">
      <td class='hitz-td' width="5%">Sr. No.</td>
      <td class='hitz-td' width="10%">Date</td>
      <td class='hitz-td' width="10%">LR No.</td>
      <td class='hitz-td' width="10%">Truck</td>
      <td class='hitz-td' width="20%">From</td>
      <td class='hitz-td' width="15%">To</td>
      <td class='hitz-td' width="10%">Weight Article</td>
      <td class='hitz-td' width="10%">Detention Or Labour Charges.</td>
      <td class='hitz-td' width="10%">Amount</td>
    </tr>
    <?php 
    $lr     = "SELECT * FROM `billing` WHERE invoice_no = '".$id."'";
    $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;
    }
    
    $i = 1;
    $total_weight = 0;
    $total_other  = 0;
    $total_amount = 0;
    foreach ($lrs as $i => $lrs) { 
    $fri   = "SELECT * FROM lr_mst WHERE lr_no = '".$lrs['lr_no']."'";
    $fri_res = mysqli_query($conn,$fri);
    $fri_row = mysqli_fetch_assoc($fri_res);
    $lr_rematks     = "SELECT * FROM `billing` WHERE invoice_no = '".$bill_no."' AND remark !=''";
    $lr_rematks_res = mysqli_query($conn,$lr_rematks);
    $remark_count   = mysqli_num_rows($lr_rematks_res);
    $total_count    =  $num + $remark_count;
    $total_weight += $fri_row['weight'];
    $total_other  += $lrs['other_charges'];
    $total_amount += $fri_row['total_amnt'];
    ?>
    <tr>
      <td><center><?php echo ++$i;?></center></td>
      <td><center><?php echo risFormatDate($fri_row['l_date']);?></center></td>
      <td><center><?php echo $lrs['lr_no'];?></center></td>
      <td><center><?php echo $fri_row['truck_no'];?></center></td>
      <td><center><?php echo $lrs['lr_from'];?></center></td>
      <td><center><?php echo $lrs['lr_to'];?></center></td>
      <td><center><?php echo $fri_row['weight'];?></center></td>
      <td><center><?php echo customFormatMoney($lrs['other_charges']);?></center></td>
      <td><center><?php echo customFormatMoney($lrs['lr_amount']);?></center></td>
    </tr>
    <?php if($lrs['remark'] !=''){ ?>
    <tr>
      <td colspan="9" align="center"><?php echo $lrs['remark'];?></td>
    </tr>
    <?php }else{ ?>

    <?php } ?>
    <?php $i++; } ?>
  </tbody>
</table>
<table class="print-bill-blank">
  <?php if( $total_count < 14 ) { ?>
      <?php for ($j=1; $j <= ( 14 - $total_count ); $j++) { ?> 
          <tr>
            <td>&nbsp;</td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
      <?php } ?>
    <?php } ?>
</table>
<table class="table table-bordered table-striped print-bill-bottom print-bill-bottom45">
  <tbody>
    <tr>  
      <td rowspan="5" colspan="5" width="55%"><label class="control-label"><strong>GST PAID BY</strong>: <?php echo $gst_paid_row['gst_paid_by'];?></label><hr/>
      <label class="control-label"><strong>BANK NAME</strong>: <?php echo $bank_det_rows['bank_name'];?></label><br/>
      <label class="control-label"><strong>Account</strong>: <?php echo $bank_det_rows['account_no'];?></label><br/>
      <label class="control-label"><strong>BRANCH</strong>: <?php echo $bank_det_rows['branch'];?></label><br/>
      <label class="control-label"><strong>IFS Code</strong>: <?php echo $bank_det_rows['ifsc_code'];?></label>
      </td>
      
      <td rowspan="5" width="15%"><b>TOTAL</b></td>
      <td align="center" class="height20" width="10%"><?php echo customFormatMoney($total_weight);?></td>
      <td align="center" class="height20" width="10%"><?php echo customFormatMoney($total_other);?></td>
      <td align="right" class="height20" width="10%"><?php echo customFormatMoney($total_amount);?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style;?>"><?php echo @$master_tax[$bill_add[0]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($bill_add[0]['amount'] != ''){
        echo $bill_add[0]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style;?>"><?php echo @$master_tax[$bill_add[1]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($bill_add[1]['amount'] != ''){
        echo $bill_add[1]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style;?>"><?php echo @$master_tax[$bill_add[2]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($bill_add[2]['amount'] != ''){
        echo $bill_add[2]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style;?>"><?php echo @$master_tax[$bill_add[3]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($bill_add[3]['amount'] != ''){
        echo $bill_add[3]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td rowspan="4" colspan="5">
        <label class="control-label"><strong>GSTIN No.</strong>: <?php echo $bank_det_rows['gst']?></label><br/>
        <label class="control-label"><strong>State Code</strong>: <?php echo $state_code?></label><br/>
        <label class="control-label"><strong>PAN</strong>: <?php echo $bank_det_rows['
        ']?></label>
      </td>
      <td colspan="1" rowspan="4">
        <label class="control-label"><strong>REMARK</strong>: <?php echo $bill_row['reamrks']?></label>
      </td>
      <td align="right" colspan="2" style="<?php echo $style?>"><?php echo @$master_tax[$sub[0]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($sub[0]['amount'] != ''){
        echo $sub[0]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style?>"><?php echo @$master_tax[$sub[1]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($sub[1]['amount'] != ''){
        echo $sub[1]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style?>"><?php echo @$master_tax[$sub[2]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($sub[2]['amount'] != ''){
        echo $sub[2]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>  
      <td align="right" colspan="2" style="<?php echo $style?>"><?php echo @$master_tax[$sub[3]['title']];?></td>
      <td align="right" colspan="2"><?php 
      if($sub[3]['amount'] != ''){
        echo $sub[3]['amount']; 
      }else{
        echo "";
      }
      ?></td>
    </tr>
    <tr>
      <td colspan="6" align="right"><b>Net Amount</b></td>
      <td align="right" colspan="3"><?php 
      $net_amnt = $total_add - $total_sub + $total_other + $total_amount;
      echo customFormatMoney($net_amnt);
      ?></td>
    </tr>
    <tr>
      <td colspan="4" rowspan="2"><label>Total Amount in Words : <br/> <?php echo convertNumber2Words($net_amnt);?></label> <br/> Subject to <span style="text-transform: capitalize;"><?php echo $branch_row['branch'];?></span> Jurisdiction</td>
    </tr>
    <tr>
      <td colspan="6" align="right"><label class="control-label">For,<?php echo $bank_det_rows['name'];?><br/><br/> Auth. Signature</label></td>
      
    </tr>
  </tbody>
</table>
<script src="assets/custom.js"></script>