From 1fb9adb241e18bb4836b0ab6a983b16146b6f478 Mon Sep 17 00:00:00 2001 From: vadivelJ96 Date: Thu, 28 Nov 2024 10:59:09 +0530 Subject: [PATCH] Gas stock details changes-1 vadivel J 28-11-2024 --- app/Controllers/StockController.php | 164 +++++++++++++++++----------- 1 file changed, 98 insertions(+), 66 deletions(-) diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 428be6a0..37fc3f82 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -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) @@ -969,10 +980,10 @@ class StockController extends BaseController ->where('date >=', $date) ->where('date <=', $endDate) ->orderBy('date', 'asc') - ->findAll(); + ->findAll(); - $updatedOpeningStock = 0; - $updates = []; + $updatedOpeningStock = 0; + $updates = []; foreach ($resultExists as $index => $result) { @@ -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,59 +1182,83 @@ class StockController extends BaseController return; } - 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'] - ]; + $this->db->transBegin(); - // Check if a row with the same date already exists - $existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first(); + try { + 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) { - // If exists, update the row + // Check if a row with the same date already exists + $existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first(); - //before updating the row update gas stock - $existingDrierGasConsumption = $existingRow['total_gas_consumption']; - $updatedDrierGasConsumption = $row['Tot Gas Consumption']; + $inserts = []; + $updates = []; + + 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; - if ($existingDrierGasConsumption != $updatedDrierGasConsumption) { - - $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); - } 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); + if (!empty($inserts)) { + $this->drierMachineDetails_model->insertBatch($inserts); + } + 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()