From 2ce29a61d4f2492b5e16c6deff8378ede23bc7a6 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Wed, 12 Jun 2024 13:06:48 +0000 Subject: [PATCH] FIX_Invoice Address Add : ps --- app/Config/Routes.php | 2 +- app/Controllers/Customer.php | 49 +++---- app/Views/invoice_form.php | 274 +++++++++++++++++++++-------------- 3 files changed, 186 insertions(+), 139 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 5f3d234a..21e63eeb 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -178,7 +178,7 @@ $routes->post('api/(:any)', 'ApiIntegration::api_integration/$1'); # Get Country List $routes->get('get_country', 'Customer::get_country_list'); -$routes->post('save_address_from_model', 'Customer::save_address_from_model'); +$routes->post('qucik_add_addresses', 'Customer::qucik_add_addresses'); diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index 585212bf..accc455c 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -633,45 +633,40 @@ class Customer extends BaseController } - public function save_address_from_model(){ - // print_r($this->request->getPost());die; - - - $requestDataBilling = $this->request->getPost('billing'); - $requestDataShipping = $this->request->getPost('shipping'); - - $customer_id_for_addresses = $this->request->getPost('customer_id'); - - // $bill_addr = $this->save_customer_addresses_model($customer_id_for_addresses, $requestDataBilling, 'b'); - // $ship_addr = $this->save_customer_addresses_model($customer_id_for_addresses, $requestDataBilling, 's'); + 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'), - 'address_1'=> $requestDataBilling['address'], - 'address_2'=> '', - 'country' => $requestDataBilling['country'], - 'state' => $requestDataBilling['state'], - 'city' => $requestDataBilling['state'], - 'postal_code' => $requestDataBilling['postalCode'], + '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'), ]; - $shippingData = [ + 'customer_id' => $this->request->getPost('customer_id'), 'first_name' => $this->request->getPost('first_name'), 'last_name' => $this->request->getPost('last_name'), - 'address_1'=> $requestDataShipping['address'], - 'address_2'=> '', - 'country' => $requestDataShipping['country'], - 'state' => $requestDataShipping['state'], - 'city' => $requestDataShipping['state'], - 'postal_code' => $requestDataShipping['postalCode'], + '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(); - $insertedId = $model->updateAddress($customer_id_for_addresses, $billingData, 'b'); - $insertedId = $model->updateAddress($customer_id_for_addresses, $shippingData, 's'); + $result = $model->storeCustomerAddresses($billingData, $shippingData); + if (!empty($result['billing_addr_id']) && !empty($result['shipping_addr_id'])) { + $results = ['status' => true, 'message' => 'New addresses 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]); } diff --git a/app/Views/invoice_form.php b/app/Views/invoice_form.php index 9bac2ba8..bd5e2458 100644 --- a/app/Views/invoice_form.php +++ b/app/Views/invoice_form.php @@ -19,7 +19,7 @@


