<?php 
include("../config.php");

$where  = '';
$start_date   = $_GET['start_date'];
$end_date     = $_GET['end_date'];
$owner_name   = $_GET['owner_name'];
$agent_name   = $_GET['agent_name'];
$lhc_no   = $_GET['lhc_no'];

if($start_date != '' && $start_date != 'null' && !empty($start_date) && $end_date != '' && $end_date != 'null' && !empty($end_date)){
    $where .= ' AND  lhc_date >= "'.risFormatDate($start_date, 'Y-m-d').'" AND lhc_date <= "'.risFormatDate($end_date, 'Y-m-d').'"';
}

if($owner_name != '' && $owner_name != 'null' && !empty($owner_name)){
    $where .= ' AND  owner_name = "'.$owner_name.'"';
}

if($agent_name != '' && $agent_name != 'null' && !empty($agent_name)){
    $where .= ' AND  agent = "'.$agent_name.'"';
}

if($lhc_no != '' && $lhc_no != 'null' && !empty($lhc_no)){
    $where .= ' AND  lhc_no = "'.$lhc_no.'"';
}

/*echo "<pre>";
print_r("SELECT * FROM `lhc` WHERE balance > 0 $where");
echo "</pre>";*/

//$query = mysqli_query($conn, "SELECT * FROM `lhc` WHERE balance > 0 $where");
$query = mysqli_query($conn, "SELECT DISTINCT `lhc_no`,`lhc_date`,`truck_no`,`from_place`,`to_place`,`agent`,`owner_name`,
	`lorry_hire`,`net_amnt`,`adv_amnt`,`balance` FROM `lhc` 
	WHERE balance > 0 and  `branch_id`='".$_SESSION['branch_id']."' AND 
	`company_id`='".$_SESSION['company_id']."' $where");
$data = array();
while ($row = mysqli_fetch_assoc($query)) {
	$data[] = $row;
}

$table = '';
$master_owner = combine(getAllRecords('truck_mst'), 'id', 'o_name');
if(count($data) > 0){
	$cou = 1;
	foreach ($data as $key => $datas) {

		$trans = mysqli_query($conn, "SELECT * FROM `trans` WHERE `voucher_no` = '".$datas['lhc_no']."' AND `branch_id`='".$_SESSION['branch_id']."' AND `company_id`='".$_SESSION['company_id']."'");
		$trans_row = mysqli_fetch_assoc($trans);
		
		if($master_owner[$datas['owner_name']] !=''){
		    $display = "display:block";
		}else{
		    $display = "display:none";
		}
		$paid_amt = $datas['net_amnt'] - $datas['balance'];
		$table .='
		<tr>
			<td style="width:2%;">
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][sr_no]" class="form-control lhc-content top add-more-table sr_no" readonly tabindex="-1" value="'.$cou++.'" data-count="'.$key.'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][lhc_no]" class="form-control lhc-content top add-more-table lhc_no" readonly tabindex="-1" value="'.$datas['lhc_no'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][lhc_date]" class="form-control lhc-content top add-more-table lhc_date" readonly tabindex="-1" value="'.risFormatDate($datas['lhc_date'],'d-m-Y').'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][truck_no]" class="form-control lhc-content top add-more-table truck_no" readonly tabindex="-1" value="'.$datas['truck_no'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][from_place]" class="form-control lhc-content top add-more-table from_place" readonly tabindex="-1" value="'.$datas['from_place'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][to_place]" class="form-control lhc-content top add-more-table to_place" readonly tabindex="-1" value="'.$datas['to_place'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][agent]" class="form-control lhc-content top add-more-table agent" readonly tabindex="-1" value="'.$datas['agent'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px; " class="form-control lhc-content top add-more-table lr_cal" readonly tabindex="-1" value="'.@$master_owner[$datas['owner_name']].'">

			<input type="hidden" name="lhc['.$key.'][owner_name]" class="form-control lhc-content top add-more-table owner_name" readonly tabindex="-1" value="'.$datas['owner_name'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][lorry_hire]" class="form-control lhc-content top add-more-table lorry_hire" readonly tabindex="-1" value="'.$datas['net_amnt'].'">
			</td>
			
			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][adv_amnt]" class="form-control lhc-content top add-more-table adv_amnt" readonly tabindex="-1" value="'.$datas['adv_amnt'].'">
			</td>
			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][adv_amnt]" class="form-control lhc-content top add-more-table adv_amnt" readonly tabindex="-1" value="'.$paid_amt.'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][balance]" class="form-control lhc-content top add-more-table balance" readonly tabindex="-1" value="'.$datas['balance'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][other_add_amount]" class="form-control lhc-content top add-more-table other_add_amount number" min="0.001" autofocus>

			<input type="hidden" name="lhc['.$key.'][last_other_add_amount]" class="form-control lhc-content top add-more-table other_add_amount number" min="0.001" autofocus value="'.$trans_row['other_add'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][other_less_amount]" class="form-control lhc-content top add-more-table other_less_amount number" min="0.001" autofocus>

			<input type="hidden" name="lhc['.$key.'][last_other_less_amount]" class="form-control lhc-content top add-more-table other_less_amount number" min="0.001" autofocus value="'.$trans_row['other_less'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][last_balance]" class="form-control lhc-content top add-more-table last_balance" readonly tabindex="-1" value="'.$datas['balance'].'">
			</td>

			<td>
			<input type="text" style="font-size:10px;" name="lhc['.$key.'][adjust_amount]" class="form-control lhc-content top add-more-table adjust_amount number" min="0.001" autofocus>

			<input type="hidden" name="lhc['.$key.'][last_adjust_amount]" class="form-control lhc-content top add-more-table adjust_amount number" min="0.001" autofocus value="'.$trans_row['adjust'].'">
			</td>

			<td>
			 <select style="font-size:10px;"  name="lhc['.$key.'][payment_to]" class="form-control lhc-content top add-more-table payment_to" tabindex="-1">
                 <option value="AGENT" style="'.$display.'">AGENT</option>
                 <option value="OWNER" style="'.$display.'">OWNER</option>
              </select>
			</td>
		</tr>
		';
	}
}else{
	$table .='
	<tr>
		<td colspan="15">NO RECORD FOUND</td>
	</tr>';
}

$data = array('status' => 'success', 'tbody' => $table);
echo json_encode($data);
die();
?>