/** * Test execute * * @return void */ public function testExecute() { $orderId = 1; $invoiceData = ['comment_text' => 'test']; $response = 'test data'; $this->requestMock->expects($this->at(0))->method('getParam')->with('order_id')->will($this->returnValue($orderId)); $this->requestMock->expects($this->at(1))->method('getParam')->with('invoice', [])->will($this->returnValue($invoiceData)); $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->setMethods([])->getMock(); $invoiceMock->expects($this->once())->method('getTotalQty')->willReturn(2); $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods(['load', 'getId', 'canInvoice'])->getMock(); $orderMock->expects($this->once())->method('load')->with($orderId)->willReturnSelf(); $orderMock->expects($this->once())->method('getId')->willReturn($orderId); $orderMock->expects($this->once())->method('canInvoice')->willReturn(true); $this->invoiceServiceMock->expects($this->once())->method('prepareInvoice')->with($orderMock, [])->willReturn($invoiceMock); $this->objectManagerMock->expects($this->at(0))->method('create')->with('Magento\\Sales\\Model\\Order')->willReturn($orderMock); $blockItemMock = $this->getMockBuilder('Magento\\Sales\\Block\\Order\\Items')->disableOriginalConstructor()->setMethods([])->getMock(); $blockItemMock->expects($this->once())->method('toHtml')->will($this->returnValue($response)); $layoutMock = $this->getMockBuilder('Magento\\Framework\\View\\Layout')->disableOriginalConstructor()->setMethods([])->getMock(); $layoutMock->expects($this->once())->method('getBlock')->with('order_items')->will($this->returnValue($blockItemMock)); $this->resultPageMock->expects($this->once())->method('getLayout')->will($this->returnValue($layoutMock)); $this->resultPageMock->expects($this->once())->method('getConfig')->will($this->returnValue($this->pageConfigMock)); $this->pageConfigMock->expects($this->once())->method('getTitle')->will($this->returnValue($this->titleMock)); $this->resultPageFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->resultPageMock)); $resultRaw = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Raw')->disableOriginalConstructor()->setMethods([])->getMock(); $resultRaw->expects($this->once())->method('setContents')->with($response); $this->resultRawFactoryMock->expects($this->once())->method('create')->will($this->returnValue($resultRaw)); $this->assertSame($resultRaw, $this->controller->execute()); }
/** * Run test setVoid method */ public function testSetVoid() { $id = 145; $returnValue = true; $invoiceMock = $this->getMock('Magento\\Sales\\Model\\Order\\Invoice', ['void'], [], '', false); $this->repositoryMock->expects($this->once())->method('get')->with($id)->will($this->returnValue($invoiceMock)); $invoiceMock->expects($this->once())->method('void')->will($this->returnValue($returnValue)); $this->assertTrue($this->invoiceService->setVoid($id)); }
/** * @param int $orderId * @param array $invoiceData * @return bool | \Magento\Sales\Model\Order\Invoice */ protected function createInvoice($orderId, $invoiceData) { $order = $this->orderFactory->create()->load($orderId); if (!$order) { return false; } $invoice = $this->invoiceManagement->prepareInvoice($order, $invoiceData); return $invoice; }
/** * Update items qty action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { try { $orderId = $this->getRequest()->getParam('order_id'); $invoiceData = $this->getRequest()->getParam('invoice', []); $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : []; /** @var \Magento\Sales\Model\Order $order */ $order = $this->_objectManager->create('Magento\\Sales\\Model\\Order')->load($orderId); if (!$order->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.')); } if (!$order->canInvoice()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order does not allow an invoice to be created.')); } $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems); if (!$invoice->getTotalQty()) { throw new \Magento\Framework\Exception\LocalizedException(__('You can\'t create an invoice without products.')); } $this->registry->register('current_invoice', $invoice); // Save invoice comment text in current invoice object in order to display it in corresponding view $invoiceRawCommentText = $invoiceData['comment_text']; $invoice->setCommentText($invoiceRawCommentText); /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Invoices')); $response = $resultPage->getLayout()->getBlock('order_items')->toHtml(); } catch (LocalizedException $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['error' => true, 'message' => __('Cannot update item quantity.')]; } if (is_array($response)) { /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); $resultJson->setData($response); return $resultJson; } else { /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */ $resultRaw = $this->resultRawFactory->create(); $resultRaw->setContents($response); return $resultRaw; } }
/** * Add one order to customer. * * @param $customer * @param $itemsData */ public function addOrder($customer, $itemsData) { /* create order for Russian store/stock */ $this->_manStore->setCurrentStore(self::STORE_ID_RUS); $store = $this->_manStore->getStore(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->_manObj->create(\Magento\Quote\Model\Quote::class); $quote->setStore($store); $quote->assignCustomer($customer); $quote->setInventoryProcessed(false); //not effect inventory /** Populate orders with data. */ $this->_populateQuoteItems($quote); $this->_populateQuoteAddrShipping($quote, $customer); $this->_populateQuoteAddrBilling($quote, $customer); $this->_populateQuoteShippingMethod($quote); $this->_populateQuotePaymentMethod($quote); /* save quote then reload it by ID to create IDs for items (see MOBI-434, $_items, $_data['items'], $_data['items_collection']) */ $quote->collectTotals(); $quote->save(); $id = $quote->getId(); $quote = $this->_manObj->create(\Magento\Quote\Model\Quote::class); $quote->load($id); $quoteItems = $quote->getItemsCollection(); // Create Order From Quote /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $this->_manQuote->submit($quote); $items = $order->getItems(); $item = reset($items); $item->setBaseOriginalPrice(8); $item->save(); $order->save(); /* register PV */ $this->_manEvent->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]); /* prepare invoice */ $invoice = $this->_manInvoice->prepareInvoice($order); $invoice->register(); $invoice->save(); // $invoiceId = $invoice->getEntityId(); /* update date paid in PV register */ // $orderId = $order->getEntityId(); // $bind = [ // \Praxigento\Pv\Data\Entity\Sale::ATTR_DATE_PAID => self::DATE_PAID // ]; // $this->_repoPvSale->updateById($orderId, $bind); /* transfer PV to customer account */ // $invoice->load($invoiceId); // $order->load($orderId); $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING); $this->_manEvent->dispatch('sales_order_invoice_pay', ['order' => $order, 'invoice' => $invoice]); }
/** * Invoice create page * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { $orderId = $this->getRequest()->getParam('order_id'); $invoiceData = $this->getRequest()->getParam('invoice', []); $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : []; try { /** @var \Magento\Sales\Model\Order $order */ $order = $this->_objectManager->create('Magento\\Sales\\Model\\Order')->load($orderId); if (!$order->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.')); } if (!$order->canInvoice()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order does not allow an invoice to be created.')); } $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems); if (!$invoice->getTotalQty()) { throw new \Magento\Framework\Exception\LocalizedException(__('You can\'t create an invoice without products.')); } $this->registry->register('current_invoice', $invoice); $comment = $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); if ($comment) { $invoice->setCommentText($comment); } /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultPageFactory->create(); $resultPage->setActiveMenu('Magento_Sales::sales_order'); $resultPage->getConfig()->getTitle()->prepend(__('Invoices')); $resultPage->getConfig()->getTitle()->prepend(__('New Invoice')); return $resultPage; } catch (\Magento\Framework\Exception\LocalizedException $exception) { $this->messageManager->addError($exception->getMessage()); return $this->_redirectToOrder($orderId); } catch (\Exception $exception) { $this->messageManager->addException($exception, 'Cannot create an invoice.'); return $this->_redirectToOrder($orderId); } }
public function shipmentTrackingSave(\Praxigento\Odoo\Api\Data\SaleOrder\Shipment\Tracking $data) { $result = false; /* replicate all data in one transaction */ $def = $this->_manTrans->begin(); try { $orderIdMage = $data->getSaleOrderIdMage(); $trackNumber = $data->getData('shipment/trackingInfo/trackingNumber'); $shippingMethodCode = $data->getData('shipment/trackingInfo/shippingCode'); $this->_shipmentLoader->setOrderId($orderIdMage); /** @var \Magento\Sales\Model\Order\Shipment $shipment */ $shipment = $this->_shipmentLoader->load(); if ($shipment) { $carrierCode = $this->_manBusCodes->getMagCodeForCarrier($shippingMethodCode); $title = $this->_manBusCodes->getTitleForCarrier($shippingMethodCode); $track = $this->_manObj->create(\Magento\Sales\Model\Order\Shipment\Track::class); $track->setNumber($trackNumber); $track->setCarrierCode($carrierCode); $track->setTitle($title); $shipment->addTrack($track); $shipment->register(); $shipment->save(); $order = $shipment->getOrder(); $invoice = $this->_manInvoice->prepareInvoice($order); $invoice->register(); $invoice->save(); $order->save(); $this->_manTrans->commit($def); $result = true; } } catch (\Exception $e) { $msg = 'Product replication from Odoo is failed. Error: ' . $e->getMessage(); $this->_logger->emergency($msg); $traceStr = $e->getTraceAsString(); $this->_logger->emergency($traceStr); throw $e; } finally { // transaction will be rolled back if commit is not done (otherwise - do nothing) $this->_manTrans->end($def); } return $result; }
public function testExecute() { $orderId = 1; $invoiceData = []; $commentText = 'comment test'; $this->requestMock->expects($this->at(0))->method('getParam')->with('order_id')->will($this->returnValue($orderId)); $this->requestMock->expects($this->at(1))->method('getParam')->with('invoice', [])->will($this->returnValue($invoiceData)); $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->setMethods([])->getMock(); $invoiceMock->expects($this->once())->method('getTotalQty')->willReturn(2); $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods(['load', 'getId', 'canInvoice'])->getMock(); $orderMock->expects($this->once())->method('load')->with($orderId)->willReturnSelf(); $orderMock->expects($this->once())->method('getId')->willReturn($orderId); $orderMock->expects($this->once())->method('canInvoice')->willReturn(true); $this->invoiceServiceMock->expects($this->once())->method('prepareInvoice')->with($orderMock, [])->willReturn($invoiceMock); $menuBlockMock = $this->getMockBuilder('Magento\\Backend\\Block\\Menu')->disableOriginalConstructor()->setMethods(['getParentItems', 'getMenuModel'])->getMock(); $menuBlockMock->expects($this->any())->method('getMenuModel')->will($this->returnSelf()); $menuBlockMock->expects($this->any())->method('getParentItems')->with('Magento_Sales::sales_order')->will($this->returnValue([])); $this->sessionMock->expects($this->once())->method('getCommentText')->with(true)->will($this->returnValue($commentText)); $this->objectManagerMock->expects($this->at(0))->method('create')->with('Magento\\Sales\\Model\\Order')->willReturn($orderMock); $this->objectManagerMock->expects($this->at(1))->method('get')->with('Magento\\Backend\\Model\\Session')->will($this->returnValue($this->sessionMock)); $this->resultPageMock->expects($this->once())->method('setActiveMenu')->with('Magento_Sales::sales_order'); $this->resultPageFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->resultPageMock)); $this->assertSame($this->resultPageMock, $this->controller->execute()); }
/** * Create new invoice with maximum qty for invoice for each item * * @param array $qtys * @return \Magento\Sales\Model\Order\Invoice */ public function prepareInvoice($qtys = []) { return $this->invoiceManagement->prepareInvoice($this, $qtys); }
/** * Save invoice * We can save only new invoice. Existing invoices are not editable * * @return \Magento\Framework\Controller\ResultInterface * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function execute() { /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest()); $isPost = $this->getRequest()->isPost(); if (!$formKeyIsValid || !$isPost) { $this->messageManager->addError(__('We can\'t save the invoice right now.')); return $resultRedirect->setPath('sales/order/index'); } $data = $this->getRequest()->getPost('invoice'); $orderId = $this->getRequest()->getParam('order_id'); if (!empty($data['comment_text'])) { $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setCommentText($data['comment_text']); } try { $invoiceData = $this->getRequest()->getParam('invoice', []); $invoiceItems = isset($invoiceData['items']) ? $invoiceData['items'] : []; /** @var \Magento\Sales\Model\Order $order */ $order = $this->_objectManager->create('Magento\\Sales\\Model\\Order')->load($orderId); if (!$order->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.')); } if (!$order->canInvoice()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order does not allow an invoice to be created.')); } $invoice = $this->invoiceService->prepareInvoice($order, $invoiceItems); if (!$invoice) { throw new LocalizedException(__('We can\'t save the invoice right now.')); } if (!$invoice->getTotalQty()) { throw new \Magento\Framework\Exception\LocalizedException(__('You can\'t create an invoice without products.')); } $this->registry->register('current_invoice', $invoice); if (!empty($data['capture_case'])) { $invoice->setRequestedCaptureCase($data['capture_case']); } if (!empty($data['comment_text'])) { $invoice->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); $invoice->setCustomerNote($data['comment_text']); $invoice->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } $invoice->register(); $invoice->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $invoice->getOrder()->setIsInProcess(true); $transactionSave = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction')->addObject($invoice)->addObject($invoice->getOrder()); $shipment = false; if (!empty($data['do_shipment']) || (int) $invoice->getOrder()->getForcedShipmentWithInvoice()) { $shipment = $this->_prepareShipment($invoice); if ($shipment) { $transactionSave->addObject($shipment); } } $transactionSave->save(); if (isset($shippingResponse) && $shippingResponse->hasErrors()) { $this->messageManager->addError(__('The invoice and the shipment have been created. ' . 'The shipping label cannot be created now.')); } elseif (!empty($data['do_shipment'])) { $this->messageManager->addSuccess(__('You created the invoice and shipment.')); } else { $this->messageManager->addSuccess(__('The invoice has been created.')); } // send invoice/shipment emails try { if (!empty($data['send_email'])) { $this->invoiceSender->send($invoice); } } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $this->messageManager->addError(__('We can\'t send the invoice email right now.')); } if ($shipment) { try { if (!empty($data['send_email'])) { $this->shipmentSender->send($shipment); } } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $this->messageManager->addError(__('We can\'t send the shipment right now.')); } } $this->_objectManager->get('Magento\\Backend\\Model\\Session')->getCommentText(true); return $resultRedirect->setPath('sales/order/view', ['order_id' => $orderId]); } catch (LocalizedException $e) { $this->messageManager->addError($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t save the invoice right now.')); $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); } return $resultRedirect->setPath('sales/*/new', ['order_id' => $orderId]); }