Ejemplo n.º 1
0
 /**
  * @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->creditmemoMock->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->creditmemoMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify);
         $this->creditmemoMock->expects($this->any())->method('getCustomerNote')->willReturn($comment);
         $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, '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->creditmemoMock->expects($this->once())->method('setEmailSent')->with(true);
             $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, ['send_email', 'email_sent']);
             $this->assertTrue($this->sender->send($this->creditmemoMock));
         } else {
             $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email');
             $this->assertFalse($this->sender->send($this->creditmemoMock));
         }
     } else {
         $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email');
         $this->assertFalse($this->sender->send($this->creditmemoMock));
     }
 }
 /**
  * Send email to customer
  *
  * @param Creditmemo $creditmemo
  * @param bool $notify
  * @param string $comment
  * @return bool
  */
 public function send(Creditmemo $creditmemo, $notify = true, $comment = '')
 {
     $order = $creditmemo->getOrder();
     $transport = ['order' => $order, 'creditmemo' => $creditmemo, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)];
     $this->eventManager->dispatch('email_creditmemo_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]);
     $this->templateContainer->setTemplateVars($transport);
     return $this->checkAndSend($order, $notify);
 }
Ejemplo n.º 3
0
 public function testCollect()
 {
     $this->creditmemoMock->expects($this->once())->method('getAllItems')->willReturn([$this->creditmemoItemMock, $this->creditmemoItemMock]);
     $this->creditmemoItemMock->expects($this->exactly(2))->method('getHasChildren')->willReturn(false);
     $this->creditmemoItemMock->expects($this->exactly(2))->method('getBaseCost')->willReturn(10);
     $this->creditmemoItemMock->expects($this->exactly(2))->method('getQty')->willReturn(2);
     $this->creditmemoMock->expects($this->once())->method('setBaseCost')->with(40)->willReturnSelf();
     $this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
 }
Ejemplo n.º 4
0
 public function testProcessRelations()
 {
     $this->creditmemoMock->expects($this->once())->method('getId')->willReturn('creditmemo-id-value');
     $this->creditmemoMock->expects($this->exactly(2))->method('getItems')->willReturn([$this->itemMock]);
     $this->creditmemoMock->expects($this->exactly(2))->method('getComments')->willReturn([$this->commentMock]);
     $this->itemMock->expects($this->once())->method('setParentId')->with('creditmemo-id-value')->willReturnSelf();
     $this->itemResourceMock->expects($this->once())->method('save')->with($this->itemMock)->willReturnSelf();
     $this->commentResourceMock->expects($this->once())->method('save')->with($this->commentMock)->willReturnSelf();
     $this->relationProcessor->processRelation($this->creditmemoMock);
 }
Ejemplo n.º 5
0
 /**
  * Run creditmemo mapper test
  *
  * @return void
  */
 public function testInvoke()
 {
     $this->creditmemoMock->expects($this->once())->method('getData')->will($this->returnValue(['field-1' => 'value-1']));
     $this->creditmemoMock->expects($this->once())->method('getAllItems')->will($this->returnValue([$this->creditmemoItemMock]));
     $this->creditmemoBuilderMock->expects($this->once())->method('populateWithArray')->with($this->equalTo(['field-1' => 'value-1']))->will($this->returnSelf());
     $this->creditmemoItemMapperMock->expects($this->once())->method('extractDto')->with($this->equalTo($this->creditmemoItemMock))->will($this->returnValue('item-1'));
     $this->creditmemoBuilderMock->expects($this->once())->method('setItems')->with($this->equalTo(['item-1']))->will($this->returnSelf());
     $this->creditmemoBuilderMock->expects($this->once())->method('create')->will($this->returnValue('data-object-with-creditmemo'));
     $this->assertEquals('data-object-with-creditmemo', $this->creditmemoMapper->extractDto($this->creditmemoMock));
 }
Ejemplo n.º 6
0
 /**
  * Send email to customer
  *
  * @param Creditmemo $creditmemo
  * @param bool $notify
  * @param string $comment
  * @return bool
  */
 public function send(Creditmemo $creditmemo, $notify = true, $comment = '')
 {
     $order = $creditmemo->getOrder();
     $this->templateContainer->setTemplateVars(['order' => $creditmemo->getOrder(), 'invoice' => $creditmemo, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore()]);
     $result = $this->checkAndSend($order, $notify);
     if ($result) {
         $creditmemo->setEmailSent(true);
         $this->creditmemoResource->saveAttribute($creditmemo, 'email_sent');
     }
     return $result;
 }
