2024-10-16 19:18:52 +05:30
< ? php
error_reporting ( E_ERROR | E_PARSE );
include ( 'includes/config/config.php' );
include ( 'log_entry.php' );
include ( 'includes/functions.php' );
$noOfRows = $_POST [ 'count_items' ];
// echo $noOfRows;
$stock_issue_id = $_POST [ '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' ];
$patient_id = $_REQUEST [ 'p_id' ];
$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 = getTableFieldValue ( 'ohc_type' , 'ohc_type_id' , 'ohc_code' , '"' . $_POST [ 'ohc_code_hidden' ] . '"' );
if ( $_POST [ 'ohc_loc' ] == " AMB " ) {
$amb_num_box_code = $_POST [ 'ambulance_number' ];
// error_log("ifsagar" . $amb_num_box_code);
} else if ( $_POST [ 'ohc_loc' ] == " FAB " ) {
$amb_num_box_code = $_POST [ 'box_number' ];
} else if ( $_POST [ 'ohc' ] == 'EMP' ) {
$amb_num_box_code = $_REQUEST [ 'patient_id' ];
}
// 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 . " ' and issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ' " ;
$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 ;
$initquery = " INSERT INTO stock_issue set issue_ref_no=' $isuue_ref_no ' ,issue_ohc_type_id = ' " . $_SESSION [ 'current_ohcttype' ] . " ' , " ;
}
error_log ( " $filled_by " . " " . $amb_num_box_code );
if ( $rolecode == 'DOC' ) {
$query = $initquery . " remarks=' " . $remarks . " ', filled_by=' " . $filled_by . " ', status=' " . $_POST [ 'approval_status' ] . " ',approving_doc_emp_id=' " . $_SESSION [ 'logged_user_empid' ] . " ' ,issue_date = STR_TO_DATE(' " . $issue_date . " ','%d/%m/%Y') ,modified_by = ' " . $_SESSION [ 'user_id' ] . " ' $endquery " ;
} else {
$query = $initquery . " amb_no_box_code=' " . $amb_num_box_code . " ',ohc_location_id=' " . $ohc_loc . " ', remarks=' " . $remarks . " ', filled_by=' " . $filled_by . " ', 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' ] . " ' $endquery " ;
}
//echo $query;
error_log ( " sqlllllll: " . $query );
if ( ! $result = @ mysqli_query ( $conn , $query )) {
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
} 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 ' and issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ' " ;
//echo $query;
error_log ( $query );
if ( ! $result = @ mysqli_query ( $conn , $query )) {
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
} else {
if ( mysqli_num_rows ( $result ) > 0 ) {
while ( $row = mysqli_fetch_assoc ( $result )) {
$data = $row ;
$stock_issue_id = $row [ 'stock_issue_id' ];
}
}
}
}
// clear existing entries into the child table
$query_del = " delete from stock_issue_items where stock_issue_id = ' " . $stock_issue_id . " ' and issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ' " ;
// echo $query_del;
error_log ( $query_del );
$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 ;
if ( $rolecode == 'DOC' || $_POST [ 'approval_status' ] == 'Y' ) {
updateStockAtStoreLevel ( $ { " item_id $i " }, $ { " item_batch_no $i " }, $batch_stock );
if ( $_POST [ 'ohc_loc' ] == " AMB " ) {
updateMiscellaneousStock ( $ { " item_id $i " }, $ { " item_batch_no $i " }, $batch_stock , $ohc_loc , $amb_num_box_code );
}
}
$query_first_issue = " 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 . " ', issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ' " ;
error_log ( $query_first_issue );
if ( ! $result_first_issue = @ mysqli_query ( $conn , $query_first_issue )) {
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
error_log ( " passed query: " . $query_first_issue );
$batchs = getItemBatch ( $ { " item_id $i " }, $ { " item_batch_no $i " });
$b = 0 ;
2024-11-02 18:03:13 +05:30
for ( $b = 0 ; $b < count ( $batchs ); $b ++ ) {
2024-10-16 19:18:52 +05:30
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 ;
// error_log('$remain '.$issued_qty);
if ( $rolecode == 'DOC' || $_POST [ 'approval_status' ] == 'Y' ) {
updateStockAtStoreLevel ( $ { " item_id $i " }, $batchs [ $b ], $issue );
if ( $_POST [ 'ohc_loc' ] == " AMB " ) {
updateMiscellaneousStock ( $ { " item_id $i " }, $batchs [ $b ], $issue , $ohc_loc , $amb_num_box_code );
}
}
$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 . " ', issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ' " ;
$issued_qty = $remain ;
error_log ( '$issued_qty ' . $issued_qty );
// echo $query1;
error_log ( $query1 );
if ( ! $result1 = @ mysqli_query ( $conn , $query1 )) {
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
error_log ( " passed query: " . $query1 );
}
}
} else {
if ( $rolecode == 'DOC' || $_POST [ 'approval_status' ] == 'Y' ) {
updateStockAtStoreLevel ( $ { " item_id $i " }, $ { " item_batch_no $i " }, $ { " issue_qty $i " });
if ( $_POST [ 'ohc_loc' ] == " AMB " ) {
updateMiscellaneousStock ( $ { " item_id $i " }, $ { " item_batch_no $i " }, $ { " issue_qty $i " }, $ohc_loc , $amb_num_box_code );
}
}
$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 " } . " ', issue_ohc_type_id=' " . $_SESSION [ 'current_ohcttype' ] . " ' " ;
// echo $query1;
error_log ( $query1 );
if ( ! $result1 = @ mysqli_query ( $conn , $query1 )) {
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
error_log ( " passed query: " . $query1 );
}
}
}
}
if ( $data == null ) {
$data [ 'status' ] = 200 ;
$data [ 'message' ] = " Data not found! " ;
}
}
echo json_encode ( $data );