2025-04-14 13:28:09 +05:30
< ? php
error_reporting ( E_ERROR | E_PARSE );
include ( 'includes/config/config.php' );
include ( 'log_entry.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 " );
}
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' ];
2025-10-16 15:19:10 +05:30
$vehicle = $_POST [ 'vehicle' ];
2025-04-14 13:28:09 +05:30
$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' ];
$sub_center = $_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' ];
// 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 stock_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 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 ;
// 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 stock_issue set issue_ref_no=' $isuue_ref_no ' , " ;
}
$ohc_type_id = $_SESSION [ 'current_ohcttype' ];
error_log ( " $filled_by " . " " . $amb_num_box_code . ' rrrr' . $_SESSION [ 'RoleCode' ]);
if ( $_SESSION [ 'RoleCode' ] != 'DISC' && $_SESSION [ 'RoleCode' ] != 'VTIC' && $_SESSION [ 'RoleCode' ] != 'HDS' ) {
2025-10-16 15:19:10 +05:30
$query = $initquery . " vehicle=' " . $vehicle . " ',remarks=' " . $remarks . " ', status=' " . $_POST [ 'approval_status' ] . " ',issue_date = STR_TO_DATE(' " . $issue_date . " ','%d/%m/%Y') ,role_type = ' " . $_SESSION [ 'role_type' ] . " ', issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ',approving_doc_emp_id=' " . $_SESSION [ 'logged_user_empid' ] . " ',sub_center=' $sub_center ' ,modified_by = ' " . $_SESSION [ 'user_id' ] . " ' $endquery " ;
2025-04-14 13:28:09 +05:30
} else {
2025-10-16 15:19:10 +05:30
$query = $initquery . " vehicle=' " . $vehicle . " ',remarks=' " . $remarks . " ', ohc_location_id=' " . $_POST [ 'ohc_loc' ] . " ',issue_date = STR_TO_DATE(' " . $issue_date . " ','%d/%m/%Y') ,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' ] . " ',stock_issue_emp_id = ' " . $_SESSION [ 'logged_user_empid' ] . " ',modified_by = ' " . $_SESSION [ 'user_id' ] . " ' ,sub_center=' $sub_center ',distribution_vendor=' " . $distribution_vendor . " ',program_name=' " . $program_name . " ',activity_name=' " . $activity_name . " ',grand_total=' " . $grand_total . " ',payment_method=' " . $payment_method . " ' $endquery " ;
2025-04-14 13:28:09 +05:30
}
//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 stock_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 );
*
* }
*/
}
resetStockAtStoreLevelForStore ( $stock_issue_id );
// clear existing entries into the child table
$query_del = " delete from stock_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 ( $_POST [ " item_id $i " ] != '' && $_POST [ " item_id $i " ] != null ) {
// echo $i;
error_log ( ' rrr ' . $i );
/* $current_stock__query = " select stock_qty,expiry_date from item_stock where item_id= $_POST["item_id$i"] and item_batch_no=' " . $_POST [ " 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', $_POST["item_id$i"], 'item_batch_no',$item_batch_no));
$item_id = $_POST [ " 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 ( $_POST [ " issue_qty $i " ]) && $_POST [ " issue_qty $i " ] != null && $_POST [ " issue_qty $i " ] != '' && $_POST [ " issue_qty $i " ] > 0 ) {
if ( $_POST [ 'approval_status' ] != 'R' ) {
//$stock_qty = floatval ( $row_stocks[0] )+ floatval ( $_POST["issue_qty$i"] );
updateStockAtStoreLevel ( $_POST [ " item_id $i " ], $_POST [ " item_batch_no $i " ], $_POST [ " issue_qty $i " ]);
}
}
}
// echo $current_stock_level;
/* $current_stock_query = " update item_stock set stock_qty=' " . $stock_qty . " ' where item_id=' $_POST["item_id$i"] ' and item_batch_no=' $_POST["item_batch_no$i"] ' " ;
// echo $current_stock_query;
mysqli_query ( $current_stock_query ); */
/*
* $current_stock_level = getTableFieldValue ( 'tbl_items' , 'current_stock_level' , 'item_id' , $_POST [ " 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=' $_POST["item_id$i"] ' " ;
* echo $current_stock_query ;
* mysqli_query ( $conn , $current_stock_query );
*/
// $batch_ref_no=$_POST["item_id$i"].'_'.${"batch$i"};
$query1 = " insert into stock_issue_items set stock_issue_id=' $stock_issue_id ', item_batch_no=' " . $_POST [ " item_batch_no $i " ] . " ', item_id =' " . $_POST [ " item_id $i " ] . " ',issue_qty = ' " . $_POST [ " issue_qty $i " ] . " ',per_unit_rate = ' " . $_POST [ " per_unit_rate $i " ] . " ' ,subsidy_percent = ' " . $_POST [ " subsidy_percent $i " ] . " ' ,payable_price = ' " . $_POST [ " payable_pricemis $i " ] . " ' ,total_rate = ' " . $_POST [ " total_rate $i " ] . " ',payment_method=' " . $_POST [ " payment_method $i " ] . " ',transaction_remarks=' " . $_POST [ " 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 = '" . $_POST["item_id$i"] . "'
AND item_batch_no = '" . $_POST["item_batch_no$i"] . "'
AND ohc_location_id = '$ohc_type_id' 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' ] + $_POST [ " issue_qty $i " ];
$update_query = " UPDATE sub_center_stock SET stock_qty = ' $new_stock_qty ' WHERE item_id = ' " . $_POST [ " item_id $i " ] . " ' AND item_batch_no = ' " . $_POST [ " item_batch_no $i " ] . " ' AND ohc_location_id = ' $ohc_type_id ' 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 = ' " . $_POST [ " item_id $i " ] . " ',item_batch_no = ' " . $_POST [ " item_batch_no $i " ] . " ', hariyali_rate = ' " . $_POST [ " per_unit_rate $i " ] . " ' ,per_unit_rate = ' " . $_POST [ " rate $i " ] . " ', stock_qty = ' " . $_POST [ " issue_qty $i " ] . " ', expiry_date = ' " . $_POST [ " expiry_date $i " ] . " ', ohc_location_id = ' $ohc_type_id ' ,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='".$_POST["item_id$i"]."'";
}
}
}
if ( $data == null ) {
$data [ 'status' ] = 200 ;
$data [ 'message' ] = " Data not found! " ;
}
echo json_encode ( $data );