From f4c7fc7b54d213c46c06eb9546ff1a7a44320ba7 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Fri, 13 Oct 2023 12:32:02 +0530 Subject: [PATCH] Customer Confirmation done : ps --- app/Controllers/Customer.php | 236 ++++++++++++++++++++++------------- app/Models/CustomerModel.php | 7 +- app/Models/UsersModel.php | 2 +- app/Views/customer_list.php | 18 +++ 4 files changed, 168 insertions(+), 95 deletions(-) diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index 968af851..0a277396 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -16,8 +16,10 @@ class Customer extends BaseController $session_role = get_user_role(); $session_bid = get_business_id(); if (!empty($session_role) && $session_role !== "sadmin") { + $this->logger->info("Customer: Listing In admin role . BID = ".$session_bid); $where = ['isactive' => 1, 'business_id' => (int)$session_bid]; } else { + $this->logger->info("Customer: Listing In Super-admin role ."); $where = ['isactive != ' => NULL]; } $CustomerModel = new CustomerModel(); @@ -73,93 +75,123 @@ class Customer extends BaseController $data['session_bid'] = $session_bid; $this->render_page('customer_form', $data); } - + ## For inserting/updating details of customer public function insert_customer() { - helper('session'); - $session_bid = get_business_id(); - $session_uid = get_logged_user_id(); - $session_role = get_user_role(); - $model = new CustomerModel(); - $data = [ - 'first_name' => $this->request->getPost('cfname'), - 'last_name' => $this->request->getPost('csname'), - 'mobile_no' => $this->request->getPost('cmobile'), - 'address' => $this->request->getPost('address'), - 'city' => $this->request->getPost('ccity'), - 'email' => $this->request->getPost('cmail'), - 'state' => $this->request->getPost('cstate'), - 'postal_code' => $this->request->getPost('czip'), - 'country' => $this->request->getPost('ccountry'), - 'date_of_birth' => $this->request->getPost('dob'), - 'gender' => $this->request->getPost('gen'), - 'profile_picture' => $this->request->getPost('cfile'), - 'mode' => $this->request->getPost('cmode'), - 'business_id' => $this->request->getPost('business_id') - ]; + $this->logger->info("Customer: Inserting/Updating Details"); + try { + helper('session'); + $session_bid = get_business_id(); + $session_uid = get_logged_user_id(); + $session_role = get_user_role(); + $model = new CustomerModel(); + $data = [ + 'first_name' => $this->request->getPost('cfname'), + 'last_name' => $this->request->getPost('csname'), + 'mobile_no' => $this->request->getPost('cmobile'), + 'address' => $this->request->getPost('address'), + 'city' => $this->request->getPost('ccity'), + 'email' => $this->request->getPost('cmail'), + 'state' => $this->request->getPost('cstate'), + 'postal_code' => $this->request->getPost('czip'), + 'country' => $this->request->getPost('ccountry'), + 'date_of_birth' => $this->request->getPost('dob'), + 'gender' => $this->request->getPost('gen'), + 'profile_picture' => $this->request->getPost('cfile'), + 'mode' => $this->request->getPost('cmode'), + 'business_id' => $this->request->getPost('business_id') + ]; - $customer_id = $this->request->getPost('customer_id'); // Get the business ID for update - if (empty($customer_id)) { - // It's an insert operation - $data['isactive'] = 1; - $data['created_by'] = $session_uid; - //print_r($data);die; - $model->insert($data); - $FK_CID = $model->insertID(); // for Customer ADDRESS Table - } else { - // It's an update operation - if ($session_role !== 'manager'){ - $isactive = $this->request->getPost('isactive'); - $data['isactive'] = ($isactive == 'on') ? 1 : 0; + $customer_id = $this->request->getPost('customer_id'); // Get the business ID for update + if (empty($customer_id)) { + // It's an insert operation + $data['isactive'] = 1; + $data['created_by'] = $session_uid; + if ($model->insert($data)) { + session()->setFlashdata('success', 'Customer has been added successfully.'); + $this->logger->info("Customer : has been added successfully. Inserted ID = " . $model->insertID()); + $customer_id_for_addresses = $model->insertID();// for Customer ADDRESS Table + } else { + session()->setFlashdata('error', 'Customer could not be added. Please try again..'); + $this->logger->error("Customer: Err could not be added. Please try again."); + $customer_id_for_addresses = ""; + } + } else { + // It's an update operation + if ($session_role !== 'manager') { + $isactive = $this->request->getPost('isactive'); + $data['isactive'] = ($isactive == 'on') ? 1 : 0; + } + $data['updated_by'] = $session_uid; + $customer_id_for_addresses = $customer_id; // for Customer ADDRESS Table + if ($model->update($customer_id, $data)) { + session()->setFlashdata('success', 'Customer has been updated successfully.'); + $this->logger->info("Customer: has been updated successfully. Updated Customer ID = " . $customer_id_for_addresses); + } else { + session()->setFlashdata('error', 'Customer update failed. Please try again.'); + $this->logger->error("Customer: Err Failed to update ID =" . $customer_id_for_addresses); + } } - $data['updated_by'] = $session_uid; - // $model->update($customer_id, $data); - $FK_CID = $customer_id; // for Customer ADDRESS Table + + if($customer_id_for_addresses != ""){ + $this->logger->info("Customer: i got customer id for addresses = " . $customer_id_for_addresses); + $requestData = $this->request->getPost(); + $bill_addr = $this->save_customer_addresses($customer_id_for_addresses, $requestData, 'b'); + $this->logger->info("Customer: bill addr final message = " .implode(" ",$bill_addr)); + $ship_addr = $this->save_customer_addresses($customer_id_for_addresses, $requestData, 's'); + $this->logger->info("Customer: Ship addr final message = " . implode(" ",$ship_addr)); + } + + } catch (\Exception $e) { + $this->logger->error("Customer : Err Occur =" . $e->getMessage()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } - $requestData = $this->request->getPost(); - $this->save_customer_addresses($FK_CID,$requestData,'b'); - $this->save_customer_addresses($FK_CID,$requestData,'s'); return redirect()->route('customer_list'); } - public function save_customer_addresses($id,$requestData,$letteringflag){ - - $addressType = ($letteringflag == 'b') ? 1 : 2; - $getAddressdetails = $this->get_customer_address($id,$addressType); - $CAid = $requestData[$letteringflag.'customer_address_id']; // Available Address IDS in Form Fields. - $model = new CustomerModel(); + ## For Save the customer addresses details (Using Letting Flag and Customer ID) + public function save_customer_addresses($id, $requestData, $letteringflag) + { + try { + $addressType = ($letteringflag == 'b') ? 1 : 2; + $this->logger->info("Customer: Addresses Lettering Flag = " . $letteringflag . " Customer address type = " . $addressType); + $getAddressdetails = $this->get_customer_address($id, $addressType); + $CAid = $requestData[$letteringflag . 'customer_address_id']; // Available Address IDS in Form Fields. + $session_uid = get_logged_user_id(); + $model = new CustomerModel(); - // Compare Address Input Field PKIDS With Existing PKIDS - // IF Any Missing value Means that values are Inactive here.... - if(!empty($CAid)){ + ## IF Any Missing value Means that values are Inactive here.... + if(!empty($CAid)){ + $this->logger->info("Customer: Primary Addresses ID = ".implode(",",$CAid)); $filteringAddressIds = []; - for ($y = 0; $y < count($getAddressdetails); $y++) { + for ($y = 0; $y < count($getAddressdetails); $y++) { $filteringAddressIds[$y] = $getAddressdetails[$y]['customer_address_id']; } - if(!empty($filteringAddressIds)){ + if (!empty($filteringAddressIds)) { $A = $filteringAddressIds; $B = $CAid; - $missingValues = array_diff($A,$B); - if(!empty($missingValues)){ - $where = ['isactive'=>1,'customer_id'=>(int)$id,'address_type'=>$addressType]; - $model->inactiveMissingAddressDetails($where,$missingValues); + $missingValues = array_diff($A, $B); + if (!empty($missingValues)) { + $where = ['isactive' => 1, 'customer_id' => (int)$id, 'address_type' => $addressType]; + $model->inactiveMissingAddressDetails($where, $missingValues); + $this->logger->info("Customer: Inactived Missing Addresses Count = " . count($missingValues)." That Primary Addresses ID = ".implode(",",$CAid)); } } - } - $session_uid = get_logged_user_id(); - - $baddress1 = $requestData[$letteringflag.'address1']; - $baddress2 = $requestData[$letteringflag.'address2']; - $bcountry = $requestData[$letteringflag.'country']; - $bcity = $requestData[$letteringflag.'city']; - $bstate = $requestData[$letteringflag.'state']; - $bzip = $requestData[$letteringflag.'zip']; + } + + + $baddress1 = $requestData[$letteringflag . 'address1']; + $baddress2 = $requestData[$letteringflag . 'address2']; + $bcountry = $requestData[$letteringflag . 'country']; + $bcity = $requestData[$letteringflag . 'city']; + $bstate = $requestData[$letteringflag . 'state']; + $bzip = $requestData[$letteringflag . 'zip']; $count = count($CAid); $address_array = []; - if($count > 0){ - for ($x = 0; $x < $count; $x++) { + if ($count > 0) { + for ($x = 0; $x < $count; $x++) { $address_array[$x]['first_name'] = $requestData['cfname']; $address_array[$x]['last_name'] = $requestData['csname']; // $address_array[$x]['company'] = ""; @@ -173,36 +205,60 @@ class Customer extends BaseController $address_array[$x]['postal_code'] = $bzip[$x]; $address_array[$x]['customer_id'] = $id; $address_array[$x]['address_type'] = $addressType; - $address_array[$x]['created_by'] = $session_uid; - $address_array[$x]['updated_by'] = $CAid[$x] ? $session_uid : null; - $address_array[$x]['customer_address_id'] = $CAid[$x]; - } - $statement = $model->saveAddressDetails($address_array); + $address_array[$x]['created_by'] = $session_uid; + $address_array[$x]['updated_by'] = $CAid[$x] ? $session_uid : null; + $address_array[$x]['customer_address_id'] = $CAid[$x]; + } + $statement = !empty($address_array) ? $model->saveAddressDetails($address_array) : ["No data found For addresses"]; + $this->logger->info("Customer: Address Final message = " . implode(",",$statement)); } - return $statement; + } catch (\Exception $e) { + $this->logger->error("Customer: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine()); + $statement = ['Message: ' . $e->getMessage()]; + } + return $statement; } ## For delete the customer details (Which means inactive the details) public function delete_customer($id) { - $CustomerModel = new CustomerModel(); - - // Check if the business ID exists - $existingCustomer = $CustomerModel->find($id); - if (!$existingCustomer) { - // return redirect()->to(base_url('Business/index'))->with('error', 'Business not found.'); - return redirect()->route('customer_list'); + try { + helper('session'); + $session_uid = get_logged_user_id(); + $CustomerModel = new CustomerModel(); + $where = ['customer_id' => (int)$id, 'isactive =' => 1]; + // Check if the business ID exists + $existingCustomer = $CustomerModel->where($where)->find($id); + if ($existingCustomer) { + $this->logger->Info("Customer: Going to Inactive ID = ".$id); + $data = ['isactive' => 0 , 'updated_by' => $session_uid]; + // Delete the business record + if ($CustomerModel->update($id, $data)) { + $billAddressdetails = $this->get_customer_address($id, 1); + if(!empty($billAddressdetails)){ + for ($y = 0; $y < count($billAddressdetails); $y++) { + $billAddressIds[$y] = $billAddressdetails[$y]['customer_address_id']; + } + $CustomerModel->inactiveMissingAddressDetails($where, $billAddressIds); + } + $shipAddressdetails = $this->get_customer_address($id, 2); + if(!empty($shipAddressdetails)){ + for ($z = 0; $z < count($shipAddressdetails); $z++) { + $shipAddressIds[$z] = $shipAddressdetails[$z]['customer_address_id']; + } + $CustomerModel->inactiveMissingAddressDetails($where, $shipAddressIds); + } + session()->setFlashdata('success', 'Deleted successfully.'); + $this->logger->info("Customer: has been Inactived successfully. Inactived ID = " . $id); + } else { + $this->logger->error("Customer: Not able to Inactive ID =" . $id); + throw new \Exception("Data Not able to Deleted"); + } + } + } catch (\Exception $e) { + $this->logger->error("Customer: Err Occur = " . $e->getMessage()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } - - helper('session'); - $session_uid = get_logged_user_id(); - // Delete the business record - $data['isactive'] = 0; - $data['updated_by'] = $session_uid; - $CustomerModel->update($id, $data); - //$BusinessModel->delete($id); - - // return redirect()->to(base_url('Business/index'))->with('success', 'Business deleted successfully.'); return redirect()->route('customer_list'); } diff --git a/app/Models/CustomerModel.php b/app/Models/CustomerModel.php index e3c07561..1b9f76a6 100644 --- a/app/Models/CustomerModel.php +++ b/app/Models/CustomerModel.php @@ -9,7 +9,7 @@ class CustomerModel extends Model { protected $table = 'customers'; protected $primaryKey = 'customer_id '; - protected $allowedFields = ['customer_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','profile_picture','date_of_birth','gender','mode','type','isactive','business_id']; + protected $allowedFields = ['customer_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','profile_picture','date_of_birth','gender','mode','type','isactive','business_id','created_by','updated_by']; public function insertCustomer($data) { @@ -73,15 +73,14 @@ class CustomerModel extends Model unset($row['created_by']); // bcoz here data Updating here. $this->db->table('customer_addresses')->where('customer_address_id', $id)->update($row);// Update the row with the specified id $affectedRows = $this->db->affectedRows(); - $statement[$i] = "address - ".$id." ".$affectedRows ? " Updated":" Not Updated"; + $statement[$i] = "address - ".$id." ". ($affectedRows ? " Updated":" Nothing to Updated"); }else{ - $this->db->table('customer_addresses')->insert($row); $insertID = $this->db->insertID(); $statement[$i] = "address - ".$insertID." Inserted"; } + $i++; } - return $statement; } diff --git a/app/Models/UsersModel.php b/app/Models/UsersModel.php index a35b3209..6c467271 100644 --- a/app/Models/UsersModel.php +++ b/app/Models/UsersModel.php @@ -5,7 +5,7 @@ class UsersModel extends Model { protected $table = 'users'; protected $primaryKey = 'user_id'; - protected $allowedFields = ['user_id','user_name','email','first_name','last_name','password','mobile_no','date_of_birth','address','gender','profile_picture','city','state','postal_code','country','role','isactive','business_id']; + protected $allowedFields = ['user_id','user_name','email','first_name','last_name','password','mobile_no','date_of_birth','address','gender','profile_picture','city','state','postal_code','country','role','isactive','business_id','created_by','updated_by']; public function saveData($data, $id = null) { diff --git a/app/Views/customer_list.php b/app/Views/customer_list.php index 0f4a3a04..036574f3 100644 --- a/app/Views/customer_list.php +++ b/app/Views/customer_list.php @@ -7,6 +7,24 @@

+ getFlashdata('success') || session()->getFlashdata('error')) : ?> + getFlashdata('success')) : ?> + + + getFlashdata('error')) : ?> + + +