Order.php 27.5 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
use Drupal\commerce_order\Exception\OrderVersionMismatchException;
8
9
use Drupal\commerce_order\Event\OrderEvents;
use Drupal\commerce_order\Event\OrderProfilesEvent;
10
use Drupal\commerce_order\OrderBalanceFieldItemList;
11
use Drupal\commerce_price\Price;
pcambra's avatar
pcambra committed
12
use Drupal\commerce_store\Entity\StoreInterface;
13
use Drupal\Core\Datetime\DrupalDateTime;
14
use Drupal\Core\Entity\EntityChangedTrait;
15
16
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
17
use Drupal\Core\Field\BaseFieldDefinition;
18
use Drupal\user\Entity\User;
19
use Drupal\user\UserInterface;
20
use Drupal\profile\Entity\ProfileInterface;
21
22

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

bojanz's avatar
bojanz committed
98
  use EntityChangedTrait;
99

100
101
102
  /**
   * {@inheritdoc}
   */
103
104
  public function getOrderNumber() {
    return $this->get('order_number')->value;
105
106
107
  }

  /**
108
   * {@inheritdoc}
109
   */
110
111
112
  public function setOrderNumber($order_number) {
    $this->set('order_number', $order_number);
    return $this;
113
114
  }

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  /**
   * {@inheritdoc}
   */
  public function getVersion() {
    return $this->get('version')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setVersion($version) {
    $this->set('version', $version);
    return $this;
  }

130
131
132
  /**
   * {@inheritdoc}
   */
133
  public function getStore() {
134
    return $this->getTranslatedReferencedEntity('store_id');
135
136
  }

137
138
139
  /**
   * {@inheritdoc}
   */
140
141
142
  public function setStore(StoreInterface $store) {
    $this->set('store_id', $store->id());
    return $this;
143
144
  }

145
146
147
  /**
   * {@inheritdoc}
   */
148
149
  public function getStoreId() {
    return $this->get('store_id')->target_id;
150
151
152
153
154
  }

  /**
   * {@inheritdoc}
   */
155
156
  public function setStoreId($store_id) {
    $this->set('store_id', $store_id);
157
158
159
    return $this;
  }

160
161
162
  /**
   * {@inheritdoc}
   */
163
  public function getCustomer() {
164
165
166
167
168
169
    $customer = $this->get('uid')->entity;
    // Handle deleted customers.
    if (!$customer) {
      $customer = User::getAnonymousUser();
    }
    return $customer;
170
171
172
173
174
  }

  /**
   * {@inheritdoc}
   */
175
176
177
  public function setCustomer(UserInterface $account) {
    $this->set('uid', $account->id());
    return $this;
178
179
180
181
182
  }

  /**
   * {@inheritdoc}
   */
183
184
  public function getCustomerId() {
    return $this->get('uid')->target_id;
185
186
  }

187
188
189
  /**
   * {@inheritdoc}
   */
190
191
  public function setCustomerId($uid) {
    $this->set('uid', $uid);
192
    return $this;
193
194
195
196
197
  }

  /**
   * {@inheritdoc}
   */
198
199
200
201
202
203
204
205
206
  public function getEmail() {
    return $this->get('mail')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setEmail($mail) {
    $this->set('mail', $mail);
207
208
209
210
211
212
    return $this;
  }

  /**
   * {@inheritdoc}
   */
213
214
  public function getIpAddress() {
    return $this->get('ip_address')->value;
215
216
217
218
219
  }

  /**
   * {@inheritdoc}
   */
220
221
  public function setIpAddress($ip_address) {
    $this->set('ip_address', $ip_address);
222
223
224
    return $this;
  }

225
226
227
228
229
230
231
232
233
234
235
  /**
   * {@inheritdoc}
   */
  public function getBillingProfile() {
    return $this->get('billing_profile')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function setBillingProfile(ProfileInterface $profile) {
236
    $this->set('billing_profile', $profile);
237
238
239
    return $this;
  }

240
241
242
243
244
245
246
247
248
249
  /**
   * {@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);
250
    \Drupal::service('event_dispatcher')->dispatch($event, OrderEvents::ORDER_PROFILES);
251
    $profiles = $event->getProfiles();
252
253
254
255

    return $profiles;
  }

256
257
258
  /**
   * {@inheritdoc}
   */
259
260
  public function getItems() {
    return $this->get('order_items')->referencedEntities();
261
262
263
264
265
  }

  /**
   * {@inheritdoc}
   */
266
267
  public function setItems(array $order_items) {
    $this->set('order_items', $order_items);
268
    $this->recalculateTotalPrice();
269
270
271
    return $this;
  }

272
273
274
  /**
   * {@inheritdoc}
   */
275
276
  public function hasItems() {
    return !$this->get('order_items')->isEmpty();
277
278
  }

279
280
281
  /**
   * {@inheritdoc}
   */
282
283
284
  public function addItem(OrderItemInterface $order_item) {
    if (!$this->hasItem($order_item)) {
      $this->get('order_items')->appendItem($order_item);
285
      $this->recalculateTotalPrice();
286
287
288
289
290
291
292
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
293
294
  public function removeItem(OrderItemInterface $order_item) {
    $index = $this->getItemIndex($order_item);
295
    if ($index !== FALSE) {
296
      $this->get('order_items')->offsetUnset($index);
297
      $this->recalculateTotalPrice();
298
299
300
301
302
303
304
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
305
306
  public function hasItem(OrderItemInterface $order_item) {
    return $this->getItemIndex($order_item) !== FALSE;
307
308
309
  }

  /**
310
   * Gets the index of the given order item.
311
   *
312
313
   * @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item
   *   The order item.
314
315
   *
   * @return int|bool
316
   *   The index of the given order item, or FALSE if not found.
317
   */
318
319
320
  protected function getItemIndex(OrderItemInterface $order_item) {
    $values = $this->get('order_items')->getValue();
    $order_item_ids = array_map(function ($value) {
321
322
323
      return $value['target_id'];
    }, $values);

324
    return array_search($order_item->id(), $order_item_ids);
325
326
  }

327
328
329
  /**
   * {@inheritdoc}
   */
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  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;
344
345
346
347
348
  }

  /**
   * {@inheritdoc}
   */
349
350
351
  public function setAdjustments(array $adjustments) {
    $this->set('adjustments', $adjustments);
    $this->recalculateTotalPrice();
352
353
354
355
356
357
    return $this;
  }

  /**
   * {@inheritdoc}
   */
358
359
360
361
  public function addAdjustment(Adjustment $adjustment) {
    $this->get('adjustments')->appendItem($adjustment);
    $this->recalculateTotalPrice();
    return $this;
362
363
364
365
366
  }

  /**
   * {@inheritdoc}
   */
367
368
369
  public function removeAdjustment(Adjustment $adjustment) {
    $this->get('adjustments')->removeAdjustment($adjustment);
    $this->recalculateTotalPrice();
370
371
372
    return $this;
  }

373
374
375
376
  /**
   * {@inheritdoc}
   */
  public function clearAdjustments() {
377
378
379
380
381
    $locked_callback = function ($adjustment) {
      /** @var \Drupal\commerce_order\Adjustment $adjustment */
      return $adjustment->isLocked();
    };
    // Remove all unlocked adjustments.
382
    foreach ($this->getItems() as $order_item) {
383
      /** @var \Drupal\commerce_order\Adjustment[] $adjustments */
384
      $adjustments = array_filter($order_item->getAdjustments(), $locked_callback);
385
386
387
388
389
390
391
      // 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);
392
      $order_item->setAdjustments($adjustments);
393
    }
394
    $adjustments = array_filter($this->getAdjustments(), $locked_callback);
395
    $this->setAdjustments($adjustments);
396

397
398
399
    return $this;
  }

400
401
402
  /**
   * {@inheritdoc}
   */
403
  public function collectAdjustments(array $adjustment_types = []) {
404
405
    $adjustments = [];
    foreach ($this->getItems() as $order_item) {
406
      foreach ($order_item->getAdjustments($adjustment_types) as $adjustment) {
407
408
409
410
        if ($order_item->usesLegacyAdjustments()) {
          $adjustment = $adjustment->multiply($order_item->getQuantity());
        }
        $adjustments[] = $adjustment;
411
412
      }
    }
413
    foreach ($this->getAdjustments($adjustment_types) as $adjustment) {
414
415
416
417
418
419
420
421
422
423
      $adjustments[] = $adjustment;
    }

    return $adjustments;
  }

  /**
   * {@inheritdoc}
   */
  public function getSubtotalPrice() {
424
425
    /** @var \Drupal\commerce_price\Price $subtotal_price */
    $subtotal_price = NULL;
426
427
428
    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;
429
      }
430
431
432
433
    }
    return $subtotal_price;
  }

434
435
436
437
  /**
   * {@inheritdoc}
   */
  public function recalculateTotalPrice() {
438
439
    /** @var \Drupal\commerce_price\Price $total_price */
    $total_price = NULL;
440
441
442
    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;
443
      }
444
445
    }
    if ($total_price) {
446
447
448
449
450
451
452
453
454
455
      $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());
          }
456
        }
457
      }
