Ejemplo n.º 1
0
 /**
  * @param \Magento\Sales\Model\Order\Invoice $invoice
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
 {
     $invoice->setShippingAmount(0);
     $invoice->setBaseShippingAmount(0);
     $orderShippingAmount = $invoice->getOrder()->getShippingAmount();
     $baseOrderShippingAmount = $invoice->getOrder()->getBaseShippingAmount();
     $shippingInclTax = $invoice->getOrder()->getShippingInclTax();
     $baseShippingInclTax = $invoice->getOrder()->getBaseShippingInclTax();
     if ($orderShippingAmount) {
         /**
          * Check shipping amount in previous invoices
          */
         foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
             if ((double) $previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
                 return $this;
             }
         }
         $invoice->setShippingAmount($orderShippingAmount);
         $invoice->setBaseShippingAmount($baseOrderShippingAmount);
         $invoice->setShippingInclTax($shippingInclTax);
         $invoice->setBaseShippingInclTax($baseShippingInclTax);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $orderShippingAmount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseOrderShippingAmount);
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * @param \Magento\Sales\Model\Order\Invoice $invoice
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
 {
     $invoice->setDiscountAmount(0);
     $invoice->setBaseDiscountAmount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     /**
      * Checking if shipping discount was added in previous invoices.
      * So basically if we have invoice with positive discount and it
      * was not canceled we don't add shipping discount to this one.
      */
     $addShippingDiscount = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getDiscountAmount()) {
             $addShippingDiscount = false;
         }
     }
     if ($addShippingDiscount) {
         $totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount();
         $baseTotalDiscountAmount = $baseTotalDiscountAmount + $invoice->getOrder()->getBaseShippingDiscountAmount();
     }
     /** @var $item \Magento\Sales\Model\Order\Invoice\Item */
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy()) {
             continue;
         }
         $orderItemDiscount = (double) $orderItem->getDiscountAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             /**
              * Resolve rounding problems
              */
             $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced();
             $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced();
             if (!$item->isLast()) {
                 $activeQty = $orderItemQty - $orderItem->getQtyInvoiced();
                 $discount = $invoice->roundPrice($discount / $activeQty * $item->getQty(), 'regular', true);
                 $baseDiscount = $invoice->roundPrice($baseDiscount / $activeQty * $item->getQty(), 'base', true);
             }
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
         }
     }
     $invoice->setDiscountAmount(-$totalDiscountAmount);
     $invoice->setBaseDiscountAmount(-$baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * @param \Magento\Sales\Model\Order\Invoice $invoice
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
 {
     $invoice->setFee(0);
     $invoice->setBaseFee(0);
     $amount = $invoice->getOrder()->getFee();
     $invoice->setFee($amount);
     $amount = $invoice->getOrder()->getBaseFee();
     $invoice->setBaseFee($amount);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $invoice->getFee());
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $invoice->getFee());
     return $this;
 }
