Exemple #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 submitForm(array &$form, FormStateInterface $form_state)
 {
     foreach ($form_state->getValue('products') as $id => $product) {
         if ($product['checked']) {
             $this->package->products[$id] = (object) $product;
         } else {
             unset($this->package->products[$id]);
         }
     }
     $this->package->shipping_type = $form_state->getValue('shipping_type');
     $this->package->save();
     $form_state->setRedirect('uc_fulfillment.packages', ['uc_order' => $this->package->order_id]);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     if ($this->t('Cancel') != (string) $form_state->getValue('op')) {
         // Package 0 is a temporary array, all other elements are Package objects.
         $packages = array(0 => array());
         foreach ($form_state->getValue('shipping_types') as $shipping_type => $products) {
             foreach ($products['table'] as $id => $product) {
                 if ($product['checked']) {
                     if ($this->t('Create one package') == (string) $form_state->getValue('op')) {
                         $product['package'] = 1;
                     }
                     if ($product['package'] != 0) {
                         if (empty($packages[$product['package']])) {
                             $packages[$product['package']] = Package::create();
                         }
                         $packages[$product['package']]->products[$id] = (object) $product;
                         if (!isset($packages[$product['package']]->shipping_type)) {
                             $packages[$product['package']]->shipping_type = $shipping_type;
                         }
                     } else {
                         $packages[0][$shipping_type][$id] = (object) $product;
                     }
                 }
             }
             if (isset($packages[0][$shipping_type])) {
                 // We reach here if some packages were checked and marked "Separate".
                 // That can only happen when "Make packages" button was pushed.
                 foreach ($packages[0][$shipping_type] as $id => $product) {
                     $qty = $product->qty;
                     $product->qty = 1;
                     // Create a package for each product.
                     for ($i = 0; $i < $qty; $i++) {
                         $packages[] = Package::create(['products' => [$id => $product], 'shipping_type' => $shipping_type]);
                     }
                 }
             }
             // "Separate" packaging is now finished.
             unset($packages[0][$shipping_type]);
         }
         if (empty($packages[0])) {
             // This should always be true?
             unset($packages[0]);
         }
         foreach ($packages as $package) {
             $package->order_id = $form_state->getValue('order_id');
             $package->save();
         }
         $form_state->setRedirect('uc_fulfillment.packages', ['uc_order' => $form_state->getValue('order_id')]);
     } else {
         // Fall through, if user chose "Cancel".
         $form_state->setRedirect('entity.uc_order.canonical', ['uc_order' => $form_state->getValue('order_id')]);
     }
 }
 /**
  * {@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 submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->package->delete();
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
 /**
  * {@inheritdoc}
  */
 public function convert($value, $definition, $name, array $defaults)
 {
     return Package::load($value);
 }