From 81631bb055d07739ef8e0ec123a14448b4e5e6f5 Mon Sep 17 00:00:00 2001 From: vadivelJ96 Date: Mon, 3 Feb 2025 18:30:14 +0530 Subject: [PATCH 1/4] stock module latest today vadivel J 03-02-2025 --- app/Controllers/StockController.php | 105 ++++++++++- app/Models/CoatingMachineDetailsModel.php | 2 +- app/Views/CoatingMachineDetail.php | 214 ++++++++++++++++------ 3 files changed, 257 insertions(+), 64 deletions(-) diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index dbc8ab0c..93a29bc9 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('coating_machine_details.*, t_batchcard_files.filename') // Select required fields + ->join('t_batchcard_files', 't_batchcard_files.coating_id = coating_machine_details.id', 'left') + ->where('coating_machine_details.date >=', $startDate) + ->where('coating_machine_details.date <=', $endDate) + ->orderBy('coating_machine_details.date', 'asc') + ->findAll(); $date = DateTime::createFromFormat('Y-m', $month); @@ -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,35 @@ 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()) { + $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 = [ + 'filename' => $newName, + 'coating_id' => $insertedId + ]; + + $builder->insert($data); + } + } + } + } + + } + if ($this->db->transStatus() === false) { @@ -250,6 +280,7 @@ 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..!! @@ -288,6 +319,66 @@ 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()) { + $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 = [ + 'filename' => $newName, + 'coating_id' => $id + ]; + $builder->insert($data); + } + } + } + } + + // Optional: Remove old files only if necessary (based on your criteria) + // Example: Delete old files if you no longer need them + // In this example, we're keeping old files that were not replaced. + foreach ($oldFiles as $oldFile) { + $filePath = WRITEPATH . 'uploads/batchcard/' . $oldFile['filename']; + if (file_exists($filePath)) { + unlink($filePath); // Delete old file from storage + } + } + + // After this, you would have: + // - New files uploaded + // - Only new files added to the database + // - Optionally, old files removed + } + } + + + + } 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..795088f1 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 @@ -357,18 +358,31 @@ 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 +452,14 @@ - - - + - + - - - + (avg) + (avg) @@ -492,7 +512,9 @@ -
+ -
+ - -
- - * - +
+ + +
+ + + +
+ +
+ + +
-
+