<?php 
	include("../config.php");
	include("../Datatable.php");
	
	$obj = new Datatable($conn);

	$obj->aColumns 		= array('id', 'substr(lr_no, 1, 9) AS lr_no', 'l_date', 'truck_no', 'from_place', 'to_place', 'consignor_name', 'consignee_name', 'weight', 'freight', 'agent', 'tracking_status', 'created_id', 'lr_type');
    $obj->eColumns 		= array('id');
    $obj->sIndexColumn 	= "id";
    $obj->sTable 		= " lr_mst";
    $obj->myWhere 		= " WHERE company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(l_date)>='".$_SESSION['from_year']."' AND DATE(l_date)<='".$_SESSION['to_year']."'";
    $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)) {
    	
    	$check = "
			SELECT billing.lr_no
			FROM billing WHERE billing.lr_no = '".$aRow['lr_no']."' AND billing.branch_id = '".$_SESSION['branch_id']."' AND billing.company_id = '".$_SESSION['company_id']."'";
		$check_res = mysqli_query($conn,$check);
		$check_num = mysqli_num_rows($check_res);
		
		$nor 	 = "SELECT * FROM `vendor_master` WHERE `id` = '".$aRow['consignor_name']."'";
		$nor_res = mysqli_query($conn,$nor);
		$nor_row = mysqli_fetch_array($nor_res);

		$nee 	 = "SELECT * FROM `vendor_master` WHERE `id` = '".$aRow['consignee_name']."'";
		$nee_res = mysqli_query($conn,$nee);
		$nee_row = mysqli_fetch_array($nee_res);
		
		if($check_num > 0 || $aRow['tracking_status'] == 'C'){
			$style = "display:none";
		}else{
			$style = "display:-webkit-inline-box";
		}
		
		$rec = mysqli_query($conn, "SELECT * FROM `image_master` WHERE `ref_no` = '".$aRow['lr_no']."'");
		if(mysqli_num_rows($rec) != 0){
			$color = 'btn-success';
		}else{
			$color = 'btn-primary';
		}

		if($aRow['lr_type'] != 'O'){
			$colors = "color:red";
		}else{
			$colors = "";
		}

        $temp_arr = array();
        $temp_arr[] = ++$cou;
		$temp_arr[] = '<a href="#" id='.$aRow["lr_no"].' onclick="viewData(this);" style="'.$colors.'">'.$aRow['lr_no'].'</a>';
		$temp_arr[] = risFormatDate($aRow['l_date'], 'd-m-Y');
		$temp_arr[] = $aRow['truck_no'];
		$temp_arr[] = $aRow['from_place'];
		$temp_arr[] = $aRow['to_place'];
		$temp_arr[] = $nor_row['name'];
		$temp_arr[] = $nee_row['name'];
		$temp_arr[] = $aRow['agent'];
		$temp_arr[] = $aRow['weight'];
		$temp_arr[] = $aRow['freight'];
		$temp_arr[] = @$master_user[$aRow['created_id']];

		$str = '';
		
		if( hasPermission($_SESSION['user_detail']['role_id'], 'lr_edit') ) {
		$str .= '<a href="index.php?page=lr/edit.php&eid='.$aRow['lr_no'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT LR...!!" style='.$style.'><em class="fa fa-pencil"></em></a>&nbsp;';
		}

		$str .= '<a href="index.php?page=lr/permit_add.php&eid='.$aRow['lr_no'].'" name="edit" class="btn '.$color.'" data-toggle="tooltip" data-placement="top" title="UPDATE PERMISSION...!!"><em class="fa fa-truck"></em></a>&nbsp;';
		$str .= '<button type="button" class="btn btn-primary" id="'.$aRow["lr_no"].'" data-toggle="tooltip" data-placement="top" title="LR PRINT..!!" onclick="LrPrint(this);"><em class="fa fa-print"></em></button>&nbsp;';
       

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

		$temp_arr[] = $str;

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

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