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,6 +1182,9 @@ class StockController extends BaseController
return; return;
} }
$this->db->transBegin();
try {
foreach ($drierData as $row) { foreach ($drierData as $row) {
$data = [ $data = [
'date' => DateTime::createFromFormat('d-m-Y', $row['Date'])->format('Y-m-d'), 'date' => DateTime::createFromFormat('d-m-Y', $row['Date'])->format('Y-m-d'),
@ -1195,6 +1206,9 @@ class StockController extends BaseController
// Check if a row with the same date already exists // Check if a row with the same date already exists
$existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first(); $existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first();
$inserts = [];
$updates = [];
if ($existingRow) { if ($existingRow) {
// If exists, update the row // If exists, update the row
@ -1204,16 +1218,13 @@ class StockController extends BaseController
if ($existingDrierGasConsumption != $updatedDrierGasConsumption) { if ($existingDrierGasConsumption != $updatedDrierGasConsumption) {
$changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption ; $changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption;
$dateToBeUpdated = $existingRow['date'] ; $dateToBeUpdated = $existingRow['date'];
$result =$this->gasStockDetails_model $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
->where('date',$dateToBeUpdated)
->find();
$consumption = $result[0]['consumption'] + $changeInConsumption ;
$result = $this->updateGasStockConsumption($dateToBeUpdated, $consumption);
} }
$data['id'] = $existingRow['id'];
$updates[] = $data;
$this->drierMachineDetails_model->update($existingRow['id'], $data);
} else { } else {
// If not, insert a new row // If not, insert a new row
//before inserting the row update gas stock //before inserting the row update gas stock
@ -1222,12 +1233,33 @@ class StockController extends BaseController
$this->updateGasStockConsumption($dateToBeInserted, $consumption); $this->updateGasStockConsumption($dateToBeInserted, $consumption);
$this->drierMachineDetails_model->insert($data); $inserts[] = $data;
}
} }
if (!empty($inserts)) {
$this->drierMachineDetails_model->insertBatch($inserts);
}
if (!empty($updates)) {
$this->drierMachineDetails_model->updateBatch($updates, 'id');
}
if ($this->db->transStatus() === false) {
throw new \Exception('Transaction failed due to database error.');
}
$this->db->transCommit();
echo "Data successfully saved."; echo "Data successfully saved.";
} }
} catch (\Exception $e) {
// Rollback the transaction on any failure
$this->db->transRollback();
echo "Failed to save/Update data: " . $e->getMessage();
}
}
public function factoryVehicleDieselDetails() public function factoryVehicleDieselDetails()
{ {