Ejemplo n.º 1
0
 /**
  * @param int $orderId
  * @param array $invoiceData
  * @return bool
  */
 protected function createInvoice($orderId, $invoiceData)
 {
     $order = $this->orderFactory->create()->load($orderId);
     if (!$order) {
         return false;
     }
     $invoice = $this->serviceOrderFactory->create(['order' => $order])->prepareInvoice($invoiceData);
     return $invoice;
 }
Ejemplo n.º 2
0
 /**
  * Initialize creditmemo model instance
  *
  * @return \Magento\Sales\Model\Order\Creditmemo|false
  */
 public function load()
 {
     $creditmemo = false;
     $creditmemoId = $this->getCreditmemoId();
     $orderId = $this->getOrderId();
     if ($creditmemoId) {
         $creditmemo = $this->creditmemoFactory->create()->load($creditmemoId);
     } elseif ($orderId) {
         $data = $this->getCreditmemo();
         $order = $this->orderFactory->create()->load($orderId);
         $invoice = $this->_initInvoice($order);
         if (!$this->_canCreditmemo($order)) {
             return false;
         }
         $savedData = $this->_getItemData();
         $qtys = array();
         $backToStock = array();
         foreach ($savedData as $orderItemId => $itemData) {
             if (isset($itemData['qty'])) {
                 $qtys[$orderItemId] = $itemData['qty'];
             }
             if (isset($itemData['back_to_stock'])) {
                 $backToStock[$orderItemId] = true;
             }
         }
         $data['qtys'] = $qtys;
         $service = $this->orderServiceFactory->create(array('order' => $order));
         if ($invoice) {
             $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
         } else {
             $creditmemo = $service->prepareCreditmemo($data);
         }
         /**
          * Process back to stock flags
          */
         foreach ($creditmemo->getAllItems() as $creditmemoItem) {
             $orderItem = $creditmemoItem->getOrderItem();
             $parentId = $orderItem->getParentItemId();
             if (isset($backToStock[$orderItem->getId()])) {
                 $creditmemoItem->setBackToStock(true);
             } elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) {
                 $creditmemoItem->setBackToStock(true);
             } elseif (empty($savedData)) {
                 $creditmemoItem->setBackToStock($this->inventoryHelper->isAutoReturnEnabled());
             } else {
                 $creditmemoItem->setBackToStock(false);
             }
         }
     }
     $this->eventManager->dispatch('adminhtml_sales_order_creditmemo_register_before', array('creditmemo' => $creditmemo, 'input' => $this->getCreditmemo()));
     $this->registry->register('current_creditmemo', $creditmemo);
     return $creditmemo;
 }
Ejemplo n.º 3
0
 /**
  * Initialize shipment model instance
  *
  * @param RequestInterface $request
  * @return bool|\Magento\Sales\Model\Order\Shipment
  * @throws \Magento\Framework\Model\Exception
  */
 public function load(RequestInterface $request)
 {
     $shipment = false;
     $shipmentId = $request->getParam('shipment_id');
     $orderId = $request->getParam('order_id');
     if ($shipmentId) {
         $shipment = $this->shipmentFactory->create()->load($shipmentId);
     } elseif ($orderId) {
         $order = $this->orderFactory->create()->load($orderId);
         /**
          * Check order existing
          */
         if (!$order->getId()) {
             $this->messageManager->addError(__('The order no longer exists.'));
             return false;
         }
         /**
          * Check shipment is available to create separate from invoice
          */
         if ($order->getForcedShipmentWithInvoice()) {
             $this->messageManager->addError(__('Cannot do shipment for the order separately from invoice.'));
             return false;
         }
         /**
          * Check shipment create availability
          */
         if (!$order->canShip()) {
             $this->messageManager->addError(__('Cannot do shipment for the order.'));
             return false;
         }
         $savedQtys = $this->_getItemQtys($request);
         $shipment = $this->orderServiceFactory->create(array('order' => $order))->prepareShipment($savedQtys);
         $tracks = $request->getPost('tracking');
         if ($tracks) {
             foreach ($tracks as $data) {
                 if (empty($data['number'])) {
                     throw new \Magento\Framework\Model\Exception(__('Please enter a tracking number.'));
                 }
                 $track = $this->trackFactory->create()->addData($data);
                 $shipment->addTrack($track);
             }
         }
     }
     $this->registry->register('current_shipment', $shipment);
     return $shipment;
 }
Ejemplo n.º 4
0
 /**
  * Create new shipment with maximum qty for shipping for each item
  *
  * @param array $qtys
  * @return \Magento\Sales\Model\Order\Shipment
  */
 public function prepareShipment($qtys = [])
 {
     $shipment = $this->_serviceOrderFactory->create(['order' => $this])->prepareShipment($qtys);
     return $shipment;
 }
Ejemplo n.º 5
0
 /**
  * Create new shipment with maximum qty for shipping for each item
  *
  * @param array $qtys
  * @return \Magento\Sales\Model\Order\Shipment
  */
 public function prepareShipment($qtys = array())
 {
     $shipment = $this->_serviceOrderFactory->create(array('order' => $this))->prepareShipment($qtys);
     return $shipment;
 }