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) { - - Others - - - @@ -377,7 +372,7 @@ foreach ($period as $day) { $summary = []; - foreach ($groupedPowerConsumptionStockDetails as $groupedPowerConsumptionStockDetailsIndex => $powerStockDetails) { + foreach ($groupedPowerConsumptionStockDetails as $groupedIndex => $powerStockDetails) { @@ -395,7 +390,7 @@ foreach ($period as $day) { format('d-m-Y'); + $startDate = DateTime::createFromFormat('Y-m-d', $totalPower[$groupedIndex]['date'])->format('d-m-Y'); ?> @@ -404,72 +399,73 @@ foreach ($period as $day) { + oninput="openingTPStockChange(this)" contenteditable="true" - > + > - + + data-id="" > - + - + - + - + - + - + @@ -479,7 +475,7 @@ foreach ($period as $day) { //before closing outer loop if (!isset($summary['openingReading'])) { - $summary['openingReading'] = '-' ; + $summary['openingReading'] = 0 ; $summary['finalReading'] = 0 ; $summary['totalReading'] = 0 ; $summary['totalUnits'] = 0 ; @@ -489,15 +485,19 @@ foreach ($period as $day) { $summary['mf'] = 0 ; } - $summary['openingReading'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['openingReading'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['openingReading'] ; - $summary['finalReading'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['finalReading'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['finalReading'] ; - $summary['totalReading'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['totalReading'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['totalReading'] ; - $summary['totalUnits'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['totalUnits'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['totalUnits'] ; - $summary['averagePf'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['averagePf'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['averagePf'] ; - $summary['presentPf'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['presentPf'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['balance_stock'] ; - $summary['md'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['md'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['md'] ; - $summary['mf'] += $totalPower[$groupedPowerConsumptionStockDetailsIndex]['mf'] == '-' ? 0 : $totalPower[$groupedPowerConsumptionStockDetailsIndex]['mf'] ; - + + + $summary['openingReading'] = " " ; + $summary['finalReading'] = is_numeric($totalPower[$groupedIndex]['final_reading']) ? $totalPower[$groupedIndex]['final_reading'] : 0 ; + $summary['totalReading'] += is_numeric($totalPower[$groupedIndex]['total_reading']) ? $totalPower[$groupedIndex]['total_reading'] : 0 ; + $summary['totalUnits'] += is_numeric($totalPower[$groupedIndex]['total_units']) ? $totalPower[$groupedIndex]['total_units'] : 0 ; + $summary['averagePf'] = " " ; + $summary['presentPf'] = " "; + $summary['md'] = " " ; + $summary['mf'] = " " ; + + + ?> @@ -521,7 +521,7 @@ foreach ($period as $day) { + oninput="openingUnitsChange(this)" contenteditable="true" > @@ -570,8 +570,8 @@ foreach ($period as $day) { // Sum values for the material - $summary["machineId_$machineId"]['opening_units'] = 0 ; - $summary["machineId_$machineId"]['closing_units'] += is_numeric($powerStock['closing_units']) ? $powerStock['closing_units'] : 0 ; + $summary["machineId_$machineId"]['opening_units'] = " " ; + $summary["machineId_$machineId"]['closing_units'] = " " ; $summary["machineId_$machineId"]['total_units'] += is_numeric($powerStock['total_units']) ? $powerStock['total_units'] : 0 ; @@ -592,25 +592,25 @@ foreach ($period as $day) { - + - - - - - - - - - + + + + + + + + + - + - - - - - + + + + + @@ -728,21 +728,22 @@ foreach ($period as $day) { > - - - - @@ -829,9 +830,9 @@ foreach ($period as $day) {
- - +
- + @@ -868,11 +869,11 @@ foreach ($period as $day) {
Date Opening Reading Final ReadingTotal ReadingTotal Units Average PFPresent PFMD MF *60Date Opening Reading Final ReadingTotal ReadingTotal Units Average PFPresent PFMD MF *60 Opening UnitsFinal UnitsTotal UnitsOpening UnitsFinal UnitsTotal Units