diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 12aebdd1..428be6a0 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -134,6 +134,9 @@ class StockController extends BaseController public function addNewCoatingMachineDetails() { + $message1 = ''; + $message2 = ''; + $postData = $this->request->getPost(); $month = $this->request->getGet('month'); $month = DateTime::createFromFormat('M-Y', $month)->format('Y-m'); @@ -158,14 +161,19 @@ class StockController extends BaseController try { // Insert into coating machine details table $inserted = $this->coatingMachineDetails_model->insert($postData); - if (!$inserted) { - throw new \Exception('Failed to insert coating machine details'); - } - // Update gas stock consumption - $dateToBeInserted = $postData['date']; - $consumption = $postData['totalGasConsumption']; - $this->updateGasStockConsumption($dateToBeInserted, $consumption); + if ($inserted !== false && $inserted > 0) { + + $message1 = 'Coating Machine details updated successfully'; + + // Update gas stock consumption + $dateToBeInserted = $postData['date']; + $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'; + + } // Commit the transaction if all queries succeed $this->db->transComplete(); @@ -175,7 +183,7 @@ class StockController extends BaseController } // Set flashdata for success message - return redirect()->to("/coatingMachineDetails?month=$month")->with('success', 'Coating Machine details updated successfully'); + return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2); } catch (\Exception $error) { // Rollback on any exception @@ -188,6 +196,8 @@ class StockController extends BaseController public function updateCoatingMachineDetails() { + $message1 = ''; + $message2 = ''; $putData = $this->request->getPost(); $month = $this->request->getGet('month'); @@ -195,27 +205,48 @@ class StockController extends BaseController $id = $putData['editCoatingMachineDetailId']; - $updated = $this->coatingMachineDetails_model->update($id, $putData); + // Start transaction + $this->db->transStart(); + try { + $updated = $this->coatingMachineDetails_model->update($id, $putData); + if ($updated !== false && $updated > 0) { + $message1 = "successfully updated Coating machineDetail ."; + //update Gas stock consumption if update is successfull..!! + $dateToBeUpdated = $putData['date']; - if ($updated) { - //update Gas stock consumption if update is successfull..!! - $dateToBeUpdated = $putData['date']; + $existingCoatingGasConsumption = $putData['existingTotalGasConsumption']; + $updatedCoatingGasConsumption = $putData['totalGasConsumption']; - $existingCoatingGasConsumption = $putData['existingTotalGasConsumption']; - $updatedCoatingGasConsumption = $putData['totalGasConsumption']; - if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) { - $changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption; - $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption); + 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 "; + } + // Commit the transaction if all queries succeed + $this->db->transComplete(); + + if ($this->db->transStatus() === false) { + throw new \Exception('Transaction failed'); + } + + + + // Set flashdata for success message + return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2); } - - // Set flashdata for success message - return redirect()->to("/coatingMachineDetails?month=$month")->with('success', 'Coating Machine details updated successfully'); - } else { - // Set flashdata for error message - return redirect()->back()->with('error', '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()); } + + + } public function deleteCoatingMachineDetails() @@ -913,7 +944,7 @@ class StockController extends BaseController private function updateGasStockConsumption($date, $consumption) { - + $count = 0; $resultExists = $this->gasStockDetails_model ->where('date', $date) @@ -937,36 +968,43 @@ class StockController extends BaseController $resultExists = $this->gasStockDetails_model ->where('date >=', $date) ->where('date <=', $endDate) - ->orderBy('date' , 'asc') - ->findAll(); + ->orderBy('date', 'asc') + ->findAll(); - $updatedOpeningStock=0; - $updates =[]; + $updatedOpeningStock = 0; + $updates = []; - foreach($resultExists as $index => $result){ + foreach ($resultExists as $index => $result) { - if($index == 0){ - $result['consumption'] = $result['consumption'] + ($consumption); - $result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'] ; + $count++; + + if ($index == 0) { + $result['consumption'] = $result['consumption'] + ($consumption); + $result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian']; $result['balanceStock'] = $result['total'] - $result['consumption']; - $updatedOpeningStock = $result['balanceStock']; - }else{ - - $result['opening'] = $updatedOpeningStock ; - $result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'] ; - $result['balanceStock'] = $result['total'] - $result['consumption']; - $updatedOpeningStock = $result['balanceStock']; + $updatedOpeningStock = $result['balanceStock']; + } else { + $result['opening'] = $updatedOpeningStock; + $result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian']; + $result['balanceStock'] = $result['total'] - $result['consumption']; + $updatedOpeningStock = $result['balanceStock']; } - - - $updates [] = $result; - + + log_message('info',$count); + log_message('info',$date); + log_message('info', $result['opening']); + + $updates[] = $result; + } - if(!empty($updates)){ - $this->gasStockDetails_model->updateBatch($updates,'id'); + if (!empty($updates)) { + $result = $this->gasStockDetails_model->updateBatch($updates, 'id'); + return ($result !== false && $result > 0) ? $result : 0; + } else { + return 0; } } else { @@ -995,7 +1033,7 @@ class StockController extends BaseController $startDate = DateTime::createFromFormat('Y-m-d', $date)->modify('first day of this month')->format('Y-m-d'); - $endDate = DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month')->format('Y-m-d'); + $endDate = DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month')->format('Y-m-d'); $datesInMonth = []; $inserts = []; @@ -1036,7 +1074,10 @@ class StockController extends BaseController } if (!empty($inserts)) { - $this->gasStockDetails_model->insertBatch($inserts); + $result = $this->gasStockDetails_model->insertBatch($inserts); + return ($result !== false && $result > 0) ? $result : 0; + } else { + return 0; } } @@ -1154,24 +1195,27 @@ class StockController extends BaseController // Check if a row with the same date already exists $existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first(); - 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); + + $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'];