db->table('t_materialmaster') ->select('materialCode') ->where('Category', 'waste core') ->get() ->getResultArray(); $materialCodes = array_column($subquery, 'materialCode'); // Extracts 'materialCode' values // Convert the array of material codes to a comma-separated string $materialCodesString = "'" . implode("','", $materialCodes) . "'"; $sql = "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, 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, tcmd.totalGasConsumption as coatingGasConsumption, tdmd.total_gas_consumption as drierGasConsumption, tsu.total_kgs as coating, invItems.client_name as coreReclamationSupplier, invItems.TotalRsQuantity, tigrd.SupplierName as wasteCoreSupplier, tigrd.TotalQuantityAsPerInvoice FROM t_trpProductionMaintenanceDetails tpmd JOIN t_trpProductionDetails tpd ON tpd.date = tpmd.date JOIN t_trpProductionWasteDetails tpwd ON tpwd.date = tpmd.date LEFT JOIN t_coatingMachineDetails tcmd ON tcmd.date = tpmd.date LEFT JOIN t_drierMachineDetails tdmd ON tdmd.date = tpmd.date LEFT JOIN t_trp_sand_use tsu ON tsu.date = tpmd.date LEFT JOIN ( Select clients.client_name, invItems.item_date_added, SUM(invItems.item_quantity) AS TotalRsQuantity from ip_invoice_items invItems -- All joins to get client name thats it LEFT JOIN ip_invoices inv ON inv.invoice_id = invItems.invoice_id LEFT JOIN ip_clients clients ON clients.client_id = inv.client_id -- getting core reclamation sand Where invItems.item_name = 'Core Sand Reclaimed' -- grouping Client who provides reclamation sand on a particular date with 1 or more invoices Group By invItems.item_date_added , clients.client_name ) invItems ON invItems.item_date_added = tpmd.date LEFT JOIN ( Select tigrd.CreatedDate ,tigrd.MaterialCode , tigrd.IGRNo , ts.SupplierName, SUM(QuantityAsPerInvoice) AS TotalQuantityAsPerInvoice from t_igr_details tigrd -- All joins to get supplier name thats it LEFT JOIN t_igr_master tigm ON tigm.IGRNo = tigrd.IGRNo LEFT JOIN t_purchaseorder_master tpom ON tpom.PONO = tigm.PONO LEFT JOIN t_supplierdetailsn ts ON tpom.SupplierID = ts.SupplierID -- getting waste core sands using three distinct material codes Where tigrd.MaterialCode IN ($materialCodesString) -- grouping supplier who provides waste core on a particular date with 1 or more IGRS Group By Date(tigrd.CreatedDate) , ts.SupplierName ) tigrd ON DATE(tigrd.CreatedDate) = tpmd.date WHERE tpmd.date >= ? AND tpmd.date <= ? ORDER BY tpmd.date ASC"; // "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, // 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, // tcmd.totalGasConsumption AS coatingGasConsumption, // tdmd.total_gas_consumption AS drierGasConsumption, // tsu.total_kgs AS coating, // -- Aggregated values for Reclaimed Sand // (SELECT GROUP_CONCAT(CONCAT(clients.client_name, ':', invItems.item_quantity) SEPARATOR '; ') // FROM ip_invoice_items invItems // LEFT JOIN ip_invoices inv ON inv.invoice_id = invItems.invoice_id // LEFT JOIN ip_clients clients ON clients.client_id = inv.client_id // WHERE invItems.item_name = 'Core Sand Reclaimed' AND invItems.item_date_added = tpmd.date // ) AS reclaimedSandDetails, // -- Aggregated values for Waste Core // (SELECT GROUP_CONCAT(CONCAT(ts.SupplierName, ':', tigrd.QuantityAsPerInvoice) SEPARATOR '; ') // FROM t_igr_details tigrd // LEFT JOIN t_igr_master tigm ON tigm.IGRNo = tigrd.IGRNo // LEFT JOIN t_purchaseorder_master tpom ON tpom.PONO = tigm.PONO // LEFT JOIN t_supplierdetailsn ts ON tpom.SupplierID = ts.SupplierID // WHERE tigrd.MaterialCode IN ($materialCodesString) AND DATE(tigrd.CreatedDate) = tpmd.date // ) AS wasteCoreDetails // FROM // t_trpProductionMaintenanceDetails tpmd // JOIN // t_trpProductionDetails tpd ON tpd.date = tpmd.date // JOIN // t_trpProductionWasteDetails tpwd ON tpwd.date = tpmd.date // LEFT JOIN // t_coatingMachineDetails tcmd ON tcmd.date = tpmd.date // LEFT JOIN // t_drierMachineDetails tdmd ON tdmd.date = tpmd.date // LEFT JOIN // t_trp_sand_use tsu ON tsu.date = tpmd.date // WHERE // tpmd.date >= ? // AND // tpmd.date <= ? // ORDER BY // tpmd.date ASC; // " // Execute the query $query = $this->db->query($sql, [ $startDate, $endDate]); $result = $query->getResultArray(); return $result; } }