diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index bacba458..12aebdd1 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -42,6 +42,8 @@ class StockController extends BaseController protected $resinStockDetails_model; protected $gasStockDetails_model; + protected $db; + /** * This is default constructor of the class */ @@ -132,7 +134,6 @@ class StockController extends BaseController public function addNewCoatingMachineDetails() { - $postData = $this->request->getPost(); $month = $this->request->getGet('month'); $month = DateTime::createFromFormat('M-Y', $month)->format('Y-m'); @@ -140,6 +141,7 @@ class StockController extends BaseController $date = $postData['date']; $shift = $postData['shift']; + // Check if the same shift already exists on the same date $sameShiftOnDateExists = $this->coatingMachineDetails_model ->where('date', $date) ->where('shift', $shift) @@ -150,24 +152,40 @@ class StockController extends BaseController return redirect()->back()->with('error', 'Already same shift exists on the same date ..!!'); } - $inserted = $this->coatingMachineDetails_model->insert($postData); + // Start transaction + $this->db->transStart(); - if ($inserted) { - //update Gas stock consumption if insert is successfull..!! + 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); + // 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', '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 updateCoatingMachineDetails() { @@ -896,15 +914,60 @@ class StockController extends BaseController private function updateGasStockConsumption($date, $consumption) { + $resultExists = $this->gasStockDetails_model ->where('date', $date) ->first(); if ($resultExists) { - $resultExists['consumption'] = $resultExists['consumption'] + ($consumption); - $resultExists['balanceStock'] = $resultExists['opening'] - $resultExists['consumption']; - $this->gasStockDetails_model->update($resultExists['id'], $resultExists); + + if ($date instanceof DateTime) { + $date = $date->format('Y-m-d'); + } elseif (!strtotime($date)) { + throw new \Exception("Invalid date format provided."); + } else { + $date = date('Y-m-d', strtotime($date)); + } + + $endDate = date('Y-m-t', strtotime($date)); + + + //what happens if the updation is on the last day of the month? + $resultExists = $this->gasStockDetails_model + ->where('date >=', $date) + ->where('date <=', $endDate) + ->orderBy('date' , 'asc') + ->findAll(); + + $updatedOpeningStock=0; + $updates =[]; + + + foreach($resultExists as $index => $result){ + + 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']; + } + + + $updates [] = $result; + + + } + + if(!empty($updates)){ + $this->gasStockDetails_model->updateBatch($updates,'id'); + } } else { @@ -932,7 +995,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 = [];