Conditions | 25 |
Paths | 18433 |
Total Lines | 191 |
Code Lines | 123 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
335 | public function onAfterWrite() |
||
336 | { |
||
337 | parent::onAfterWrite(); |
||
338 | $config = $this->owner->config(); |
||
339 | $defaults = Defaults::config(); |
||
340 | |||
341 | if ($defaults->send_sales_orders_to_unleashed |
||
342 | && $this->owner->Status == 'Paid' |
||
343 | && !$this->owner->OrderSentToUnleashed) { |
||
344 | // Definitions |
||
345 | $order = $this->owner; |
||
346 | $member = $order->Member(); |
||
347 | $date_paid = new DateTime($order->Paid); |
||
348 | $date_placed = new DateTime($order->Placed); |
||
349 | $body = [ |
||
350 | 'Addresses' => [], |
||
351 | 'Currency' => [], |
||
352 | 'Customer' => [], |
||
353 | 'DiscountRate' => 0, |
||
354 | 'Guid' => $order->Guid, |
||
355 | 'OrderDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
356 | 'OrderNumber' => $order->Reference, |
||
357 | 'OrderStatus' => $defaults->order_status, |
||
358 | 'PaymentDueDate' => $date_paid->format('Y-m-d\TH:i:s'), |
||
359 | 'PaymentTerm' => $defaults->payment_term, |
||
360 | 'PrintPackingSlipInsteadOfInvoice' => $defaults->print_packingslip_instead_of_invoice, |
||
361 | 'ReceivedDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
362 | 'SalesOrderLines' => [], |
||
363 | 'SellPriceTier' => ShopConfigExtension::current()->CustomerGroup()->Title, |
||
364 | 'Taxable' => false, |
||
365 | 'Tax' => [], |
||
366 | 'Total' => round(floatval($order->Total()), $config->rounding_precision), |
||
367 | ]; |
||
368 | |||
369 | $body = $this->setBodyAddress($body, $order, 'Postal'); |
||
370 | $body = $this->setBodyAddress($body, $order, 'Physical'); |
||
371 | $body = $this->setBodyCurrencyCode($body, $order); |
||
372 | $body = $this->setBodyCustomerCodeAndName($body, $order); |
||
373 | $body = $this->setBodyDeliveryMethodAndDeliveryName($body, $order, $defaults->shipping_modifier_class_name); |
||
374 | $body = $this->setBodyTaxCode($body, $order, $defaults->tax_modifier_class_name); |
||
375 | $body = $this->setBodySalesOrderLines($body, $order, $defaults->tax_modifier_class_name, $config->rounding_precision); |
||
376 | $body = $this->setBodySubTotalAndTax($body, $order, $defaults->tax_modifier_class_name, $config->rounding_precision); |
||
377 | |||
378 | // Add optional defaults |
||
379 | if ($defaults->created_by) { |
||
380 | $body['CreatedBy'] = $defaults->created_by; |
||
381 | } |
||
382 | |||
383 | if ($defaults->customer_type) { |
||
384 | $body['CustomerType'] = $defaults->customer_type; |
||
385 | } |
||
386 | |||
387 | if ($defaults->sales_order_group) { |
||
388 | $body['SalesOrderGroup'] = $defaults->sales_order_group; |
||
389 | } |
||
390 | |||
391 | if ($defaults->source_id) { |
||
392 | $body['SourceId'] = $defaults->source_id; |
||
393 | } |
||
394 | |||
395 | // add phone number if available |
||
396 | if ($order->BillingAddress()->Phone) { |
||
397 | $body['PhoneNumber'] = $order->BillingAddress()->Phone; |
||
398 | } |
||
399 | |||
400 | // add required date |
||
401 | $date_required = new DateTime($order->Paid); |
||
402 | if ($defaults->expected_days_to_deliver) { |
||
403 | $date_required->modify('+' . $defaults->expected_days_to_deliver . 'day'); |
||
404 | } |
||
405 | $body['RequiredDate'] = $date_required->format('Y-m-d\TH:i:s'); |
||
406 | |||
407 | if ($order->Notes) { |
||
408 | $body['Comments'] = $order->Notes; |
||
409 | } |
||
410 | |||
411 | // Create Member for Guests |
||
412 | if (!$member->exists()) { |
||
413 | $member = Member::create(); |
||
414 | $member->FirstName = $order->FirstName; |
||
415 | $member->Surname = $order->Surname; |
||
416 | $member->Email = $order->getLatestEmail(); |
||
417 | } |
||
418 | |||
419 | // See if New Customer/Guest has previously purchased |
||
420 | if (!$member->Guid) { |
||
421 | $response = UnleashedAPI::sendCall( |
||
422 | 'GET', |
||
423 | 'https://api.unleashedsoftware.com/Customers?contactEmail=' . $member->Email |
||
424 | ); |
||
425 | |||
426 | if ($response->getStatusCode() == '200') { |
||
427 | $contents = json_decode($response->getBody()->getContents(), true); |
||
428 | $items = $contents['Items']; |
||
429 | if ($items) { |
||
430 | // Email address exists |
||
431 | $member->Guid = $items[0]['Guid']; |
||
432 | } else { |
||
433 | // A Customer is not returned, we have a unique email address. |
||
434 | // Check to see if the Customer Code exists (note that the Customer Code cannot be doubled up) |
||
435 | $response = UnleashedAPI::sendCall( |
||
436 | 'GET', |
||
437 | 'https://api.unleashedsoftware.com/Customers?customerCode=' . $body['CustomerCode'] |
||
438 | ); |
||
439 | |||
440 | if ($response->getStatusCode() == '200') { |
||
441 | $contents = json_decode($response->getBody()->getContents(), true); |
||
442 | $items = $contents['Items']; |
||
443 | if ($items) { |
||
444 | // A Customer Code already exists (and the email address is unique). |
||
445 | // If the address is the same then this is the Customer |
||
446 | if ($this->matchCustomerAddress($items, $order->ShippingAddress())) { |
||
447 | $member->Guid = $items[0]['Guid']; |
||
448 | |||
449 | //Note the existing email address in the Comment |
||
450 | //PUT Customer is not available in Unleashed |
||
451 | if ($body['Comments']) { |
||
452 | $body['Comments'] .= PHP_EOL; |
||
453 | } |
||
454 | $body['Comments'] .= _t( |
||
455 | 'UnleashedAPI.addEmailToCustomerComment', |
||
456 | 'Add email to Customer: {email_address}', |
||
457 | '', |
||
458 | ['email_address' => $member->Email] |
||
459 | ); |
||
460 | } else { |
||
461 | // The Customer Code already exists, we have a unique email address, but |
||
462 | // the delivery address is new. |
||
463 | // Therefore, we need to create a new Customer with a unique Customer Code. |
||
464 | $body['CustomerCode'] .= rand(10000000, 99999999); |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | } |
||
469 | } |
||
470 | } |
||
471 | |||
472 | if (!$member->Guid) { |
||
473 | // The Customer Code does not exists in Unleashed and the email address is unique |
||
474 | // therefore create in Unleashed |
||
475 | $member->Guid = (string) Utils::createGuid(); |
||
476 | $body_member = [ |
||
477 | 'Addresses' => $body['Addresses'], |
||
478 | 'ContactFirstName' => $member->FirstName, |
||
479 | 'ContactLastName' => $member->Surname, |
||
480 | 'CreatedBy' => $body['CreatedBy'], |
||
481 | 'Currency' => $body['Currency'], |
||
482 | 'CustomerCode' => $body['CustomerCode'], |
||
483 | 'CustomerName' => $body['CustomerName'], |
||
484 | 'CustomerType' => $body['CustomerType'], |
||
485 | 'Email' => $member->Email, |
||
486 | 'Guid' => $member->Guid, |
||
487 | 'PaymentTerm' => $body['PaymentTerm'], |
||
488 | 'PhoneNumber' => $body['PhoneNumber'], |
||
489 | 'PrintPackingSlipInsteadOfInvoice' => $body['PrintPackingSlipInsteadOfInvoice'], |
||
490 | 'SellPriceTier' => $body['SellPriceTier'], |
||
491 | 'SourceId' => $body['SourceId'], |
||
492 | 'Taxable' => $body['Taxable'], |
||
493 | 'TaxCode' => $body['Tax']['TaxCode'] |
||
494 | ]; |
||
495 | |||
496 | foreach ($body_member['Addresses'] as $index => $value) { |
||
497 | $body_member['Addresses'][$index]['IsDefault'] = true; |
||
498 | } |
||
499 | |||
500 | $response = UnleashedAPI::sendCall( |
||
501 | 'POST', |
||
502 | 'https://api.unleashedsoftware.com/Customers/' . $member->Guid, |
||
503 | ['json' => $body_member ] |
||
504 | ); |
||
505 | |||
506 | if ($response->getReasonPhrase() == 'Created' && $order->Member()->exists()) { |
||
507 | $member->write(); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | // Prepare Sales Order data |
||
512 | // Skip if previous calls to Customer have failed and the Guid has not been set |
||
513 | if ($member->Guid) { |
||
514 | $body['Customer']['Guid'] = $member->Guid; |
||
515 | |||
516 | $this->owner->extend('updateUnleashedSalesOrder', $body); |
||
517 | |||
518 | $response = UnleashedAPI::sendCall( |
||
519 | 'POST', |
||
520 | 'https://api.unleashedsoftware.com/SalesOrders/' . $order->Guid, |
||
521 | ['json' => $body] |
||
522 | ); |
||
523 | if ($response->getReasonPhrase() == 'Created') { |
||
524 | $this->owner->OrderSentToUnleashed = DBDatetime::now()->Rfc2822(); |
||
525 | $this->owner->write(); |
||
526 | } |
||
531 |