/mall/upload/catalog/controller/checkout/payment_address.php

https://bitbucket.org/allanxyh/uniquemall · PHP · 245 lines · 178 code · 56 blank · 11 comment · 70 complexity · 6c8ff9b3250d5c2b94666ad2d9d77ea1 MD5 · raw file

  1. <?php
  2. class ControllerCheckoutPaymentAddress extends Controller {
  3. public function index() {
  4. $this->language->load('checkout/checkout');
  5. $this->data['text_address_existing'] = $this->language->get('text_address_existing');
  6. $this->data['text_address_new'] = $this->language->get('text_address_new');
  7. $this->data['text_select'] = $this->language->get('text_select');
  8. $this->data['text_none'] = $this->language->get('text_none');
  9. $this->data['entry_firstname'] = $this->language->get('entry_firstname');
  10. $this->data['entry_lastname'] = $this->language->get('entry_lastname');
  11. $this->data['entry_company'] = $this->language->get('entry_company');
  12. $this->data['entry_company_id'] = $this->language->get('entry_company_id');
  13. $this->data['entry_tax_id'] = $this->language->get('entry_tax_id');
  14. $this->data['entry_address_1'] = $this->language->get('entry_address_1');
  15. $this->data['entry_address_2'] = $this->language->get('entry_address_2');
  16. $this->data['entry_postcode'] = $this->language->get('entry_postcode');
  17. $this->data['entry_city'] = $this->language->get('entry_city');
  18. $this->data['entry_country'] = $this->language->get('entry_country');
  19. $this->data['entry_zone'] = $this->language->get('entry_zone');
  20. $this->data['button_continue'] = $this->language->get('button_continue');
  21. if (isset($this->session->data['payment_address_id'])) {
  22. $this->data['address_id'] = $this->session->data['payment_address_id'];
  23. } else {
  24. $this->data['address_id'] = $this->customer->getAddressId();
  25. }
  26. $this->data['addresses'] = array();
  27. $this->load->model('account/address');
  28. $this->data['addresses'] = $this->model_account_address->getAddresses();
  29. $this->load->model('account/customer_group');
  30. $customer_group_info = $this->model_account_customer_group->getCustomerGroup($this->customer->getCustomerGroupId());
  31. if ($customer_group_info) {
  32. $this->data['company_id_display'] = $customer_group_info['company_id_display'];
  33. } else {
  34. $this->data['company_id_display'] = '';
  35. }
  36. if ($customer_group_info) {
  37. $this->data['company_id_required'] = $customer_group_info['company_id_required'];
  38. } else {
  39. $this->data['company_id_required'] = '';
  40. }
  41. if ($customer_group_info) {
  42. $this->data['tax_id_display'] = $customer_group_info['tax_id_display'];
  43. } else {
  44. $this->data['tax_id_display'] = '';
  45. }
  46. if ($customer_group_info) {
  47. $this->data['tax_id_required'] = $customer_group_info['tax_id_required'];
  48. } else {
  49. $this->data['tax_id_required'] = '';
  50. }
  51. if (isset($this->session->data['payment_country_id'])) {
  52. $this->data['country_id'] = $this->session->data['payment_country_id'];
  53. } else {
  54. $this->data['country_id'] = $this->config->get('config_country_id');
  55. }
  56. if (isset($this->session->data['payment_zone_id'])) {
  57. $this->data['zone_id'] = $this->session->data['payment_zone_id'];
  58. } else {
  59. $this->data['zone_id'] = '';
  60. }
  61. $this->load->model('localisation/country');
  62. $this->data['countries'] = $this->model_localisation_country->getCountries();
  63. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/payment_address.tpl')) {
  64. $this->template = $this->config->get('config_template') . '/template/checkout/payment_address.tpl';
  65. } else {
  66. $this->template = 'default/template/checkout/payment_address.tpl';
  67. }
  68. $this->response->setOutput($this->render());
  69. }
  70. public function validate() {
  71. $this->language->load('checkout/checkout');
  72. $json = array();
  73. // Validate if customer is logged in.
  74. if (!$this->customer->isLogged()) {
  75. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  76. }
  77. // Validate cart has products and has stock.
  78. if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  79. $json['redirect'] = $this->url->link('checkout/cart');
  80. }
  81. // Validate minimum quantity requirments.
  82. $products = $this->cart->getProducts();
  83. foreach ($products as $product) {
  84. $product_total = 0;
  85. foreach ($products as $product_2) {
  86. if ($product_2['product_id'] == $product['product_id']) {
  87. $product_total += $product_2['quantity'];
  88. }
  89. }
  90. if ($product['minimum'] > $product_total) {
  91. $json['redirect'] = $this->url->link('checkout/cart');
  92. break;
  93. }
  94. }
  95. if (!$json) {
  96. if (isset($this->request->post['payment_address']) && $this->request->post['payment_address'] == 'existing') {
  97. $this->load->model('account/address');
  98. if (empty($this->request->post['address_id'])) {
  99. $json['error']['warning'] = $this->language->get('error_address');
  100. } elseif (!in_array($this->request->post['address_id'], array_keys($this->model_account_address->getAddresses()))) {
  101. $json['error']['warning'] = $this->language->get('error_address');
  102. } else {
  103. // Default Payment Address
  104. $this->load->model('account/address');
  105. $address_info = $this->model_account_address->getAddress($this->request->post['address_id']);
  106. if ($address_info) {
  107. $this->load->model('account/customer_group');
  108. $customer_group_info = $this->model_account_customer_group->getCustomerGroup($this->customer->getCustomerGroupId());
  109. // Company ID
  110. if ($customer_group_info['company_id_display'] && $customer_group_info['company_id_required'] && !$address_info['company_id']) {
  111. $json['error']['warning'] = $this->language->get('error_company_id');
  112. }
  113. // Tax ID
  114. if ($customer_group_info['tax_id_display'] && $customer_group_info['tax_id_required'] && !$address_info['tax_id']) {
  115. $json['error']['warning'] = $this->language->get('error_tax_id');
  116. }
  117. }
  118. }
  119. if (!$json) {
  120. $this->session->data['payment_address_id'] = $this->request->post['address_id'];
  121. if ($address_info) {
  122. $this->session->data['payment_country_id'] = $address_info['country_id'];
  123. $this->session->data['payment_zone_id'] = $address_info['zone_id'];
  124. } else {
  125. unset($this->session->data['payment_country_id']);
  126. unset($this->session->data['payment_zone_id']);
  127. }
  128. unset($this->session->data['payment_method']);
  129. unset($this->session->data['payment_methods']);
  130. }
  131. } else {
  132. if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
  133. $json['error']['firstname'] = $this->language->get('error_firstname');
  134. }
  135. if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
  136. $json['error']['lastname'] = $this->language->get('error_lastname');
  137. }
  138. // Customer Group
  139. $this->load->model('account/customer_group');
  140. $customer_group_info = $this->model_account_customer_group->getCustomerGroup($this->customer->getCustomerGroupId());
  141. if ($customer_group_info) {
  142. // Company ID
  143. if ($customer_group_info['company_id_display'] && $customer_group_info['company_id_required'] && empty($this->request->post['company_id'])) {
  144. $json['error']['company_id'] = $this->language->get('error_company_id');
  145. }
  146. // Tax ID
  147. if ($customer_group_info['tax_id_display'] && $customer_group_info['tax_id_required'] && empty($this->request->post['tax_id'])) {
  148. $json['error']['tax_id'] = $this->language->get('error_tax_id');
  149. }
  150. }
  151. if ((utf8_strlen($this->request->post['address_1']) < 3) || (utf8_strlen($this->request->post['address_1']) > 128)) {
  152. $json['error']['address_1'] = $this->language->get('error_address_1');
  153. }
  154. if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) {
  155. $json['error']['city'] = $this->language->get('error_city');
  156. }
  157. $this->load->model('localisation/country');
  158. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  159. if ($country_info) {
  160. if ($country_info['postcode_required'] && (utf8_strlen($this->request->post['postcode']) < 2) || (utf8_strlen($this->request->post['postcode']) > 10)) {
  161. $json['error']['postcode'] = $this->language->get('error_postcode');
  162. }
  163. // VAT Validation
  164. $this->load->helper('vat');
  165. if ($this->config->get('config_vat') && !empty($this->request->post['tax_id']) && (vat_validation($country_info['iso_code_2'], $this->request->post['tax_id']) == 'invalid')) {
  166. $json['error']['tax_id'] = $this->language->get('error_vat');
  167. }
  168. }
  169. if ($this->request->post['country_id'] == '') {
  170. $json['error']['country'] = $this->language->get('error_country');
  171. }
  172. if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
  173. $json['error']['zone'] = $this->language->get('error_zone');
  174. }
  175. if (!$json) {
  176. // Default Payment Address
  177. $this->load->model('account/address');
  178. $this->session->data['payment_address_id'] = $this->model_account_address->addAddress($this->request->post);
  179. $this->session->data['payment_country_id'] = $this->request->post['country_id'];
  180. $this->session->data['payment_zone_id'] = $this->request->post['zone_id'];
  181. unset($this->session->data['payment_method']);
  182. unset($this->session->data['payment_methods']);
  183. }
  184. }
  185. }
  186. $this->response->setOutput(json_encode($json));
  187. }
  188. }
  189. ?>