Ejemplo n.º 7
0
 /**
  * Collect total cost of refunded items
  *
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $baseRefundTotalCost = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if (!$item->getHasChildren()) {
             $baseRefundTotalCost += $item->getBaseCost() * $item->getQty();
         }
     }
     $creditmemo->setBaseCost($baseRefundTotalCost);
     return $this;
 }
Ejemplo n.º 8
0
 /**
  * Send email to customer
  *
  * @param Creditmemo $creditmemo
  * @param bool $notify
  * @param string $comment
  * @return bool
  */
 public function send(Creditmemo $creditmemo, $notify = true, $comment = '')
 {
     $order = $creditmemo->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, 'creditmemo' => $creditmemo, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore(), 'formattedShippingAddress' => $formattedShippingAddress, 'formattedBillingAddress' => $formattedBillingAddress]]);
     $this->eventManager->dispatch('email_creditmemo_comment_set_template_vars_before', ['sender' => $this, 'transport' => $transport]);
     $this->templateContainer->setTemplateVars($transport->getTemplateVars());
     return $this->checkAndSend($order, $notify);
 }
 /**
  * Sends order creditmemo 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 Creditmemo $creditmemo
  * @param bool $forceSyncMode
  * @return bool
  */
 public function send(Creditmemo $creditmemo, $forceSyncMode = false)
 {
     $creditmemo->setSendEmail(true);
     if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
         $order = $creditmemo->getOrder();
         $transport = ['order' => $order, 'creditmemo' => $creditmemo, 'comment' => $creditmemo->getCustomerNoteNotify() ? $creditmemo->getCustomerNote() : '', 'billing' => $order->getBillingAddress(), 'payment_html' => $this->getPaymentHtml($order), 'store' => $order->getStore(), 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), 'formattedBillingAddress' => $this->getFormattedBillingAddress($order)];
         $this->eventManager->dispatch('email_creditmemo_set_template_vars_before', ['sender' => $this, 'transport' => $transport]);
         $this->templateContainer->setTemplateVars($transport);
         if ($this->checkAndSend($order)) {
             $creditmemo->setEmailSent(true);
             $this->creditmemoResource->saveAttribute($creditmemo, ['send_email', 'email_sent']);
             return true;
         }
     }
     $this->creditmemoResource->saveAttribute($creditmemo, 'send_email');
     return false;
 }
Ejemplo n.º 10
0
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     $allowedAmount = $order->getShippingAmount() - $order->getShippingRefunded();
     $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded();
     $orderShippingAmount = $order->getShippingAmount();
     $orderBaseShippingAmount = $order->getBaseShippingAmount();
     $orderShippingInclTax = $order->getShippingInclTax();
     $orderBaseShippingInclTax = $order->getBaseShippingInclTax();
     $shippingAmount = $baseShippingAmount = $shippingInclTax = $baseShippingInclTax = 0;
     /**
      * Check if shipping amount was specified (from invoice or another source).
      * Using has magic method to allow setting 0 as shipping amount.
      */
     if ($creditmemo->hasBaseShippingAmount()) {
         $baseShippingAmount = $this->priceCurrency->round($creditmemo->getBaseShippingAmount());
         /*
          * Rounded allowed shipping refund amount is the highest acceptable shipping refund amount.
          * Shipping refund amount shouldn't cause errors, if it doesn't exceed that limit.
          * Note: ($x < $y + 0.0001) means ($x <= $y) for floats
          */
         if ($baseShippingAmount < $this->priceCurrency->round($baseAllowedAmount) + 0.0001) {
             $ratio = 0;
             if ($orderBaseShippingAmount > 0) {
                 $ratio = $baseShippingAmount / $orderBaseShippingAmount;
             }
             /*
              * Shipping refund amount should be equated to allowed refund amount,
              * if it exceeds that limit.
              * Note: ($x > $y - 0.0001) means ($x >= $y) for floats
              */
             if ($baseShippingAmount > $baseAllowedAmount - 0.0001) {
                 $shippingAmount = $allowedAmount;
                 $baseShippingAmount = $baseAllowedAmount;
             } else {
                 $shippingAmount = $this->priceCurrency->round($orderShippingAmount * $ratio);
             }
             $shippingInclTax = $this->priceCurrency->round($orderShippingInclTax * $ratio);
             $baseShippingInclTax = $this->priceCurrency->round($orderBaseShippingInclTax * $ratio);
         } else {
             $baseAllowedAmount = $order->getBaseCurrency()->format($baseAllowedAmount, null, false);
             throw new \Magento\Framework\Exception\LocalizedException(__('Maximum shipping amount allowed to refund is: %1', $baseAllowedAmount));
         }
     } else {
         $shippingAmount = $allowedAmount;
         $baseShippingAmount = $baseAllowedAmount;
         $allowedTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
         $baseAllowedTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
         $shippingInclTax = $this->priceCurrency->round($allowedAmount + $allowedTaxAmount);
         $baseShippingInclTax = $this->priceCurrency->round($baseAllowedAmount + $baseAllowedTaxAmount);
     }
     $creditmemo->setShippingAmount($shippingAmount);
     $creditmemo->setBaseShippingAmount($baseShippingAmount);
     $creditmemo->setShippingInclTax($shippingInclTax);
     $creditmemo->setBaseShippingInclTax($baseShippingInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $shippingAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShippingAmount);
     return $this;
 }
