Gas stock details changes-1 vadivel J 28-11-2024

This commit is contained in:
vadivelJ96 2024-11-28 10:59:09 +05:30
parent aa67d69b8d
commit 1fb9adb241

View File

@ -156,7 +156,7 @@ class StockController extends BaseController
} }
// Start transaction // Start transaction
$this->db->transStart(); $this->db->transBegin();
try { try {
// Insert into coating machine details table // Insert into coating machine details table
@ -171,24 +171,28 @@ class StockController extends BaseController
$consumption = $postData['totalGasConsumption']; $consumption = $postData['totalGasConsumption'];
$updatedResult = $this->updateGasStockConsumption($dateToBeInserted, $consumption); $updatedResult = $this->updateGasStockConsumption($dateToBeInserted, $consumption);
$message2 = ($updatedResult !== false && $updatedResult > 0) ? 'And Updated Gas Consumption in Gas Stock' : 'And failed to Updated Gas Consumption in Gas Stock'; if ($updatedResult === false || $updatedResult <= 0) {
// Explicit failure handling for gas stock update
throw new \Exception('Failed to update gas stock consumption.');
}
$message2 = 'And updated gas stock consumption.';
} else {
throw new \Exception('Failed to insert Coating machine details.');
} }
// Commit the transaction if all queries succeed
$this->db->transComplete();
if ($this->db->transStatus() === false) { if ($this->db->transStatus() === false) {
throw new \Exception('Transaction failed'); throw new \Exception('Transaction failed due to database errors.');
} }
$this->db->transCommit();
// Set flashdata for success message // Set flashdata for success message
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2); return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2);
} catch (\Exception $error) { } catch (\Exception $error) {
// Rollback on any exception
$this->db->transRollback(); $this->db->transRollback();
return redirect()->back()->with('error', 'Failed to update Coating machine details: ' . $error->getMessage()); return redirect()->back()->with('error', ' ' . $error->getMessage());
} }
} }
@ -206,7 +210,7 @@ class StockController extends BaseController
$id = $putData['editCoatingMachineDetailId']; $id = $putData['editCoatingMachineDetailId'];
// Start transaction // Start transaction
$this->db->transStart(); $this->db->transBegin();
try { try {
$updated = $this->coatingMachineDetails_model->update($id, $putData); $updated = $this->coatingMachineDetails_model->update($id, $putData);
if ($updated !== false && $updated > 0) { if ($updated !== false && $updated > 0) {
@ -220,27 +224,35 @@ class StockController extends BaseController
if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) { if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) {
$changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption; $changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption;
$updateResult = $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption); $updatedResult = $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
$message2 = ($updateResult !== false && $updateResult > 0)
? 'And Updated Gas Stock Consumption ' if ($updatedResult === false || $updatedResult <= 0) {
: "But Failed to Update Gas Stock Consumption "; // Explicit failure handling for gas stock update
throw new \Exception('Failed to update gas stock consumption.');
}
$message2 = 'And updated gas stock consumption.';
} }
// Commit the transaction if all queries succeed // Commit the transaction if all queries succeed
$this->db->transComplete();
if ($this->db->transStatus() === false) { if ($this->db->transStatus() === false) {
throw new \Exception('Transaction failed'); throw new \Exception('Transaction failed due to database errors.');
} }
$this->db->transCommit();
// Set flashdata for success message // Set flashdata for success message
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2); return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2);
} else {
throw new \Exception('Failed to update Coating machine details.');
} }
} catch (\Exception $error) { } catch (\Exception $error) {
// Rollback on any exception
$this->db->transRollback(); $this->db->transRollback();
return redirect()->back()->with('error', 'Failed to update Coating machine details' . $error->getMessage()); return redirect()->back()->with('error', ' ' . $error->getMessage());
} }
@ -944,7 +956,6 @@ class StockController extends BaseController
private function updateGasStockConsumption($date, $consumption) private function updateGasStockConsumption($date, $consumption)
{ {
$count = 0;
$resultExists = $this->gasStockDetails_model $resultExists = $this->gasStockDetails_model
->where('date', $date) ->where('date', $date)
@ -991,9 +1002,6 @@ class StockController extends BaseController
$updatedOpeningStock = $result['balanceStock']; $updatedOpeningStock = $result['balanceStock'];
} }
log_message('info',$count);
log_message('info',$date);
log_message('info', $result['opening']);
$updates[] = $result; $updates[] = $result;
@ -1174,59 +1182,83 @@ class StockController extends BaseController
return; return;
} }
foreach ($drierData as $row) { $this->db->transBegin();
$data = [
'date' => DateTime::createFromFormat('d-m-Y', $row['Date'])->format('Y-m-d'),
'drier_on_time' => $row['Drier On Time'],
'drier_off_time' => $row['Drier Off Time'],
'drier_running_hours' => $row['Running Hrs'],
'supplier_name' => $row['Supplier Name'],
'input_sand_moisture' => $row['i/p Sand Moisture(%)'],
'total_gas_consumption' => $row['Tot Gas Consumption'],
'gas_per_ton' => $row['Gas Pre Ton'],
'input_sand_qty' => $row['i/p Sand Qty'],
'moisture_loss_qty' => $row['Moisture Loss Qty'],
'sand_dried_qty' => $row['Sand Dried Qty'],
'qty_per_hours' => $row['Qty Per Hrs'],
'dust_qty' => $row['Dust Qty'],
'customer_name' => $row['Customer Name']
];
// Check if a row with the same date already exists try {
$existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first(); foreach ($drierData as $row) {
$data = [
'date' => DateTime::createFromFormat('d-m-Y', $row['Date'])->format('Y-m-d'),
'drier_on_time' => $row['Drier On Time'],
'drier_off_time' => $row['Drier Off Time'],
'drier_running_hours' => $row['Running Hrs'],
'supplier_name' => $row['Supplier Name'],
'input_sand_moisture' => $row['i/p Sand Moisture(%)'],
'total_gas_consumption' => $row['Tot Gas Consumption'],
'gas_per_ton' => $row['Gas Pre Ton'],
'input_sand_qty' => $row['i/p Sand Qty'],
'moisture_loss_qty' => $row['Moisture Loss Qty'],
'sand_dried_qty' => $row['Sand Dried Qty'],
'qty_per_hours' => $row['Qty Per Hrs'],
'dust_qty' => $row['Dust Qty'],
'customer_name' => $row['Customer Name']
];
if ($existingRow) { // Check if a row with the same date already exists
// If exists, update the row $existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first();
//before updating the row update gas stock $inserts = [];
$existingDrierGasConsumption = $existingRow['total_gas_consumption']; $updates = [];
$updatedDrierGasConsumption = $row['Tot Gas Consumption'];
if ($existingDrierGasConsumption != $updatedDrierGasConsumption) { if ($existingRow) {
// If exists, update the row
//before updating the row update gas stock
$existingDrierGasConsumption = $existingRow['total_gas_consumption'];
$updatedDrierGasConsumption = $row['Tot Gas Consumption'];
if ($existingDrierGasConsumption != $updatedDrierGasConsumption) {
$changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption;
$dateToBeUpdated = $existingRow['date'];
$this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
}
$data['id'] = $existingRow['id'];
$updates[] = $data;
} else {
// If not, insert a new row
//before inserting the row update gas stock
$consumption = $row['Tot Gas Consumption'];
$dateToBeInserted = $data['date'];
$this->updateGasStockConsumption($dateToBeInserted, $consumption);
$inserts[] = $data;
$changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption ;
$dateToBeUpdated = $existingRow['date'] ;
$result =$this->gasStockDetails_model
->where('date',$dateToBeUpdated)
->find();
$consumption = $result[0]['consumption'] + $changeInConsumption ;
$result = $this->updateGasStockConsumption($dateToBeUpdated, $consumption);
} }
$this->drierMachineDetails_model->update($existingRow['id'], $data); if (!empty($inserts)) {
} else { $this->drierMachineDetails_model->insertBatch($inserts);
// If not, insert a new row }
//before inserting the row update gas stock
$consumption = $row['Tot Gas Consumption'];
$dateToBeInserted = $data['date'];
$this->updateGasStockConsumption($dateToBeInserted, $consumption);
if (!empty($updates)) {
$this->drierMachineDetails_model->updateBatch($updates, 'id');
}
$this->drierMachineDetails_model->insert($data); if ($this->db->transStatus() === false) {
throw new \Exception('Transaction failed due to database error.');
}
$this->db->transCommit();
echo "Data successfully saved.";
} }
} catch (\Exception $e) {
// Rollback the transaction on any failure
$this->db->transRollback();
echo "Failed to save/Update data: " . $e->getMessage();
} }
echo "Data successfully saved.";
} }
public function factoryVehicleDieselDetails() public function factoryVehicleDieselDetails()