458
459
    }
    $this->total_price = $total_price;
460

461
462
463
    return $this;
  }

464
465
466
467
  /**
   * {@inheritdoc}
   */
  public function getTotalPrice() {
468
469
470
    if (!$this->get('total_price')->isEmpty()) {
      return $this->get('total_price')->first()->toPrice();
    }
471
472
  }

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
  /**
   * {@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
499
      return $total_price->subtract($this->getTotalPaid());
500
501
502
    }
  }

503
504
505
506
  /**
   * {@inheritdoc}
   */
  public function isPaid() {
507
508
509
510
511
    $total_price = $this->getTotalPrice();
    if (!$total_price) {
      return FALSE;
    }

512
    $balance = $this->getBalance();
513
514
    // Free orders are considered fully paid once they have been placed.
    if ($total_price->isZero()) {
515
      return $this->getState()->getId() != 'draft';
516
517
518
519
    }
    else {
      return $balance->isNegative() || $balance->isZero();
    }
520
521
  }

mglaman's avatar
mglaman committed
522
523
524
  /**
   * {@inheritdoc}
   */
525
526
  public function getState() {
    return $this->get('state')->first();
mglaman's avatar
mglaman committed
527
528
  }

529
530
531
532
  /**
   * {@inheritdoc}
   */
  public function getRefreshState() {
533
    return $this->getData('refresh_state');
534
535
536
537
538
539
  }

  /**
   * {@inheritdoc}
   */
  public function setRefreshState($refresh_state) {
540
    return $this->setData('refresh_state', $refresh_state);
541
542
  }

