stock module latest today vadivel J 29-01-2025

This commit is contained in:
vadivelJ96 2025-01-29 11:23:20 +05:30
parent b8a1490d76
commit 2406b0fa71

View File

@ -1409,7 +1409,9 @@ class StockController extends BaseController
$inserts = $this->generateDummyData($startDate, $endDate); $inserts = $this->generateDummyData($startDate, $endDate);
$data['trpProductionMaintenanceData'] = $this->updateTrpProductionDetails($inserts); $freshEntry = true ;
$data['trpProductionMaintenanceData'] = $this->updateTrpProductionDetails($inserts ,$freshEntry);
} }
@ -1443,37 +1445,47 @@ class StockController extends BaseController
public function updateTrpProductionDetails($inserts) { public function updateTrpProductionDetails($inserts, $freshEntry = false)
{
$postData = $this->request->getPost(); $postData = $this->request->getPost();
if(empty($postData['updateTrpProductionDetails'])){ if (empty($postData['updateTrpProductionDetails'])) {
if(empty($inserts)){ if (empty($inserts)) {
return $this->response->setJSON(['error' => 'No data found to update']); return $this->response->setJSON(['error' => 'No data found to update']);
}else{ } else {
$postData['updateTrpProductionDetails'] = $inserts; $postData['updateTrpProductionDetails'] = $inserts;
} }
} }
$updateTrpProductionDetails = $postData['updateTrpProductionDetails']; $updateTrpProductionDetails = $postData['updateTrpProductionDetails'];
// Extract all dates from input data
$dates = array_column($updateTrpProductionDetails, 'Date');
$formattedDates = array_map(fn($date) => $this->convertToDateWithFlexibleFormats($date, ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"), $dates);
// Fetch all existing records in a single query per table
$existingMaintenance = $this->TrpProductionMaintenance_model->whereIn('date', $formattedDates)->findAll();
$existingProduction = $this->TrpProduction_model->whereIn('date', $formattedDates)->findAll();
$existingWaste = $this->TrpProductionWaste_model->whereIn('date', $formattedDates)->findAll();
// Map existing records by date for quick lookup
$existingMaintenanceMap = array_column($existingMaintenance, null, 'date');
$existingProductionMap = array_column($existingProduction, null, 'date');
$existingWasteMap = array_column($existingWaste, null, 'date');
$insertMaintenance = [];
$updateMaintenance = [];
$insertProduction = [];
$updateProduction = []; $updateProduction = [];
$updateMaintanence = []; $insertWaste = [];
$updateWaste = []; $updateWaste = [];
$insertProduction = [];
$insertMaintanence = [];
$insertWaste = [];
foreach ($updateTrpProductionDetails as $data) { foreach ($updateTrpProductionDetails as $data) {
$date = $this->convertToDateWithFlexibleFormats($data['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d");
$data['date'] = $this->convertToDateWithFlexibleFormats($data['Date'], ["d-m-Y", "Y-m-d", "m/d/Y", "m-d-Y"], "Y-m-d"); // Data arrays
$date = $data['date'];
$dbDataTrpMaintenance = [ $dbDataTrpMaintenance = [
'date' => $data['date'], 'date' => $date,
'openingTime' => $data['openingTime'], 'openingTime' => $data['openingTime'],
'closingTime' => $data['closingTime'], 'closingTime' => $data['closingTime'],
'totalHours' => $data['totalHours'], 'totalHours' => $data['totalHours'],
@ -1491,11 +1503,11 @@ class StockController extends BaseController
]; ];
$dbDataTrpProduction = [ $dbDataTrpProduction = [
'date' => $data['date'], 'date' => $date,
'gasReceiptBg' => $data['gasReceiptBg'], 'gasReceiptBg' => $data['gasReceiptBg'],
'gasReceiptPg' => $data['gasReceiptPg'], 'gasReceiptPg' => $data['gasReceiptPg'],
'physicalGasConsumption'=> $data['physicalGasConsumption'], 'physicalGasConsumption' => $data['physicalGasConsumption'],
'trpPlusDrierGasConsumption'=> $data['trpPlusDrierGasConsumption'], 'trpPlusDrierGasConsumption' => $data['trpPlusDrierGasConsumption'],
'trpPhysicalGasPerTon' => $data['trpPhysicalGasPerTon'], 'trpPhysicalGasPerTon' => $data['trpPhysicalGasPerTon'],
'panelGasConsumption' => $data['panelGasConsumption'], 'panelGasConsumption' => $data['panelGasConsumption'],
'panelGasPerTon' => $data['panelGasPerTon'], 'panelGasPerTon' => $data['panelGasPerTon'],
@ -1511,7 +1523,7 @@ class StockController extends BaseController
]; ];
$dbDataTrpWaste = [ $dbDataTrpWaste = [
'date' => $data['date'], 'date' => $date,
'msSeperation' => $data['msSeperation'], 'msSeperation' => $data['msSeperation'],
'edWaste' => $data['edWaste'], 'edWaste' => $data['edWaste'],
'coolerBags' => $data['coolerBags'], 'coolerBags' => $data['coolerBags'],
@ -1519,67 +1531,63 @@ class StockController extends BaseController
'cycloneWaste' => $data['cycloneWaste'], 'cycloneWaste' => $data['cycloneWaste'],
]; ];
$trpMaintenanceResultExists = $this->TrpProductionMaintenance_model // Check if data already exists
->where('date', $date) if (isset($existingMaintenanceMap[$date])) {
->first(); $dbDataTrpMaintenance['id'] = $existingMaintenanceMap[$date]['id'];
$updateMaintenance[] = $dbDataTrpMaintenance;
$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 { } else {
$dbDataTrpMaintenance['id'] = $trpMaintenanceResultExists['id']; $insertMaintenance[] = $dbDataTrpMaintenance;
$dbDataTrpProduction['id'] = $trpProductionResultExists['id']; }
$dbDataTrpWaste['id'] = $trpWasteResultExists['id'];
$updateMaintanence[] = $dbDataTrpMaintenance; if (isset($existingProductionMap[$date])) {
$dbDataTrpProduction['id'] = $existingProductionMap[$date]['id'];
$updateProduction[] = $dbDataTrpProduction; $updateProduction[] = $dbDataTrpProduction;
} else {
$insertProduction[] = $dbDataTrpProduction;
}
if (isset($existingWasteMap[$date])) {
$dbDataTrpWaste['id'] = $existingWasteMap[$date]['id'];
$updateWaste[] = $dbDataTrpWaste; $updateWaste[] = $dbDataTrpWaste;
} else {
$insertWaste[] = $dbDataTrpWaste;
} }
} }
// Start the transaction // Start transaction
$this->db->transBegin(); $this->db->transBegin();
try { try {
if (!empty($insertMaintanence)) { // Insert new records in batch
$this->TrpProductionMaintenance_model->insertBatch($insertMaintanence); if (!empty($insertMaintenance)) {
$this->TrpProductionMaintenance_model->insertBatch($insertMaintenance);
} }
if (!empty($insertProduction)) { if (!empty($insertProduction)) {
$this->TrpProduction_model->insertBatch($insertProduction); $this->TrpProduction_model->insertBatch($insertProduction);
} }
if (!empty($insertWaste)) { if (!empty($insertWaste)) {
$this->TrpProductionWaste_model->insertBatch($insertWaste); $this->TrpProductionWaste_model->insertBatch($insertWaste);
} }
if (!empty($updateMaintanence)) { // Update existing records in batch
$this->TrpProductionMaintenance_model->updateBatch($updateMaintanence, 'id'); if (!empty($updateMaintenance)) {
$this->TrpProductionMaintenance_model->updateBatch($updateMaintenance, 'id');
} }
if (!empty($updateProduction)) { if (!empty($updateProduction)) {
$this->TrpProduction_model->updateBatch($updateProduction, 'id'); $this->TrpProduction_model->updateBatch($updateProduction, 'id');
} }
if (!empty($updateWaste)) { if (!empty($updateWaste)) {
$this->TrpProductionWaste_model->updateBatch($updateWaste, 'id'); $this->TrpProductionWaste_model->updateBatch($updateWaste, 'id');
} }
// Commit the transaction if all operations are successful // Commit transaction if successful
$this->db->transCommit(); $this->db->transCommit();
echo "Data Saved Successfully";
if (!$freshEntry) {
echo "Data Saved Successfully";
}
} catch (\Exception $error) { } catch (\Exception $error) {
// Rollback the transaction in case of any errors // Rollback transaction on error
$this->db->transRollback(); $this->db->transRollback();
echo "An error occurred: " . $error->getMessage(); echo "An error occurred: " . $error->getMessage();
} }
@ -1587,6 +1595,8 @@ class StockController extends BaseController
public function generateDummyData($startDate, $endDate) public function generateDummyData($startDate, $endDate)
{ {
$datesInMonth = []; $datesInMonth = [];
@ -1606,41 +1616,41 @@ class StockController extends BaseController
$inserts[] = [ $inserts[] = [
'Date' => $dateInMonth, 'Date' => $dateInMonth,
'openingTime' => '00:00', 'openingTime' => " ",
'closingTime' => '00:00', 'closingTime' => " ",
'totalHours' => '00:00', 'totalHours' => " ",
'coldStart' => '00:00', 'coldStart' => " ",
'breakdownHours' => '00:00', 'breakdownHours' => " ",
'burnerFiringHours' => '00:00', 'burnerFiringHours' => " ",
'sandFeedingHours' => '00:00', 'sandFeedingHours' => " ",
'workingPersonEngineers' => 0, 'workingPersonEngineers' => " ",
'workingPersonSupervisiors' => 0, 'workingPersonSupervisiors' => " ",
'workingPersonOperators' => 0, 'workingPersonOperators' => " ",
'rollerDrivers' => 0, 'rollerDrivers' => " ",
'natureOfMaintenance' => '', 'natureOfMaintenance' => '',
'location' => '', 'location' => '',
'description' => '', 'description' => '',
'gasReceiptBg' => 0, 'gasReceiptBg' => " ",
'gasReceiptPg' => 0, 'gasReceiptPg' => " ",
'physicalGasConsumption' => 0, 'physicalGasConsumption' => " ",
'trpPlusDrierGasConsumption' => 0, 'trpPlusDrierGasConsumption' => " ",
'trpPhysicalGasPerTon' => 0, 'trpPhysicalGasPerTon' => " ",
'panelGasConsumption' => 0, 'panelGasConsumption' => " ",
'panelGasPerTon' => 0, 'panelGasPerTon' => " ",
'edRunningHours' => 0, 'edRunningHours' => " ",
'edProduction' => 0, 'edProduction' => " ",
'edUsage' => 0, 'edUsage' => " ",
'trpProduction' => 0, 'trpProduction' => " ",
'trpProductionPerHour' => 0, 'trpProductionPerHour' => " ",
'waterReceipt' => 0, 'waterReceipt' => " ",
'waterConsumption' => 0, 'waterConsumption' => " ",
'ebReading' => 0, 'ebReading' =>" ",
'ebReadingPerUnitTon' => 0, 'ebReadingPerUnitTon' =>" ",
'msSeperation' => 0, 'msSeperation' => " ",
'edWaste' => 0, 'edWaste' => " ",
'coolerBags' => 0, 'coolerBags' =>" ",
'edPlus20Waste' => 0, 'edPlus20Waste' => " ",
'cycloneWaste' => 0 'cycloneWaste' => " "
]; ];
} }