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: class Mage_Customer_Model_Observer
33: {
34: 35: 36:
37: const VIV_PROCESSED_FLAG = 'viv_after_address_save_processed';
38:
39: 40: 41:
42: const VIV_CURRENTLY_SAVED_ADDRESS = 'currently_saved_address';
43:
44: 45: 46: 47: 48: 49:
50: protected function _isDefaultBilling($address)
51: {
52: return ($address->getId() && $address->getId() == $address->getCustomer()->getDefaultBilling())
53: || $address->getIsPrimaryBilling() || $address->getIsDefaultBilling();
54: }
55:
56: 57: 58: 59: 60: 61:
62: protected function _isDefaultShipping($address)
63: {
64: return ($address->getId() && $address->getId() == $address->getCustomer()->getDefaultShipping())
65: || $address->getIsPrimaryShipping() || $address->getIsDefaultShipping();
66: }
67:
68: 69: 70: 71: 72: 73:
74: protected function _canProcessAddress($address)
75: {
76: if ($address->getForceProcess()) {
77: return true;
78: }
79:
80: if (Mage::registry(self::VIV_CURRENTLY_SAVED_ADDRESS) != $address->getId()) {
81: return false;
82: }
83:
84: $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType();
85: if ($configAddressType == Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING) {
86: return $this->_isDefaultShipping($address);
87: }
88: return $this->_isDefaultBilling($address);
89: }
90:
91: 92: 93: 94: 95:
96: public function beforeLoadLayout($observer)
97: {
98: $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
99:
100: $observer->getEvent()->getLayout()->getUpdate()
101: ->addHandle('customer_logged_' . ($loggedIn ? 'in' : 'out'));
102: }
103:
104: 105: 106: 107: 108:
109: public function beforeAddressSave($observer)
110: {
111: if (Mage::registry(self::VIV_CURRENTLY_SAVED_ADDRESS)) {
112: Mage::unregister(self::VIV_CURRENTLY_SAVED_ADDRESS);
113: }
114:
115:
116: $customerAddress = $observer->getCustomerAddress();
117: if ($customerAddress->getId()) {
118: Mage::register(self::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddress->getId());
119: } else {
120: $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType();
121:
122: $forceProcess = ($configAddressType == Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING)
123: ? $customerAddress->getIsDefaultShipping() : $customerAddress->getIsDefaultBilling();
124:
125: if ($forceProcess) {
126: $customerAddress->setForceProcess(true);
127: } else {
128: Mage::register(self::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address');
129: }
130: }
131: }
132:
133: 134: 135: 136: 137:
138: public function afterAddressSave($observer)
139: {
140:
141: $customerAddress = $observer->getCustomerAddress();
142: $customer = $customerAddress->getCustomer();
143:
144: if (!Mage::helper('customer/address')->isVatValidationEnabled($customer->getStore())
145: || Mage::registry(self::VIV_PROCESSED_FLAG)
146: || !$this->_canProcessAddress($customerAddress)
147: ) {
148: return;
149: }
150:
151: try {
152: Mage::register(self::VIV_PROCESSED_FLAG, true);
153:
154:
155: $customerHelper = Mage::helper('customer');
156:
157: if ($customerAddress->getVatId() == ''
158: || !Mage::helper('core')->isCountryInEU($customerAddress->getCountry()))
159: {
160: $defaultGroupId = $customerHelper->getDefaultCustomerGroupId($customer->getStore());
161:
162: if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
163: $customer->setGroupId($defaultGroupId);
164: $customer->save();
165: }
166: } else {
167:
168: $result = $customerHelper->checkVatNumber(
169: $customerAddress->getCountryId(),
170: $customerAddress->getVatId()
171: );
172:
173: $newGroupId = $customerHelper->getCustomerGroupIdBasedOnVatNumber(
174: $customerAddress->getCountryId(), $result, $customer->getStore()
175: );
176:
177: if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) {
178: $customer->setGroupId($newGroupId);
179: $customer->save();
180: }
181:
182: if (!Mage::app()->getStore()->isAdmin()) {
183: $validationMessage = Mage::helper('customer')->getVatValidationUserMessage($customerAddress,
184: $customer->getDisableAutoGroupChange(), $result);
185:
186: if (!$validationMessage->getIsError()) {
187: Mage::getSingleton('customer/session')->addSuccess($validationMessage->getMessage());
188: } else {
189: Mage::getSingleton('customer/session')->addError($validationMessage->getMessage());
190: }
191: }
192: }
193: } catch (Exception $e) {
194: Mage::register(self::VIV_PROCESSED_FLAG, false, true);
195: }
196: }
197:
198: 199: 200: 201: 202:
203: public function quoteSubmitAfter($observer)
204: {
205:
206: $customer = $observer->getQuote()->getCustomer();
207:
208: if (!Mage::helper('customer/address')->isVatValidationEnabled($customer->getStore())) {
209: return;
210: }
211:
212: if (!$customer->getId()) {
213: return;
214: }
215:
216: $customer->setGroupId(
217: $customer->getOrigData('group_id')
218: );
219: $customer->save();
220: }
221: }
222: