<?php 
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);
$master_truck = combine(getAllRecords('truck_mst'), 'id', 'o_name');
if($_POST['type'] =='A'){
  $gl = "SELECT * FROM `lhc` WHERE agent = '".$_POST['employee_id']."' AND payment_to = 'AGENT' AND balance > 0 AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";

  $lhc      = "SELECT * FROM `lhc` WHERE agent = '".$_POST['employee_id']."' AND payment_to = 'AGENT' AND balance > 0 AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";

  $lhc_res  = mysqli_query($conn,$lhc);
  $lhc_row  = mysqli_fetch_assoc($lhc_res);
  $name     = $lhc_row['agent'];
  $id       = $lhc_row['agent'];
  $type     = 'AGENT';
}else{

  $gl = "SELECT * FROM `lhc` WHERE owner_name = '".$_POST['id']."' AND payment_to = 'OWNER' AND balance > 0 AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";

  $lhc      = "SELECT * FROM `lhc` WHERE owner_name = '".$_POST['id']."' AND payment_to = 'OWNER' AND balance > 0 AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
  
  $lhc_res  = mysqli_query($conn,$lhc);
  $lhc_row  = mysqli_fetch_assoc($lhc_res);
  $name     = @$master_truck[$lhc_row['owner_name']];
  $id       = $lhc_row['owner_name'];
  $type     = 'OWNER';
}

$gl_res         = mysqli_query($conn,$gl);
$total_records  = mysqli_num_rows($gl_res);
if($total_records > 0){
  $style = "display:''";
}else{
  $style = "display:none";
}
?>
<section>
  <div class="card">
    <div class="card-body">
      <?php echo $report_row['header_data']; ?>
      <!-- <a href="report/vendoer_wise_excel.php?type=<?php echo $type;?>&id=<?php echo $id;?>" class="btn btn-primary" style="position: relative;bottom: 5px;"><i class="fa fa-file-excel-o" aria-hidden="true"></i></a> -->
      <table id="example1" class="table table-bordered table-striped list_data">
        <thead>
          <tr style="<?php echo $style;?>">
            <td colspan="13" style="text-align: center;"><span style="font-size: 13px;"><b><?php echo $name;?></span></b></span></td>
            
          </tr>
          
          <tr>
            <th class="text-center" width="50">LHC NO</th>
            <th class="text-center" width="80">DATE</th>
            <th class="text-center" width="50">TRUCK NO</th>
            <th class="text-center" width="150">FROM - TO</th>
            <th class="text-center" width="100">OWNER</th>
            <th class="text-center" width="150">LORRY HIRE CHARGES</th>
            <th class="text-center" width="100">TOTAL ADDITION</th>
            <th class="text-center" width="100">DISEL</th>
            <th class="text-center" width="100">TOTAL DEDUCTION</th>
            <th class="text-center" width="100">ADVANCE</th>
            <th class="text-center" width="50">BANK</th>
            <th class="text-center" width="50">CASH</th>
            <th class="text-center" width="50">PENDING</th>
          </tr>
        </thead>
        <tbody>
          <?php
          $count=$total_records;  if($count>0){ 
            $due = 0;
            while ($row = mysqli_fetch_array($gl_res)) { 
            $due      += $row["balance"];
            $add      = "SELECT SUM(amount) AS total_add FROM lhc_details WHERE type = 'add' AND lhc_id = '".$row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY lhc_id";
            $add_res  = mysqli_query($conn,$add);
            $add_row  = mysqli_fetch_assoc($add_res);
            
            $sub      = "SELECT SUM(amount) AS total_sub FROM lhc_details WHERE type = 'sub' AND lhc_id = '".$row['lhc_no']."' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' GROUP BY lhc_id";
            $sub_res  = mysqli_query($conn,$sub);
            $sub_row  = mysqli_fetch_assoc($sub_res);
            ?>
            <tr class="text-center">
              <td><?php echo $row["lhc_no"]?></td>
              <td><?php echo risFOrmatDate($row["lhc_date"])?></td>
              <td><?php echo $row["truck_no"]?></td>
              <td><?php echo $row["from_place"] ?> - <?php echo $row["to_place"]?></td>
              <td><?php echo @$master_truck[$row['owner_name']]?></td>
              <td><?php echo $row["lorry_hire"]?></td>
              <td><?php echo $add_row["total_add"]?></td>
              <td><?php echo $row["disel_amnt"]?></td>
              <td><?php echo $sub_row["total_sub"]?></td>
              <td><?php echo $row["adv_amnt"]?></td>
              <td><?php echo $row["pay_mode_amnt"]?></td>
              <td><?php echo $row["cash"]?></td>
              <td><?php echo $row["balance"]?></td>
            </tr>
            <?php } } else{?>
              <tr>
               <td colspan="14" align="center"><b><?php echo "No Record Found...!!!!"; ?></b></td>
              </tr>
            <?php } ?>
          <tr>
            <td colspan="12" class="text-right" style="color: red"><b>TOTAL DUE</b></td>
            <td style="color: red" class="text-left"><b><?php echo customFormatMoney($due); ?></b></td>
          </tr>
        </tbody>
      </table>
      
    </div>
  </div>
</section>