コード例 #1
0
ファイル: Fee.php プロジェクト: sivajik34/Custom-Fee-Magento2
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $creditmemo->setFee(0);
     $creditmemo->setBaseFee(0);
     $amount = $creditmemo->getOrder()->getFee();
     $creditmemo->setFee($amount);
     $amount = $creditmemo->getOrder()->getBaseFee();
     $creditmemo->setBaseFee($amount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $creditmemo->getFee());
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $creditmemo->getBaseFee());
     return $this;
 }
コード例 #2
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;
 }
コード例 #3
0
ファイル: Grand.php プロジェクト: pradeep-wagento/magento2
 /**
  * @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;
 }
コード例 #4
0
ファイル: Discount.php プロジェクト: aiesh/magento2
 /**
  * @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;
 }
コード例 #5
0
ファイル: Subtotal.php プロジェクト: aiesh/magento2
 /**
  * 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;
 }
コード例 #6
0
ファイル: Shipping.php プロジェクト: pradeep-wagento/magento2
 /**
  * @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;
 }
コード例 #7
0
 /**
  * @param \Magento\Sales\Model\Order\Creditmemo $creditmemo
  * @return $this
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
 {
     $shippingTaxAmount = 0;
     $baseShippingTaxAmount = 0;
     $totalTax = 0;
     $baseTotalTax = 0;
     $totalDiscountTaxCompensation = 0;
     $baseTotalDiscountTaxCompensation = 0;
     $order = $creditmemo->getOrder();
     /** @var $item \Magento\Sales\Model\Order\Creditmemo\Item */
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy() || $item->getQty() <= 0) {
             continue;
         }
         $orderItemTax = (double) $orderItem->getTaxInvoiced();
         $baseOrderItemTax = (double) $orderItem->getBaseTaxInvoiced();
         $orderItemQty = (double) $orderItem->getQtyInvoiced();
         if ($orderItemTax && $orderItemQty) {
             /**
              * Check item tax amount
              */
             $tax = $orderItemTax - $orderItem->getTaxRefunded();
             $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxRefunded();
             $discountTaxCompensation = $orderItem->getDiscountTaxCompensationInvoiced() - $orderItem->getDiscountTaxCompensationRefunded();
             $baseDiscountTaxCompensation = $orderItem->getBaseDiscountTaxCompensationInvoiced() - $orderItem->getBaseDiscountTaxCompensationRefunded();
             if (!$item->isLast()) {
                 $availableQty = $orderItemQty - $orderItem->getQtyRefunded();
                 $tax = $creditmemo->roundPrice($tax / $availableQty * $item->getQty());
                 $baseTax = $creditmemo->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
                 $discountTaxCompensation = $creditmemo->roundPrice($discountTaxCompensation / $availableQty * $item->getQty());
                 $baseDiscountTaxCompensation = $creditmemo->roundPrice($baseDiscountTaxCompensation / $availableQty * $item->getQty(), 'base');
             }
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $item->setDiscountTaxCompensationAmount($discountTaxCompensation);
             $item->setBaseDiscountTaxCompensationAmount($baseDiscountTaxCompensation);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
             $totalDiscountTaxCompensation += $discountTaxCompensation;
             $baseTotalDiscountTaxCompensation += $baseDiscountTaxCompensation;
         }
     }
     $isPartialShippingRefunded = false;
     if ($invoice = $creditmemo->getInvoice()) {
         //recalculate tax amounts in case if refund shipping value was changed
         if ($order->getBaseShippingAmount() && $creditmemo->getBaseShippingAmount()) {
             $taxFactor = $creditmemo->getBaseShippingAmount() / $order->getBaseShippingAmount();
             $shippingTaxAmount = $invoice->getShippingTaxAmount() * $taxFactor;
             $baseShippingTaxAmount = $invoice->getBaseShippingTaxAmount() * $taxFactor;
             $totalDiscountTaxCompensation += $invoice->getShippingDiscountTaxCompensationAmount() * $taxFactor;
             $baseTotalDiscountTaxCompensation += $invoice->getBaseShippingDiscountTaxCompensationAmnt() * $taxFactor;
             $shippingDiscountTaxCompensationAmount = $invoice->getShippingDiscountTaxCompensationAmount() * $taxFactor;
             $baseShippingDiscountTaxCompensationAmount = $invoice->getBaseShippingDiscountTaxCompensationAmnt() * $taxFactor;
             $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
             $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
             $totalDiscountTaxCompensation = $creditmemo->roundPrice($totalDiscountTaxCompensation);
             $baseTotalDiscountTaxCompensation = $creditmemo->roundPrice($baseTotalDiscountTaxCompensation, 'base');
             $shippingDiscountTaxCompensationAmount = $creditmemo->roundPrice($shippingDiscountTaxCompensationAmount);
             $baseShippingDiscountTaxCompensationAmount = $creditmemo->roundPrice($baseShippingDiscountTaxCompensationAmount, 'base');
             if ($taxFactor < 1 && $invoice->getShippingTaxAmount() > 0) {
                 $isPartialShippingRefunded = true;
             }
             $totalTax += $shippingTaxAmount;
             $baseTotalTax += $baseShippingTaxAmount;
         }
     } else {
         $orderShippingAmount = $order->getShippingAmount();
         $baseOrderShippingAmount = $order->getBaseShippingAmount();
         $baseOrderShippingRefundedAmount = $order->getBaseShippingRefunded();
         $shippingTaxAmount = 0;
         $baseShippingTaxAmount = 0;
         $shippingDiscountTaxCompensationAmount = 0;
         $baseShippingDiscountTaxCompensationAmount = 0;
         $shippingDelta = $baseOrderShippingAmount - $baseOrderShippingRefundedAmount;
         if ($shippingDelta > $creditmemo->getBaseShippingAmount()) {
             $part = $creditmemo->getShippingAmount() / $orderShippingAmount;
             $basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;
             $shippingTaxAmount = $order->getShippingTaxAmount() * $part;
             $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() * $basePart;
             $shippingDiscountTaxCompensationAmount = $order->getShippingDiscountTaxCompensationAmount() * $part;
             $baseShippingDiscountTaxCompensationAmount = $order->getBaseShippingDiscountTaxCompensationAmnt() * $basePart;
             $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
             $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
             $shippingDiscountTaxCompensationAmount = $creditmemo->roundPrice($shippingDiscountTaxCompensationAmount);
             $baseShippingDiscountTaxCompensationAmount = $creditmemo->roundPrice($baseShippingDiscountTaxCompensationAmount, 'base');
             if ($part < 1 && $order->getShippingTaxAmount() > 0) {
                 $isPartialShippingRefunded = true;
             }
         } elseif ($shippingDelta == $creditmemo->getBaseShippingAmount()) {
             $shippingTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
             $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
             $shippingDiscountTaxCompensationAmount = $order->getShippingDiscountTaxCompensationAmount() - $order->getShippingDiscountTaxCompensationRefunded();
             $baseShippingDiscountTaxCompensationAmount = $order->getBaseShippingDiscountTaxCompensationAmnt() - $order->getBaseShippingDiscountTaxCompensationRefunded();
         }
         $totalTax += $shippingTaxAmount;
         $baseTotalTax += $baseShippingTaxAmount;
         $totalDiscountTaxCompensation += $shippingDiscountTaxCompensationAmount;
         $baseTotalDiscountTaxCompensation += $baseShippingDiscountTaxCompensationAmount;
     }
     $allowedTax = $order->getTaxInvoiced() - $order->getTaxRefunded() - $creditmemo->getTaxAmount();
     $allowedBaseTax = $order->getBaseTaxInvoiced() - $order->getBaseTaxRefunded() - $creditmemo->getBaseTaxAmount();
     $allowedDiscountTaxCompensation = $order->getDiscountTaxCompensationInvoiced() + $order->getShippingDiscountTaxCompensationAmount() - $order->getDiscountTaxCompensationRefunded() - $order->getShippingDiscountTaxCompensationRefunded() - $creditmemo->getDiscountTaxCompensationAmount() - $creditmemo->getShippingDiscountTaxCompensationAmount();
     $allowedBaseDiscountTaxCompensation = $order->getBaseDiscountTaxCompensationInvoiced() + $order->getBaseShippingDiscountTaxCompensationAmnt() - $order->getBaseDiscountTaxCompensationRefunded() - $order->getBaseShippingDiscountTaxCompensationRefunded() - $creditmemo->getBaseShippingDiscountTaxCompensationAmnt() - $creditmemo->getBaseDiscountTaxCompensationAmount();
     if ($creditmemo->isLast() && !$isPartialShippingRefunded) {
         $totalTax = $allowedTax;
         $baseTotalTax = $allowedBaseTax;
         $totalDiscountTaxCompensation = $allowedDiscountTaxCompensation;
         $baseTotalDiscountTaxCompensation = $allowedBaseDiscountTaxCompensation;
     } else {
         $totalTax = min($allowedTax, $totalTax);
         $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
         $totalDiscountTaxCompensation = min($allowedDiscountTaxCompensation, $totalDiscountTaxCompensation);
         $baseTotalDiscountTaxCompensation = min($allowedBaseDiscountTaxCompensation, $baseTotalDiscountTaxCompensation);
     }
     $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTax);
     $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTax);
     $creditmemo->setDiscountTaxCompensationAmount($totalDiscountTaxCompensation);
     $creditmemo->setBaseDiscountTaxCompensationAmount($baseTotalDiscountTaxCompensation);
     $creditmemo->setShippingTaxAmount($shippingTaxAmount);
     $creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax + $totalDiscountTaxCompensation);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax + $baseTotalDiscountTaxCompensation);
     return $this;
 }
