diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index 4795365d..18b17b25 100755
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -435,6 +435,9 @@ $routes->match(['GET', 'POST', 'PUT', 'DELETE'],'addOrEditdrierMachineDetails',
$routes->match(['GET', 'POST', 'PUT', 'DELETE'],'factoryVehicleDieselDetails', 'StockController::factoryVehicleDieselDetails');
$routes->match(['GET', 'POST', 'PUT', 'DELETE'],'addOrEditfactoryVehicleDieselDetails', 'StockController::addOrEditfactoryVehicleDieselDetails');
+$routes->match(['GET', 'POST', 'PUT', 'DELETE'],'powerConsumptionDetails', 'StockController::powerConsumptionDetails');
+$routes->match(['GET', 'POST', 'PUT', 'DELETE'],'addOrEditPowerConsumptionDetails', 'StockController::addOrEditPowerConsumptionDetails');
+
//incoming Silica Sand Detaails
$routes->match(['GET','POST'],'incomingSilicaSandDetails', 'StockController::loadView_incomingSilicaSandDetails');
diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php
index d0fd93ac..d68212d4 100644
--- a/app/Controllers/StockController.php
+++ b/app/Controllers/StockController.php
@@ -3,6 +3,7 @@
namespace App\Controllers;
use App\Controllers\BaseController;
+use App\Models\PowerConsumptionDetails;
use App\Models\CoatingMachineDetailsModel;
use App\Models\DrierMachineDetailsModel;
use App\Models\FactoryMachineModel;
@@ -18,6 +19,7 @@ use DateTime;
class StockController extends BaseController
{
+ protected $powerConsumptionDetails_model;
protected $coatingMachineDetails_model;
protected $drierMachineDetails_model;
protected $factoryMachine_model;
@@ -37,6 +39,7 @@ class StockController extends BaseController
{
$this->db = \Config\Database::connect();
parent::__construct();
+ $this->powerConsumptionDetails_model = new PowerConsumptionDetails();
$this->coatingMachineDetails_model = new CoatingMachineDetailsModel();
$this->drierMachineDetails_model = new DrierMachineDetailsModel();
$this->factoryMachine_model = new FactoryMachineModel();
@@ -697,21 +700,146 @@ class StockController extends BaseController
}
+ foreach ($transformedData as $row) {
+
+ if($row['machine_id'] == 0)
+ {
+ // Summary data: if machine_id is 0, it indicates summary data. This distinction is used because these records will be stored in a separate table.
+ $this->factoryVehicleDieselStockSummary_model->where('date',$row['date'])->set($row)->update();
+
+ }else{
+
+ // Diesel - factory vehicle data :
+ $existingRow = $this->factoryVehicleDieselDetails_model->where('date', $row['date'])->where('machine_id', $row['machine_id'])->first();
+ if ($existingRow) {
+ $this->factoryVehicleDieselDetails_model->update($existingRow['id'], $row);
+ } else {
+ $this->factoryVehicleDieselDetails_model->insert($row);
+ }
+
+ }
+
+ }
+
+
+ echo "Data successfully saved.";
+
+ }
+
+
+ public function powerConsumptionDetails()
+ {
+ $currentMonth = $this->request->getPost('month') ? $this->request->getPost('month') : date('M-Y');
+
+ $monthData = $this->getMonthDatesFromInput($currentMonth);
+
+ $data['startDate'] = $monthData[0]['database'];
+ $data['endDate'] = $monthData[count($monthData)-1]['database'];
+ $data['datefordropdown'] = $currentMonth;
+
+ //Machine Details
+ $data['machineDetails'] = $this->factoryMachine_model->where('energy_type', 'electric')->where('is_active', 1)->findAll();
+ if(!count([$data['machineDetails']]))
+ {
+ return $this->response->setBody("");
+ }
+
+
+
+ //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'] ]);
+ }
+ $this->powerConsumptionDetails_model->insertBatch($tempInsert);
+
+ $data['powerConsumptionDetails'] = $this->getElectricConsumptionPivot($data['machineDetails'] , $data['startDate'] , $data['endDate']);
+ }
+
+
+
+
+ $data['machineName'] = array_column($data['machineDetails'], 'machine_name');
+
+ $this->global['pageTitle'] = 'Power Consumption Details';
+ $this->loadViews("powerConsumptionDetails", $this->global, $data, NULL);
+
+ }
+
+ public function getElectricConsumptionPivot($machineDetails , $startDate , $endDeate)
+ {
+ $machineIds = array_column($machineDetails, 'id');
+
+ if (empty($machineIds)) { return []; }
+
+ $fields = ['opening_units', 'closing_units', 'total_units'];
+
+ $pivotColumns = [];
+ foreach ($machineIds as $machineId) {
+ foreach ($fields as $field) {
+ $pivotColumns[] = "MAX(CASE WHEN machine_id = $machineId THEN $field ELSE 0 END) AS {$field}_{$machineId}";
+ }
+ }
+
+ $pivotColumnString = implode(", ", $pivotColumns);
+
+ $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;
+ }
+ }
+ }
+
foreach ($transformedData as $row) {
-
-
- // Check if a row with the same date already exists
- $existingRow = $this->factoryVehicleDieselDetails_model->where('date', $row['date'])->where('machine_id', $row['machine_id'])->first();
-
- if ($existingRow) {
- // If exists, update the row
- $this->factoryVehicleDieselDetails_model->update($existingRow['id'], $row);
- } else {
- // If not, insert a new row
- $this->factoryVehicleDieselDetails_model->insert($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);
+ }
}
diff --git a/app/Models/PowerConsumptionDetails.php b/app/Models/PowerConsumptionDetails.php
new file mode 100644
index 00000000..67a88fcc
--- /dev/null
+++ b/app/Models/PowerConsumptionDetails.php
@@ -0,0 +1,25 @@
+
-
diff --git a/app/Views/powerConsumptionDetails.php b/app/Views/powerConsumptionDetails.php
new file mode 100644
index 00000000..ea528e59
--- /dev/null
+++ b/app/Views/powerConsumptionDetails.php
@@ -0,0 +1,541 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Stocks
+ -
+
Diesel - Factory Vehicle Details
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ |
+
+ = $name; ?> |
+
+
+
+
+
+
+
+ | = formatHeader($column) ?> |
+
+
+
+
+
+ $row): ?>
+
+ $column): ?>
+
+
+ |
+ format('d-m-Y'); ?>
+ |
+
+ style="padding: 5px;">= $row[$column] ?? 'N/A' ?> |
+
+ = $row[$column] ?? 'N/A' ?> |
+
+ = $row[$column] ?? 'N/A' ?> |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file