diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 0a174d34..166dccc2 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -169,6 +169,7 @@ $routes->group("/client", ["filter" => "authMVC"], function ($routes) { $routes->post("create_2", "ClientController::createClientKYCInfo_2"); $routes->post("edit_2", "ClientController::editClientKYCInfo_2"); $routes->post("delete_2", "ClientController::deleteClientKycDocs_2/$1"); + $routes->get("download_2/(:any)", "ClientController::downloadClientKycDocs_2/$1"); }); diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 69b977b4..d7902e54 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -1121,37 +1121,43 @@ class ClientController extends AdminController } } - public function fetch_dropdown($client_id){ - - $db = db_connect(); + public function fetch_dropdown($client_id) + { + $db = db_connect(); - - $submitted_ids_subquery = $db->table('client_kyc_documents ckd') - ->select('ckd.kyc_doc_type_id') - ->where('ckd.client_id', $client_id) - ->where('ckd.is_active', 1) - ->getCompiledSelect(); - - - $result = $db->table('kyc_docs kd') - ->select('kd.*') - ->join('clients c', 'kd.kyc_type_id = c.entity_type_id') - ->where('c.id', $client_id) - ->where("kd.kyc_type_id NOT IN ({$submitted_ids_subquery})") - ->groupBy('kd.id') - ->get() - ->getResultArray(); + // Already uploaded docs + $uploadedData = $db->table('client_kyc_documents') + ->select('kyc_doc_type_id') + ->where('client_id', $client_id) + ->where('is_active', 1) + ->get() + ->getResultArray(); - - $dropdown = ''; - $dropdown .= ''; + $excludedIds = array_column($uploadedData, 'kyc_doc_type_id'); + + // Available docs + $builder = $db->table('kyc_docs kd'); + $builder->select('kd.id, kd.file_name'); + $builder->join('clients c', 'kd.kyc_type_id = c.entity_type_id'); + $builder->where('c.id', $client_id); + $builder->where('kd.is_active', 1); + + if (!empty($excludedIds)) { + $builder->whereNotIn('kd.id', $excludedIds); + } + + $result = $builder->get()->getResultArray(); // ✅ ARRAY + + // Build dropdown + $dropdown = ''; + $dropdown .= ''; + + foreach ($result as $row) { + $dropdown .= ''; + } - foreach ($result as $row) { - $dropdown .= ''; - } - return $dropdown; } @@ -2517,22 +2523,57 @@ class ClientController extends AdminController { - $result['ckdlist'] = db_connect()->table('client_kyc_documents ckd') - ->select("ckd.id,ckd.client_id,ckd.kyc_doc_type_id,ckd.file_name,kd.file_name AS kd_docs_name,ckd.other_docs_name,ckd.vehicle_id,ckd.is_active, - CASE - WHEN ckd.kyc_doc_type_id IS NULL - OR ckd.kyc_doc_type_id = 0 - OR ckd.kyc_doc_type_id = '' - THEN ckd.other_docs_name - ELSE kd.file_name - END AS ui_docs_name") - ->join('kyc_docs kd','ckd.kyc_doc_type_id = kd.kyc_type_id','left') - ->where('ckd.client_id',$client_id) - ->where('ckd.is_active',1) - ->groupBy('ckd.id') - ->get() - ->getResultArray(); + // $result['ckdlist'] = db_connect()->table('client_kyc_documents ckd') + // ->select("ckd.id,ckd.client_id,ckd.kyc_doc_type_id,ckd.file_name,kd.file_name AS kd_docs_name,ckd.other_docs_name,ckd.vehicle_id,ckd.is_active, + // CASE + // WHEN ckd.kyc_doc_type_id IS NULL + // OR ckd.kyc_doc_type_id = 0 + // OR ckd.kyc_doc_type_id = '' + // THEN ckd.other_docs_name + // ELSE kd.file_name + // END AS ui_docs_name") + // ->join('kyc_docs kd','ckd.kyc_doc_type_id = kd.id','left') + // ->where('ckd.client_id',$client_id) + // ->where('ckd.is_active',1) + // ->groupBy('ckd.id') + // ->get() + // ->getResultArray(); + + $builder = db_connect()->table('client_kyc_documents AS ckd'); + + // Select the standard fields + $builder->select(' + ckd.id, + ckd.client_id, + ckd.kyc_doc_type_id, + ckd.file_name, + ckd.other_docs_name, + ckd.vehicle_id, + ckd.is_active + '); + + // Add the CASE statement for ui_docs_name + // Using false as the second parameter tells CI4 not to escape the string + $builder->select(" + CASE + WHEN ckd.kyc_doc_type_id IS NULL OR ckd.kyc_doc_type_id = 0 OR ckd.kyc_doc_type_id = '' + THEN ckd.other_docs_name + ELSE kd.file_name + END AS ui_docs_name + ", false); + + // Join with kyc_docs + $builder->join('kyc_docs AS kd', 'ckd.kyc_doc_type_id = kd.id', 'left'); + + // Where clauses + $builder->where('ckd.client_id', $client_id); + $builder->where('ckd.is_active', 1); + + // Group By + $builder->groupBy('ckd.id'); + + $result['ckdlist'] = $builder->get()->getResultArray(); $result['client_id'] = $client_id; $table = view('client_kyc_single_table', $result); @@ -8338,6 +8379,32 @@ class ClientController extends AdminController return $default_templates_inserted ? true : false; } + // public function downloadClientKycDocs_2() + // { + + // $file = WRITEPATH . 'uploads/client_kyc_documents/' . $file_name; // Example file path + // if (file_exists($file)) { + // return $this->response->download($file, null)->setFileName($file_name); + // } else { + // return "File not found."; + // } + // } + public function downloadClientKycDocs_2($file_name = null) + { + if (!$file_name) { + return "No file specified."; + } + + // Use absolute path to your upload folder + $file = WRITEPATH . 'uploads/client_kyc_documents/' . $file_name; + + if (file_exists($file)) { + // download() takes the path as first param and null (or data) as second + return $this->response->download($file, null); + } else { + return "File not found at: " . $file; + } + } } diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 8d8a161d..2c243fee 100755 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -2350,6 +2350,13 @@ class MasterController extends AdminController return $this->respond([ 'status' => false, 'code' => 400, 'message' => $e->getMessage(), 'data' => $data ], 400); } + foreach ($data as $k => $v) { + if ($v === '' || $v === null) { + unset($data[$k]); + } + } + + // INSERT / UPDATE if (empty($id)) { $status = $posModel->insert($data); diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 3879b49d..48f3c7a3 100755 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -594,35 +594,70 @@ class UserController extends AdminController // add/update if ($method === 'post') { - $data = $this->request->getPost(); - if (!empty($data['id'])) { - $text = "update"; - $data['updated_by'] = get_session_userid(); - $result = $this->partnerStaffModel->update($data['id'], $data); - $updateID = $data['id']; - } else { - $text = "create"; - $data['role_id'] = 1; - $data['created_by'] = get_session_userid(); - $insertID = $this->partnerStaffModel->insert($data); - if($insertID){ - $details['manager_id'] = $insertID; - $details['updated_by'] = get_session_userid(); - $this->partnerStaffModel->update($insertID, $details); + $id = !empty($data['PrimaryKey']) ? $data['PrimaryKey'] : null; + + // don't forgot same means just unset the key because partner_staff some UNIQUE KEY sets in table thats why + if ($id) { + $existing = $this->partnerStaffModel->find($id); + if ($existing) { + if ($data['email'] === $existing['email']) { unset($data['email']); } + if ($data['mobile'] === $existing['mobile']) { unset($data['mobile']); } } - $result = true; } - $id = isset($insertID) && !empty($insertID) ? $insertID : ($updateID ?? null); + $errors = []; + + // Check Email Duplicate (if it wasn't unset) + if (isset($data['email'])) { + $count = $this->partnerStaffModel->where('email', $data['email'])->countAllResults(); + if ($count > 0) $errors['email'] = "This email is already taken by another user."; + } + + // Check Mobile Duplicate (if it wasn't unset) + if (isset($data['mobile'])) { + $count = $this->partnerStaffModel->where('mobile', $data['mobile'])->countAllResults(); + if ($count > 0) $errors['mobile'] = "This mobile is already taken by another user."; + } + + // If there are duplicates, stop and return error + if (!empty($errors)) { + return $this->response->setJSON([ + 'status' => 'error', + 'message' => 'Duplicate detected', + 'errors' => $errors + ])->setStatusCode(400); + } + + // --- Save/Update --- + if ($id) { + $text = "update"; + $data['updated_by'] = get_session_userid(); + + $result = $this->partnerStaffModel->update($updateID, $data); + } else { + $text = "create"; + $data['role_id'] = 1; + $data['created_by'] = get_session_userid(); + $id = $this->partnerStaffModel->insert($data); + if($id){ + $details['manager_id'] = $id; + $details['updated_by'] = get_session_userid(); + $this->partnerStaffModel->update($id, $details); + } + $result = true; + + } + + return $this->response->setJSON([ 'status' => $id ? 'success' : 'error', 'message' => $id ? "Staff {$text}d successfully" : "Unable to {$text} staff. Please try again.", 'id' => $id ])->setStatusCode($id ? 200 : 400); - } + } // delete if ($method === 'put') { $data = $this->request->getRawInput(); diff --git a/app/Models/PartnerPosModel.php b/app/Models/PartnerPosModel.php index 6e897e35..b1b99328 100644 --- a/app/Models/PartnerPosModel.php +++ b/app/Models/PartnerPosModel.php @@ -61,7 +61,7 @@ class PartnerPosModel extends Model 'mobile', 'pos_code', 'certificate_file_name', - 'pan', + 'gst','pan', 'pan_file_name', 'aadhar', 'aadhar_file_name', @@ -73,6 +73,10 @@ class PartnerPosModel extends Model 'account_holder_name', 'account_number', 'ifsc_code', + 'address', + 'state', + 'city', + 'pincode', // 'created_on' and 'updated_on' are handled by the Model, // but need to be in $allowedFields if $useTimestamps is false. diff --git a/app/Models/PartnerStaffModel.php b/app/Models/PartnerStaffModel.php index 340a0ec5..f79f06ad 100755 --- a/app/Models/PartnerStaffModel.php +++ b/app/Models/PartnerStaffModel.php @@ -16,7 +16,7 @@ class PartnerStaffModel extends Model protected $useSoftDeletes = false; // allowed fields for insert/update - protected $allowedFields = ['name','email','mobile','emp_id','role_id','email_otp','is_active','created_by','updated_by','manager_id']; + protected $allowedFields = ['name','email','mobile','emp_id','role_id','email_otp','is_active','created_by','updated_by','manager_id','retention_rate']; // automatic timestamps protected $useTimestamps = true; diff --git a/app/Views/UserList.php b/app/Views/UserList.php index 1382369b..1e3e2766 100755 --- a/app/Views/UserList.php +++ b/app/Views/UserList.php @@ -623,6 +623,14 @@ table.dataTable tbody td {