Order.php 26 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\commerce_order\Entity;

5
use Drupal\commerce\Entity\CommerceContentEntityBase;
6
use Drupal\commerce_order\Adjustment;
7 8
use Drupal\commerce_order\Event\OrderEvents;
use Drupal\commerce_order\Event\OrderProfilesEvent;
9
use Drupal\commerce_order\OrderBalanceFieldItemList;
10
use Drupal\commerce_price\Price;
pcambra's avatar
pcambra committed
11
use Drupal\commerce_store\Entity\StoreInterface;
12
use Drupal\Core\Datetime\DrupalDateTime;
13
use Drupal\Core\Entity\EntityChangedTrait;
14 15
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
16
use Drupal\Core\Field\BaseFieldDefinition;
17
use Drupal\user\Entity\User;
18
use Drupal\user\UserInterface;
19
use Drupal\profile\Entity\ProfileInterface;
20 21

/**
bojanz's avatar
bojanz committed
22
 * Defines the order entity class.
23 24 25
 *
 * @ContentEntityType(
 *   id = "commerce_order",
26 27 28 29
 *   label = @Translation("Order", context = "Commerce"),
 *   label_collection = @Translation("Orders", context = "Commerce"),
 *   label_singular = @Translation("order", context = "Commerce"),
 *   label_plural = @Translation("orders", context = "Commerce"),
30 31 32
 *   label_count = @PluralTranslation(
 *     singular = "@count order",
 *     plural = "@count orders",
33
 *     context = "Commerce",
34
 *   ),
35
 *   bundle_label = @Translation("Order type", context = "Commerce"),
36
 *   handlers = {
37
 *     "event" = "Drupal\commerce_order\Event\OrderEvent",
38
 *     "storage" = "Drupal\commerce_order\OrderStorage",
39
 *     "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
40
 *     "access" = "Drupal\commerce_order\OrderAccessControlHandler",
41
 *     "query_access" = "Drupal\commerce_order\OrderQueryAccessHandler",
42
 *     "permission_provider" = "Drupal\commerce_order\OrderPermissionProvider",
43
 *     "list_builder" = "Drupal\commerce_order\OrderListBuilder",
44
 *     "views_data" = "Drupal\commerce\CommerceEntityViewsData",
45
 *     "form" = {
46
 *       "default" = "Drupal\commerce_order\Form\OrderForm",
47 48
 *       "add" = "Drupal\commerce_order\Form\OrderForm",
 *       "edit" = "Drupal\commerce_order\Form\OrderForm",
49 50
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
 *       "unlock" = "Drupal\commerce_order\Form\OrderUnlockForm",
51
 *       "resend-receipt" = "Drupal\commerce_order\Form\OrderReceiptResendForm",
bojanz's avatar
bojanz committed
52
 *     },
53 54 55
 *     "local_task_provider" = {
 *       "default" = "Drupal\entity\Menu\DefaultEntityLocalTaskProvider",
 *     },
bojanz's avatar
bojanz committed
56
 *     "route_provider" = {
57
 *       "default" = "Drupal\commerce_order\OrderRouteProvider",
58
 *       "delete-multiple" = "Drupal\entity\Routing\DeleteMultipleRouteProvider",
bojanz's avatar
bojanz committed
59
 *     },
60
 *     "entity_print" = "Drupal\commerce_order\EntityPrint\OrderRenderer"
61 62
 *   },
 *   base_table = "commerce_order",
63 64
 *   admin_permission = "administer commerce_order",
 *   permission_granularity = "bundle",
65 66 67
 *   field_indexes = {
 *     "order_number"
 *   },
68 69
 *   entity_keys = {
 *     "id" = "order_id",
70
 *     "label" = "order_number",
71 72 73 74
 *     "uuid" = "uuid",
 *     "bundle" = "type"
 *   },
 *   links = {
75
 *     "canonical" = "/admin/commerce/orders/{commerce_order}",
76
 *     "edit-form" = "/admin/commerce/orders/{commerce_order}/edit",
77
 *     "delete-form" = "/admin/commerce/orders/{commerce_order}/delete",
78
 *     "delete-multiple-form" = "/admin/commerce/orders/delete",
79
 *     "reassign-form" = "/admin/commerce/orders/{commerce_order}/reassign",
80
 *     "unlock-form" = "/admin/commerce/orders/{commerce_order}/unlock",
81 82
 *     "collection" = "/admin/commerce/orders",
 *     "resend-receipt-form" = "/admin/commerce/orders/{commerce_order}/resend-receipt"
83
 *   },
84
 *   bundle_entity_type = "commerce_order_type",
85 86
 *   field_ui_base_route = "entity.commerce_order_type.edit_form",
 *   allow_number_patterns = TRUE,
87 88
 * )
 */
