You are here

public function Shipment::save in Ubercart 8.4

Saves this shipment.

File

shipping/uc_fulfillment/src/Shipment.php, line 439

Class

Shipment
Defines the Shipment class.

Namespace

Drupal\uc_fulfillment

Code

public function save() {
  $this->changed = \Drupal::time()
    ->getCurrentTime();

  // Break Address objects into individual fields for saving.
  $fields = [];
  if (isset($this->origin)) {
    foreach ($this->origin as $field => $value) {
      $field = 'o_' . $field;
      $fields[$field] = $value;
    }
  }
  if (isset($this->destination)) {
    foreach ($this->destination as $field => $value) {
      $field = 'd_' . $field;
      $fields[$field] = $value;
    }
  }

  // Yuck.
  $fields += [
    'order_id' => $this->orderId,
    'shipping_method' => $this->shipping_method,
    'accessorials' => $this->accessorials,
    'carrier' => $this->carrier,
    'transaction_id' => $this->transaction_id,
    'tracking_number' => $this->tracking_number,
    'ship_date' => $this->ship_date,
    'expected_delivery' => $this->expected_delivery,
    'cost' => $this->cost,
    'currency' => $this->currency,
    'changed' => $this->changed,
  ];
  if (!isset($this->sid)) {
    $this->sid = \Drupal::database()
      ->insert('uc_shipments')
      ->fields($fields)
      ->execute();
    $this->is_new = TRUE;
  }
  else {
    \Drupal::database()
      ->update('uc_shipments')
      ->fields($fields)
      ->condition('sid', $this->sid, '=')
      ->execute();
    $this->is_new = FALSE;
  }
  if (is_array($this->packages)) {
    foreach ($this->packages as $package) {
      $package
        ->setSid($this->sid);

      // Since the products haven't changed, we take them out of the object so
      // that they are not deleted and re-inserted.
      $products = $package
        ->getProducts();
      $package
        ->setProducts([]);
      $package
        ->save();

      // But they're still necessary for hook_uc_shipment(), so they're added
      // back in.
      $package
        ->setProducts($products);
    }
  }
  \Drupal::moduleHandler()
    ->invokeAll('uc_shipment', [
    'save',
    $this,
  ]);
  $order = Order::load($this->orderId);

  /* rules_invoke_event('uc_shipment_save', $order, $this); */
  $event = new ShipmentSaveEvent($order, $this);
  \Drupal::service('event_dispatcher')
    ->dispatch($event::EVENT_NAME, $event);
}