diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 82d879b7..bb47a222 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -463,8 +463,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/Payslip.php b/app/Controllers/Payslip.php index 989b3e2a..5e3aae25 100755 --- a/app/Controllers/Payslip.php +++ b/app/Controllers/Payslip.php @@ -688,7 +688,7 @@ class Payslip extends BaseController - if ($value->esi_applicable == 1) { $esiAmount = $currentDayBasic * $ESI_Rate / 100; } else { $esiAmount = 0; } + if ($value->esi_applicable == 1) { $esiAmount = ($currentDayBasic + $currentDayHra) * $ESI_Rate / 100; } else { $esiAmount = 0; } if ($value->pf_applicable == 1) { $pfAmount = $currentDayBasic * $PF_Rate / 100; } else { $pfAmount = 0; } /** pf amount per day**/ @@ -830,7 +830,7 @@ class Payslip extends BaseController //$totalSalary = $currentDayBasic + $currentDayHra + $totSalayOT; - if ($value->esi_applicable == 1) { $esiAmount = $currentDayBasic * $ESI_Rate / 100; } else { $esiAmount = 0;} + if ($value->esi_applicable == 1) { $esiAmount = ($currentDayBasic + $currentDayHra) * $ESI_Rate / 100; } else { $esiAmount = 0;} if ($value->pf_applicable == 1) { $pfAmount = $currentDayBasic * $PF_Rate / 100; } else { $pfAmount = 0;} /** pf amount per day**/ diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index dbc8ab0c..bacb93b4 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -149,11 +149,21 @@ 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.*, + 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); @@ -181,8 +191,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 +203,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 +224,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) { @@ -236,6 +277,8 @@ class StockController extends BaseController public function updateCoatingMachineDetails() { + + $message1 = ''; $message2 = ''; $message3 = ''; @@ -250,13 +293,48 @@ 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 ."; //update Gas stock consumption if update is successfull..!! $dateToBeUpdated = $putData['date']; $existingCoatingGasConsumption = $putData['existingTotalGasConsumption']; - $updatedCoatingGasConsumption = $putData['totalGasConsumption']; + $updatedCoatingGasConsumption = $putData['totalGasConsumption']; if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) { @@ -288,29 +366,67 @@ class StockController extends BaseController } else { throw new \Exception('Failed to update Coating machine details.'); } + + + + + } catch (\Exception $error) { $this->db->transRollback(); return redirect()->back()->with('error', ' ' . $error->getMessage()); } } - 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 1e353d11..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' , + '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 916c1c67..6e356d93 100644 --- a/app/Views/CoatingMachineDetail.php +++ b/app/Views/CoatingMachineDetail.php @@ -327,12 +327,13 @@