282 lines
12 KiB
PHP
282 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');
|
|
|
|
$noOfRows = $_POST['count_items'];
|
|
|
|
$stock_issue_id = $_REQUEST['stock_issue_id'];
|
|
$issue_type = $_REQUEST['issue_type'];
|
|
|
|
$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'];
|
|
$beneficiary_type = $_POST['beneficiary_type'];
|
|
$filled_by = $_SESSION['logged_user_empid'];
|
|
$rolecode = $_SESSION['RoleCode'];
|
|
|
|
$initquery = "";
|
|
$data = array();
|
|
$data['approval_status'] = $_POST['approval_status'];
|
|
$endquery = "";
|
|
$isuue_ref_no = "";
|
|
|
|
$ohc_loc = $_POST['ohc_loc'];
|
|
$beneficiary = $_POST['beneficiary'];
|
|
|
|
|
|
$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'];
|
|
} else {
|
|
$amb_num_box_code = $_POST['box_number'];
|
|
|
|
}
|
|
|
|
if (!empty($stock_issue_id)) {
|
|
$initquery = "update stock_issue set ";
|
|
$endquery = " where stock_issue_id = '" . $stock_issue_id . "' ";
|
|
$data['stock_issue_id'] = $stock_issue_id;
|
|
} else {
|
|
$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 stock_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;
|
|
|
|
$initquery = "INSERT INTO stock_issue set issue_ref_no='$isuue_ref_no' ,";
|
|
}
|
|
|
|
error_log("$filled_by" . " " . $amb_num_box_code);
|
|
|
|
error_log("$filled_by" . " " . $amb_num_box_code);
|
|
if ($_SESSION['RoleCode']!='DISC' && $_SESSION['RoleCode']!='VTIC' && $_SESSION['RoleCode']!='HDS') {
|
|
$query = $initquery . " remarks='" . $remarks . "', ohc_location_id='" . $_POST['ohc_loc'] . "',role_type = '" . $_SESSION['role_type'] . "', issue_ohc_type_id='" . $_SESSION['current_ohcttype'] . "', filled_by='" . $filled_by . "', amb_no_box_code='" . $amb_num_box_code . "', status='" . $_POST['approval_status'] . "',issue_date = STR_TO_DATE('" . $issue_date . "','%d/%m/%Y') ,approving_doc_emp_id='" . $_SESSION['user_id'] . "',modified_by = '" . $_SESSION['user_id'] . "' ,batch_name='" . $batch_name . "',issue_type='" . $issue_type . "',beneficiary_type='" . $beneficiary_type . "',beneficiary='" . $beneficiary . "',distribution_vendor='" . $distribution_vendor . "',program_name='" . $program_name . "',activity_name='" . $activity_name . "',grand_total='" . $grand_total . "',txn_id='".$txn_id."',payment_method='" . $payment_method . "' $endquery ";
|
|
} else {
|
|
$query = $initquery . " remarks='" . $remarks . "', ohc_location_id='" . $_POST['ohc_loc'] . "' ,role_type = '" . $_SESSION['role_type'] . "', issue_ohc_type_id='" . $_SESSION['current_ohcttype'] . "', filled_by='" . $filled_by . "', amb_no_box_code='" . $amb_num_box_code . "', status='" . $_POST['approval_status'] . "', issue_date = STR_TO_DATE('" . $issue_date . "','%d/%m/%Y'),stock_issue_emp_id = '" . $_SESSION['user_id'] . "' ,modified_by = '" . $_SESSION['user_id'] . "' ,batch_name='" . $batch_name . "',issue_type='" . $issue_type . "',beneficiary_type='" . $beneficiary_type . "',beneficiary='" . $beneficiary . "',distribution_vendor='" . $distribution_vendor . "',program_name='" . $program_name . "',activity_name='" . $activity_name . "',grand_total='" . $grand_total . "',payment_method='" . $payment_method . "',txn_id='".$txn_id."' $endquery ";
|
|
}
|
|
error_log("sagar: " . $query);
|
|
|
|
if (!$result = @mysqli_query($conn, $query)) {
|
|
exit(mysqli_error($conn));
|
|
} else {
|
|
|
|
if (empty($_POST['stock_issue_id'])) {
|
|
|
|
$query = "select stock_issue_id from stock_issue where issue_ref_no= '$isuue_ref_no' ";
|
|
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'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
resetStockAtStoreLevelForStore($stock_issue_id);
|
|
|
|
|
|
$query_del = " delete from stock_issue_items where stock_issue_id = '" . $stock_issue_id . "' ";
|
|
|
|
$result_del = @mysqli_query($conn, $query_del);
|
|
|
|
for ($i = 0; $i < $noOfRows; $i++) {
|
|
|
|
if (isset(${"issue_qty$i"}) && ${"issue_qty$i"} != null && ${"issue_qty$i"} != '' && ${"issue_qty$i"} > 0) {
|
|
if ($_POST['approval_status'] != 'R') {
|
|
|
|
$batch_stock = getBatchStock(${"item_id$i"}, ${"item_batch_no$i"});
|
|
|
|
if ($batch_stock < ${"issue_qty$i"}) {
|
|
$issued_qty = ${"issue_qty$i"} - $batch_stock;
|
|
///
|
|
|
|
$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");
|
|
|
|
$required_qty = $row_company['qty'];
|
|
$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) {
|
|
updateStockAtStoreLevel($stock_row['item_id'], $stock_row['item_batch_no'], $required_qty);
|
|
} else {
|
|
error_log("No suitable batch found for item_id: " . $row_company['item_id']);
|
|
}
|
|
$row_company = mysqli_fetch_array($resultc);
|
|
} while ($row_company);
|
|
} else {
|
|
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') {
|
|
updateStockAtStoreLevel(${"item_id$i"}, ${"item_batch_no$i"}, $batch_stock);
|
|
}
|
|
}
|
|
}
|
|
|
|
$query1 = "insert into stock_issue_items set stock_issue_id='$stock_issue_id',item_batch_no='" . ${"item_batch_no$i"} . "', item_id ='" . ${"item_id$i"} . "',issue_qty = '" . $batch_stock . "',per_unit_rate = '" . ${"per_unit_rate$i"} . "' ,subsidy_percent = '" . ${"subsidy_percent$i"} . "' ,payable_price = '" . ${"payable_price$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));
|
|
}
|
|
error_log("passed query:" . $query1);
|
|
|
|
$batchs = getItemBatch(${"item_id$i"}, ${"item_batch_no$i"});
|
|
$b = 0;
|
|
for ($b = 0; $b < sizeof($batchs); $b++) {
|
|
error_log('$issued_qty ' . $issued_qty);
|
|
if ($issued_qty > 0 && $batchs[$b] != null && !empty($batchs[$b]) && $batchs[$b] != '') {
|
|
$this_batch_stock = getBatchStock(${"item_id$i"}, $batchs[$b]);
|
|
if ($issued_qty > $this_batch_stock) {
|
|
$remain = $issued_qty - $this_batch_stock;
|
|
} else {
|
|
$remain = 0;
|
|
}
|
|
$issue = $issued_qty - $remain;
|
|
$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");
|
|
|
|
$required_qty = $row_company['qty'];
|
|
|
|
$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) {
|
|
|
|
updateStockAtStoreLevel($stock_row['item_id'], $stock_row['item_batch_no'], $required_qty);
|
|
} else {
|
|
error_log("No suitable batch found for item_id: " . $row_company['item_id']);
|
|
}
|
|
$row_company = mysqli_fetch_array($resultc); // Fetch the next row
|
|
} while ($row_company);
|
|
} else {
|
|
error_log("No group item found for item_id: " . $item_id);
|
|
|
|
|
|
$is_group_item = '';
|
|
if (isset($issue) && $issue != null && $issue != '' && $issue > 0) {
|
|
if ($_POST['approval_status'] != 'R') {
|
|
updateStockAtStoreLevel(${"item_id$i"}, $batchs[$b], $issue);
|
|
}
|
|
}
|
|
}
|
|
|
|
$query1 = "insert into stock_issue_items set stock_issue_id='$stock_issue_id',item_batch_no='" . $batchs[$b] . "', item_id ='" . ${"item_id$i"} . "',issue_qty = '" . $issue . "',per_unit_rate = '" . ${"per_unit_rate$i"} . "' ,subsidy_percent = '" . ${"subsidy_percent$i"} . "' ,payable_price = '" . ${"payable_price$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));
|
|
}
|
|
error_log("passed query:" . $query1);
|
|
///
|
|
}
|
|
}
|
|
} else {
|
|
///
|
|
$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");
|
|
|
|
$required_qty = $row_company['qty'];
|
|
|
|
$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) {
|
|
updateStockAtStoreLevel($stock_row['item_id'], $stock_row['item_batch_no'], $required_qty);
|
|
} else {
|
|
error_log("No suitable batch found for item_id: " . $row_company['item_id']);
|
|
}
|
|
|
|
$row_company = mysqli_fetch_array($resultc);
|
|
} while ($row_company);
|
|
} else {
|
|
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') {
|
|
updateStockAtStoreLevel(${"item_id$i"}, ${"item_batch_no$i"}, ${"issue_qty$i"});
|
|
}
|
|
}
|
|
}
|
|
|
|
$query1 = "insert into stock_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_price$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));
|
|
}
|
|
error_log("passed query:" . $query1);
|
|
|
|
|
|
///
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($data == null) {
|
|
$data['status'] = 200;
|
|
$data['message'] = "Data not found!";
|
|
}
|
|
echo json_encode($data);
|