From 830db88b7be903d426d261ed9275212911be70c7 Mon Sep 17 00:00:00 2001 From: vadivelJ96 Date: Sat, 25 Jan 2025 17:26:48 +0530 Subject: [PATCH] stock module latest today vadivel J 25-01-2025 --- app/Config/Routes.php | 7 - app/Controllers/StockController.php | 486 +++--- app/Models/PowerConsumptionSummaryModel.Php | 30 + app/Views/powerConsumptionDetails.php | 1619 ++++++++++++++----- 4 files changed, 1413 insertions(+), 729 deletions(-) create mode 100644 app/Models/PowerConsumptionSummaryModel.Php 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..ed5e3c56 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,224 @@ 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', 'Diesel') + ->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(){ + + + $postData = $this->request->getPost(); - if (empty($machineIds)) { - return []; - } + $updatePowerConsumptionDetailsData = json_decode($postData['updatePowerConsumptionDetails'], true); - $fields = ['opening_units', 'closing_units', 'total_units']; + $updates = []; - $pivotColumns = []; - foreach ($machineIds as $machineId) { - foreach ($fields as $field) { - $pivotColumns[] = "MAX(CASE WHEN machine_id = $machineId THEN $field ELSE 0 END) AS {$field}_{$machineId}"; - } - } + $inserts = []; - $pivotColumnString = implode(", ", $pivotColumns); + foreach ($updatePowerConsumptionDetailsData as $data) { - $sql = "SELECT `date`, $pivotColumnString FROM `t_powerConsumptionDetails` - WHERE machine_id IN (" . implode(",", $machineIds) . ") AND `date` >= ? AND `date` <= ? - GROUP BY `date`"; - - $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; - } - - 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 - ]; - } - - $transformedData[$insertKey][$field] = (float) $value; - } - } - } + $date = $data['date']; + $machineId = $data['machine_id']; - foreach ($transformedData as $row) { + $dbData = [ + 'date' => $data['date'], + 'machine_id' => $data['machine_id'], + 'opening_units' => $data['opening'], + 'closing_units' => $data['receipt'], + 'total_units' => $data['used'] + ]; - $existingRow = $this->powerConsumptionDetails_model->where('date', $row['date'])->where('machine_id', $row['machine_id'])->first(); - if ($existingRow) { - $this->powerConsumptionDetails_model->update($existingRow['id'], $row); + $resultExists = $this->factoryVehicleDieselDetails_model + ->where('date', $date) + ->where('machine_id', $machineId) + ->first(); + + + if (empty($resultExists)) { + $inserts[] = $dbData; } else { - $this->powerConsumptionDetails_model->insert($row); + $dbData['id'] = $resultExists['id']; + $updates[] = $dbData; } } - echo "Data successfully saved."; + 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"; + + + } + public function dieselMachineStockDetails(){ @@ -2290,208 +2381,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 @@ + + modify('first day of this month')->format('Y-m-d'); $endDate = $date->modify('last day of this month')->format('Y-m-d'); @@ -184,7 +240,7 @@ $datesInMonth = []; $period = new DatePeriod( new DateTime($startDate), new DateInterval('P1D'), - (new DateTime($endDate))->modify('+1 day') + (new DateTime($endDate))->modify('+1 day') ); foreach ($period as $day) { @@ -195,55 +251,6 @@ foreach ($period as $day) { - - - - - - - -
@@ -257,7 +264,7 @@ foreach ($period as $day) {
@@ -274,136 +281,526 @@ foreach ($period as $day) {
+
-
+
+
- - -
- -
- - - -
-
- -
- -
- -
- -
- - +
+
+ - - - - - - - - -
- - function formatHeader($key) - { - $parts = explode('_', $key); - if (is_numeric(end($parts))) { array_pop($parts); } - return ucwords(implode(' ', $parts)); - - } - ?> + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - + - - $row): ?> - - - $powerStockDetails) { + + + + ?> + format('d-m-Y'); + if ($dateInMonthDmYFormat === $currentDate) { + echo 'style="background: #cef0ad;"'; + } + ?>> + + + format('d-m-Y'); + ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + $powerStock) { ?> + + + + + + + + + + + + + + " - style="" - > - $column): ?> - - - - - - - - - - - - - - - + //before closing inner loop we just adding the total values inside it + $machineId = $powerStock['machine_id']; // Unique identifier + + + // Initialize the material entry if it doesn't exist + if (!isset($summary["machineId_$machineId"])) { + $summary["machineId_$machineId"] = [ + 'opening_units' => "-" , + 'closing_units' => 0 , + 'total_units' => 0 , + ]; + } + + // 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"]['total_units'] += is_numeric($powerStock['total_units']) ? $powerStock['total_units'] : 0 ; + + + + + + + + } ?> + + + + + +
+
Machine + 150 KW Power + + + + Others +
Date Opening Reading Final ReadingTotal ReadingTotal Units Average PFPresent PFMD MF *60Opening UnitsFinal UnitsTotal Units
+ + + oninput="openingTPStockChange(this)" + contenteditable="true" + + + > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + oninput="openingUnitsChange(this)" + contenteditable="true" + > + + + + + + + + + + + + + - format('d-m-Y'); ?> - style="padding: 5px;">
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Date Opening Reading Final ReadingTotal ReadingTotal Units Average PFPresent PFMD MF *60Opening UnitsFinal UnitsTotal Units
+ Total +
+
+ + + + + + $dateInMonth) { ?> + format('d-m-Y'); + if ($dateInMonthDmYFormat === $currentDate) { + echo 'style="background: #cef0ad;"'; + } + ?> > + + format('d-m-Y'); + + ?> + + + + + + + + + oninput="openingTPStockChange(this)" + contenteditable="true" + + + + > + + + + + + + + + + + + + + + + + + + + + + + + $powerMachine) { + ?> + + + + + + + + + + + + + + + oninput="openingUnitsChange(this)" + contenteditable="true" + + + + > + + + + + + + + + + + + + + - - -
+
- +
@@ -419,157 +816,531 @@ foreach ($period as $day) {
+
+ +
- + + + + \ No newline at end of file + function validateInput(input) { + + + if (input == '' || input == ' ' || input == '-') { + return 0; + } + + if (typeof input === 'string' && input.split('').map(char => char.charCodeAt(0))[0] == 10) { + return 0; + } + + // Validate against the regex + const isValid = /^-?\d*\.?\d*$/.test(input); + if (!isValid) { + alert('Invalid input! Please enter a valid number.'); + return 0; + } + + return input; + } + + + + + + + + \ No newline at end of file