mglaman's avatar
mglaman committed
543
544
545
  /**
   * {@inheritdoc}
   */
546
  public function getData($key, $default = NULL) {
547
548
549
550
    $data = [];
    if (!$this->get('data')->isEmpty()) {
      $data = $this->get('data')->first()->getValue();
    }
551
    return isset($data[$key]) ? $data[$key] : $default;
552
553
554
555
556
  }

  /**
   * {@inheritdoc}
   */
557
558
  public function setData($key, $value) {
    $this->get('data')->__set($key, $value);
mglaman's avatar
mglaman committed
559
560
    return $this;
  }
561

562
563
564
565
566
567
568
569
570
571
572
573
  /**
   * {@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;
  }

574
575
576
577
  /**
   * {@inheritdoc}
   */
  public function isLocked() {
578
    return (bool) $this->get('locked')->value;
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
  }

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

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

597
598
599
  /**
   * {@inheritdoc}
   */
600
601
  public function getCreatedTime() {
    return $this->get('created')->value;
602
603
604
605
606
  }

  /**
   * {@inheritdoc}
   */
607
608
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
609
610
611
    return $this;
  }

612
613
614
  /**
   * {@inheritdoc}
   */
615
616
  public function getPlacedTime() {
    return $this->get('placed')->value;
617
618
619
620
621
  }

  /**
   * {@inheritdoc}
   */
622
623
  public function setPlacedTime($timestamp) {
    $this->set('placed', $timestamp);
624
625
626
    return $this;
  }

627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
  /**
   * {@inheritdoc}
   */
  public function getCompletedTime() {
    return $this->get('completed')->value;
  }

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

642
643
644
645
  /**
   * {@inheritdoc}
   */
  public function getCalculationDate() {
646
    $timezone = $this->getStore()->getTimezone();
647
    $timestamp = $this->getPlacedTime() ?: \Drupal::time()->getRequestTime();
648
    $date = DrupalDateTime::createFromTimestamp($timestamp, $timezone);
649
650
651
652

    return $date;
  }

