<?php
  include("../config.php");
  $strat      = $_REQUEST['start_invoice'];
  $end        = $_REQUEST['end_invoice'];
  $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);

  $lr     = "SELECT * FROM `billing` WHERE invoice_no >='".$strat."' AND invoice_no <='".$end."' GROUP BY invoice_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_remark    = $lr_row['remarks'];
  }
  $output     = ''; 
  $output .= '<!DOCTYPE html>';
  $output .= '<html lang="en">';
  $output .= '<head>';
  $output .= '<meta charset="utf-8">';
  $output .= '<title>INVOICE FOR '.$strat.' - '.$end.'</title>';
  $output .= '<link rel="stylesheet" href="../assets/pdf.css" media="all" />';
  $output .= '<link rel="stylesheet" href="assets/print.css" type="text/css" media="print">';
  $output .= '</head>';
  foreach ($lrs as $lr) {
  $bill_no = $lr['invoice_no'];
  // bill
  $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;
  }

  $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)) {
    $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);
  $output .= '<body>';
  if( isset($_POST['include_header']) && !empty($_POST['include_header']) ) {
    $output .='
    <div class="hide-header-blank">
    <table width="100%">
      <tr>
        <td width="30%" align="left"><img src="../ssk_logo.png" style="width:150px;"></td>
        <td width="50%" align="left"><h5 class="bill-copy-header"><b>Subject to Delhi Jurisdiction</b></h5></td>
        <td width="20%" align="right"><b>Mobile#</b> 8929920007<br/>9813320007<br>7834819005<br/>9619905027</td>
      </tr>
      <tr>
        <td colspan="3">
          <label class="bill-copy-add">Khasra No. 103, Satayam Puram Colony, Jharoda Border, Beside Police Point New Delhi-110072</label><br/>
          <label class="bill-copy-add">E Mail # sskindialogistics@gmail.com Visit Us # www.sskindialogistics.com</label>
      </tr>
    </table><hr/>
    </div>';
  }else{
    $output .='
    <div class="show-header-blank clickable">
      <table width="100%">
        <tbody>';
          for ($j=1; $j <=10 ; $j++) {
            $output .='<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>';  
          }
        $output .='</tbody>
      </table>
    </div><hr/>';  
  }
  $output .='<table class="table no-top-border-table">
  <tbody>
  <tr>
    <td width="55%" colspan="3" class="text-left"><label class="control-label">M/s. <br> '.$vendor_row['name'].' <br>'.$vendor_row['address'].'</label></td>
    
    <td class="text-right" colspan="2"><label class="control-label"><strong class="hitz-top-header">Bill No</strong> : <span>'.$bill_row['invoice_no'].'</span></label><br><label class="control-label"><strong>Date</strong> : <span>'.risFormatDate($bill_row['bill_date']).'</span></label></td>    
  </tr>
  <tr>
    <td colspan="3"><span>GSTIN No : '.$vendor_row['gstin'].' &nbsp;&nbsp;&nbsp; State Code : '.$state_code.'</span></td>
  </tr>

