vb_book/app/Controllers/Customer.php
2024-12-19 10:46:22 +05:30

684 lines
33 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Models\CustomerModel;
use App\Models\SubscriptionModel;
class Customer extends BaseController
{
## For Customer Listing
public function index()
{
helper('session');
if (is_session_active()) {
$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();
$data['page_name'] = 'Customer Details';
$data['customer'] = $CustomerModel->where($where)->orderBy('customer_id', 'DESC')->findAll();
$this->render_page('customer_list', $data);
} else {
return redirect()->to('login');
}
}
## To Load Customer Form (Add/Update)
public function new_customer($id)
{
helper('session');
$session_bid = get_business_id();
if ($id === '0') {
// Add Customer
$data['page_name'] = 'Add Customer Details';
$data['customer'] = [];
$data['customer_billing'] = [];
$data['customer_shipping'] = [];
$data['subscriptions'] = [];
$data['invoices'] = [];
} else if ($id !== '0') {
// Edit Customer
$data['page_name'] = 'Edit Customer Details';
// Load your Customer Model
$customerModel = new CustomerModel();
// Retrieve customer details by customer ID
$edit_user_details = $customerModel->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);
// Retrieve subscriber data based on the customer ID
$subscriberData = $customerModel->getSubscriberDataByCustomerId($id);
$data['subscriptions'] = $subscriberData;
// Fetch invoice data for the customer
$CustomerModel = new CustomerModel();
$invoices = $CustomerModel->getInvoicesByCustomerId($id);
$data['invoices'] = $invoices;
}
$data['session_bid'] = $session_bid;
$data['country_details'] = $this->get_country_details();
$data['state_details'] = $this->get_state_details();
$this->render_page('customer_form', $data);
}
## For inserting/updating details of customer
public function insert_customer()
{
$requestData = $this->request->getPost();
// print_r($requestData);die;
$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();
$dob = $this->request->getPost('dob');
$dob = (isset($dob) && $dob != "") ? $dob : NULL ;
$data = [
'first_name' => $this->request->getPost('cfname'),
'last_name' => $this->request->getPost('csname'),
'mobile_no' => $this->request->getPost('cmobile'),
'email' => $this->request->getPost('cmail'),
'date_of_birth' => $dob,
'type' => "customer",
'mode' => "Offline",
'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;
if ($model->insert($data)) {
session()->setFlashdata('success', 'Customer successfully created.');
$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 successfully updated');
$this->logger->info("Customer: has been updated successfully. Updated Customer ID = " . $customer_id_for_addresses);
} else {
session()->setFlashdata('error', 'Customer updation failed. Please try again.');
$this->logger->error("Customer: Err Failed to update ID =" . $customer_id_for_addresses);
}
}
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());
}
return redirect()->route('customer_list');
}
## 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();
## 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++) {
$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);
$this->logger->info("Customer: Inactived Missing Addresses Count = " . count($missingValues)." That Primary Addresses ID = ".implode(",",$CAid));
}
}
}
$baddress1 = $requestData[$letteringflag . 'address1'];
$baddress2 = $requestData[$letteringflag . 'address2'];
$bcountry = $requestData[$letteringflag . 'country'];
$bcity = $requestData[$letteringflag . 'city'];
$binputstate = $requestData[$letteringflag . 'istate'];
$bdropdownstate = $requestData[$letteringflag . 'dstate'];
$bzip = $requestData[$letteringflag . 'zip'];
$count = count($CAid);
$address_array = [];
if ($count > 0) {
for ($x = 0; $x < $count; $x++) {
$saddress1_name = $requestData['saddress1_name'][$x];
$address_array[$x]['first_name'] = $addressType == 1? $requestData['cfname']: $saddress1_name;
$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'] = $bcountry[$x] === 'IN' ? $bdropdownstate[$x] : $binputstate[$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 = !empty($address_array) ? $model->saveAddressDetails($address_array) : ["No data found For addresses"];
$this->logger->info("Customer: Address Final message = " . implode(",",$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)
{
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', 'Customer successfully deleted.');
$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());
}
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;
}
public function get_country_details()
{
$model = new CustomerModel();
$model->setTable('countries');
$country_details = $model->orderBy('country_id', 'ASC')->findAll();
return $country_details;
}
public function get_country_list()
{
$model = new CustomerModel();
$model->setTable('countries');
$country_details = $model->orderBy('country_id', 'ASC')->findAll();
return json_encode($country_details);
}
public function get_state_details()
{
$model = new CustomerModel();
$model->setTable('states');
$state_details = $model->orderBy('state_id', 'ASC')->findAll();
return $state_details;
}
public function customer_group(){
$data['page_name'] = 'Customer Group Details';
$model = new CustomerModel();
$data['customer_group'] = $model->getGroupDetails();
$this->render_page('customer_group', $data);
}
public function view_customer_group($id){
helper('session');
$model = new CustomerModel();
$session_bid = get_business_id();
$data['type_values'] = $model->getType();
$data['mode_values'] = $model->getMode();
$data['category_values'] = $model->getCategory();
$data['field'] = [
['value'=>'','fieldflag'=>1,'text'=> 'Choose the Field','disable' => false],
['value'=>'C.type','fieldflag'=>3,'text'=> 'Type','disable' => false],
['value'=>'CA.city','fieldflag'=>1,'text'=> 'City','disable' => false],
// ['value'=>'CA.state','fieldflag'=>1,'text'=> 'State','disable' => false],
['value'=>'CA.postal_code','fieldflag'=>1,'text'=> 'Postal Code','disable' => false],
['value'=>'C.mode','fieldflag'=>3,'text'=> 'Mode','disable' => false],
['value'=>'CM.name','fieldflag'=>3,'text'=> 'Category','disable' => false],
['value'=>'I.invoice_date','fieldflag'=>2,'text'=> 'Invoice Date','disable' => false],
['value'=>'S.to_subscription','fieldflag'=>2,'text'=> 'Expiry Date','disable' => false]];
$data['operator'] = [
'' => 'Choose the operator',
'equal' => 'Equal to (=)',
'not equal' => 'Not Equal (!=)',
'like' => 'Like (%)',
'greater than' => 'Greater than (>)',
'greater than or equal to' => 'Greater than or Equal to (>=)',
'less than' => 'Less than (<)',
'less than or equal to' => 'Less than or Equal to (<=)',
'contain' => 'Contain (in)',
'not contain' => 'Not Contain (not in)',
'between' => 'Between',
'is null' => 'Is NULL',
'is not null' => 'Is Not NULL',
];
$data['customer_group'] = [];
if ($id === '0') {
// Add Customer
$data['page_name'] = 'Add Customer Group';
} else if ($id !== '0') {
// Edit Customer
$data['page_name'] = 'Edit Customer Group';
// Load your Customer Model
$model = new CustomerModel();
$model->setTable('customer_groups');
$where = ['group_id'=>$id];
$result = $model->where($where)->findAll();
if (!empty($result)) {
$data['customer_group']['groupname'] = $result[0]['group_name'];
$data['customer_group']['column'] = $result[0]['column'] ? unserialize($result[0]['column']) : [];
$data['customer_group']['operator'] = $result[0]['operator'] ? unserialize($result[0]['operator']) : [];
$data['customer_group']['values'] = $result[0]['value'] ? unserialize($result[0]['value']) : [];
$data['customer_group']['group_id'] = $result[0]['group_id'];
$data['customer_group']['isactive'] = $result[0]['isactive'];
}
}
$this->render_page('customer_group_form', $data);
}
public function preview_customer_group($id){
try {
helper('session');
$model = new CustomerModel();
$model->setTable('customer_groups');
$where = ['group_id'=>$id];
$result = $model->where($where)->findAll();
$db = \Config\Database::connect();
$sql = (string)$result[0]['group_query'];
if($sql){
$query = $db->query($sql);
$results = $query->getResultArray();
return $this->response->setJSON($results);
}else{
return $this->response->setJSON([]);
}
} catch (\Exception $e) {
$this->logger->error("Customer Group: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
}
}
public function delete_customer_group($id){
try {
helper('session');
$session_uid = get_logged_user_id();
$model = new CustomerModel();
$model->setTable('customer_groups');
$where = ['isactive' => 1,'group_id'=>(int)$id];
$result = $model->where($where)->findAll();
if ($result) {
$this->logger->Info("Customer Group: Going to Inactive ID = ".$id);
$data = ['isactive' => 0 , 'updated_by' => $session_uid,'group_id'=>(int)$id];
$statement = $model->saveGroupDetails($data);// just updating Inactive status only.
if ($statement['success']) {
session()->setFlashdata('success', $statement['success'] ? 'Deleted successfully.' : "" );
$this->logger->info($statement['log']);
} else if ($statement['error']) {
session()->setFlashdata('error', $statement['error']);
$this->logger->error($statement['log']);
}else{
throw new \Exception("Customer Group: Err Occur on data the Customer Group Inactive");
}
}
} catch (\Exception $e) {
$this->logger->error("Customer Group: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
}
return redirect()->route('customer_group');
}
public function insert_customer_group(){
try {
$this->logger->info("Customer Group: Inserting/Updating Details");
helper('session');
$session_uid = get_logged_user_id();
$request_data = $this->request->getPost();
$string_flag = $request_data['string_flag'];
// $this->logger->info("Customer Group: Request data = ".json_encode($request_data));
$model = new CustomerModel();
// Array ( [groupname] => nil [column] => Array ( [0] => city [1] => state ) [operator] => Array ( [0] => not contain [1] => not equal ) [values] => Array ( [0] => 2 [1] => 3 ) )
$data = [
'group_name' => $request_data['groupname'],
'column' => serialize($request_data['column']),
'operator' => serialize($request_data['operator']),
'value' => serialize($request_data['values'])
];
// Initialize an empty array to store the conditions
$conditionStrings = array();
// Loop through the elements and build the conditions
for ($i = 0; $i < count($request_data['column']); $i++) {
$column = $request_data['column'][$i];
$operator = $request_data['operator'][$i];
$value = $request_data['values'][$i];
// Handle different operators and build corresponding conditions
switch ($operator) {
case 'equal':
$conditionStrings[] = "LOWER($column) = LOWER('$value') ";
break;
case 'not equal':
$conditionStrings[] = "LOWER($column) <> LOWER('$value') ";
break;
case 'like':
$conditionStrings[] = "LOWER($column) LIKE '%$value%'";
break;
case 'contain':
case 'in':
$values = explode(',', $value);
$conditionStrings[] = "LOWER($column) IN ('" . implode("', '", $values) . "')";
break;
case 'not contain':
case 'not in':
$values = explode(',', $value);
$conditionStrings[] = "LOWER($column) NOT IN ('" . implode("', '", $values) . "')";
break;
case 'between':
$dates = explode(',', $value);
$conditionStrings[] = "$column BETWEEN '$dates[0]' AND '$dates[1]'";
break;
case 'greater than':
$conditionStrings[] = "$column > $value";
break;
case 'greater than or equal to':
$conditionStrings[] = "$column >= $value";
break;
case 'less than':
$conditionStrings[] = "$column < $value";
break;
case 'less than or equal to':
$conditionStrings[] = "$column <= $value";
break;
case 'is null':
$conditionStrings[] = "$column IS NULL";
break;
case 'is not null':
$conditionStrings[] = "$column IS NOT NULL";
break;
}
}
if(empty($conditionStrings)){
throw new \Exception("can't able to save because Field Details Not Founded");
}
// Join the conditions with 'AND'
$whereCondition = implode(' AND ', $conditionStrings);
// echo "*************** whereCondition =>>> ";
// echo $whereCondition;
// If a groupname is specified, include it in the condition
// if ($conditionsArray['groupname'] !== 'nil') {
// $whereCondition = "($whereCondition) AND groupname = '{$conditionsArray['groupname']}'";
// }
// Now you can use $whereCondition in your SQL query
$db = \Config\Database::connect();
// $sql = "SELECT customer_id,CONCAT(first_name,'',last_name) as customer_name,email,mobile_no FROM customers WHERE ".$whereCondition;
$sql = "SELECT CA.city,CA.state,I.invoice_date,C.customer_id,CONCAT(C.first_name,' ',C.last_name) as customer_name,C.email,C.mobile_no ,S.to_subscription as due_date, C.isactive ,count(I.invoice_id) as invoice_count_raised_by_customer, S.scheme_id , BK.title as scheme_name ,CM.name as scheme_type_name,
DATE_FORMAT(S.to_subscription, '%d/%m/%Y') AS formatted_due_date,
DATE_FORMAT(I.invoice_date, '%d/%m/%Y') AS formatted_invoice_date
FROM customers as C
LEFT JOIN invoice I on I.customer_id = C.customer_id and I.isactive = 1
LEFT JOIN customer_addresses CA on CA.customer_id = C.customer_id and CA.address_type = 1 and CA.isactive = 1
LEFT JOIN subscription S on S.customer_id = C.customer_id
LEFT JOIN book_categories BC on BC.book_id = S.scheme_id
LEFT JOIN books BK on BK.book_id = S.scheme_id
LEFT JOIN category CM on CM.id = BC.category_id
WHERE C.isactive = 1 AND ".$whereCondition." GROUP BY C.customer_id";
$this->logger->info("Customer Group: SQL = ".$sql);
// Execute the query
$query = $db->query($sql);
// Get the result
$results = $query->getResult();
$this->logger->info("Customer Group: ".json_encode($results));
$group_id = $request_data['group_id']; // Get the ID for update
$isactive = $group_id == "" ? 1 : (isset($request_data['isactive']) && $request_data['isactive'] == 'on' ? 1 : 0);
if($string_flag == "save"){
$data['group_query'] = $sql;
$data['group_id'] = $group_id;
$data['isactive'] = $isactive;
$data['created_by'] = $session_uid;
$data['updated_by'] = $session_uid;
if($group_id != ""){
$cmod = new CustomerModel();
$cmod->setTable('customer_groups');
$where = ['group_id'=>(int)$group_id];
$isactive_check = $cmod->where($where)->select('isactive')->first();
if($isactive_check['isactive'] == '0' && $isactive == '0'){
$statement['success'] = 'Customer Group already In-Active. can`t able to update.';
$statement['error'] = "";
$statement['log'] = "Customer Group: already inactive status.Can`t able to Updated Customer Group ID = " .$group_id ;
}else{
$statement = $model->saveGroupDetails($data);
}
}else{
$statement = $model->saveGroupDetails($data);
}
if ($statement['success']) {
session()->setFlashdata('success', $statement['success']);
$this->logger->info($statement['log']);
} else if ($statement['error']) {
session()->setFlashdata('error', $statement['error']);
$this->logger->error($statement['log']);
}else{
throw new \Exception("Customer Group: Err Occur on data the Save");
}
}else if($string_flag == "preview"){
return $this->response->setJSON($results);
}else{
throw new \Exception("Data Not Found");
}
} catch (\Exception $e) {
$this->logger->error("Customer Group: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
}
return redirect()->route('customer_group');
}
## For Ajax Call To Save Invoice Customer...
public function save_invoice_customer()
{
helper('session');
$session_bid = get_business_id();
$session_uid = get_logged_user_id();
$model = new CustomerModel();
$data = [
'first_name' => $this->request->getPost('cus_first_name'),
'last_name' => $this->request->getPost('cus_last_name') ? $this->request->getPost('cus_last_name') : '',
'mobile_no' => $this->request->getPost('cus_mobile_no'),
'email' => $this->request->getPost('cus_email'),
'date_of_birth' => NULL,
'type' => "customer",
'mode' => "Offline",
'business_id' => $session_bid,
'isactive' => 1,
'created_by' => $session_uid
];
if ($model->insert($data)) {
$customerId = $model->insertID();
$this->logger->info("Invoice Customer : has been added successfully. Inserted ID = " . $customerId);
// Store both billing and shipping addresses
$billingAddress = [
'customer_id' => $customerId,
'first_name' => $this->request->getPost('cus_first_name'),
'last_name' => $this->request->getPost('cus_last_name') ? $this->request->getPost('cus_last_name') : '',
'address_type' => 1, // Billing address type
'billing_address1' => $this->request->getPost('baddress1'),
'billing_address2' => $this->request->getPost('baddress2'),
'billing_city' => $this->request->getPost('bcity'),
'billing_state' => $this->request->getPost('bstate'),
'billing_country' => $this->request->getPost('bcountry'),
'billing_pincode' => $this->request->getPost('bzip')
];
$shippingAddress = [
'customer_id' => $customerId,
'first_name' => $this->request->getPost('cus_first_name'),
'last_name' => $this->request->getPost('cus_last_name') ? $this->request->getPost('cus_last_name') : '',
'address_type' => 2, // Shipping address type
'shipping_address1' => $this->request->getPost('saddress1'),
'shipping_address2' => $this->request->getPost('saddress2'),
'shipping_city' => $this->request->getPost('scity'),
'shipping_state' => $this->request->getPost('sstate'),
'shipping_country' => $this->request->getPost('scountry'),
'shipping_pincode' => $this->request->getPost('szip')
];
// Call the method to store both addresses
$result = $model->storeCustomerAddresses($billingAddress, $shippingAddress);
if($customerId){
$cus_first_name = $this->request->getPost('cus_first_name');
$cus_last_name = $this->request->getPost('cus_last_name');
$name = $cus_first_name;
if ($cus_last_name !== null) {
$name .= ' ' . $cus_last_name;
}
}
if ($customerId) {
// Success response
$results = ['status' => true, 'message' => 'Customer and addresses saved successfully', 'cus_id' => $customerId,'cus_name'=>$name,"billing_addr_id"=>$result['billing_addr_id'],"shipping_addr_id"=>$result['shipping_addr_id']];
} else {
// Error response
$results = ['status' => false, 'message' => 'Failed to save customer and addresses'];
}
} else {
$this->logger->error("Invoice Customer: Error could not be added. Please try again.");
$results = ['status' => false, 'message' => 'Customer could not be added'];
}
return $this->response->setJSON(['data' => $results]);
}
public function qucik_add_addresses(){
$billingData = [
'customer_id' => $this->request->getPost('customer_id'),
'first_name' => $this->request->getPost('first_name'),
'last_name' => $this->request->getPost('last_name'),
'billing_address1'=> $this->request->getPost('QB_address'),
'billing_address2'=> '',
'billing_country' => $this->request->getPost('QB_country'),
'billing_state' => $this->request->getPost('QB_state'),
'billing_city' => $this->request->getPost('QB_city'),
'billing_pincode' => $this->request->getPost('QB_zip'),
];
$shipping_address_name = $this->request->getPost('shipping_name');
$shippingData = [
'customer_id' => $this->request->getPost('customer_id'),
'first_name' => isset($shipping_address_name)?$shipping_address_name:$this->request->getPost('first_name'),
'last_name' => '',
'shipping_address1'=> $this->request->getPost('QS_address'),
'shipping_address2'=> '',
'shipping_country' => $this->request->getPost('QS_country'),
'shipping_state' => $this->request->getPost('QS_state'),
'shipping_city' => $this->request->getPost('QS_city'),
'shipping_pincode' => $this->request->getPost('QS_zip'),
];
$model = new CustomerModel();
$result = $model->storeCustomerAddresses($billingData, $shippingData);
if (!empty($result['billing_addr_id']) && !empty($result['shipping_addr_id'])) {
$results = ['status' => true, 'message' => 'New Address Saved Successfully', 'cus_id' =>$this->request->getPost('customer_id'),"billing_addr_id"=>$result['billing_addr_id'],"shipping_addr_id"=>$result['shipping_addr_id']];
} else {
$results = ['status' => false, 'message' => 'Failed to save New addresses'];
}
return $this->response->setJSON(['data' => $results]);
}
}