/** * Convert order object to invoice * * @param \Magento\Sales\Model\Order $order * @return \Magento\Sales\Model\Order\Invoice */ public function toInvoice(\Magento\Sales\Model\Order $order) { $invoice = $this->_orderInvoiceFactory->create(); $invoice->setOrder($order)->setStoreId($order->getStoreId())->setCustomerId($order->getCustomerId())->setBillingAddressId($order->getBillingAddressId())->setShippingAddressId($order->getShippingAddressId()); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order', 'to_invoice', $order, $invoice); return $invoice; }
/** * @param int $configValue * @param bool|null $forceSyncMode * @param bool|null $customerNoteNotify * @param bool|null $emailSendingResult * @dataProvider sendDataProvider * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSend($configValue, $forceSyncMode, $customerNoteNotify, $emailSendingResult) { $comment = 'comment_test'; $address = 'address_test'; $configPath = 'sales_email/general/async_sending'; $this->invoiceMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with($configPath)->willReturn($configValue); if (!$configValue || $forceSyncMode) { $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->any())->method('format')->with($addressMock, 'html')->willReturn($address); $this->orderMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock); $this->orderMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock); $this->invoiceMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify); $this->invoiceMock->expects($this->any())->method('getCustomerNote')->willReturn($comment); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'invoice' => $this->invoiceMock, 'comment' => $customerNoteNotify ? $comment : '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $address, 'formattedBillingAddress' => $address]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn($emailSendingResult); if ($emailSendingResult) { $this->senderBuilderFactoryMock->expects($this->once())->method('create')->willReturn($this->senderMock); $this->senderMock->expects($this->once())->method('send'); $this->senderMock->expects($this->once())->method('sendCopyTo'); $this->invoiceMock->expects($this->once())->method('setEmailSent')->with(true); $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, ['send_email', 'email_sent']); $this->assertTrue($this->sender->send($this->invoiceMock)); } else { $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, 'send_email'); $this->assertFalse($this->sender->send($this->invoiceMock)); } } else { $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, 'send_email'); $this->assertFalse($this->sender->send($this->invoiceMock)); } }
/** * Send email to customer * * @param Invoice $invoice * @param bool $notify * @param string $comment * @return bool */ public function send(Invoice $invoice, $notify = true, $comment = '') { $order = $invoice->getOrder(); $transport = ['order' => $order, 'invoice' => $invoice, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)]; $this->eventManager->dispatch('email_invoice_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport); return $this->checkAndSend($order, $notify); }
/** * Prepare shipment * * @param \Magento\Sales\Model\Order\Invoice $invoice * @return \Magento\Sales\Model\Order\Shipment|false */ protected function _prepareShipment($invoice) { $invoiceData = $this->getRequest()->getParam('invoice'); $shipment = $this->shipmentFactory->create($invoice->getOrder(), isset($invoiceData['items']) ? $invoiceData['items'] : [], $this->getRequest()->getPost('tracking')); if (!$shipment->getTotalQty()) { return false; } return $shipment->register(); }
/** * @param \Magento\Sales\Model\Order\Invoice $invoice * @return $this */ public function collect(\Magento\Sales\Model\Order\Invoice $invoice) { /** * Check order grand total and invoice amounts */ if ($invoice->isLast()) { // } return $this; }
/** * Run invoice mapper test * * @return void */ public function testInvoke() { $this->invoiceMock->expects($this->once())->method('getData')->will($this->returnValue(['field-1' => 'value-1'])); $this->invoiceMock->expects($this->once())->method('getAllItems')->will($this->returnValue([$this->invoiceItemMock])); $this->invoiceBuilderMock->expects($this->once())->method('populateWithArray')->with($this->equalTo(['field-1' => 'value-1']))->will($this->returnSelf()); $this->invoiceItemMapperMock->expects($this->once())->method('extractDto')->with($this->equalTo($this->invoiceItemMock))->will($this->returnValue('item-1')); $this->invoiceBuilderMock->expects($this->once())->method('setItems')->with($this->equalTo(['item-1']))->will($this->returnSelf()); $this->invoiceBuilderMock->expects($this->once())->method('create')->will($this->returnValue('data-object-with-invoice')); $this->assertEquals('data-object-with-invoice', $this->invoiceMapper->extractDto($this->invoiceMock)); }
/** * Collect total cost of invoiced items * * @param \Magento\Sales\Model\Order\Invoice $invoice * @return $this */ public function collect(\Magento\Sales\Model\Order\Invoice $invoice) { $baseInvoiceTotalCost = 0; foreach ($invoice->getAllItems() as $item) { if (!$item->getHasChildren()) { $baseInvoiceTotalCost += $item->getBaseCost() * $item->getQty(); } } $invoice->setBaseCost($baseInvoiceTotalCost); return $this; }
/** * Send email to customer * * @param Invoice $invoice * @param bool $notify * @param string $comment * @return bool */ public function send(Invoice $invoice, $notify = true, $comment = '') { $order = $invoice->getOrder(); $this->templateContainer->setTemplateVars(['order' => $order, 'invoice' => $invoice, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore()]); $result = $this->checkAndSend($order, $notify); if ($result) { $invoice->setEmailSent(true); $this->invoiceResource->saveAttribute($invoice, 'email_sent'); } return $result; }
public function testProcessRelation() { $this->invoiceMock->expects($this->once())->method('getId')->willReturn('invoice-id-value'); $this->invoiceMock->expects($this->exactly(2))->method('getItems')->willReturn([$this->invoiceItemMock]); $this->invoiceItemMock->expects($this->once())->method('setParentId')->with('invoice-id-value')->willReturnSelf(); $this->invoiceItemMock->expects($this->once())->method('getOrderItem')->willReturn($this->orderItemMock); $this->invoiceItemMock->expects($this->once())->method('setOrderItem')->with($this->orderItemMock)->willReturnSelf(); $this->invoiceItemResourceMock->expects($this->once())->method('save')->with($this->invoiceItemMock)->willReturnSelf(); $this->invoiceMock->expects($this->exactly(2))->method('getComments')->willReturn([$this->invoiceCommentMock]); $this->invoiceCommentResourceMock->expects($this->once())->method('save')->with($this->invoiceCommentMock)->willReturnSelf(); $this->relationProcessor->processRelation($this->invoiceMock); }
/** * Send email to customer * * @param Invoice $invoice * @param bool $notify * @param string $comment * @return bool */ public function send(Invoice $invoice, $notify = true, $comment = '') { $order = $invoice->getOrder(); if ($order->getShippingAddress()) { $formattedShippingAddress = $this->addressRenderer->format($order->getShippingAddress(), 'html'); } else { $formattedShippingAddress = ''; } $formattedBillingAddress = $this->addressRenderer->format($order->getBillingAddress(), 'html'); $transport = new \Magento\Framework\Object(['template_vars' => ['order' => $order, 'invoice' => $invoice, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $formattedShippingAddress, 'formattedBillingAddress' => $formattedBillingAddress]]); $this->eventManager->dispatch('email_invoice_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport->getTemplateVars()); return $this->checkAndSend($order, $notify); }
/** * Update invoice state value * Method set text label instead id value * @return void */ private function setStateValue() { $value = $this->getData(self::$stateAttributeCode); /** @var \Magento\Framework\Phrase $state */ $state = Invoice::getStates()[$value]; $this->setCustomAttribute(self::$stateAttributeCode, $state->getText()); }
/** * Collect invoice subtotal * * @param \Magento\Sales\Model\Order\Invoice $invoice * @return $this */ public function collect(\Magento\Sales\Model\Order\Invoice $invoice) { $subtotal = 0; $baseSubtotal = 0; $subtotalInclTax = 0; $baseSubtotalInclTax = 0; $order = $invoice->getOrder(); foreach ($invoice->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } $item->calcRowTotal(); $subtotal += $item->getRowTotal(); $baseSubtotal += $item->getBaseRowTotal(); $subtotalInclTax += $item->getRowTotalInclTax(); $baseSubtotalInclTax += $item->getBaseRowTotalInclTax(); } $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced(); $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced(); $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount() + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced(); $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount() + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced(); /** * Check if shipping tax calculation is included to current invoice. */ $includeShippingTax = true; foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) { if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) { $includeShippingTax = false; break; } } if ($includeShippingTax) { $allowedSubtotalInclTax -= $order->getShippingTaxAmount(); $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount(); } else { $allowedSubtotalInclTax += $order->getShippingHiddenTaxAmount(); $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount(); } if ($invoice->isLast()) { $subtotal = $allowedSubtotal; $baseSubtotal = $baseAllowedSubtotal; $subtotalInclTax = $allowedSubtotalInclTax; $baseSubtotalInclTax = $baseAllowedSubtotalInclTax; } else { $subtotal = min($allowedSubtotal, $subtotal); $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal); $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax); $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax); } $invoice->setSubtotal($subtotal); $invoice->setBaseSubtotal($baseSubtotal); $invoice->setSubtotalInclTax($subtotalInclTax); $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax); $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal); return $this; }
/** * Sends order invoice email to the customer. * * Email will be sent immediately in two cases: * * - if asynchronous email sending is disabled in global settings * - if $forceSyncMode parameter is set to TRUE * * Otherwise, email will be sent later during running of * corresponding cron job. * * @param Invoice $invoice * @param bool $forceSyncMode * @return bool */ public function send(Invoice $invoice, $forceSyncMode = false) { $invoice->setSendEmail(true); if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) { $order = $invoice->getOrder(); $transport = ['order' => $order, 'invoice' => $invoice, 'comment' => $invoice->getCustomerNoteNotify() ? $invoice->getCustomerNote() : '', 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)]; $this->eventManager->dispatch('email_invoice_set_template_vars_before', ['sender' => $this, 'transport' => $transport]); $this->templateContainer->setTemplateVars($transport); if ($this->checkAndSend($order)) { $invoice->setEmailSent(true); $this->invoiceResource->saveAttribute($invoice, ['send_email', 'email_sent']); return true; } } $this->invoiceResource->saveAttribute($invoice, 'send_email'); return false; }
/** * @param \Magento\Sales\Model\Order\Invoice $invoice * * @return $this */ public function collect(\Magento\Sales\Model\Order\Invoice $invoice) { $order = $invoice->getOrder(); $amount = $order->getFinanceCostAmount(); $baseAmount = $order->getBaseFinanceCostAmount(); if ($amount) { $invoice->setFinanceCostAmount($amount); $invoice->setBaseFinanceCostAmount($baseAmount); $invoice->setGrandTotal($invoice->getGrandTotal() + $amount); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseAmount); } return $this; }
/** * Prepare shipment * * @param \Magento\Sales\Model\Order\Invoice $invoice * @return \Magento\Sales\Model\Order\Shipment|false */ protected function _prepareShipment($invoice) { $savedQtys = []; $data = $this->getRequest()->getParam('invoice'); if (isset($data['items'])) { $savedQtys = $data['items']; } $shipment = $this->_objectManager->create('Magento\\Sales\\Model\\Service\\Order', ['order' => $invoice->getOrder()])->prepareShipment($savedQtys); if (!$shipment->getTotalQty()) { return false; } $shipment->register(); $tracks = $this->getRequest()->getPost('tracking'); if ($tracks) { foreach ($tracks as $data) { $track = $this->_objectManager->create('Magento\\Sales\\Model\\Order\\Shipment\\Track')->addData($data); $shipment->addTrack($track); } } return $shipment; }
public function testProcessRelation() { $this->addressHandlerMock->expects($this->once())->method('removeEmptyAddresses')->with($this->orderMock)->willReturnSelf(); $this->addressHandlerMock->expects($this->once())->method('process')->with($this->orderMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getItems')->willReturn([$this->orderItemMock]); $this->orderMock->expects($this->exactly(3))->method('getId')->willReturn('order-id-value'); $this->orderItemMock->expects($this->once())->method('setOrderId')->with('order-id-value')->willReturnSelf(); $this->orderItemMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->orderItemRepositoryMock->expects($this->once())->method('save')->with($this->orderItemMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getPayment')->willReturn($this->orderPaymentMock); $this->orderPaymentMock->expects($this->once())->method('setParentId')->with('order-id-value')->willReturnSelf(); $this->orderPaymentMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->orderPaymentResourceMock->expects($this->once())->method('save')->with($this->orderPaymentMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getStatusHistories')->willReturn([$this->orderStatusHistoryMock]); $this->orderStatusHistoryMock->expects($this->once())->method('setParentId')->with('order-id-value')->willReturnSelf(); $this->orderStatusHistoryMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->statusHistoryResource->expects($this->once())->method('save')->with($this->orderStatusHistoryMock)->willReturnSelf(); $this->orderMock->expects($this->exactly(2))->method('getRelatedObjects')->willReturn([$this->orderInvoiceMock]); $this->orderInvoiceMock->expects($this->once())->method('setOrder')->with($this->orderMock)->willReturnSelf(); $this->orderInvoiceMock->expects($this->once())->method('save')->willReturnSelf(); $this->relationProcessor->processRelation($this->orderMock); }
/** * @dataProvider payDataProvider * @param float $orderTotalPaid * @param float $orderBaseTotalPaid * @param float $grandTotal * @param float $baseGrandTotal * @param float $expectedState */ public function testPay($orderTotalPaid, $orderBaseTotalPaid, $grandTotal, $baseGrandTotal, $expectedState) { $this->mockPay(); $this->model->setGrandTotal($grandTotal); $this->model->setBaseGrandTotal($baseGrandTotal); $this->orderMock->setTotalPaid($orderTotalPaid); $this->orderMock->setBaseTotalPaid($orderBaseTotalPaid); $this->assertFalse($this->model->wasPayCalled()); $this->assertEquals($this->model, $this->model->pay()); $this->assertTrue($this->model->wasPayCalled()); $this->assertEquals($expectedState, $this->model->getState()); #second call of pay() method must do nothing $this->model->pay(); }
/** * @param bool $isVirtualOrder * @param int $formatCallCount * @param string|null $expectedShippingAddress * @dataProvider sendVirtualOrderDataProvider */ public function testSendVirtualOrder($isVirtualOrder, $formatCallCount, $expectedShippingAddress) { $billingAddress = 'address_test'; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->invoiceMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with('sales_email/general/async_sending')->willReturn(false); $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->exactly($formatCallCount))->method('format')->with($addressMock, 'html')->willReturn($billingAddress); $this->stepAddressFormat($addressMock, $isVirtualOrder); $this->invoiceMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(false); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'invoice' => $this->invoiceMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $billingAddress]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false); $this->invoiceResourceMock->expects($this->once())->method('saveAttribute')->with($this->invoiceMock, 'send_email'); $this->assertFalse($this->sender->send($this->invoiceMock)); }
public function testCalcRowTotal() { $this->item->setData(['order_item_id' => 1, 'qty' => 2]); $this->item->setInvoice($this->invoiceMock); $this->invoiceMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock); $this->orderMock->expects($this->once())->method('getItemById')->with(1)->willReturn($this->orderItemMock); $this->orderItemMock->expects($this->once())->method('getQtyOrdered')->willReturn(1); $this->orderItemMock->expects($this->once())->method('getRowTotal')->willReturn(2); $this->orderItemMock->expects($this->once())->method('getRowInvoiced')->willReturn(1); $this->orderItemMock->expects($this->once())->method('getBaseRowTotal')->willReturn(2); $this->orderItemMock->expects($this->once())->method('getBaseRowInvoiced')->willReturn(1); $this->orderItemMock->expects($this->once())->method('getRowTotalInclTax')->willReturn(1); $this->orderItemMock->expects($this->once())->method('getBaseRowTotalInclTax')->willReturn(1); $this->orderItemMock->expects($this->once())->method('getQtyToInvoice')->willReturn(1); $this->orderItemMock->expects($this->once())->method('getQtyInvoiced')->willReturn(0); $this->invoiceMock->expects($this->exactly(4))->method('roundPrice')->willReturnMap([[2, 'regular', false, 2], [2, 'base', false, 2], [2, 'including', false, 2], [2, 'including_base', false, 2]]); $this->assertEquals($this->item->calcRowTotal(), $this->item); }
/** * test execute method */ public function testExecute() { $this->requestMock->expects($this->exactly(4))->method('getParam')->will($this->returnValueMap([['order_id', null, 'order_id'], ['creditmemo_id', null, 'creditmemo_id'], ['creditmemo', null, 'creditmemo'], ['invoice_id', null, 'invoice_id']])); $this->creditmemoLoaderMock->expects($this->once())->method('setOrderId')->with($this->equalTo('order_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemoId')->with($this->equalTo('creditmemo_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemo')->with($this->equalTo('creditmemo')); $this->creditmemoLoaderMock->expects($this->once())->method('setInvoiceId')->with($this->equalTo('invoice_id')); $this->creditmemoLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->creditmemoMock)); $this->creditmemoMock->expects($this->exactly(2))->method('getInvoice')->will($this->returnValue($this->invoiceMock)); $this->invoiceMock->expects($this->once())->method('getIncrementId')->will($this->returnValue('invoice-increment-id')); $this->titleMock->expects($this->exactly(2))->method('prepend')->will($this->returnValueMap([['Credit Memos', null], ['New Memo for #invoice-increment-id', null], ['item-title', null]])); $this->objectManagerMock->expects($this->once())->method('get')->with($this->equalTo('Magento\\Backend\\Model\\Session'))->will($this->returnValue($this->backendSessionMock)); $this->backendSessionMock->expects($this->once())->method('getCommentText')->with($this->equalTo(true))->will($this->returnValue('comment')); $this->creditmemoMock->expects($this->once())->method('setCommentText')->with($this->equalTo('comment')); $this->resultPageMock->expects($this->any())->method('getConfig')->will($this->returnValue($this->pageConfigMock)); $this->pageConfigMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock); $this->resultPageFactoryMock->expects($this->once())->method('create')->willReturn($this->resultPageMock); $this->resultPageMock->expects($this->once())->method('setActiveMenu')->with('Magento_Sales::sales_order')->willReturnSelf(); $this->resultPageMock->expects($this->atLeastOnce())->method('getConfig')->willReturn($this->pageConfigMock); $this->assertInstanceOf('Magento\\Backend\\Model\\View\\Result\\Page', $this->controller->execute()); }
/** * @param array $orderData * @param array $invoiceData * @param array $expectedResults * @dataProvider collectDataProvider */ public function testCollect($orderData, $invoiceData, $expectedResults) { $roundingDelta = []; //Set up order mock foreach ($orderData['data_fields'] as $key => $value) { $this->order->setData($key, $value); } /** @var \Magento\Sales\Model\Order\Invoice[] $previousInvoices */ $previousInvoices = []; foreach ($orderData['previous_invoices'] as $previousInvoiceData) { $previousInvoice = $this->getMockBuilder('\\Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->setMethods(['isCanceled', '__wakeup'])->getMock(); $previousInvoice->setData('shipping_amount', $previousInvoiceData['shipping_amount']); $previousInvoices[] = $previousInvoice; } $this->order->expects($this->once())->method('getInvoiceCollection')->will($this->returnValue($previousInvoices)); //Set up invoice mock /** @var \Magento\Sales\Model\Order\Invoice\Item[] $invoiceItems */ $invoiceItems = []; foreach ($invoiceData['items'] as $itemKey => $invoiceItemData) { $invoiceItems[$itemKey] = $this->getInvoiceItem($invoiceItemData); } $this->invoice->expects($this->once())->method('getAllItems')->will($this->returnValue($invoiceItems)); $this->invoice->expects($this->once())->method('isLast')->will($this->returnValue($invoiceData['is_last'])); foreach ($invoiceData['data_fields'] as $key => $value) { $this->invoice->setData($key, $value); } $this->invoice->expects($this->any())->method('roundPrice')->will($this->returnCallback(function ($price, $type) use(&$roundingDelta) { if (!isset($roundingDelta[$type])) { $roundingDelta[$type] = 0; } $roundedPrice = round($price + $roundingDelta[$type], 2); $roundingDelta[$type] = $price - $roundedPrice; return $roundedPrice; })); $this->model->collect($this->invoice); //verify invoice data foreach ($expectedResults['invoice_data'] as $key => $value) { $this->assertEquals($value, $this->invoice->getData($key)); } //verify invoice item data foreach ($expectedResults['invoice_items'] as $itemKey => $itemData) { $invoiceItem = $invoiceItems[$itemKey]; foreach ($itemData as $key => $value) { $this->assertEquals($value, $invoiceItem->getData($key)); } } }
/** * @param array $orderData * @param array $invoiceData * @param array $expectedResults * @dataProvider collectDataProvider */ public function testCollect($orderData, $invoiceData, $expectedResults) { $roundingDelta = []; $this->setupOrder($orderData); //Set up weeeData mock $this->weeeData->expects($this->once())->method('includeInSubtotal')->will($this->returnValue($invoiceData['include_in_subtotal'])); //Set up invoice mock /** @var \Magento\Sales\Model\Order\Invoice\Item[] $invoiceItems */ $invoiceItems = []; foreach ($invoiceData['items'] as $itemKey => $invoiceItemData) { $invoiceItems[$itemKey] = $this->getInvoiceItem($invoiceItemData); } $this->invoice->expects($this->once())->method('getAllItems')->will($this->returnValue($invoiceItems)); $this->invoice->expects($this->once())->method('isLast')->will($this->returnValue($invoiceData['is_last'])); foreach ($invoiceData['data_fields'] as $key => $value) { $this->invoice->setData($key, $value); } $this->invoice->expects($this->any())->method('roundPrice')->will($this->returnCallback(function ($price, $type) use(&$roundingDelta) { if (!isset($roundingDelta[$type])) { $roundingDelta[$type] = 0; } $roundedPrice = round($price + $roundingDelta[$type], 2); $roundingDelta[$type] = $price - $roundedPrice; return $roundedPrice; })); $this->model->collect($this->invoice); //verify invoice data foreach ($expectedResults['invoice_data'] as $key => $value) { $this->assertEquals($value, $this->invoice->getData($key), 'Invoice data field ' . $key . ' is incorrect'); } //verify invoice item data foreach ($expectedResults['invoice_items'] as $itemKey => $itemData) { $invoiceItem = $invoiceItems[$itemKey]; foreach ($itemData as $key => $value) { if ($key == 'tax_ratio') { $taxRatio = unserialize($invoiceItem->getData($key)); $expectedTaxRatio = unserialize($itemData[$key]); $this->assertEquals($expectedTaxRatio['weee'], $taxRatio['weee'], "Tax ratio is incorrect"); } else { $this->assertEquals($value, $invoiceItem->getData($key), 'Invoice item field ' . $key . ' is incorrect'); } } } }
/** * test execute method */ public function testExecute() { $this->requestMock->expects($this->exactly(4))->method('getParam')->will($this->returnValueMap([['order_id', null, 'order_id'], ['creditmemo_id', null, 'creditmemo_id'], ['creditmemo', null, 'creditmemo'], ['invoice_id', null, 'invoice_id']])); $this->creditmemoLoaderMock->expects($this->once())->method('setOrderId')->with($this->equalTo('order_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemoId')->with($this->equalTo('creditmemo_id')); $this->creditmemoLoaderMock->expects($this->once())->method('setCreditmemo')->with($this->equalTo('creditmemo')); $this->creditmemoLoaderMock->expects($this->once())->method('setInvoiceId')->with($this->equalTo('invoice_id')); $this->creditmemoLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->creditmemoMock)); $this->creditmemoMock->expects($this->exactly(2))->method('getInvoice')->will($this->returnValue($this->invoiceMock)); $this->invoiceMock->expects($this->once())->method('getIncrementId')->will($this->returnValue('invoice-increment-id')); $this->titleMock->expects($this->exactly(3))->method('add')->will($this->returnValueMap([['Credit Memos', null], ['New Memo for #invoice-increment-id', null], ['item-title', null]])); $this->objectManagerMock->expects($this->once())->method('get')->with($this->equalTo('Magento\\Backend\\Model\\Session'))->will($this->returnValue($this->backendSessionMock)); $this->backendSessionMock->expects($this->once())->method('getCommentText')->with($this->equalTo(true))->will($this->returnValue('comment')); $this->creditmemoMock->expects($this->once())->method('setCommentText')->with($this->equalTo('comment')); $this->viewMock->expects($this->once())->method('loadLayout'); $this->viewMock->expects($this->once())->method('renderLayout'); $this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->layoutMock)); $this->layoutMock->expects($this->once())->method('getBlock')->with($this->equalTo('menu'))->will($this->returnValue($this->blockMenuMock)); $this->blockMenuMock->expects($this->once())->method('setActive')->with($this->equalTo('Magento_Sales::sales_order')); $this->blockMenuMock->expects($this->once())->method('getMenuModel')->will($this->returnValue($this->modelMenuMock)); $this->modelMenuMock->expects($this->once())->method('getParentItems')->will($this->returnValue([$this->modelMenuItem])); $this->modelMenuItem->expects($this->once())->method('getTitle')->will($this->returnValue('item-title')); $this->assertNull($this->controller->execute()); }
/** * Mock capture transaction id in invoice * * @param \Magento\Sales\Model\Order\Invoice $invoice * @param \Magento\Sales\Model\Order\Payment $payment * @return $this */ public function processInvoice($invoice, $payment) { $invoice->setTransactionId(1); return $this; }
/** * Cancel invoice and register order cancellation * * @param Invoice|false $invoice * @param string $message */ protected function cancelInvoiceAndRegisterCancellation($invoice, $message) { if ($invoice instanceof Invoice) { $invoice->cancel(); $this->getOrder()->addRelatedObject($invoice); } $this->getOrder()->registerCancellation($message, false); }
/** * Triggers invoice pay and updates base_amount_paid_online total. * * @param \Magento\Sales\Model\Order\Invoice|false $invoice */ protected function updateBaseAmountPaidOnlineTotal($invoice) { if ($invoice instanceof Invoice) { $invoice->pay(); $this->_updateTotals(['base_amount_paid_online' => $invoice->getBaseGrandTotal()]); $this->getOrder()->addRelatedObject($invoice); } }
/** * Collect invoice subtotal * * @param \Magento\Sales\Model\Order\Invoice $invoice * @return $this */ public function collect(\Magento\Sales\Model\Order\Invoice $invoice) { $subtotal = 0; $baseSubtotal = 0; $subtotalInclTax = 0; $baseSubtotalInclTax = 0; $order = $invoice->getOrder(); foreach ($invoice->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } $item->calcRowTotal(); $subtotal += $item->getRowTotal(); $baseSubtotal += $item->getBaseRowTotal(); $subtotalInclTax += $item->getRowTotalInclTax(); $baseSubtotalInclTax += $item->getBaseRowTotalInclTax(); } $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced(); $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced(); //Note: The $subtotalInclTax and $baseSubtotalInclTax are not adjusted from those provide by the line items //because the "InclTax" is displayed before any tax adjustments based on discounts, shipping, etc. if ($invoice->isLast()) { $subtotal = $allowedSubtotal; $baseSubtotal = $baseAllowedSubtotal; } else { $subtotal = min($allowedSubtotal, $subtotal); $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal); } $invoice->setSubtotal($subtotal); $invoice->setBaseSubtotal($baseSubtotal); $invoice->setSubtotalInclTax($subtotalInclTax); $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax); $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal); return $this; }
/** * test invoice capture service */ public function testInvoke() { $this->invoiceRepositoryMock->expects($this->once())->method('get')->with($this->equalTo(1))->will($this->returnValue($this->invoiceMock)); $this->invoiceMock->expects($this->once())->method('capture')->will($this->returnSelf()); $this->assertTrue($this->invoiceCapture->invoke(1)); }
/** * Test case when email has not been sent */ public function testNotifyFail() { $this->invoice->expects($this->once())->method('getEmailSent')->will($this->returnValue(false)); $this->assertFalse($this->notifier->notify($this->invoice)); }
/** * Cancel specified invoice: update self totals from it * * @param Invoice $invoice * @return $this */ public function cancelInvoice($invoice) { $this->_updateTotals(['amount_paid' => -1 * $invoice->getGrandTotal(), 'base_amount_paid' => -1 * $invoice->getBaseGrandTotal(), 'shipping_captured' => -1 * $invoice->getShippingAmount(), 'base_shipping_captured' => -1 * $invoice->getBaseShippingAmount()]); $this->_eventManager->dispatch('sales_order_payment_cancel_invoice', ['payment' => $this, 'invoice' => $invoice]); return $this; }