You are here

function uc_ups_fulfill_order in Ubercart 8.4

Same name and namespace in other branches
  1. 5 shipping/uc_ups/uc_ups.module \uc_ups_fulfill_order()
  2. 6.2 shipping/uc_ups/uc_ups.ship.inc \uc_ups_fulfill_order()
  3. 7.3 shipping/uc_ups/uc_ups.ship.inc \uc_ups_fulfill_order()

Shipment creation callback.

Confirms shipment data before requesting a shipping label.

Parameters

\Drupal\uc_order\OrderInterface $order: The order entity for the shipment.

array $package_ids: Array of package ids to shipped.

See also

uc_ups_fulfill_order_validate()

uc_ups_fulfill_order_submit()

1 string reference to 'uc_ups_fulfill_order'
uc_ups_uc_shipping_method in shipping/uc_ups/uc_ups.module
Implements hook_uc_shipping_method().

File

shipping/uc_ups/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc, line 30
UPS functions for label generation.

Code

function uc_ups_fulfill_order($form, FormStateInterface $form_state, OrderInterface $order, array $package_ids) {
  $store_config = \Drupal::config('uc_store.settings');
  $pkg_types = UPSUtilities::packageTypes();
  $form['order_id'] = [
    '#type' => 'value',
    '#value' => $order
      ->id(),
  ];
  $packages = [];
  $addresses = [];

  // Container for package data.
  $form['packages'] = [
    '#type' => 'fieldset',
    '#title' => t('Packages'),
    '#tree' => TRUE,
  ];
  foreach ($package_ids as $id) {
    $package = uc_shipping_package_load($id);
    if ($package) {
      foreach ($package->addresses as $address) {
        if (!in_array($address, $addresses)) {
          $addresses[] = $address;
        }
      }

      // Create list of products and get a representative product (last one in
      // the loop) to use for some default values.
      $product_list = [];
      $declared_value = 0;
      foreach ($package->products as $product) {
        $product_list[] = $product->qty . ' x ' . $product->model;
        $declared_value += $product->qty * $product->price;
      }

      // Use last product in package to determine package type.
      $ups_data = \Drupal::database()
        ->query("SELECT pkg_type FROM {uc_ups_products} WHERE nid = :nid", [
        ':nid' => $product->nid,
      ])
        ->fetchAssoc();
      $product->ups = $ups_data;
      $pkg_form = [
        '#type' => 'fieldset',
        '#title' => t('Package @id', [
          '@id' => $id,
        ]),
      ];
      $pkg_form['products'] = [
        '#theme' => 'item_list',
        '#items' => $product_list,
      ];
      $pkg_form['package_id'] = [
        '#type' => 'hidden',
        '#value' => $id,
      ];
      $pkg_form['pkg_type'] = [
        '#type' => 'select',
        '#title' => t('Package type'),
        '#options' => $pkg_types,
        '#default_value' => $product->ups['pkg_type'],
        '#required' => TRUE,
      ];
      $pkg_form['declared_value'] = [
        '#type' => 'textfield',
        '#title' => t('Declared value'),
        '#default_value' => $declared_value,
        '#required' => TRUE,
      ];
      $pkg_form['weight'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'uc-inline-form',
            'clearfix',
          ],
        ],
        '#description' => t('Weight of the package. Default value is sum of product weights in the package.'),
        '#weight' => 15,
      ];
      $pkg_form['weight']['weight'] = [
        '#type' => 'textfield',
        '#title' => t('Weight'),
        '#default_value' => isset($package->weight) ? $package->weight : 0,
        '#size' => 10,
        '#maxlength' => 15,
      ];
      $pkg_form['weight']['units'] = [
        '#type' => 'select',
        '#title' => t('Units'),
        '#options' => [
          'lb' => t('Pounds'),
          'kg' => t('Kilograms'),
          'oz' => t('Ounces'),
          'g' => t('Grams'),
        ],
        '#default_value' => isset($package->weight_units) ? $package->weight_units : $store_config
          ->get('weight.units'),
      ];
      $pkg_form['dimensions'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'uc-inline-form',
            'clearfix',
          ],
        ],
        '#description' => t('Physical dimensions of the package.'),
        '#weight' => 20,
      ];
      $pkg_form['dimensions']['length'] = [
        '#type' => 'textfield',
        '#title' => t('Length'),
        '#default_value' => isset($package->length) ? $package->length : 1,
        '#size' => 10,
      ];
      $pkg_form['dimensions']['width'] = [
        '#type' => 'textfield',
        '#title' => t('Width'),
        '#default_value' => isset($package->width) ? $package->width : 1,
        '#size' => 10,
      ];
      $pkg_form['dimensions']['height'] = [
        '#type' => 'textfield',
        '#title' => t('Height'),
        '#default_value' => isset($package->height) ? $package->height : 1,
        '#size' => 10,
      ];
      $pkg_form['dimensions']['units'] = [
        '#type' => 'select',
        '#title' => t('Units'),
        '#options' => [
          'in' => t('Inches'),
          'ft' => t('Feet'),
          'cm' => t('Centimeters'),
          'mm' => t('Millimeters'),
        ],
        '#default_value' => isset($package->length_units) ? $package->length_units : $store_config
          ->get('length.units'),
      ];
      $form['packages'][$id] = $pkg_form;
    }
  }
  $form = uc_shipping_address_form($form, $form_state, $addresses, $order);
  foreach ([
    'delivery_email',
    'delivery_last_name',
    'delivery_street1',
    'delivery_city',
    'delivery_country',
    'delivery_postal_code',
  ] as $field) {
    $form['destination'][$field]['#required'] = TRUE;
  }

  // Determine shipping option chosen by the customer.
  $method = $order->quote['method'];
  $methods = \Drupal::moduleHandler()
    ->invokeAll('uc_shipping_method');
  if (isset($methods[$method])) {
    $services = $methods[$method]['quote']['accessorials'];
    $method = $services[$order->quote['accessorials']];
  }

  // Container for shipment data.
  $form['shipment'] = [
    '#type' => 'fieldset',
    '#title' => t('Shipment data'),
  ];

  // Inform user of customer's shipping choice.
  $form['shipment']['shipping_choice'] = [
    '#type' => 'markup',
    '#prefix' => '<div>',
    '#markup' => t('Customer selected "@method" as the shipping method and paid @rate', [
      '@method' => $method,
      '@rate' => uc_currency_format($order->quote['rate']),
    ]),
    '#suffix' => '</div>',
  ];

  // Pass shipping charge paid information on to validation function so it
  // can be displayed alongside actual costs.
  $form['shipment']['paid'] = [
    '#type' => 'value',
    '#value' => uc_currency_format($order->quote['rate']),
  ];
  $services = _uc_ups_service_list();
  $default_service = '';
  if ($method == 'ups') {
    $default_service = $order->quote['accessorials'];
  }
  $form['shipment']['service'] = [
    '#type' => 'select',
    '#title' => t('UPS service'),
    '#options' => $services,
    '#default_value' => $default_service,
  ];
  $today = getdate();
  $form['shipment']['ship_date'] = [
    '#type' => 'date',
    '#title' => t('Ship date'),
    '#default_value' => [
      'year' => $today['year'],
      'month' => $today['mon'],
      'day' => $today['mday'],
    ],
  ];
  $form['shipment']['expected_delivery'] = [
    '#type' => 'date',
    '#title' => t('Expected delivery'),
    '#default_value' => [
      'year' => $today['year'],
      'month' => $today['mon'],
      'day' => $today['mday'],
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('Review shipment'),
  ];
  return $form;
}