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
$this->db->transStart();
$this->db->transBegin();
try {
// Insert into coating machine details table
@ -171,24 +171,28 @@ class StockController extends BaseController
$consumption = $postData['totalGasConsumption'];
$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) {
throw new \Exception('Transaction failed');
throw new \Exception('Transaction failed due to database errors.');
}
$this->db->transCommit();
// Set flashdata for success message
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2);
} catch (\Exception $error) {
// Rollback on any exception
$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'];
// Start transaction
$this->db->transStart();
$this->db->transBegin();
try {
$updated = $this->coatingMachineDetails_model->update($id, $putData);
if ($updated !== false && $updated > 0) {
@ -220,27 +224,35 @@ class StockController extends BaseController
if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) {
$changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption;
$updateResult = $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
$message2 = ($updateResult !== false && $updateResult > 0)
? 'And Updated Gas Stock Consumption '
: "But Failed to Update Gas Stock Consumption ";
$updatedResult = $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
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.';
}
// Commit the transaction if all queries succeed
$this->db->transComplete();
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
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2);
} else {
throw new \Exception('Failed to update Coating machine details.');
}
} catch (\Exception $error) {
// Rollback on any exception
$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)
{
$count = 0;
$resultExists = $this->gasStockDetails_model
->where('date', $date)
@ -991,9 +1002,6 @@ class StockController extends BaseController
$updatedOpeningStock = $result['balanceStock'];
}
log_message('info',$count);
log_message('info',$date);
log_message('info', $result['opening']);
$updates[] = $result;
@ -1174,6 +1182,9 @@ class StockController extends BaseController
return;
}
$this->db->transBegin();
try {
foreach ($drierData as $row) {
$data = [
'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
$existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first();
$inserts = [];
$updates = [];
if ($existingRow) {
// If exists, update the row
@ -1206,14 +1220,11 @@ class StockController extends BaseController
$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->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
}
$data['id'] = $existingRow['id'];
$updates[] = $data;
$this->drierMachineDetails_model->update($existingRow['id'], $data);
} else {
// If not, insert a new row
//before inserting the row update gas stock
@ -1222,12 +1233,33 @@ class StockController extends BaseController
$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.";
}
} catch (\Exception $e) {
// Rollback the transaction on any failure
$this->db->transRollback();
echo "Failed to save/Update data: " . $e->getMessage();
}
}
public function factoryVehicleDieselDetails()
{