Ejemplo n.º 4
0
 /**
  * @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;
 }
Ejemplo n.º 5
0
 /**
  * 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;
 }
Ejemplo n.º 6
0
 /**
  * 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);
     }
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }
Ejemplo n.º 8
0
 /**
  * Collect Weee amounts for the invoice
  *
  * @param  \Magento\Sales\Model\Order\Invoice $invoice
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
 {
     $store = $invoice->getStore();
     $order = $invoice->getOrder();
     $totalWeeeAmount = 0;
     $baseTotalWeeeAmount = 0;
     $totalWeeeAmountInclTax = 0;
     $baseTotalWeeeAmountInclTax = 0;
     $totalWeeeTaxAmount = 0;
     $baseTotalWeeeTaxAmount = 0;
     /** @var \Magento\Sales\Model\Order\Invoice\Item $item */
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if (!$orderItemQty || $orderItem->isDummy() || $item->getQty() <= 0) {
             continue;
         }
         $ratio = $item->getQty() / $orderItemQty;
         $orderItemWeeeAmount = $orderItem->getWeeeTaxAppliedRowAmount();
         $orderItemBaseWeeeAmount = $orderItem->getBaseWeeeTaxAppliedRowAmnt();
         $weeeAmount = $invoice->roundPrice($orderItemWeeeAmount * $ratio);
         $baseWeeeAmount = $invoice->roundPrice($orderItemBaseWeeeAmount * $ratio, 'base');
         $orderItemWeeeInclTax = $this->_weeeData->getRowWeeeTaxInclTax($orderItem);
         $orderItemBaseWeeeInclTax = $this->_weeeData->getBaseRowWeeeTaxInclTax($orderItem);
         $weeeAmountInclTax = $invoice->roundPrice($orderItemWeeeInclTax * $ratio);
         $baseWeeeAmountInclTax = $invoice->roundPrice($orderItemBaseWeeeInclTax * $ratio, 'base');
         $orderItemWeeeTax = $orderItemWeeeInclTax - $orderItemWeeeAmount;
         $itemWeeeTax = $weeeAmountInclTax - $weeeAmount;
         $itemBaseWeeeTax = $baseWeeeAmountInclTax - $baseWeeeAmount;
         if ($item->isLast()) {
             $weeeAmount = $orderItemWeeeAmount - $this->_weeeData->getWeeeAmountInvoiced($orderItem);
             $baseWeeeAmount = $orderItemBaseWeeeAmount - $this->_weeeData->getBaseWeeeAmountInvoiced($orderItem);
             $itemWeeeTax = $orderItemWeeeTax - $this->_weeeData->getWeeeTaxAmountInvoiced($orderItem);
             $itemBaseWeeeTax = $orderItemWeeeTax - $this->_weeeData->getBaseWeeeTaxAmountInvoiced($orderItem);
         }
         $totalWeeeTaxAmount += $itemWeeeTax;
         $baseTotalWeeeTaxAmount += $itemBaseWeeeTax;
         //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
         if ($orderItemWeeeTax != 0) {
             $taxRatio = [];
             if ($item->getTaxRatio()) {
                 $taxRatio = unserialize($item->getTaxRatio());
             }
             $taxRatio[\Magento\Weee\Model\Total\Quote\Weee::ITEM_TYPE] = $itemWeeeTax / $orderItemWeeeTax;
             $item->setTaxRatio(serialize($taxRatio));
         }
         $item->setWeeeTaxAppliedRowAmount($weeeAmount);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmount);
         $newApplied = [];
         $applied = $this->_weeeData->getApplied($orderItem);
         foreach ($applied as $one) {
             $title = (string) $one['title'];
             $one['base_row_amount'] = $invoice->roundPrice($one['base_row_amount'] * $ratio, $title . '_base');
             $one['row_amount'] = $invoice->roundPrice($one['row_amount'] * $ratio, $title);
             $one['base_row_amount_incl_tax'] = $invoice->roundPrice($one['base_row_amount_incl_tax'] * $ratio, $title . '_base');
             $one['row_amount_incl_tax'] = $invoice->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_INVOICED])) {
                 $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] = $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] + $baseWeeeAmount;
             } else {
                 $one[WeeeHelper::KEY_BASE_WEEE_AMOUNT_INVOICED] = $baseWeeeAmount;
             }
             if (isset($one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED])) {
                 $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] = $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] + $weeeAmount;
             } else {
                 $one[WeeeHelper::KEY_WEEE_AMOUNT_INVOICED] = $weeeAmount;
             }
             if (isset($one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED])) {
                 $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] = $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] + $itemWeeeTax;
             } else {
                 $one[WeeeHelper::KEY_BASE_WEEE_TAX_AMOUNT_INVOICED] = $itemWeeeTax;
             }
             if (isset($one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED])) {
                 $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] = $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] + $itemBaseWeeeTax;
             } else {
                 $one[WeeeHelper::KEY_WEEE_TAX_AMOUNT_INVOICED] = $itemBaseWeeeTax;
             }
             $newApplied[] = $one;
         }
         $this->_weeeData->setApplied($orderItem, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
         $totalWeeeAmount += $weeeAmount;
         $baseTotalWeeeAmount += $baseWeeeAmount;
         $totalWeeeAmountInclTax += $weeeAmountInclTax;
         $baseTotalWeeeAmountInclTax += $baseWeeeAmountInclTax;
     }
     $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount();
     $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount();
     $totalWeeeTaxAmount = min($totalWeeeTaxAmount, $allowedTax);
     $baseTotalWeeeTaxAmount = min($baseTotalWeeeTaxAmount, $allowedBaseTax);
     $invoice->setTaxAmount($invoice->getTaxAmount() + $totalWeeeTaxAmount);
     $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalWeeeTaxAmount);
     // Add FPT to subtotal and grand total
     if ($this->_weeeData->includeInSubtotal($store)) {
         $order = $invoice->getOrder();
         $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
         $allowedBaseSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced() - $invoice->getBaseSubtotal();
         $totalWeeeAmount = min($allowedSubtotal, $totalWeeeAmount);
         $baseTotalWeeeAmount = min($allowedBaseSubtotal, $baseTotalWeeeAmount);
         $invoice->setSubtotal($invoice->getSubtotal() + $totalWeeeAmount);
         $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalWeeeAmount);
     }
     if (!$invoice->isLast()) {
         // need to add the Weee amounts including all their taxes
         $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalWeeeAmountInclTax);
         $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalWeeeAmountInclTax);
     } else {
         // since the Subtotal Incl Tax line will already have the taxes on Weee, just add the non-taxable amounts
         $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalWeeeAmount);
         $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalWeeeAmount);
     }
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalWeeeAmount + $totalWeeeTaxAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalWeeeAmount + $baseTotalWeeeTaxAmount);
     return $this;
 }
