diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index 8728cb8d..c5e0c06e 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -40,15 +40,17 @@ class Customer extends BaseController if ($id === '0') { $data['page_name'] = 'Add Customer'; $data['customer'] = []; + $data['customer_billing'] = []; + $data['customer_shipping'] = []; } else if ($id !== '0') { $data['page_name'] = 'Edit Customer'; $model = new CustomerModel(); $model->setTable('customers'); $edit_user_details = $model->where(['customer_id' => $id])->first(); - $data['customer'] = $edit_user_details; + $data['customer_billing'] = $this->get_customer_address($id,1); + $data['customer_shipping'] = $this->get_customer_address($id,2); } - $data['session_bid'] = $session_bid; $this->render_page('customer_form', $data); } @@ -59,7 +61,7 @@ class Customer extends BaseController helper('session'); $session_bid = get_business_id(); $session_uid = get_logged_user_id(); - $model = new CustomerModel(); + $model = new CustomerModel(); $data = [ 'first_name' => $this->request->getPost('cfname'), 'last_name' => $this->request->getPost('csname'), @@ -76,27 +78,88 @@ class Customer extends BaseController '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 - $isactive = $this->request->getPost('isactivce'); + $isactive = $this->request->getPost('isactive'); $data['isactive'] = ($isactive == 'on') ? 1 : 0; $data['updated_by'] = $session_uid; - $model->update($customer_id, $data); + // $model->update($customer_id, $data); + $FK_CID = $customer_id; // for Customer ADDRESS Table } + $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(); + + // Compare Address Input Field PKIDS With Existing PKIDS + // IF Any Missing value Means that values are Inactive here.... + if(!empty($CAid)){ + $filteringAddressIds = []; + for ($y = 0; $y < count($getAddressdetails); $y++) { + $filteringAddressIds[$y] = $getAddressdetails[$y]['customer_address_id']; + } + 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); + } + } + } + $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']; + $count = count($CAid); + $address_array = []; + 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'] = ""; + $address_array[$x]['email'] = $requestData['cmail']; + $address_array[$x]['mobile_no'] = $requestData['cmobile']; + $address_array[$x]['address_1'] = $baddress1[$x]; + $address_array[$x]['address_2'] = $baddress2[$x]; + $address_array[$x]['city'] = $bcity[$x]; + $address_array[$x]['state'] = $bstate[$x]; + $address_array[$x]['country'] = $bcountry[$x]; + $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); + } + return $statement; + } + ## For delete the customer details (Which means inactive the details) public function delete_customer($id) { @@ -121,6 +184,13 @@ class Customer extends BaseController return redirect()->route('customer_list'); } + public function get_customer_address($id,$type){ + $model = new CustomerModel(); + $model->setTable('customer_addresses'); + $where = ['isactive' => 1, 'customer_id' => (int)$id,'address_type'=>(int)$type]; + $address_details = $model->where($where)->findAll(); + return $address_details; + } ## ApiIntegration For Customer. public function apiintegration() { diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 218308e7..0cbf639b 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -1,17 +1,28 @@ 1, 'business_id' => (int)$session_bid]; + } else if (!empty($session_role) && $session_role !== "admin") { + $where = ['isactive != ' => NULL]; + } else { + $where = []; + } + $model = new HomeModel(); + $data['customer'] = $model->customers($where); + $data['customer']['label'] = "Customer"; $this->render_page('dashboard', $data); - } } diff --git a/app/Models/CustomerModel.php b/app/Models/CustomerModel.php index 71b07cc2..780986b6 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','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']; public function insertCustomer($data) { @@ -25,5 +25,30 @@ class CustomerModel extends Model $this->db->table('customer_addresses')->insertBatch($addressesDataArray); return $this->db->insertID(); // Note: insertID() might not be applicable for batch inserts } + + public function saveAddressDetails($data){ + $i = 0; + $statement = []; + foreach ($data as $row) { + $id = $row['customer_address_id']; + if($id != ''){ + unset($row['customer_address_id']); // Remove the id from the data to avoid updating it + 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"; + }else{ + + $this->db->table('customer_addresses')->insert($row); + $insertID = $this->db->insertID(); + $statement[$i] = "address - ".$insertID." Inserted"; + } + } + return $statement; + } + public function inactiveMissingAddressDetails($where,$missingValues){ + $dataToUpdate = ['isactive' => 0]; + $this->db->table('customer_addresses')->where($where)->whereIn('customer_address_id',$missingValues)->update($dataToUpdate); + } } ?> \ No newline at end of file diff --git a/app/Models/HomeModel.php b/app/Models/HomeModel.php new file mode 100644 index 00000000..1b774092 --- /dev/null +++ b/app/Models/HomeModel.php @@ -0,0 +1,27 @@ +db->table('customers'); + + $totalCustomers = $query->where($where)->countAll(); + $activeCustomers = $query->where($where)->countAllResults(); + + $where['DATE(created_on)'] = date('Y-m-d'); + $todayCustomers = $query->where($where)->countAllResults(); + + if ($totalCustomers > 0) { + $customer['percentage'] = ($activeCustomers / $totalCustomers) * 100; + } else { + $customer['percentage'] = 0; + } + + $customer['total'] = $totalCustomers; + $customer['active'] = $activeCustomers; + $customer['today'] = $todayCustomers; + return $customer; + } +} diff --git a/app/Views/customer_form.php b/app/Views/customer_form.php index eedbb3c2..31c223ab 100644 --- a/app/Views/customer_form.php +++ b/app/Views/customer_form.php @@ -7,85 +7,74 @@