</tbody></table><br/>
<table class="table-border">
  <tbody>
    <tr class="text-center">
      <th class="hitz-td table-th" width="5%">Sr. No.</th>
      <th class="text-left hitz-td table-th" width="10%">Date</th>
      <th class="text-left hitz-td table-th" width="8%">LR No.</th>
      <th class="text-left hitz-td table-th" width="10%">Truck</th>
      <th class="text-left hitz-td table-th" width="15%">From</th>
      <th class="text-left hitz-td table-th" width="15%">To</th>
      <th class="text-right hitz-td table-th" width="13%">Weight Article</th>
      <th class="text-right hitz-td table-th" width="13%">Detention Or Labour Charges.</th>
      <th class="text-right hitz-td table-th" width="10%">Amount</th>
    </tr>';
    $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);
    $total_weight += $fri_row['weight'];
    $total_other  += $lrs['other_charges'];
    $total_amount += $fri_row['total_amnt'];
    $net_amnt = $total_add - $total_sub + $total_other + $total_amount;
    $output .='<tr>
      <td class="table-td">'.++$i.'</td>
      <td class="table-td">'.risFormatDate($fri_row['l_date']).'</td>
      <td class="table-td">'.$lrs['lr_no'].'</td>
      <td class="table-td">'.$fri_row['truck_no'].'</td>
      <td class="table-td">'.$lrs['lr_from'].'</td>
      <td class="table-td">'.$lrs['lr_to'].'</td>
      <td class="table-td text-right">'.$fri_row['weight'].'</td>
      <td class="table-td text-right">'.customFormatMoney($lrs['other_charges']).'</td>
      <td class="table-td text-right">'.customFormatMoney($lrs['lr_amount']).'</td>
    </tr>';
    if($lrs['remark'] !=''){ 
      $output .='<tr><td colspan="9" class="table-td"><center>'.$lrs['remark'].'</center></td></tr>';
    }else{

    }$i++; }
    if( $num < 23 ) {
    $k = 0;
    for ($j=1; $j <= ( 23 - $num ); $j++) {
    $count = $k + $j;
    $output .='<tr>
      <td>&nbsp;</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>';  
    } }
    $output .='
    <tr>
      <td colspan="5" class="bill-border-top border-bottom-none"><strong>GST PAID BY</strong>: '.$gst_paid_row['gst_paid_by'].'</td>
      <td colspan="1" rowspan="5" class="bill-border-top text-center border-left border-right"><b>TOTAL</b></td>
      <td colspan="1" class="bill-border-top text-right border-left">'.customFormatMoney($total_weight).'</td>
      <td colspan="1" class="bill-border-top text-right">'.customFormatMoney($total_other).'</td>
      <td colspan="1" class="bill-border-top text-right">'.customFormatMoney($total_amount).'</td>
    </tr>

    <tr>
      <td colspan="5" class="padding7"><strong>BANK NAME</strong>: '.$bank_det_rows['bank_name'].'</td>
      
      <td colspan="2" class="padding7 text-left">'.@$master_tax[$bill_add[0]['title']].'</td>
      <td colspan="1" class="padding7 text-right">'.$bill_amnt_0.'</td>
    </tr>
    <tr>
      <td colspan="5" class="padding7"><strong>Account</strong>: '.$bank_det_rows['account_no'].'</td>
      
      <td colspan="2" class="padding7 text-left">'.@$master_tax[$bill_add[1]['title']].'</td>
      <td colspan="1" class="padding7 text-right">'.$bill_amnt_1.'</td>
    </tr>

    <tr>
      <td colspan="5" class="padding7"><strong>BRANCH</strong>: '.$bank_det_rows['branch'].'</td>
      
      <td colspan="2" class="padding7 text-left">'.@$master_tax[$bill_add[2]['title']].'</td>
      <td colspan="1" class="padding7 text-right">'.$bill_amnt_2.'</td>
    </tr>
    <tr>
      <td colspan="5" class="padding7"><strong>RTGS Detail : IFS Code</strong>: '.$bank_det_rows['ifsc_code'].'</td>
      
      <td colspan="2" class="padding7 text-left">'.@$master_tax[$bill_add[3]['title']].'</td>
      <td colspan="1" class="padding7 text-right">'.$bill_amnt_3.'</td>
    </tr>
    <tr>
      <td colspan="5" class="bill-border-top"><strong>GSTIN NO</strong>: '.$bank_det_rows['gst'].'</td>
      <td colspan="1" class="bill-border-top text-center border-left border-right"><b>Remarks : </b></td>
      <td colspan="2" class="text-left">'.@$master_tax[$sub[0]['title']].'</td>
      <td colspan="1" class="text-right">'.$sub_0.'</td>
    </tr>

    <tr>
      <td colspan="5" class="padding7"><strong>STATE CODE</strong>: '.$state_code.'</td>
      <td colspan="1" rowspan="4" class="bill-border-top text-center border-left border-right"><b>'.$common_remark.' </b></td>
      <td colspan="2" class="text-left">'.@$master_tax[$sub[1]['title']].'</td>
      <td colspan="1" class="text-right">'.$sub_1.'</td>
    </tr>


    <tr>
      <td colspan="5" class="padding7"><strong>PANCARD</strong>: '.$bank_det_rows['panno'].'</td>
      
      <td colspan="2" class="text-left">'.@$master_tax[$sub[2]['title']].'</td>
      <td colspan="1" class="text-right">'.$sub_2.'</td>
    </tr>

    <tr>
      <td colspan="5" class="padding7"><strong>SEC CODE</strong>: </td>
      
      <td colspan="2" class="text-left">'.@$master_tax[$sub[3]['title']].'</td>
      <td colspan="1" class="text-right">'.$sub_3.'</td>
    </tr>
    <tr>
      <td colspan="7" class="text-right bill-border-top">Taxable Amount</td>
      <td colspan="2" class="text-right bill-border-top">'.customFormatMoney($net_amnt).'</td>
    </tr>
    <tr>
      <td colspan="7" class="text-right bill-border-top">Net Amount</td>
      <td colspan="2" class="text-right bill-border-top">'.customFormatMoney($net_amnt).'</td>
    </tr>
    <tr>
      <td colspan="7" class="bill-border-top"><strong>TOTAL AMOUNT IN WORDS</strong></td>
      <td colspan="2" class="text-right bill-border-top">FOR,'. $bank_det_rows['name'].'</td>
    </tr>';
    for ($i=1; $i <=4 ; $i++) {
    $output .='<tr>
      <td colspan="9"></td>
    </tr>';
    }
    $output .='<tr>
      <td colspan="5" class="padding7">'.convertNumber2Words($net_amnt).'</td>
      <td colspan="4" class="text-right">AUTH. SIGNATURE</td>
    </tr>
    ';
  $output .='</tbody>
</table>
</div>
  ';  


  $output .= '</body>';
  }
  $output .= '</html>';

/*echo'<pre>';
print_r($output);
echo'</pre>';
die();*/
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($output);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

$filename = $pro = $_GET['pro_name'] . "COMPLETION CERTIFICATE";
$dompdf->stream($filename,array("Attachment" => 0));

?>

