csrtechnew.ohctech.in/upload_excel_procurement_script.php
2025-04-14 13:28:09 +05:30

148 lines
6.7 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/functions.php');
$response_array = array();
$response_mssages = "";
// echo "shu".$Row[0];
$interm_proc_id=$_REQUEST['proc_id'];
error_log("raju".$interm_proc_id);
for($i=0;$i<count($interm_proc_id);$i++){
if($interm_proc_id[$i]!=null || $interm_proc_id[$i]!=''){
$data_q="select a.procurement_refno,a.procurement_date,b.* from interm_procurement a left join interm_procurement_items b on a. interm_procurement_id=b.interm_procurement_id where a.interm_procurement_id='$interm_proc_id[$i]'";
error_log('$data_q'.$data_q);
$result_data_q = mysqli_query($conn,$data_q);
while($row_data_q=mysqli_fetch_array($result_data_q)){
$procurement_reference_no = $row_data_q['procurement_refno'];
$procurement_date =$row_data_q['procurement_date'];
$item_code =getTableFieldValue('tbl_items','item_code','item_id', $row_data_q['item_id']) ;
$quantity = $row_data_q['qty'];
$net_value = $row_data_q['net_value'];
$batch = $row_data_q['batch'];
$expiry = $row_data_q['expiry'];
$item_remarks = $row_data_q['remarks'];
$ohc_id = $_SESSION['current_ohcttype'];
if (!empty($procurement_reference_no) && !empty($procurement_date) && !empty($item_code) && !empty($quantity) && !empty($batch) && !empty($expiry)) {
$time1 = strtotime($procurement_date);
$procurement_date = date('d/m/Y', $time1);
$time2 = strtotime($expiry);
$expiry = date('d/m/Y', $time2);
$procurement_id_sql = "select procurement_id from procurement where procurement_refno='" . $procurement_reference_no . "' and ohc_type_id='" . $ohc_id . "'";
$result_id_sql = mysqli_query($conn,$procurement_id_sql);
$row_id = mysqli_fetch_array($result_id_sql);
$procurement_id = $row_id['procurement_id'];
error_log("PROCUREMENT ID::" . $procurement_id);
$initquery = "";
$endquery = "";
if ($procurement_id != "" && $procurement_id != null) {
$initquery = "update procurement set ";
$endquery = " where procurement_id = '" . $procurement_id . "' and ohc_type_id='" . $ohc_id . "'";
$update_counter++;
} else {
$initquery = "INSERT INTO procurement set procurement_refno = '" . $procurement_reference_no . "', RoleCode='".$_SESSION['RoleCode']."', ohc_type_id='" . $ohc_id . "',";
$endquery = "";
$insert_counter++;
}
$query_procurement = $initquery . " procurement_date = STR_TO_DATE('" . $procurement_date . "','%d/%m/%Y') $endquery ";
error_log("PROCUREMENT QUERY::" . $query_procurement);
$result1 = mysqli_query($conn, $query_procurement);
if ($procurement_id == null or $procurement_id == "")
$procurement_id = mysqli_insert_id($conn);
$item_id = getTableFieldValue('tbl_items', 'item_id', 'item_code', "'" . $item_code . "'");
// to reset stock in previous qty before procurement
if (!empty($procurement_id)) {
$stock_qty = getTableFieldValue('item_stock', 'stock_qty', 'item_id', $item_id, 'item_batch_no', '"' . $batch . '"','ohc_type_id',$ohc_id);
error_log("pre existig " . $stock_qty);
$prev_qty = getTableFieldValue('procurement_items', 'qty', 'item_id', $item_id, 'procurement_id', $procurement_id, 'batch', '"' . $batch . '"','ohc_type_id',$ohc_id);
if ($stock_qty != null && $stock_qty != '' && $stock_qty != 0.00) {
$current_stock_level_items_qty = $stock_qty - $prev_qty;
$current_stock_query = "update item_stock set stock_qty='" . $current_stock_level_items_qty . "' where item_id='" . $item_id . "' and item_batch_no='" . $batch . "' and ohc_type_id='".$ohc_id."' ";
error_log("current_stock_query " . $current_stock_query);
if (!@mysqli_query($conn, $current_stock_query)) {
rollback();
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(die('failed!' . mysqli_error($conn)));
}
}
}
$procurement_item_id = getTableFieldValue('procurement_items', 'procurement_item_id', 'item_id', $item_id, 'procurement_id', $procurement_id, 'batch', "'" . $batch . "'",'ohc_type_id',$ohc_id);
error_log("procurement item id " . $procurement_item_id);
$initquery_item = "";
$final_item = "";
$qty = 0.0;
if ($procurement_item_id != null && $procurement_item_id = !"") {
$query2 = " update procurement_items set qty='" . $quantity . "',net_value='" . $net_value . "' where procurement_id=$procurement_id and item_id=$item_id and batch='" . $batch . "' and RoleCode='".$_SESSION['RoleCode']."' and ohc_type_id='".$ohc_id."' ";
} else {
$query2 = " insert into procurement_items set procurement_id=$procurement_id,item_id=$item_id,qty=$quantity,net_value='" . $net_value . "',batch='" . $batch . "', RoleCode='".$_SESSION['RoleCode']."',ohc_type_id='".$ohc_id."',expiry=STR_TO_DATE('" . $expiry . "','%d/%m/%Y')";
}
error_log("UPLOAD PROCUREMENT::" . $query2);
$result2 = mysqli_query($conn, $query2);
$stock_qty = getTableFieldValue('item_stock', 'stock_qty', 'item_id', $item_id, 'item_batch_no', "'" . $batch . "'",'ohc_type_id',$ohc_id);
error_log("stock before inserting/updating " . $stock_qty);
$current_stock_query = "";
if ($stock_qty != null && $stock_qty != "") {
$stock_qty = $stock_qty + $quantity;
error_log("stock while inserting/updating in if " . $stock_qty);
$current_stock_query = "update item_stock set stock_qty='" . $stock_qty . "' where item_id='" . $item_id . "' and item_batch_no='" . $batch . "' and ohc_type_id='".$ohc_id."'";
} else {
$stock_qty = $stock_qty + $quantity;
error_log("stock while inserting/updating in else " . $stock_qty);
$current_stock_query = "insert into item_stock set stock_qty='" . $stock_qty . "',item_id='" . $item_id . "',item_batch_no='" . $batch . "', RoleCode='".$_SESSION['RoleCode']."',ohc_type_id='".$ohc_id."',expiry_date=STR_TO_DATE('" . $expiry . "','%d/%m/%Y') ";
}
error_log("final query update for item quantity " . $current_stock_query);
$result3 = mysqli_query($conn, $current_stock_query);
if (!empty($result1) && !empty($result2) && !empty($result3)) {
$type = "success";
//$message = "Excel Data Imported Into the Database";
} else {
$type = "error";
//$message = "Problem In Importing Excel Data";
}
}
}}
}
commit();
echo json_encode($type);