Ejemplo n.º 9
0
 /**
  * Collect Weee amounts for the invoice
  *
  * @param \Magento\Sales\Model\Order\Invoice $invoice
  * @return $this
  */
 public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
 {
     $store = $invoice->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     $weeeInclTax = 0;
     $baseWeeeInclTax = 0;
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if (!$orderItemQty || $orderItem->isDummy()) {
             continue;
         }
         $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty();
         $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty();
         $weeeTaxAmountInclTax = $this->_weeeData->getWeeeTaxInclTax($item) * $item->getQty();
         $baseWeeeTaxAmountInclTax = $this->_weeeData->getBaseWeeeTaxInclTax($item) * $item->getQty();
         $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount);
         $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());
         $totalTax += $weeeTaxAmount;
         $baseTotalTax += $baseWeeeTaxAmount;
         $weeeInclTax += $weeeTaxAmountInclTax;
         $baseWeeeInclTax += $baseWeeeTaxAmountInclTax;
     }
     // Add FPT to subtotal and grand total
     if ($this->_weeeData->includeInSubtotal($store)) {
         $order = $invoice->getOrder();
         $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
         $allowedBaseSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced() - $invoice->getBaseSubtotal();
         $totalTax = min($allowedSubtotal, $totalTax);
         $baseTotalTax = min($allowedBaseSubtotal, $baseTotalTax);
         $invoice->setSubtotal($invoice->getSubtotal() + $totalTax);
         $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalTax);
     }
     $useWeeeInclTax = true;
     if ($this->_weeeData->isTaxIncluded($store) && $invoice->isLast()) {
         $useWeeeInclTax = false;
     }
     if ($useWeeeInclTax) {
         // need to add the Weee amounts including all their taxes
         $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $weeeInclTax);
         $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseWeeeInclTax);
     } else {
         // since the Subtotal Incl Tax line will already have the taxes on Weee, just add the non-taxable amounts
         $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $totalTax);
         $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseTotalTax);
     }
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Ejemplo n.º 10
0
/**   
 * 2016-09-08
 * 2016-03-27
 * «How is an online refunding implemented?» https://mage2.pro/t/959
 * Сначала хотел cделать по аналогии с @see \Magento\Paypal\Model\Ipn::_registerPaymentRefund()
 * https://github.com/magento/magento2/blob/9546277/app/code/Magento/Paypal/Model/Ipn.php#L467-L501
 * Однако используемый там метод @see \Magento\Sales\Model\Order\Payment::registerRefundNotification()
 * нерабочий: «Invalid method Magento\Sales\Model\Order\Creditmemo::register»
 * https://mage2.pro/t/1029
 * Поэтому делаю по аналогии с @see \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\Save::execute()
 *
 * @param P $p
 * @param I $i
 * @param string|int|float|null $amount [optional]
 * @return int
 */
function dfp_refund(P $p, I $i, $amount = null)
{
    /** @var M $m */
    $m = $p->getMethodInstance();
    /** @var O $o */
    $o = $m->o();
    /** @var CML $cml */
    $cml = df_o(CML::class);
    $cml->setOrderId($o->getId());
    $cml->setInvoiceId($i->getId());
    if ($amount) {
        /**
         * 2016-09-08
         * Обработка частичного возврата.
         * Делаем по аналогии с @see \Dfe\TwoCheckout\Handler\RefundIssued::cm()
         *
         * Произвожу расчёты в базовой валюте, чтобы не мешали курсовые колебания,
         * которые могли произойти в период между платежом и возвратом.
         */
        /** @var float $refundAmountB */
        $refundAmountB = $m->cToBase($m->amountParse($amount));
        /** @var float $invoiceAmountB */
        $invoiceAmountB = $i->getBaseGrandTotal();
        /** @var float $diffB */
        $diffB = $invoiceAmountB - $refundAmountB;
        if (!df_is0($diffB)) {
            /**
             * 2016-05-23
             * https://mage2.pro/tags/credit-memo-adjustment
             *
             * Стек вызова:
             * 1) @used-by \Magento\Sales\Controller\Adminhtml\Order\CreditmemoLoader::load()
             * https://github.com/magento/magento2/blob/b366da/app/code/Magento/Sales/Controller/Adminhtml/Order/CreditmemoLoader.php#L186
             * 2) @used-by \Magento\Sales\Model\Order\CreditmemoFactory::createByInvoice()
             * https://github.com/magento/magento2/blob/b366da/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L155
             * 3) @used-by \Magento\Sales\Model\Order\CreditmemoFactory::initData()
             * https://github.com/magento/magento2/blob/b366da/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L244-L246
             */
            $cml->setCreditmemo(['adjustment_negative' => df_currency_convert($diffB, df_currency_base($o), $o->getOrderCurrency())]);
        }
    }
    /** @var CM $cm */
    $cm = $cml->load();
    df_assert($cm);
    /**
     * 2016-03-28
     * Важно! Иначе order загрузит payment автоматически вместо нашего,
     * и флаг @see \Dfe\Stripe\Method::WEBHOOK_CASE будет утерян
     */
    $cm->getOrder()->setData(O::PAYMENT, $p);
    /** @var ICMS|CMS $cms */
    $cms = df_om()->create(ICMS::class);
    $cms->refund($cm, false);
    /**
     * 2016-03-28
     * @todo Надо отослать покупателю письмо-оповещение о возврате оплаты.
     * 2016-05-15
     * Что интересно, при возврате из административной части Magento 2
     * покупатель тоже не получает уведомление.
     */
    return $cm->getId();
}
Ejemplo n.º 11
0
 /**
  * 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;
 }