308 lines
12 KiB
PHP
308 lines
12 KiB
PHP
|
|
<?php
|
||
|
|
error_reporting(E_ERROR | E_PARSE);
|
||
|
|
include('includes/config/config.php');
|
||
|
|
include('log_entry.php');
|
||
|
|
include('includes/functions.php');
|
||
|
|
save_log($requestStr, 'save_direct_item_issue', 'SAVE', 'save_direct_item_issue.php');
|
||
|
|
// error_log('sagar');
|
||
|
|
$noOfRows = $_POST['count_items'];
|
||
|
|
// echo $noOfRows;
|
||
|
|
$stock_issue_id = $_POST['stock_issue_id'];
|
||
|
|
error_log('$stock_issue_id '.$stock_issue_id);
|
||
|
|
// $procurement_refno = $_POST['ref_no'];
|
||
|
|
// echo $procurement_refno;
|
||
|
|
$remarks = $_POST['remarks'];
|
||
|
|
$ambulance_number = $_POST['ambulance_number'];
|
||
|
|
$box_number = $_POST['box_number'];
|
||
|
|
$issue_date = $_POST['issue_date'];
|
||
|
|
$grand_total = $_POST['grand_total'];
|
||
|
|
$payment_method = $_POST['payment_method'];
|
||
|
|
$filled_by = $_SESSION['logged_user_empid'];
|
||
|
|
$rolecode = $_SESSION['RoleCode'];
|
||
|
|
// echo $procurement_id;
|
||
|
|
$initquery = "";
|
||
|
|
$data = array();
|
||
|
|
$data['approval_status'] = $_POST['approval_status'];
|
||
|
|
$endquery = "";
|
||
|
|
$isuue_ref_no = "";
|
||
|
|
|
||
|
|
$ohc_loc = $_POST['ohc_loc'];
|
||
|
|
$beneficiary = $_POST['beneficiary'];
|
||
|
|
$user_id = $_SESSION['user_id'];
|
||
|
|
|
||
|
|
$staff_id = getFieldFromTable('emp_id','tbl_users','user_id',$user_id);
|
||
|
|
$staff_category = getFieldFromTable('staff_category','staff_master','staff_id',$staff_id);
|
||
|
|
$sub_center = getFieldFromTable('sub_center','staff_master','staff_id',$staff_id);
|
||
|
|
|
||
|
|
$distribution_vendor = $_POST['distribution_vendor'];
|
||
|
|
|
||
|
|
$program_name = $_POST['program_name'];
|
||
|
|
|
||
|
|
$activity_name = $_POST['activity_name'];
|
||
|
|
|
||
|
|
|
||
|
|
// $ohc_loc_type = getTableFieldValue('ohc_type', 'ohc_category', 'ohc_type_id', $ohc_loc);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
if ($ohc_loc == '3') {
|
||
|
|
$amb_num_box_code = $_POST['ambulance_number'];
|
||
|
|
// error_log("ifsagar" . $amb_num_box_code);
|
||
|
|
} else {
|
||
|
|
$amb_num_box_code = $_POST['box_number'];
|
||
|
|
|
||
|
|
// error_log("elsesagar" . $amb_num_box_code);
|
||
|
|
}
|
||
|
|
|
||
|
|
// error_log("sagar".$ambulance_number);
|
||
|
|
if (!empty($stock_issue_id)) {
|
||
|
|
// update case
|
||
|
|
$initquery = "update hari_sub_center_issue set ";
|
||
|
|
$endquery = " where stock_issue_id = '" . $stock_issue_id . "' ";
|
||
|
|
$data['stock_issue_id'] = $stock_issue_id;
|
||
|
|
// error_log("sagarif".$initquery);
|
||
|
|
} else {
|
||
|
|
// insert case
|
||
|
|
$month = date('n');
|
||
|
|
$year = date('Y');
|
||
|
|
|
||
|
|
if ($month > 3) {
|
||
|
|
$year = $year;
|
||
|
|
} else {
|
||
|
|
$year = $year - 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
$query_issue__ref_no = "select max( CAST(substring(issue_ref_no,locate('-',issue_ref_no)+1,length(issue_ref_no)-(5+locate('-',issue_ref_no))) as UNSIGNED) ) from hari_sub_center_issue where issue_ref_no like '%$year%' ";
|
||
|
|
$result_issue_ref_no = @mysqli_query($conn, $query_issue__ref_no);
|
||
|
|
$row_isuue_ref_no = mysqli_fetch_row($result_issue_ref_no);
|
||
|
|
$isuue_ref_no = 'ISS-' . ($row_isuue_ref_no[0] + 1) . '-' . $year;
|
||
|
|
// error_log("sagarelse".$query_issue_ref_no);
|
||
|
|
/*
|
||
|
|
* if(mysqli_num_rows($result_issue_ref_no) > 0){
|
||
|
|
* $row_isuue_ref_no = mysqli_fetch_row($result_issue_ref_no);
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* $isuue_ref_no = 'ISS-'.($isuue_ref_no[0]+1).'-'.$year;
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* }else{
|
||
|
|
* $isuue_ref_no = 'ISS-1-'.$year;
|
||
|
|
*
|
||
|
|
* }
|
||
|
|
*/
|
||
|
|
$initquery = "INSERT INTO hari_sub_center_issue set issue_ref_no='$isuue_ref_no' ,";
|
||
|
|
}
|
||
|
|
// $ohc_type_id = $_SESSION['current_ohcttype'];
|
||
|
|
$ohc_type_id = getFieldFromTable('ohc_type_id','ohc_type','ohc_code','HS');
|
||
|
|
$use_id = $_SESSION['user_id'];
|
||
|
|
$staff_id = getFieldFromTable('emp_id', 'tbl_users', 'user_id', $use_id);
|
||
|
|
$sub_c_id = getFieldFromTable('sub_center', 'staff_master', 'staff_id', $staff_id);
|
||
|
|
|
||
|
|
error_log("$filled_by" . " " . $amb_num_box_code.' rrrr'. $_SESSION['RoleCode']);
|
||
|
|
|
||
|
|
|
||
|
|
$query = $initquery . " remarks='" . $remarks . "', ohc_location_id='" . $_POST['ohc_loc'] . "', filled_by='" . $filled_by . "', amb_no_box_code='" . $amb_num_box_code . "', status='" . $_POST['approval_status'] . "',stock_issue_emp_id = '" . $_SESSION['logged_user_empid'] . "', issue_date = STR_TO_DATE('" . $issue_date . "','%d/%m/%Y') ,modified_by = '" . $_SESSION['user_id'] . "' ,issue_ohc_type_id='".$ohc_type_id."',sub_center='$sub_c_id',beneficiary='" . $beneficiary . "',distribution_vendor='" . $distribution_vendor . "',program_name='" . $program_name . "',activity_name='" . $activity_name . "',grand_total='" . $grand_total . "',payment_method='" . $payment_method . "' $endquery ";
|
||
|
|
|
||
|
|
//echo $query;
|
||
|
|
error_log("sagar: " . $query);
|
||
|
|
|
||
|
|
if (!$result = @mysqli_query($conn, $query)) {
|
||
|
|
exit(mysqli_error($conn));
|
||
|
|
} else {
|
||
|
|
|
||
|
|
if (empty($_POST['stock_issue_id'])) {
|
||
|
|
|
||
|
|
// insert case when procuremnt id is not available but needed for saving into the child table
|
||
|
|
$query = "select stock_issue_id from hari_sub_center_issue where issue_ref_no= '$isuue_ref_no' ";
|
||
|
|
//echo $query;
|
||
|
|
if (!$result = @mysqli_query($conn, $query)) {
|
||
|
|
exit(mysqli_error($conn));
|
||
|
|
} else {
|
||
|
|
if (mysqli_num_rows($result) > 0) {
|
||
|
|
while ($row = mysqli_fetch_assoc($result)) {
|
||
|
|
$data = $row;
|
||
|
|
$stock_issue_id = $row['stock_issue_id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!empty($stock_issue_id)) {
|
||
|
|
|
||
|
|
/*
|
||
|
|
* $current_stock_level_procurement_items_query="select item_id,qty from requisition_items where req_id='".req_id."' ";
|
||
|
|
* $results_procuerment_items=mysqli_query($conn,$current_stock_level_procurement_items_query);
|
||
|
|
* echo $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']);
|
||
|
|
* $current_stock_level_items_qty=$current_stock_level_items_qty-$rows_procurement_items['qty'];
|
||
|
|
* echo current_stock_level_items_qty;
|
||
|
|
* $current_stock_query="update tbl_items set current_stock_level='".$current_stock_level_items_qty."' where item_id='".$rows_procurement_items['item_id']."' ";
|
||
|
|
* echo $current_stock_query;
|
||
|
|
* mysqli_query($conn,$current_stock_query);
|
||
|
|
*
|
||
|
|
* }
|
||
|
|
*/
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
resetStockAtStoreLevelForStorehari($stock_issue_id);
|
||
|
|
|
||
|
|
// clear existing entries into the child table
|
||
|
|
$query_del = " delete from hari_sub_center_issue_items where stock_issue_id = '" . $stock_issue_id . "' ";
|
||
|
|
// echo $query_del;
|
||
|
|
$result_del = @mysqli_query($conn, $query_del);
|
||
|
|
|
||
|
|
for ($i = 0; $i < $noOfRows; $i++) {
|
||
|
|
if (${"item_id$i"} != '' && ${"item_id$i"} != null) {
|
||
|
|
// echo $i;
|
||
|
|
error_log(' rrr ' . $i);
|
||
|
|
/*$current_stock__query="select stock_qty,expiry_date from item_stock where item_id=${"item_id$i"} and item_batch_no='".${"item_batch_no$i"}."' ";
|
||
|
|
$results_stock__query=mysqli_query($conn,$current_stock__query);
|
||
|
|
$row_stocks = mysqli_fetch_row ( $results_stock__query );*/
|
||
|
|
//$stock_qty = getTableFieldValue ( 'item_stock', 'stock_qty', 'item_id', ${"item_id$i"}, 'item_batch_no',$item_batch_no));
|
||
|
|
|
||
|
|
$item_id = ${"item_id$i"};
|
||
|
|
$sql_check_group = "SELECT gi.* FROM group_items gi WHERE gi.group_id = '" . $item_id . "'";
|
||
|
|
error_log("group item minus:" . $sql_check_group);
|
||
|
|
$resultc = mysqli_query($conn, $sql_check_group);
|
||
|
|
$row_company = mysqli_fetch_array($resultc);
|
||
|
|
$group_item_exists = $row_company['item_id'];
|
||
|
|
|
||
|
|
if ($group_item_exists) {
|
||
|
|
do {
|
||
|
|
error_log("golu");
|
||
|
|
|
||
|
|
// Get the required quantity
|
||
|
|
$required_qty = $row_company['qty'];
|
||
|
|
|
||
|
|
// Query the item_stock table for batches with larger quantities
|
||
|
|
$sql_check_stock = "SELECT * FROM item_stock WHERE item_id = '" . $row_company['item_id'] . "' AND stock_qty >= '" . $required_qty . "' LIMIT 1";
|
||
|
|
error_log("auto batch update:" . $sql_check_stock);
|
||
|
|
|
||
|
|
$result_stock = mysqli_query($conn, $sql_check_stock);
|
||
|
|
$stock_row = mysqli_fetch_array($result_stock);
|
||
|
|
|
||
|
|
if ($stock_row) {
|
||
|
|
// Update stock if a suitable batch is found
|
||
|
|
|
||
|
|
updateStockAtStoreLevel($stock_row['item_id'], $stock_row['item_batch_no'], $required_qty);
|
||
|
|
} else {
|
||
|
|
// Handle case where no suitable stock batch is found
|
||
|
|
error_log("No suitable batch found for item_id: " . $row_company['item_id']);
|
||
|
|
// You can add a popup or notification logic here
|
||
|
|
}
|
||
|
|
|
||
|
|
$row_company = mysqli_fetch_array($resultc); // Fetch the next row
|
||
|
|
} while ($row_company);
|
||
|
|
} else {
|
||
|
|
// Handle the case when no group item exists
|
||
|
|
error_log("No group item found for item_id: " . $item_id);
|
||
|
|
|
||
|
|
|
||
|
|
$is_group_item = '';
|
||
|
|
if (isset(${"issue_qty$i"}) && ${"issue_qty$i"} != null && ${"issue_qty$i"} != '' && ${"issue_qty$i"} > 0) {
|
||
|
|
if ($_POST['approval_status'] != 'R') {
|
||
|
|
//$stock_qty = floatval ( $row_stocks[0] )+ floatval ( ${"issue_qty$i"} );
|
||
|
|
updateStockAtStoreLevel(${"item_id$i"}, ${"item_batch_no$i"}, ${"issue_qty$i"});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// echo $current_stock_level;
|
||
|
|
/*$current_stock_query = "update item_stock set stock_qty='" . $stock_qty . "' where item_id='${"item_id$i"}' and item_batch_no='${"item_batch_no$i"}' ";
|
||
|
|
// echo $current_stock_query;
|
||
|
|
mysqli_query ( $current_stock_query );*/
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* $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);
|
||
|
|
*/
|
||
|
|
|
||
|
|
// $batch_ref_no=${"item_id$i"}.'_'.${"batch$i"};
|
||
|
|
$query1 = "insert into hari_sub_center_issue_items set stock_issue_id='$stock_issue_id',item_batch_no='" . ${"item_batch_no$i"} . "', item_id ='" . ${"item_id$i"} . "',issue_qty = '" . ${"issue_qty$i"} . "',per_unit_rate = '" . ${"per_unit_rate$i"} . "' ,subsidy_percent = '" . ${"subsidy_percent$i"} . "' ,payable_price = '" . ${"payable_pricemis$i"} . "' ,total_rate = '" . ${"total_rate$i"} . "',payment_method='" . ${"payment_method$i"} . "',transaction_remarks='" . ${"transaction_remarks$i"} . "' ";
|
||
|
|
error_log($query1);
|
||
|
|
if (!$result1 = @mysqli_query($conn, $query1)) {
|
||
|
|
|
||
|
|
exit(mysqli_error($conn));
|
||
|
|
|
||
|
|
|
||
|
|
}else{
|
||
|
|
// Check query
|
||
|
|
$check_query = "SELECT stock_qty
|
||
|
|
FROM sub_center_stock
|
||
|
|
WHERE item_id = '" . ${"item_id$i"} . "'
|
||
|
|
AND item_batch_no = '" . ${"item_batch_no$i"} . "'
|
||
|
|
AND sub_center = '$sub_center'
|
||
|
|
ORDER BY id DESC
|
||
|
|
LIMIT 1";
|
||
|
|
|
||
|
|
error_log($check_query); // Log the check query
|
||
|
|
|
||
|
|
$check_result = mysqli_query($conn, $check_query);
|
||
|
|
|
||
|
|
if (!$check_result) {
|
||
|
|
// Log the error if query fails
|
||
|
|
error_log("Error executing check query: " . mysqli_error($conn));
|
||
|
|
exit(); // Exit to avoid further errors
|
||
|
|
}
|
||
|
|
|
||
|
|
error_log("Check query executed successfully");
|
||
|
|
|
||
|
|
// Log to see if any rows were found
|
||
|
|
if (mysqli_num_rows($check_result) > 0) {
|
||
|
|
// Item exists, fetch and update the stock_qty
|
||
|
|
$row = mysqli_fetch_assoc($check_result);
|
||
|
|
$new_stock_qty = $row['stock_qty'] - ${"issue_qty$i"};
|
||
|
|
|
||
|
|
$update_query = "UPDATE sub_center_stock SET stock_qty = '$new_stock_qty',sub_center='$sub_center', expiry_date = '" . ${"expiry_date$i"} . "' WHERE item_id = '" . ${"item_id$i"} . "' AND item_batch_no = '" . ${"item_batch_no$i"} . "' AND sub_center='$sub_center'";
|
||
|
|
|
||
|
|
error_log($update_query . " - Updating stock");
|
||
|
|
|
||
|
|
$update_result = mysqli_query($conn, $update_query);
|
||
|
|
if (!$update_result) {
|
||
|
|
error_log("Error updating stock: " . mysqli_error($conn));
|
||
|
|
} else {
|
||
|
|
error_log("Stock updated successfully");
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// No rows found, insert a new record
|
||
|
|
$insert_query = "INSERT INTO sub_center_stock SET item_id = '" . ${"item_id$i"} . "',item_batch_no = '" . ${"item_batch_no$i"} . "', stock_qty = '" . ${"issue_qty$i"} . "', expiry_date = '" . ${"expiry_date$i"} . "',sub_center = $sub_center";
|
||
|
|
|
||
|
|
error_log($insert_query . " - Inserting new stock");
|
||
|
|
|
||
|
|
$insert_result = mysqli_query($conn, $insert_query);
|
||
|
|
if (!$insert_result) {
|
||
|
|
error_log("Error inserting stock: " . mysqli_error($conn));
|
||
|
|
} else {
|
||
|
|
error_log("Stock inserted successfully");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
error_log("passed query:" . $query1);
|
||
|
|
|
||
|
|
|
||
|
|
// echo $query1;
|
||
|
|
|
||
|
|
|
||
|
|
// $item_stock_query="select current_stock_level from tbl_items where item_id='".${"item_id$i"}."'";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ($data == null) {
|
||
|
|
$data['status'] = 200;
|
||
|
|
$data['message'] = "Data not found!";
|
||
|
|
}
|
||
|
|
echo json_encode($data);
|