Ejemplo n.º 11
0
 /**
  * Collect Weee amounts for the credit memo
  *
  * @param Creditmemo $creditmemo
  * @return $this
  */
 public function collect(Creditmemo $creditmemo)
 {
     $store = $creditmemo->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     $weeeTaxAmount = 0;
     $baseWeeeTaxAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $weeeAmountExclTax = ($this->_weeeData->getWeeeTaxInclTax($item) - $this->_weeeData->getTotalTaxAppliedForWeeeTax($item)) * $item->getQty();
         $totalTax += $weeeAmountExclTax;
         $baseWeeeAmountExclTax = ($this->_weeeData->getBaseWeeeTaxInclTax($item) - $this->_weeeData->getBaseTotalTaxAppliedForWeeeTax($item)) * $item->getQty();
         $baseTotalTax += $baseWeeeAmountExclTax;
         $item->setWeeeTaxAppliedRowAmount($weeeAmountExclTax);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmountExclTax);
         $weeeTaxAmount += $this->_weeeData->getWeeeTaxInclTax($item) * $item->getQty();
         $baseWeeeTaxAmount += $this->_weeeData->getBaseWeeeTaxInclTax($item) * $item->getQty();
         $newApplied = array();
         $applied = $this->_weeeData->getApplied($item);
         foreach ($applied as $one) {
             $one['base_row_amount'] = $one['base_amount'] * $item->getQty();
             $one['row_amount'] = $one['amount'] * $item->getQty();
             $one['base_row_amount_incl_tax'] = $one['base_amount_incl_tax'] * $item->getQty();
             $one['row_amount_incl_tax'] = $one['amount_incl_tax'] * $item->getQty();
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($item, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
     }
     if ($this->_weeeData->includeInSubtotal($store)) {
         $creditmemo->setSubtotal($creditmemo->getSubtotal() + $totalTax);
         $creditmemo->setBaseSubtotal($creditmemo->getBaseSubtotal() + $baseTotalTax);
     }
     $creditmemo->setSubtotalInclTax($creditmemo->getSubtotalInclTax() + $weeeTaxAmount);
     $creditmemo->setBaseSubtotalInclTax($creditmemo->getBaseSubtotalInclTax() + $baseWeeeTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Ejemplo n.º 12
0
 /**
  * @param array $creditmemoData
  * @param array $expectedResults
  * @dataProvider collectDataProvider
  */
 public function testCollect($creditmemoData, $expectedResults)
 {
     $roundingDelta = [];
     //Set up weeeData mock
     $this->weeeData->expects($this->once())->method('includeInSubtotal')->will($this->returnValue($creditmemoData['include_in_subtotal']));
     //Set up invoice mock
     /** @var \Magento\Sales\Model\Order\Invoice\Item[] $creditmemoItems */
     $creditmemoItems = [];
     foreach ($creditmemoData['items'] as $itemKey => $creditmemoItemData) {
         $creditmemoItems[$itemKey] = $this->getInvoiceItem($creditmemoItemData);
     }
     $this->creditmemo->expects($this->once())->method('getAllItems')->will($this->returnValue($creditmemoItems));
     foreach ($creditmemoData['data_fields'] as $key => $value) {
         $this->creditmemo->setData($key, $value);
     }
     $this->creditmemo->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->creditmemo);
     //verify invoice data
     foreach ($expectedResults['creditmemo_data'] as $key => $value) {
         $this->assertEquals($value, $this->creditmemo->getData($key), 'Creditmemo data field ' . $key . ' is incorrect');
     }
     //verify invoice item data
     foreach ($expectedResults['creditmemo_items'] as $itemKey => $itemData) {
         $creditmemoItem = $creditmemoItems[$itemKey];
         foreach ($itemData as $key => $value) {
             if ($key == 'tax_ratio') {
                 $taxRatio = unserialize($creditmemoItem->getData($key));
                 $expectedTaxRatio = unserialize($itemData[$key]);
                 $this->assertEquals($expectedTaxRatio['weee'], $taxRatio['weee'], "Tax ratio is incorrect");
             } else {
                 $this->assertEquals($value, $creditmemoItem->getData($key), 'Creditmemo item field ' . $key . ' is incorrect');
             }
         }
     }
 }
Ejemplo n.º 13
0
 public function testCollect()
 {
     $this->creditmemoMock->expects($this->once())->method('getAllItems')->willReturn([$this->creditmemoItemMock]);
     $this->creditmemoItemMock->expects($this->atLeastOnce())->method('getOrderItem')->willReturn($this->orderItemMock);
     $this->orderItemMock->expects($this->once())->method('isDummy')->willReturn(false);
     $this->creditmemoItemMock->expects($this->once())->method('calcRowTotal')->willReturnSelf();
     $this->creditmemoItemMock->expects($this->once())->method('getRowTotal')->willReturn(1);
     $this->creditmemoItemMock->expects($this->once())->method('getBaseRowTotal')->willReturn(1);
     $this->creditmemoItemMock->expects($this->once())->method('getRowTotalInclTax')->willReturn(1);
     $this->creditmemoItemMock->expects($this->once())->method('getBaseRowTotalInclTax')->willReturn(1);
     $this->creditmemoMock->expects($this->once())->method('setSubtotal')->with(1)->willReturnSelf();
     $this->creditmemoMock->expects($this->once())->method('setBaseSubtotal')->with(1)->willReturnSelf();
     $this->creditmemoMock->expects($this->once())->method('setSubtotalInclTax')->with(1)->willReturnSelf();
     $this->creditmemoMock->expects($this->once())->method('setBaseSubtotalInclTax')->with(1)->willReturnSelf();
     $this->creditmemoMock->expects($this->once())->method('getGrandTotal')->willReturn(1);
     $this->creditmemoMock->expects($this->once())->method('setGrandTotal')->with(2)->willReturnSelf();
     $this->creditmemoMock->expects($this->once())->method('getBaseGrandTotal')->willReturn(1);
     $this->creditmemoMock->expects($this->once())->method('setBaseGrandTotal')->with(2)->willReturnSelf();
     $this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
 }
Ejemplo n.º 14
0
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     $amount = $order->getFinanceCostAmount();
     $baseAmount = $order->getBaseFinanceCostAmount();
     if ($amount) {
         $creditmemo->setFinanceCostAmount($amount);
         $creditmemo->setBaseFinanceCostAmount($baseAmount);
         $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $amount);
         $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseAmount);
     }
     return $this;
 }