89
class Order extends CommerceContentEntityBase implements OrderInterface {
90

bojanz's avatar
bojanz committed
91
  use EntityChangedTrait;
92

93 94 95
  /**
   * {@inheritdoc}
   */
96 97
  public function getOrderNumber() {
    return $this->get('order_number')->value;
98 99 100
  }

  /**
101
   * {@inheritdoc}
102
   */
103 104 105
  public function setOrderNumber($order_number) {
    $this->set('order_number', $order_number);
    return $this;
106 107
  }

108 109 110
  /**
   * {@inheritdoc}
   */
111
  public function getStore() {
112
    return $this->getTranslatedReferencedEntity('store_id');
113 114
  }

115 116 117
  /**
   * {@inheritdoc}
   */
118 119 120
  public function setStore(StoreInterface $store) {
    $this->set('store_id', $store->id());
    return $this;
121 122
  }

123 124 125
  /**
   * {@inheritdoc}
   */
126 127
  public function getStoreId() {
    return $this->get('store_id')->target_id;
128 129 130 131 132
  }

  /**
   * {@inheritdoc}
   */
133 134
  public function setStoreId($store_id) {
    $this->set('store_id', $store_id);
135 136 137
    return $this;
  }

138 139 140
  /**
   * {@inheritdoc}
   */
141
  public function getCustomer() {
142 143 144 145 146 147
    $customer = $this->get('uid')->entity;
    // Handle deleted customers.
    if (!$customer) {
      $customer = User::getAnonymousUser();
    }
    return $customer;
148 149 150 151 152
  }

  /**
   * {@inheritdoc}
   */
153 154 155
  public function setCustomer(UserInterface $account) {
    $this->set('uid', $account->id());
    return $this;
156 157 158 159 160
  }

  /**
   * {@inheritdoc}
   */
161 162
  public function getCustomerId() {
    return $this->get('uid')->target_id;
163 164
  }

165 166 167
  /**
   * {@inheritdoc}
   */
168 169
  public function setCustomerId($uid) {
    $this->set('uid', $uid);
170
    return $this;
171 172 173 174 175
  }

