diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 21843367..76cabd29 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -1409,7 +1409,9 @@ class StockController extends BaseController $inserts = $this->generateDummyData($startDate, $endDate); - $data['trpProductionMaintenanceData'] = $this->updateTrpProductionDetails($inserts); + $freshEntry = true ; + + $data['trpProductionMaintenanceData'] = $this->updateTrpProductionDetails($inserts ,$freshEntry); } @@ -1443,147 +1445,155 @@ class StockController extends BaseController - public function updateTrpProductionDetails($inserts) { - - $postData = $this->request->getPost(); - - if(empty($postData['updateTrpProductionDetails'])){ - if(empty($inserts)){ - return $this->response->setJSON(['error' => 'No data found to update']); - }else{ - $postData['updateTrpProductionDetails'] = $inserts; - } - } - - - $updateTrpProductionDetails = $postData['updateTrpProductionDetails']; - - $updateProduction = []; - $updateMaintanence = []; - $updateWaste = []; - - $insertProduction = []; - $insertMaintanence = []; - $insertWaste = []; - - foreach ($updateTrpProductionDetails as $data) { - - $data['date'] = $this->convertToDateWithFlexibleFormats($data['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"); - - $date = $data['date']; - - $dbDataTrpMaintenance = [ - 'date' => $data['date'], - 'openingTime' => $data['openingTime'], - 'closingTime' => $data['closingTime'], - 'totalHours' => $data['totalHours'], - 'coldStart' => $data['coldStart'], - 'breakdownHours' => $data['breakdownHours'], - 'burnerFiringHours' => $data['burnerFiringHours'], - 'sandFeedingHours' => $data['sandFeedingHours'], - 'workingPersonEngineers' => $data['workingPersonEngineers'], - 'workingPersonSupervisiors' => $data['workingPersonSupervisiors'], - 'workingPersonOperators' => $data['workingPersonOperators'], - 'rollerDrivers' => $data['rollerDrivers'], - 'natureOfMaintenance' => $data['natureOfMaintenance'], - 'location' => $data['location'], - 'description' => $data['description'] - ]; - - $dbDataTrpProduction = [ - 'date' => $data['date'], - 'gasReceiptBg' => $data['gasReceiptBg'], - 'gasReceiptPg' => $data['gasReceiptPg'], - 'physicalGasConsumption'=> $data['physicalGasConsumption'], - 'trpPlusDrierGasConsumption'=> $data['trpPlusDrierGasConsumption'], - 'trpPhysicalGasPerTon' => $data['trpPhysicalGasPerTon'], - 'panelGasConsumption' => $data['panelGasConsumption'], - 'panelGasPerTon' => $data['panelGasPerTon'], - 'edRunningHours' => $data['edRunningHours'], - 'edProduction' => $data['edProduction'], - 'edUsage' => $data['edUsage'], - 'trpProduction' => $data['trpProduction'], - 'trpProductionPerHour' => $data['trpProductionPerHour'], - 'waterReceipt' => $data['waterReceipt'], - 'waterConsumption' => $data['waterConsumption'], - 'ebReading' => $data['ebReading'], - 'ebReadingPerUnitTon' => $data['ebReadingPerUnitTon'], - ]; - - $dbDataTrpWaste = [ - 'date' => $data['date'], - 'msSeperation' => $data['msSeperation'], - 'edWaste' => $data['edWaste'], - 'coolerBags' => $data['coolerBags'], - 'edPlus20Waste' => $data['edPlus20Waste'], - 'cycloneWaste' => $data['cycloneWaste'], - ]; - - $trpMaintenanceResultExists = $this->TrpProductionMaintenance_model - ->where('date', $date) - ->first(); - - $trpProductionResultExists = $this->TrpProduction_model - ->where('date', $date) - ->first(); - - $trpWasteResultExists = $this->TrpProductionWaste_model - ->where('date', $date) - ->first(); - - if (empty($trpMaintenanceResultExists)) { - $insertMaintanence[] = $dbDataTrpMaintenance; - $insertProduction[] = $dbDataTrpProduction; - $insertWaste[] = $dbDataTrpWaste; - } else { - $dbDataTrpMaintenance['id'] = $trpMaintenanceResultExists['id']; - $dbDataTrpProduction['id'] = $trpProductionResultExists['id']; - $dbDataTrpWaste['id'] = $trpWasteResultExists['id']; - - $updateMaintanence[] = $dbDataTrpMaintenance; - $updateProduction[] = $dbDataTrpProduction; - $updateWaste[] = $dbDataTrpWaste; - } - } - - // Start the transaction - $this->db->transBegin(); - - try { - if (!empty($insertMaintanence)) { - $this->TrpProductionMaintenance_model->insertBatch($insertMaintanence); - } - - if (!empty($insertProduction)) { - $this->TrpProduction_model->insertBatch($insertProduction); - } - - if (!empty($insertWaste)) { - $this->TrpProductionWaste_model->insertBatch($insertWaste); - } - - if (!empty($updateMaintanence)) { - $this->TrpProductionMaintenance_model->updateBatch($updateMaintanence, 'id'); - } - - if (!empty($updateProduction)) { - $this->TrpProduction_model->updateBatch($updateProduction, 'id'); - } - - if (!empty($updateWaste)) { - $this->TrpProductionWaste_model->updateBatch($updateWaste, 'id'); - } - - // Commit the transaction if all operations are successful - $this->db->transCommit(); - echo "Data Saved Successfully"; - - } catch (\Exception $error) { - // Rollback the transaction in case of any errors - $this->db->transRollback(); - echo "An error occurred: " . $error->getMessage(); - } - } + public function updateTrpProductionDetails($inserts, $freshEntry = false) + { + $postData = $this->request->getPost(); + + if (empty($postData['updateTrpProductionDetails'])) { + if (empty($inserts)) { + return $this->response->setJSON(['error' => 'No data found to update']); + } else { + $postData['updateTrpProductionDetails'] = $inserts; + } + } + + $updateTrpProductionDetails = $postData['updateTrpProductionDetails']; + + // Extract all dates from input data + $dates = array_column($updateTrpProductionDetails, 'Date'); + $formattedDates = array_map(fn($date) => $this->convertToDateWithFlexibleFormats($date, ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"), $dates); + + // Fetch all existing records in a single query per table + $existingMaintenance = $this->TrpProductionMaintenance_model->whereIn('date', $formattedDates)->findAll(); + $existingProduction = $this->TrpProduction_model->whereIn('date', $formattedDates)->findAll(); + $existingWaste = $this->TrpProductionWaste_model->whereIn('date', $formattedDates)->findAll(); + + // Map existing records by date for quick lookup + $existingMaintenanceMap = array_column($existingMaintenance, null, 'date'); + $existingProductionMap = array_column($existingProduction, null, 'date'); + $existingWasteMap = array_column($existingWaste, null, 'date'); + + $insertMaintenance = []; + $updateMaintenance = []; + $insertProduction = []; + $updateProduction = []; + $insertWaste = []; + $updateWaste = []; + + foreach ($updateTrpProductionDetails as $data) { + $date = $this->convertToDateWithFlexibleFormats($data['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"); + + // Data arrays + $dbDataTrpMaintenance = [ + 'date' => $date, + 'openingTime' => $data['openingTime'], + 'closingTime' => $data['closingTime'], + 'totalHours' => $data['totalHours'], + 'coldStart' => $data['coldStart'], + 'breakdownHours' => $data['breakdownHours'], + 'burnerFiringHours' => $data['burnerFiringHours'], + 'sandFeedingHours' => $data['sandFeedingHours'], + 'workingPersonEngineers' => $data['workingPersonEngineers'], + 'workingPersonSupervisiors' => $data['workingPersonSupervisiors'], + 'workingPersonOperators' => $data['workingPersonOperators'], + 'rollerDrivers' => $data['rollerDrivers'], + 'natureOfMaintenance' => $data['natureOfMaintenance'], + 'location' => $data['location'], + 'description' => $data['description'] + ]; + + $dbDataTrpProduction = [ + 'date' => $date, + 'gasReceiptBg' => $data['gasReceiptBg'], + 'gasReceiptPg' => $data['gasReceiptPg'], + 'physicalGasConsumption' => $data['physicalGasConsumption'], + 'trpPlusDrierGasConsumption' => $data['trpPlusDrierGasConsumption'], + 'trpPhysicalGasPerTon' => $data['trpPhysicalGasPerTon'], + 'panelGasConsumption' => $data['panelGasConsumption'], + 'panelGasPerTon' => $data['panelGasPerTon'], + 'edRunningHours' => $data['edRunningHours'], + 'edProduction' => $data['edProduction'], + 'edUsage' => $data['edUsage'], + 'trpProduction' => $data['trpProduction'], + 'trpProductionPerHour' => $data['trpProductionPerHour'], + 'waterReceipt' => $data['waterReceipt'], + 'waterConsumption' => $data['waterConsumption'], + 'ebReading' => $data['ebReading'], + 'ebReadingPerUnitTon' => $data['ebReadingPerUnitTon'], + ]; + + $dbDataTrpWaste = [ + 'date' => $date, + 'msSeperation' => $data['msSeperation'], + 'edWaste' => $data['edWaste'], + 'coolerBags' => $data['coolerBags'], + 'edPlus20Waste' => $data['edPlus20Waste'], + 'cycloneWaste' => $data['cycloneWaste'], + ]; + + // Check if data already exists + if (isset($existingMaintenanceMap[$date])) { + $dbDataTrpMaintenance['id'] = $existingMaintenanceMap[$date]['id']; + $updateMaintenance[] = $dbDataTrpMaintenance; + } else { + $insertMaintenance[] = $dbDataTrpMaintenance; + } + + if (isset($existingProductionMap[$date])) { + $dbDataTrpProduction['id'] = $existingProductionMap[$date]['id']; + $updateProduction[] = $dbDataTrpProduction; + } else { + $insertProduction[] = $dbDataTrpProduction; + } + + if (isset($existingWasteMap[$date])) { + $dbDataTrpWaste['id'] = $existingWasteMap[$date]['id']; + $updateWaste[] = $dbDataTrpWaste; + } else { + $insertWaste[] = $dbDataTrpWaste; + } + } + + // Start transaction + $this->db->transBegin(); + + try { + // Insert new records in batch + if (!empty($insertMaintenance)) { + $this->TrpProductionMaintenance_model->insertBatch($insertMaintenance); + } + if (!empty($insertProduction)) { + $this->TrpProduction_model->insertBatch($insertProduction); + } + if (!empty($insertWaste)) { + $this->TrpProductionWaste_model->insertBatch($insertWaste); + } + + // Update existing records in batch + if (!empty($updateMaintenance)) { + $this->TrpProductionMaintenance_model->updateBatch($updateMaintenance, 'id'); + } + if (!empty($updateProduction)) { + $this->TrpProduction_model->updateBatch($updateProduction, 'id'); + } + if (!empty($updateWaste)) { + $this->TrpProductionWaste_model->updateBatch($updateWaste, 'id'); + } + + // Commit transaction if successful + $this->db->transCommit(); + + if (!$freshEntry) { + echo "Data Saved Successfully"; + } + } catch (\Exception $error) { + // Rollback transaction on error + $this->db->transRollback(); + echo "An error occurred: " . $error->getMessage(); + } + } + + @@ -1592,10 +1602,10 @@ class StockController extends BaseController $datesInMonth = []; $inserts = []; - $period = new DatePeriod( - new DateTime($startDate), - new DateInterval('P1D'), - (new DateTime($endDate))->modify('+1 day') + $period = new DatePeriod( + new DateTime($startDate), + new DateInterval('P1D'), + (new DateTime($endDate))->modify('+1 day') ); foreach ($period as $day) { @@ -1606,41 +1616,41 @@ class StockController extends BaseController $inserts[] = [ 'Date' => $dateInMonth, - 'openingTime' => '00:00', - 'closingTime' => '00:00', - 'totalHours' => '00:00', - 'coldStart' => '00:00', - 'breakdownHours' => '00:00', - 'burnerFiringHours' => '00:00', - 'sandFeedingHours' => '00:00', - 'workingPersonEngineers' => 0, - 'workingPersonSupervisiors' => 0, - 'workingPersonOperators' => 0, - 'rollerDrivers' => 0, + 'openingTime' => " ", + 'closingTime' => " ", + 'totalHours' => " ", + 'coldStart' => " ", + 'breakdownHours' => " ", + 'burnerFiringHours' => " ", + 'sandFeedingHours' => " ", + 'workingPersonEngineers' => " ", + 'workingPersonSupervisiors' => " ", + 'workingPersonOperators' => " ", + 'rollerDrivers' => " ", 'natureOfMaintenance' => '', 'location' => '', 'description' => '', - 'gasReceiptBg' => 0, - 'gasReceiptPg' => 0, - 'physicalGasConsumption' => 0, - 'trpPlusDrierGasConsumption' => 0, - 'trpPhysicalGasPerTon' => 0, - 'panelGasConsumption' => 0, - 'panelGasPerTon' => 0, - 'edRunningHours' => 0, - 'edProduction' => 0, - 'edUsage' => 0, - 'trpProduction' => 0, - 'trpProductionPerHour' => 0, - 'waterReceipt' => 0, - 'waterConsumption' => 0, - 'ebReading' => 0, - 'ebReadingPerUnitTon' => 0, - 'msSeperation' => 0, - 'edWaste' => 0, - 'coolerBags' => 0, - 'edPlus20Waste' => 0, - 'cycloneWaste' => 0 + 'gasReceiptBg' => " ", + 'gasReceiptPg' => " ", + 'physicalGasConsumption' => " ", + 'trpPlusDrierGasConsumption' => " ", + 'trpPhysicalGasPerTon' => " ", + 'panelGasConsumption' => " ", + 'panelGasPerTon' => " ", + 'edRunningHours' => " ", + 'edProduction' => " ", + 'edUsage' => " ", + 'trpProduction' => " ", + 'trpProductionPerHour' => " ", + 'waterReceipt' => " ", + 'waterConsumption' => " ", + 'ebReading' =>" ", + 'ebReadingPerUnitTon' =>" ", + 'msSeperation' => " ", + 'edWaste' => " ", + 'coolerBags' =>" ", + 'edPlus20Waste' => " ", + 'cycloneWaste' => " " ]; }