stock module latest today vadivel J 25-01-2025

This commit is contained in:
vadivelJ96 2025-01-25 17:26:48 +05:30
parent 0307cc1411
commit 830db88b7b
4 changed files with 1413 additions and 729 deletions

View File

@ -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');

View File

@ -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("<script>alert('No Machine data available');</script>");
$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";
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class PowerConsumptionSummaryModel extends Model
{
protected $table = 't_powerConsumptionSummary';
protected $primaryKey = 'id';
protected $allowedFields = [
'date',
'machine_id',
'opening_reading',
'final_reading',
'total_reading',
'total_units',
'present_pf',
'average_pf',
'md',
'mf'
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
}

File diff suppressed because it is too large Load Diff