2288 lines
78 KiB
PHP
2288 lines
78 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Supplier_model;
|
|
use App\Models\PowerConsumptionDetails;
|
|
use App\Models\CoatingMachineDetailsModel;
|
|
use App\Models\DrierMachineDetailsModel;
|
|
use App\Models\FactoryMachineModel;
|
|
use App\Models\FactoryVehicleDieselDetailsModel;
|
|
use App\Models\FactoryVehicleDieselStockSummaryModel;
|
|
use App\Models\IncomingSilicaSandDetailsModel;
|
|
use App\Models\Inwardgateregister_model;
|
|
use App\Models\Rawmaterialdetails_model;
|
|
use App\Models\BagStockDetailsModel;
|
|
use App\Models\DustAndRoughModel;
|
|
use App\Models\ResinStockModel;
|
|
use App\Models\GasStockModel;
|
|
use App\Models\TrpSandUseStockModel;
|
|
use App\Models\TrpProductionModel;
|
|
use App\Models\TrpProductionMaintenanceModel;
|
|
use App\Models\TrpProductionWasteModel;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use DateTime;
|
|
use DatePeriod;
|
|
use DateInterval;
|
|
|
|
class StockController extends BaseController
|
|
{
|
|
|
|
protected $supplier_model;
|
|
protected $powerConsumptionDetails_model;
|
|
protected $coatingMachineDetails_model;
|
|
protected $drierMachineDetails_model;
|
|
protected $factoryMachine_model;
|
|
protected $factoryVehicleDieselDetails_model;
|
|
protected $factoryVehicleDieselStockSummary_model;
|
|
protected $session;
|
|
protected $incomingSilicaSandDetails_model;
|
|
protected $inwardGateRegister_model;
|
|
protected $rawmaterialdetails_model;
|
|
protected $bagStockDetails_model;
|
|
protected $Rawmaterialdetails_model;
|
|
protected $dustAndRoughStockDetails_model;
|
|
protected $resinStockDetails_model;
|
|
protected $gasStockDetails_model;
|
|
|
|
protected $TrpSandUseStock_model;
|
|
|
|
protected $TrpProduction_model;
|
|
|
|
protected $TrpProductionMaintenance_model;
|
|
|
|
protected $TrpProductionWaste_model;
|
|
|
|
protected $db;
|
|
|
|
/**
|
|
* This is default constructor of the class
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->db = \Config\Database::connect();
|
|
parent::__construct();
|
|
|
|
$this->supplier_model = new Supplier_model();
|
|
|
|
$this->powerConsumptionDetails_model = new PowerConsumptionDetails();
|
|
|
|
$this->coatingMachineDetails_model = new CoatingMachineDetailsModel();
|
|
|
|
$this->drierMachineDetails_model = new DrierMachineDetailsModel();
|
|
|
|
$this->factoryMachine_model = new FactoryMachineModel();
|
|
|
|
$this->factoryVehicleDieselDetails_model = new FactoryVehicleDieselDetailsModel();
|
|
|
|
$this->factoryVehicleDieselStockSummary_model = new FactoryVehicleDieselStockSummaryModel();
|
|
|
|
$this->rawmaterialdetails_model = new Rawmaterialdetails_model();
|
|
|
|
$this->incomingSilicaSandDetails_model = new IncomingSilicaSandDetailsModel();
|
|
|
|
$this->inwardGateRegister_model = new Inwardgateregister_model();
|
|
|
|
$this->bagStockDetails_model = new BagStockDetailsModel();
|
|
|
|
$this->dustAndRoughStockDetails_model = new DustAndRoughModel();
|
|
|
|
$this->resinStockDetails_model = new ResinStockModel();
|
|
|
|
$this->gasStockDetails_model = new GasStockModel();
|
|
|
|
$this->TrpSandUseStock_model = new TrpSandUseStockModel();
|
|
|
|
$this->TrpProduction_model = new TrpProductionModel();
|
|
|
|
$this->TrpProductionMaintenance_model = new TrpProductionMaintenanceModel();
|
|
|
|
$this->TrpProductionWaste_model = new TrpProductionWasteModel();
|
|
|
|
|
|
$this->session = session();
|
|
helper('form');
|
|
|
|
$this->isLoggedIn();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
//Coating Machine Details starts
|
|
public function loadView_coatingMachineDetails()
|
|
{
|
|
|
|
if ($this->request->getMethod() === 'POST') {
|
|
$month = $this->request->getPost('month');
|
|
|
|
$date = DateTime::createFromFormat('M-Y', $month);
|
|
|
|
$formattedDate = $date->format('Y-m');
|
|
|
|
$month = $formattedDate;
|
|
} else {
|
|
|
|
$filteredMonth = $this->request->getGet('month');
|
|
$month = $filteredMonth ?? date('Y-m');
|
|
}
|
|
|
|
$this->global['pageTitle'] = 'Coating Machine Details ';
|
|
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
$data['coatingMachineDetails'] = $this->coatingMachineDetails_model
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->where('is_active', 1)
|
|
->orderBy('date', 'asc')
|
|
->findAll();
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
return $this->loadViews("CoatingMachineDetail", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function addNewCoatingMachineDetails()
|
|
{
|
|
$message1 = '';
|
|
$message2 = '';
|
|
$message3 = '';
|
|
|
|
$postData = $this->request->getPost();
|
|
$month = $this->request->getGet('month');
|
|
$month = DateTime::createFromFormat('M-Y', $month)->format('Y-m');
|
|
|
|
$date = $postData['date'];
|
|
$shift = $postData['shift'];
|
|
|
|
// Check if the same shift already exists on the same date
|
|
$sameShiftOnDateExists = $this->coatingMachineDetails_model
|
|
->where('date', $date)
|
|
->where('shift', $shift)
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
if (!empty($sameShiftOnDateExists)) {
|
|
return redirect()->back()->with('error', 'Already same shift exists on the same date ..!!');
|
|
}
|
|
|
|
// Start transaction
|
|
$this->db->transBegin();
|
|
|
|
try {
|
|
// Insert into coating machine details table
|
|
$inserted = $this->coatingMachineDetails_model->insert($postData);
|
|
|
|
if ($inserted !== false && $inserted > 0) {
|
|
|
|
$message1 = 'Coating Machine details updated successfully';
|
|
|
|
// Update gas stock consumption
|
|
$dateToBeInserted = $postData['date'];
|
|
$consumption = $postData['totalGasConsumption'];
|
|
$updatedResult = $this->updateGasStockConsumption($dateToBeInserted, $consumption);
|
|
|
|
if ($updatedResult === false || $updatedResult <= 0) {
|
|
// Explicit failure handling for gas stock update
|
|
throw new \Exception('Failed to update gas stock consumption.');
|
|
}
|
|
$message2 = 'And updated gas stock consumption.';
|
|
$message3 = " Warning..!! Kindly Update the next subsequent month Gas Stock If older month Gas Stock Updated..!!";
|
|
} else {
|
|
throw new \Exception('Failed to insert Coating machine details.');
|
|
}
|
|
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
throw new \Exception('Transaction failed due to database errors.');
|
|
}
|
|
$this->db->transCommit();
|
|
|
|
|
|
|
|
// Set flashdata for success message
|
|
return redirect()->to("/coatingMachineDetails?month=$month")
|
|
->with('success', $message1 . " " . $message2 . " " . $message3);
|
|
} catch (\Exception $error) {
|
|
$this->db->transRollback();
|
|
return redirect()->back()->with('error', ' ' . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function updateCoatingMachineDetails()
|
|
{
|
|
$message1 = '';
|
|
$message2 = '';
|
|
$message3 = '';
|
|
|
|
$putData = $this->request->getPost();
|
|
$month = $this->request->getGet('month');
|
|
$month = DateTime::createFromFormat('M-Y', $month)->format('Y-m');
|
|
|
|
$id = $putData['editCoatingMachineDetailId'];
|
|
|
|
// Start transaction
|
|
$this->db->transBegin();
|
|
try {
|
|
$updated = $this->coatingMachineDetails_model->update($id, $putData);
|
|
if ($updated !== false && $updated > 0) {
|
|
$message1 = "successfully updated Coating machineDetail .";
|
|
//update Gas stock consumption if update is successfull..!!
|
|
$dateToBeUpdated = $putData['date'];
|
|
|
|
$existingCoatingGasConsumption = $putData['existingTotalGasConsumption'];
|
|
$updatedCoatingGasConsumption = $putData['totalGasConsumption'];
|
|
|
|
if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) {
|
|
|
|
$changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption;
|
|
$updatedResult = $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
|
|
|
|
if ($updatedResult === false || $updatedResult <= 0) {
|
|
// Explicit failure handling for gas stock update
|
|
throw new \Exception('Failed to update gas stock consumption.');
|
|
}
|
|
$message2 = 'And updated gas stock consumption.';
|
|
|
|
$message3 = "Warning..!! Kindly Update the next subsequent month Gas Stock If older month Gas Stock Updated..!!";
|
|
}
|
|
// Commit the transaction if all queries succeed
|
|
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
throw new \Exception('Transaction failed due to database errors.');
|
|
}
|
|
$this->db->transCommit();
|
|
|
|
|
|
|
|
|
|
// Set flashdata for success message
|
|
return redirect()->to("/coatingMachineDetails?month=$month")
|
|
->with('success', $message1 . " " . $message2 . " " . $message3);
|
|
} else {
|
|
throw new \Exception('Failed to update Coating machine details.');
|
|
}
|
|
} catch (\Exception $error) {
|
|
$this->db->transRollback();
|
|
return redirect()->back()->with('error', ' ' . $error->getMessage());
|
|
}
|
|
}
|
|
|
|
public function deleteCoatingMachineDetails()
|
|
{
|
|
|
|
$id = $this->request->getGet('id');
|
|
|
|
$data['is_active'] = 0;
|
|
|
|
$updated = $this->coatingMachineDetails_model->update($id, $data);
|
|
|
|
if ($updated) {
|
|
// Set flashdata for success message
|
|
return redirect()->to('/coatingMachineDetails')->with('success', 'Coating Machine details deleted successfully');
|
|
} else {
|
|
// Set flashdata for error message
|
|
return redirect()->back()->with('error', 'Failed to delete Coating machine details');
|
|
}
|
|
}
|
|
|
|
//incoming Silica Sand Details starts
|
|
public function loadView_incomingSilicaSandDetails()
|
|
{
|
|
|
|
if ($this->request->getMethod() === 'POST') {
|
|
|
|
$remark = $this->request->getPost('remark');
|
|
|
|
$month = $this->request->getPost('month');
|
|
|
|
$date = DateTime::createFromFormat('M-Y', $month);
|
|
|
|
$formattedDate = $date->format('Y-m');
|
|
|
|
$month = $formattedDate;
|
|
} else {
|
|
$remark = $this->request->getVar('remark');
|
|
|
|
$month = date('Y-m');
|
|
}
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = 'Incoming Silica Sand Details';
|
|
|
|
$materialCodes = [];
|
|
|
|
$silicaSandMaterialCodes = $this->rawmaterialdetails_model->getAllSilicaRawMaterialCode();
|
|
|
|
$materialCodes [] = $silicaSandMaterialCodes;
|
|
|
|
$wasteCoreSandMaterialCodes = $this->rawmaterialdetails_model->getAllWasteCoreSandMaterialCode();
|
|
|
|
$materialCodes [] = $wasteCoreSandMaterialCodes;
|
|
|
|
$materialCodes = call_user_func_array('array_merge', $materialCodes);
|
|
|
|
|
|
if (!empty($materialCodes)) {
|
|
$data['igrDetails'] = $this->inwardGateRegister_model->findIgrDetailsForIncomingSilicaSand($materialCodes, $month, $remark);
|
|
} else {
|
|
$data['igrDetails'] = [];
|
|
}
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
$data['remark'] = $remark;
|
|
|
|
return $this->loadViews("incomingSilicaSandDetails", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function updateIncomingSilicaSandDetails()
|
|
{
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$incomingSilicaSandDetailsData = json_decode($postData['incomingSilicaSandDetailsData'], true);
|
|
|
|
$inserts = [];
|
|
$updates = [];
|
|
|
|
foreach ($incomingSilicaSandDetailsData as $data) {
|
|
|
|
$id = $data['id'];
|
|
|
|
$dbData = [
|
|
'igrNo_fk' => $data['IGR No'],
|
|
'materialCode' => $data['MaterialCode'],
|
|
'grade' => $data['Grade'],
|
|
'gradeCondition' => $data['Grade Condition'],
|
|
'_10' => $data['___10___'],
|
|
'_20' => $data['___20___'],
|
|
'_30' => $data['___30___'],
|
|
'_40' => $data['___40___'],
|
|
'_50' => $data['___50___'],
|
|
'_70' => $data['___70___'],
|
|
'_100' => $data['___100___'],
|
|
'_140' => $data['___140___'],
|
|
'_200' => $data['___200___'],
|
|
'_300' => $data['___300___'],
|
|
'pan' => $data['PAN'],
|
|
'total' => $data['Total'],
|
|
'afsSpec' => $data['AFS Spec'],
|
|
'afsActual' => $data['AFS Actual'],
|
|
'plus20loss' => $data['Plus 20 loss%'],
|
|
'plus30Loss' => $data['Plus 30 Loss %'],
|
|
'moistureSpec' => $data['Moisture % Spec'],
|
|
'moistureActual' => $data['Moisture Actual'],
|
|
'moistureLoss' => $data['Moisture Loss'],
|
|
'moistureLossQty' => $data['Moisture loss Qty'],
|
|
'sieveLossQty' => $data['Sieve Loss Qty'],
|
|
'salableQty' => $data['Salable Qty'],
|
|
'remark' => $data['Remark']
|
|
|
|
];
|
|
|
|
|
|
|
|
$resultExists = $this->incomingSilicaSandDetails_model
|
|
->where('id', $id)
|
|
->first();
|
|
|
|
if (!empty($resultExists)) {
|
|
$dbData['id'] = $resultExists['id'];
|
|
$updates[] = $dbData;
|
|
} else {
|
|
$inserts[] = $dbData;
|
|
}
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
$updated = $this->incomingSilicaSandDetails_model->updateBatch($updates, 'id');
|
|
}
|
|
|
|
if (!empty($inserts)) {
|
|
$inserted = $this->incomingSilicaSandDetails_model->insertBatch($inserts);
|
|
}
|
|
|
|
echo "Data Saved Successfully";
|
|
}
|
|
|
|
public function updateLabReport()
|
|
{
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$id = $postData['id'];
|
|
|
|
$dbData = [
|
|
'igrNo_fk' => $postData['igrNo'],
|
|
'materialCode' => $postData['materialCode'],
|
|
'_10' => $postData['_10Mm'],
|
|
'_20' => $postData['_20Mm'],
|
|
'_30' => $postData['_30Mm'],
|
|
'_40' => $postData['_40Mm'],
|
|
'_50' => $postData['_50Mm'],
|
|
'_70' => $postData['_70Mm'],
|
|
'_100' => $postData['_100Mm'],
|
|
'_140' => $postData['_140Mm'],
|
|
'_200' => $postData['_200Mm'],
|
|
'_300' => $postData['_300Mm'],
|
|
'pan' => $postData['pan'],
|
|
'total' => $postData['total'],
|
|
'plus20Loss' => $postData['plus20Loss'],
|
|
'plus30Loss' => $postData['plus30Loss'],
|
|
'moistureSpec' => $postData['moistureSpec'],
|
|
'moistureActual' => $postData['moistureActual'],
|
|
'moistureLoss' => $postData['moistureLoss'],
|
|
'moistureLossQty' => $postData['moistureLossQty'],
|
|
'sieveLossQty' => $postData['sieveLossQty'],
|
|
'salableQty' => $postData['salableQty'],
|
|
|
|
];
|
|
|
|
$resultExists = $this->incomingSilicaSandDetails_model
|
|
->where('id', $id)
|
|
->first();
|
|
if ($resultExists) {
|
|
$updated = $this->incomingSilicaSandDetails_model
|
|
->update($id, $dbData);
|
|
} else {
|
|
$updated = $this->incomingSilicaSandDetails_model
|
|
->insert($dbData);
|
|
}
|
|
|
|
|
|
if ($updated) {
|
|
return $this->response->setJSON(['status' => 'success', 'message' => 'Data Updated Successfully']);
|
|
} else {
|
|
return $this->response->setJSON(['status' => 'error', 'message' => 'Failed to update data']);
|
|
}
|
|
}
|
|
|
|
|
|
// Bag Stock details starts
|
|
public function loadView_bagStockDetails()
|
|
{
|
|
|
|
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 = date('Y-m');
|
|
}
|
|
|
|
$data = [];
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = 'Bag Stock Details';
|
|
|
|
|
|
// Get the previous month's last date bag 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['previousMonthBagStockDetails'] = $this->bagStockDetails_model
|
|
->select(['materialCode', 'date', 'balanceStock'])
|
|
->distinct()
|
|
->where('date =', $previousMonthEndDate)
|
|
->groupBy(['materialCode', 'date'])
|
|
->orderBy('date', 'desc')
|
|
->orderBy('materialCode', 'asc')
|
|
->findAll();
|
|
|
|
|
|
|
|
// checking in database if the data is available for the month
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
$data['bagStockDetails'] = $this->bagStockDetails_model
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->groupBy(['materialCode', 'date'])
|
|
->orderBy('date', 'asc')
|
|
->orderBy('materialCode', 'asc')
|
|
->findAll();
|
|
|
|
// Initialize an empty array to hold the grouped data
|
|
$data['groupedBagStockDetails'] = [];
|
|
|
|
// Temporary array to store grouped data by date
|
|
$groupedByDate = [];
|
|
|
|
// Group by 'date'
|
|
foreach ($data['bagStockDetails'] as $detail) {
|
|
$date = $detail['date'];
|
|
|
|
// Initialize the 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 [[bag1],[bag2],[bag3],[bag4],[bag5]] inner array
|
|
|
|
the above process is done for single row (date wise) and the same process is repeated for all the rows
|
|
*******************************************************************************************************************/
|
|
|
|
|
|
// Append the detail to the sub-array for this date
|
|
$groupedByDate[$date][] = $detail;
|
|
}
|
|
|
|
// Reset the structure to have indexed arrays without the date keys
|
|
$data['groupedBagStockDetails'] = array_values($groupedByDate);
|
|
|
|
|
|
|
|
|
|
// getting bag material for creating table headers
|
|
// checking already existing stock if yes then get the material code from the existing bag
|
|
// if not get all the material codes from the raw material details table
|
|
|
|
if (!empty($data['groupedBagStockDetails'])) {
|
|
|
|
$result = $groupedByDate[$date];
|
|
|
|
$distinctMaterialCodes = [];
|
|
foreach ($result as $index => $eachResult) {
|
|
$distinctMaterialCodes[] = $result[$index]['materialCode'];
|
|
}
|
|
$bagMaterials = $this->rawmaterialdetails_model->getSelectedBagMaterialCode($distinctMaterialCodes);
|
|
} else {
|
|
$bagMaterials = $this->rawmaterialdetails_model->getAllBagMaterialCode();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// getting customer for creating table headers
|
|
// checking already existing stock if yes then get customer from the existing stock
|
|
// if not set customer as '' empty string
|
|
|
|
foreach ($bagMaterials as $key => $bagMaterial) {
|
|
$materialCode = $bagMaterial['MaterialCode'];
|
|
$bagMaterials[$key]['customers'] = $this->bagStockDetails_model
|
|
->select('customer')
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->where('materialCode', $materialCode)
|
|
->first()['customer'] ?? '';
|
|
}
|
|
|
|
$bagMaterialcount = count($bagMaterials);
|
|
|
|
$data['bagMaterialcount'] = $bagMaterialcount;
|
|
$data['bagMaterials'] = $bagMaterials;
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
return $this->loadViews("bagStockDetails", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function updateBagStockDetails()
|
|
{
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$updateBagStockDetailsData = json_decode($postData['updateBagStockDetails'], true);
|
|
|
|
$inserts = [];
|
|
$updates = [];
|
|
|
|
foreach ($updateBagStockDetailsData as $data) {
|
|
|
|
$date = $data['date'];
|
|
$materialCode = $data['materialCode'];
|
|
|
|
|
|
$dbData = [
|
|
'date' => $data['date'],
|
|
'materialCode' => $data['materialCode'],
|
|
'customer' => $data['customer'],
|
|
'opening' => $data['opening'],
|
|
'receipt' => $data['receipt'],
|
|
'used' => $data['used'],
|
|
'balanceStock' => $data['balanceStock'],
|
|
];
|
|
|
|
$resultExists = $this->bagStockDetails_model
|
|
->where('date', $date)
|
|
->where('materialCode', $materialCode)
|
|
->first();
|
|
|
|
|
|
if (empty($resultExists)) {
|
|
$inserts[] = $dbData;
|
|
} else {
|
|
$dbData['id'] = $resultExists['id'];
|
|
$updates[] = $dbData;
|
|
}
|
|
}
|
|
|
|
if (!empty($inserts)) {
|
|
$inserted = $this->bagStockDetails_model->insertBatch($inserts);
|
|
}
|
|
if (!empty($updates)) {
|
|
|
|
$updateResult = $this->bagStockDetails_model->updateBatch($updates, 'id');
|
|
|
|
if ($updateResult === FALSE) {
|
|
echo "Error during update";
|
|
} else {
|
|
echo "Data Updated Successfully";
|
|
return;
|
|
}
|
|
}
|
|
|
|
echo "Data Saved Successfully";
|
|
}
|
|
|
|
|
|
//Dust And Rough Stock Details
|
|
public function loadView_dustAndRoughStockDetails()
|
|
{
|
|
|
|
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 = date('Y-m');
|
|
}
|
|
|
|
$data = [];
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = 'Dust And Rough Stock Details';
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
$data['dustAndRoughStockDetails'] = $this->dustAndRoughStockDetails_model
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->orderBy('date', 'asc')
|
|
->findAll();
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
return $this->loadViews("dustAndRoughStockDetails", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function updateDustAndRoughStockDetails()
|
|
{
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$updateDustAndRoughStockDetailsData = json_decode($postData['updateDustAndRoughStockDetails'], true);
|
|
|
|
$updates = [];
|
|
|
|
$inserts = [];
|
|
|
|
foreach ($updateDustAndRoughStockDetailsData as $data) {
|
|
|
|
$data['date'] = DateTime::createFromFormat('d-m-Y', $data['date'])->format('Y-m-d');
|
|
|
|
$date = $data['date'];
|
|
|
|
$dbData = [
|
|
'date' => $data['date'],
|
|
'finalRough' => $data['finalRough'],
|
|
'dust' => $data['dust'],
|
|
'total' => $data['total']
|
|
];
|
|
|
|
$resultExists = $this->dustAndRoughStockDetails_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
|
|
if (empty($resultExists)) {
|
|
$inserts[] = $dbData;
|
|
} else {
|
|
$updates[] = $dbData;
|
|
}
|
|
}
|
|
|
|
if (!empty($inserts)) {
|
|
$this->dustAndRoughStockDetails_model->insertBatch($inserts);
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
|
|
$updateResult = $this->dustAndRoughStockDetails_model->updateBatch($updates, 'date');
|
|
|
|
if ($updateResult === FALSE) {
|
|
echo "Error during update";
|
|
} else {
|
|
echo "Data Updated Successfully";
|
|
return;
|
|
}
|
|
}
|
|
|
|
echo "Data Saved Successfully";
|
|
}
|
|
|
|
|
|
//Resin Stock Details
|
|
public function loadView_resinStockDetails()
|
|
{
|
|
|
|
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 = date('Y-m');
|
|
}
|
|
|
|
$data = [];
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = 'Resin Stock Details';
|
|
|
|
|
|
// Get the previous month's last date resin 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['previousMonthResinStockDetails'] = $this->resinStockDetails_model
|
|
->select(['materialCode', 'date', 'balanceStock'])
|
|
->distinct()
|
|
->where('date =', $previousMonthEndDate)
|
|
->groupBy(['materialCode', 'date'])
|
|
->orderBy('date', 'desc')
|
|
->orderBy('materialCode', 'asc')
|
|
->findAll();
|
|
|
|
|
|
|
|
// checking in database if the data is available for the month
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
$data['resinStockDetails'] = $this->resinStockDetails_model
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->groupBy(['materialCode', 'date'])
|
|
->orderBy('date', 'asc')
|
|
->orderBy('materialCode', 'asc')
|
|
->findAll();
|
|
|
|
// Initialize an empty array to hold the grouped data
|
|
$data['groupedResinStockDetails'] = [];
|
|
|
|
// Temporary array to store grouped data by date
|
|
$groupedByDate = [];
|
|
|
|
// Group by 'date'
|
|
foreach ($data['resinStockDetails'] 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['groupedResinStockDetails'] = array_values($groupedByDate);
|
|
|
|
|
|
|
|
// getting resin material for creating table headers
|
|
// checking already existing stock if yes then get the material code from the existing stock
|
|
// if not get all the material codes from the raw material details table
|
|
|
|
if (!empty($data['groupedResinStockDetails'])) {
|
|
|
|
$result = $groupedByDate[$date];
|
|
|
|
$distinctMaterialCodes = [];
|
|
|
|
foreach ($result as $index => $eachResult) {
|
|
$distinctMaterialCodes[] = $result[$index]['materialCode'];
|
|
}
|
|
|
|
$resinMaterials = $this->rawmaterialdetails_model->getSelectedResinMaterialCode($distinctMaterialCodes);
|
|
|
|
} else {
|
|
|
|
$resinMaterials = $this->rawmaterialdetails_model->getAllResinMaterialCode();
|
|
|
|
}
|
|
|
|
|
|
// getting customer for creating table headers
|
|
// checking already existing stock if yes then get customer from the existing stock
|
|
// if not set customer in db , then set '' empty string
|
|
|
|
foreach ($resinMaterials as $key => $resinMaterial) {
|
|
$materialCode = $resinMaterial['MaterialCode'];
|
|
$resinMaterials[$key]['customers'] = $this->resinStockDetails_model
|
|
->select('customer')
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->where('materialCode', $materialCode)
|
|
->first()['customer'] ?? '';
|
|
}
|
|
|
|
$resinMaterialCount = count($resinMaterials);
|
|
|
|
$data['resinMaterialCount'] = $resinMaterialCount;
|
|
$data['resinMaterials'] = $resinMaterials;
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
return $this->loadViews("resinStockDetails", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function updateResinStockDetails()
|
|
{
|
|
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$updateResinStockDetailsData = json_decode($postData['updateResinStockDetails'], true);
|
|
|
|
$updates = [];
|
|
|
|
$inserts = [];
|
|
|
|
foreach ($updateResinStockDetailsData as $data) {
|
|
|
|
$date = $data['date'];
|
|
$materialCode = $data['materialCode'];
|
|
|
|
|
|
$dbData = [
|
|
'date' => $data['date'],
|
|
'materialCode' => $data['materialCode'],
|
|
'customer' => $data['customer'],
|
|
'opening' => $data['opening'],
|
|
'receipt' => $data['receipt'],
|
|
'used' => $data['used'],
|
|
'balanceStock' => $data['balanceStock'],
|
|
];
|
|
|
|
$resultExists = $this->resinStockDetails_model
|
|
->where('date', $date)
|
|
->where('materialCode', $materialCode)
|
|
->first();
|
|
|
|
|
|
if (empty($resultExists)) {
|
|
$inserts[] = $dbData;
|
|
} else {
|
|
$dbData['id'] = $resultExists['id'];
|
|
$updates[] = $dbData;
|
|
}
|
|
}
|
|
|
|
|
|
if (!empty($inserts)) {
|
|
$this->resinStockDetails_model->insertBatch($inserts);
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
|
|
$updateResult = $this->resinStockDetails_model->updateBatch($updates, 'id');
|
|
|
|
if ($updateResult === FALSE) {
|
|
echo "Error during update";
|
|
} else {
|
|
echo "Data Updated Successfully";
|
|
return;
|
|
}
|
|
}
|
|
|
|
echo "Data Saved Successfully";
|
|
}
|
|
|
|
|
|
//Gas Stock Details
|
|
public function loadView_gasStockDetails()
|
|
{
|
|
|
|
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 = date('Y-m');
|
|
}
|
|
|
|
$data = [];
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = "Gas Stock Details";
|
|
|
|
|
|
$previousMonth = DateTime::createFromFormat('Y-m', $month)->modify('-1 month')->format('Y-m');
|
|
$previousMonthStartDate = "$previousMonth-01";
|
|
$previousMonthEndDate = date("Y-m-t", strtotime($previousMonthStartDate));
|
|
$data['previousMonthGasStockDetails'] = $this->gasStockDetails_model
|
|
->select('date,balanceStock')
|
|
->where('date =', $previousMonthEndDate)
|
|
->orderBy('date', 'desc')
|
|
->findAll();
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
|
|
$data['gasStockDetails'] = $this->gasStockDetails_model
|
|
->select(
|
|
't_gasStockDetails.*,
|
|
drier_summary.drierMachineGasconsumption AS drierMachineGasConsumption,
|
|
drier_summary.driedSand AS sandDried,
|
|
coating_summary.coatingMachineGasconsumption,
|
|
coating_summary.coatedSand'
|
|
)
|
|
// Subquery with early date filtering
|
|
->join(
|
|
'(SELECT date,
|
|
SUM(totalGasconsumption) AS coatingMachineGasconsumption,
|
|
SUM(totalCoatingSandInKg) / 1000 AS coatedSand
|
|
FROM t_coatingMachineDetails
|
|
WHERE is_active = 1
|
|
AND date >= ' . $this->db->escape($startDate) . '
|
|
AND date <= ' . $this->db->escape($endDate) . '
|
|
GROUP BY date
|
|
) AS coating_summary',
|
|
'coating_summary.date = t_gasStockDetails.date',
|
|
'left'
|
|
)
|
|
->join(
|
|
'(SELECT date,
|
|
SUM(total_gas_consumption) AS drierMachineGasconsumption,
|
|
SUM(sand_dried_qty) AS driedSand
|
|
FROM t_drierMachineDetails
|
|
WHERE date >= ' . $this->db->escape($startDate) . '
|
|
AND date <= ' . $this->db->escape($endDate) . '
|
|
GROUP BY date
|
|
) AS drier_summary',
|
|
'drier_summary.date = t_gasStockDetails.date',
|
|
'left'
|
|
)
|
|
|
|
->where('t_gasStockDetails.date >=', $startDate)
|
|
->where('t_gasStockDetails.date <=', $endDate)
|
|
->groupBy('t_gasStockDetails.date')
|
|
->findAll();
|
|
|
|
|
|
|
|
$data['month'] = DateTime::createFromFormat('Y-m', $month)->format('M-Y');
|
|
|
|
return $this->loadViews("gasStockDetails", $this->global, $data, NULL);
|
|
}
|
|
|
|
public function updateGasStockDetails()
|
|
{
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$updateGasStockDetailsData = json_decode($postData['updateGasStockDetails'], true);
|
|
|
|
$updates = [];
|
|
|
|
$inserts = [];
|
|
|
|
foreach ($updateGasStockDetailsData as $data) {
|
|
|
|
$date = $data['date'];
|
|
|
|
|
|
$dbData = [
|
|
'date' => $data['date'],
|
|
'opening' => $data['opening'],
|
|
'purchaseBharath' => $data['purchaseBharath'],
|
|
'purchaseIndian' => $data['purchaseIndian'],
|
|
'total' => $data['total'],
|
|
'consumption' => $data['consumption'],
|
|
'balanceStock' => $data['balanceStock'],
|
|
];
|
|
|
|
$resultExists = $this->gasStockDetails_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
|
|
if (empty($resultExists)) {
|
|
$inserts[] = $dbData;
|
|
} else {
|
|
$dbData['id'] = $resultExists['id'];
|
|
$updates[] = $dbData;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if (!empty($inserts)) {
|
|
$this->gasStockDetails_model->insertBatch($inserts);
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
|
|
$updateResult = $this->gasStockDetails_model->updateBatch($updates, 'id');
|
|
|
|
if ($updateResult === FALSE) {
|
|
echo "Error during update";
|
|
} else {
|
|
echo "Data Updated Successfully";
|
|
return;
|
|
}
|
|
}
|
|
|
|
echo "Data Saved Successfully Warning..!! Kindly Update the next subsequent month Gas Stock If older month Gas Stock Updated..!!";
|
|
}
|
|
|
|
private function updateGasStockConsumption($date, $consumption)
|
|
{
|
|
|
|
$resultExists = $this->gasStockDetails_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
if ($resultExists) {
|
|
|
|
|
|
if ($date instanceof DateTime) {
|
|
$date = $date->format('Y-m-d');
|
|
} elseif (!strtotime($date)) {
|
|
throw new \Exception("Invalid date format provided.");
|
|
} else {
|
|
$date = date('Y-m-d', strtotime($date));
|
|
}
|
|
|
|
$endDate = date('Y-m-t', strtotime($date));
|
|
|
|
|
|
//what happens if the updation is on the last day of the month?
|
|
$resultExists = $this->gasStockDetails_model
|
|
->where('date >=', $date)
|
|
->where('date <=', $endDate)
|
|
->orderBy('date', 'asc')
|
|
->findAll();
|
|
|
|
$updatedOpeningStock = 0;
|
|
$updates = [];
|
|
|
|
|
|
foreach ($resultExists as $index => $result) {
|
|
|
|
if ($index == 0) {
|
|
$result['consumption'] = $result['consumption'] + ($consumption);
|
|
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'];
|
|
$result['balanceStock'] = $result['total'] - $result['consumption'];
|
|
$updatedOpeningStock = $result['balanceStock'];
|
|
} else {
|
|
$result['opening'] = $updatedOpeningStock;
|
|
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'];
|
|
$result['balanceStock'] = $result['total'] - $result['consumption'];
|
|
$updatedOpeningStock = $result['balanceStock'];
|
|
}
|
|
|
|
|
|
$updates[] = $result;
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
$result = $this->gasStockDetails_model->updateBatch($updates, 'id');
|
|
return ($result !== false && $result > 0) ? $result : 0;
|
|
} else {
|
|
return 0;
|
|
}
|
|
} else {
|
|
|
|
|
|
|
|
$previousMonthDate = new DateTime($date);
|
|
$previousMonthDate = $previousMonthDate->modify('first day of this month')
|
|
->modify('-1 day')
|
|
->format('Y-m-d');
|
|
|
|
|
|
|
|
$previousMonthResultExists = $this->gasStockDetails_model
|
|
->where('date', $previousMonthDate)
|
|
->first();
|
|
|
|
|
|
|
|
$opening = 0;
|
|
|
|
if (!empty($previousMonthResultExists) && isset($previousMonthResultExists['balanceStock'])) {
|
|
$opening = $previousMonthResultExists['balanceStock'];
|
|
}
|
|
|
|
|
|
|
|
$startDate = DateTime::createFromFormat('Y-m-d', $date)->modify('first day of this month')->format('Y-m-d');
|
|
$endDate = DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month')->format('Y-m-d');
|
|
|
|
$datesInMonth = [];
|
|
$inserts = [];
|
|
|
|
$period = new DatePeriod(
|
|
new DateTime($startDate),
|
|
new DateInterval('P1D'),
|
|
(new DateTime($endDate))->modify('+1 day')
|
|
);
|
|
|
|
foreach ($period as $day) {
|
|
$datesInMonth[] = $day->format('Y-m-d');
|
|
}
|
|
|
|
foreach ($datesInMonth as $datesInMonthIndex => $dateInMonth) {
|
|
|
|
//update previousMonthResultExists
|
|
$balanceStock = $opening;
|
|
$consumptionForDate = 0;
|
|
if ($dateInMonth == $date) {
|
|
$consumptionForDate = $consumption;
|
|
$balanceStock = $opening - $consumption;
|
|
}
|
|
|
|
$inserts[] = [
|
|
'date' => $dateInMonth,
|
|
'opening' => $opening,
|
|
'purchaseBharath' => 0,
|
|
'purchaseIndian' => 0,
|
|
'total' => 0,
|
|
'consumption' => $consumptionForDate,
|
|
'balanceStock' => $balanceStock
|
|
];
|
|
|
|
$opening = $balanceStock;
|
|
}
|
|
|
|
if (!empty($inserts)) {
|
|
$result = $this->gasStockDetails_model->insertBatch($inserts);
|
|
return ($result !== false && $result > 0) ? $result : 0;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function trpSandUseStockDetails(){
|
|
|
|
|
|
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 = date('Y-m');
|
|
}
|
|
|
|
$data = [];
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = 'Trp Sand Use Stock Details';
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
$data['trpSandUseStockDetails'] = $this->TrpSandUseStock_model
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->orderBy('date', 'asc')
|
|
->findAll();
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
return $this->loadViews("trpSandUseStockDetails", $this->global, $data, NULL);
|
|
|
|
|
|
}
|
|
|
|
public function updateTrpSandUseStockDetails(){
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
$updateTrpSandUseStockDetailsData = json_decode($postData['updateTrpSandUseStockDetails'], true);
|
|
|
|
$updates = [];
|
|
|
|
$inserts = [];
|
|
|
|
foreach ($updateTrpSandUseStockDetailsData as $data) {
|
|
|
|
$data['date'] = DateTime::createFromFormat('d-m-Y', $data['date'])->format('Y-m-d');
|
|
|
|
$date = $data['date'];
|
|
|
|
$dbData = [
|
|
'date' => $data['date'],
|
|
'rough_kgs' => $data['rough_kgs'],
|
|
'fine_kgs' => $data['fine_kgs'],
|
|
'total_kgs' => $data['total_kgs'],
|
|
'customer_name' => $data['customer_name']
|
|
];
|
|
|
|
$resultExists = $this->TrpSandUseStock_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
|
|
if (empty($resultExists)) {
|
|
$inserts[] = $dbData;
|
|
} else {
|
|
$dbData['id'] = $resultExists['id'];
|
|
$updates[] = $dbData;
|
|
}
|
|
}
|
|
|
|
if (!empty($inserts)) {
|
|
$this->TrpSandUseStock_model->insertBatch($inserts);
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
|
|
$updateResult = $this->TrpSandUseStock_model->updateBatch($updates, 'id');
|
|
|
|
if ($updateResult === FALSE) {
|
|
echo "Error during update";
|
|
} else {
|
|
echo "Data Updated Successfully";
|
|
return;
|
|
}
|
|
}
|
|
|
|
echo "Data Saved Successfully";
|
|
}
|
|
|
|
|
|
|
|
public function trpProductionDetails(){
|
|
|
|
|
|
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 = new DateTime('2024-10-01');
|
|
// $month = $month->format('Y-m');
|
|
|
|
$month = date('Y-m');
|
|
|
|
}
|
|
|
|
$data = [];
|
|
|
|
$data['month'] = $month;
|
|
|
|
$this->global['pageTitle'] = 'Trp production Details';
|
|
|
|
$startDate = "$month-01";
|
|
$endDate = date("Y-m-t", strtotime($startDate));
|
|
|
|
$trpProductionMaintenanceData = $this->TrpProductionMaintenance_model
|
|
->where('date >=', $startDate)
|
|
->where('date <=', $endDate)
|
|
->find();
|
|
|
|
if(empty($trpProductionMaintenanceData)){
|
|
|
|
$inserts = [];
|
|
|
|
$inserts = $this->generateDummyData($startDate, $endDate);
|
|
|
|
$data['trpProductionMaintenanceData'] = $this->updateTrpProductionDetails($inserts);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['trpProductionDetails'] = $this->TrpProduction_model->trpProductionDetails($startDate,$endDate);
|
|
|
|
|
|
// dd($data['trpProductionDetails']);
|
|
|
|
// dd($this->db->getLastQuery());
|
|
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $month);
|
|
|
|
$formattedDate = $date->format('M-Y');
|
|
|
|
$data['month'] = $formattedDate;
|
|
|
|
|
|
return $this->loadViews("trpProductionStockDetails", $this->global, $data, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function updateTrpProductionDetails($inserts) {
|
|
|
|
$postData = $this->request->getPost();
|
|
|
|
if(empty($postData['updateTrpProductionDetails'])){
|
|
if(empty($inserts)){
|
|
return $this->response->setJSON(['error' => 'No data found to update']);
|
|
}else{
|
|
$postData['updateTrpProductionDetails'] = $inserts;
|
|
}
|
|
}
|
|
|
|
|
|
$updateTrpProductionDetails = $postData['updateTrpProductionDetails'];
|
|
|
|
$updateProduction = [];
|
|
$updateMaintanence = [];
|
|
$updateWaste = [];
|
|
|
|
$insertProduction = [];
|
|
$insertMaintanence = [];
|
|
$insertWaste = [];
|
|
|
|
foreach ($updateTrpProductionDetails as $data) {
|
|
|
|
$data['date'] = $this->convertToDateWithFlexibleFormats($data['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d");
|
|
|
|
$date = $data['date'];
|
|
|
|
$dbDataTrpMaintenance = [
|
|
'date' => $data['date'],
|
|
'openingTime' => $data['openingTime'],
|
|
'closingTime' => $data['closingTime'],
|
|
'totalHours' => $data['totalHours'],
|
|
'coldStart' => $data['coldStart'],
|
|
'breakdownHours' => $data['breakdownHours'],
|
|
'burnerFiringHours' => $data['burnerFiringHours'],
|
|
'sandFeedingHours' => $data['sandFeedingHours'],
|
|
'workingPersonEngineers' => $data['workingPersonEngineers'],
|
|
'workingPersonSupervisiors' => $data['workingPersonSupervisiors'],
|
|
'workingPersonOperators' => $data['workingPersonOperators'],
|
|
'rollerDrivers' => $data['rollerDrivers'],
|
|
'natureOfMaintenance' => $data['natureOfMaintenance'],
|
|
'location' => $data['location'],
|
|
'description' => $data['description']
|
|
];
|
|
|
|
$dbDataTrpProduction = [
|
|
'date' => $data['date'],
|
|
'gasReceiptBg' => $data['gasReceiptBg'],
|
|
'gasReceiptPg' => $data['gasReceiptPg'],
|
|
'physicalGasConsumption'=> $data['physicalGasConsumption'],
|
|
'trpPlusDrierGasConsumption'=> $data['trpPlusDrierGasConsumption'],
|
|
'trpPhysicalGasPerTon' => $data['trpPhysicalGasPerTon'],
|
|
'panelGasConsumption' => $data['panelGasConsumption'],
|
|
'panelGasPerTon' => $data['panelGasPerTon'],
|
|
'edRunningHours' => $data['edRunningHours'],
|
|
'edProduction' => $data['edProduction'],
|
|
'edUsage' => $data['edUsage'],
|
|
'trpProduction' => $data['trpProduction'],
|
|
'trpProductionPerHour' => $data['trpProductionPerHour'],
|
|
'waterReceipt' => $data['waterReceipt'],
|
|
'waterConsumption' => $data['waterConsumption'],
|
|
'ebReading' => $data['ebReading'],
|
|
'ebReadingPerUnitTon' => $data['ebReadingPerUnitTon'],
|
|
];
|
|
|
|
$dbDataTrpWaste = [
|
|
'date' => $data['date'],
|
|
'msSeperation' => $data['msSeperation'],
|
|
'edWaste' => $data['edWaste'],
|
|
'coolerBags' => $data['coolerBags'],
|
|
'edPlus20Waste' => $data['edPlus20Waste'],
|
|
'cycloneWaste' => $data['cycloneWaste'],
|
|
];
|
|
|
|
$trpMaintenanceResultExists = $this->TrpProductionMaintenance_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
$trpProductionResultExists = $this->TrpProduction_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
$trpWasteResultExists = $this->TrpProductionWaste_model
|
|
->where('date', $date)
|
|
->first();
|
|
|
|
if (empty($trpMaintenanceResultExists)) {
|
|
$insertMaintanence[] = $dbDataTrpMaintenance;
|
|
$insertProduction[] = $dbDataTrpProduction;
|
|
$insertWaste[] = $dbDataTrpWaste;
|
|
} else {
|
|
$dbDataTrpMaintenance['id'] = $trpMaintenanceResultExists['id'];
|
|
$dbDataTrpProduction['id'] = $trpProductionResultExists['id'];
|
|
$dbDataTrpWaste['id'] = $trpWasteResultExists['id'];
|
|
|
|
$updateMaintanence[] = $dbDataTrpMaintenance;
|
|
$updateProduction[] = $dbDataTrpProduction;
|
|
$updateWaste[] = $dbDataTrpWaste;
|
|
}
|
|
}
|
|
|
|
// Start the transaction
|
|
$this->db->transBegin();
|
|
|
|
try {
|
|
if (!empty($insertMaintanence)) {
|
|
$this->TrpProductionMaintenance_model->insertBatch($insertMaintanence);
|
|
}
|
|
|
|
if (!empty($insertProduction)) {
|
|
$this->TrpProduction_model->insertBatch($insertProduction);
|
|
}
|
|
|
|
if (!empty($insertWaste)) {
|
|
$this->TrpProductionWaste_model->insertBatch($insertWaste);
|
|
}
|
|
|
|
if (!empty($updateMaintanence)) {
|
|
$this->TrpProductionMaintenance_model->updateBatch($updateMaintanence, 'id');
|
|
}
|
|
|
|
if (!empty($updateProduction)) {
|
|
$this->TrpProduction_model->updateBatch($updateProduction, 'id');
|
|
}
|
|
|
|
if (!empty($updateWaste)) {
|
|
$this->TrpProductionWaste_model->updateBatch($updateWaste, '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();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function generateDummyData($startDate, $endDate)
|
|
{
|
|
$datesInMonth = [];
|
|
$inserts = [];
|
|
|
|
$period = new DatePeriod(
|
|
new DateTime($startDate),
|
|
new DateInterval('P1D'),
|
|
(new DateTime($endDate))->modify('+1 day')
|
|
);
|
|
|
|
foreach ($period as $day) {
|
|
$datesInMonth[] = $day->format('Y-m-d');
|
|
}
|
|
|
|
foreach ($datesInMonth as $datesInMonthIndex => $dateInMonth) {
|
|
|
|
$inserts[] = [
|
|
'Date' => $dateInMonth,
|
|
'openingTime' => '00:00',
|
|
'closingTime' => '00:00',
|
|
'totalHours' => '00:00',
|
|
'coldStart' => '00:00',
|
|
'breakdownHours' => '00:00',
|
|
'burnerFiringHours' => '00:00',
|
|
'sandFeedingHours' => '00:00',
|
|
'workingPersonEngineers' => 0,
|
|
'workingPersonSupervisiors' => 0,
|
|
'workingPersonOperators' => 0,
|
|
'rollerDrivers' => 0,
|
|
'natureOfMaintenance' => '',
|
|
'location' => '',
|
|
'description' => '',
|
|
'gasReceiptBg' => 0,
|
|
'gasReceiptPg' => 0,
|
|
'physicalGasConsumption' => 0,
|
|
'trpPlusDrierGasConsumption' => 0,
|
|
'trpPhysicalGasPerTon' => 0,
|
|
'panelGasConsumption' => 0,
|
|
'panelGasPerTon' => 0,
|
|
'edRunningHours' => 0,
|
|
'edProduction' => 0,
|
|
'edUsage' => 0,
|
|
'trpProduction' => 0,
|
|
'trpProductionPerHour' => 0,
|
|
'waterReceipt' => 0,
|
|
'waterConsumption' => 0,
|
|
'ebReading' => 0,
|
|
'ebReadingPerUnitTon' => 0,
|
|
'msSeperation' => 0,
|
|
'edWaste' => 0,
|
|
'coolerBags' => 0,
|
|
'edPlus20Waste' => 0,
|
|
'cycloneWaste' => 0
|
|
];
|
|
}
|
|
|
|
return $inserts;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------gwm----------------------------------------------
|
|
|
|
|
|
public function getMonthDatesFromInput($monthYear)
|
|
{
|
|
|
|
$date = DateTime::createFromFormat('M-Y', $monthYear);
|
|
|
|
if (!$date) {
|
|
return "Invalid date format. Use 'M-yyyy' format.";
|
|
}
|
|
|
|
$year = $date->format('Y');
|
|
$month = $date->format('m');
|
|
|
|
$totalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
|
|
|
|
$dates = [];
|
|
for ($day = 1; $day <= $totalDays; $day++) {
|
|
|
|
$currentDate = DateTime::createFromFormat('Y-m-d', "$year-$month-$day");
|
|
|
|
$dates[] = [
|
|
'readable' => $currentDate->format('d-m-Y'),
|
|
'database' => $currentDate->format('Y-m-d')
|
|
];
|
|
}
|
|
|
|
return $dates;
|
|
}
|
|
|
|
public function generateTimeOptions()
|
|
{
|
|
$times = [];
|
|
$start = strtotime('00:00'); // Start at midnight (12:00 AM)
|
|
$end = strtotime('23:45'); // End at 11:45 PM
|
|
|
|
while ($start <= $end) {
|
|
$timeValue = date('H:i', $start); // 24-hour format for value
|
|
$timeLabel = date('h:i A', $start); // 12-hour format with AM/PM for display
|
|
$times[$timeValue] = $timeLabel; // Store time in associative array
|
|
$start = strtotime('+15 minutes', $start); // Increment by 15 minutes
|
|
}
|
|
|
|
return $times;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function drierMachineDetails()
|
|
{
|
|
|
|
|
|
$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['timeDropdown'] = $this->generateTimeOptions();
|
|
$data['supplierData'] = $this->supplier_model->supplierlisting();
|
|
|
|
$data['drierDetails'] = $this->drierMachineDetails_model
|
|
->where('date >=', $data['startDate'])
|
|
->where('date <=', $data['endDate'])
|
|
->orderBy('date','asc')
|
|
->findAll();
|
|
|
|
|
|
$data['datefordropdown'] = $currentMonth;
|
|
|
|
$this->global['pageTitle'] = 'Drier Details';
|
|
$this->loadViews("drierMachineDetails", $this->global, $data, NULL);
|
|
|
|
|
|
}
|
|
|
|
public function addOrEditdrierMachineDetails()
|
|
{
|
|
$drierData = [];
|
|
$drierData = json_decode($this->request->getPost('drierData'), true);
|
|
|
|
|
|
if (empty($drierData)) {
|
|
|
|
$additionalEntry = $this->request->getPost();
|
|
|
|
if (!empty($additionalEntry)) {
|
|
$drierData[] = [
|
|
'Date' => $this->convertToDateWithFlexibleFormats($additionalEntry['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"),
|
|
'Drier On Time' => $additionalEntry['Drier_On_Time'],
|
|
'Drier Off Time' => $additionalEntry['Drier_Off_Time'],
|
|
'Running Hrs' => $additionalEntry['Running_Hrs'],
|
|
'Supplier Name' => $additionalEntry['Supplier_Name'],
|
|
'i/p Sand Moisture(%)' => $additionalEntry['i/p_Sand_Moisture(%)'],
|
|
'Tot Gas Consumption' => $additionalEntry['Tot_Gas_Consumption'],
|
|
'Gas Per Ton' => $additionalEntry['Gas_Per_Ton'],
|
|
'i/p Sand Qty' => $additionalEntry['i/p_Sand_Qty'],
|
|
'Moisture Loss Qty' => $additionalEntry['Moisture_Loss_Qty'],
|
|
'Sand Dried Qty' => $additionalEntry['Sand_Dried_Qty'],
|
|
'Qty Per Hrs' => $additionalEntry['Qty_Per_Hrs'],
|
|
'Dust Qty' => $additionalEntry['Dust_Qty'],
|
|
'Customer Name' => $additionalEntry['Customer_Name']
|
|
];
|
|
} else {
|
|
|
|
echo "No data received.";
|
|
return;
|
|
}
|
|
}
|
|
|
|
$message1 = '';
|
|
|
|
$this->db->transBegin();
|
|
|
|
try {
|
|
foreach ($drierData as $row) {
|
|
$data = [
|
|
'date' => $this->convertToDateWithFlexibleFormats($row['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"),
|
|
'drier_on_time' => $row['Drier On Time'],
|
|
'drier_off_time' => $row['Drier Off Time'],
|
|
'drier_running_hours' => $row['Running Hrs'],
|
|
'supplier_name' => $row['Supplier Name'],
|
|
'input_sand_moisture' => $row['i/p Sand Moisture(%)'],
|
|
'total_gas_consumption' => $row['Tot Gas Consumption'],
|
|
'gas_per_ton' => $row['Gas Per Ton'],
|
|
'input_sand_qty' => $row['i/p Sand Qty'],
|
|
'moisture_loss_qty' => $row['Moisture Loss Qty'],
|
|
'sand_dried_qty' => $row['Sand Dried Qty'],
|
|
'qty_per_hours' => $row['Qty Per Hrs'],
|
|
'dust_qty' => $row['Dust Qty'],
|
|
'customer_name' => $row['Customer Name']
|
|
];
|
|
|
|
// Check if a row with the same date already exists
|
|
$existingRow = $this->drierMachineDetails_model->where('id', $row['id'] ?? '')->first();
|
|
|
|
$inserts = [];
|
|
$updates = [];
|
|
|
|
if ($existingRow) {
|
|
// If exists, update the row
|
|
|
|
//before updating the row update gas stock
|
|
$existingDrierGasConsumption = $existingRow['total_gas_consumption'];
|
|
$updatedDrierGasConsumption = $row['Tot Gas Consumption'];
|
|
|
|
if ($existingDrierGasConsumption != $updatedDrierGasConsumption) {
|
|
|
|
$changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption;
|
|
$dateToBeUpdated = $existingRow['date'];
|
|
$this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
|
|
}
|
|
$data['id'] = $existingRow['id'];
|
|
$updates[] = $data;
|
|
} else {
|
|
// If not, insert a new row
|
|
//before inserting the row update gas stock
|
|
$consumption = $row['Tot Gas Consumption'];
|
|
$dateToBeInserted = $data['date'];
|
|
$this->updateGasStockConsumption($dateToBeInserted, $consumption);
|
|
|
|
$inserts[] = $data;
|
|
}
|
|
|
|
if (!empty($inserts)) {
|
|
$this->drierMachineDetails_model->insertBatch($inserts);
|
|
|
|
$message1 = "Warning..!! Kindly Update the next subsequent month Gas Stock If older month Gas Stock Updated..!!";
|
|
}
|
|
|
|
if (!empty($updates)) {
|
|
$this->drierMachineDetails_model->updateBatch($updates, 'id');
|
|
|
|
$message1 = "Warning..!! Kindly Update the next subsequent month Gas Stock If older month Gas Stock Updated..!!";
|
|
}
|
|
|
|
if ($this->db->transStatus() === false) {
|
|
throw new \Exception('Transaction failed due to database error.');
|
|
}
|
|
}
|
|
|
|
$this->db->transCommit();
|
|
|
|
if (!empty($additionalEntry)) {
|
|
// Data to be sent via POST
|
|
$postData = [
|
|
'month' => $this->convertToDateWithFlexibleFormats($additionalEntry['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "M-Y"),
|
|
|
|
];
|
|
|
|
echo '<form id="postForm" action="' . base_url('drierMachineDetails') . '" method="POST">';
|
|
foreach ($postData as $key => $value) {
|
|
echo '<input type="hidden" name="' . $key . '" value="' . $value . '">';
|
|
}
|
|
echo '</form>';
|
|
echo '<script type="text/javascript">
|
|
document.getElementById("postForm").submit();
|
|
</script>';
|
|
exit;
|
|
}
|
|
|
|
echo "Data saved successfully . $message1";
|
|
|
|
} catch (\Exception $e) {
|
|
// Rollback the transaction on any failure
|
|
$this->db->transRollback();
|
|
echo "Failed to save/Update data: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
|
|
function convertToDateWithFlexibleFormats($dateInput, array $inputFormats, string $outputFormat): ?string
|
|
{
|
|
// If it's already a DateTime object, just format it
|
|
if ($dateInput instanceof DateTime) {
|
|
return $dateInput->format($outputFormat);
|
|
}
|
|
|
|
// If it's a string, attempt to parse it
|
|
if (is_string($dateInput)) {
|
|
foreach ($inputFormats as $format) {
|
|
$date = DateTime::createFromFormat($format, $dateInput);
|
|
if ($date) {
|
|
return $date->format($outputFormat);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $dateInput;
|
|
}
|
|
|
|
|
|
|
|
public function factoryVehicleDieselDetails()
|
|
{
|
|
$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', 'diesel')->where('is_active', 1)->findAll();
|
|
if (!count([$data['machineDetails']])) {
|
|
return $this->response->setBody("<script>alert('No Machine data available');</script>");
|
|
}
|
|
|
|
//Diesel Stock Summary Details
|
|
$data['summaryDetails'] = $this->factoryVehicleDieselStockSummary_model->select('date,opening_stock as opening_stock_0,purchase_diesel as purchase_diesel_0,total_filling_diesel as total_filling_diesel_0,balance_stock as balance_stock_0')
|
|
->where('date >=', $data['startDate'])
|
|
->where('date <=', $data['endDate'])
|
|
->findAll();
|
|
|
|
if (!count($data['summaryDetails'])) {
|
|
|
|
//before insert check previous month data present?
|
|
|
|
if (isset($data['startDate']) && !empty($data['startDate'])) {
|
|
// Extract the year and month part from the startDate
|
|
$startDate = substr($data['startDate'], 0, 7); // Get 'Y-m' part
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $startDate);
|
|
|
|
if ($date) {
|
|
$previousMonth = $date->modify('-1 month')->format('Y-m');
|
|
$previousMonthStartDate = "$previousMonth-01";
|
|
$previousMonthEndDate = date("Y-m-t", strtotime($previousMonthStartDate));
|
|
}
|
|
}
|
|
|
|
// Now you can use $previousMonth, $previousMonthStartDate, and $previousMonthEndDate
|
|
|
|
$previousMonthData = $this->factoryVehicleDieselStockSummary_model
|
|
->where('date >=', $previousMonthStartDate)
|
|
->where('date <=', $previousMonthEndDate)
|
|
->orderBy('date', 'desc')
|
|
->first();
|
|
|
|
if (!empty($previousMonthData)) {
|
|
//insert empty data
|
|
$tempInsert = [];
|
|
foreach ($monthData as $key => $value) {
|
|
if($value['database'] == $previousMonthData['date']){ //add +1day to previous month date
|
|
|
|
array_push($tempInsert, [
|
|
'date' => $value['database'],
|
|
'opening_stock' => $previousMonthData['balance_stock'],
|
|
'purchase_diesel' => 0.00,
|
|
'total_filling_diesel' => 0.00,
|
|
'balance_stock' => $previousMonthData['balance_stock']]);
|
|
}else{
|
|
array_push($tempInsert, ['date' => $value['database']]);
|
|
}
|
|
}
|
|
|
|
$this->factoryVehicleDieselStockSummary_model->insertBatch($tempInsert);
|
|
|
|
$data['summaryDetails'] = $this->factoryVehicleDieselStockSummary_model->where('date >=', $data['startDate'])->where('date <=', $data['endDate'])->findAll();
|
|
|
|
|
|
|
|
}else{
|
|
//insert empty data
|
|
$tempInsert = [];
|
|
foreach ($monthData as $key => $value) {
|
|
array_push($tempInsert, ['date' => $value['database']]);
|
|
}
|
|
$this->factoryVehicleDieselStockSummary_model->insertBatch($tempInsert);
|
|
|
|
$data['summaryDetails'] = $this->factoryVehicleDieselStockSummary_model->where('date >=', $data['startDate'])->where('date <=', $data['endDate'])->findAll();
|
|
}
|
|
|
|
}
|
|
|
|
//Diesel Consumption Details
|
|
$data['dieselConsumptionDetails'] = $this->getDieselConsumptionPivot($data['machineDetails'],$data['startDate'], $data['endDate']);
|
|
|
|
if (!count($data['dieselConsumptionDetails'])) {
|
|
|
|
//before insert check previous month data present?
|
|
|
|
if (isset($data['startDate']) && !empty($data['startDate'])) {
|
|
// Extract the year and month part from the startDate
|
|
$startDate = substr($data['startDate'], 0, 7); // Get 'Y-m' part
|
|
|
|
$date = DateTime::createFromFormat('Y-m', $startDate);
|
|
|
|
if ($date) {
|
|
$previousMonth = $date->modify('-1 month')->format('Y-m');
|
|
$previousMonthStartDate = "$previousMonth-01";
|
|
$previousMonthEndDate = date("Y-m-t", strtotime($previousMonthStartDate));
|
|
}
|
|
}
|
|
|
|
$previousMonthData = $this->factoryVehicleDieselDetails_model
|
|
->where('date >=', $previousMonthStartDate)
|
|
->where('date <=', $previousMonthEndDate)
|
|
->orderBy('date', 'desc')
|
|
->first();
|
|
|
|
|
|
|
|
if (!empty($previousMonthData)) {
|
|
//insert empty data
|
|
$tempInsert = [];
|
|
foreach ($monthData as $key => $value) {
|
|
|
|
foreach($data['machineDetails'] as $machine){
|
|
|
|
|
|
$previousMonthDataFetched = $this->factoryVehicleDieselDetails_model
|
|
->where('date >=', $previousMonthStartDate)
|
|
->where('date <=', $previousMonthEndDate)
|
|
->where('machine_id', $machine['id'])
|
|
->orderBy('date', 'desc')
|
|
->first();
|
|
|
|
|
|
if($value['database'] == $previousMonthData['date']){ //add +1day to previous month date
|
|
|
|
array_push($tempInsert, [
|
|
'date' => $value['database'],
|
|
'machine_id' => $machine['id'],
|
|
'opening_reading' => $previousMonthDataFetched['closing_reading'],
|
|
'closing_reading' => $previousMonthDataFetched['closing_reading'],
|
|
'filling_diesel' => 0.00,
|
|
'consumption' => 0.00,
|
|
'running_hours' => 0.00,
|
|
'mileage' => 0.00]);
|
|
|
|
|
|
}else{
|
|
array_push($tempInsert, [
|
|
'date' => $value['database'],
|
|
'machine_id' => $machine['id'],
|
|
'opening_reading' => 0.00,
|
|
'closing_reading' => 0.00,
|
|
'filling_diesel' => 0.00,
|
|
'consumption' => 0.00,
|
|
'running_hours' => 0.00,
|
|
'mileage' => 0.00]);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}else{
|
|
$tempInsert = [];
|
|
foreach ($monthData as $key => $value) {
|
|
|
|
foreach($data['machineDetails'] as $machine){
|
|
array_push( $tempInsert,
|
|
['date' => $value['database'],
|
|
'machine_id' => $machine['id'],
|
|
'opening_reading' => 0.00,
|
|
'closing_reading' => 0.00,
|
|
'filling_diesel' => 0.00,
|
|
'consumption' => 0.00,
|
|
'running_hours' => 0.00,
|
|
'mileage' => 0.00]);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
$this->factoryVehicleDieselDetails_model->insertBatch($tempInsert);
|
|
|
|
|
|
$data['dieselConsumptionDetails'] = $this->getDieselConsumptionPivot($data['machineDetails'], $data['startDate'], $data['endDate']);
|
|
|
|
}
|
|
|
|
// Merge the two arrays
|
|
$mergedArray = [];
|
|
|
|
// Loop through the first array
|
|
foreach ($data['summaryDetails'] as $item1) {
|
|
$date = $item1["date"];
|
|
|
|
// Find the matching item in the second array by date
|
|
foreach ($data['dieselConsumptionDetails'] as $item2) {
|
|
if ($item2["date"] === $date) {
|
|
// Merge the two arrays
|
|
$mergedArray[] = array_merge($item1, $item2);
|
|
break; // Stop once the matching date is found
|
|
}
|
|
}
|
|
}
|
|
$data['dieselConsumptionDetails'] = $mergedArray;
|
|
|
|
$data['machineName'] = array_column($data['machineDetails'], 'machine_name');
|
|
|
|
$this->global['pageTitle'] = 'Drier Details';
|
|
|
|
$this->loadViews("factoryDieselMachineDetails", $this->global, $data, NULL);
|
|
|
|
}
|
|
|
|
public function getDieselConsumptionPivot($machineDetails, $startDate, $endDeate)
|
|
{
|
|
$machineIds = array_column($machineDetails, 'id');
|
|
|
|
if (empty($machineIds)) {
|
|
return [];
|
|
}
|
|
|
|
$fields = ['opening_reading', 'closing_reading', 'filling_diesel', 'consumption', 'running_hours', 'mileage'];
|
|
|
|
$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_factoryVehicleDieselDetails`
|
|
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 addOrEditfactoryVehicleDieselDetails()
|
|
{
|
|
$dieselMachineTableData = json_decode($this->request->getPost('dieselMachineTableData'), true);
|
|
|
|
$transformedData = [];
|
|
foreach ($dieselMachineTableData 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_reading' => 0.00,
|
|
'closing_reading' => 0.00,
|
|
'filling_diesel' => 0.00,
|
|
'consumption' => 0.00,
|
|
'running_hours' => 0.00,
|
|
'mileage' => 0.00,
|
|
];
|
|
}
|
|
|
|
$transformedData[$insertKey][$field] = (float) $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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("<script>alert('No Machine data available');</script>");
|
|
}
|
|
|
|
|
|
|
|
//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) {
|
|
|
|
$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.";
|
|
}
|
|
}
|