diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index b624935c..a18b3c3e 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -18,6 +18,8 @@ use App\Models\ResinStockModel; use App\Models\GasStockModel; use CodeIgniter\HTTP\ResponseInterface; use DateTime; +use DatePeriod; +use DateInterval; class StockController extends BaseController { @@ -855,10 +857,58 @@ class StockController extends BaseController echo "Data Saved Successfully"; } + private function updateGasStockConsumption($date , $consumption){ + echo"$date"; + $resultExists = $this->gasStockDetails_model + ->where('date',$date) + ->first(); + + if($resultExists){ + $resultExists['consumption'] = $resultExists['consumption']+($consumption); + $this->gasStockDetails_model->update($resultExists['id'],$resultExists); + } + else{ + $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'); + + $datesInMonth = []; + $inserts = []; + + $period = new DatePeriod( + new DateTime($startDate), + new DateInterval('P1D'), + (new DateTime($endDate))->modify('+1 day') + ); + + foreach ($period as $day) { + $datesInMonth[] = $day->format('Y-m-d'); + } + + foreach ($datesInMonth as $datesInMonthIndex => $dateInMonth) { + + $inserts[] = [ + 'date' => $dateInMonth, + 'opening' => 0, + 'purchaseBharath' => 0, + 'purchaseIndian' => 0, + 'total' => 0, + 'consumption' => $dateInMonth == $date ? $consumption : 0 , + 'balanceStock' => 0 + ]; + + } + + if(!empty($inserts)){ + $this->gasStockDetails_model->insertBatch($inserts); + + } + + } + } // --------------------------------------------gwm---------------------------------------------- @@ -1003,9 +1053,27 @@ class StockController extends BaseController 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); + } + $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); + + $this->drierMachineDetails_model->insert($data); } } diff --git a/app/Views/gasStockDetails.php b/app/Views/gasStockDetails.php index 88803b65..672eff1f 100644 --- a/app/Views/gasStockDetails.php +++ b/app/Views/gasStockDetails.php @@ -195,8 +195,7 @@ - - +