" id="myForm"> - +
@@ -74,7 +74,7 @@
- +
@@ -648,34 +648,34 @@
- - + +
- - +
- - + +
- - + +
- - + +
@@ -690,39 +690,39 @@
- - + +
- - +
- - + +
- - + +
- - + +
- +
@@ -1129,8 +1129,8 @@ // Loop through the countries array $.each(countries, function(index, country) { // Append an '); - $('#shipping_country').append(''); + $('#quick_billing_country').append(''); + $('#quick_shipping_country').append(''); }); }, error: function() { alert("Error fetching address."); @@ -1588,8 +1588,7 @@ function saveInvoiceCustomer() { success: function(response) { console.log(response['data']); if (response['data']['status']) { - $('#customer_id_set').val(response['data']['cus_id']); - console.log($('#customer_id_set')); + // Handle success response alert(response['data']['message']); @@ -1929,108 +1928,161 @@ function saveInvoiceCustomer() { $("#addressCopiedAsShippingModel").on("change", function() { if ($(this).is(":checked")) { - var customerAddress1 = $("#billing_address").val(); - var customerCountry = $("#billing_country").val(); - var customerState = $("#billing_state").val(); - var customerCity = $("#billing_city").val(); - var customerPostalCode = $("#billing_postal_code").val(); + + var quick_customerAddress1 = $("#quick_billing_address").val(); + var quick_customerCountry = $("#quick_billing_country").val(); + var quick_customerState = $("#quick_billing_state").val(); + var quick_customerCity = $("#quick_billing_city").val(); + var quick_customerPostalCode = $("#quick_billing_postal_code").val(); - $("#shipping_address").val(customerAddress1).prop("readonly", false); - $("#shipping_country").val(customerCountry).prop("readonly", false); - $("#shipping_state").val(customerState).prop("readonly", false); - $("#shipping_city").val(customerCity).prop("readonly", false); - $("#shipping_postal_code").val(customerPostalCode).prop("readonly", false); + $("#quick_shipping_address").val(quick_customerAddress1).prop("readonly", false); + $("#quick_shipping_country").val(quick_customerCountry).prop("readonly", false); + $("#quick_shipping_state").val(quick_customerState).prop("readonly", false); + $("#quick_shipping_city").val(quick_customerCity).prop("readonly", false); + $("#quick_shipping_postal_code").val(quick_customerPostalCode).prop("readonly", false); } else { // Clear the billing address fields when the checkbox is unchecked - $("#shipping_address").val("").prop("readonly", false); - $("#shipping_country").val("").prop("readonly", false); - $("#shipping_state").val("").prop("readonly", false); - $("#shipping_city").val("").prop("readonly", false); - $("#shipping_postal_code").val("").prop("readonly", false); + $("#quick_shipping_address").val("").prop("readonly", false); + $("#quick_shipping_country").val("").prop("readonly", false); + $("#quick_shipping_state").val("").prop("readonly", false); + $("#quick_shipping_city").val("").prop("readonly", false); + $("#quick_shipping_postal_code").val("").prop("readonly", false); } }); - function submitAddress(params) { + function submitAddress() { $('#storeBillingAddress').val("").prop('readonly',false); $('#storeShippingAddress').val("").prop('readonly',false); + var cus_id = $('#customer_name').val(); + console.log("Qucik add Addresses - customer id = ",cus_id); var last_name = $("#last_name").val(); var first_name = $("#first_name").val(); - var bAddress = $("#billing_address").val(); - var bCountry = $("#billing_country").val(); - var bState = $("#billing_state").val(); - var bCity = $("#billing_city").val(); - var bPostalCode = $("#billing_postal_code").val(); - var sAddress = $("#shipping_address").val(); - var sCountry = $("#shipping_country").val(); - var sState = $("#shipping_state").val(); - var sCity = $("#shipping_city").val(); - var sPostalCode = $("#shipping_postal_code").val(); - - var cus_id = 0 ; - if ($('#customer_name').val()) { - cus_id = $('#customer_name').val(); - }else{ - cus_id = $('#customer_id_set').val(); + var QB_AddressLine = $("#quick_billing_address").val(); + var QB_Country = $("#quick_billing_country").val(); + var QB_State = $("#quick_billing_state").val(); + var QB_City = $("#quick_billing_city").val(); + var QB_PostalCode = $("#quick_billing_postal_code").val(); + var QS_AddressLine = $("#quick_shipping_address").val(); + var QS_Country = $("#quick_shipping_country").val(); + var QS_State = $("#quick_shipping_state").val(); + var QS_City = $("#quick_shipping_city").val(); + var QS_PostalCode = $("#quick_shipping_postal_code").val(); + + var isValid = true; + var errorMessage = ''; + if (cus_id === '') { + errorMessage += 'Please Select the Customer.\n'; + isValid = false; + } + if (QB_AddressLine === '') { + errorMessage += 'Billing address is required.\n'; + isValid = false; + } + if (QB_Country === '') { + errorMessage += 'Billing country is required.\n'; + isValid = false; + } + if (QB_State === '') { + errorMessage += 'Billing state is required.\n'; + isValid = false; + } + if (QB_City === '') { + errorMessage += 'Billing city is required.\n'; + isValid = false; + } + if (QB_PostalCode === '') { + errorMessage += 'Billing postal code is required.\n'; + isValid = false; + } + if (QS_AddressLine === '') { + errorMessage += 'Shipping address is required.\n'; + isValid = false; + } + if (QS_Country === '') { + errorMessage += 'Shipping country is required.\n'; + isValid = false; + } + if (QS_State === '') { + errorMessage += 'Shipping state is required.\n'; + isValid = false; + } + if (QS_City === '') { + errorMessage += 'Shipping city is required.\n'; + isValid = false; + } + if (QS_PostalCode === '') { + errorMessage += 'Shipping postal code is required.\n'; + isValid = false; } - console.log("customer_name",cus_id); - var formData = { - customer_id :cus_id, - last_name: last_name, - first_name: first_name, - billing: { - address: bAddress, - country: bCountry, - state: bState, - city: bCity, - postalCode: bPostalCode - }, - shipping: { - address: sAddress, - country: sCountry, - state: sState, - city: sCity, - postalCode: sPostalCode - } - }; - - $.ajax({ - type: "POST", - url: "", - data: formData, - success: function(response) { - // Handle success response - console.log("11111111111111111"); - console.log(response); - $('#billing_shipping_addresses_modal').modal('hide'); - - var billing_Address = bAddress + ','+bCity + ',' + bCountry + '-' +bPostalCode + ',' + bState + '.' ; - var shipping_Address = sAddress + ','+sCity + ',' + sCountry + '-' +sPostalCode + ',' + sState + '.' ; - - console.log(billing_Address); + if (!isValid) { + alert(errorMessage); + } else { + var formData = { + customer_id :cus_id, + last_name: last_name, + first_name: first_name, + QB_address: QB_AddressLine, + QB_country: QB_Country, + QB_state: QB_State, + QB_city: QB_City, + QB_zip: QB_PostalCode, + QS_address: QS_AddressLine, + QS_country: QS_Country, + QS_state: QS_State, + QS_city: QS_City, + QS_zip: QS_PostalCode + }; + + $.ajax({ + type: "POST", + url: "", + data: formData, + success: function(response) { + console.log(response); + if (response['data']['status']) { + alert(response['data']['message']); + $('#billing_shipping_addresses_modal').modal('hide'); + + var customerId = response['data']['cus_id']; + var QB_AddressId = response['data']['billing_addr_id']; + var QS_AddressId = response['data']['shipping_addr_id']; + if(QB_AddressId !== null ){ + var quick_billing_Address = QB_AddressLine + ','+QB_City + ',' + QB_State + ',' +QB_Country + '-' + QB_PostalCode + '.' ; $('#storeBillingAddress').empty(); - $('#storeBillingAddress').html(billing_Address); - $('#storeBillingAddress').val(billing_Address); - $('#storeShippingAddress').empty(); - $('#storeShippingAddress').html(shipping_Address); - $('#storeShippingAddress').val(shipping_Address); - - // $('#editAddressesCheckbox').prop('checked', false).trigger('change'); - + $('#storeBillingAddress').val(quick_billing_Address).prop("readonly", true); + $('#hiddenBillingAddressId').val(QB_AddressId); + } + + if(QS_AddressId !== null ){ + var quick_shipping_Address = QS_AddressLine + ','+QS_City + ',' + QS_State + '-' +QS_Country + '-' + QS_PostalCode + '.' ; + $('#storeShippingAddress').empty(); + var new_QS_ShippingAddrOption = $('