653
654
655
  /**
   * {@inheritdoc}
   */
656
657
658
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

659
660
661
662
663
664
665
666
667
668
669
670
    if (isset($this->original) && !$this->isNew() && $this->original->getVersion() > $this->getVersion()) {
      $mismatch_exception = new OrderVersionMismatchException(sprintf('Attempted to save order %s with version %s. Current version is %s.', $this->id(), $this->getVersion(), $this->original->getVersion()));
      $log_only = $this->getEntityType()->get('log_version_mismatch');
      if ($log_only) {
        watchdog_exception('commerce_order', $mismatch_exception);
      }
      else {
        throw $mismatch_exception;
      }
    }
    $this->setVersion($this->getVersion() + 1);

671
672
    if ($this->isNew() && !$this->getIpAddress()) {
      $this->setIpAddress(\Drupal::request()->getClientIp());
673
    }
674
675
676
    $customer = $this->getCustomer();
    // The customer has been deleted, clear the reference.
    if ($this->getCustomerId() && $customer->isAnonymous()) {
677
      $this->setCustomerId(0);
678
679
680
    }
    // Maintain the order email.
    if (!$this->getEmail() && $customer->isAuthenticated()) {
681
      $this->setEmail($customer->getEmail());
682
    }
683
684
685
686
687
688
    // 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();
    }
689

690
    if ($this->getState()->getId() == 'draft') {
691
692
693
694
      // Refresh draft orders on every save.
      if (empty($this->getRefreshState())) {
        $this->setRefreshState(self::REFRESH_ON_SAVE);
      }
695
      // Initialize the flag for OrderStorage::doOrderPreSave().
696
697
698
      if ($this->getData('paid_event_dispatched') === NULL) {
        $this->setData('paid_event_dispatched', FALSE);
      }
699
    }
700
701
  }

702
703
704
  /**
   * {@inheritdoc}
   */
705
706
707
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

708
709
710
711
712
    // 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();
713
714
715
716
717
718
719
720
      }
    }
  }

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

723
724
725
    // Delete the order items of a deleted order.
    $order_items = [];
    /** @var \Drupal\commerce_order\Entity\OrderInterface $entity */
726
    foreach ($entities as $entity) {
727
728
      foreach ($entity->getItems() as $order_item) {
        $order_items[$order_item->id()] = $order_item;
729
730
      }
    }
731
732
733
    /** @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);
734
735
  }

736
737
738
  /**
   * {@inheritdoc}
   */
739
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
bojanz's avatar
bojanz committed
740
    $fields = parent::baseFieldDefinitions($entity_type);
741

742
    $fields['order_number'] = BaseFieldDefinition::create('string')
743
744
745
746
747
      ->setLabel(t('Order number'))
      ->setDescription(t('The order number displayed to the customer.'))
      ->setRequired(TRUE)
      ->setDefaultValue('')
      ->setSetting('max_length', 255)
748
749
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
750

751
752
753
754
755
756
757
758
    $fields['version'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Version'))
      ->setDescription(t('The order version number, it gets incremented on each save.'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE)
      // Default to zero, so that the first save is version one.
      ->setDefaultValue(0);

759
760
761
762
763
764
765
766
767
768
769
    $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);

770
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
771
772
      ->setLabel(t('Customer'))
      ->setDescription(t('The customer.'))
773
      ->setSetting('target_type', 'user')
774
      ->setSetting('handler', 'default')
775
      ->setDefaultValueCallback('Drupal\commerce_order\Entity\Order::getCurrentUserId')
776
      ->setTranslatable(TRUE)
777
      ->setDisplayOptions('view', [
778
        'label' => 'above',
779
780
        'type' => 'author',
        'weight' => 0,
781
      ])
782
783
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
784

785
    $fields['mail'] = BaseFieldDefinition::create('email')
786
      ->setLabel(t('Contact email'))
787
      ->setDescription(t('The email address associated with the order.'))
788
789
      ->setDefaultValue('')
      ->setSetting('max_length', 255)
790
      ->setDisplayOptions('view', [
791
        'label' => 'above',
792
793
        'type' => 'string',
        'weight' => 0,
794
      ])
795
796
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
797

798
799
800
801
802
803
804
805
806
807
808
    $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', [
809
        'region' => 'hidden',
810
811
812
813
814
        'weight' => 0,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

815
    $fields['billing_profile'] = BaseFieldDefinition::create('entity_reference_revisions')
816
      ->setLabel(t('Billing information'))
817
818
819
      ->setDescription(t('Billing profile'))
      ->setSetting('target_type', 'profile')
      ->setSetting('handler', 'default')
820
      ->setSetting('handler_settings', ['target_bundles' => ['customer' => 'customer']])
821
822
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', [
823
        'type' => 'commerce_billing_profile',
824
825
826
827
828
829
        'weight' => 0,
        'settings' => [],
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
    $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);

