1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Customer_Helper_Data extends Mage_Core_Helper_Abstract
36: {
37: 38: 39:
40: const REFERER_QUERY_PARAM_NAME = 'referer';
41:
42: 43: 44:
45: const ROUTE_ACCOUNT_LOGIN = 'customer/account/login';
46:
47: 48: 49:
50: const XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD = 'customer/startup/redirect_dashboard';
51:
52: 53: 54:
55: const XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP = 'customer/create_account/viv_intra_union_group';
56: const XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP = 'customer/create_account/viv_domestic_group';
57: const XML_PATH_CUSTOMER_VIV_INVALID_GROUP = 'customer/create_account/viv_invalid_group';
58: const XML_PATH_CUSTOMER_VIV_ERROR_GROUP = 'customer/create_account/viv_error_group';
59:
60: 61: 62:
63: const XML_PATH_CUSTOMER_VIV_GROUP_AUTO_ASSIGN = 'customer/create_account/viv_disable_auto_group_assign_default';
64:
65: 66: 67:
68: const XML_PATH_SUPPORT_EMAIL = 'trans_email/ident_support/email';
69:
70: 71: 72: 73:
74: const VAT_VALIDATION_WSDL_URL = 'http://ec.europa.eu/taxation_customs/vies/services/checkVatService?wsdl';
75:
76: 77: 78:
79: const XML_PATH_CUSTOMER_RESET_PASSWORD_LINK_EXPIRATION_PERIOD
80: = 'default/customer/password/reset_link_expiration_period';
81:
82: 83: 84:
85: const VAT_CLASS_DOMESTIC = 'domestic';
86: const VAT_CLASS_INTRA_UNION = 'intra_union';
87: const VAT_CLASS_INVALID = 'invalid';
88: const VAT_CLASS_ERROR = 'error';
89:
90: 91: 92: 93: 94:
95: protected $_groups;
96:
97: 98: 99: 100: 101:
102: public function isLoggedIn()
103: {
104: return Mage::getSingleton('customer/session')->isLoggedIn();
105: }
106:
107: 108: 109: 110: 111:
112: public function getCustomer()
113: {
114: if (empty($this->_customer)) {
115: $this->_customer = Mage::getSingleton('customer/session')->getCustomer();
116: }
117: return $this->_customer;
118: }
119:
120: 121: 122: 123: 124:
125: public function getGroups()
126: {
127: if (empty($this->_groups)) {
128: $this->_groups = Mage::getModel('customer/group')->getResourceCollection()
129: ->setRealGroupsFilter()
130: ->load();
131: }
132: return $this->_groups;
133: }
134:
135: 136: 137: 138: 139:
140: public function getCurrentCustomer()
141: {
142: return $this->getCustomer();
143: }
144:
145: 146: 147: 148: 149:
150: public function getCustomerName()
151: {
152: return $this->getCustomer()->getName();
153: }
154:
155: 156: 157: 158: 159:
160: public function customerHasAddresses()
161: {
162: return count($this->getCustomer()->getAddresses()) > 0;
163: }
164:
165: 166: 167:
168:
169: 170: 171: 172: 173:
174: public function getLoginUrl()
175: {
176: return $this->_getUrl(self::ROUTE_ACCOUNT_LOGIN, $this->getLoginUrlParams());
177: }
178:
179: 180: 181: 182: 183:
184: public function getLoginUrlParams()
185: {
186: $params = array();
187:
188: $referer = $this->_getRequest()->getParam(self::REFERER_QUERY_PARAM_NAME);
189:
190: if (!$referer && !Mage::getStoreConfigFlag(self::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD)
191: && !Mage::getSingleton('customer/session')->getNoReferer()
192: ) {
193: $referer = Mage::getUrl('*/*/*', array('_current' => true, '_use_rewrite' => true));
194: $referer = Mage::helper('core')->urlEncode($referer);
195: }
196:
197: if ($referer) {
198: $params = array(self::REFERER_QUERY_PARAM_NAME => $referer);
199: }
200:
201: return $params;
202: }
203:
204: 205: 206: 207: 208:
209: public function getLoginPostUrl()
210: {
211: $params = array();
212: if ($this->_getRequest()->getParam(self::REFERER_QUERY_PARAM_NAME)) {
213: $params = array(
214: self::REFERER_QUERY_PARAM_NAME => $this->_getRequest()->getParam(self::REFERER_QUERY_PARAM_NAME)
215: );
216: }
217: return $this->_getUrl('customer/account/loginPost', $params);
218: }
219:
220: 221: 222: 223: 224:
225: public function getLogoutUrl()
226: {
227: return $this->_getUrl('customer/account/logout');
228: }
229:
230: 231: 232: 233: 234:
235: public function getDashboardUrl()
236: {
237: return $this->_getUrl('customer/account');
238: }
239:
240: 241: 242: 243: 244:
245: public function getAccountUrl()
246: {
247: return $this->_getUrl('customer/account');
248: }
249:
250: 251: 252: 253: 254:
255: public function getRegisterUrl()
256: {
257: return $this->_getUrl('customer/account/create');
258: }
259:
260: 261: 262: 263: 264:
265: public function getRegisterPostUrl()
266: {
267: return $this->_getUrl('customer/account/createpost');
268: }
269:
270: 271: 272: 273: 274:
275: public function getEditUrl()
276: {
277: return $this->_getUrl('customer/account/edit');
278: }
279:
280: 281: 282: 283: 284:
285: public function getEditPostUrl()
286: {
287: return $this->_getUrl('customer/account/editpost');
288: }
289:
290: 291: 292: 293: 294:
295: public function getForgotPasswordUrl()
296: {
297: return $this->_getUrl('customer/account/forgotpassword');
298: }
299:
300: 301: 302: 303: 304:
305: public function isConfirmationRequired()
306: {
307: return $this->getCustomer()->isConfirmationRequired();
308: }
309:
310: 311: 312: 313: 314: 315:
316: public function getEmailConfirmationUrl($email = null)
317: {
318: return $this->_getUrl('customer/account/confirmation', array('email' => $email));
319: }
320:
321: 322: 323: 324: 325:
326: public function isRegistrationAllowed()
327: {
328: $result = new Varien_Object(array('is_allowed' => true));
329: Mage::dispatchEvent('customer_registration_is_allowed', array('result' => $result));
330: return $result->getIsAllowed();
331: }
332:
333: 334: 335: 336: 337:
338: public function getNamePrefixOptions($store = null)
339: {
340: return $this->_prepareNamePrefixSuffixOptions(
341: Mage::helper('customer/address')->getConfig('prefix_options', $store)
342: );
343: }
344:
345: 346: 347: 348: 349:
350: public function getNameSuffixOptions($store = null)
351: {
352: return $this->_prepareNamePrefixSuffixOptions(
353: Mage::helper('customer/address')->getConfig('suffix_options', $store)
354: );
355: }
356:
357: 358: 359: 360: 361: 362:
363: protected function _prepareNamePrefixSuffixOptions($options)
364: {
365: $options = trim($options);
366: if (empty($options)) {
367: return false;
368: }
369: $result = array();
370: $options = explode(';', $options);
371: foreach ($options as $value) {
372: $value = $this->escapeHtml(trim($value));
373: $result[$value] = $value;
374: }
375: return $result;
376: }
377:
378: 379: 380: 381: 382:
383: public function generateResetPasswordLinkToken()
384: {
385: return Mage::helper('core')->uniqHash();
386: }
387:
388: 389: 390: 391: 392:
393: public function getResetPasswordLinkExpirationPeriod()
394: {
395: return (int) Mage::getConfig()->getNode(self::XML_PATH_CUSTOMER_RESET_PASSWORD_LINK_EXPIRATION_PERIOD);
396: }
397:
398: 399: 400: 401: 402: 403:
404: public function getDefaultCustomerGroupId($store = null)
405: {
406: return (int)Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID, $store);
407: }
408:
409: 410: 411: 412: 413: 414: 415: 416:
417: public function getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $vatValidationResult, $store = null)
418: {
419: $groupId = null;
420:
421: $vatClass = $this->getCustomerVatClass($customerCountryCode, $vatValidationResult, $store);
422:
423: $vatClassToGroupXmlPathMap = array(
424: self::VAT_CLASS_DOMESTIC => self::XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP,
425: self::VAT_CLASS_INTRA_UNION => self::XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP,
426: self::VAT_CLASS_INVALID => self::XML_PATH_CUSTOMER_VIV_INVALID_GROUP,
427: self::VAT_CLASS_ERROR => self::XML_PATH_CUSTOMER_VIV_ERROR_GROUP
428: );
429:
430: if (isset($vatClassToGroupXmlPathMap[$vatClass])) {
431: $groupId = (int)Mage::getStoreConfig($vatClassToGroupXmlPathMap[$vatClass], $store);
432: }
433:
434: return $groupId;
435: }
436:
437: 438: 439: 440: 441: 442: 443: 444: 445: 446:
447: public function checkVatNumber($countryCode, $vatNumber, $requesterCountryCode = '', $requesterVatNumber = '')
448: {
449:
450: $gatewayResponse = new Varien_Object(array(
451: 'is_valid' => false,
452: 'request_date' => '',
453: 'request_identifier' => '',
454: 'request_success' => false
455: ));
456:
457: if (!extension_loaded('soap')) {
458: Mage::logException(Mage::exception('Mage_Core',
459: Mage::helper('core')->__('PHP SOAP extension is required.')));
460: return $gatewayResponse;
461: }
462:
463: if (!$this->canCheckVatNumber($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber)) {
464: return $gatewayResponse;
465: }
466:
467: try {
468: $soapClient = $this->_createVatNumberValidationSoapClient();
469:
470: $requestParams = array();
471: $requestParams['countryCode'] = $countryCode;
472: $requestParams['vatNumber'] = str_replace(array(' ', '-'), array('', ''), $vatNumber);
473: $requestParams['requesterCountryCode'] = $requesterCountryCode;
474: $requestParams['requesterVatNumber'] = str_replace(array(' ', '-'), array('', ''), $requesterVatNumber);
475:
476:
477: $result = $soapClient->checkVatApprox($requestParams);
478:
479: $gatewayResponse->setIsValid((boolean) $result->valid);
480: $gatewayResponse->setRequestDate((string) $result->requestDate);
481: $gatewayResponse->setRequestIdentifier((string) $result->requestIdentifier);
482: $gatewayResponse->setRequestSuccess(true);
483: } catch (Exception $exception) {
484: $gatewayResponse->setIsValid(false);
485: $gatewayResponse->setRequestDate('');
486: $gatewayResponse->setRequestIdentifier('');
487: }
488:
489: return $gatewayResponse;
490: }
491:
492: 493: 494: 495: 496: 497: 498: 499: 500: 501:
502: public function canCheckVatNumber($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber)
503: {
504: $result = true;
505:
506: $coreHelper = Mage::helper('core');
507:
508: if (!is_string($countryCode)
509: || !is_string($vatNumber)
510: || !is_string($requesterCountryCode)
511: || !is_string($requesterVatNumber)
512: || empty($countryCode)
513: || !$coreHelper->isCountryInEU($countryCode)
514: || empty($vatNumber)
515: || (empty($requesterCountryCode) && !empty($requesterVatNumber))
516: || (!empty($requesterCountryCode) && empty($requesterVatNumber))
517: || (!empty($requesterCountryCode) && !$coreHelper->isCountryInEU($requesterCountryCode))
518: ) {
519: $result = false;
520: }
521:
522: return $result;
523: }
524:
525: 526: 527: 528: 529: 530: 531: 532:
533: public function getCustomerVatClass($customerCountryCode, $vatValidationResult, $store = null)
534: {
535: $vatClass = null;
536:
537: $isVatNumberValid = $vatValidationResult->getIsValid();
538:
539: if (is_string($customerCountryCode)
540: && !empty($customerCountryCode)
541: && $customerCountryCode === Mage::helper('core')->getMerchantCountryCode($store)
542: && $isVatNumberValid
543: ) {
544: $vatClass = self::VAT_CLASS_DOMESTIC;
545: } elseif ($isVatNumberValid) {
546: $vatClass = self::VAT_CLASS_INTRA_UNION;
547: } else {
548: $vatClass = self::VAT_CLASS_INVALID;
549: }
550:
551: if (!$vatValidationResult->getRequestSuccess()) {
552: $vatClass = self::VAT_CLASS_ERROR;
553: }
554:
555: return $vatClass;
556: }
557:
558: 559: 560: 561: 562: 563: 564: 565:
566: public function getVatValidationUserMessage($customerAddress, $customerGroupAutoAssignDisabled, $validationResult)
567: {
568: $message = '';
569: $isError = true;
570: $customerVatClass = $this->getCustomerVatClass($customerAddress->getCountryId(), $validationResult);
571: $groupAutoAssignDisabled = Mage::getStoreConfigFlag(self::XML_PATH_CUSTOMER_VIV_GROUP_AUTO_ASSIGN);
572:
573: $willChargeTaxMessage = $this->__('You will be charged tax.');
574: $willNotChargeTaxMessage = $this->__('You will not be charged tax.');
575:
576: if ($validationResult->getIsValid()) {
577: $message = $this->__('Your VAT ID was successfully validated.');
578: $isError = false;
579:
580: if (!$groupAutoAssignDisabled && !$customerGroupAutoAssignDisabled) {
581: $message .= ' ' . ($customerVatClass == self::VAT_CLASS_DOMESTIC
582: ? $willChargeTaxMessage
583: : $willNotChargeTaxMessage);
584: }
585: } else if ($validationResult->getRequestSuccess()) {
586: $message = sprintf(
587: $this->__('The VAT ID entered (%s) is not a valid VAT ID.') . ' ',
588: $this->escapeHtml($customerAddress->getVatId())
589: );
590: if (!$groupAutoAssignDisabled && !$customerGroupAutoAssignDisabled) {
591: $message .= $willChargeTaxMessage;
592: }
593: }
594: else {
595: $contactUsMessage = sprintf($this->__('If you believe this is an error, please contact us at %s'),
596: Mage::getStoreConfig(self::XML_PATH_SUPPORT_EMAIL));
597:
598: $message = $this->__('Your Tax ID cannot be validated.') . ' '
599: . (!$groupAutoAssignDisabled && !$customerGroupAutoAssignDisabled
600: ? $willChargeTaxMessage . ' ' : '')
601: . $contactUsMessage;
602: }
603:
604: $validationMessageEnvelope = new Varien_Object();
605: $validationMessageEnvelope->setMessage($message);
606: $validationMessageEnvelope->setIsError($isError);
607:
608: return $validationMessageEnvelope;
609: }
610:
611: 612: 613: 614: 615: 616:
617: protected function _createVatNumberValidationSoapClient($trace = false)
618: {
619: return new SoapClient(self::VAT_VALIDATION_WSDL_URL, array('trace' => $trace));
620: }
621: }
622: