/** * Get array of arrays with totals information for display in PDF * array( * $index => array( * 'amount' => $amount, * 'label' => $label, * 'font_size'=> $font_size * ) * ) * @return array */ public function getTotalsForDisplay() { $store = $this->getOrder()->getStore(); $amount = $this->getOrder()->formatPriceTxt($this->getAmount()); $amountInclTax = $this->getSource()->getShippingInclTax(); if (!$amountInclTax) { $amountInclTax = $this->getAmount() + $this->getSource()->getShippingTaxAmount(); } $amountInclTax = $this->getOrder()->formatPriceTxt($amountInclTax); $fontSize = $this->getFontSize() ? $this->getFontSize() : 7; if ($this->_taxConfig->displaySalesShippingBoth($store)) { $totals = [['amount' => $this->getAmountPrefix() . $amount, 'label' => __('Shipping (Excl. Tax)') . ':', 'font_size' => $fontSize], ['amount' => $this->getAmountPrefix() . $amountInclTax, 'label' => __('Shipping (Incl. Tax)') . ':', 'font_size' => $fontSize]]; } elseif ($this->_taxConfig->displaySalesShippingInclTax($store)) { $totals = [['amount' => $this->getAmountPrefix() . $amountInclTax, 'label' => __($this->getTitle()) . ':', 'font_size' => $fontSize]]; } else { $totals = [['amount' => $this->getAmountPrefix() . $amount, 'label' => __($this->getTitle()) . ':', 'font_size' => $fontSize]]; } return $totals; }
/** * Get label for shipping total based on configuration settings * * @return string */ public function getShippingLabel() { $source = $this->getSource(); if ($this->_taxConfig->displaySalesShippingInclTax($source->getOrder()->getStoreId())) { $label = __('Refund Shipping (Incl. Tax)'); } elseif ($this->_taxConfig->displaySalesShippingBoth($source->getOrder()->getStoreId())) { $label = __('Refund Shipping (Excl. Tax)'); } else { $label = __('Refund Shipping'); } return $label; }
/** * @return $this */ protected function _initShipping() { $store = $this->getStore(); $parent = $this->getParentBlock(); $shipping = $parent->getTotal('shipping'); if (!$shipping) { return $this; } if ($this->_config->displaySalesShippingBoth($store)) { $shipping = (double) $this->_source->getShippingAmount(); $baseShipping = (double) $this->_source->getBaseShippingAmount(); $shippingIncl = (double) $this->_source->getShippingInclTax(); if (!$shippingIncl) { $shippingIncl = $shipping + (double) $this->_source->getShippingTaxAmount(); } $baseShippingIncl = (double) $this->_source->getBaseShippingInclTax(); if (!$baseShippingIncl) { $baseShippingIncl = $baseShipping + (double) $this->_source->getBaseShippingTaxAmount(); } $totalExcl = new \Magento\Framework\DataObject(['code' => 'shipping', 'value' => $shipping, 'base_value' => $baseShipping, 'label' => __('Shipping & Handling (Excl.Tax)')]); $totalIncl = new \Magento\Framework\DataObject(['code' => 'shipping_incl', 'value' => $shippingIncl, 'base_value' => $baseShippingIncl, 'label' => __('Shipping & Handling (Incl.Tax)')]); $parent->addTotal($totalExcl, 'shipping'); $parent->addTotal($totalIncl, 'shipping'); } elseif ($this->_config->displaySalesShippingInclTax($store)) { $shippingIncl = $this->_source->getShippingInclTax(); if (!$shippingIncl) { $shippingIncl = $this->_source->getShippingAmount() + $this->_source->getShippingTaxAmount(); } $baseShippingIncl = $this->_source->getBaseShippingInclTax(); if (!$baseShippingIncl) { $baseShippingIncl = $this->_source->getBaseShippingAmount() + $this->_source->getBaseShippingTaxAmount(); } $total = $parent->getTotal('shipping'); if ($total) { $total->setValue($shippingIncl); $total->setBaseValue($baseShippingIncl); } } return $this; }
/** * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo * @return $this * @throws \Magento\Framework\Model\Exception */ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo) { $order = $creditmemo->getOrder(); $allowedAmount = $order->getShippingAmount() - $order->getShippingRefunded(); $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded(); $shipping = $order->getShippingAmount(); $baseShipping = $order->getBaseShippingAmount(); $shippingInclTax = $order->getShippingInclTax(); $baseShippingInclTax = $order->getBaseShippingInclTax(); $isShippingInclTax = $this->_taxConfig->displaySalesShippingInclTax($order->getStoreId()); /** * 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()); if ($isShippingInclTax && $baseShippingInclTax != 0) { $part = $baseShippingAmount / $baseShippingInclTax; $shippingInclTax = $this->priceCurrency->round($shippingInclTax * $part); $baseShippingInclTax = $baseShippingAmount; $baseShippingAmount = $this->priceCurrency->round($baseShipping * $part); } /* * 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) { /* * 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) { $shipping = $allowedAmount; $baseShipping = $baseAllowedAmount; } else { if ($baseShipping != 0) { $shipping = $shipping * $baseShippingAmount / $baseShipping; } $shipping = $this->priceCurrency->round($shipping); $baseShipping = $baseShippingAmount; } } else { $baseAllowedAmount = $order->getBaseCurrency()->format($baseAllowedAmount, null, false); throw new \Magento\Framework\Model\Exception(__('Maximum shipping amount allowed to refund is: %1', $baseAllowedAmount)); } } else { if ($baseShipping != 0) { $allowedTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded(); $baseAllowedTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded(); $shippingInclTax = $this->priceCurrency->round($allowedAmount + $allowedTaxAmount); $baseShippingInclTax = $this->priceCurrency->round($baseAllowedAmount + $baseAllowedTaxAmount); } $shipping = $allowedAmount; $baseShipping = $baseAllowedAmount; } $creditmemo->setShippingAmount($shipping); $creditmemo->setBaseShippingAmount($baseShipping); $creditmemo->setShippingInclTax($shippingInclTax); $creditmemo->setBaseShippingInclTax($baseShippingInclTax); $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $shipping); $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShipping); return $this; }
/** * Prepare order creditmemo based on invoice items and requested requested params * * @param object $invoice * @param array $data * @return \Magento\Sales\Model\Order\Creditmemo * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function prepareInvoiceCreditmemo($invoice, $data = []) { $totalQty = 0; $qtys = isset($data['qtys']) ? $data['qtys'] : []; $creditmemo = $this->_convertor->toCreditmemo($this->_order); $creditmemo->setInvoice($invoice); $invoiceQtysRefunded = []; foreach ($invoice->getOrder()->getCreditmemosCollection() as $createdCreditmemo) { if ($createdCreditmemo->getState() != \Magento\Sales\Model\Order\Creditmemo::STATE_CANCELED && $createdCreditmemo->getInvoiceId() == $invoice->getId()) { foreach ($createdCreditmemo->getAllItems() as $createdCreditmemoItem) { $orderItemId = $createdCreditmemoItem->getOrderItem()->getId(); if (isset($invoiceQtysRefunded[$orderItemId])) { $invoiceQtysRefunded[$orderItemId] += $createdCreditmemoItem->getQty(); } else { $invoiceQtysRefunded[$orderItemId] = $createdCreditmemoItem->getQty(); } } } } $invoiceQtysRefundLimits = []; foreach ($invoice->getAllItems() as $invoiceItem) { $invoiceQtyCanBeRefunded = $invoiceItem->getQty(); $orderItemId = $invoiceItem->getOrderItem()->getId(); if (isset($invoiceQtysRefunded[$orderItemId])) { $invoiceQtyCanBeRefunded = $invoiceQtyCanBeRefunded - $invoiceQtysRefunded[$orderItemId]; } $invoiceQtysRefundLimits[$orderItemId] = $invoiceQtyCanBeRefunded; } foreach ($invoice->getAllItems() as $invoiceItem) { $orderItem = $invoiceItem->getOrderItem(); if (!$this->_canRefundItem($orderItem, $qtys, $invoiceQtysRefundLimits)) { continue; } $item = $this->_convertor->itemToCreditmemoItem($orderItem); if ($orderItem->isDummy()) { $qty = 1; } else { if (isset($qtys[$orderItem->getId()])) { $qty = (double) $qtys[$orderItem->getId()]; } elseif (!count($qtys)) { $qty = $orderItem->getQtyToRefund(); } else { continue; } if (isset($invoiceQtysRefundLimits[$orderItem->getId()])) { $qty = min($qty, $invoiceQtysRefundLimits[$orderItem->getId()]); } } $qty = min($qty, $invoiceItem->getQty()); $totalQty += $qty; $item->setQty($qty); $creditmemo->addItem($item); } $creditmemo->setTotalQty($totalQty); $this->_initCreditmemoData($creditmemo, $data); if (!isset($data['shipping_amount'])) { $order = $invoice->getOrder(); $isShippingInclTax = $this->_taxConfig->displaySalesShippingInclTax($order->getStoreId()); if ($isShippingInclTax) { $baseAllowedAmount = $order->getBaseShippingInclTax() - $order->getBaseShippingRefunded() - $order->getBaseShippingTaxRefunded(); } else { $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded(); $baseAllowedAmount = min($baseAllowedAmount, $invoice->getBaseShippingAmount()); } $creditmemo->setBaseShippingAmount($baseAllowedAmount); } $creditmemo->collectTotals(); return $creditmemo; }