コード例 #8
0
ファイル: Weee.php プロジェクト: kidaa30/magento2-platformsh
 /**
  * Collect Weee amounts for the credit memo
  *
  * @param  Creditmemo $creditmemo
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function collect(Creditmemo $creditmemo)
 {
     $store = $creditmemo->getStore();
     $totalWeeeAmount = 0;
     $baseTotalWeeeAmount = 0;
     $totalWeeeAmountInclTax = 0;
     $baseTotalWeeeAmountInclTax = 0;
     $totalTaxAmount = 0;
     $baseTotalTaxAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() < 0) {
             continue;
         }
         $ratio = $item->getQty() / $orderItemQty;
         $orderItemWeeeAmountExclTax = $orderItem->getWeeeTaxAppliedRowAmount();
         $orderItemBaseWeeeAmountExclTax = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
         $weeeAmountExclTax = $creditmemo->roundPrice($orderItemWeeeAmountExclTax * $ratio);
         $baseWeeeAmountExclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountExclTax * $ratio, 'base');
         $orderItemWeeeAmountInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem);
         $orderItemBaseWeeeAmountInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem);
         $weeeAmountInclTax = $creditmemo->roundPrice($orderItemWeeeAmountInclTax * $ratio);
         $baseWeeeAmountInclTax = $creditmemo->roundPrice($orderItemBaseWeeeAmountInclTax * $ratio, 'base');
         $itemTaxAmount = $weeeAmountInclTax - $weeeAmountExclTax;
         $itemBaseTaxAmount = $baseWeeeAmountInclTax - $baseWeeeAmountExclTax;
         $weeeAmountAvailable = $this->_weeeData->getWeeeAmountInvoiced($orderItem) - $this->_weeeData->getWeeeAmountRefunded($orderItem);
         $baseWeeeAmountAvailable = $this->_weeeData->getBaseWeeeAmountInvoiced($orderItem) - $this->_weeeData->getBaseWeeeAmountRefunded($orderItem);
         $weeeTaxAmountAvailable = $this->_weeeData->getWeeeTaxAmountInvoiced($orderItem) - $this->_weeeData->getWeeeTaxAmountRefunded($orderItem);
         $baseWeeeTaxAmountAvailable = $this->_weeeData->getBaseWeeeTaxAmountInvoiced($orderItem) - $this->_weeeData->getBaseWeeeTaxAmountRefunded($orderItem);
         if ($item->isLast()) {
             $weeeAmountExclTax = $weeeAmountAvailable;
             $baseWeeeAmountExclTax = $baseWeeeAmountAvailable;
             $itemTaxAmount = $weeeTaxAmountAvailable;
             $itemBaseTaxAmount = $baseWeeeTaxAmountAvailable;
         } else {
             $weeeAmountExclTax = min($weeeAmountExclTax, $weeeAmountAvailable);
             $baseWeeeAmountExclTax = min($baseWeeeAmountExclTax, $baseWeeeAmountAvailable);
             $itemTaxAmount = min($itemTaxAmount, $weeeTaxAmountAvailable);
             $itemBaseTaxAmount = min($itemBaseTaxAmount, $baseWeeeTaxAmountAvailable);
         }
         $totalWeeeAmount += $weeeAmountExclTax;
         $baseTotalWeeeAmount += $baseWeeeAmountExclTax;
         $item->setWeeeTaxAppliedRowAmount($weeeAmountExclTax);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmountExclTax);
         $totalTaxAmount += $itemTaxAmount;
         $baseTotalTaxAmount += $itemBaseTaxAmount;
         //Set the ratio of the tax amount in invoice item compared to tax amount in order item
         //This information is needed to calculate tax per tax rate later
         $orderItemTaxAmount = $orderItemWeeeAmountInclTax - $orderItemWeeeAmountExclTax;
         if ($orderItemTaxAmount != 0) {
             $taxRatio = [];
             if ($item->getTaxRatio()) {
                 $taxRatio = unserialize($item->getTaxRatio());
             }
             $taxRatio[\Magento\Weee\Model\Total\Quote\Weee::ITEM_TYPE] = $itemTaxAmount / $orderItemTaxAmount;
             $item->setTaxRatio(serialize($taxRatio));
         }
         $totalWeeeAmountInclTax += $weeeAmountInclTax;
         $baseTotalWeeeAmountInclTax += $baseWeeeAmountInclTax;
         $newApplied = [];
         $applied = $this->_weeeData->getApplied($orderItem);
         foreach ($applied as $one) {
             $title = (string) $one['title'];
             $one['base_row_amount'] = $creditmemo->roundPrice($one['base_row_amount'] * $ratio, $title . '_base');
             $one['row_amount'] = $creditmemo->roundPrice($one['row_amount'] * $ratio, $title);
             $one['base_row_amount_incl_tax'] = $creditmemo->roundPrice($one['base_row_amount_incl_tax'] * $ratio, $title . '_base');
             $one['row_amount_incl_tax'] = $creditmemo->roundPrice($one['row_amount_incl_tax'] * $ratio, $title);
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($item, $newApplied);
         // Update order item
         $newApplied = [];
         $applied = $this->_weeeData->getApplied($orderItem);
         foreach ($applied as $one) {
             if (isset($one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED] + $baseWeeeAmountExclTax;
             } else {
                 $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_REFUNDED] = $baseWeeeAmountExclTax;
             }
             if (isset($one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED] + $weeeAmountExclTax;
             } else {
                 $one[WeeeHelper::KEY_WEEE_AMOUNT_REFUNDED] = $weeeAmountExclTax;
             }
             if (isset($one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED] + $itemBaseTaxAmount;
             } else {
                 $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_REFUNDED] = $itemBaseTaxAmount;
             }
             if (isset($one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED])) {
                 $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED] = $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED] + $itemTaxAmount;
             } else {
                 $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_REFUNDED] = $itemTaxAmount;
             }
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($orderItem, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
     }
     if ($this->_weeeData->includeInSubtotal($store)) {
         $creditmemo->setSubtotal($creditmemo->getSubtotal() + $totalWeeeAmount);
         $creditmemo->setBaseSubtotal($creditmemo->getBaseSubtotal() + $baseTotalWeeeAmount);
     }
     $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTaxAmount);
     $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTaxAmount);
     $creditmemo->setSubtotalInclTax($creditmemo->getSubtotalInclTax() + $totalWeeeAmountInclTax);
     $creditmemo->setBaseSubtotalInclTax($creditmemo->getBaseSubtotalInclTax() + $baseTotalWeeeAmountInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalWeeeAmount + $totalTaxAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalWeeeAmount + $baseTotalTaxAmount);
     return $this;
 }
コード例 #9
0
 public function testIsValidGrandTotal()
 {
     $this->creditmemo->setGrandTotal(1);
     $this->assertTrue($this->creditmemo->isValidGrandTotal());
 }
コード例 #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();
     // amounts without tax
     $orderShippingAmount = $order->getShippingAmount();
     $orderBaseShippingAmount = $order->getBaseShippingAmount();
     $allowedAmount = $orderShippingAmount - $order->getShippingRefunded();
     $baseAllowedAmount = $orderBaseShippingAmount - $order->getBaseShippingRefunded();
     // amounts including tax
     $orderShippingInclTax = $order->getShippingInclTax();
     $orderBaseShippingInclTax = $order->getBaseShippingInclTax();
     $allowedTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
     $baseAllowedTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
     $allowedAmountInclTax = $allowedAmount + $allowedTaxAmount;
     $baseAllowedAmountInclTax = $baseAllowedAmount + $baseAllowedTaxAmount;
     // for the credit memo
     $shippingAmount = $baseShippingAmount = $shippingInclTax = $baseShippingInclTax = 0;
     // Check if the desired shipping amount to refund was specified (from invoice or another source).
     if ($creditmemo->hasBaseShippingAmount()) {
         // For the conditional logic, we will either use amounts that always include tax -OR- never include tax.
         // The logic uses the 'base' currency to be consistent with what the user (admin) provided as input.
         $useAmountsWithTax = $this->isSuppliedShippingAmountInclTax($order);
         // Since the user (admin) supplied 'desiredAmount' it already has tax -OR- does not include tax
         $desiredAmount = $this->priceCurrency->round($creditmemo->getBaseShippingAmount());
         $maxAllowedAmount = $useAmountsWithTax ? $baseAllowedAmountInclTax : $baseAllowedAmount;
         $originalTotalAmount = $useAmountsWithTax ? $orderBaseShippingInclTax : $orderBaseShippingAmount;
         // Note: ($x < $y + 0.0001) means ($x <= $y) for floats
         if ($desiredAmount < $this->priceCurrency->round($maxAllowedAmount) + 0.0001) {
             // since the admin is returning less than the allowed amount, compute the ratio being returned
             $ratio = 0;
             if ($originalTotalAmount > 0) {
                 $ratio = $desiredAmount / $originalTotalAmount;
             }
             // capture amounts without tax
             // Note: ($x > $y - 0.0001) means ($x >= $y) for floats
             if ($desiredAmount > $maxAllowedAmount - 0.0001) {
                 $shippingAmount = $allowedAmount;
                 $baseShippingAmount = $baseAllowedAmount;
             } else {
                 $shippingAmount = $this->priceCurrency->round($orderShippingAmount * $ratio);
                 $baseShippingAmount = $this->priceCurrency->round($orderBaseShippingAmount * $ratio);
             }
             $shippingInclTax = $this->priceCurrency->round($orderShippingInclTax * $ratio);
             $baseShippingInclTax = $this->priceCurrency->round($orderBaseShippingInclTax * $ratio);
         } else {
             $maxAllowedAmount = $order->getBaseCurrency()->format($maxAllowedAmount, null, false);
             throw new \Magento\Framework\Exception\LocalizedException(__('Maximum shipping amount allowed to refund is: %1', $maxAllowedAmount));
         }
     } else {
         $shippingAmount = $allowedAmount;
         $baseShippingAmount = $baseAllowedAmount;
         $shippingInclTax = $this->priceCurrency->round($allowedAmountInclTax);
         $baseShippingInclTax = $this->priceCurrency->round($baseAllowedAmountInclTax);
     }
     $creditmemo->setShippingAmount($shippingAmount);
     $creditmemo->setBaseShippingAmount($baseShippingAmount);
     $creditmemo->setShippingInclTax($shippingInclTax);
     $creditmemo->setBaseShippingInclTax($baseShippingInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $shippingAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShippingAmount);
     return $this;
 }
コード例 #11
0
ファイル: Weee.php プロジェクト: aiesh/magento2
 /**
  * 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;
 }