diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 94478251..1bda9c11 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -450,8 +450,7 @@ $routes->post('getSilicaIGR', 'Supplier::getSilicaIGR'); $routes->match(['GET', 'POST'], 'coatingMachineDetails', 'StockController::loadView_coatingMachineDetails'); $routes->post('addNewCoatingMachineDetails', 'StockController::addNewCoatingMachineDetails'); $routes->post('updateCoatingMachineDetails', 'StockController::updateCoatingMachineDetails'); -$routes->get('deleteCoatingMachineDetails', 'StockController::deleteCoatingMachineDetails'); - +$routes->post('deleteBatchCardFile','StockController::deleteBatchCardFile'); //drier machine details $routes->match(['GET', 'POST', 'PUT', 'DELETE'], 'drierMachineDetails', 'StockController::drierMachineDetails'); diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 6267b06c..bacb93b4 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -149,12 +149,21 @@ class StockController extends BaseController $startDate = "$month-01"; $endDate = date("Y-m-t", strtotime($startDate)); $data['coatingMachineDetails'] = $this->coatingMachineDetails_model - ->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(); + ->select('t_coatingMachineDetails.*, + JSON_ARRAYAGG( + JSON_OBJECT( + "client_given_name", t_batchcard_files.client_file_name, + "filename", t_batchcard_files.filename, + "batchCardId" , t_batchcard_files.id + ) + ) as batchcard_files') + ->join('t_batchcard_files', 't_batchcard_files.coating_id = t_coatingMachineDetails.id', 'left') + ->where('t_coatingMachineDetails.date >=', $startDate) + ->where('t_coatingMachineDetails.date <=', $endDate) + ->groupBy('t_coatingMachineDetails.id') + ->orderBy('t_coatingMachineDetails.date', 'asc') + ->findAll(); + $date = DateTime::createFromFormat('Y-m', $month); @@ -268,6 +277,8 @@ class StockController extends BaseController public function updateCoatingMachineDetails() { + + $message1 = ''; $message2 = ''; $message3 = ''; @@ -282,6 +293,40 @@ class StockController extends BaseController $this->db->transBegin(); try { $updated = $this->coatingMachineDetails_model->update($id, $putData); + + + + if ($updated !== false && $updated > 0) { // Assuming $id is the record ID being updated + + $batchCardFiles = $this->request->getFileMultiple('batchCard'); + + + // Step 4: Upload new files and insert into DB + foreach ($batchCardFiles as $index => $file) { + + if ( !empty($batchCardFiles[$index]) && !($batchCardFiles[$index]->getError() === UPLOAD_ERR_NO_FILE)) { + + $db = \Config\Database::connect(); + $builder = $db->table('t_batchcard_files'); + + if ($file->isValid() && !$file->hasMoved()) { + $clientGivenName = $file->getClientName(); // User-given filename + $newName = $file->getRandomName(); // Randomized unique filename + + // Move the new file to storage + if ($file->move(WRITEPATH . 'uploads/batchcard', $newName)) { + // Insert new file record into the database + $data = [ + 'client_file_name' => $clientGivenName, + 'filename' => $newName, + 'coating_id' => $id + ]; + $builder->insert($data); + } + } + } + } + } if ($updated !== false && $updated > 0) { $message1 = "successfully updated Coating machineDetail ."; @@ -289,7 +334,7 @@ class StockController extends BaseController $dateToBeUpdated = $putData['date']; $existingCoatingGasConsumption = $putData['existingTotalGasConsumption']; - $updatedCoatingGasConsumption = $putData['totalGasConsumption']; + $updatedCoatingGasConsumption = $putData['totalGasConsumption']; if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) { @@ -321,59 +366,7 @@ 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 - } - } - - - } - } @@ -384,23 +377,56 @@ class StockController extends BaseController } } - public function deleteCoatingMachineDetails() + public function deleteBatchCardFile() { - - $id = $this->request->getGet('id'); - - $data['is_active'] = 0; - - $updated = $this->coatingMachineDetails_model->update($id, $data); - - if ($updated) { - // Set flashdata for success message - return redirect()->to('/coatingMachineDetails')->with('success', 'Coating Machine details deleted successfully'); - } else { - // Set flashdata for error message - return redirect()->back()->with('error', 'Failed to delete Coating machine details'); + $data = $this->request->getPost(); + + if (!empty($data)) { + + $batchId = $data['id']; + $db = \Config\Database::connect(); + $builder = $db->table('t_batchcard_files'); + + // Step 1: Fetch the filename from DB before deleting + $fileRecord = $builder->select('filename')->where('id', $batchId)->get()->getRow(); + + if ($fileRecord) { + $filePath = WRITEPATH . 'uploads/batchcard/' . $fileRecord->filename; + + // Step 2: Delete the file from storage + if (file_exists($filePath)) { + unlink($filePath); + } + + // Step 3: Delete the record from the database + $sql = "DELETE FROM t_batchcard_files WHERE id = ?"; + $deleted = $db->query($sql, [$batchId]); + + if ($deleted) { + return $this->response->setJSON([ + 'status' => '200', + 'message' => "Deleted Batch Card File Successfully..!!" + ]); + } else { + return $this->response->setJSON([ + 'status' => '400', + 'message' => "Failed to Delete..!! Try Again Later" + ]); + } + } else { + return $this->response->setJSON([ + 'status' => '404', + 'message' => "File Not Found..!!" + ]); + } } + + return $this->response->setJSON([ + 'status' => '400', + 'message' => "Invalid Request..!!" + ]); } + //incoming Silica Sand Details starts public function loadView_incomingSilicaSandDetails() diff --git a/app/Models/CoatingMachineDetailsModel.php b/app/Models/CoatingMachineDetailsModel.php index f7f48d08..46819fe7 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' ,'grade','batchCard', + 'machineRunningInHrs' , 'perHourCoatingSandQTY' , 'perHourGasConsumption' ,'grade', '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 0f6e57e9..6e356d93 100644 --- a/app/Views/CoatingMachineDetail.php +++ b/app/Views/CoatingMachineDetail.php @@ -341,6 +341,7 @@ + + @@ -703,7 +706,7 @@ - + @@ -911,16 +914,23 @@ -
+
- - * - + + + +
+ +
+ + +
+ +
@@ -1224,14 +1234,43 @@ var totalGasConsumption = row.find('td:eq(9)').text(); var perHourGasConsumption = row.find('td:eq(10)').text(); var perHourCoatingSandQTY = row.find('td:eq(11)').text(); + var batchCards = row.find('td:eq(13)').text().trim(); + + console.log(batchCards); + + JSON.parse(batchCards).map(element => { + + if(!element.client_given_name){ + console.log("filename null") + return; + } + + let fileCount = $('.editBatchFile').length; // Count existing batch files + + if (fileCount >= 5) { + return; + } + + + let string = `
+ + +

( Previously Uploaded File ${element.client_given_name} )

+ +
`; + + $('#editBatchCardRowId').append(string) + }); // Fill the modal fields with the values + //batchfile + $('#editCoatingMachineDetailId').val(row.data('id')); $('#editCustomerNameId').val(customerName); - $('#editDateId').val(date); //convert this from d-m-y to Y-m-d in javascript + $('#editDateId').val(date); //convert this from d-m-y to Y-m-d in javascript $('#editShiftId').val(shift); $('#editMachineOnTimeHrId').val(machineOnTimeHr).change(); $('#editMachineOffTimeHrId').val(machineOffTimeHr).change(); @@ -1252,6 +1291,16 @@ }); + + \ No newline at end of file