| Conditions | 25 |
| Paths | 18433 |
| Total Lines | 190 |
| Code Lines | 122 |
| 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 namespace AntonyThorpe\SilvershopUnleashed; |
||
| 291 | public function onAfterWrite() |
||
| 292 | { |
||
| 293 | parent::onAfterWrite(); |
||
| 294 | $config = $this->owner->config(); |
||
| 295 | $defaults = Defaults::config(); |
||
| 296 | |||
| 297 | if ($defaults->send_sales_orders_to_unleashed |
||
| 298 | && $this->owner->Status == 'Paid' |
||
| 299 | && !$this->owner->OrderSentToUnleashed) { |
||
| 300 | // Definitions |
||
| 301 | $order = $this->owner; |
||
| 302 | $member = $order->Member(); |
||
| 303 | $date_paid = new DateTime($order->Paid); |
||
| 304 | $date_placed = new DateTime($order->Placed); |
||
| 305 | $body = [ |
||
| 306 | 'Addresses' => [], |
||
| 307 | 'Currency' => [], |
||
| 308 | 'Customer' => [], |
||
| 309 | 'DiscountRate' => 0, |
||
| 310 | 'Guid' => $order->Guid, |
||
| 311 | 'OrderDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
| 312 | 'OrderNumber' => $order->Reference, |
||
| 313 | 'OrderStatus' => $defaults->order_status, |
||
| 314 | 'PaymentDueDate' => $date_paid->format('Y-m-d\TH:i:s'), |
||
| 315 | 'PaymentTerm' => $defaults->payment_term, |
||
| 316 | 'PrintPackingSlipInsteadOfInvoice' => $defaults->print_packingslip_instead_of_invoice, |
||
| 317 | 'ReceivedDate' => $date_placed->format('Y-m-d\TH:i:s'), |
||
| 318 | 'SalesOrderLines' => [], |
||
| 319 | 'SellPriceTier' => ShopConfigExtension::current()->CustomerGroup()->Title, |
||
| 320 | 'Taxable' => false, |
||
| 321 | 'Tax' => [], |
||
| 322 | 'Total' => round(floatval($order->Total()), $config->rounding_precision), |
||
| 323 | ]; |
||
| 324 | |||
| 325 | $body = $this->setBodyAddress($body, $order, 'Postal'); |
||
| 326 | $body = $this->setBodyAddress($body, $order, 'Physical'); |
||
| 327 | $body = $this->setBodyCurrencyCode($body, $order); |
||
| 328 | $body = $this->setBodyCustomerCodeAndName($body, $order); |
||
| 329 | $body = $this->setBodySubTotalAndTax($body, $order, $defaults->tax_modifier_class_name, $config->rounding_precision); |
||
| 330 | $body = $this->setBodyDeliveryMethodAndDeliveryName($body, $order, $defaults->shipping_modifier_class_name); |
||
| 331 | $body = $this->setBodySalesOrderLines($body, $order, $defaults->tax_modifier_class_name, $config->rounding_precision); |
||
| 332 | |||
| 333 | // Add optional defaults |
||
| 334 | if ($defaults->created_by) { |
||
| 335 | $body['CreatedBy'] = $defaults->created_by; |
||
| 336 | } |
||
| 337 | |||
| 338 | if ($defaults->customer_type) { |
||
| 339 | $body['CustomerType'] = $defaults->customer_type; |
||
| 340 | } |
||
| 341 | |||
| 342 | if ($defaults->sales_order_group) { |
||
| 343 | $body['SalesOrderGroup'] = $defaults->sales_order_group; |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($defaults->source_id) { |
||
| 347 | $body['SourceId'] = $defaults->source_id; |
||
| 348 | } |
||
| 349 | |||
| 350 | // add phone number if available |
||
| 351 | if ($order->BillingAddress()->Phone) { |
||
| 352 | $body['PhoneNumber'] = $order->BillingAddress()->Phone; |
||
| 353 | } |
||
| 354 | |||
| 355 | // add required date |
||
| 356 | $date_required = new DateTime($order->Paid); |
||
| 357 | if ($defaults->expected_days_to_deliver) { |
||
| 358 | $date_required->modify('+' . $defaults->expected_days_to_deliver . 'day'); |
||
| 359 | } |
||
| 360 | $body['RequiredDate'] = $date_required->format('Y-m-d\TH:i:s'); |
||
| 361 | |||
| 362 | if ($order->Notes) { |
||
| 363 | $body['Comments'] = $order->Notes; |
||
| 364 | } |
||
| 365 | |||
| 366 | // Create Member for Guests |
||
| 367 | if (!$member->exists()) { |
||
| 368 | $member = Member::create(); |
||
| 369 | $member->FirstName = $order->FirstName; |
||
| 370 | $member->Surname = $order->Surname; |
||
| 371 | $member->Email = $order->getLatestEmail(); |
||
| 372 | } |
||
| 373 | |||
| 374 | // See if New Customer/Guest has previously purchased |
||
| 375 | if (!$member->Guid) { |
||
| 376 | $response = UnleashedAPI::sendCall( |
||
| 377 | 'GET', |
||
| 378 | 'https://api.unleashedsoftware.com/Customers?contactEmail=' . $member->Email |
||
| 379 | ); |
||
| 380 | |||
| 381 | if ($response->getStatusCode() == '200') { |
||
| 382 | $contents = json_decode($response->getBody()->getContents(), true); |
||
| 383 | $items = $contents['Items']; |
||
| 384 | if ($items) { |
||
| 385 | // Email address exists |
||
| 386 | $member->Guid = $items[0]['Guid']; |
||
| 387 | } else { |
||
| 388 | // A Customer is not returned, we have a unique email address. |
||
| 389 | // Check to see if the Customer Code exists (note that the Customer Code cannot be doubled up) |
||
| 390 | $response = UnleashedAPI::sendCall( |
||
| 391 | 'GET', |
||
| 392 | 'https://api.unleashedsoftware.com/Customers?customerCode=' . $body['CustomerCode'] |
||
| 393 | ); |
||
| 394 | |||
| 395 | if ($response->getStatusCode() == '200') { |
||
| 396 | $contents = json_decode($response->getBody()->getContents(), true); |
||
| 397 | $items = $contents['Items']; |
||
| 398 | if ($items) { |
||
| 399 | // A Customer Code already exists (and the email address is unique). |
||
| 400 | // If the address is the same then this is the Customer |
||
| 401 | if ($this->matchCustomerAddress($items, $order->ShippingAddress())) { |
||
| 402 | $member->Guid = $items[0]['Guid']; |
||
| 403 | |||
| 404 | //Note the existing email address in the Comment |
||
| 405 | //PUT Customer is not available in Unleashed |
||
| 406 | if ($body['Comments']) { |
||
| 407 | $body['Comments'] .= PHP_EOL; |
||
| 408 | } |
||
| 409 | $body['Comments'] .= _t( |
||
| 410 | 'UnleashedAPI.addEmailToCustomerComment', |
||
| 411 | 'Add email to Customer: {email_address}', |
||
| 412 | '', |
||
| 413 | ['email_address' => $member->Email] |
||
| 414 | ); |
||
| 415 | } else { |
||
| 416 | // The Customer Code already exists, we have a unique email address, but |
||
| 417 | // the delivery address is new. |
||
| 418 | // Therefore, we need to create a new Customer with a unique Customer Code. |
||
| 419 | $body['CustomerCode'] .= rand(10000000, 99999999); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | if (!$member->Guid) { |
||
| 428 | // The Customer Code does not exists in Unleashed and the email address is unique |
||
| 429 | // therefore create in Unleashed |
||
| 430 | $member->Guid = (string) Utils::createGuid(); |
||
| 431 | $body_member = [ |
||
| 432 | 'Addresses' => $body['Addresses'], |
||
| 433 | 'ContactFirstName' => $member->FirstName, |
||
| 434 | 'ContactLastName' => $member->Surname, |
||
| 435 | 'CreatedBy' => $body['CreatedBy'], |
||
| 436 | 'Currency' => $body['Currency'], |
||
| 437 | 'CustomerCode' => $body['CustomerCode'], |
||
| 438 | 'CustomerName' => $body['CustomerName'], |
||
| 439 | 'CustomerType' => $body['CustomerType'], |
||
| 440 | 'Email' => $member->Email, |
||
| 441 | 'Guid' => $member->Guid, |
||
| 442 | 'PaymentTerm' => $body['PaymentTerm'], |
||
| 443 | 'PhoneNumber' => $body['PhoneNumber'], |
||
| 444 | 'PrintPackingSlipInsteadOfInvoice' => $body['PrintPackingSlipInsteadOfInvoice'], |
||
| 445 | 'SellPriceTier' => $body['SellPriceTier'], |
||
| 446 | 'SourceId' => $body['SourceId'], |
||
| 447 | 'Taxable' => $body['Taxable'], |
||
| 448 | 'TaxCode' => $body['Tax']['TaxCode'] |
||
| 449 | ]; |
||
| 450 | |||
| 451 | foreach ($body_member['Addresses'] as $index => $value) { |
||
| 452 | $body_member['Addresses'][$index]['IsDefault'] = true; |
||
| 453 | } |
||
| 454 | |||
| 455 | $response = UnleashedAPI::sendCall( |
||
| 456 | 'POST', |
||
| 457 | 'https://api.unleashedsoftware.com/Customers/' . $member->Guid, |
||
| 458 | ['json' => $body_member ] |
||
| 459 | ); |
||
| 460 | |||
| 461 | if ($response->getReasonPhrase() == 'Created' && $order->Member()->exists()) { |
||
| 462 | $member->write(); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | // Prepare Sales Order data |
||
| 467 | // Skip if previous calls to Customer have failed and the Guid has not been set |
||
| 468 | if ($member->Guid) { |
||
| 469 | $body['Customer']['Guid'] = $member->Guid; |
||
| 470 | |||
| 471 | $this->owner->extend('updateUnleashedSalesOrder', $body); |
||
| 472 | |||
| 473 | $response = UnleashedAPI::sendCall( |
||
| 474 | 'POST', |
||
| 475 | 'https://api.unleashedsoftware.com/SalesOrders/' . $order->Guid, |
||
| 476 | ['json' => $body] |
||
| 477 | ); |
||
| 478 | if ($response->getReasonPhrase() == 'Created') { |
||
| 479 | $this->owner->OrderSentToUnleashed = DBDatetime::now()->Rfc2822(); |
||
| 480 | $this->owner->write(); |
||
| 481 | } |
||
| 486 |