/** * @param array|null $tracks * @dataProvider createDataProvider * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testCreate($tracks) { $orderItem = $this->getMock('Magento\\Sales\\Model\\Order\\Item', ['getId', 'getQtyOrdered'], [], '', false); $orderItem->expects($this->any())->method('getId')->willReturn(1); $orderItem->expects($this->any())->method('getQtyOrdered')->willReturn(5); $shipmentItem = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment\\Item', ['setQty'], [], '', false); $shipmentItem->expects($this->once())->method('setQty')->with(5); $order = $this->getMock('Magento\\Sales\\Model\\Order', ['getAllItems'], [], '', false); $order->expects($this->any())->method('getAllItems')->willReturn([$orderItem]); $shipment = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment', ['addItem', 'setTotalQty', 'addTrack'], [], '', false); $shipment->expects($this->once())->method('addItem')->with($shipmentItem); $shipment->expects($this->once())->method('setTotalQty')->with(5)->willReturn($shipment); $this->converter->expects($this->any())->method('toShipment')->with($order)->willReturn($shipment); $this->converter->expects($this->any())->method('itemToShipmentItem')->with($orderItem)->willReturn($shipmentItem); if ($tracks) { $shipmentTrack = $this->getMock('Magento\\Sales\\Model\\Order\\Shipment\\Track', ['addData'], [], '', false); if (empty($tracks[0]['number'])) { $shipmentTrack->expects($this->never())->method('addData'); $this->trackFactory->expects($this->never())->method('create'); $shipment->expects($this->never())->method('addTrack'); $this->setExpectedException('Magento\\Framework\\Exception\\LocalizedException'); } else { $shipmentTrack->expects($this->once())->method('addData')->willReturnSelf(); $this->trackFactory->expects($this->once())->method('create')->willReturn($shipmentTrack); $shipment->expects($this->once())->method('addTrack')->with($shipmentTrack); } } $this->assertEquals($shipment, $this->subject->create($order, ['1' => 5], $tracks)); }
/** * @magentoDataFixture Magento/Sales/_files/order.php */ public function testConvertToCreditmemo() { /** @var \Magento\Sales\Model\Order $order */ $order = Bootstrap::getObjectManager()->create('Magento\\Sales\\Model\\Order'); $order->loadByIncrementId('100000001'); //MAGETWO-45612 fix $order->setBaseShippingAmount(5); $this->assertNull($this->_model->toCreditmemo($order)->getBaseShippingAmount()); }
/** * @param Order $order * @param array $qtys * @return \Magento\Sales\Model\Order\Invoice * @throws \Magento\Framework\Exception\LocalizedException */ public function prepareInvoice(Order $order, array $qtys = []) { $invoice = $this->orderConverter->toInvoice($order); $totalQty = 0; foreach ($order->getAllItems() as $orderItem) { if (!$this->_canInvoiceItem($orderItem)) { continue; } $item = $this->orderConverter->itemToInvoiceItem($orderItem); if ($orderItem->isDummy()) { $qty = $orderItem->getQtyOrdered() ? $orderItem->getQtyOrdered() : 1; } elseif (isset($qtys[$orderItem->getId()])) { $qty = (double) $qtys[$orderItem->getId()]; } else { $qty = $orderItem->getQtyToInvoice(); } $totalQty += $qty; $this->setInvoiceItemQuantity($item, $qty); $invoice->addItem($item); } $invoice->setTotalQty($totalQty); $invoice->collectTotals(); $order->getInvoiceCollection()->addItem($invoice); return $invoice; }
/** * Adds items to the shipment. * * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment * @param \Magento\Sales\Model\Order $order * @param array $items * @return \Magento\Sales\Api\Data\ShipmentInterface */ protected function prepareItems(\Magento\Sales\Api\Data\ShipmentInterface $shipment, \Magento\Sales\Model\Order $order, array $items = []) { $totalQty = 0; foreach ($order->getAllItems() as $orderItem) { if (!$this->canShipItem($orderItem, $items)) { continue; } /** @var \Magento\Sales\Model\Order\Shipment\Item $item */ $item = $this->converter->itemToShipmentItem($orderItem); if ($orderItem->isDummy(true)) { $qty = 0; if (isset($items[$orderItem->getParentItemId()])) { $productOptions = $orderItem->getProductOptions(); if (isset($productOptions['bundle_selection_attributes'])) { $bundleSelectionAttributes = unserialize($productOptions['bundle_selection_attributes']); if ($bundleSelectionAttributes) { $qty = $bundleSelectionAttributes['qty'] * $items[$orderItem->getParentItemId()]; $qty = min($qty, $orderItem->getSimpleQtyToShip()); $item->setQty($qty); $shipment->addItem($item); continue; } else { $qty = 1; } } } else { $qty = 1; } } else { if (isset($items[$orderItem->getId()])) { $qty = min($items[$orderItem->getId()], $orderItem->getQtyToShip()); } elseif (!count($items)) { $qty = $orderItem->getQtyToShip(); } else { continue; } } $totalQty += $qty; $item->setQty($qty); $shipment->addItem($item); } return $shipment->setTotalQty($totalQty); }
/** * Prepare order creditmemo based on invoice items and requested requested params * * @param object $invoice * @param array $data * @return \Magento\Sales\Model\Order\Creditmemo * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function prepareInvoiceCreditmemo($invoice, $data = []) { $totalQty = 0; $qtys = isset($data['qtys']) ? $data['qtys'] : []; $creditmemo = $this->_convertor->toCreditmemo($this->_order); $creditmemo->setInvoice($invoice); $invoiceQtysRefunded = []; foreach ($invoice->getOrder()->getCreditmemosCollection() as $createdCreditmemo) { if ($createdCreditmemo->getState() != \Magento\Sales\Model\Order\Creditmemo::STATE_CANCELED && $createdCreditmemo->getInvoiceId() == $invoice->getId()) { foreach ($createdCreditmemo->getAllItems() as $createdCreditmemoItem) { $orderItemId = $createdCreditmemoItem->getOrderItem()->getId(); if (isset($invoiceQtysRefunded[$orderItemId])) { $invoiceQtysRefunded[$orderItemId] += $createdCreditmemoItem->getQty(); } else { $invoiceQtysRefunded[$orderItemId] = $createdCreditmemoItem->getQty(); } } } } $invoiceQtysRefundLimits = []; foreach ($invoice->getAllItems() as $invoiceItem) { $invoiceQtyCanBeRefunded = $invoiceItem->getQty(); $orderItemId = $invoiceItem->getOrderItem()->getId(); if (isset($invoiceQtysRefunded[$orderItemId])) { $invoiceQtyCanBeRefunded = $invoiceQtyCanBeRefunded - $invoiceQtysRefunded[$orderItemId]; } $invoiceQtysRefundLimits[$orderItemId] = $invoiceQtyCanBeRefunded; } foreach ($invoice->getAllItems() as $invoiceItem) { $orderItem = $invoiceItem->getOrderItem(); if (!$this->_canRefundItem($orderItem, $qtys, $invoiceQtysRefundLimits)) { continue; } $item = $this->_convertor->itemToCreditmemoItem($orderItem); if ($orderItem->isDummy()) { $qty = 1; } else { if (isset($qtys[$orderItem->getId()])) { $qty = (double) $qtys[$orderItem->getId()]; } elseif (!count($qtys)) { $qty = $orderItem->getQtyToRefund(); } else { continue; } if (isset($invoiceQtysRefundLimits[$orderItem->getId()])) { $qty = min($qty, $invoiceQtysRefundLimits[$orderItem->getId()]); } } $qty = min($qty, $invoiceItem->getQty()); $totalQty += $qty; $item->setQty($qty); $creditmemo->addItem($item); } $creditmemo->setTotalQty($totalQty); $this->_initCreditmemoData($creditmemo, $data); if (!isset($data['shipping_amount'])) { $order = $invoice->getOrder(); $isShippingInclTax = $this->_taxConfig->displaySalesShippingInclTax($order->getStoreId()); if ($isShippingInclTax) { $baseAllowedAmount = $order->getBaseShippingInclTax() - $order->getBaseShippingRefunded() - $order->getBaseShippingTaxRefunded(); } else { $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded(); $baseAllowedAmount = min($baseAllowedAmount, $invoice->getBaseShippingAmount()); } $creditmemo->setBaseShippingAmount($baseAllowedAmount); } $creditmemo->collectTotals(); return $creditmemo; }