diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 7b075eb5..94478251 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -463,16 +463,9 @@ $routes->post('updateDieselMachineDetails' , 'StockController::updateDieselMachi //power consumption details $routes->match(['GET', 'POST', 'PUT', 'DELETE'], 'powerConsumptionDetails', 'StockController::powerConsumptionDetails'); -$routes->match(['GET', 'POST', 'PUT', 'DELETE'], 'addOrEditPowerConsumptionDetails', 'StockController::addOrEditPowerConsumptionDetails'); - - -//new power consumption details -$routes->match(['GET','POST'] , 'newPowerConsumptionDetails' , 'StockController::newPowerConsumptionDetails'); $routes->post('updatePowerConsumptionDetails','StockController::updatePowerConsumptionDetails'); - - //incoming Silica Sand Detaails $routes->match(['GET', 'POST'], 'incomingSilicaSandDetails', 'StockController::loadView_incomingSilicaSandDetails'); $routes->post('updateIncomingSilicaSandDetails', 'StockController::updateIncomingSilicaSandDetails'); diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 456ce71b..553668ba 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -22,6 +22,7 @@ use App\Models\TrpProductionModel; use App\Models\TrpProductionMaintenanceModel; use App\Models\TrpProductionWasteModel; use App\Models\Employeedetails_model; +use App\Models\PowerConsumptionSummaryModel; @@ -36,6 +37,7 @@ class StockController extends BaseController protected $supplier_model; protected $powerConsumptionDetails_model; + protected $powerConsumptionSummary_model; protected $coatingMachineDetails_model; protected $drierMachineDetails_model; protected $factoryMachine_model; @@ -109,6 +111,8 @@ class StockController extends BaseController $this->employeedetails_model = new Employeedetails_model(); + $this->powerConsumptionSummary_model = new PowerConsumptionSummaryModel(); + $this->session = session(); helper('form'); @@ -1702,12 +1706,6 @@ class StockController extends BaseController return $times; } - - - - - - public function drierMachineDetails() { @@ -1892,131 +1890,281 @@ class StockController extends BaseController return $dateInput; } + public function powerConsumptionDetails(){ - - + + if ($this->request->getMethod() === 'POST') { - public function powerConsumptionDetails() - { - $currentMonth = $this->request->getPost('month') ? $this->request->getPost('month') : date('M-Y'); + $month = $this->request->getPost('month'); - $monthData = $this->getMonthDatesFromInput($currentMonth); + $date = DateTime::createFromFormat('M-Y', $month); - $data['startDate'] = $monthData[0]['database']; - $data['endDate'] = $monthData[count($monthData) - 1]['database']; - $data['datefordropdown'] = $currentMonth; + $formattedDate = $date->format('Y-m'); - //Machine Details - $data['machineDetails'] = $this->factoryMachine_model->where('energy_type', 'electric')->where('is_active', 1)->findAll(); - if (!count([$data['machineDetails']])) { - return $this->response->setBody(""); + $month = $formattedDate; + } else { + + // $month = '2024-12'; + + $month = date('Y-m'); } + $data = []; + + $data['month'] = $month; + + $this->global['pageTitle'] = 'Power Consumption Stock Details'; - //Diesel Consumption Details - $data['powerConsumptionDetails'] = $this->getElectricConsumptionPivot($data['machineDetails'], $data['startDate'], $data['endDate']); - if (!count($data['powerConsumptionDetails'])) { - //insert empty data - $tempInsert = []; - foreach ($monthData as $key => $value) { - array_push($tempInsert, ['date' => $value['database'], 'machine_id' => $data['machineDetails'][0]['id']]); + // Get the previous month's last date power consumption stock details for next month updation + + $previousMonth = DateTime::createFromFormat('Y-m', $month)->modify('-1 month')->format('Y-m'); + $previousMonthStartDate = "$previousMonth-01"; + $previousMonthEndDate = date("Y-m-t", strtotime($previousMonthStartDate)); + + $data['previousMonthPowerConsumptionStockDetails'] = $this->powerConsumptionDetails_model + ->select(['machine_id', 'date', 'closing_units']) + ->distinct() + ->where('date =', $previousMonthEndDate) + ->groupBy(['machine_id', 'date']) + ->orderBy('date', 'desc') + ->orderBy('machine_id', 'asc') + ->findAll(); + + $data['previousMonthTotalPowerMachineStockDetails'] = $this->powerConsumptionSummary_model + ->select(['id', 'date', 'final_reading']) + ->distinct() + ->where('date =', $previousMonthEndDate) + ->groupBy(['id', 'date']) + ->orderBy('date', 'desc') + ->findAll(); + + + + // checking in database if the data is available for the month + + $startDate = "$month-01"; + $endDate = date("Y-m-t", strtotime($startDate)); + + $data['powerConsumptionStockDetails'] = $this->powerConsumptionDetails_model + ->where('date >=', $startDate) + ->where('date <=', $endDate) + ->groupBy(['machine_id', 'date']) + ->orderBy('date', 'asc') + ->orderBy('machine_id', 'asc') + ->findAll(); + + $data['totalPower'] = $this->powerConsumptionSummary_model + ->where('date >=', $startDate) + ->where('date <=', $endDate) + ->orderBy('date', 'asc') + ->findAll(); + + // Initialize an empty array to hold the grouped data + $data['groupedPowerConsumptionStockDetails'] = []; + + // Temporary array to store grouped data by date + $groupedByDate = []; + + // Group by 'date' + foreach ($data['powerConsumptionStockDetails'] as $detail) { + $date = $detail['date']; + + // Initialize the outer array for this date if it doesn't exist + if (!isset($groupedByDate[$date])) { + $groupedByDate[$date] = []; } - $this->powerConsumptionDetails_model->insertBatch($tempInsert); - $data['powerConsumptionDetails'] = $this->getElectricConsumptionPivot($data['machineDetails'], $data['startDate'], $data['endDate']); + /******************************************************************************************************************* + creating an sub array (inner array) for date wise and append the detail into an array as sub-array + + $groupedByDate[] outer array + + $groupedByDate[$date] + will look like [[material1],[material2],[material3],[material4],[material5]] inner array + + the above process is done for single row (date wise) and the same process is repeated for all the rows + *******************************************************************************************************************/ + + $groupedByDate[$date][] = $detail; + } + + // Reset the structure to have indexed arrays without the date keys + $data['groupedPowerConsumptionStockDetails'] = array_values($groupedByDate); + + + // getting electric machines for creating table headers + // checking already existing stock if yes then get the electric machines id from the existing stock + // if not get all the electric machines from the factory Machine master table + + if (!empty($data['groupedPowerConsumptionStockDetails'])) { + + $result = $groupedByDate[$date]; + + $distinctMachineId = []; + + foreach ($result as $index => $eachResult) { + $distinctMachineId[] = $result[$index]['machine_id']; + } + + $electricMachines = $this->factoryMachine_model + ->whereIn('id', $distinctMachineId) + ->orderBy('id', 'asc') + ->get() + ->getResultArray(); + + + } else { + + $electricMachines = $this->factoryMachine_model + ->where('is_active', 1) + ->where('energy_type', 'Electric') + ->orderBy('id', 'asc') + ->get() + ->getResultArray(); + } + $electricMachinesCount = count($electricMachines); - $data['machineName'] = array_column($data['machineDetails'], 'machine_name'); + $data['electricMachinesCount'] = $electricMachinesCount; + $data['electricMachines'] = $electricMachines; + + $date = DateTime::createFromFormat('Y-m', $month); + + $formattedDate = $date->format('M-Y'); + + $data['month'] = $formattedDate; + + // return $this->response->setJSON(['status' =>'success' ,'message' => $data['electricMachines']]); + + return $this->loadViews("powerConsumptionDetails", $this->global, $data, NULL); - $this->global['pageTitle'] = 'Power Consumption Details'; - $this->loadViews("powerConsumptionDetails", $this->global, $data, NULL); } - public function getElectricConsumptionPivot($machineDetails, $startDate, $endDeate) - { - $machineIds = array_column($machineDetails, 'id'); + public function updatePowerConsumptionDetails(){ - if (empty($machineIds)) { - return []; - } + //pm stands for power machine + + + $postData = $this->request->getPost(); - $fields = ['opening_units', 'closing_units', 'total_units']; + $updatePowerConsumptionDetailsData = json_decode($postData['updatePowerConsumptionDetails'], true); - $pivotColumns = []; - foreach ($machineIds as $machineId) { - foreach ($fields as $field) { - $pivotColumns[] = "MAX(CASE WHEN machine_id = $machineId THEN $field ELSE 0 END) AS {$field}_{$machineId}"; - } - } + $updateTotalPmDetailsData = $updatePowerConsumptionDetailsData[0]; + $updatePmDetailsData = $updatePowerConsumptionDetailsData[1]; + + //PmR stands for power machine readings - $pivotColumnString = implode(", ", $pivotColumns); + $updatesPmR = []; + $insertsPmR = []; - $sql = "SELECT `date`, $pivotColumnString FROM `t_powerConsumptionDetails` - WHERE machine_id IN (" . implode(",", $machineIds) . ") AND `date` >= ? AND `date` <= ? - GROUP BY `date`"; + $updatesTotalPmR = []; + $insertsTotalPmR = []; - $query = $this->db->query($sql, [$startDate, $endDeate]); - $result = $query->getResultArray(); - - return $result; - } - - public function addOrEditPowerConsumptionDetails() - { - $electricMachineTableData = json_decode($this->request->getPost('electricMachineTableData'), true); - - $transformedData = []; - foreach ($electricMachineTableData as $entry) { - $date = $entry['date']; - - foreach ($entry as $key => $value) { - - if ($key === 'date') { - continue; + // 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; } - - preg_match('/(.*?)_(\d+)$/', $key, $matches); - - if (count($matches) === 3) { - $field = $matches[1]; - $machineId = $matches[2]; - - $insertKey = "{$date}_{$machineId}"; - if (!isset($transformedData[$insertKey])) { - $transformedData[$insertKey] = [ - 'date' => DateTime::createFromFormat('d-m-Y', $date)->format('Y-m-d'), - 'machine_id' => $machineId, - 'opening_units' => 0.00, - 'closing_units' => 0.00, - 'total_units' => 0.00 - ]; + + 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; } - - $transformedData[$insertKey][$field] = (float) $value; } - } - } + + // 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(); + } - - foreach ($transformedData as $row) { - - $existingRow = $this->powerConsumptionDetails_model->where('date', $row['date'])->where('machine_id', $row['machine_id'])->first(); - if ($existingRow) { - $this->powerConsumptionDetails_model->update($existingRow['id'], $row); - } else { - $this->powerConsumptionDetails_model->insert($row); - } - } - - - echo "Data successfully saved."; } + public function dieselMachineStockDetails(){ @@ -2290,208 +2438,7 @@ class StockController extends BaseController } } - public function newPowerConsumptionDetails(){ - - - if ($this->request->getMethod() === 'POST') { - - $month = $this->request->getPost('month'); - - $date = DateTime::createFromFormat('M-Y', $month); - - $formattedDate = $date->format('Y-m'); - - $month = $formattedDate; - } else { - - // $month = '2024-12'; - - $month = date('Y-m'); - } - - $data = []; - - $data['month'] = $month; - - $this->global['pageTitle'] = 'Power Consumption Stock Details'; - - - // Get the previous month's last date power consumption stock details for next month updation - - $previousMonth = DateTime::createFromFormat('Y-m', $month)->modify('-1 month')->format('Y-m'); - $previousMonthStartDate = "$previousMonth-01"; - $previousMonthEndDate = date("Y-m-t", strtotime($previousMonthStartDate)); - - $data['previousMonthPowerConsumptionStockDetails'] = $this->powerConsumptionDetails_model - ->select(['machine_id', 'date', 'closing_units']) - ->distinct() - ->where('date =', $previousMonthEndDate) - ->groupBy(['machine_id', 'date']) - ->orderBy('date', 'desc') - ->orderBy('machine_id', 'asc') - ->findAll(); - - - - // checking in database if the data is available for the month - - $startDate = "$month-01"; - $endDate = date("Y-m-t", strtotime($startDate)); - - $data['powerConsumptionStockDetails'] = $this->powerConsumptionDetails_model - ->where('date >=', $startDate) - ->where('date <=', $endDate) - ->groupBy(['machine_id', 'date']) - ->orderBy('date', 'asc') - ->orderBy('machine_id', 'asc') - ->findAll(); - - // Initialize an empty array to hold the grouped data - $data['groupedPowerConsumptionStockDetails'] = []; - - // Temporary array to store grouped data by date - $groupedByDate = []; - - // Group by 'date' - foreach ($data['powerConsumptionStockDetails'] as $detail) { - $date = $detail['date']; - - // Initialize the outer array for this date if it doesn't exist - if (!isset($groupedByDate[$date])) { - $groupedByDate[$date] = []; - } - - /******************************************************************************************************************* - creating an sub array (inner array) for date wise and append the detail into an array as sub-array - - $groupedByDate[] outer array - - $groupedByDate[$date] - will look like [[material1],[material2],[material3],[material4],[material5]] inner array - - the above process is done for single row (date wise) and the same process is repeated for all the rows - *******************************************************************************************************************/ - - $groupedByDate[$date][] = $detail; - } - - // Reset the structure to have indexed arrays without the date keys - $data['groupedPowerConsumptionStockDetails'] = array_values($groupedByDate); - - - // getting electric machines for creating table headers - // checking already existing stock if yes then get the electric machines id from the existing stock - // if not get all the electric machines from the factory Machine master table - - if (!empty($data['groupedPowerConsumptionStockDetails'])) { - - $result = $groupedByDate[$date]; - - $distinctMachineId = []; - - foreach ($result as $index => $eachResult) { - $distinctMachineId[] = $result[$index]['machine_id']; - } - - $electricMachines = $this->factoryMachine_model - ->whereIn('id', $distinctMachineId) - ->orderBy('id', 'asc') - ->get() - ->getResultArray(); - - - } else { - - $electricMachines = $this->factoryMachine_model - ->where('is_active', 1) - ->where('energy_type', 'Diesel') - ->orderBy('id', 'asc') - ->get() - ->getResultArray(); - - } - - - - $electricMachinesCount = count($electricMachines); - - $data['electricMachinesCount'] = $electricMachinesCount; - $data['electricMachines'] = $electricMachines; - - $date = DateTime::createFromFormat('Y-m', $month); - - $formattedDate = $date->format('M-Y'); - - $data['month'] = $formattedDate; - - return $this->response->setJSON(['status' =>'success' ,'message' => $data['electricMachines']]); - - // return $this->loadViews("powerConsumptionStockDetails", $this->global, $data, NULL); - - } - - public function updatePowerConsumptionDetails(){ - - - $postData = $this->request->getPost(); - - $updatePowerConsumptionDetailsData = json_decode($postData['updatePowerConsumptionDetails'], true); - - $updates = []; - - $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"; - - - - } - + diff --git a/app/Models/PowerConsumptionSummaryModel.Php b/app/Models/PowerConsumptionSummaryModel.Php new file mode 100644 index 00000000..80aff61d --- /dev/null +++ b/app/Models/PowerConsumptionSummaryModel.Php @@ -0,0 +1,30 @@ +.xlsx'); }) diff --git a/app/Views/bagStockDetails.php b/app/Views/bagStockDetails.php index 06a27484..ae0a1a0a 100644 --- a/app/Views/bagStockDetails.php +++ b/app/Views/bagStockDetails.php @@ -97,29 +97,7 @@ padding-bottom: 8px; padding-top: 8px; width: 100%; - } - - - .celda_encabezado_general { - background-color: #00a65a; - border: 1px solid #ccc; - color: #ffffff; - font-weight: bold; - padding: 6px 10px;; - text-align: center; - } - - - .celda_encabezado_total { - background-color:#ffffff; - border: 1px solid #ccc; - color: #ffffff; - font-weight: bold; - padding: 6px 10px; - ; - text-align: center; - } - + } ._Separador { background-color: #fff; @@ -133,14 +111,6 @@ width: 4px; } - .celda_normal { - /*background-color: #fff;*/ - /* */ - text-align: center; - border: 1px solid #ccc; - padding: 2px 4px; - } - /*style excel*/ .excel_cell { border: 1px solid #CCC; @@ -167,37 +137,82 @@ height: 20px; } - #bagStockDetailsTableId th:nth-child(1), - #bagStockDetailsTableId td:nth-child(1) { - position: sticky; - left: 0; + + + .celda_encabezado_general { background-color: #00a65a; - z-index: 2; - border-right: 2px solid #ddd; + border: 1px solid #ccc; color: #ffffff; + font-weight: bold; + padding: 6px 10px; + text-align: center; } + .celda_encabezado_total { + background-color:#ffffff !important; + border: 1px solid #ffffff; + color: #ffffff !important; + font-weight: bold; + padding: 6px 10px; + text-align: center; + } + .celda_normal { + /*background-color: #fff;*/ + /* */ + text-align: center; + border: 1px solid #ccc; + padding: 10px 6px; + } - #bagStockDetailsTableSummaryId th:nth-child(1), - #bagStockDetailsTableSummaryId td:nth-child(1) { + + /* Make entire thead sticky */ + .fht-table thead { + position: sticky; + top: 0; + z-index: 20; /* Ensure the entire header stays above table rows */ + background-color: #00a65a; /* Header background color */ + } + + .dropdown-menu { + z-index :25 ; + } + + + + /* Ensure each