diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 08de9d9d..a16a4e52 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -1717,12 +1717,14 @@ class StockController extends BaseController - $data['trpProductionDetails'] = $this->TrpProduction_model->trpProductionDetails($startDate,$endDate); + $result = $this->TrpProduction_model->trpProductionDetails($startDate,$endDate); + $data['trpProductionDetails'] = $result[0] ; - // dd($data['trpProductionDetails']); + $data['crsClientList'] = $result[1] ; + + $data['wcsSupplierList'] = $result[2] ; - // dd($this->db->getLastQuery()); $date = DateTime::createFromFormat('Y-m', $month); @@ -1737,8 +1739,6 @@ class StockController extends BaseController return $this->loadViews("stock/trpProductionStockDetails", $this->global, $data, NULL); - - } diff --git a/app/Models/TrpProductionModel.php b/app/Models/TrpProductionModel.php index 8cf96e23..0b498b98 100644 --- a/app/Models/TrpProductionModel.php +++ b/app/Models/TrpProductionModel.php @@ -60,7 +60,7 @@ class TrpProductionModel extends Model // Subquery definition $subquery = $this->db->table('t_materialmaster') ->select('materialCode') - ->where('Category = 328') + ->where('Category' ,'waste core') //328 - is for waste core in config master ->get() ->getResultArray(); @@ -78,57 +78,65 @@ class TrpProductionModel extends Model $builder = $this->db->table('t_trpProductionMaintenanceDetails tpmd'); // Select required columns - $builder->select([ - 'tpmd.date', - 'tpmd.openingTime', - 'tpmd.closingTime', - 'tpmd.totalHours', - 'tpmd.coldStart', - 'tpmd.breakdownHours', - 'tpmd.burnerFiringHours', - 'tpmd.sandFeedingHours', - 'tpmd.workingPersonEngineers', - 'tpmd.workingPersonSupervisiors', - 'tpmd.workingPersonOperators', - 'tpmd.rollerDrivers', - 'tpmd.natureOfMaintenance', - 'tpmd.location', - 'tpmd.description', + $builder->select( + 'tpmd.date, + tpmd.openingTime, + tpmd.closingTime, + tpmd.totalHours, + tpmd.coldStart, + tpmd.breakdownHours, + tpmd.burnerFiringHours, + tpmd.sandFeedingHours, + tpd.gasReceiptBg, + tpd.gasReceiptPg, + + + tpd.physicalGasConsumption, + IFNULL(tdmd.totalGasConsumption, 0) AS drierGasConsumption, + IFNULL(tcmd.totalGasConsumption, 0) AS coatingGasConsumption, + tpd.trpPlusDrierGasConsumption, + tpd.trpPhysicalGasPerTon, + tpd.panelGasConsumption, + tpd.panelGasPerTon, + + tigrdot.* , - 'tpd.gasReceiptBg', - 'tpd.gasReceiptPg', - 'tpd.physicalGasConsumption', - 'tpd.trpPlusDrierGasConsumption', - 'tpd.trpPhysicalGasPerTon', - 'tpd.panelGasConsumption', - 'tpd.panelGasPerTon', - 'tpd.edRunningHours', - 'tpd.edProduction', - 'tpd.edUsage', - 'tpd.trpProduction', - 'tpd.trpProductionPerHour', - 'tpd.waterReceipt', - 'tpd.waterConsumption', - 'tpd.ebReading', - 'tpd.ebReadingPerUnitTon', - - 'tpwd.msSeperation', - 'tpwd.edWaste', - 'tpwd.coolerBags', - 'tpwd.edPlus20Waste', - 'tpwd.cycloneWaste', + tpd.edRunningHours, + tpd.edProduction, + tpd.edUsage, - // Aggregated gas consumption - 'IFNULL(tcmd.totalGasConsumption, 0) AS coatingGasConsumption', - 'IFNULL(tdmd.totalGasConsumption, 0) AS drierGasConsumption', + tpd.trpProduction, + tpd.trpProductionPerHour, - 'tsu.total_kgs AS coating' , - - 'tigrdot.*', - 'invItems.*' + tsu.total_kgs AS coating, + invItems.* , + tpd.waterReceipt, + tpd.waterConsumption, + tpd.ebReading, + tpd.ebReadingPerUnitTon, - ]); + + tpmd.workingPersonEngineers, + tpmd.workingPersonSupervisiors, + tpmd.workingPersonOperators, + tpmd.rollerDrivers, + tpmd.natureOfMaintenance, + tpmd.location, + tpmd.description, + + tpwd.msSeperation, + tpwd.edWaste, + tpwd.coolerBags, + tpwd.edPlus20Waste, + tpwd.cycloneWaste, + + + +' + + ); + // Join with other tables $builder->join('t_trpProductionDetails tpd', 'tpd.date = tpmd.date', 'inner'); @@ -170,26 +178,27 @@ class TrpProductionModel extends Model $crsClientList = array_column($crsClientList, 'client_name'); // Initialize an empty array for dynamic SQL conditions - $caseStatements = []; + $caseStatementsCrs = []; // Loop through the client list and build dynamic SUM(CASE WHEN...) - foreach ($crsClientList as $client) { - // Escape column name (remove spaces and special characters) - $safeClient = str_replace(' ', '_', $client); - + foreach ($crsClientList as $index => $client) { + + $crsClientList[$index] = $client."_crs"; + $clientCrs = $client."_crs"; + // Add the condition to the array - $caseStatements[] = "SUM(CASE WHEN clients.client_name = " . $this->db->escape($client) . " THEN invItems.item_quantity ELSE 0 END) AS `$safeClient`"; + $caseStatementsCrs[] = "SUM(CASE WHEN clients.client_name = " . $this->db->escape($client) . " THEN invItems.item_quantity ELSE 0 END) AS `$clientCrs`"; } // Convert array to a comma-separated string - $caseSQL = implode(', ', $caseStatements); + $caseSQLCrs = implode(', ', $caseStatementsCrs); $subquery3 = $this->db->table('ip_invoice_items invItems') ->select([ 'invItems.item_date_added', - $caseSQL + $caseSQLCrs ]) ->join('ip_invoices inv', 'inv.invoice_id = invItems.invoice_id', 'left') ->join('ip_clients clients', 'clients.client_id = inv.client_id', 'left') @@ -210,19 +219,46 @@ class TrpProductionModel extends Model $materialCodes = ! empty($materialCodes) ? $materialCodes : ['600374','600422','600434']; - + + + $wcsSupplierList = $this->db->table('t_igr_details tigrd') + ->distinct() + ->select('ts.SupplierName as supplierName') + ->join('t_igr_master tigm', 'tigm.IGRNo = tigrd.IGRNo', 'left') + ->join('t_purchaseorder_master tpom', 'tpom.PONO = tigm.PONO', 'left') + ->join('t_supplierdetailsn ts', 'tpom.SupplierID = ts.SupplierID', 'left') + ->whereIn('tigrd.MaterialCode', $materialCodes) + ->where('tigrd.CreatedDate >=', $startDate) + ->where('tigrd.CreatedDate <=', $endDate) + ->groupBy('DATE(tigrd.CreatedDate)') + ->get() + ->getResultArray(); + + + $wcsSupplierList = array_column($wcsSupplierList, 'supplierName'); + + // Initialize an empty array for dynamic SQL conditions + $caseStatementsWcs = []; + + // Loop through the client list and build dynamic SUM(CASE WHEN...) + foreach ($wcsSupplierList as $index => $supplier) { + + $wcsSupplierList[$index] = $supplier."_wcs"; + $supplierWcs = $supplier."_wcs"; + + // Add the condition to the array + $caseStatementsWcs[] = "SUM(CASE WHEN ts.SupplierName = " . $this->db->escape($supplier) . " THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS `$supplierWcs` "; + } + + // Convert array to a comma-separated string + $caseSQLWcs = implode(', ', $caseStatementsWcs); + // Subquery: Waste Core Sand Suppliers $subquery4 = $this->db->table('t_igr_details tigrd') ->select([ 'DATE(tigrd.CreatedDate) AS CreatedDate', // Convert CreatedDate to DATE format - "SUM(CASE WHEN ts.SupplierName = 'Ashok leyland limited foundry divn-SPU' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS ALs_wcs", - "SUM(CASE WHEN ts.SupplierName = 'Ashok leyland limited foundry divn-ENU' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS ALe_wcs", - "SUM(CASE WHEN ts.SupplierName = 'MADRAS ENGINEERING INDUSTRIES PRIVATE LIMITED' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS ME_wcs", - "SUM(CASE WHEN ts.SupplierName = 'SIDDHARTH INDUSTRIES' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS SI_wcs", - "SUM(CASE WHEN ts.SupplierName = 'Caparo Engineering India Limited' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS caparo_wcs", - "SUM(CASE WHEN ts.SupplierName = 'NEMAK ALUMINIUM CASTING INDIA PVT LTD' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS Nemak_wcs", - "SUM(CASE WHEN ts.SupplierName = 'DR Axion India Private Limited' THEN tigrd.QuantityAsPerInvoice ELSE 0 END) AS DR_wcs" + $caseSQLWcs ]) ->join('t_igr_master tigm', 'tigm.IGRNo = tigrd.IGRNo', 'left') ->join('t_purchaseorder_master tpom', 'tpom.PONO = tigm.PONO', 'left') @@ -254,7 +290,7 @@ class TrpProductionModel extends Model $query = $builder->get(); $result = $query->getResultArray(); - return $result; + return [$result,$crsClientList,$wcsSupplierList]; } diff --git a/app/Views/stock/trpProductionStockDetails.php b/app/Views/stock/trpProductionStockDetails.php index 9c863f62..135af237 100644 --- a/app/Views/stock/trpProductionStockDetails.php +++ b/app/Views/stock/trpProductionStockDetails.php @@ -380,13 +380,15 @@ $timeOtions[] = "12:00 AM"; Panel Gas Per Ton - Incoming Sand/ Lump details + Incoming Sand/ Lump details ED details TRP Sand + + Coating - Usage + Usage Water @@ -420,20 +422,16 @@ $timeOtions[] = "12:00 AM"; BG PG - - AL(S) - - AL(E) - - ME - - SI - - caparo - - Nemak - - DR + + + + + + + + + + Running Hours @@ -445,17 +443,16 @@ $timeOtions[] = "12:00 AM"; Production Per Hour - Coating - - Hindhuja - - Nemak - - ME - - BI - - Karmen + + + + + + + + + + Receipt @@ -485,8 +482,8 @@ $timeOtions[] = "12:00 AM"; @@ -667,56 +664,18 @@ $timeOtions[] = "12:00 AM"; - - - - - + - - - - - + - - - - - - - - - - - - - - - - - + + + + - - - - - - - - - - - + @@ -767,40 +726,22 @@ $timeOtions[] = "12:00 AM"; - - - - + + + + + + + + + - - - - - - - - - - - - - - - - - - - - + + + + @@ -942,8 +883,11 @@ $timeOtions[] = "12:00 AM"; $eachDate = $trpProductionDetail['date']; // Unique identifier + + // Initialize the date entry if it doesn't exist if (!isset($summary['totalHours'])) { + $summary = [ 'totalHours' => 0, 'coldStart' => 0, @@ -959,24 +903,12 @@ $timeOtions[] = "12:00 AM"; 'trpPhysicalGasPerTon' => 0, 'panelGasConsumption' => 0, 'panelGasPerTon' => 0, - 'ALs_wcs' => 0, - 'ALe_wcs' => 0, - 'ME_wcs' => 0, - 'SI_wcs' => 0, - 'caparo_wcs' => 0, - 'Nemak_wcs' => 0, - 'DR_wcs' => 0, 'edRunningHours' => 0, 'edProduction' => 0, 'edUsage' => 0, 'trpProduction' => 0, 'trpProductionPerHour' => 0, 'coating' => 0, - 'ALs_crs' => 0, - 'Nemak_crs' => 0, - 'ME_crs' => 0, - 'BI_crs' => 0, - 'Karmen_crs' => 0, 'waterReceipt' => 0, 'waterConsumption' => 0, 'ebReading' => 0, @@ -991,6 +923,15 @@ $timeOtions[] = "12:00 AM"; 'cycloneWaste' => 0 ]; + + foreach($crsClientList as $client){ + $summary[$client] = 0; + } + + foreach($wcsSupplierList as $supplier){ + $summary[$supplier] = 0; + } + } // Sum values for the material @@ -1009,24 +950,22 @@ $timeOtions[] = "12:00 AM"; $summary['trpPhysicalGasPerTon'] += is_numeric($trpProductionDetail['trpPhysicalGasPerTon']) ? $trpProductionDetail['trpPhysicalGasPerTon'] : 0; $summary['panelGasConsumption'] += is_numeric($trpProductionDetail['panelGasConsumption']) ? $trpProductionDetail['panelGasConsumption'] : 0; $summary['panelGasPerTon'] += is_numeric($trpProductionDetail['panelGasPerTon']) ? $trpProductionDetail['panelGasPerTon'] : 0; - $summary['ALs_wcs'] += is_numeric($trpProductionDetail['ALs_wcs']) ? $trpProductionDetail['ALs_wcs'] : 0; - $summary['ALe_wcs'] += is_numeric($trpProductionDetail['ALe_wcs']) ? $trpProductionDetail['ALe_wcs'] : 0; - $summary['ME_wcs'] += is_numeric($trpProductionDetail['ME_wcs']) ? $trpProductionDetail['ME_wcs'] : 0; - $summary['SI_wcs'] += is_numeric($trpProductionDetail['SI_wcs']) ? $trpProductionDetail['SI_wcs'] : 0; - $summary['caparo_wcs'] += is_numeric($trpProductionDetail['caparo_wcs']) ? $trpProductionDetail['caparo_wcs'] : 0; - $summary['Nemak_wcs'] += is_numeric($trpProductionDetail['Nemak_wcs']) ? $trpProductionDetail['Nemak_wcs'] : 0; - $summary['DR_wcs'] += is_numeric($trpProductionDetail['DR_wcs']) ? $trpProductionDetail['DR_wcs'] : 0; + + foreach($wcsSupplierList as $supplier){ + $summary[$supplier] += is_numeric($trpProductionDetail[$supplier]) ? $trpProductionDetail[$supplier] : 0; + } + $summary['edRunningHours'] += is_numeric($trpProductionDetail['edRunningHours']) ? $trpProductionDetail['edRunningHours'] : 0; $summary['edProduction'] += is_numeric($trpProductionDetail['edProduction']) ? $trpProductionDetail['edProduction'] : 0; $summary['edUsage'] += is_numeric($trpProductionDetail['edUsage']) ? $trpProductionDetail['edUsage'] : 0; $summary['trpProduction'] += is_numeric($trpProductionDetail['trpProduction']) ? $trpProductionDetail['trpProduction'] : 0; $summary['trpProductionPerHour'] += is_numeric($trpProductionDetail['trpProductionPerHour']) ? $trpProductionDetail['trpProductionPerHour'] : 0; $summary['coating'] += is_numeric($trpProductionDetail['coating']) ? $trpProductionDetail['coating'] : 0; - $summary['ALs_crs'] += is_numeric($trpProductionDetail['ALs_crs']) ? $trpProductionDetail['ALs_crs'] : 0; - $summary['Nemak_crs'] += is_numeric($trpProductionDetail['Nemak_crs']) ? $trpProductionDetail['Nemak_crs'] : 0; - $summary['ME_crs'] += is_numeric($trpProductionDetail['ME_crs']) ? $trpProductionDetail['ME_crs'] : 0; - $summary['BI_crs'] += is_numeric($trpProductionDetail['BI_crs']) ? $trpProductionDetail['BI_crs'] : 0; - $summary['Karmen_crs'] += is_numeric($trpProductionDetail['Karmen_crs']) ? $trpProductionDetail['Karmen_crs'] : 0; + + foreach($crsClientList as $client){ + $summary[$client] += is_numeric($trpProductionDetail[$client]) ? $trpProductionDetail[$client] : 0; + } + $summary['waterReceipt'] += is_numeric($trpProductionDetail['waterReceipt']) ? $trpProductionDetail['waterReceipt'] : 0; $summary['waterConsumption'] += is_numeric($trpProductionDetail['waterConsumption']) ? $trpProductionDetail['waterConsumption'] : 0; $summary['ebReading'] += is_numeric($trpProductionDetail['ebReading']) ? $trpProductionDetail['ebReading'] : 0; @@ -1122,20 +1061,13 @@ $timeOtions[] = "12:00 AM"; BG PG - - AL(S) - - AL(E) - - ME - - SI - - caparo - - Nemak - - DR + + + + + + + Running Hours @@ -1149,15 +1081,14 @@ $timeOtions[] = "12:00 AM"; Coating - Hindhuja - - Nemak - - ME - - BI - - Karmen + + + + + + + + Receipt @@ -1296,48 +1227,14 @@ $timeOtions[] = "12:00 AM"; - - - - - - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1377,32 +1274,14 @@ $timeOtions[] = "12:00 AM"; - - - - - + + + + + + - - - - - - - - - - - - - - - - - - - -