  /**
   * {@inheritdoc}
   */
176 177 178 179 180 181 182 183 184
  public function getEmail() {
    return $this->get('mail')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setEmail($mail) {
    $this->set('mail', $mail);
185 186 187 188 189 190
    return $this;
  }

  /**
   * {@inheritdoc}
   */
191 192
  public function getIpAddress() {
    return $this->get('ip_address')->value;
193 194 195 196 197
  }

  /**
   * {@inheritdoc}
   */
198 199
  public function setIpAddress($ip_address) {
    $this->set('ip_address', $ip_address);
200 201 202
    return $this;
  }

203 204 205 206 207 208 209 210 211 212 213
  /**
   * {@inheritdoc}
   */
  public function getBillingProfile() {
    return $this->get('billing_profile')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setBillingProfile(ProfileInterface $profile) {
214
    $this->set('billing_profile', $profile);
215 216 217
    return $this;
  }

218 219 220 221 222 223 224 225 226 227 228
  /**
   * {@inheritdoc}
   */
  public function collectProfiles() {
    $profiles = [];
    if ($billing_profile = $this->getBillingProfile()) {
      $profiles['billing'] = $billing_profile;
    }
    // Allow other modules to register their own profiles (e.g. shipping).
    $event = new OrderProfilesEvent($this, $profiles);
    \Drupal::service('event_dispatcher')->dispatch(OrderEvents::ORDER_PROFILES, $event);
229
    $profiles = $event->getProfiles();
230 231 232 233

    return $profiles;
  }

234 235 236
  /**
   * {@inheritdoc}
   */
237 238
  public function getItems() {
    return $this->get('order_items')->referencedEntities();
239 240 241 242 243
  }

  /**
   * {@inheritdoc}
   */
244 245
  public function setItems(array $order_items) {
    $this->set('order_items', $order_items);
246
    $this->recalculateTotalPrice();
247 248 249
    return $this;
  }

250 251 252
  /**
   * {@inheritdoc}
   */
253 254
  public function hasItems() {
    return !$this->get('order_items')->isEmpty();
255 256
  }

257 258 259
  /**
   * {@inheritdoc}
   */
260 261 262
  public function addItem(OrderItemInterface $order_item) {
    if (!$this->hasItem($order_item)) {
      $this->get('order_items')->appendItem($order_item);
263
      $this->recalculateTotalPrice();
264 265 266 267 268 269 270
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
271 272
  public function removeItem(OrderItemInterface $order_item) {
    $index = $this->getItemIndex($order_item);
273
    if ($index !== FALSE) {
274
      $this->get('order_items')->offsetUnset($index);
275
      $this->recalculateTotalPrice();
276 277 278 279 280 281 282
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
283 284
  public function hasItem(OrderItemInterface $order_item) {
    return $this->getItemIndex($order_item) !== FALSE;
285 286 287
  }

  /**
288
   * Gets the index of the given order item.
289
   *
290 291
   * @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item
   *   The order item.
292 293
   *
   * @return int|bool
294
   *   The index of the given order item, or FALSE if not found.
295
   */
296 297 298
  protected function getItemIndex(OrderItemInterface $order_item) {
    $values = $this->get('order_items')->getValue();
    $order_item_ids = array_map(function ($value) {
299 300 301
      return $value['target_id'];
    }, $values);

302
    return array_search($order_item->id(), $order_item_ids);
303 304
  }

305 306 307
  /**
   * {@inheritdoc}
   */
308 309 310 311 312 313 314 315 316 317 318 319 320 321
  public function getAdjustments(array $adjustment_types = []) {
    /** @var \Drupal\commerce_order\Adjustment[] $adjustments */
    $adjustments = $this->get('adjustments')->getAdjustments();
    // Filter adjustments by type, if needed.
    if ($adjustment_types) {
      foreach ($adjustments as $index => $adjustment) {
        if (!in_array($adjustment->getType(), $adjustment_types)) {
          unset($adjustments[$index]);
        }
      }
      $adjustments = array_values($adjustments);
    }

    return $adjustments;
322 323 324 325 326
  }

  /**
   * {@inheritdoc}
   */
327 328 329
  public function setAdjustments(array $adjustments) {
    $this->set('adjustments', $adjustments);
    $this->recalculateTotalPrice();
330 331 332 333 334 335
    return $this;
  }

  /**
   * {@inheritdoc}
   */
336 337 338 339
  public function addAdjustment(Adjustment $adjustment) {
    $this->get('adjustments')->appendItem($adjustment);
    $this->recalculateTotalPrice();
    return $this;
340 341 342 343 344
  }

  /**
   * {@inheritdoc}
   */
345 346 347
  public function removeAdjustment(Adjustment $adjustment) {
    $this->get('adjustments')->removeAdjustment($adjustment);
    $this->recalculateTotalPrice();
348 349 350
    return $this;
  }

351 352 353 354
  /**
   * {@inheritdoc}
   */
  public function clearAdjustments() {
355 356 357 358 359
    $locked_callback = function ($adjustment) {
      /** @var \Drupal\commerce_order\Adjustment $adjustment */
      return $adjustment->isLocked();
    };
    // Remove all unlocked adjustments.
360
    foreach ($this->getItems() as $order_item) {
361
      /** @var \Drupal\commerce_order\Adjustment[] $adjustments */
362
      $adjustments = array_filter($order_item->getAdjustments(), $locked_callback);
363 364 365 366 367 368 369
      // Convert legacy locked adjustments.
      if ($adjustments && $order_item->usesLegacyAdjustments()) {
        foreach ($adjustments as $index => $adjustment) {
          $adjustments[$index] = $adjustment->multiply($order_item->getQuantity());
        }
      }
      $order_item->set('uses_legacy_adjustments', FALSE);
370
      $order_item->setAdjustments($adjustments);
371
    }
372
    $adjustments = array_filter($this->getAdjustments(), $locked_callback);
373
    $this->setAdjustments($adjustments);
374

375 376 377
    return $this;
  }

378 379 380
  /**
   * {@inheritdoc}
   */
381
  public function collectAdjustments(array $adjustment_types = []) {
382 383
    $adjustments = [];
    foreach ($this->getItems() as $order_item) {
384
      foreach ($order_item->getAdjustments($adjustment_types) as $adjustment) {
385 386 387 388
        if ($order_item->usesLegacyAdjustments()) {
          $adjustment = $adjustment->multiply($order_item->getQuantity());
        }
        $adjustments[] = $adjustment;
389 390
      }
    }
391
    foreach ($this->getAdjustments($adjustment_types) as $adjustment) {
392 393 394 395 396 397 398 399 400 401
      $adjustments[] = $adjustment;
    }

    return $adjustments;
  }

  /**
   * {@inheritdoc}
   */
  public function getSubtotalPrice() {
402 403
    /** @var \Drupal\commerce_price\Price $subtotal_price */
    $subtotal_price = NULL;
404 405 406
    foreach ($this->getItems() as $order_item) {
      if ($order_item_total = $order_item->getTotalPrice()) {
        $subtotal_price = $subtotal_price ? $subtotal_price->add($order_item_total) : $order_item_total;
407
      }
408 409 410 411
    }
    return $subtotal_price;
  }

412 413 414 415
  /**
   * {@inheritdoc}
   */
  public function recalculateTotalPrice() {
416 417
    /** @var \Drupal\commerce_price\Price $total_price */
    $total_price = NULL;
418 419 420
    foreach ($this->getItems() as $order_item) {
      if ($order_item_total = $order_item->getTotalPrice()) {
        $total_price = $total_price ? $total_price->add($order_item_total) : $order_item_total;
421
      }
422 423
    }
    if ($total_price) {
424 425 426 427 428 429 430 431 432 433
      $adjustments = $this->collectAdjustments();
      if ($adjustments) {
        /** @var \Drupal\commerce_order\AdjustmentTransformerInterface $adjustment_transformer */
        $adjustment_transformer = \Drupal::service('commerce_order.adjustment_transformer');
        $adjustments = $adjustment_transformer->combineAdjustments($adjustments);
        $adjustments = $adjustment_transformer->roundAdjustments($adjustments);
        foreach ($adjustments as $adjustment) {
          if (!$adjustment->isIncluded()) {
            $total_price = $total_price->add($adjustment->getAmount());
          }
434
        }
435
      }
436 437
    }
    $this->total_price = $total_price;
438

439 440 441
    return $this;
  }

442 443 444 445
  /**
   * {@inheritdoc}
   */
  public function getTotalPrice() {
446 447 448
    if (!$this->get('total_price')->isEmpty()) {
      return $this->get('total_price')->first()->toPrice();
    }
449 450
  }

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  /**
   * {@inheritdoc}
   */
  public function getTotalPaid() {
    if (!$this->get('total_paid')->isEmpty()) {
      return $this->get('total_paid')->first()->toPrice();
    }
    elseif ($total_price = $this->getTotalPrice()) {
      // Provide a default without storing it, to avoid having to update
      // the field if the order currency changes before the order is placed.
      return new Price('0', $total_price->getCurrencyCode());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setTotalPaid(Price $total_paid) {
    $this->set('total_paid', $total_paid);
  }

  /**
   * {@inheritdoc}
   */
  public function getBalance() {
    if ($total_price = $this->getTotalPrice()) {
bojanz's avatar
bojanz committed
477
      return $total_price->subtract($this->getTotalPaid());
478 479 480
    }
  }

481 482 483 484
  /**
   * {@inheritdoc}
   */
  public function isPaid() {
485 486 487 488 489
    $total_price = $this->getTotalPrice();
    if (!$total_price) {
      return FALSE;
    }

490
    $balance = $this->getBalance();
491 492
    // Free orders are considered fully paid once they have been placed.
    if ($total_price->isZero()) {
493
      return $this->getState()->getId() != 'draft';
494 495 496 497
    }
    else {
      return $balance->isNegative() || $balance->isZero();
    }
498 499
  }

mglaman's avatar
mglaman committed
500 501 502
  /**
   * {@inheritdoc}
   */
503 504
  public function getState() {
    return $this->get('state')->first();
mglaman's avatar
mglaman committed
505 506
  }

507 508 509 510
  /**
   * {@inheritdoc}
   */
  public function getRefreshState() {
511
    return $this->getData('refresh_state');
512 513 514 515 516 517
  }

  /**
   * {@inheritdoc}
   */
  public function setRefreshState($refresh_state) {
518
    return $this->setData('refresh_state', $refresh_state);
519 520
  }

mglaman's avatar
mglaman committed
521 522 523
  /**
   * {@inheritdoc}
   */
524
  public function getData($key, $default = NULL) {
525 526 527 528
    $data = [];
    if (!$this->get('data')->isEmpty()) {
      $data = $this->get('data')->first()->getValue();
    }
529
    return isset($data[$key]) ? $data[$key] : $default;
530 531 532 533 534
  }

  /**
   * {@inheritdoc}
   */
535 536
  public function setData($key, $value) {
    $this->get('data')->__set($key, $value);
mglaman's avatar
mglaman committed
537 538
    return $this;
  }
539

540 541 542 543 544 545 546 547 548 549 550 551
  /**
   * {@inheritdoc}
   */
  public function unsetData($key) {
    if (!$this->get('data')->isEmpty()) {
      $data = $this->get('data')->first()->getValue();
      unset($data[$key]);
      $this->set('data', $data);
    }
    return $this;
  }

552 553 554 555
  /**
   * {@inheritdoc}
   */
  public function isLocked() {
556
    return (bool) $this->get('locked')->value;
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  }

  /**
   * {@inheritdoc}
   */
  public function lock() {
    $this->set('locked', TRUE);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function unlock() {
    $this->set('locked', FALSE);
    return $this;
  }

575 576 577
  /**
   * {@inheritdoc}
   */
578 579
  public function getCreatedTime() {
    return $this->get('created')->value;
580 581 582 583 584
  }

  /**
   * {@inheritdoc}
   */
585 586
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
587 588 589
    return $this;
  }

590 591 592
  /**
   * {@inheritdoc}
   */
593 594
  public function getPlacedTime() {
    return $this->get('placed')->value;
595 596 597 598 599
  }

  /**
   * {@inheritdoc}
   */
600 601
  public function setPlacedTime($timestamp) {
    $this->set('placed', $timestamp);
602 603 604
    return $this;
  }

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  /**
   * {@inheritdoc}
   */
  public function getCompletedTime() {
    return $this->get('completed')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCompletedTime($timestamp) {
    $this->set('completed', $timestamp);
    return $this;
  }

620 621 622 623
  /**
   * {@inheritdoc}
   */
  public function getCalculationDate() {
624
    $timezone = $this->getStore()->getTimezone();
625
    $timestamp = $this->getPlacedTime() ?: \Drupal::time()->getRequestTime();
626
    $date = DrupalDateTime::createFromTimestamp($timestamp, $timezone);
627 628 629 630

    return $date;
  }

631 632 633
  /**
   * {@inheritdoc}
   */
634 635 636
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

637 638
    if ($this->isNew() && !$this->getIpAddress()) {
      $this->setIpAddress(\Drupal::request()->getClientIp());
639
    }
640 641 642
    $customer = $this->getCustomer();
    // The customer has been deleted, clear the reference.
    if ($this->getCustomerId() && $customer->isAnonymous()) {
643
      $this->setCustomerId(0);
644 645 646
    }
    // Maintain the order email.
    if (!$this->getEmail() && $customer->isAuthenticated()) {
647
      $this->setEmail($customer->getEmail());
648
    }
649 650 651 652 653 654
    // Make sure the billing profile is owned by the order, not the customer.
    $billing_profile = $this->getBillingProfile();
    if ($billing_profile && $billing_profile->getOwnerId()) {
      $billing_profile->setOwnerId(0);
      $billing_profile->save();
    }
655

656
    if ($this->getState()->getId() == 'draft') {
657 658 659 660
      // Refresh draft orders on every save.
      if (empty($this->getRefreshState())) {
        $this->setRefreshState(self::REFRESH_ON_SAVE);
      }
661
      // Initialize the flag for OrderStorage::doOrderPreSave().
662 663 664
      if ($this->getData('paid_event_dispatched') === NULL) {
        $this->setData('paid_event_dispatched', FALSE);
      }
665
    }
666 667
  }

668 669 670
  /**
   * {@inheritdoc}
   */
671 672 673
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

674 675 676 677 678
    // Ensure there's a back-reference on each order item.
    foreach ($this->getItems() as $order_item) {
      if ($order_item->order_id->isEmpty()) {
        $order_item->order_id = $this->id();
        $order_item->save();
679 680 681 682 683 684 685 686
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
687 688
    parent::postDelete($storage, $entities);

689 690 691
    // Delete the order items of a deleted order.
    $order_items = [];
    /** @var \Drupal\commerce_order\Entity\OrderInterface $entity */
692
    foreach ($entities as $entity) {
693 694
      foreach ($entity->getItems() as $order_item) {
        $order_items[$order_item->id()] = $order_item;
695 696
      }
    }
697 698 699
    /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
    $order_item_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_order_item');
    $order_item_storage->delete($order_items);
700 701
  }

702 703 704
  /**
   * {@inheritdoc}
   */
705
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
bojanz's avatar
bojanz committed
706
    $fields = parent::baseFieldDefinitions($entity_type);
707

708
    $fields['order_number'] = BaseFieldDefinition::create('string')
709 710 711 712 713
      ->setLabel(t('Order number'))
      ->setDescription(t('The order number displayed to the customer.'))
      ->setRequired(TRUE)
      ->setDefaultValue('')
      ->setSetting('max_length', 255)
714 715
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
716

717 718 719 720 721 722 723 724 725 726 727
    $fields['store_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Store'))
      ->setDescription(t('The store to which the order belongs.'))
      ->setCardinality(1)
      ->setRequired(TRUE)
      ->setSetting('target_type', 'commerce_store')
      ->setSetting('handler', 'default')
      ->setTranslatable(TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

728
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
729 730
      ->setLabel(t('Customer'))
      ->setDescription(t('The customer.'))
731
      ->setSetting('target_type', 'user')
732
      ->setSetting('handler', 'default')
733
      ->setDefaultValueCallback('Drupal\commerce_order\Entity\Order::getCurrentUserId')
734
      ->setTranslatable(TRUE)
735
      ->setDisplayOptions('view', [
736
        'label' => 'above',
737 738
        'type' => 'author',
        'weight' => 0,
739
      ])
740 741
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
742

743
    $fields['mail'] = BaseFieldDefinition::create('email')
744
      ->setLabel(t('Contact email'))
745
      ->setDescription(t('The email address associated with the order.'))
746 747
      ->setDefaultValue('')
      ->setSetting('max_length', 255)
748
      ->setDisplayOptions('view', [
749
        'label' => 'above',
750 751
        'type' => 'string',
        'weight' => 0,
752
      ])
753 754
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
755

756 757 758 759 760 761 762 763 764 765 766
    $fields['ip_address'] = BaseFieldDefinition::create('string')
      ->setLabel(t('IP address'))
      ->setDescription(t('The IP address of the order.'))
      ->setDefaultValue('')
      ->setSetting('max_length', 128)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => 0,
      ])
      ->setDisplayOptions('form', [
767
        'region' => 'hidden',
768 769 770 771 772
        'weight' => 0,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

773
    $fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
774
      ->setLabel(t('Billing information'))
775 776 777
      ->setDescription(t('Billing profile'))
      ->setSetting('target_type', 'profile')
      ->setSetting('handler', 'default')
778
      ->setSetting('handler_settings', ['target_bundles' => ['customer' => 'customer']])
779 780
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', [
781
        'type' => 'commerce_billing_profile',
782 783 784 785 786 787
        'weight' => 0,
        'settings' => [],
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    $fields['order_items'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Order items'))
      ->setDescription(t('The order items.'))
      ->setRequired(TRUE)
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
      ->setSetting('target_type', 'commerce_order_item')
      ->setSetting('handler', 'default')
      ->setDisplayOptions('form', [
        'type' => 'inline_entity_form_complex',
        'weight' => 0,
        'settings' => [
          'override_labels' => TRUE,
          'label_singular' => t('order item'),
          'label_plural' => t('order items'),
        ],
      ])
      ->setDisplayOptions('view', [
        'type' => 'commerce_order_item_table',
        'weight' => 0,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

811 812 813 814 815 816 817
    $fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
      ->setLabel(t('Adjustments'))
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
      ->setDisplayOptions('form', [
        'type' => 'commerce_adjustment_default',
        'weight' => 0,
      ])
818
      ->setDisplayConfigurable('form', TRUE)
819 820
      ->setDisplayConfigurable('view', FALSE);

821
    $fields['total_price'] = BaseFieldDefinition::create('commerce_price')
822
      ->setLabel(t('Total price'))
823 824
      ->setDescription(t('The total price of the order.'))
      ->setReadOnly(TRUE)
825 826 827
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'commerce_order_total_summary',
828
        'weight' => 0,
829
      ])
830 831 832
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);

833 834 835 836 837 838
    $fields['total_paid'] = BaseFieldDefinition::create('commerce_price')
      ->setLabel(t('Total paid'))
      ->setDescription(t('The total paid price of the order.'))
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);

839 840 841 842 843 844 845 846 847
    $fields['balance'] = BaseFieldDefinition::create('commerce_price')
      ->setLabel(t('Order balance'))
      ->setDescription(t('The order balance.'))
      ->setReadOnly(TRUE)
      ->setComputed(TRUE)
      ->setClass(OrderBalanceFieldItemList::class)
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);

848 849 850 851 852
    $fields['state'] = BaseFieldDefinition::create('state')
      ->setLabel(t('State'))
      ->setDescription(t('The order state.'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
853
      ->setDisplayOptions('view', [
854
        'label' => 'hidden',
855 856
        'type' => 'state_transition_form',
        'weight' => 10,
857
      ])
858
      ->setDisplayConfigurable('form', TRUE)
859 860
      ->setDisplayConfigurable('view', TRUE)
      ->setSetting('workflow_callback', ['\Drupal\commerce_order\Entity\Order', 'getWorkflowId']);
861

862
    $fields['data'] = BaseFieldDefinition::create('map')
863 864 865
      ->setLabel(t('Data'))
      ->setDescription(t('A serialized array of additional data.'));

866 867 868 869 870 871 872 873
    $fields['locked'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Locked'))
      ->setSettings([
        'on_label' => t('Yes'),
        'off_label' => t('No'),
      ])
      ->setDefaultValue(FALSE);

874 875
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
mglaman's avatar
mglaman committed
876 877 878 879
      ->setDescription(t('The time when the order was created.'));

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
880 881
      ->setDescription(t('The time when the order was last edited.'))
      ->setDisplayConfigurable('view', TRUE);
mglaman's avatar
mglaman committed
882 883 884 885

    $fields['placed'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Placed'))
      ->setDescription(t('The time when the order was placed.'))
886
      ->setDisplayOptions('view', [
887
        'label' => 'above',
888
        'type' => 'timestamp',
889
        'weight' => 0,
890 891
      ])
      ->setDisplayConfigurable('view', TRUE);
892

893 894 895 896 897 898 899 900 901 902
    $fields['completed'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Completed'))
      ->setDescription(t('The time when the order was completed.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'timestamp',
        'weight' => 0,
      ])
      ->setDisplayConfigurable('view', TRUE);

903 904 905
    return $fields;
  }

906 907 908 909 910 911 912 913 914
  /**
   * Default value callback for 'uid' base field definition.
   *
   * @see ::baseFieldDefinitions()
   *
   * @return array
   *   An array of default values.
   */
  public static function getCurrentUserId() {
915
    return [\Drupal::currentUser()->id()];
916 917
  }

918
  /**
919
   * Gets the workflow ID for the state field.
920 921 922 923 924
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The order.
   *
   * @return string
925
   *   The workflow ID.
926
   */
927 928
  public static function getWorkflowId(OrderInterface $order) {
    $workflow = OrderType::load($order->bundle())->getWorkflowId();
929 930 931
    return $workflow;
  }

932
}