2024-10-16 19:18:52 +05:30
< ? php
include ( 'includes/config/config.php' );
include ( 'includes/functions.php' );
$indent_id = $_POST [ 'indent_id' ];
$status = $_POST [ 'status' ];
$remarks = $_POST [ 'app_remarks' ];
$vendor_id = $_POST [ 'vendor' ];
$app_date = $_POST [ 'approval_date' ];
$rolecode = $_SESSION [ 'RoleCode' ];
$grn = $_POST [ 'grn' ];
$item_list = $_POST [ 'item_list' ];
$items = explode ( " , " , $item_list );
error_log ( 'itemlist' . count ( $items ));
begin ();
$sql_grn = " insert into grn_master set item_grn_no=' " . $grn . " ', indent_id=' " . $indent_id . " ' ,approver = ' " . $_SESSION [ 'user_id' ] . " ',modified_by = ' " . $_SESSION [ 'user_id' ] . " ' " ;
error_log ( $sql_grn );
if ( ! $result_grn = @ mysqli_query ( $conn , $sql_grn )) {
error_log ( " Exception save_app_indent_grn saving insert into sql_grn: " . mysqli_error ( $conn ) . " failing query: " . $sql_grn );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
for ( $i = 0 ; $i < count ( $items ); $i ++ ) {
$indent_item_id = $items [ $i ];
$sql_item = " update indent_items set status=' " . $status . " ',item_grn_no=' " . $grn . " ' where item_id=' " . $indent_item_id . " ' and indent_id=' " . $indent_id . " ' " ;
error_log ( $sql_item );
if ( ! $result_item = @ mysqli_query ( $conn , $sql_item )) {
error_log ( " Exception save_app_indent_grn saving update indent_item sql_item: " . mysqli_error ( $conn ) . " failing query: " . $sql_item );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
$grn_data = " select vendor_rate,new_dispatch_qty,expiry,batch,gst,discount,unit,mrp from indent_items where item_id=' " . $indent_item_id . " ' and indent_id=' " . $indent_id . " ' " ;
error_log ( 'grn_data' . $grn_data );
if ( ! $result_grn_data = @ mysqli_query ( $conn , $grn_data )) {
error_log ( " Exception save_app_indent_grn saving grn_data: " . mysqli_error ( $conn ) . " failing query: " . $grn_data );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
$row_grn_data = mysqli_fetch_array ( $result_grn_data );
$initialq = '' ;
$endquery = '' ;
$old_item_rate_data = " select * from item_rate where item_id=' " . $indent_item_id . " ' " ;
error_log ( 'old_item_rate_data' . $old_item_rate_data );
if ( ! $result_old_item_rate_data = @ mysqli_query ( $conn , $old_item_rate_data )) {
error_log ( " Exception save_app_indent_grn saving old_item_rate_data: " . mysqli_error ( $conn ) . " failing query: " . $old_item_rate_data );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
$row_old_item_rate_data = mysqli_fetch_array ( $result_old_item_rate_data );
if ( $row_old_item_rate_data == '' || $row_old_item_rate_data == null ){
$initialq = 'insert into' ;
$endquery = '' ;
}
else {
$update_rate_change_history = " insert into item_rate_change_history set item_id=' " . $row_old_item_rate_data [ 'item_id' ] . " ', procurement_item_id=' " . $row_old_item_rate_data [ 'item_rate_id' ] . " ', old_rate=' " . $row_old_item_rate_data [ 'item_rate' ] . " ' , new_rate=' " . $row_grn_data [ 'vendor_rate' ] . " ' ,vendor_id=' " . $row_old_item_rate_data [ 'vendor_id' ] . " ' ,modified_by = ' " . $_SESSION [ 'user_id' ] . " ' " ;
error_log ( '$update_rate_change_history' . $update_rate_change_history );
if ( ! $result_update_rate_change_history = @ mysqli_query ( $conn , $update_rate_change_history )) {
error_log ( " Exception save_app_indent_grn saving update_rate_change_history: " . mysqli_error ( $conn ) . " failing query: " . $update_rate_change_history );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
$initialq = 'update' ;
$endquery = " where item_id=' " . $indent_item_id . " ' " ;
}
$update_item_rate = $initialq . " item_rate set modified_by = ' " . $_SESSION [ 'user_id' ] . " ',unit=' " . $row_grn_data [ 'unit' ] . " ' ,mrp=' " . $row_grn_data [ 'mrp' ] . " ' ,gst=' " . $row_grn_data [ 'gst' ] . " ',discount=' " . $row_grn_data [ 'discount' ] . " ' ,item_rate=' " . $row_grn_data [ 'vendor_rate' ] . " ' ,item_id=' " . $indent_item_id . " ' " . $endquery ;
error_log ( 'update_item_rate' . $update_item_rate );
if ( ! $result_update_item_rate = @ mysqli_query ( $conn , $update_item_rate )) {
error_log ( " Exception save_app_indent_grn saving update_item_rate: " . mysqli_error ( $conn ) . " failing query: " . $update_item_rate );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
$sql_grn_item = " insert into grn_items set status=' " . $status . " ', item_grn_no=' " . $grn . " ', item_id=' " . $indent_item_id . " ' ,indent_id=' " . $indent_id . " ' ,modified_by = ' " . $_SESSION [ 'user_id' ] . " ',grn_qty=' " . $row_grn_data [ 'new_dispatch_qty' ] . " ' ,batch=' " . $row_grn_data [ 'batch' ] . " ' ,expiry=' " . $row_grn_data [ 'expiry' ] . " ',vendor_rate=' " . $row_grn_data [ 'vendor_rate' ] . " ' " ;
error_log ( $sql_grn_item );
if ( ! $result_grn_item = @ mysqli_query ( $conn , $sql_grn_item )) {
error_log ( " Exception save_app_indent_grn saving approved grn no.: " . mysqli_error ( $conn ) . " failing query: " . $sql_grn_item );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
}
$approved = 0 ;
$sql_approved = " select count(*) as items from indent_items where indent_id=' " . $indent_id . " ' and status in ('DISPATCHE','PENDING_DISPATCH','REJECTED') " ;
error_log ( 'approved' . $sql_approved );
if ( ! $result_approved = @ mysqli_query ( $conn , $sql_approved )) {
error_log ( " Exception save_app_indent_grn saving approved grn item check: " . mysqli_error ( $conn ) . " failing query: " . $sql_approved );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
}
$row_approved = mysqli_fetch_array ( $result_approved );
$unapproved = $row_approved [ 'items' ];
if ( $unapproved == 0 ) {
$sql = " update indent_master set status=' " . $status . " ',approval_date=STR_TO_DATE(' " . $app_date . " ','%d/%m/%Y'),approving_doc_id=' " . $_SESSION [ 'user_id' ] . " ',approval_remarks=' " . $remarks . " ' where indent_id=' " . $indent_id . " ' " ;
error_log ( " sql " . $sql );
if ( ! $result = @ mysqli_query ( $conn , $sql )) {
error_log ( " Exception save_app_indent_grn saving approveiing grn: " . mysqli_error ( $conn ) . " failing query: " . $sql );
rollback ();
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $conn ));
2024-10-16 19:18:52 +05:30
} else {
$data = " success " ;
}
}
commit ();
echo json_encode ( $data );