<?php 
  require_once '../assets/plugins/tcpdf/tcpdf.php'; 

  $pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  $pdf->SetAutoPageBreak(TRUE, 0);
$pdf->SetMargins(5, 5, 5);
  include("../config.php");
  $id = $_GET['p1'];  
  $select = "SELECT * FROM `general_sales` WHERE `id` = '".$id."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
  $result = mysqli_query($conn,$select);
  $row    = mysqli_fetch_assoc($result);

  $vendor     = "SELECT * FROM `vendor_master` WHERE `id` = '".$row['vendor_id']."'";
  $vendor_res = mysqli_query($conn,$vendor);
  $vendor_row = mysqli_fetch_assoc($vendor_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);

  $general_sales_data = mysqli_query($conn, "SELECT * FROM `general_sales_details` WHERE `gs_id` = '".$id."'");
  $data = array();
  while ($general_sales_row = mysqli_fetch_assoc($general_sales_data)) {
  $data[] = $general_sales_row;
  }

  //ADD 
  $genral_add     = "SELECT * FROM `general_sales_tax_history` WHERE `type` = 'add' AND `gs_id` =  '".$id."' ORDER BY id DESC";
  $bill_add_res = mysqli_query($conn,$genral_add);

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

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

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

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

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

  //LESS 
  $genral_sub     = "SELECT * FROM `general_sales_tax_history` WHERE `type` = 'sub' AND `gs_id` =  '".$id."' ORDER BY id DESC";
  $bill_sub_res = mysqli_query($conn,$genral_sub);

  $genral_sub = array();
  $total_sub  = 0;
  while ($genral_sub_row = mysqli_fetch_assoc($bill_sub_res)) {
  $genral_sub[] = array('title' => $genral_sub_row['tax_mst_id'], 'amount' => $genral_sub_row['amount']);
  $total_sub += $genral_sub_row['amount'];
  }
  $master_tax = combine(getAllRecords('tax_mst'), 'id', 'descr');

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

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

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

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

  $state_code       = substr($bank_det_rows['gst'],0,2);

    $total_count = count($data);
   $output   = ''; 
