2024-10-16 19:18:52 +05:30
< ? php
include ( 'includes/config/config.php' );
include ( 'log_entry.php' );
$sql_menu = " select ohc_type_id from ohc_type " ;
if ( $result_menu = mysqli_query ( $GLOBALS [ 'conn' ], $sql_menu )){
while ( $row_menu = mysqli_fetch_array ( $result_menu )){
error_log ( " Batch Run### **Start******* Running Daily Stock Update Dispensary for OHC: " . $row_menu [ 'ohc_type_id' ]);
updateItemStockDispensaryDailyBalance ( $row_menu [ 'ohc_type_id' ]);
error_log ( " Batch Run### **End******* Running Daily Stock Update Dispensary for OHC: " . $row_menu [ 'ohc_type_id' ]);
}
} else {
error_log ( " Failed to get OHCs for Batch Run Running Daily Stock Update Dispensary for OHC " );
}
error_log ( " Batch Run### **Start******* Running Daily Stock Update Store " );
updateItemStockStoreDailyBalance ();
error_log ( " Batch Run### **End******* Running Daily Stock Update Store " );
//take_daily_db_backup();
function updateItemStockDispensaryDailyBalance ( $ohc_id ){
mysqli_query ( $GLOBALS [ 'conn' ], " BEGIN " );
error_log ( " param ohc_location: " . $ohc_id );
$sql_existing_today_balance = " select * from item_stock_dispensary_daily_balance where record_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY) and ohc_location_id = ' $ohc_id ' " ;
$num_rows_existing = mysqli_num_rows ( mysqli_query ( $GLOBALS [ 'conn' ], $sql_existing_today_balance ) );
if ( $num_rows_existing != 0 ) {
$delete_existing_today_balance = " delete from item_stock_dispensary_daily_balance where record_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY) and ohc_location_id = ' $ohc_id ' " ;
if ( ! mysqli_query ( $GLOBALS [ 'conn' ], $delete_existing_today_balance )) {
error_log ( " Failed to clear existing: " . mysqli_error ( $GLOBALS [ 'conn' ]) . " failing query: " . $delete_existing_today_balance );
mysqli_query ( $GLOBALS [ 'conn' ], " ROLLBACK " );
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $GLOBALS [ 'conn' ]) );
2024-10-16 19:18:52 +05:30
}
}
$sql_bulk_insert = " " ;
$sql_existing_today_balance = " select * from item_stock_dispensary where ohc_location_id = ' $ohc_id ' " ;
error_log ( " $sql_existing_today_balance " );
if ( ! $result_today_bal = mysqli_query ( $GLOBALS [ 'conn' ], $sql_existing_today_balance )){
error_log ( " sql_existing_today_balance: " . $sql_existing_today_balance );
}
while ( $row_bal = @ mysqli_fetch_array ( $result_today_bal )) {
@ extract ( $row_bal );
$sql_bulk_insert = " insert into item_stock_dispensary_daily_balance(item_id,item_batch_no,ohc_location_id,expiry_date,stock_qty,record_date) values( " ;
$sql_bulk_insert .= " ' " . $row_bal [ 'item_id' ] . " ',' " . $row_bal [ 'item_batch_no' ] . " ',' " . $row_bal [ 'ohc_location_id' ] . " ',' " . $row_bal [ 'expiry_date' ] . " ',' " . $row_bal [ 'stock_qty' ] . " ',DATE_SUB(CURDATE(), INTERVAL 1 DAY)); " ;
mysqli_query ( $GLOBALS [ 'conn' ], $sql_bulk_insert );
}
error_log ( " bulk Insert Query: " . $sql_bulk_insert );
// mysqli_free_result( $result_today_bal);
/* if ( ! $GLOBALS [ 'conn' ] -> multi_query ( $sql_bulk_insert )) {
echo " Multi query failed: ( " . $GLOBALS [ 'conn' ] -> errno . " ) " . $GLOBALS [ 'conn' ] -> error ;
} */
$GLOBALS [ 'conn' ] -> commit ();
mysqli_query ( $GLOBALS [ 'conn' ], " COMMIT " );
}
function updateItemStockStoreDailyBalance (){
mysqli_query ( $GLOBALS [ 'conn' ], " BEGIN " );
error_log ( " Store Stock Daily Balance Record " );
$sql_existing_today_balance = " select * from item_stock_daily_balance where record_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY) " ;
$num_rows_existing = mysqli_num_rows ( mysqli_query ( $GLOBALS [ 'conn' ], $sql_existing_today_balance ) );
if ( $num_rows_existing != 0 ) {
$delete_existing_today_balance = " delete from item_stock_daily_balance where record_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY) " ;
if ( ! mysqli_query ( $GLOBALS [ 'conn' ], $delete_existing_today_balance )) {
error_log ( " Failed to clear existing: " . mysqli_error ( $GLOBALS [ 'conn' ]) . " failing query: " . $delete_existing_today_balance );
mysqli_query ( $GLOBALS [ 'conn' ], " ROLLBACK " );
2024-11-02 18:03:13 +05:30
die ( mysqli_error ( $GLOBALS [ 'conn' ]) );
2024-10-16 19:18:52 +05:30
}
}
$sql_bulk_insert = " " ;
$sql_existing_today_balance = " select * from item_stock " ;
error_log ( " $sql_existing_today_balance " );
if ( ! $result_today_bal = mysqli_query ( $GLOBALS [ 'conn' ], $sql_existing_today_balance )){
error_log ( " sql_existing_today_balance: " . $sql_existing_today_balance );
}
while ( $row_bal = @ mysqli_fetch_array ( $result_today_bal )) {
@ extract ( $row_bal );
$sql_bulk_insert = " insert into item_stock_daily_balance(item_id,item_batch_no,expiry_date,stock_qty,record_date) values( " ;
$sql_bulk_insert .= " ' " . $row_bal [ 'item_id' ] . " ',' " . $row_bal [ 'item_batch_no' ] . " ',' " . $row_bal [ 'expiry_date' ] . " ',' " . $row_bal [ 'stock_qty' ] . " ',DATE_SUB(CURDATE(), INTERVAL 1 DAY)); " ;
mysqli_query ( $GLOBALS [ 'conn' ], $sql_bulk_insert );
}
error_log ( " bulk Insert Query Store: " . $sql_bulk_insert );
/* mysqli_free_result ( $result_today_bal );
if ( ! $GLOBALS [ 'conn' ] -> multi_query ( $sql_bulk_insert )) {
echo " Multi query failed: ( " . $GLOBALS [ 'conn' ] -> errno . " ) " . $GLOBALS [ 'conn' ] -> error ;
} */
$GLOBALS [ 'conn' ] -> commit ();
mysqli_query ( $GLOBALS [ 'conn' ], " COMMIT " );
}
function take_daily_db_backup (){
}
?>