Ejemplo n.º 15
0
 public function testGetItemsCollectionWithoutId()
 {
     $items = [];
     $itemMock = $this->getMockBuilder('\\Magento\\Sales\\Model\\Order\\Creditmemo\\Item')->disableOriginalConstructor()->getMock();
     $itemMock->expects($this->never())->method('setCreditmemo');
     $items[] = $itemMock;
     /** @var ItemCollection|\PHPUnit_Framework_MockObject_MockObject $itemCollectionMock */
     $itemCollectionMock = $this->getMockBuilder('\\Magento\\Sales\\Model\\ResourceModel\\Order\\Creditmemo\\Item\\Collection')->disableOriginalConstructor()->getMock();
     $itemCollectionMock->expects($this->once())->method('setCreditmemoFilter')->with(null)->will($this->returnValue($items));
     $this->cmItemCollectionFactoryMock->expects($this->any())->method('create')->will($this->returnValue($itemCollectionMock));
     $itemsCollection = $this->creditmemo->getItemsCollection();
     $this->assertEquals($items, $itemsCollection);
 }
Ejemplo n.º 16
0
 /**
  * @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->creditmemoMock->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->creditmemoMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(true);
     $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $billingAddress]);
     $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false);
     $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email');
     $this->assertFalse($this->sender->send($this->creditmemoMock));
 }
Ejemplo n.º 17
0
 /**
  * Collect Creditmemo subtotal
  *
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
     }
     $creditmemo->setSubtotal($subtotal);
     $creditmemo->setBaseSubtotal($baseSubtotal);
     $creditmemo->setSubtotalInclTax($subtotalInclTax);
     $creditmemo->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $subtotal);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Ejemplo n.º 18
0
 /**
  * @covers \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\PrintAction::execute
  */
 public function testExecute()
 {
     $creditmemoId = 2;
     $date = '2015-01-19_13-03-45';
     $fileName = 'creditmemo2015-01-19_13-03-45.pdf';
     $fileContents = 'pdf0123456789';
     $this->prepareTestExecute($creditmemoId);
     $this->objectManagerMock->expects($this->any())->method('create')->willReturnMap([['Magento\\Sales\\Model\\Order\\Creditmemo', [], $this->creditmemoMock], ['Magento\\Sales\\Model\\Order\\Pdf\\Creditmemo', [], $this->creditmemoPdfMock]]);
     $this->creditmemoMock->expects($this->once())->method('load')->with($creditmemoId)->willReturnSelf();
     $this->creditmemoPdfMock->expects($this->once())->method('getPdf')->with([$this->creditmemoMock])->willReturn($this->pdfMock);
     $this->objectManagerMock->expects($this->once())->method('get')->with('Magento\\Framework\\Stdlib\\DateTime\\DateTime')->willReturn($this->dateTimeMock);
     $this->dateTimeMock->expects($this->once())->method('date')->with('Y-m-d_H-i-s')->willReturn($date);
     $this->pdfMock->expects($this->once())->method('render')->willReturn($fileContents);
     $this->fileFactoryMock->expects($this->once())->method('create')->with($fileName, $fileContents, \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR, 'application/pdf')->willReturn($this->responseMock);
     $this->assertInstanceOf('Magento\\Framework\\App\\ResponseInterface', $this->printAction->execute());
 }
