ESH/save_procurement.php
2024-10-23 18:28:06 +05:30

234 lines
10 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
error_log("Start Printing Request Attributes");
$requestStr = "";
foreach ($_REQUEST as $key => $value) {
$requestStr .= $key . " : " . $value . "\n";
error_log($key . " : " . $value . "<br />\r\n");
}
error_log("End Printing Request Attributes");
save_log($requestStr, 'Procurement', 'SAVE', 'save_procurement.php');
$noOfRows = $_POST['count_items'];
begin();
//echo $noOfRows;
$procurement_id = $_POST['procurement_id'];
$procurement_refno = $_POST['ref_no'];
//echo $procurement_refno;
$remarks = $_POST['remarks'];
$procurement_date = $_POST['procurement_date'];
$invoice_no = $_POST['invoice_no'];
$ohc_id = $_SESSION['current_ohcttype'];
echo $procurement_id;
$initquery = "";
//$data = array();
$endquery = "";
if (!empty($procurement_id)) {
//update case
$initquery = "update procurement set";
$endquery = " where procurement_id = '" . $procurement_id . "' and ohc_type_id='" . $ohc_id . "' ";
} else {
//insert case
$procurement_refno = creatingProcurementNo();
$initquery = "INSERT INTO procurement set ohc_type_id='" . $ohc_id . "', ";
}
$query = $initquery . " procurement_refno = '" . strtoupper($procurement_refno) . "',invoice_no='" . $invoice_no . "',remarks='" . $remarks . "', procurement_date = STR_TO_DATE('" . $procurement_date . "','%d/%m/%Y') ,modified_by = '" . $_SESSION['user_id'] . "' $endquery ";
echo $query;
error_log("query: " . $query);
if (!$result = @mysqli_query($conn, $query)) {
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
} else {
if (empty($_POST['procurement_id'])) {
//insert case when procuremnt id is not available but needed for saving into the child table
$query = "select max(procurement_id) as procurement_id from procurement where procurement_refno= '" . strtoupper($procurement_refno) . "' and ohc_type_id = '" . $ohc_id . "' ";
//echo $query;
if (!$result = @mysqli_query($conn, $query)) {
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
} else {
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data = $row;
$procurement_id = $row['procurement_id'];
}
}
}
}
if (!empty($procurement_id)) {
$current_stock_level_procurement_items_query = "select item_id,qty,batch from procurement_items where procurement_id='" . $procurement_id . "' and ohc_type_id = '" . $ohc_id . "' ";
$results_procuerment_items = mysqli_query($conn, $current_stock_level_procurement_items_query);
error_log("current store level for proc " . $current_stock_level_procurement_items_query);
while ($rows_procurement_items = mysqli_fetch_assoc($results_procuerment_items)) {
//$current_stock_level_items_qty= getTableFieldValue('tbl_items','current_stock_level','item_id',$rows_procurement_items['item_id']);
$stock_qty = getTableFieldValue('item_stock', 'stock_qty', 'item_id', $rows_procurement_items['item_id'], 'item_batch_no', "'" . $rows_procurement_items['batch'] . "'");
$current_stock_level_items_qty = $stock_qty - $rows_procurement_items['qty'];
//echo current_stock_level_items_qty;
$current_stock_query = "update item_stock set stock_qty='" . $current_stock_level_items_qty . "' where item_id='" . $rows_procurement_items['item_id'] . "' and item_batch_no='" . $rows_procurement_items['batch'] . "' and ohc_type_id = '" . $ohc_id . "' ";
//$current_stock_query="update tbl_items set current_stock_level='".$current_stock_level_items_qty."' where item_id='".$rows_procurement_items['item_id']."' ";
error_log("current stock update query in edit " . $current_stock_query);
if (!@mysqli_query($conn, $current_stock_query)) {
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
}
}
//echo $stock_qty;
/*if($stock_qty!=null && $stock_qty!=""){
$stock_qty=$stock_qty-${"item_qty$i"};
$current_stock_query="update item_stock set stock_qty='".$stock_qty."' where item_id='${"item_id$i"}' and item_batch_no='${"batch$i"}' ";
}
mysqli_query($conn,$current_stock_query);*/
}
//clear existing entries into the child table
$query_del = " delete from procurement_items where procurement_id = '" . $procurement_id . "' and ohc_type_id = '" . $ohc_id . "' ";
error_log("delete query for pro: " . $query_del);
$result_del = @mysqli_query($conn, $query_del);
for ($i = 0; $i < $noOfRows; $i++) {
//echo $i;
if (isset(${"item_qty$i"}) || ${"item_qty$i"} != null || ${"item_qty$i"} != '' || ${"item_qty$i"} > 0) {
/*$current_stock_level= getTableFieldValue('tbl_items','current_stock_level','item_id',${"item_id$i"});
$current_stock_level=$current_stock_level-${"item_qty$i"};
//echo $current_stock_level;
$current_stock_query="update tbl_items set current_stock_level='".$current_stock_level."' where item_id='${"item_id$i"}' ";
echo $current_stock_query;
mysqli_query($conn,$current_stock_query);*/
$stock_qty = getTableFieldValue('item_stock', 'stock_qty', 'item_id', ${"item_id$i"}, 'item_batch_no', "'" . ${"batch$i"} . "'");
//echo $stock_qty;
if ($stock_qty != null && $stock_qty != "") {
$stock_qty = $stock_qty + ${"item_qty$i"};
error_log("to update stock that is present in stock " . $stock_qty);
$current_stock_query = "update item_stock set stock_qty='" . $stock_qty . "' where item_id='${"item_id$i"}' and item_batch_no='${"batch$i"}' and ohc_type_id = '" . $ohc_id . "' ";
error_log("to update stock query " . $current_stock_query);
} else {
$stock_qty = $stock_qty + ${"item_qty$i"};
error_log("to insert first time in stock " . $stock_qty);
$current_stock_query = $current_stock_query = "insert into item_stock set stock_qty='" . $stock_qty . "' , item_id='${"item_id$i"}' , item_batch_no='${"batch$i"}',mrp='${"mrp$i"}',discount='${"discount$i"}',net_value='${"net_value$i"}',expiry_date='" . ${"expiry_date$i"} . "', ohc_type_id = '" . $ohc_id . "' ";
error_log("to insert stock query " . $current_stock_query);
}
//echo $current_stock_level;
//echo $current_stock_query;
if (!@mysqli_query($conn, $current_stock_query)) {
error_log("mysql error:" . mysqli_error($conn));
error_log("mysqli error des:" . mysqli_error($conn));
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
}
$query1 = "insert into procurement_items set procurement_id='$procurement_id', item_id ='" . ${"item_id$i"} . "',qty = '" . ${"item_qty$i"} . "',batch ='" . ${"batch$i"} . "',per_unit_rate='${"per_unit_rate$i"}',tax='${"tax$i"}',net_value='${"net_value$i"}',expiry='" . ${"expiry_date$i"} . "',remarks='" . ${"remarks$i"} . "' ,modified_by = '" . $_SESSION['user_id'] . "', ohc_type_id = '" . $ohc_id . "' ";
if (!$result1 = @mysqli_query($conn, $query1)) {
error_log("item insert query in pro items " . $query1);
error_log("mysql error insert pro:" . mysqli_errno($conn));
error_log("mysqli error des insert pro:" . mysqli_error($conn));
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
}
if (!empty($_POST['ind_id'])) {
$change_indent_status = "update indent_items set status='COMPLETED' where indent_id='" . $_POST['ind_id'] . "' and item_grn_no='" . $_POST['ind_item_grn_no'] . "' and item_id='" . ${"item_id$i"} . "' and ohc_type_id='" . $ohc_id . "' ";
error_log("change_indent_status " . $change_indent_status);
if (!$result = @mysqli_query($conn, $change_indent_status)) {
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
}
$change_grn_status = "update grn_items set status='COMPLETED' where indent_id='" . $_POST['ind_id'] . "' and item_grn_no='" . $_POST['ind_item_grn_no'] . "' and item_id='" . ${"item_id$i"} . "'";
error_log("change_grn_item_status " . $change_grn_status);
if (!$result = @mysqli_query($conn, $change_grn_status)) {
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
}
//$item_stock_query="select current_stock_level from tbl_items where item_id='".${"item_id$i"}."'";
$approved = 0;
$sql_approved = "select count(*) as items from indent_items where indent_id='" . $_POST['ind_id'] . "' and status != 'COMPLETED' and ohc_type_id = '" . $ohc_id . "' ";
error_log('approved' . $sql_approved);
if (!$result_approved = @mysqli_query($conn, $sql_approved)) {
exit(mysqli_error($conn));
}
$row_approved = mysqli_fetch_array($result_approved);
$unapproved = $row_approved['items'];
if ($unapproved == 0) {
$sql = "update indent_master set status='COMPLETED' where indent_id='" . $_POST['ind_id'] . "' and ohc_type_id = '" . $ohc_id . "' ";
error_log("sql " . $sql);
if (!$result = @mysqli_query($conn, $sql)) {
exit(mysqli_error($conn));
} else {
$data = "success";
}
}
}
}
////grn master status update
// $approved_grn = 0;
// $sql_approved_grn = "select count(*) as items from grn_items where indent_id='" . $_POST['ind_id'] . "' and status != 'COMPLETED' and item_grn_no='" . $_POST['ind_item_grn_no'] . "' ";
// error_log('approved' . $sql_approved_grn);
// if (!$result_approved_grn = @mysqli_query($conn, $sql_approved_grn)) {
// exit(mysqli_error($conn));
// }
// $row_approved_grn = mysqli_fetch_array($result_approved_grn);
// $unapproved_grn = $row_approved_grn['items'];
// if ($unapproved_grn == 0) {
// $sql_grn = "update grn_master set status='COMPLETED' where indent_id='" . $_POST['ind_id'] . "' and item_grn_no='" . $_POST['ind_item_grn_no'] . "'";
// error_log("sql " . $sql_grn);
// if (!$result_grn = @mysqli_query($conn, $sql_grn)) {
// exit(mysqli_error($conn));
// } else {
// $data = "success";
// }
// }
}
}
commit();
//echo json_encode($data);