Conditions | 36 |
Paths | > 20000 |
Total Lines | 279 |
Code Lines | 185 |
Lines | 10 |
Ratio | 3.58 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace SilvershopUnleashed\Model; |
||
107 | public function onAfterWrite() |
||
108 | { |
||
109 | parent::onAfterWrite(); |
||
110 | $config = $this->owner->config(); |
||
111 | |||
112 | if ( |
||
113 | $config->send_sales_orders_to_unleashed |
||
114 | && $this->owner->Status == "Paid" |
||
115 | && !$this->owner->OrderSentToUnleashed |
||
116 | ) { |
||
117 | // Definitions |
||
118 | $order = $this->owner; |
||
119 | $billing_address = $order->BillingAddress(); |
||
120 | $shipping_address = $order->ShippingAddress(); |
||
121 | $member = $order->Member(); |
||
122 | $countries = ShopConfig::config()->iso_3166_country_codes; |
||
123 | $subtotal = $order->Total(); |
||
124 | $sell_price_tier = ShopConfig::current()->CustomerGroup()->Title; |
||
125 | $taxable = false; |
||
126 | $tax_code = ''; |
||
127 | $tax_total = 0; |
||
128 | $tax_class_name = $config->tax_modifier_class_name; |
||
129 | $modifiers = $order->Modifiers(); |
||
130 | $tax_modifier = $order->getModifier($config->tax_modifier_class_name); |
||
131 | $shipping_method = ''; |
||
132 | $sales_order_lines = []; |
||
133 | $line_number = 0; |
||
134 | |||
135 | |||
136 | // Customer |
||
137 | if (!$member->exists()) { // Create Member for Guests |
||
138 | $member = Member::create(); |
||
139 | $member->FirstName = $order->FirstName; |
||
140 | $member->Surname = $order->Surname; |
||
141 | $member->Email = $order->getLatestEmail(); |
||
142 | } |
||
143 | |||
144 | // Selling Price Tier if Customer set with a different pricecard using the Extended Pricing Module |
||
145 | if (class_exists('HasGroupPricing') && $member->Groups()->exists()) { |
||
146 | $levels = HasGroupPricing::get_levels(); |
||
147 | foreach ($member->Groups() as $group) { |
||
148 | if (array_key_exists($group->Code, $levels)) { |
||
149 | // Assign member specific group |
||
150 | $sell_price_tier = $group->Title; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | // Taxation (e.g. Sales Tax/GST) |
||
156 | if (!empty($tax_modifier)) { |
||
157 | $subtotal -= $tax_modifier->Amount; |
||
158 | $taxable = true; |
||
159 | $tax_code = $tax_modifier::config()->name; |
||
160 | $tax_total = floatval($tax_modifier->Amount); |
||
161 | } |
||
162 | |||
163 | // Define Customer (use Company field of BillingAddress to allow for B2B eCommerce sites) |
||
164 | if ($billing_address->Company) { |
||
165 | $customer_name = $billing_address->Company; // use Organisation name |
||
166 | } else { |
||
167 | $customer_name = $order->getName(); // use Contact full name instead |
||
168 | } |
||
169 | |||
170 | if (!$member->Guid) { // See if New Customer/Guest has previously purchased |
||
171 | $response = UnleashedAPI::sendCall( |
||
172 | 'GET', |
||
173 | 'https://api.unleashedsoftware.com/Customers?contactEmail=' . $member->Email |
||
174 | ); |
||
175 | |||
176 | if ($response->getStatusCode() == '200') { |
||
177 | $contents = json_decode($response->getBody()->getContents(), true); |
||
178 | if ($items = $contents['Items']) { |
||
179 | $member->Guid = $items[0]['Guid']; |
||
180 | } else { |
||
181 | |||
182 | // Create new Customer in Unleashed |
||
183 | $member->Guid = (string) Utils::createGuid(); |
||
184 | $address_name_postal_new_customer = $this->getAddressName($billing_address); |
||
185 | $address_name_physical_new_customer = $this->getAddressName($shipping_address); |
||
186 | |||
187 | $body = [ |
||
188 | 'Addresses' => [ |
||
189 | [ |
||
190 | 'AddressName' => $address_name_postal_new_customer, |
||
191 | 'AddressType' => 'Postal', |
||
192 | 'City' => $billing_address->City, |
||
193 | 'Country' => $countries[$billing_address->Country], |
||
194 | 'PostalCode' => $billing_address->PostalCode, |
||
195 | 'Region' => $billing_address->State, |
||
196 | 'StreetAddress' => $billing_address->Address, |
||
197 | 'StreetAddress2' => $billing_address->AddressLine2 |
||
198 | ], |
||
199 | [ |
||
200 | 'AddressName' => $address_name_physical_new_customer, |
||
201 | 'AddressType' => 'Physical', |
||
202 | 'City' => $shipping_address->City, |
||
203 | 'Country' => $countries[$shipping_address->Country], |
||
204 | 'PostalCode' => $shipping_address->PostalCode, |
||
205 | 'Region' => $shipping_address->State, |
||
206 | 'StreetAddress' => $shipping_address->Address, |
||
207 | 'StreetAddress2' => $shipping_address->AddressLine2 |
||
208 | ] |
||
209 | ], |
||
210 | 'Currency' =>[ |
||
211 | 'CurrencyCode' => $order->Currency() |
||
212 | ], |
||
213 | 'CustomerCode' => $customer_name, |
||
214 | 'CustomerName' => $customer_name, |
||
215 | 'ContactFirstName' => $member->FirstName, |
||
216 | 'ContactLastName' => $member->Surname, |
||
217 | 'Email' => $member->Email, |
||
218 | 'Guid' => $member->Guid, |
||
219 | 'PaymentTerm' => $config->default_payment_term, |
||
220 | 'PrintPackingSlipInsteadOfInvoice' => true, |
||
221 | 'SellPriceTier' => $sell_price_tier |
||
222 | ]; |
||
223 | |||
224 | if ($taxable) { |
||
225 | $body['Taxable'] = $taxable; |
||
226 | } |
||
227 | |||
228 | if ($created_by = $config->default_created_by) { |
||
229 | $body['CreatedBy'] = $created_by; |
||
230 | } |
||
231 | |||
232 | if ($customer_type = $config->default_customer_type) { |
||
233 | $body['CustomerType'] = $customer_type; |
||
234 | } |
||
235 | |||
236 | if ($phone = $billing_address->Phone) { // add phone number if available |
||
237 | $body['PhoneNumber'] = $phone; |
||
238 | } |
||
239 | |||
240 | $response = UnleashedAPI::sendCall( |
||
241 | 'POST', |
||
242 | 'https://api.unleashedsoftware.com/Customers/' . $member->Guid, |
||
243 | ['json' => $body ] |
||
244 | ); |
||
245 | |||
246 | if ($response->getReasonPhrase() == 'Created' && $order->Member()->exists()) { |
||
247 | $member->write(); |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | |||
254 | // Prepare Sales Order data |
||
255 | if ($member->Guid) { // Skip if previous calls to Customer have failed and the Guid has not been set |
||
256 | |||
257 | // Dates |
||
258 | $date_placed = new DateTime($order->Placed); |
||
259 | $date_paid = new DateTime($order->Paid); |
||
260 | $date_required = new DateTime($order->Paid); |
||
261 | if ($expected_days_to_deliver = $config->expected_days_to_deliver) { |
||
262 | $date_required->modify('+' . $expected_days_to_deliver . 'day'); |
||
263 | } |
||
264 | |||
265 | // Sales Order Lines |
||
266 | foreach ($order->Items()->getIterator() as $item) { |
||
267 | // Definitions |
||
268 | $product = $item->Product(); |
||
269 | $line_number += 1; |
||
270 | |||
271 | $sales_order_line = [ |
||
272 | 'DiscountRate' => 0, |
||
273 | 'Guid' => $item->Guid, |
||
274 | 'LineNumber' => $line_number, |
||
275 | 'LineType' => null, |
||
276 | 'LineTotal' => round(floatval($item->Total()), $config->rounding_precision), |
||
277 | 'OrderQuantity' => (int) $item->Quantity, |
||
278 | 'Product' => [ |
||
279 | 'Guid' => $product->Guid |
||
280 | ], |
||
281 | 'UnitPrice' => round(floatval($product->getPrice()), $config->rounding_precision) |
||
282 | ]; |
||
283 | View Code Duplication | if ($tax_class_name) { |
|
284 | $tax_calculator = new $tax_class_name; |
||
285 | $sales_order_line['LineTax'] = round($tax_calculator->value($item->Total()), $config->rounding_precision); |
||
286 | $sales_order_line['LineTaxCode'] = $tax_code; |
||
287 | } |
||
288 | $sales_order_lines[] = $sales_order_line; |
||
289 | } |
||
290 | |||
291 | // Add Modifiers that have an associated product_code |
||
292 | foreach ($modifiers->sort('Sort')->getIterator() as $modifier) { |
||
293 | if ($modifier::config()->product_code && $modifier->Type !== 'Ignored' && $modifier->value()) { |
||
294 | $line_number += 1; |
||
295 | $sales_order_line = [ |
||
296 | 'DiscountRate' => 0, |
||
297 | 'Guid' => $modifier->Guid, |
||
298 | 'LineNumber' => $line_number, |
||
299 | 'LineTotal' => round(floatval($modifier->Amount), $config->rounding_precision), |
||
300 | 'LineType' => null, |
||
301 | 'OrderQuantity' => 1, |
||
302 | 'Product' => [ |
||
303 | 'ProductCode' => $modifier::config()->product_code, |
||
304 | ], |
||
305 | 'UnitPrice' => round(floatval($modifier->Amount), $config->rounding_precision) |
||
306 | ]; |
||
307 | View Code Duplication | if ($tax_class_name) { |
|
308 | $tax_calculator = new $tax_class_name; |
||
309 | $sales_order_line['LineTax'] = round($tax_calculator->value($modifier->Amount), $config->rounding_precision); |
||
310 | $sales_order_line['LineTaxCode'] = $tax_code; |
||
311 | } |
||
312 | $sales_order_lines[] = $sales_order_line; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | // Shipping Module |
||
317 | if (class_exists('ShippingMethod')) { |
||
318 | if ($name = $order->ShippingMethod()->Name) { |
||
319 | $shipping_method = $name; |
||
320 | } |
||
321 | } |
||
322 | |||
323 | $body = [ |
||
324 | 'Comments' => $order->Notes, |
||
325 | 'Currency' =>[ |
||
326 | 'CurrencyCode' => $order->Currency() |
||
327 | ], |
||
328 | 'Customer' => [ |
||
329 | 'Guid' => $member->Guid |
||
330 | ], |
||
331 | 'DeliveryCity' => $shipping_address->City, |
||
332 | 'DeliveryCountry' => $countries[$shipping_address->Country], |
||
333 | 'DeliveryPostCode' => $shipping_address->PostalCode, |
||
334 | 'DeliveryRegion' => $shipping_address->State, |
||
335 | 'DeliveryStreetAddress' => $shipping_address->Address, |
||
336 | 'DeliveryStreetAddress2' => $shipping_address->AddressLine2, |
||
337 | 'DiscountRate' => 0, |
||
338 | 'Guid' => $order->Guid, |
||
339 | 'OrderDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
340 | 'OrderNumber' => $order->Reference, |
||
341 | 'OrderStatus' => 'Parked', |
||
342 | 'PaymentDueDate' => $date_paid->format('Y-m-d\TH:i:s'), |
||
343 | 'ReceivedDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
344 | 'RequiredDate' => $date_required->format('Y-m-d\TH:i:s'), |
||
345 | 'SalesOrderLines' => $sales_order_lines, |
||
346 | 'SubTotal' => $subtotal, |
||
347 | 'Tax' => [ |
||
348 | 'TaxCode' => $tax_code |
||
349 | ], |
||
350 | 'TaxTotal' => $tax_total, |
||
351 | 'Total' => round(floatval($order->Total()), $config->rounding_precision) |
||
352 | ]; |
||
353 | |||
354 | if ($shipping_method) { |
||
355 | $body['DeliveryMethod'] = $shipping_method; |
||
356 | $body['DeliveryName'] = $shipping_method; |
||
357 | } |
||
358 | |||
359 | if ($sales_order_group = $config->default_sales_order_group) { |
||
360 | $body['SalesOrderGroup'] = $sales_order_group; |
||
361 | } |
||
362 | |||
363 | if ($sales_person = $config->default_sales_person) { |
||
364 | $body['SalesPerson'] = $sales_person; |
||
365 | } |
||
366 | |||
367 | if ($source_id = $config->default_source_id) { |
||
368 | $body['SourceId'] = $source_id; |
||
369 | } |
||
370 | |||
371 | $this->owner->extend('updateUnleashedSalesOrder', $body); |
||
372 | |||
373 | $response = UnleashedAPI::sendCall( |
||
374 | 'POST', |
||
375 | 'https://api.unleashedsoftware.com/SalesOrders/' . $order->Guid, |
||
376 | ['json' => $body] |
||
377 | ); |
||
378 | if ($response->getReasonPhrase() == 'Created') { |
||
379 | $this->owner->OrderSentToUnleashed = SS_Datetime::now()->Rfc2822(); |
||
380 | $this->owner->write(); |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | |||
385 | } |
||
386 | } |
||
388 |