Ejemplo n.º 19
0
 /**
  *  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());
 }
Ejemplo n.º 20
0
 /**
  *  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());
 }
Ejemplo n.º 21
0
 public function testCollect()
 {
     $this->creditmemoMock->expects($this->exactly(2))->method('setDiscountAmount')->willReturnSelf();
     $this->creditmemoMock->expects($this->exactly(2))->method('setBaseDiscountAmount')->willReturnSelf();
     $this->creditmemoMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock);
     $this->creditmemoMock->expects($this->once())->method('getBaseShippingAmount')->willReturn(1);
     $this->orderMock->expects($this->once())->method('getBaseShippingDiscountAmount')->willReturn(1);
     $this->orderMock->expects($this->exactly(2))->method('getBaseShippingAmount')->willReturn(1);
     $this->orderMock->expects($this->once())->method('getShippingAmount')->willReturn(1);
     $this->creditmemoMock->expects($this->once())->method('getAllItems')->willReturn([$this->creditmemoItemMock]);
     $this->creditmemoItemMock->expects($this->atLeastOnce())->method('getOrderItem')->willReturn($this->orderItemMock);
     $this->orderItemMock->expects($this->once())->method('isDummy')->willReturn(false);
     $this->orderItemMock->expects($this->once())->method('getDiscountInvoiced')->willReturn(1);
     $this->orderItemMock->expects($this->once())->method('getBaseDiscountInvoiced')->willReturn(1);
     $this->orderItemMock->expects($this->once())->method('getQtyInvoiced')->willReturn(1);
     $this->orderItemMock->expects($this->once())->method('getDiscountRefunded')->willReturn(1);
     $this->orderItemMock->expects($this->once())->method('getQtyRefunded')->willReturn(0);
     $this->creditmemoItemMock->expects($this->once())->method('isLast')->willReturn(false);
     $this->creditmemoItemMock->expects($this->atLeastOnce())->method('getQty')->willReturn(1);
     $this->creditmemoItemMock->expects($this->exactly(1))->method('setDiscountAmount')->willReturnSelf();
     $this->creditmemoItemMock->expects($this->exactly(1))->method('setBaseDiscountAmount')->willReturnSelf();
     $this->creditmemoMock->expects($this->exactly(2))->method('roundPrice')->willReturnMap([[1, 'regular', true, 1], [1, 'base', true, 1]]);
     $this->assertEquals($this->total, $this->total->collect($this->creditmemoMock));
 }
Ejemplo n.º 22
0
 /**
  * @param array $orderData
  * @param array $creditmemoData
  * @param array $expectedResults
  * @dataProvider collectDataProvider
  */
 public function testCollect($orderData, $creditmemoData, $expectedResults)
 {
     $roundingDelta = [];
     //Set up order mock
     foreach ($orderData['data_fields'] as $key => $value) {
         $this->order->setData($key, $value);
     }
     //Set up creditmemo mock
     /** @var \Magento\Sales\Model\Order\Creditmemo\Item[] $creditmemoItems */
     $creditmemoItems = [];
     foreach ($creditmemoData['items'] as $itemKey => $creditmemoItemData) {
         $creditmemoItems[$itemKey] = $this->getCreditmemoItem($creditmemoItemData);
     }
     $this->creditmemo->expects($this->once())->method('getAllItems')->will($this->returnValue($creditmemoItems));
     $this->creditmemo->expects($this->any())->method('isLast')->will($this->returnValue($creditmemoData['is_last']));
     foreach ($creditmemoData['data_fields'] as $key => $value) {
         $this->creditmemo->setData($key, $value);
     }
     $this->creditmemo->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->creditmemo);
     //verify invoice data
     foreach ($expectedResults['creditmemo_data'] as $key => $value) {
         $this->assertEquals($value, $this->creditmemo->getData($key));
     }
     //verify invoice item data
     foreach ($expectedResults['creditmemo_items'] as $itemKey => $itemData) {
         $creditmemoItem = $creditmemoItems[$itemKey];
         foreach ($itemData as $key => $value) {
             $this->assertEquals($value, $creditmemoItem->getData($key));
         }
     }
 }
