Example #1
0
 /**
  * {@inheritdoc}
  */
 public function fulfillOrder(OrderInterface $order, array $package_ids)
 {
     $shipment = new \stdClass();
     $shipment->order_id = $order->id();
     $shipment->packages = array();
     foreach ($package_ids as $id) {
         $package = Package::load($id);
         $shipment->packages[$id] = $package;
     }
     return \Drupal::formBuilder()->getForm('\\Drupal\\uc_fulfillment\\Form\\ShipmentEditForm', $order, $shipment);
 }
 /**
  * Displays a list of an order's packaged products.
  *
  * @param \Drupal\uc_order\OrderInterface $uc_order
  *   The order.
  *
  * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  *   A render array, or a redirect response if there are no packaged products.
  */
 public function listOrderPackages(OrderInterface $uc_order)
 {
     $shipping_type_options = uc_quote_shipping_type_options();
     $header = array($this->t('Package ID'), $this->t('Products'), $this->t('Shipping type'), $this->t('Package type'), $this->t('Shipment ID'), $this->t('Tracking number'), $this->t('Labels'), $this->t('Actions'));
     $rows = array();
     $result = db_query('SELECT package_id FROM {uc_packages} WHERE order_id = :id', [':id' => $uc_order->id()]);
     while ($package_id = $result->fetchField()) {
         $package = Package::load($package_id);
         $row = array();
         // Package ID.
         $row[] = array('data' => array('#plain_text' => $package->package_id));
         $product_list = array();
         $result2 = db_query('SELECT op.order_product_id, pp.qty, op.title, op.model FROM {uc_packaged_products} pp LEFT JOIN {uc_order_products} op ON op.order_product_id = pp.order_product_id WHERE pp.package_id = :id', [':id' => $package->package_id]);
         foreach ($result2 as $product) {
             $product_list[] = $product->qty . ' x ' . $product->model;
         }
         // Products.
         $row[] = array('data' => array('#theme' => 'item_list', '#items' => $product_list));
         // Shipping type.
         $row[] = isset($shipping_type_options[$package->shipping_type]) ? $shipping_type_options[$package->shipping_type] : strtr($package->shipping_type, '_', ' ');
         // Package type.
         $row[] = array('data' => array('#plain_text' => $package->pkg_type));
         // Shipment ID.
         $row[] = isset($package->sid) ? Link::createFromRoute($package->sid, 'uc_fulfillment.view_shipment', ['uc_order' => $uc_order->id(), 'shipment_id' => $package->sid])->toString() : '';
         // Tracking number.
         $row[] = isset($package->tracking_number) ? array('data' => array('#plain_text' => $package->tracking_number)) : '';
         if ($package->label_image && ($image = file_load($package->label_image))) {
             $package->label_image = $image;
         } else {
             unset($package->label_image);
         }
         // Shipping label.
         if (isset($package->sid) && isset($package->label_image)) {
             $method = db_query('SELECT shipping_method FROM {uc_shipments} WHERE sid = :sid', [':sid' => $package->sid])->fetchField();
             $row[] = Link::fromTextAndUrl("image goes here", Url::fromUri('base:admin/store/orders/' . $uc_order->id() . '/shipments/labels/' . $method . '/' . $package->label_image->uri, ['uc_order' => $uc_order->id(), 'method' => $method, 'image_uri' => $package->label_image->uri]))->toString();
         } else {
             $row[] = '';
         }
         // Operations.
         $ops = array('#type' => 'operations', '#links' => array('edit' => array('title' => $this->t('Edit'), 'url' => Url::fromRoute('uc_fulfillment.edit_package', ['uc_order' => $uc_order->id(), 'package_id' => $package->package_id])), 'ship' => array('title' => $this->t('Ship'), 'url' => Url::fromRoute('uc_fulfillment.new_shipment', ['uc_order' => $uc_order->id()], ['query' => ['pkgs' => $package->package_id]])), 'delete' => array('title' => $this->t('Delete'), 'url' => Url::fromRoute('uc_fulfillment.delete_package', ['uc_order' => $uc_order->id(), 'package_id' => $package->package_id]))));
         if ($package->sid) {
             $ops['#links']['cancel'] = array('title' => $this->t('Cancel'), 'url' => Url::fromRoute('uc_fulfillment.cancel_package', ['uc_order' => $uc_order->id(), 'package_id' => $package->package_id]));
         }
         $row[] = array('data' => $ops);
         $rows[] = $row;
     }
     if (empty($rows)) {
         drupal_set_message($this->t("This order's products have not been organized into packages."), 'warning');
         return $this->redirect('uc_fulfillment.new_package', ['uc_order' => $uc_order->id()]);
     }
     $build['packages'] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows);
     return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, $package_id = NULL)
 {
     $this->package = Package::load($package_id);
     $form['#tree'] = TRUE;
     $form['#attached']['library'][] = 'uc_fulfillment/uc_fulfillment.scripts';
     $products = array();
     $shipping_types_products = array();
     foreach ($uc_order->products as $product) {
         if (uc_order_product_is_shippable($product)) {
             $product->shipping_type = uc_product_get_shipping_type($product);
             $shipping_types_products[$product->shipping_type][$product->order_product_id->value] = $product;
             $products[$product->order_product_id->value] = $product;
         }
     }
     $header = array(array('data' => '', 'class' => array('select-all')), 'model' => $this->t('SKU'), 'name' => $this->t('Title'), 'qty' => $this->t('Quantity'));
     $result = db_query('SELECT order_product_id, SUM(qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id WHERE p.order_id = :id GROUP BY order_product_id', [':id' => $uc_order->id()]);
     foreach ($result as $packaged_product) {
         // Make already packaged products unavailable, except those in this package.
         $products[$packaged_product->order_product_id]->qty->value -= $packaged_product->quantity;
         if (isset($this->package->products[$packaged_product->order_product_id])) {
             $products[$packaged_product->order_product_id]->qty->value += $this->package->products[$packaged_product->order_product_id]->qty;
         }
     }
     $form['products'] = array('#type' => 'table', '#header' => $header, '#empty' => $this->t('There are no products available for this type of package.'));
     foreach ($products as $product) {
         if ($product->qty->value > 0) {
             $row = array();
             $row['checked'] = array('#type' => 'checkbox', '#default_value' => isset($this->package->products[$product->order_product_id->value]));
             $row['model'] = array('#markup' => $product->model->value);
             $row['name'] = array('#markup' => $product->title->value);
             $range = range(1, $product->qty->value);
             $row['qty'] = array('#type' => 'select', '#options' => array_combine($range, $range), '#default_value' => isset($this->package->products[$product->order_product_id->value]) ? $this->package->products[$product->order_product_id->value]->qty : 1);
             $form['products'][$product->order_product_id->value] = $row;
         }
     }
     $options = array();
     $shipping_type_options = uc_quote_shipping_type_options();
     foreach (array_keys($shipping_types_products) as $type) {
         $options[$type] = isset($shipping_type_options[$type]) ? $shipping_type_options[$type] : Unicode::ucwords(str_replace('_', ' ', $type));
     }
     $form['shipping_type'] = array('#type' => 'select', '#title' => $this->t('Shipping type'), '#options' => $options, '#default_value' => isset($this->package->shipping_type) ? $this->package->shipping_type : 'small_package');
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $shipment = new \stdClass();
     $shipment->order_id = $form_state->getValue('order_id');
     if ($form_state->hasValue('sid')) {
         $shipment->sid = $form_state->getValue('sid');
     }
     $shipment->origin = (object) $form_state->getValue('pickup_address');
     $shipment->destination = new \stdClass();
     foreach ($form_state->getValues() as $key => $value) {
         if (substr($key, 0, 9) == 'delivery_') {
             $field = substr($key, 9);
             $shipment->destination->{$field} = $value;
         }
     }
     $shipment->packages = array();
     foreach ($form_state->getValue('packages') as $id => $pkg_form) {
         $package = Package::load($id);
         $package->pkg_type = $pkg_form['pkg_type'];
         $package->value = $pkg_form['declared_value'];
         $package->length = $pkg_form['dimensions']['length'];
         $package->width = $pkg_form['dimensions']['width'];
         $package->height = $pkg_form['dimensions']['height'];
         $package->length_units = $pkg_form['dimensions']['units'];
         $package->tracking_number = $pkg_form['tracking_number'];
         $package->qty = 1;
         $shipment->packages[$id] = $package;
     }
     $shipment->shipping_method = $form_state->getValue('shipping_method');
     $shipment->accessorials = $form_state->getValue('accessorials');
     $shipment->carrier = $form_state->getValue('carrier');
     $shipment->transaction_id = $form_state->getValue('transaction_id');
     $shipment->tracking_number = $form_state->getValue('tracking_number');
     $shipment->ship_date = $form_state->getValue('ship_date')->getTimestamp();
     $shipment->expected_delivery = $form_state->getValue('expected_delivery')->getTimestamp();
     $shipment->cost = $form_state->getValue('cost');
     $shipment->save();
     $form_state->setRedirect('uc_fulfillment.shipments', ['uc_order' => $form_state->getValue('order_id')]);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $package = Package::load($form_state->getValue('package_id'));
     $shipment = Shipment::load($package->sid);
     $methods = \Drupal::moduleHandler()->invokeAll('uc_fulfillment_method');
     if (isset($methods[$shipment->shipping_method]['cancel']) && function_exists($methods[$shipment->shipping_method]['cancel'])) {
         $result = call_user_func($methods[$shipment->shipping_method]['cancel'], $shipment->tracking_number, array($package->tracking_number));
         if ($result) {
             db_update('uc_packages')->fields(array('sid' => NULL, 'label_image' => NULL, 'tracking_number' => NULL))->condition('package_id', $package->package_id)->execute();
             if (isset($package->label_image)) {
                 file_usage_delete($package->label_image, 'uc_fulfillment', 'package', $package->package_id);
                 file_delete($package->label_image);
                 unset($package->label_image);
             }
             unset($shipment->packages[$package->package_id]);
             if (!count($shipment->packages)) {
                 $shipment->delete();
             }
         }
     }
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $uc_order = NULL, $package_id = NULL)
 {
     $this->package = Package::load($package_id);
     $this->order_id = $uc_order->id();
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function convert($value, $definition, $name, array $defaults)
 {
     return Package::load($value);
 }