diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 6646aa6..c43b256 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -877,7 +877,29 @@ class ClientController extends AdminController } $data['created_by'] = get_session_userid(); + + // before updating check if post_branch_id is already existing in the current db + + if(isset($data['post_branch_id']) && !empty($data['post_branch_id'])) + { + $existing_pre_branch = $this->clientBranchModel + ->where('post_branch_id',$data['post_branch_id']) + // ->where('id !=',$pre_branch_id) + ->where('is_active',1) + ->first(); + + if($existing_pre_branch) + { + return $this->respond([ + 'status' => false, + 'code' => 409, + 'message' => 'The pre branch id '.$data['pre_branch_id'].' is already mapped with another branch. Please check.', + ], 409); + } + } + $insert = $this->clientBranchModel->insert($data); + $pre_branch_id = $insert; if ($insert) { $level_contact_data = $this->request->getPost('level_contect_data'); @@ -885,6 +907,16 @@ class ClientController extends AdminController $this->saveLevelContacts($level_contact_data, $insert); } + if($pre_branch_id && isset($data['post_branch_id']) && !empty($data['post_branch_id'])) + { + // need to update the client_branch in the pre + $result = $this->updatePostClientBranch($data['post_branch_id'],$pre_branch_id , "create"); + + log_message('error','Post client_branch update result for post_branch_id '.$data['post_branch_id'].' and pre_branch_id '.$pre_branch_id.' is '.json_encode($result)); + } + + + // if ($insert) { // for ($i = 0; $i < count($this->request->getPost('name')); $i++) { // // Prepare data to insert @@ -925,7 +957,11 @@ class ClientController extends AdminController $this->myLogger->logme('error', 'Client branch EDIT function called'); $id = $this->request->getPost('branch_id_primarykey'); $client_id = $this->request->getPost('client_id'); + $post_branch_id = $this->request->getPost('post_branch_id') ?? ""; + $data = $this->request->getPost(); + $data['post_branch_id'] = $post_branch_id; + $units = $this->request->getPost('units'); $emp_unit_count = 0; @@ -984,7 +1020,44 @@ class ClientController extends AdminController } $data['updated_by'] = get_session_userid(); + + $pre_branch_id = $id; + + // before updating check if post_branch_id is already existing in the current db + + if(isset($data['post_branch_id']) && !empty($data['post_branch_id'])) + { + $existing_pre_branch = $this->clientBranchModel + ->where('post_branch_id',$data['post_branch_id']) + ->where('id !=',$pre_branch_id) + ->where('is_active',1) + ->first(); + + if($existing_pre_branch) + { + return $this->respond([ + 'status' => false, + 'code' => 409, + 'message' => 'The pre branch id '.$data['pre_branch_id'].' is already mapped with another branch. Please check.', + ], 409); + } + } + + + $insert = $this->clientBranchModel->update($id, $data); + + + + if($pre_branch_id && isset($data['post_branch_id']) && !empty($data['post_branch_id'])) + { + // need to update the client_branch in the post + $result = $this->updatePostClientBranch($data['post_branch_id'],$pre_branch_id , "update"); + + log_message('error','Post client_branch update result for post_branch_id '.$data['post_branch_id'].' and pre_branch_id '.$pre_branch_id.' is '.json_encode($result)); + } + + $this->myLogger->logme('error', 'Client branch EDITED by {data}', ['data' => get_session_userid()]); if ($insert) { @@ -5209,6 +5282,29 @@ class ClientController extends AdminController } + public function updatePostClientBranch($post_branch_id,$pre_branch_id , $operation){ + + + + + $postDB = \Config\Database::connect('postDB'); + + if($operation != 'create'){ + + $builder = $postDB->table('client_branch'); + $builder->where('pre_branch_id', $pre_branch_id); + $builder->update(['pre_branch_id' => null]); + } + + $builder = $postDB->table('client_branch'); + $builder->where('id', $post_branch_id); + $builder->update(['pre_branch_id' => $pre_branch_id]); + + return true; + + } + + diff --git a/app/Views/client_branch.php b/app/Views/client_branch.php index 0dc28eb..4450e93 100755 --- a/app/Views/client_branch.php +++ b/app/Views/client_branch.php @@ -91,10 +91,14 @@ + + + + @@ -379,7 +383,7 @@ $("#branch_form").submit(function(event) { event.preventDefault(); branch_PrimaryKey = $('#client_id_branch').val(); - console.log('branch_PrimaryKey', branch_PrimaryKey) + console.log('branch_PrimaryKey', branch_PrimaryKey); if (branch_PrimaryKey === '') { toastr.error('Client is required', 'Error'); @@ -488,6 +492,10 @@ $("#branch_form").submit(function(event) { $('.loader').fadeOut(); $('.loader-mask').delay(350).fadeOut('slow'); console.log('Something Wrong!', 'warning'); + if(xhr.status == 409){ + alert('The Current Branch is Already Existing..!!'); + } + }, 300); } });