Ejemplo n.º 23
0
 /**
  * Initialize creditmemo state based on requested parameters
  *
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @param array $data
  * @return void
  */
 protected function _initCreditmemoData($creditmemo, $data)
 {
     if (isset($data['shipping_amount'])) {
         $creditmemo->setBaseShippingAmount((double) $data['shipping_amount']);
     }
     if (isset($data['adjustment_positive'])) {
         $creditmemo->setAdjustmentPositive($data['adjustment_positive']);
     }
     if (isset($data['adjustment_negative'])) {
         $creditmemo->setAdjustmentNegative($data['adjustment_negative']);
     }
 }
Ejemplo n.º 24
0
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $creditmemo->setDiscountAmount(0);
     $creditmemo->setBaseDiscountAmount(0);
     $order = $creditmemo->getOrder();
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     /**
      * Calculate how much shipping discount should be applied
      * basing on how much shipping should be refunded.
      */
     $baseShippingAmount = $creditmemo->getBaseShippingAmount();
     if ($baseShippingAmount) {
         $baseShippingDiscount = $baseShippingAmount * $order->getBaseShippingDiscountAmount() / $order->getBaseShippingAmount();
         $shippingDiscount = $order->getShippingAmount() * $baseShippingDiscount / $order->getBaseShippingAmount();
         $totalDiscountAmount = $totalDiscountAmount + $shippingDiscount;
         $baseTotalDiscountAmount = $baseTotalDiscountAmount + $baseShippingDiscount;
     }
     /** @var $item \Magento\Sales\Model\Order\Invoice\Item */
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy()) {
             continue;
         }
         $orderItemDiscount = (double) $orderItem->getDiscountInvoiced();
         $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountInvoiced();
         $orderItemQty = $orderItem->getQtyInvoiced();
         if ($orderItemDiscount && $orderItemQty) {
             $discount = $orderItemDiscount - $orderItem->getDiscountRefunded();
             $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountRefunded();
             if (!$item->isLast()) {
                 $availableQty = $orderItemQty - $orderItem->getQtyRefunded();
                 $discount = $creditmemo->roundPrice($discount / $availableQty * $item->getQty(), 'regular', true);
                 $baseDiscount = $creditmemo->roundPrice($baseDiscount / $availableQty * $item->getQty(), 'base', true);
             }
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
         }
     }
     $creditmemo->setDiscountAmount(-$totalDiscountAmount);
     $creditmemo->setBaseDiscountAmount(-$baseTotalDiscountAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $totalDiscountAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Ejemplo n.º 25
0
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return \Magento\Framework\Service\Data\AbstractExtensibleObject
  */
 public function extractDto(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $this->creditmemoBuilder->populateWithArray($creditmemo->getData());
     $this->creditmemoBuilder->setItems($this->getItems($creditmemo));
     return $this->creditmemoBuilder->create();
 }
Ejemplo n.º 26
0
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $grandTotal = $creditmemo->getGrandTotal();
     $baseGrandTotal = $creditmemo->getBaseGrandTotal();
     $grandTotal += $creditmemo->getAdjustmentPositive();
     $baseGrandTotal += $creditmemo->getBaseAdjustmentPositive();
     $grandTotal -= $creditmemo->getAdjustmentNegative();
     $baseGrandTotal -= $creditmemo->getBaseAdjustmentNegative();
     $creditmemo->setGrandTotal($grandTotal);
     $creditmemo->setBaseGrandTotal($baseGrandTotal);
     $creditmemo->setAdjustment($creditmemo->getAdjustmentPositive() - $creditmemo->getAdjustmentNegative());
     $creditmemo->setBaseAdjustment($creditmemo->getBaseAdjustmentPositive() - $creditmemo->getBaseAdjustmentNegative());
     return $this;
 }
