<?php 
require_once '../assets/plugins/tcpdf/tcpdf.php'; 
include("../config.php");
$bill_no  = $_GET['p1'];
$pdf      = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetAutoPageBreak(TRUE, 0);
$pdf->SetMargins(5, 5, 5);

$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);

$bill             =  "SELECT * FROM `billing` WHERE `invoice_no` = '".$bill_no."'";
$bill_res         = mysqli_query($conn,$bill);
$bill_row         = mysqli_fetch_assoc($bill_res);
$common_remark    = $bill_row['remarks'];
$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;
}

$total_count      =  count($lrs);
$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']); 
  $countaddone = '1';
}else{
  $bill_amnt_0 = '';
}

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

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

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

$count_add_row = $countaddone + $countaddtwo + $countaddthree + $countaddfour;

$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']);
 $countsub1 = '1';
 $style = ''; 
}else{
 $sub_0 = '';
 $style = "height:17px";
}

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

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

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

$count_sub_row = $countsub1 + $countsub2 + $countsub3 + $countsub4;

$total_add_less_row = (8 - ($count_sub_row + $count_add_row));

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

$output  = ''; 
$output .= '<!DOCTYPE html>
<html>
   <head>
      <title>BILL COPY</title>
   </head>
   <body>
      <table cellpadding="3">
      ';
      
      $output .='
          <tr>
            <td style="width:100%;"><img src="../assets/logo/pawan_logistics.png"></td>
          </tr>
          
          <tr>
            <td style="text-align:center;width:100%;font-size:11px;font-weight:bolder;">FREIGHT INVOICE</td>
          </tr>
          <tr>
            <td style="text-align:left;width:65%;font-weight:bolder;border-top:1px solid black;border-left:1px solid black;">M/s. : '.$vendor_row['name'].'</td>
            <td style="text-align:left;width:35%;font-weight:bolder;border-top:1px solid black;border-right:1px solid black;border-left:1px solid black;"> Bill No : P - '.substr($bill_row['invoice_no'], 9).'</td>
          </tr>
          <tr>
            <td rowspan="2" style="text-align:left;width:65%;border-left:1px solid black;">'.$vendor_row['address'].'</td>
            <td style="text-align:left;width:35%;font-weight:bolder;border-right:1px solid black;border-left:1px solid black;"> Date &nbsp;&nbsp;&nbsp;&nbsp;: '.risFormatDate($bill_row['bill_date']).'</td>
          </tr>
          <tr>
            <td style="text-align:left;width:35%;font-weight:bolder;border-right:1px solid black;border-left:1px solid black;"></td>
          </tr>
          <tr>
            <td style="text-align:left;width:65%;font-weight:bolder;border-left:1px solid black;">GST No. : '.$vendor_row['gstin'].'</td>
            <td style="text-align:left;width:35%;font-weight:bolder;border-right:1px solid black;border-left:1px solid black;"></td>
          </tr>
          
          <tr>
            <td style="width:5%;text-align:center;font-weight:bolder;border-top:1px solid black;border-left:1px solid black;">SR.<br/>NO</td>

            <td style="width:7%;text-align:left;font-weight:bolder;border-top:1px solid black;">LR.NO</td>

            <td style="width:10%;text-align:left;font-weight:bolder;border-top:1px solid black;"> DATE</td>

            <td style="width:20%;text-align:left;font-weight:bolder;border-top:1px solid black;">LOCATION</td>

            <td style="width:11%;text-align:center;font-weight:bolder;border-top:1px solid black;">VEHICLE NO</td>

            <td style="width:17%;text-align:left;font-weight:bolder;border-top:1px solid black;">VEHICLE TYPE</td>

            <td style="width:10%;text-align:left;font-weight:bolder;border-top:1px solid black;">INVOICE</td>

            <td style="width:10%;text-align:center;font-weight:bolder;border-top:1px solid black;">CARGO WEIGHT</td>

            <td style="width:10%;text-align:right;font-weight:bolder;border-top:1px solid black;border-right:1px solid black;">AMOUNT</td>
          </tr>';
           $i = 1;
           $total_weight = 0;
           $total_other  = 0;
           $total_amount = 0;
           $total_pcs    = 0;
           $line_count   = 0;
           $cou = 1;
          foreach ($lrs as $i => $lrs) {
            $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);
            
            
            $fri      = "SELECT * FROM lr_mst WHERE lr_no = '".$lrs['lr_no']."'";
            $fri_res  = mysqli_query($conn,$fri);
            $fri_row  = mysqli_fetch_assoc($fri_res);

            if($fri_row['packing'] || $fri_row['remark']){
              $line_count += 1;
            }

            $lr_wise_remark = $fri_row['packing'] .''.$fri_row['remark'];

            $total_weight += $fri_row['weight'];
            $total_other  += $lrs['other_charges'];
            $total_amount += $lrs['lr_amount'];
            $net_amnt = $total_add - $total_sub + $total_other + $total_amount;
            $total_pcs += $fri_row['pcs'];

            $truck = mysqli_query($conn, "SELECT * FROM `truck_mst` WHERE `truck_no` = '".$fri_row['truck_no']."'");
            $truck_row = mysqli_fetch_assoc($truck);

            $total_count    =  $num + $remark_count + $line_count;

          $output .='
          <tr>
            <td style="width:5%;text-align:center;border-top:1px solid black;border-left:1px solid black;font-size:7px;">'.$cou++.'</td>

            <td style="width:7%;text-align:left;border-top:1px solid black;font-size:7px;">'.substr($lrs['lr_no'], 5).'</td>

            <td style="width:10%;text-align:left;border-top:1px solid black;font-size:7px;"> '.risFormatDate($fri_row['l_date']).'</td>

            <td style="width:20%;text-align:left;border-top:1px solid black;font-size:7px;">'.$lrs['lr_from'].' - '.$lrs['lr_to'].'</td>

            <td style="width:11%;text-align:center;border-top:1px solid black;font-size:7px;">'.$fri_row['truck_no'].'</td>

            <td style="width:17%;text-align:left;border-top:1px solid black;font-size:7px;">'.$truck_row['lorry_type'].'</td>

            <td style="width:10%;text-align:left;border-top:1px solid black;font-size:7px;">'.$fri_row['party_invoice'].'</td>

            <td style="width:10%;text-align:center;border-top:1px solid black;font-size:7px;">'.$fri_row['weight'].'</td>

            <td style="width:10%;text-align:right;border-top:1px solid black;border-right:1px solid black;font-size:7px;">'.customFormatMoney($lrs['lr_amount']).'</td>
          </tr>
          <tr>
            <td style="width:100%;text-align:center;border-top:1px solid black;border-right:1px solid black;border-left:1px solid black;font-size:7px;">'.$lr_wise_remark.'</td>
          </tr>
          ';
          $i++; }
          if($total_count < 25) {
            for ($j=1; $j <= ( 25 - $total_count ); $j++) {
            $output .='
            <tr>
              <td style="width:10%;text-align:left;font-weight:bolder;border-left:1px solid black;"></td>
              <td style="width:11%;text-align:left;font-weight:bolder;"></td>
              <td style="width:10%;text-align:left;font-weight:bolder;"></td>
              <td style="width:8%;text-align:left;font-weight:bolder;"></td>
              <td style="width:8%;text-align:left;font-weight:bolder;"></td>
              <td style="width:12%;text-align:left;font-weight:bolder;"></td>
              <td style="width:12%;text-align:left;font-weight:bolder;"></td>
              <td style="width:10%;text-align:left;font-weight:bolder;"></td>
              <td style="width:9%;text-align:left;font-weight:bolder;"></td>
              <td style="width:10%;text-align:left;font-weight:bolder;border-right:1px solid black;"></td>
            </tr>
           ';
           }
         }
         if($bill_add[0]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$bill_add[0]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$bill_amnt_0.'</td>
          </tr>';
        }

        if($bill_add[1]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$bill_add[1]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$bill_amnt_1.'</td>
          </tr>';
        }

        if($bill_add[2]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$bill_add[2]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$bill_amnt_2.'</td>
         </tr>';
        }

        if($bill_add[3]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$bill_add[3]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$bill_amnt_3.'</td>
          </tr>';
        }

        if($sub[0]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$sub[0]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$sub_0.'</td>
         </tr>';
        }

        if($sub[1]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$sub[1]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$sub_1.'</td>
         </tr>';
        }

        if($sub[2]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$sub[2]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$sub_2.'</td>
          </tr>';
        }

        if($sub[3]['title'] != ''){
          $output .='
          <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> </td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.@$master_tax[$sub[3]['title']].'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.$sub_3.'</td>
         </tr>';
        }

        $output .='
          <tr>
            <td colspan="6" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black"> Rupees(word): '.convertNumber2Words($net_amnt).'</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> TOTAL Wt.</td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> Total</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.customFormatMoney($total_amount).'</td>
         </tr>
         <tr>
            <td colspan="7" style="font-weight:bolder;text-align:left;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black"> Bill Remarks : - '.$lrs['remarks'].'</td>
            <td colspan="2" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> Net Total</td>
            <td colspan="1" style="font-weight:bolder;text-align:right;border-top:1px solid black;border-right:1px solid black"> '.customFormatMoney($net_amnt).'</td>
         </tr>
         <tr>
            <td style="width:12%;text-align:left;border-left:1px solid black;border-top:1px solid black;font-weight:bolder;"> Bank Details : </td>
            <td style="width:58%;text-align:left;border-right:1px solid black;border-top:1px solid black;font-weight:bolder;"> A/c. Name : '.$bank_det_rows['name'].' A/c. No. : '.$bank_det_rows['account_no'].',</td>
            <td style="width:30%;text-align:right;border-top:1px solid black;border-right:1px solid black;font-weight:bolder;"> E. & O. E.</td>
         </tr>
         <tr>
            <td style="width:12%;text-align:left;border-left:1px solid black;font-weight:bolder;"> </td>
            <td style="width:58%;text-align:left;border-right:1px solid black;font-weight:bolder;"> Bank : '.$bank_det_rows['bank_name'].', Branch : '.$bank_det_rows['branch'].',</td>
            <td style="width:30%;text-align:right;border-right:1px solid black;font-weight:bolder;"> For, '.$bank_det_rows['name'].'</td>
         </tr>
         <tr>
            <td style="width:12%;text-align:left;border-left:1px solid black;font-weight:bolder;"> </td>
            <td style="width:58%;text-align:left;border-right:1px solid black;font-weight:bolder;"> IFSC:- '.$bank_det_rows['ifsc_code'].'</td>
            <td rowspan= "2" style="width:30%;text-align:center;border-right:1px solid black;font-weight:bolder;"> <img src="../assets/logo/pawan_logistics_sign.PNG" style="height:50px;"></td>
         </tr>
         <tr>
            <td style="width:70%;text-align:left;border-right:1px solid black;border-left:1px solid black;border-top:1px solid black;">1. The Payment of the bill should be made only by NEFT/ Account Payee Cheque/DD drawn in favour of M/s. PAWAN LOGISTICS. <br/> 2. All disputes subject to Vadodara Jurisdiction only</td>
         </tr>
         <tr>
            <td style="width:40%;text-align:left;border-left:1px solid black;font-weight:bolder;border-top:1px solid black;border-right:1px solid black;"> PAN No. : '.$bank_det_rows['panno'].'</td>
            <td style="width:30%;text-align:center;border-right:1px solid black;font-weight:bolder;border-top:1px solid black;"> GST NO.: '.$bank_det_rows['gst'].'</td>
            <td style="width:30%;text-align:center;border-right:1px solid black;font-weight:bolder;"> Authorized Signatory</td>
         </tr>
         
          <tr>
            <td style="width:100%;text-align:left;border-left:1px solid black;border-top:1px solid black;font-weight:bolder;border-right:1px solid black;border-bottom:1px solid black;"> GST TAX WILL BE PAID BY SERVICE RECEIPIENT UNDER R.C.M.(Reverse Charge Mechanism)</td>
          </tr>
      </table>
      
   </body>
</html>';
$pdf->SetFont('helvetica', '', 8, '', true);
$pdf->AddPage('P', 'A4');
$pdf->writeHTML($output, true, false, false, false, '');
$pdf->SetTitle('BILL COPY'.'-'.$_GET['p1']);
$pdf->Output('bill_copy'.'_'.$_GET['p1'].'.pdf', 'I');
?>