public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $totalTax = 0; $baseTotalTax = 0; foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $orderItemTax = $orderItem->getTaxAmount(); $baseOrderItemTax = $orderItem->getBaseTaxAmount(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemTax && $orderItemQty) { /** * Resolve rounding problems */ if ($item->isLast()) { $tax = $orderItemTax - $orderItem->getTaxInvoiced(); $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxInvoiced(); } else { $tax = $orderItemTax * $item->getQty() / $orderItemQty; $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty; $tax = $invoice->getStore()->roundPrice($tax); $baseTax = $invoice->getStore()->roundPrice($baseTax); } $item->setTaxAmount($tax); $item->setBaseTaxAmount($baseTax); $totalTax += $tax; $baseTotalTax += $baseTax; } } $invoice->setTaxAmount($totalTax); $invoice->setBaseTaxAmount($baseTotalTax); $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax); return $this; }
public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $invoice->setGomageGiftWrapAmount(0); $invoice->setBaseGomageGiftWrapAmount(0); $totalGomageGiftWrapAmount = 0; $baseTotalGomageGiftWrapAmount = 0; foreach ($invoice->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } $orderItem = $item->getOrderItem(); $orderItemGomageGiftWrap = (double) $orderItem->getGomageGiftWrapAmount(); $baseOrderItemGomageGiftWrap = (double) $orderItem->getBaseGomageGiftWrapAmount(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemGomageGiftWrap && $orderItemQty) { $GomageGiftWrap = $orderItemGomageGiftWrap * $item->getQty() / $orderItemQty; $baseGomageGiftWrap = $baseOrderItemGomageGiftWrap * $item->getQty() / $orderItemQty; $GomageGiftWrap = $invoice->getStore()->roundPrice($GomageGiftWrap); $baseGomageGiftWrap = $invoice->getStore()->roundPrice($baseGomageGiftWrap); $item->setGomageGiftWrapAmount($GomageGiftWrap); $item->setBaseGomageGiftWrapAmount($baseGomageGiftWrap); $totalGomageGiftWrapAmount += $GomageGiftWrap; $baseTotalGomageGiftWrapAmount += $baseGomageGiftWrap; } } $invoice->setGomageGiftWrapAmount($totalGomageGiftWrapAmount); $invoice->setBaseGomageGiftWrapAmount($baseTotalGomageGiftWrapAmount); $invoice->setGrandTotal($invoice->getGrandTotal() + $totalGomageGiftWrapAmount); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalGomageGiftWrapAmount); return $this; }
public function collect(Mage_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. */ $addShippingDicount = true; foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) { if ($previusInvoice->getDiscountAmount()) { $addShippingDicount = false; } } if ($addShippingDicount) { $totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount(); $baseTotalDiscountAmount = $baseTotalDiscountAmount + $invoice->getOrder()->getBaseShippingDiscountAmount(); } foreach ($invoice->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } $orderItem = $item->getOrderItem(); $orderItemDiscount = (double) $orderItem->getDiscountAmount(); $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemDiscount && $orderItemQty) { /** * Resolve rounding problems */ if ($item->isLast()) { $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced(); $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced(); } else { $discount = $orderItemDiscount * $item->getQty() / $orderItemQty; $baseDiscount = $baseOrderItemDiscount * $item->getQty() / $orderItemQty; $discount = $invoice->getStore()->roundPrice($discount); $baseDiscount = $invoice->getStore()->roundPrice($baseDiscount); } $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; }
public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $totalTax = 0; $baseTotalTax = 0; foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $orderItemTax = $orderItem->getTaxAmount(); $baseOrderItemTax = $orderItem->getBaseTaxAmount(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemTax && $orderItemQty) { if ($item->getOrderItem()->isDummy()) { continue; } /** * Resolve rounding problems */ if ($item->isLast()) { $tax = $orderItemTax - $orderItem->getTaxInvoiced(); $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxInvoiced(); } else { $tax = $orderItemTax * $item->getQty() / $orderItemQty; $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty; $tax = $invoice->getStore()->roundPrice($tax); $baseTax = $invoice->getStore()->roundPrice($baseTax); } $item->setTaxAmount($tax); $item->setBaseTaxAmount($baseTax); $totalTax += $tax; $baseTotalTax += $baseTax; } } $includeShippingTax = true; /** * Check shipping amount in previus invoices */ foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) { if ($previusInvoice->getShippingAmount() && !$previusInvoice->isCanceled()) { $includeShippingTax = false; } } if ($includeShippingTax) { $totalTax += $invoice->getOrder()->getShippingTaxAmount(); $baseTotalTax += $invoice->getOrder()->getBaseShippingTaxAmount(); $invoice->setShippingTaxAmount($invoice->getOrder()->getShippingTaxAmount()); $invoice->setBaseShippingTaxAmount($invoice->getOrder()->getBaseShippingTaxAmount()); } $invoice->setTaxAmount($totalTax); $invoice->setBaseTaxAmount($baseTotalTax); $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax); return $this; }
public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $store = $invoice->getStore(); $totalTax = 0; $baseTotalTax = 0; foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemQty) { if ($orderItem->isDummy()) { continue; } $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty(); $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty(); $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount); $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount); $newApplied = array(); $applied = Mage::helper('weee')->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; } Mage::helper('weee')->setApplied($item, $newApplied); $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty()); $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty()); $totalTax += $weeeTaxAmount; $baseTotalTax += $baseWeeeTaxAmount; } } /* * Add FPT to totals * Notice that we check restriction on allowed tax, because * a) for last invoice we don't need to collect FPT - it is automatically collected by subtotal/tax collector, * that adds whole remaining (not invoiced) subtotal/tax value, so fpt is automatically included into it * b) FPT tax is included into order subtotal/tax value, so after multiple invoices with partial item quantities * it can happen that other collector will take some FPT value from shared subtotal/tax order value */ $order = $invoice->getOrder(); if (Mage::helper('weee')->includeInSubtotal($store)) { $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); } else { $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount(); $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount(); $totalTax = min($allowedTax, $totalTax); $baseTotalTax = min($allowedBaseTax, $baseTotalTax); $invoice->setTaxAmount($invoice->getTaxAmount() + $totalTax); $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalTax); } $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax); return $this; }
public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $invoice->setDiscountAmount(0); $invoice->setBaseDiscountAmount(0); $totalDiscountAmount = 0; $baseTotalDiscountAmount = 0; foreach ($invoice->getAllItems() as $item) { if ($item->getOrderItem()->isDummy()) { continue; } $orderItem = $item->getOrderItem(); $orderItemDiscount = (double) $orderItem->getDiscountAmount(); $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemDiscount && $orderItemQty) { /** * Resolve rounding problems */ if ($item->isLast()) { $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced(); $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced(); } else { $discount = $orderItemDiscount * $item->getQty() / $orderItemQty; $baseDiscount = $baseOrderItemDiscount * $item->getQty() / $orderItemQty; $discount = $invoice->getStore()->roundPrice($discount); $baseDiscount = $invoice->getStore()->roundPrice($baseDiscount); } $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; }
public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $store = $invoice->getStore(); $totalTax = 0; $baseTotalTax = 0; foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemQty) { if ($orderItem->isDummy()) { continue; } $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty(); $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty(); $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount); $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount); $newApplied = array(); $applied = Mage::helper('weee')->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; } Mage::helper('weee')->setApplied($item, $newApplied); $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty()); $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty()); $totalTax += $weeeTaxAmount; $baseTotalTax += $baseWeeeTaxAmount; } } if (Mage::helper('weee')->includeInSubtotal($store)) { $invoice->setSubtotal($invoice->getSubtotal() + $totalTax); $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalTax); } else { $invoice->setTaxAmount($invoice->getTaxAmount() + $totalTax); $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalTax); } $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax); return $this; }
/** * Determines if the object (quote, invoice, or credit memo) should use AvaTax services * * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object * @param Mage_Sales_Model_Quote_Address $shippingAddress * @return bool */ public function isObjectActionable($object, $shippingAddress = null) { $storeId = $object->getStore()->getId(); //is action enabled? $action = $object->getOrder() ? OnePica_AvaTax_Model_Config::ACTION_CALC_SUBMIT : OnePica_AvaTax_Model_Config::ACTION_CALC; if (Mage::getStoreConfig('tax/avatax/action', $storeId) < $action) { return false; } if (!$shippingAddress) { $shippingAddress = $object->getShippingAddress(); } if (!$shippingAddress) { $shippingAddress = $object->getBillingAddress(); } //is the region filtered? if (!$this->isAddressActionable($shippingAddress, $storeId, OnePica_AvaTax_Model_Config::REGIONFILTER_TAX)) { return false; } return true; }
public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $currencyFrom = Mage::getModel('directory/currency')->load($invoice->getOrderCurrencyCode()); $currencyTo = $invoice->getStore()->getBaseCurrency(); /* @var $helper Dutycalculator_Charge_Helper_Data */ $helper = Mage::helper('dccharge'); $invoice->setImportDutyTax(0); $invoice->setBaseImportDutyTax(0); $invoice->setImportDuty(0); $invoice->setBaseImportDuty(0); $invoice->setSalesTax(0); $invoice->setBaseSalesTax(0); $invoice->setDeliveryDutyType($invoice->getOrder()->getDeliveryDutyType()); $invoice->setFailedCalculation($invoice->getOrder()->getFailedCalculation()); $invoice->setDcOrderId(0); foreach ($invoice->getAllItems() as $invoiceItem) { $invoiceItem->setImportDutyTax(0); $invoiceItem->setBaseImportDutyTax(0); $invoiceItem->setImportDuty(0); $invoiceItem->setBaseImportDuty(0); $invoiceItem->setSalesTax(0); $invoiceItem->setBaseSalesTax(0); } if ($invoice->getOrder()->getDcOrderId()) { $result = Dutycalculator_Charge_Model_Importdutytaxes::invoiceCalculation($invoice); if ($result) { $amountToInvoice = $result['total']; $baseAmountToInvoice = $helper->convertPrice($currencyFrom, $currencyTo, $result['total']); $invoice->setImportDutyTax($result['total']); $invoice->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $result['total'])); $invoice->setImportDuty($result['duty']); $invoice->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $result['duty'])); $invoice->setSalesTax($result['sales_tax']); $invoice->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $result['sales_tax'])); $invoice->setDeliveryDutyType($invoice->getOrder()->getDeliveryDutyType()); $invoice->setFailedCalculation($invoice->getOrder()->getFailedCalculation()); $invoice->setDcOrderId($result['dc_order_id']); if ($invoice->getOrder()->getDeliveryDutyType() == Dutycalculator_Charge_Helper_Data::DC_DELIVERY_TYPE_DDP) { $invoice->setGrandTotal($invoice->getGrandTotal() + $amountToInvoice); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseAmountToInvoice); $aggregatedItemsValues = array(); foreach ($invoice->getAllItems() as $invoiceItem) { if ($invoiceItem->getOrderItem()->getParentItemId()) { continue; } $id = $invoiceItem->getOrderItem()->getQuoteItemId(); if (isset($result['items'][$id])) { $invoiceItem->setImportDutyTax($result['items'][$id]['total']); $invoiceItem->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['total'])); $invoiceItem->setImportDuty($result['items'][$id]['duty']); $invoiceItem->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['duty'])); $invoiceItem->setSalesTax($result['items'][$id]['sales_tax']); $invoiceItem->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['sales_tax'])); } else { foreach ($result['aggregated_items'] as $key => $_items) { if (in_array($id, $_items['items'])) { $aggregatedItemsValues[$key][$id] = $invoiceItem->getRowTotal(); } } } } $totals = array(); $totalDuty = array(); $totalSalesTaxes = array(); foreach ($aggregatedItemsValues as $key => $aggregatedItemsValue) { $aggregatedTotal = $result['aggregated_items'][$key]['aggregated_total']; $aggregatedDuty = $result['aggregated_items'][$key]['aggregated_duty']; $aggregatedSalesTax = $result['aggregated_items'][$key]['aggregated_sales_tax']; $totalAggregatedItemsValue = array_sum($aggregatedItemsValue); foreach ($aggregatedItemsValue as $itemId => $value) { $totals[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedTotal, 2); $totalDuty[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedDuty, 2); $totalSalesTaxes[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedSalesTax, 2); } } foreach ($invoice->getAllItems() as $invoiceItem) { if ($invoiceItem->getOrderItem()->getParentItemId()) { continue; } $id = $invoiceItem->getOrderItem()->getQuoteItemId(); if (isset($taxes[$id])) { $invoiceItem->setImportDutyTax($totals[$id]); $invoiceItem->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $totals[$id])); $invoiceItem->setImportDuty($totalDuty[$id]); $invoiceItem->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $totalDuty[$id])); $invoiceItem->setSalesTax($totalSalesTaxes[$id]); $invoiceItem->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $totalSalesTaxes[$id])); } if ($invoiceItem->getQty() == 0) { $invoiceItem->setImportDutyTax(0); $invoiceItem->setBaseImportDutyTax(0); $invoiceItem->setImportDuty(0); $invoiceItem->setBaseImportDuty(0); $invoiceItem->setSalesTax(0); $invoiceItem->setBaseSalesTax(0); } } } } } return $this; }
/** * Adds giftwrap printed card cost to request as item * * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object * @param bool $credit * @return int|bool */ protected function _addGwPrintedCardAmount($object, $credit = false) { if (!$object->getGwPrintedCardBasePrice()) { return false; } $lineNumber = $this->_getNewLineCode(); $storeId = $object->getStore()->getId(); $amount = $object->getGwPrintedCardBasePrice(); $line = $this->_getNewDocumentRequestLineObject(); if ($this->_getTaxDataHelper()->priceIncludesTax($storeId)) { $amount += $object->getGwCardBaseTaxAmount(); $line->setTaxIncluded('true'); } //@startSkipCommitHooks $amount = $credit ? -1 * $amount : $amount; //@finishSkipCommitHooks $line->setLineCode($lineNumber); $gwPrintedCardSku = $this->_getConfigHelper()->getGwPrintedCardSku($storeId); $line->setItemCode($gwPrintedCardSku ? $gwPrintedCardSku : self::DEFAULT_GW_PRINTED_CARD_SKU); $line->setItemDescription(self::DEFAULT_GW_PRINTED_CARD_DESCRIPTION); $line->setAvalaraGoodsAndServicesType($this->_getGiftTaxClassCode($storeId)); $line->setNumberOfItems(1); $line->setlineAmount($amount); $line->setDiscounted('false'); $this->_lineToItemId[$lineNumber] = $gwPrintedCardSku; $this->_lines[$lineNumber] = $line; $this->_setLinesToRequest(); return $lineNumber; }
/** * Adds giftwrap printed card cost to request as item * * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object * @param bool $credit * @return int|bool */ protected function _addGwPrintedCardAmount($object, $credit = false) { if (!$object->getGwPrintedCardBasePrice()) { return false; } $lineNumber = count($this->_lines); $storeId = $object->getStore()->getId(); $amount = $object->getGwPrintedCardBasePrice(); $line = new Line(); if ($this->_getTaxDataHelper()->priceIncludesTax($storeId)) { $amount += $object->getGwCardBaseTaxAmount(); $line->setTaxIncluded(true); } if ($credit) { //@startSkipCommitHooks $amount *= -1; //@finishSkipCommitHooks } $line->setNo($lineNumber); $line->setItemCode($this->_getConfigHelper()->getGwPrintedCardSku($storeId)); $line->setDescription('Gift Wrap Printed Card Amount'); $line->setTaxCode($this->_getGiftTaxClassCode($storeId)); $line->setQty(1); $line->setAmount($amount); $line->setDiscounted(false); $this->_lineToItemId[$lineNumber] = $this->_getConfigHelper()->getGwPrintedCardSku($storeId); $this->_lines[$lineNumber] = $line; $this->_request->setLines($this->_lines); return $lineNumber; }
/** * Collect invoice tax amount * * @param Mage_Sales_Model_Order_Invoice $invoice * * @return Mage_Sales_Model_Order_Invoice_Total_Tax * @throws Aoe_AvaTax_Exception */ public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $store = $invoice->getStore(); if (!$this->getHelper()->isActive($store)) { return parent::collect($invoice); } $invoice->setTaxAmount(0.0); $invoice->setBaseTaxAmount(0.0); $invoice->setHiddenTaxAmount(0.0); $invoice->setBaseHiddenTaxAmount(0.0); $items = $this->getHelper()->getActionableInvoiceItems($invoice); // Get taxes via API call $api = $this->getHelper()->getApi($store); $result = $api->callGetTaxForInvoice($invoice, false); if ($result['ResultCode'] !== 'Success') { throw new Aoe_AvaTax_Exception($result['ResultCode'], $result['Messages']); } $totalTax = 0; $baseTotalTax = 0; /** @var Mage_Tax_Model_Config $taxConfig */ $taxConfig = Mage::getSingleton('tax/config'); $hasDisplayCurrency = $invoice->getBaseCurrencyCode() !== $invoice->getOrderCurrencyCode(); $exchangeRate = $hasDisplayCurrency ? $invoice->getBaseToOrderRate() : 1.0; $shippingPriceIncludesTax = $taxConfig->shippingPriceIncludesTax($store); $itemPriceIncludesTax = $taxConfig->priceIncludesTax($store); foreach ($result['TaxLines'] as $line) { $itemId = $line['LineNo']; $chargeTax = $store->roundPrice(floatval($line['Tax'])); switch ($itemId) { case 'SHIPPING': // Store the tax amount $invoice->setBaseShippingTaxAmount($chargeTax); $invoice->setShippingTaxAmount($store->roundPrice($chargeTax * $exchangeRate)); // Update shipping totals if ($shippingPriceIncludesTax) { $invoice->setBaseShippingAmount($invoice->getBaseShippingInclTax() - $invoice->getBaseShippingTaxAmount()); $invoice->setShippingAmount($invoice->getShippingInclTax() - $invoice->getShippingTaxAmount()); } else { $invoice->setBaseShippingInclTax($invoice->getBaseShippingAmount() + $invoice->getBaseShippingTaxAmount()); $invoice->setShippingInclTax($invoice->getShippingAmount() + $invoice->getShippingTaxAmount()); } // Add shipping tax to total $baseTotalTax += $invoice->getBaseShippingTaxAmount(); $totalTax += $invoice->getShippingTaxAmount(); break; default: /** @var Mage_Sales_Model_Order_Invoice_Item $item */ $item = isset($items[$itemId]) ? $items[$itemId] : false; if (!$item) { continue; } // Store the tax amount $item->setBaseTaxAmount($chargeTax); $item->setTaxAmount($store->roundPrice($chargeTax * $exchangeRate)); if ($itemPriceIncludesTax) { $item->setBaseRowTotal($item->getBaseRowTotalInclTax() - $item->getBaseTaxAmount()); $item->setRowTotal($item->getRowTotalInclTax() - $item->getTaxAmount()); } else { $item->setBaseRowTotalInclTax($item->getBaseRowTotal() + $item->getBaseTaxAmount()); $item->setRowTotalInclTax($item->getRowTotal() + $item->getTaxAmount()); } // Add item tax to tax total $baseTotalTax += $item->getBaseTaxAmount(); $totalTax += $item->getTaxAmount(); } } $invoice->setTaxAmount($totalTax); $invoice->setBaseTaxAmount($baseTotalTax); $invoice->setHiddenTaxAmount(0.0); $invoice->setBaseHiddenTaxAmount(0.0); $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax); if ($invoice->getCommitTaxDocuments()) { $invoice->setAvataxDocument($result['DocCode']); } }
/** * Collect invoice tax amount * * @param Mage_Sales_Model_Order_Invoice $invoice */ public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $totalTax = 0; $baseTotalTax = 0; $totalHiddenTax = 0; $baseTotalHiddenTax = 0; $order = $invoice->getOrder(); foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItem->getTaxAmount() && $orderItemQty) { if ($item->getOrderItem()->isDummy()) { continue; } /** * Resolve rounding problems */ if ($item->isLast()) { $tax = $orderItem->getTaxAmount() - $orderItem->getTaxInvoiced(); $baseTax = $orderItem->getBaseTaxAmount() - $orderItem->getBaseTaxInvoiced(); $hiddenTax = $orderItem->getHiddenTaxAmount() - $orderItem->getHiddenTaxInvoiced(); $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() - $orderItem->getBaseHiddenTaxInvoiced(); } else { $tax = $orderItem->getTaxAmount() * $item->getQty() / $orderItemQty; $baseTax = $orderItem->getBaseTaxAmount() * $item->getQty() / $orderItemQty; $hiddenTax = $orderItem->getHiddenTaxAmount() * $item->getQty() / $orderItemQty; $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() * $item->getQty() / $orderItemQty; $tax = $invoice->getStore()->roundPrice($tax); $baseTax = $invoice->getStore()->roundPrice($baseTax); $hiddenTax = $invoice->getStore()->roundPrice($hiddenTax); $baseHiddenTax = $invoice->getStore()->roundPrice($baseHiddenTax); } $item->setTaxAmount($tax); $item->setBaseTaxAmount($baseTax); $item->setHiddenTaxAmount($hiddenTax); $item->setBaseHiddenTaxAmount($baseHiddenTax); $totalTax += $tax; $baseTotalTax += $baseTax; $totalHiddenTax += $hiddenTax; $baseTotalHiddenTax += $baseHiddenTax; } } if ($this->_canIncludeShipping($invoice)) { $totalTax += $order->getShippingTaxAmount(); $baseTotalTax += $order->getBaseShippingTaxAmount(); $totalHiddenTax += $order->getShippingHiddenTaxAmount(); $baseTotalHiddenTax += $order->getBaseShippingHiddenTaxAmount(); $invoice->setShippingTaxAmount($order->getShippingTaxAmount()); $invoice->setBaseShippingTaxAmount($order->getBaseShippingTaxAmount()); $invoice->setShippingHiddenTaxAmount($order->getShippingHiddenTaxAmount()); $invoice->setBaseShippingHiddenTaxAmount($order->getBaseShippingHiddenTaxAmount()); } $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced(); $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced(); $allowedHiddenTax = $order->getHiddenTaxAmount() + $order->getShippingHiddenTaxAmount() - $order->getHiddenTaxInvoiced() - $order->getShippingHiddenTaxInvoiced(); $allowedBaseHiddenTax = $order->getBaseHiddenTaxAmount() + $order->getBaseShippingHiddenTaxAmount() - $order->getBaseHiddenTaxInvoiced() - $order->getBaseShippingHiddenTaxInvoiced(); if ($invoice->isLast()) { $totalTax = $allowedTax; $baseTotalTax = $allowedBaseTax; $totalHiddenTax = $allowedHiddenTax; $baseTotalHiddenTax = $allowedBaseHiddenTax; } else { $totalTax = min($allowedTax, $totalTax); $baseTotalTax = min($allowedBaseTax, $baseTotalTax); $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax); $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax); } $invoice->setTaxAmount($totalTax); $invoice->setBaseTaxAmount($baseTotalTax); $invoice->setHiddenTaxAmount($totalHiddenTax); $invoice->setBaseHiddenTaxAmount($baseTotalHiddenTax); $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax + $totalHiddenTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax); return $this; }
public function getInvoiceDocCode(Mage_Sales_Model_Order_Invoice $invoice) { $prefix = trim($this->getConfig('invoice_prefix', $invoice->getStore()), self::DOC_CODE_SEPARATOR); $prefix = (empty($prefix) ? 'I' : $prefix) . self::DOC_CODE_SEPARATOR; if ($invoice->getAvataxDocument()) { return $invoice->getAvataxDocument(); } elseif ($invoice->getIncrementId()) { return $prefix . $invoice->getIncrementId(); } else { return null; } }
public function callDeleteTaxForInvoice(Mage_Sales_Model_Order_Invoice $invoice) { $request = new AvaTax\CancelTaxRequest(); $request->setCompanyCode($this->limit($this->getHelper()->getConfig('company_code', $invoice->getStore()), 25)); $request->setDocType(AvaTax\DocumentType::$SalesInvoice); $request->setDocCode($this->limit($this->getHelper()->getInvoiceDocCode($invoice), 50)); $request->setCancelCode(AvaTax\CancelCode::$DocDeleted); Mage::dispatchEvent('aoe_avatax_soapapi_delete_tax_for_invoice_before', array('request' => $request, 'invoice' => $invoice)); return $this->callCancelTax($invoice->getStore(), $request); }
protected function createTaxRequestFromInvoice(Mage_Sales_Model_Order_Invoice $invoice, $commit = false) { $order = $invoice->getOrder(); $store = $invoice->getStore(); /** @var Mage_Customer_Model_Customer $customer */ $customer = Mage::getModel('customer/customer')->load($order->getCustomerId()); $hideDiscountAmount = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT, $store); $request = array('Client' => 'Aoe_AvaTax', 'CompanyCode' => $this->limit($this->getHelper()->getConfig('company_code', $store), 25), 'DocType' => $commit ? 'SalesInvoice' : 'SalesOrder', 'DocCode' => $this->limit($this->getHelper()->getInvoiceDocCode($invoice), 50), 'ReferenceCode' => $this->getHelper()->getOrderDocCode($order), 'Commit' => $commit, 'DetailLevel' => 'Tax', 'DocDate' => $invoice->getCreatedAtDate()->toString('yyyy-MM-dd'), 'CustomerCode' => $this->getHelper()->getCustomerDocCode($customer) ?: $this->getHelper()->getOrderDocCode($order), 'CurrencyCode' => $this->limit($invoice->getBaseCurrencyCode(), 3), 'Discount' => $hideDiscountAmount ? 0.0 : $store->roundPrice($invoice->getBaseDiscountAmount()), 'Addresses' => array(), 'Lines' => array()); if ($order->getCustomerTaxvat()) { $request['BusinessIdentificationNo'] = $this->limit($order->getCustomerTaxvat(), 25); } $request['Addresses'][] = $this->getOriginAddress('ORIGIN', $store); $request['Addresses'][] = $this->getAddress('DESTINATION', $order->getShippingAddress()); $itemPriceIncludesTax = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $store); foreach ($this->getHelper()->getActionableInvoiceItems($invoice) as $k => $item) { /** @var Mage_Sales_Model_Order_Invoice_Item $item */ $request['Lines'][] = array("LineNo" => $this->limit($k, 50), "ItemCode" => $this->limit($item->getSku(), 50), "Qty" => round(floatval($item->getQty()), 4), "Amount" => $store->roundPrice($itemPriceIncludesTax ? $item->getBaseRowTotalInclTax() : $item->getBaseRowTotal()) - $store->roundPrice($item->getBaseDiscountAmount()), "OriginCode" => "ORIGIN", "DestinationCode" => "DESTINATION", "Description" => $this->limit($item->getName(), 255), "TaxCode" => $this->limit($this->getHelper()->getProductTaxCode($item->getOrderItem()->getProduct()), 25), "Discounted" => $item->getBaseDiscountAmount() > 0.0, "TaxIncluded" => $itemPriceIncludesTax, "Ref1" => $this->limit($this->getHelper()->getInvoiceItemRef1($item, $store), 250), "Ref2" => $this->limit($this->getHelper()->getInvoiceItemRef2($item, $store), 250)); } $shippingPriceIncludesTax = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_INCLUDES_TAX, $store); $request['Lines'][] = array("LineNo" => "SHIPPING", "ItemCode" => "SHIPPING", "Qty" => "1", "Amount" => $store->roundPrice($shippingPriceIncludesTax ? $invoice->getBaseShippingInclTax() : $invoice->getBaseShippingAmount()) - $store->roundPrice($invoice->getBaseShippingDiscountAmount()), "OriginCode" => "ORIGIN", "DestinationCode" => "DESTINATION", "Description" => $this->limit("Shipping: " . $order->getShippingMethod(), 255), "TaxCode" => $this->limit($this->getHelper()->getShippingTaxCode($store), 25), "Discounted" => $invoice->getBaseShippingDiscountAmount() > 0.0, "TaxIncluded" => $shippingPriceIncludesTax, "Ref1" => $this->limit($order->getShippingMethod(), 250)); // TODO: Handle giftwrapping return $request; }