diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index bd9f4a54..09f34f77 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -1611,7 +1611,7 @@ class ClientController extends AdminController } - public function createClientBranch() + public function createClientBranchNew() { $this->myLogger->logme('error', 'Client branch CREATE function called'); @@ -1796,7 +1796,7 @@ class ClientController extends AdminController } } - public function editClientBranch() + public function editClientBranchNew() { $this->myLogger->logme('error', 'Client branch EDIT function called'); @@ -1916,11 +1916,9 @@ class ClientController extends AdminController $level_contact_data = []; if (!empty($level_contact_data_raw)) { - $level_contact_data_decoded = json_decode($level_contact_data_raw, true); + $level_contact_data_decoded = json_decode($level_contact_data_raw ?? '{}', true); - if (json_last_error() === JSON_ERROR_NONE && is_array($level_contact_data_decoded)) { $level_contact_data = sanitizeInputArrayAdvanced($level_contact_data_decoded); - } } if (!empty($level_contact_data)) { @@ -1949,6 +1947,367 @@ class ClientController extends AdminController } } + public function saveLevelContactsNew($level_contact_data, $branch_id) + { + if (!empty($level_contact_data) && is_array($level_contact_data)) { + foreach ($level_contact_data as $value) { + if (!empty($value['id'])) { + $id = $value['id']; + unset($value['id']); + $this->levelContactModel->update($id, $value); + } else { + unset($value['id']); + $value['contact_type'] = "client"; + $value['ref_id'] = $branch_id ?? null; + $this->levelContactModel->insert($value); + } + } + } + } + + public function removeLevelContactsNew() + { + $id = $this->request->getGet('id'); + try { + if (empty($id)) { + return $this->respond([ + 'status' => false, + 'message' => 'Invalid ID provided. ID is empty', + 'data' => $id + ], 400); + } + + $updated = $this->levelContactModel->update($id, ['is_active' => 0]); + + if ($updated === false) { + return $this->respond([ + 'status' => false, + 'message' => 'Failed to remove contact' + ], 500); + } + + return $this->respond([ + 'status' => true, + 'message' => 'Contact removed successfully' + ]); + } catch (\Exception $e) { + return $this->respond([ + 'status' => false, + 'message' => 'An error occurred: ' . $e->getMessage() + ], 500); + } + } + + + + public function createClientBranch() + { + + $this->myLogger->logme('error', 'Client branch CREATE function called'); + + $rules = [ + 'branch_name' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'Branch Name is required', + ] + ], + 'branch_code' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'Branch Code is required', + ] + ], + 'address1' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'Address Line 1 is required', + ] + ], + 'state' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'State is required', + ] + ], + 'district' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'District is required.', + ] + ], + 'city' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'City is required.', + ] + ], + 'pincode' => [ + 'rules' => 'required', + 'errors' => [ + 'required' => 'Pincode is required.', + ] + ], + 'gst' => [ + 'rules' => 'required|regex_match[/^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}Z[A-Z\d]{1}$/]', + 'errors' => [ + 'required' => 'GST number is required', + 'regex_match' => 'Invalid GST Number. Example: 12ABCDE1234F5Z6' + ] + ], + 'name.*' => [ + 'rules' => 'required|alpha_space', + 'errors' => [ + 'required' => 'Contact name is required', + 'alpha_space' => 'Contact name may contain only letters and spaces' + ] + ], + 'designation.*' => [ + 'rules' => 'required|alpha_space', + 'errors' => [ + 'required' => 'Designation is required', + 'alpha_space' => 'Designation may contain only letters and spaces' + ] + ], + 'email.*' => [ + 'rules' => 'required|valid_email', + 'errors' => [ + 'required' => 'Email is required', + 'valid_email' => 'Please enter a valid email address' + ] + ], + 'mobile.*' => [ + 'rules' => 'required|numeric|exact_length[10]', + 'errors' => [ + 'required' => 'Mobile number is required', + 'numeric' => 'Mobile number must contain digits only', + 'exact_length' => 'Mobile number must be exactly 10 digits' + ] + ], + ]; + + if (!$this->validate($rules)) { + return $this->response->setStatusCode(400)->setJSON([ + 'status' => false, + 'message' => 'Input validation failed', + 'code' => 400, + 'errors' => $this->validator->getErrors() + ]); + } + + $data = $this->request->getPost(); + $data = sanitizeInputArrayAdvanced($data); + + + if (!isset($data['sez'])) { + $data['sez'] = 0; + } elseif ($data['sez']) { + $data['sez'] = 1; + } + + $units = json_decode($data['units'], true) ?? []; + + if (!is_array($units) || empty($units)) { + $client_data = $this->clientModel->where('id', $data['client_id'])->first(); + $default_unit = trim(($client_data['short_name'] ?? '') . '-' . ($data['branch_code'] ?? ''), '-'); + $data['units'] = json_encode([$default_unit]); + } + + $data['created_by'] = get_session_userid(); + + + // before updating check if pre_branch_id is already existing in the current db + + if(isset($data['pre_branch_id']) && !empty($data['pre_branch_id'])) + { + $existing_pre_branch = $this->clientBranchModel + ->where('pre_branch_id',$data['pre_branch_id']) + //->where('id !=',$post_branch_id) + ->first(); + + if($existing_pre_branch) + { + return $this->respond([ + 'status' => false, + 'code' => 409, + 'message' => 'The branch is already mapped with another branch. Please check.', + ], 409); + } + } + + + + $insert = $this->clientBranchModel->insert($data); + + $post_branch_id = $insert; + + if ($insert) { + $level_contact_data = $this->request->getPost('level_contect_data'); + $level_contact_data = !empty($level_contact_data) ? json_decode($level_contact_data, true) : null; + $this->saveLevelContacts($level_contact_data, $insert); + } + + if($post_branch_id && isset($data['pre_branch_id']) && !empty($data['pre_branch_id'])) + { + // need to update the client_branch in the pre + $result = $this->updatePreClientBranch($data['pre_branch_id'],$post_branch_id , "create"); + + log_message('error','Pre client_branch update result for pre_branch_id '.$data['pre_branch_id'].' and post_branch_id '.$post_branch_id.' is '.json_encode($result)); + } + + if ($insert) { + $branchData = $this->clientBranchModel->where('client_id', $this->request->getPost('client_id'))->findAll(); + $branchData['role'] = get_role_id(); + return $this->respond([ + 'status' => true, + 'code' => 200, + 'data' => $branchData, + 'message' => 'Client branch created successfully', + ], 200); + } else { + return $this->respond([ + 'status' => false, + 'code' => 404, + 'message' => 'Failed to create client branch ', + ], 200); + } + } + + public function editClientBranch() + { + + $this->myLogger->logme('error', 'Client branch EDIT function called'); + $id = $this->request->getPost('branch_id_primarykey'); + $client_id = $this->request->getPost('client_id'); + $pre_branch_id = $this->request->getPost('pre_branch_id') ?? ''; + + $data = $this->request->getPost(); + $data = sanitizeInputArrayAdvanced($data); + $data['pre_branch_id'] = $pre_branch_id; + + $units = $this->request->getPost('units'); + + $emp_unit_count = 0; + $rr_unit_count = 0; + $rr_unit_count2 = 0; + $total_count = 0; + + $list_of_branch_units = $this->clientBranchModel->find((int)$id); + $units = json_decode($list_of_branch_units['units']); + + if (!is_array($units) || empty($units)) { + $client_data = $this->clientModel->where('id', $data['client_id'])->first(); + $default_unit = trim(($client_data['short_name'] ?? '') . '-' . ($data['branch_code'] ?? ''), '-'); + $data['units'] = json_encode([$default_unit]); + } + + if (!empty($units)) { + foreach ($units as $unit) { + $emp_unit_count += $this->employeeModel->where('unit', $unit)->countAllResults(); + $rr_unit_count += $this->policyPremium2Model->where('unit', $unit)->countAllResults(); + $rr_unit_count2 += $this->policyPremium1Model->where('unit', $unit)->countAllResults(); + } + + $total_count = $emp_unit_count + $rr_unit_count + $rr_unit_count2; + } + + $uncommonValues = []; + + if ($total_count > 0) { + $units = (string) $this->request->getPost('units'); // Assuming 'units' is an array + + $list_of_branch_units = $this->clientBranchModel->find((int)$id); + $branch_units = json_decode($list_of_branch_units['units'], true); + $units = json_decode($units); + + $uncommonValues = array_diff($branch_units, $units); + + if (count($uncommonValues) > 0) { + + $branchData = $this->clientBranchModel->where('client_id', $client_id)->findAll(); + + return $this->respond([ + 'status' => false, + 'code' => 404, + 'message' => "Deleted unit(s) in use. couldn't complete this operation.", + 'uncommonValues' => $uncommonValues, + 'data' => $branchData, + ], 200); + } + } + + if (!isset($data['sez'])) { + $data['sez'] = 0; + } elseif ($data['sez']) { + $data['sez'] = 1; + } + + $data['updated_by'] = get_session_userid(); + + $post_branch_id = $id; + + // before updating check if pre_branch_id is already existing in the current db + + if(isset($data['pre_branch_id']) && !empty($data['pre_branch_id'])) + { + $existing_pre_branch = $this->clientBranchModel + ->where('pre_branch_id',$data['pre_branch_id']) + ->where('id !=',$post_branch_id) + ->first(); + + if($existing_pre_branch) + { + return $this->respond([ + 'status' => false, + 'code' => 409, + 'message' => 'The branch is already mapped with another branch. Please check.', + ], 409); + } + } + + $insert = $this->clientBranchModel->update($id, $data); + + + + if($post_branch_id && isset($data['pre_branch_id']) && !empty($data['pre_branch_id'])) + { + // need to update the client_branch in the pre + $result = $this->updatePreClientBranch($data['pre_branch_id'],$post_branch_id , "update"); + + log_message('error','Pre client_branch update result for pre_branch_id '.$data['pre_branch_id'].' and post_branch_id '.$post_branch_id.' is '.json_encode($result)); + } + + + $this->myLogger->logme('error', 'Client branch EDITED by {data}', ['data' => get_session_userid()]); + + + if ($insert) { + + $level_contact_data = $this->request->getPost('level_contect_data'); + $level_contact_data = !empty($level_contact_data) ? json_decode($level_contact_data, true) : null; + $this->saveLevelContacts($level_contact_data, $id); + } + + if ($insert) { + $branchData = $this->clientBranchModel->where('client_id', $client_id)->findAll(); + return $this->respond([ + 'status' => true, + 'code' => 200, + 'data' => $branchData, + 'emp_unit_count' => $emp_unit_count, + 'rr_unit_count' => $rr_unit_count, + 'list_of_branch_units' => $list_of_branch_units, + 'total_count' => $total_count, + 'uncommonValues' => $uncommonValues, + 'message' => 'Client branch updated successfully', + 'response_data' => $this->request->getPost() + + ], 200); + } else { + return $this->respond(['status' => false, 'code' => 404, 'message' => 'Failed to update client branch'], 200); + } + } + public function saveLevelContacts($level_contact_data, $branch_id) { if (!empty($level_contact_data) && is_array($level_contact_data)) { diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index 0d079092..405ae063 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -4724,7 +4724,7 @@ class EmployeeRestController extends AdminController try { $file_id = $this->request->getGet('id') ?? $id; - $client_id = $this->request->getGet('client_id') ?? null; + $client_id = $this->request->getGet('cliend_id') ?? null; $client_data = []; if(!empty($client_id)){