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


	$obj = new Datatable($conn);

	$obj->aColumns 		= array('id', 'substr(lhc_no, 1, 10) AS lhc_no', 'lhc_date', 'truck_no', 'lr_no', 'from_place', 'to_place', 'lorry_hire', 'adv_amnt', 'balance', 'payment_to', 'image', 'created_id');
    $obj->eColumns 		= array('id');
    $obj->sIndexColumn 	= "id";
    $obj->sTable 		= "lhc";
    $obj->myWhere 		= " WHERE company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'";
    $obj->groupBy       = ' GROUP BY lhc.lhc_no';
    $obj->datatable_process();

    $cou = 0;
    if(isset($_GET['iDisplayStart'])) {
    	$cou = $_GET['iDisplayStart'];
    }
    $master_user = combine(getAllRecords('users'), 'id', 'name');
    while ($aRow = mysqli_fetch_array($obj->rResult)) {

    	$lbh 	 = "SELECT * FROM lhc_balance_history WHERE lhc_no = '".$aRow['lhc_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    	$lbh_res = mysqli_query($conn,$lbh);
        $lbh_no  = mysqli_num_rows($lbh_res);


        $temp_arr = array();

		$temp_arr[] = ++$cou;
		$temp_arr[] = $aRow['lhc_no'];
		$temp_arr[] = risFormatDate($aRow['lhc_date'], 'd-m-Y');
		$temp_arr[] = $aRow['truck_no'];
		$temp_arr[] = $aRow['lr_no'];
		$temp_arr[] = $aRow['from_place'];
		$temp_arr[] = $aRow['to_place'];
		$temp_arr[] = $aRow['lorry_hire'];
		$temp_arr[] = $aRow['adv_amnt'];
		$temp_arr[] = $aRow['balance'];
		$temp_arr[] = $aRow['payment_to'];
		$temp_arr[] = @$master_user[$aRow['created_id']];
		
		/*if($lbh_no != 0){
			$block 	= "";
			$display= "display:none";
		}else{
			$block 	= "display:none";
			$display= "";
		}*/

		if($aRow['balance'] == 0){
			$css = "display:none";
		}else{
			$css = '';
		}

		$str = '';

		if( hasPermission($_SESSION['user_detail']['role_id'], 'lhc_edit') ) {
		$str .= '<a href="index.php?page=lhc/edit.php&eid='.$aRow['lhc_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT LHC...!!" style='.$css.'><em class="fa fa-pencil"></em></a>&nbsp;';
		}
		$str .= '<a href="index.php?page=lhc/lhc_balance.php&eid='.$aRow['lhc_no'].'" name="edit" class="btn btn-warning" data-toggle="tooltip" data-placement="top" title="EDIT LHC BALANCE...!!" style='.$css.'><em class="fa fa-balance-scale"></em></a>&nbsp;';
		//$str .= '<a href="lhc/sl_lhc.php?eid='.$aRow['lhc_no'].'" name="edit" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="ADVANCE PAYMENT PRINT..!!" target="_blank"><em class="fa fa-print"></em></a>&nbsp;';
		
		/*$str .= '<button type="button" class="btn btn-primary" id="'.$aRow["lhc_no"].'" data-toggle="tooltip" data-placement="top" title="BALANCE PAYMENT PRINT..!!" onclick="get_payment_history(this);" style='.$block.'><em class="fa fa-history"></em></button>&nbsp;';*/

		if( hasPermission($_SESSION['user_detail']['role_id'], 'lhc_delete') ) {
		$str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["lhc_no"].'" data-toggle="tooltip" data-placement="top" title="DELETE LHC..!!" onclick="confirmDelete(this);"><em class="fa fa-trash"></em></button>&nbsp;';
		}

		if($aRow['image'] != ''){
			$str .= '<a href="assets/upload/lhc_document/'.$aRow['image'].'" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="IMAGE..!!" target="_blank"><em class="fa fa-file-image-o"></em></a>&nbsp;';
		}
		$temp_arr[] = $str;

        $obj->output['aaData'][] = $temp_arr;
    }

    echo json_encode($obj->output);
    exit();
?>