public function getDuty()
 {
     $result = Dutycalculator_Charge_Model_Importdutytaxes::getAmount($this->getQuote());
     if (is_array($result)) {
         return $result['total'];
     }
     return 0;
 }
 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;
 }
 public function initDCRates($event)
 {
     if (!Mage::helper('dccharge')->enabledShowTax()) {
         return;
     }
     /* @var $order Mage_Sales_Model_Order */
     $order = $event->getOrder();
     /* @var $quote Mage_Sales_Model_Quote */
     $quote = $event->getQuote();
     if (!$quote->getFailedCalculation() && !$quote->getIsVirtual()) {
         $quoteItems = $quote->getAllVisibleItems();
         $params = array();
         $params['cat'] = array();
         $params['desc'] = array();
         $params['reference'] = array();
         $idx = 0;
         $additionalAttributes = explode(',', Mage::getStoreConfig('dc_charge_extension/dccharge/additional-attributes-for-documents'));
         /* @var $attributesCollection Mage_Eav_Model_Resource_Entity_Attribute_Collection */
         $attributesCollection = Mage::getModel('eav/entity_attribute')->getResourceCollection();
         $attributesCollection->setCodeFilter($additionalAttributes);
         $attributesCollection->load();
         $additionalAttributes = array();
         foreach ($attributesCollection as $additionalAttribute) {
             $additionalAttributes[$additionalAttribute->getAttributeCode()] = $additionalAttribute->getFrontendLabel();
         }
         foreach ($quoteItems as $quoteItem) {
             /* @var $quoteItem Mage_Sales_Model_Quote_Item */
             /* @var $product Mage_Catalog_Model_Product */
             $product = $quoteItem->getProduct();
             $product->load($product->getId());
             $quoteItemId = $quoteItem->getId();
             $qty = $quoteItem->getQty();
             if ($product->isVirtual() || $qty <= 0) {
                 continue;
             }
             $params['reference'][$idx] = $quoteItemId;
             if ($product->getDcProductId()) {
                 $params['cat'][$idx] = $product->getDcProductId();
             } else {
                 $params['cat'][$idx] = '';
             }
             $description = $product->getName();
             if ($additionalAttributes) {
                 $additionalDescription = '';
                 foreach ($additionalAttributes as $attrCode => $attrLabel) {
                     if ($product->getAttributeText($attrCode)) {
                         $additionalDescription .= $attrLabel . ': ' . $product->getAttributeText($attrCode) . ', ';
                     } else {
                         if ($product->getData($attrCode)) {
                             $additionalDescription .= $attrLabel . ': ' . $product->getData($attrCode) . ', ';
                         }
                     }
                 }
                 if (strlen($additionalDescription)) {
                     $description .= ' ' . rtrim($additionalDescription, ', ');
                 }
             }
             $params['desc'][$idx] = $description;
             $idx++;
         }
         $quote->save();
         $rates = Dutycalculator_Charge_Model_Importdutytaxes::getRatesDetails($quote->getShippingAddress(), $params['cat'], $params['desc'], $params['reference']);
         foreach ($quoteItems as $quoteItem) {
             if (isset($rates[$quoteItem->getId()])) {
                 $quoteItem->setSalesTaxRate($rates[$quoteItem->getId()]['sales_tax_rate']);
                 $quoteItem->setImportDutyRate($rates[$quoteItem->getId()]['duty_rate']);
             }
         }
         $quote->save();
         $orderItems = $order->getAllVisibleItems();
         foreach ($orderItems as $orderItem) {
             /* @var $orderItem Mage_Sales_Model_Order_Item */
             if (isset($rates[$orderItem->getQuoteItemId()])) {
                 $orderItem->setSalesTaxRate($rates[$orderItem->getQuoteItemId()]['sales_tax_rate']);
                 $orderItem->setImportDutyRate($rates[$orderItem->getQuoteItemId()]['duty_rate']);
             }
         }
     }
 }
 public function fetch(Mage_Sales_Model_Quote_Address $address)
 {
     if ($address->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) {
         if (Dutycalculator_Charge_Model_Importdutytaxes::canApply($address)) {
             $amt = $address->getImportDutyTax();
             $title = $address->getQuote()->getDeliveryDutyType() == Dutycalculator_Charge_Helper_Data::DC_DELIVERY_TYPE_DDU ? $address->getQuote()->getFailedCalculation() ? 'Any import duty & taxes are paid upon delivery and are not included in the final price' : 'Estimated import duty & taxes (Not included in grand total, paid upon delivery)' : 'Import duty and taxes';
             $address->addTotal(array('code' => $this->getCode(), 'title' => $title, 'value' => $amt));
         }
     }
     return $this;
 }
 public function salesOrderAddressSaveAfter($event)
 {
     /* @var $salesOrderAddress Mage_Sales_Model_Order_Address */
     $salesOrderAddress = $event->getAddress();
     try {
         $shipmentsCollection = $salesOrderAddress->getOrder()->getShipmentsCollection();
         if ($shipmentsCollection->getSize()) {
             foreach ($shipmentsCollection as $shipment) {
                 $result = Dutycalculator_Charge_Model_Importdutytaxes::generateDocuments($shipment);
                 if ($result) {
                     $shipment->setCommercialInvoiceUrl($result['commercial_invoice_url']);
                     $shipment->setPackingListUrl($result['packing_list_url']);
                 }
             }
             $shipmentsCollection->save();
         }
     } catch (Exception $ex) {
     }
     return $this;
 }