diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index ed5e3c56..553668ba 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -2018,7 +2018,7 @@ class StockController extends BaseController $electricMachines = $this->factoryMachine_model ->where('is_active', 1) - ->where('energy_type', 'Diesel') + ->where('energy_type', 'Electric') ->orderBy('id', 'asc') ->get() ->getResultArray(); @@ -2045,64 +2045,121 @@ class StockController extends BaseController } public function updatePowerConsumptionDetails(){ + + //pm stands for power machine $postData = $this->request->getPost(); $updatePowerConsumptionDetailsData = json_decode($postData['updatePowerConsumptionDetails'], true); - $updates = []; + $updateTotalPmDetailsData = $updatePowerConsumptionDetailsData[0]; + $updatePmDetailsData = $updatePowerConsumptionDetailsData[1]; + + //PmR stands for power machine readings - $inserts = []; - - foreach ($updatePowerConsumptionDetailsData as $data) { - - $date = $data['date']; - $machineId = $data['machine_id']; - - - $dbData = [ - 'date' => $data['date'], - 'machine_id' => $data['machine_id'], - 'opening_units' => $data['opening'], - 'closing_units' => $data['receipt'], - 'total_units' => $data['used'] - ]; - - $resultExists = $this->factoryVehicleDieselDetails_model - ->where('date', $date) - ->where('machine_id', $machineId) - ->first(); - - - if (empty($resultExists)) { - $inserts[] = $dbData; - } else { - $dbData['id'] = $resultExists['id']; - $updates[] = $dbData; - } - } - - - if (!empty($inserts)) { - $this->factoryVehicleDieselDetails_model->insertBatch($inserts); - } - - if (!empty($updates)) { - - $updateResult = $this->factoryVehicleDieselDetails_model->updateBatch($updates, 'id'); - - if ($updateResult === FALSE) { - echo "Error during update"; - } else { - echo "Data Updated Successfully"; - return; - } - } - - echo "Data Saved Successfully"; + $updatesPmR = []; + $insertsPmR = []; + $updatesTotalPmR = []; + $insertsTotalPmR = []; + // Fetch existing records in bulk for power machine details + $dates = array_column($updatePmDetailsData, 'date'); + $machineIds = array_column($updatePmDetailsData, 'machine_id'); + $existingPmRecords = $this->powerConsumptionDetails_model + ->whereIn('date', $dates) + ->whereIn('machine_id', $machineIds) + ->findAll(); + + $existingPmMap = []; + foreach ($existingPmRecords as $record) { + $existingPmMap[$record['date']][$record['machine_id']] = $record; + } + + foreach ($updatePmDetailsData as $data) { + $date = $data['date']; + $machineId = $data['machine_id']; + + $dbData = [ + 'date' => $data['date'], + 'machine_id' => $data['machine_id'], + 'opening_units' => $data['opening_units'], + 'closing_units' => $data['closing_units'], + 'total_units' => $data['total_units'], + ]; + + if (isset($existingPmMap[$date][$machineId])) { + $dbData['id'] = $existingPmMap[$date][$machineId]['id']; + $updatesPmR[] = $dbData; + } else { + $insertsPmR[] = $dbData; + } + } + + // Fetch existing records in bulk for power stock summary + $dates = array_column($updateTotalPmDetailsData, 'date'); + $existingTotalPmRecords = $this->powerConsumptionSummary_model + ->whereIn('date', $dates) + ->findAll(); + + $existingTotalPmMap = []; + foreach ($existingTotalPmRecords as $record) { + $existingTotalPmMap[$record['date']] = $record; + } + + foreach ($updateTotalPmDetailsData as $data) { + $date = $data['date']; + + $dbData = [ + 'date' => $data['date'], + 'opening_reading' => $data['opening_reading'], + 'final_reading' => $data['final_reading'], + 'total_reading' => $data['total_reading'], + 'total_units' => $data['total_units'], + 'average_pf' => $data['average_pf'], + 'present_pf' => $data['present_pf'], + 'md' => $data['md'], + 'mf' => $data['mf'] + ]; + + if (isset($existingTotalPmMap[$date])) { + $dbData['id'] = $existingTotalPmMap[$date]['id']; + $updatesTotalPmR[] = $dbData; + } else { + $insertsTotalPmR[] = $dbData; + } + } + + // Start the transaction + $this->db->transBegin(); + + try { + if (!empty($insertsPmR)) { + $this->powerConsumptionDetails_model->insertBatch($insertsPmR); + } + + if (!empty($updatesPmR)) { + $this->powerConsumptionDetails_model->updateBatch($updatesPmR, 'id'); + } + + if (!empty($insertsTotalPmR)) { + $this->powerConsumptionSummary_model->insertBatch($insertsTotalPmR); + } + + if (!empty($updatesTotalPmR)) { + $this->powerConsumptionSummary_model->updateBatch($updatesTotalPmR, '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(); + } } diff --git a/app/Views/powerConsumptionDetails.php b/app/Views/powerConsumptionDetails.php index 38392f3b..5b8faea9 100644 --- a/app/Views/powerConsumptionDetails.php +++ b/app/Views/powerConsumptionDetails.php @@ -335,11 +335,6 @@ foreach ($period as $day) { -
| Date | -Opening Reading | -Final Reading | -Total Reading | -Total Units | -Average PF | -Present PF | -MD | -MF *60 | +Date | +Opening Reading | +Final Reading | +Total Reading | +Total Units | +Average PF | +Present PF | +MD | +MF *60 | - + - - -Opening Units | -Final Units | -Total Units | + + +Opening Units | +Final Units | +Total Units | - | - | - | @@ -829,9 +830,9 @@ foreach ($period as $day) { |
|---|