2024-10-16 19:18:52 +05:30
< ? php
include ( 'includes/config/config.php' );
include ( 'includes/auth/auth.php' );
include ( 'includes/functions.php' );
include 'log_entry.php' ;
include ( 'access.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 , 'Requisition' , 'SAVE' , 'save_requisition.php' );
$noOfRows = $_POST [ 'count_items' ];
// echo $noOfRows;
$req_id = $_POST [ 'req_id' ];
// $procurement_refno = $_POST['ref_no'];
// echo $procurement_refno;
$remarks = $_POST [ 'remarks' ];
$req_date = $_POST [ 'req_date' ];
// echo $procurement_id;
$initquery = " " ;
$data = array ();
begin ();
$data [ 'approval_status' ] = $_POST [ 'approval_status' ];
$endquery = " " ;
$user_id = $_SESSION [ 'user_id' ];
$ohc_type = $_SESSION [ 'current_ohcttype' ];
if ( $ohc_type == null or $ohc_type == " " )
$ohc_type = $_POST [ 'ohc_location_id' ];
// $ohc_type=getTableFieldValue('tbl_users','ohc_type','user_id',$user_id);
if ( ! empty ( $req_id )) {
// update case
$initquery = " update requisition set " ;
$endquery = " where req_id = ' " . $req_id . " ' " ;
$data [ 'req_id' ] = $req_id ;
} else {
// insert case
// $month=4;
$month = date ( 'n' );
$year = date ( 'Y' );
if ( $month > 3 ) {
$year = $year ;
} else {
$year = $year - 1 ;
}
$ohc_type_code = getTableFieldValue ( 'ohc_type' , 'ohc_code' , 'ohc_type_id' , $ohc_type );
$query_req_ref_no = " select max( CAST(substring(req_ref_no,locate('-',req_ref_no)+1,length(req_ref_no)-(5+locate('-',req_ref_no))) as UNSIGNED) ) from requisition where req_ref_no like '% $year %' and ohc_location_id= $ohc_type " ;
// echo $query_req_ref_no;
$result_req_ref_no = @ mysqli_query ( $conn , $query_req_ref_no );
$row_req_ref_no = mysqli_fetch_row ( $result_req_ref_no );
$req_ref_no = 'REQ' . $ohc_type_code . '-' . ( $row_req_ref_no [ 0 ] + 1 ) . '-' . $year ;
$initquery = " INSERT INTO requisition set req_ref_no=' $req_ref_no ' , " ;
}
$approval_status_qry = " " ;
$isAutoApprove = getConfigKey ( " AUTO_APPROVE_REQUISITIONS " );
if ( $isAutoApprove == " TRUE " ) {
$approval_status_qry = " , status= 'Y' " ;
} if ( isset ( $_POST [ 'approval_status' ] ) && $_POST [ 'approval_status' ] != '' ){
$approval_status_qry = " , status=' " . $_POST [ 'approval_status' ] . " ' " ;
}
error_log ( " Auto Approval Config key:AUTO_APPROVE_REQUISITIONS, value: " . $isAutoApprove );
$query = $initquery . " remarks=' " . $remarks . " ', ohc_location_id=' " . $ohc_type . " ', req_date = STR_TO_DATE(' " . $req_date . " ','%d/%m/%Y') , modified_by = ' " . $_SESSION [ 'user_id' ] . " ' $approval_status_qry $endquery " ;
error_log ( $query );
if ( ! $result = @ mysqli_query ( $conn , $query )) {
error_log ( " Error Executing Query: " . mysqli_error ( $conn ) . " Query: " . $query );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ) );
2024-10-16 19:18:52 +05:30
} else {
if ( empty ( $_POST [ 'req_id' ] )) {
// insert case when procuremnt id is not available but needed for saving into the child table
$query = " select req_id from requisition where req_ref_no= ' " . $req_ref_no . " ' " ;
// echo $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 ;
$req_id = $row [ 'req_id' ];
}
}
}
}
// clear existing entries into the child table
$query_del = " delete from requisition_items where req_id = ' " . $req_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 ) {
$query1 = " insert into requisition_items set req_id=' $req_id ', item_id =' " . $ { " item_id $i " } . " ',qty = ' " . $ { " item_qty $i " } . " ',modified_by = ' " . $_SESSION [ 'user_id' ] . " ' " ;
// echo $query1;
if ( ! $result1 = @ mysqli_query ( $conn , $query1 )) {
error_log ( " Error Executing requisition_items Query: " . mysqli_error ( $conn ) . " Query: " . $query1 );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ) );
2024-10-16 19:18:52 +05:30
}
}
}
}
commit ();
if ( $data == null ) {
$data [ 'status' ] = 200 ;
$data [ 'message' ] = " Data not found! " ;
}
echo json_encode ( $data );
?>