$output .= '<!DOCTYPE html>
<html>
   <body>
      
      <table cellpadding="2">
        <tr>
          <td style="text-align:center;width:100%;vertical-align: baseline;" colspan="5"><img src="../assets/logo/pawan_logistics.png"><hr/></td>
        </tr>
        <tr>
          <td style="width:100%;text-align:center;font-size:12px;"><b>TAX INVOICE</b></td>
        </tr>
        <tr>
          <td style="width:75%;text-align:left;"><b>Party Name : '.$vendor_row['name'].'</b></td>
          <td style="width:10%;text-align:right;"><b>Bill No </b></td>
          <td style="width:15%;text-align:left;"><b>: '.$row['bill_no'].'</b></td>
        </tr>
        <tr>
          <td style="width:75%;text-align:left;"><b>'.$vendor_row['address'].'</b></td>
          <td style="width:10%;text-align:right;"><b>Date </b></td>
          <td style="width:15%;text-align:left;"><b>: '.risFormatDate($row['bill_date']).'</b></td>
        </tr>
        <tr>
          <td style="width:25%;text-align:left;"><b>City : '.$vendor_row['city'].'</b></td>
          <td style="width:75%;text-align:left;"><b>Pincode : '.$vendor_row['pincode'].'</b></td>
        </tr>
        <tr>
          <td style="width:100%;text-align:left;"><b>GST No. : '.$vendor_row['gstin'].'</b></td>
        </tr>
        <tr>
          <td style="width:5%;text-align:center;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black"><b> SR. NO.</b></td>

          <td style="width:10%;text-align:center;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black"><b> LR. NO.</b></td>

          <td style="width:10%;text-align:center;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black"><b> DATE</b></td>

          <td style="width:29%;text-align:center;border-top:1px solid black;border-right:1px solid black;"><b> PARTICULARS</b></td>
          <td style="width:15%;text-align:center;border-top:1px solid black;border-right:1px solid black;"><b> UOM</b></td>
          <td style="width:5%;text-align:center;border-top:1px solid black;border-right:1px solid black;"><b> QTY</b></td>
          <td style="width:11%;text-align:center;border-top:1px solid black;border-right:1px solid black;"><b> RATE</b></td>
          <td style="width:15%;text-align:right;border-top:1px solid black;border-right:1px solid black;"><b> AMOUNT(Rs.)</b></td>
        </tr>';
        foreach ($data as $datas) {
          
          $amount   += $datas['amount'];
          $quntity  += $datas['qty'];
          $final_amount = $amount + $total_add + $total_sub;
        $output .='
        <tr>
          <td style="width:5%;text-align:center;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black">'.++$i.'</td>
          <td style="width:10%;text-align:center;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black">'.$datas['lr_no'].'</td>
          <td style="width:10%;text-align:center;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black">'.risFormatDate($row['working_strat_date'],'d/m/Y').'</td>
          <td style="width:29%;text-align:left;border-top:1px solid black;border-right:1px solid black;">'.$datas['remark'].'</td>
          <td style="width:15%;text-align:center;border-top:1px solid black;border-right:1px solid black;">'.$datas['uom'].'</td>
          <td style="width:5%;text-align:center;border-top:1px solid black;border-right:1px solid black;">'.$datas['qty'].'</td>
          <td style="width:11%;text-align:center;border-top:1px solid black;border-right:1px solid black;">'.$datas['rate'].'</td>
          <td style="width:15%;text-align:right;border-top:1px solid black;border-right:1px solid black;">'.customFormatMoney($datas['amount']).'</td>
        </tr>';}
        $output .='
         ';if($total_count < 17) {
           for ($j=1; $j <= ( 17 - $total_count ); $j++) {
           $output .='
        <tr>
          <td style="width:5%;text-align:center;border-left:1px solid black;border-right:1px solid black"></td>
          <td style="width:10%;text-align:center;border-left:1px solid black;border-right:1px solid black"></td>
          <td style="width:10%;text-align:center;border-left:1px solid black;border-right:1px solid black"></td>
          <td style="width:29%;text-align:center;border-right:1px solid black;"></td>
          <td style="width:15%;text-align:center;border-right:1px solid black;"></td>
          <td style="width:5%;text-align:center;border-right:1px solid black;"></td>
          <td style="width:11%;text-align:center;border-right:1px solid black;"></td>
          <td style="width:15%;text-align:center;border-right:1px solid black;"></td>
        </tr>';
           }
           }
        $output .='
        <tr>
          <td style="width:69%;text-align:left;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;border-bottom:1px solid black;"><b> TOTAL </b></td>
          <td style="width:5%;text-align:right;border-top:1px solid black;border-right:1px solid black;border-bottom:1px solid black;"><b>'.customFormatMoney($quntity).'</b></td>
          <td style="width:11%;text-align:right;border-top:1px solid black;border-right:1px solid black;border-bottom:1px solid black;"><b>TOTAL</b></td>
          <td style="width:15%;text-align:right;border-top:1px solid black;border-right:1px solid black;border-bottom:1px solid black;"><b>'.customFormatMoney($amount).'</b></td>
        </tr>
        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> TAXABLE VALUE</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.customFormatMoney($amount).'</b></td>
        </tr>
        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_add[0]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_amnt_0.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_add[1]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_amnt_1.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_add[2]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_amnt_2.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_add[3]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_amnt_3.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_sub[0]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_sub_amnt_0.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_sub[1]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_sub_amnt_1.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_sub[2]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_sub_amnt_2.'</b></td>
        </tr>

        <tr>
          <td style="width:85%;text-align:right;border-left:1px solid black;border-right:1px solid black"><b> '.@$master_tax[$genral_sub[3]['title']].'</b></td>
          <td style="width:15%;text-align:right;border-right:1px solid black;"><b>'.$genral_sub_amnt_3.'</b></td>
        </tr>
        <tr>
          <td style="width:59%;text-align:left;border-top:1px solid black;border-left:1px solid black;"><b> </b></td>
          <td style="width:26%;text-align:right;border-top:1px solid black;border-right:1px solid black"><b> NET AMOUNT</b></td>
          <td style="width:15%;text-align:right;border-top:1px solid black;border-right:1px solid black;"><b>'.customFormatMoney($final_amount).'</b></td>
        </tr>
        <tr>
          <td rowspan="2" style="width:100%;text-align:left;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black"><b> AMOUNT IN WORDS : '.convertNumber2Words($final_amount).'</b></td>
        </tr>
        <tr>
          <td><b></b></td>
        </tr>
        <tr>
          <td style="width:100%;text-align:left;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black"><b> REMARKS : '.$row['remark'].'</b></td>
        </tr>
        <tr>
          <td style="width:50%;text-align:left;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;"><b> COMPANY BANK DETAIL</b></td>
          <td style="width:50%;text-align:left;border-top:1px solid black;border-right:1px solid black;"><b> GST NO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: '.$bank_det_rows['gst'].'</b></td>
        </tr>
        <tr>
          <td style="width:50%;text-align:left;border-left:1px solid black;border-right:1px solid black;"><b> BANK NAME : '.$bank_det_rows['bank_name'].'</b></td>
          <td style="width:50%;text-align:left;border-right:1px solid black;"><b> PAN NO. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: '.$bank_det_rows['panno'].'</b></td>
        </tr>
        <tr>
          <td style="width:50%;text-align:left;border-left:1px solid black;border-right:1px solid black;"><b> A/c No. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: '.$bank_det_rows['account_no'].'</b></td>
          <td style="width:50%;text-align:left;border-right:1px solid black;"><b> STATE CODE : '.$state_code.'</b></td>
        </tr>
        <tr>
          <td style="width:50%;text-align:left;border-bottom:1px solid black;border-left:1px solid black;border-right:1px solid black;"><b> IFSC CODE &nbsp;&nbsp;&nbsp;: '.$bank_det_rows['ifsc_code'].'</b></td>
          <td style="width:50%;text-align:left;border-bottom:1px solid black;border-right:1px solid black;"><b></b></td>
        </tr>';
        
        $output .='
        <tr>
          <td style="width:60%;text-align:left;border-left:1px solid black;"><b></b></td>
          <td rowspan="3" style="width:40%;text-align:right;border-right:1px solid black;"><img src="../assets/logo/pawan_logistics_sign.png" style="height:80px;"></td>
        </tr>';
        
        $output .='
        <tr>
          <td style="width:60%;text-align:left;border-left:1px solid black;"><b></b></td>
          
        </tr>';
        
        $output .='
        <tr>
          <td style="width:60%;text-align:left;border-left:1px solid black;"><b></b></td>
        </tr>';
     $output .='
        <tr>
          <td style="width:60%;text-align:left;border-bottom:1px solid black;border-left:1px solid black;"><b></b></td>
          <td style="width:40%;text-align:right;border-bottom:1px solid black;border-right:1px solid black;"><b> FOR, '.$bank_det_rows['name'].' </b></td>
        </tr>
      </table>
   </body>
</html>';
/*echo'<pre>';
print_r($output);
echo'</pre>';
die();*/

$pdf->SetPrintHeader(true);
$pdf->SetPrintFooter(true);

$pdf->SetFont('helvetica', '', 7.9, '', true);

$pdf->AddPage('P', 'A4');
$pdf->writeHTML($output, true, false, false, false, '');

$pdf->SetTitle($title);
$pdf->IncludeJS("print();");
$pdf->Output($fname);

?>