Ejemplo n.º 27
0
 /**
  * Set transaction ID into creditmemo for informational purposes
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @param \Magento\Sales\Model\Order\Payment $payment
  * @return $this
  */
 public function processCreditmemo($creditmemo, $payment)
 {
     $creditmemo->setTransactionId(1);
     return $this;
 }
Ejemplo n.º 28
0
 /**
  * Send email to customer
  *
  * @param Creditmemo $creditmemo
  * @param bool $notify
  * @param string $comment
  * @return bool
  */
 public function send(Creditmemo $creditmemo, $notify = true, $comment = '')
 {
     $order = $creditmemo->getOrder();
     $this->templateContainer->setTemplateVars(['order' => $order, 'creditmemo' => $creditmemo, 'comment' => $comment, 'billing' => $order->getBillingAddress(), 'store' => $order->getStore()]);
     return $this->checkAndSend($order, $notify);
 }
Ejemplo n.º 29
0
 /**
  * test creditmemo cancel service
  */
 public function testInvoke()
 {
     $this->creditmemoRepositoryMock->expects($this->once())->method('get')->with($this->equalTo(1))->will($this->returnValue($this->creditmemoMock));
     $this->creditmemoMock->expects($this->once())->method('cancel')->will($this->returnSelf());
     $this->assertTrue($this->creditmemoCancel->invoke(1));
 }
Ejemplo n.º 30
0
 /**
  * Cancel a creditmemo: substract its totals from the payment
  *
  * @param Creditmemo $creditmemo
  * @return $this
  */
 public function cancelCreditmemo($creditmemo)
 {
     $this->_updateTotals(['amount_refunded' => -1 * $creditmemo->getGrandTotal(), 'base_amount_refunded' => -1 * $creditmemo->getBaseGrandTotal(), 'shipping_refunded' => -1 * $creditmemo->getShippingAmount(), 'base_shipping_refunded' => -1 * $creditmemo->getBaseShippingAmount()]);
     $this->_eventManager->dispatch('sales_order_payment_cancel_creditmemo', ['payment' => $this, 'creditmemo' => $creditmemo]);
     return $this;
 }