853
854
855
856
857
858
859
    $fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
      ->setLabel(t('Adjustments'))
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
      ->setDisplayOptions('form', [
        'type' => 'commerce_adjustment_default',
        'weight' => 0,
      ])
860
      ->setDisplayConfigurable('form', TRUE)
861
862
      ->setDisplayConfigurable('view', FALSE);

863
    $fields['total_price'] = BaseFieldDefinition::create('commerce_price')
864
      ->setLabel(t('Total price'))
865
866
      ->setDescription(t('The total price of the order.'))
      ->setReadOnly(TRUE)
867
868
869
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'commerce_order_total_summary',
870
        'weight' => 0,
871
      ])
872
873
874
      ->setDisplayConfigurable('form', FALSE)
      ->setDisplayConfigurable('view', TRUE);

875
876
877
878
879
880
    $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);

881
882
883
884
885
886
887
888
889
    $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);

890
891
892
893
894
    $fields['state'] = BaseFieldDefinition::create('state')
      ->setLabel(t('State'))
      ->setDescription(t('The order state.'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
895
      ->setDisplayOptions('view', [
896
        'label' => 'hidden',
897
        'type' => 'state_transition_form',
898
899
        'settings' => [
          'require_confirmation' => TRUE,
900
          'use_modal' => TRUE,
901
        ],
902
        'weight' => 10,
903
      ])
904
      ->setDisplayConfigurable('form', TRUE)
905
906
      ->setDisplayConfigurable('view', TRUE)
      ->setSetting('workflow_callback', ['\Drupal\commerce_order\Entity\Order', 'getWorkflowId']);
907

908
    $fields['data'] = BaseFieldDefinition::create('map')
909
910
911
      ->setLabel(t('Data'))
      ->setDescription(t('A serialized array of additional data.'));

912
913
914
915
916
917
918
919
    $fields['locked'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Locked'))
      ->setSettings([
        'on_label' => t('Yes'),
        'off_label' => t('No'),
      ])
      ->setDefaultValue(FALSE);

920
921
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
mglaman's avatar
mglaman committed
922
923
924
925
      ->setDescription(t('The time when the order was created.'));

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
926
927
      ->setDescription(t('The time when the order was last edited.'))
      ->setDisplayConfigurable('view', TRUE);
mglaman's avatar
mglaman committed
928
929
930
931

    $fields['placed'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Placed'))
      ->setDescription(t('The time when the order was placed.'))
932
      ->setDisplayOptions('view', [
933
        'label' => 'above',
934
        'type' => 'timestamp',
935
        'weight' => 0,
936
937
      ])
      ->setDisplayConfigurable('view', TRUE);
938

939
940
941
942
943
944
945
946
947
948
    $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);

949
950
951
    return $fields;
  }

952
953
954
955
956
957
958
959
960
  /**
   * Default value callback for 'uid' base field definition.
   *
   * @see ::baseFieldDefinitions()
   *
   * @return array
   *   An array of default values.
   */
  public static function getCurrentUserId() {
961
    return [\Drupal::currentUser()->id()];
962
963
  }

964
  /**
965
   * Gets the workflow ID for the state field.
966
967
968
969
970
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The order.
   *
   * @return string
971
   *   The workflow ID.
972
   */
973
974
  public static function getWorkflowId(OrderInterface $order) {
    $workflow = OrderType::load($order->bundle())->getWorkflowId();
975
976
977
    return $workflow;
  }

978
}