<?php

/* Database connection start */
require_once("../includes/config.php");
$get_opr_rgts = getOprRights($_SESSION['userid'], $_REQUEST['pid'], $link1);

/* Database connection end */
// storing  request (ie, get/post) global array to a variable  
$gatepassdata = $_REQUEST;
/////get status//
$arrstatus = getFullStatus("", $link1);
## selected  Request From
if (!empty($_REQUEST['location'])) {
    $location = "location_code ='" . $_REQUEST['location'] . "'";
} else {
    $location = "1";
}
## selected  Status
if (!empty($_REQUEST['product'])) {
    $product = "outcome_part='" . $_REQUEST['product'] . "'";
} else {
    $product = "1";
}

## selected  date range
if (!empty($_REQUEST['daterange'])) {
    $seldate = explode(" - ", $_REQUEST['daterange']);
    $fromdate = $seldate[0];
    $todate = $seldate[1];
} else {
    $seldate = $today;
    $fromdate = date("Y-m-01");
    $todate = $today;
}
$columns = array(
// datatable column index  => database column name
    0 => 'id',
    1 => 'system_ref_no',
    2 => 'location_code',
    3 => 'part_name',
    4 => 'outcome_qty',
    5 => 'purchase_unit',
    6 => 'entry_date'
);
// getting total number records without any search
$sql = "SELECT *";
$sql.=" FROM production_master where " . $location . " and " . $product . " and entry_date >='" . $fromdate . "' and entry_date <='" . $todate . "'";

$query = mysqli_query($link1, $sql) or die("readyproduct-grid_data.php: ER1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.

$query = mysqli_query($link1, $sql) or die("readyproduct-grid_data.php: ER2");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY " . $columns[$gatepassdata['order'][0]['column']] . "   " . $gatepassdata['order'][0]['dir'] . "  LIMIT " . $gatepassdata['start'] . " ," . $gatepassdata['length'] . "   ";
/* $gatepassdata['order'][0]['column'] contains colmun index, $gatepassdata['order'][0]['dir'] contains order such as asc/desc  */
$query = mysqli_query($link1, $sql) or die("readyproduct-grid_data.php: ER3");

$data = array();
$j = 1;
while ($row = mysqli_fetch_array($query)) {  // preparing an array
    $nestedData = array();

    $status = getAnyDetails($row["job_no"], "status", "system_ref_no", " jobcard_master", $link1);
    $clickicon = "<button title='Click to view job details' type='button' class='btn" . $btncolor . "' onClick = openModel('" . $row["job_no"] . "');>" . $row["job_no"] . "</button>";
    $nestedData[] = $j;
    $nestedData[] = $clickicon;
    $nestedData[] = $row["system_ref_no"];
    $nestedData[] = $row["location_code"];
    $nestedData[] = getAnyDetails($row["outcome_part"], "part_name", "partcode", "partcode_master", $link1);
    $nestedData[] = $row["outcome_qty"] . " " . $row["purchase_unit"];
    $nestedData[] = dt_format($row["entry_date"]);
    ////// check if view right is assigned
    if ($get_opr_rgts["view"] == "Y") {
        $nestedData[] = "<div align='center'><a href='jobcard_iqc_view.php?id=" . base64_encode($row['system_ref_no']) . "&location=" . $_REQUEST['location'] . "&product=" . $_REQUEST['product'] . "&daterange=" . $_REQUEST['daterange'] . "" . $pagenav . "' title='View Jobcard IQC Details'><i class='fa fa-eye fa-lg faicon' title='View Jobcard IQC Details'></i></a></div>";
    } else {
        $nestedData[] = "";
    }

    ////// check if jobcard iqc not done
    if ($row['status'] != "18") {
        $nestedData[] = "<div align='center'><a href='jobcardiqc.php?id=" . base64_encode($row['system_ref_no']) . "&location=" . $_REQUEST['location'] . "&product=" . $_REQUEST['product'] . "&daterange=" . $_REQUEST['daterange'] . "" . $pagenav . "' title='IQC View'><i class='fa fa-eye fa-lg faicon' title='IQC View'></i></a></div>";
    } else {
        $nestedData[] = $arrstatus[$row['status']];
    }

    $data[] = $nestedData;
    $j++;
}

$json_data = array(
    "draw" => intval($gatepassdata['draw']), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
    "recordsTotal" => intval($totalData), // total number of records
    "recordsFiltered" => intval($totalFiltered), // total number of records after searching, if there is no searching then totalFiltered = totalData
    "data" => $data   // total data array
);


echo json_encode($json_data);  // send data as json format
?>
