diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index dbc8ab0c..6267b06c 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -149,11 +149,12 @@ class StockController extends BaseController $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(); + ->select('t_coatingMachineDetails.*, t_batchcard_files.*') // Select required fields + ->join('t_batchcard_files', 't_batchcard_files.coating_id = t_coatingMachineDetails.id', 'left') + ->where('t_coatingMachineDetails.date >=', $startDate) + ->where('t_coatingMachineDetails.date <=', $endDate) + ->orderBy('t_coatingMachineDetails.date', 'asc') + ->findAll(); $date = DateTime::createFromFormat('Y-m', $month); @@ -181,8 +182,8 @@ class StockController extends BaseController $sameShiftOnDateExists = $this->coatingMachineDetails_model ->where('date', $date) ->where('shift', $shift) - ->where('is_active', 1) - ->first(); + ->where('is_active', 1) + ->first(); if (!empty($sameShiftOnDateExists)) { return redirect()->back()->with('error', 'Already same shift exists on the same date ..!!'); @@ -193,9 +194,9 @@ class StockController extends BaseController try { // Insert into coating machine details table - $inserted = $this->coatingMachineDetails_model->insert($postData); + $insertedId = $this->coatingMachineDetails_model->insert($postData); - if ($inserted !== false && $inserted > 0) { + if ($insertedId !== false && $insertedId > 0) { $message1 = 'Coating Machine details updated successfully'; @@ -214,6 +215,37 @@ class StockController extends BaseController throw new \Exception('Failed to insert Coating machine details.'); } + if ($insertedId !== false && $insertedId > 0) { + + $batchCardFiles = $this->request->getFileMultiple('batchCard'); + + if (!empty($batchCardFiles) && !($batchCardFiles[0]->getError() === UPLOAD_ERR_NO_FILE)) { + + foreach ($batchCardFiles as $file) { + + if ($file->isValid() && !$file->hasMoved()) { + $clientGivenName = $file->getClientName(); // User-given filename + $newName = $file->getRandomName(); + + if ($file->move(WRITEPATH . 'uploads/batchcard', $newName)) { + // Only save to DB if file moved successfully + $db = \Config\Database::connect(); + $builder = $db->table('t_batchcard_files'); + + $data = [ + 'client_file_name' => $clientGivenName, + 'filename' => $newName, + 'coating_id' => $insertedId + ]; + + $builder->insert($data); + } + } + } + } + + } + if ($this->db->transStatus() === false) { @@ -250,13 +282,14 @@ class StockController extends BaseController $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']; + $updatedCoatingGasConsumption = $putData['totalGasConsumption']; if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) { @@ -288,6 +321,63 @@ class StockController extends BaseController } else { throw new \Exception('Failed to update Coating machine details.'); } + + if ($updated !== false && $updated > 0) { // Assuming $id is the record ID being updated + + $batchCardFiles = $this->request->getFileMultiple('batchCard'); + + if (!empty($batchCardFiles) && !($batchCardFiles[0]->getError() === UPLOAD_ERR_NO_FILE)) { + + // Optional: Delete only files that need to be replaced (if you have a way to identify which are replaced) + // Step 1: Fetch existing files linked with the $coating_id + $db = \Config\Database::connect(); + $builder = $db->table('t_batchcard_files'); + $oldFiles = $builder->where('coating_id', $id)->findAll(); + + // Step 2: Upload new files and save them in the database + foreach ($batchCardFiles as $file) { + if ($file->isValid() && !$file->hasMoved()) { + $clientGivenName = $file->getClientName(); // User-given filename + $newName = $file->getRandomName(); + + // Check if the new file is different from the old ones (if needed) + $isNewFile = true; + foreach ($oldFiles as $oldFile) { + if ($oldFile['filename'] === $newName) { + $isNewFile = false; // File already exists, skip uploading + break; + } + } + + if ($isNewFile) { + // Move the new file and save to DB + if ($file->move(WRITEPATH . 'uploads/batchcard', $newName)) { + $data = [ + 'client_file_name' => $clientGivenName, + 'filename' => $newName, + 'coating_id' => $id + ]; + $builder->insert($data); + } + } + } + } + + + foreach ($oldFiles as $oldFile) { + $filePath = WRITEPATH . 'uploads/batchcard/' . $oldFile['filename']; + if (file_exists($filePath)) { + unlink($filePath); // Delete old file from storage + } + } + + + } + } + + + + } catch (\Exception $error) { $this->db->transRollback(); return redirect()->back()->with('error', ' ' . $error->getMessage()); diff --git a/app/Models/CoatingMachineDetailsModel.php b/app/Models/CoatingMachineDetailsModel.php index 1e353d11..f7f48d08 100644 --- a/app/Models/CoatingMachineDetailsModel.php +++ b/app/Models/CoatingMachineDetailsModel.php @@ -13,7 +13,7 @@ class CoatingMachineDetailsModel extends Model protected $useSoftDeletes = false; protected $protectFields = true; protected $allowedFields = ['shift','machineOnTime','machineOffTime','totalCoating','date', - 'machineRunningInHrs' , 'perHourCoatingSandQTY' , 'perHourGasConsumption' , + 'machineRunningInHrs' , 'perHourCoatingSandQTY' , 'perHourGasConsumption' ,'grade','batchCard', 'totalCoatingSandInKg','totalGasConsumption','customerName', 'is_active','created_at','updated_at']; protected bool $allowEmptyInserts = false; diff --git a/app/Views/CoatingMachineDetail.php b/app/Views/CoatingMachineDetail.php index 916c1c67..0f6e57e9 100644 --- a/app/Views/CoatingMachineDetail.php +++ b/app/Views/CoatingMachineDetail.php @@ -327,12 +327,13 @@ Machine On Time Machine Off Time Machine Running(hr) + Customer Name + Grade Total Coating Total Coating Sand(kg) Total Gas Consumption Hour Gas Hour QTY(kg) - Customer Name Action @@ -340,7 +341,6 @@ format('d-m-Y'); ?> - + + + + - + + + + + + + + + + + - + Machine On Time Machine Off Time Machine Running(hr) + Customer Name + Grade Total Coating Total Coating Sand(kg) Total Gas Consumption @@ -434,12 +451,14 @@ - - - + - + - - - + (avg) + (avg) @@ -492,7 +511,9 @@ -
+ -
+ - -
- - * - +
+ + +
+ + + +
+ +
+ + +
+ - -
+