<?php
error_reporting(E_ERROR | E_PARSE);
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
$initial_query = "";
$end_query = "";
$noRows = $_POST['count_items'];
$indent_id = $_POST['indent_id0'];
error_log("indent id:" . $indent_id);
// $item_id=$_POST['item_id'];
// error_log("item id ".$_POST['items_id']);
$indent_date = $_POST['indent_date0'];
$remarks = $_POST['remarks0'];
$ref_no = $_POST['indent_ref_no0'];
$rolecode = $_SESSION['RoleCode'];
error_log("rows " . $noRows);
//for dirct send for approval on creation 
$status = 'PENDING';
//
// $item_id= implode(', ', $item_id);
// $indent_qty= implode(', ', $indent_qty);
error_log("indent qty " . $_POST['indent_qty']);
$data = array();

begin();
if (!empty($indent_id)) {

	$initial_query = "update indent_master set ";
	$end_query = "where indent_id='" . $_POST['indent_id0'] . "' and ohc_type_id = '" . $_SESSION['current_ohcttype'] . "' ";
} else {
	$month = date('n');
	$year = date('Y');

	if ($month > 3) {
		$year = $year;
	} else {
		$year = $year - 1;
	}

	$query_indent_ref_no = "select  max( CAST(substring(indent_ref_no,locate('-',indent_ref_no)+1,length(indent_ref_no)-(5+locate('-',indent_ref_no))) as UNSIGNED) ) from indent_master where indent_ref_no like '%$year%'";
	$result_indent_ref_no = @mysqli_query($conn, $query_indent_ref_no);
	$row_indent_ref_no = mysqli_fetch_row($result_indent_ref_no);
	$ref_no = 'IND-' . ($row_indent_ref_no[0] + 1) . '-' . $year;

	$initial_query = "insert into  indent_master set indent_ref_no='$ref_no', ohc_type_id = '" . $_SESSION['current_ohcttype'] . "' ,";
	$end_query = "";
}
if ($rolecode == 'DOC') {
	$final_query = $initial_query . " indent_date=STR_TO_DATE('" . $indent_date . "','%d/%m/%Y'),remarks='" . $remarks . "',approving_doc_id='" . $_SESSION['user_id'] . "' " . $end_query;
} else {
	$final_query = $initial_query . " indent_date=STR_TO_DATE('" . $indent_date . "','%d/%m/%Y'),remarks='" . $remarks . "',generated_by='" . $_SESSION['user_id'] . "', status='$status' " . $end_query;
}

error_log("indent:: " . $final_query);
if (!$result = @mysqli_query($conn, $final_query)) {
	error_log("Error saving indent:" . mysqli_error($conn) . ": Query Failing: " . $final_query);
	rollback();
	die(error_log(mysqli_error($conn)));
} else {

	if (empty($_POST['indent_id0'])) {

		// insert case when procuremnt id is not available but needed for saving into the child table
		$query = "select indent_id from indent_master where indent_date=STR_TO_DATE('" . $indent_date . "','%d/%m/%Y') and ohc_type_id = '" . $_SESSION['current_ohcttype'] . "'";
		//echo $query;
		error_log("INDENT ID QUERY::" . $query);
		if (!$result = @mysqli_query($conn, $query)) {
			error_log("no result found: Query Failing: " . $query);
			//	rollback();
			//die(error_log(mysql_error()));
		} else {
			if (mysqli_num_rows($result) > 0) {
				while ($row = mysqli_fetch_assoc($result)) {
					$data = $row;
					$indent_id = $row['indent_id'];
					error_log("INDENT ID::" . $indentId);
				}
			}
		}
	}
}

// clear existing entries into the child table
$query_del = " delete from indent_items where indent_id = '" . $indent_id . "' and ohc_type_id = '" . $_SESSION['current_ohcttype'] . "'   ";
error_log("indent delete: " . $query_del);

if (!$result_del = @mysqli_query($conn, $query_del)) {
	rollback();
	die(error_log(mysqli_error($conn)));
}

for ($i = 0; $i < $noRows; $i++) {

	error_log("ROWS::" . $noRows);

	// error_log("INDENT ID::" .$indentId);

	if (isset(${"indent_qty$i"}) && ${"indent_qty$i"} != null && ${"indent_qty$i"} != '' && ${"indent_qty$i"} > 0 && ${"indent_qty$i"} != 0) {



		$query1 = "insert into indent_items set indent_id='" . $indent_id . "', item_id ='" . ${"item_id$i"} . "',indent_qty='" . ${"indent_qty$i"} . "',modified_by = '" . $_SESSION['user_id'] . "', ohc_type_id = '" . $_SESSION['current_ohcttype'] . "'  ";
		error_log("indent item:: " . $query1);
		if (!$result1 = @mysqli_query($conn, $query1)) {
			error_log("Exception saving stock_issue_items:" . mysqli_error($conn) . " failing query:" . $query1);
			rollback();
			die(mysqli_error($conn));
		}
	}

	// $item_stock_query="select current_stock_level from tbl_items where item_id='".${"item_id$i"}."'";
}

commit();

if ($data == null) {
	$data['status'] = 200;
	$data['message'] = "Data not found!";
}
echo json_encode($data);