/**
  * Total all taxes associated with the current order.
  *
  * @return float
  */
 protected function totalTaxAmount()
 {
     $records = array_reduce($this->taxCollector->getTaxRecords(), function ($total, $item) {
         return $total + $item->getCalculatedTax();
     }, 0.0);
     $duties = array_reduce($this->taxCollector->getTaxDuties(), function ($total, $item) {
         return $total + $item->getAmount();
     }, 0.0);
     $fees = array_reduce($this->taxCollector->getTaxFees(), function ($total, $item) {
         return $total + $item->getAmount();
     }, 0.0);
     return $records + $duties + $fees;
 }
 /**
  * set tax data on the orderitem payload for the order create request
  * @param  Varien_Event_Observer
  * @return self
  */
 public function handleOrderCreateItemEvent(Varien_Event_Observer $observer)
 {
     $event = $observer->getEvent();
     $item = $event->getItem();
     $quoteItemId = $item->getQuoteItemId();
     Mage::getModel('ebayenterprise_tax/order_create_orderitem', ['item' => $event->getItem(), 'tax_records' => $this->taxCollector->getTaxRecordsByItemId($quoteItemId), 'duty' => $this->taxCollector->getTaxDutyByItemId($quoteItemId), 'fees' => $this->taxCollector->getTaxFeesByItemId($quoteItemId), 'order_item_payload' => $event->getItemPayload()])->addTaxesToPayload();
     return $this;
 }
 /**
  * Check if there are any errors in the taxes.
  * @param  Mage_Sales_Model_Order_Address
  * @return bool
  */
 protected function _itemsHaveErrors()
 {
     foreach ($this->_taxCollector->getTaxDuties() as $duty) {
         if ($duty->getCalculationError()) {
             return true;
         }
     }
     return false;
 }
 /**
  * Get all tax records for this address that should were applied to
  * address level gifting prices.
  *
  * @return EbayEnterprise_Tax_Model_Record[]
  */
 protected function _getAddressGiftTaxRecords()
 {
     $addressId = $this->_address->getQuoteAddressId();
     // Filter down the list of tax records to only those with a matching
     // quote address id, with a source indicating they apply to address level
     // gifting, and do not indicate a calculation error for the record.
     return array_filter($this->_taxCollector->getTaxRecords(), function ($record) use($addressId) {
         return $record->getAddressId() === $addressId && $record->getTaxSource() === EbayEnterprise_Tax_Model_Record::SOURCE_ADDRESS_GIFTING;
     });
 }
 /**
  * When none of the collected tax records apply to the ship group,
  * no taxes should be added to the tax container.
  */
 public function testAddGiftTaxesToPayloadNoTaxesToAdd()
 {
     // Set the tax collector to have no relevent taxes to add to the ship group.
     $this->_taxCollector->expects($this->any())->method('getTaxRecords')->will($this->returnValue([$this->_itemGiftTax, $this->_merchGiftTax]));
     // Ship group can already have a tax container.
     $this->_shipGroup->expects($this->any())->method('getGiftPricing')->will($this->returnValue($this->_taxContainer));
     // Side-effect tests: There should be no tax records for the ship
     // group so no tax payloads should be added to the iterable.
     $this->_taxIterable->expects($this->never())->method('offsetSet');
     $this->_shipGroupHandler->addGiftTaxesToPayload();
 }
 /**
  * When the tax collector fails to collect new tax data, the session should
  * not get updated and quote totals should not be recollected.
  */
 public function testHandleSalesQuoteCollectTotalsCollectFailure()
 {
     // Indicate new tax collection is required in the session flags.
     $this->coreSession->expects($this->any())->method('isTaxUpdateRequired')->will($this->returnValue(true));
     // Set the tax collector to fail to make the TDF request and throw
     // an exception.
     $this->taxCollector->expects($this->once())->method('collectTaxes')->will($this->throwException(Mage::exception('EbayEnterprise_Tax_Exception_Collector')));
     // Ensure session quote data is not updated, flags are not reset and
     // quote totals are not re-collected.
     $this->coreSession->expects($this->never())->method('updateWithQuote');
     $this->coreSession->expects($this->never())->method('resetTaxUpdateRequired');
     $this->quote->expects($this->never())->method('collectTotals');
     $this->taxObserver->handleSalesQuoteCollectTotalsAfter($this->eventObserver);
 }
 /**
  * Test collected tax totals for an address. Should gather tax records from
  * the tax collector and set relevent fields on the address.
  */
 public function testCollect()
 {
     // Setup tax collector to return some tax records.
     $this->_taxCollector->expects($this->any())->method('getTaxRecordsByAddressId')->with($this->identicalTo($this->_addressId))->will($this->returnValue($this->_taxRecords));
     $this->_taxCollector->expects($this->any())->method('getTaxDutiesByAddressId')->with($this->identicalTo($this->_addressId))->will($this->returnValue($this->_duties));
     $this->_taxCollector->expects($this->any())->method('getTaxFeesByAddressId')->with($this->identicalTo($this->_addressId))->will($this->returnValue($this->_fees));
     // Side-effect test - address should have total amount set for the
     // tax total.
     $this->_address->expects($this->once())->method('setTotalAmount')->with($this->identicalTo($this->_taxTotal->getCode()), $this->identicalTo($this->_totalTaxAmount))->wilL($this->returnSelf());
     // Side-effect test - address should have base total amount set for the
     // tax total.
     $this->_address->expects($this->once())->method('setBaseTotalAmount')->with($this->identicalTo($this->_taxTotal->getCode()), $this->identicalTo($this->_totalTaxAmount))->wilL($this->returnSelf());
     $this->_taxTotal->collect($this->_address);
 }
 /**
  * When the quote is invalid for making a tax request, no request should
  * be attempted and existing tax records should be cleared - reset
  * to empty arrays.
  *
  * By default, the quote used in the tests will be considered to be invalid
  * for tax requests - no item count as well as missing address data.
  */
 public function testCollectTaxesFailedInvalidQuote()
 {
     // Simulate the TDK request returning a set of tax records.
     $this->_taxHelper->expects($this->never())->method('requestTaxesForQuote');
     // Side-effect test - ensure all tax records in the session storage are
     // emptied of existing tax records when a tax request fails to be made.
     $this->_taxSession->expects($this->once())->method('setTaxRecords')->with($this->identicalTo([]))->will($this->returnSelf());
     $this->_taxSession->expects($this->once())->method('setTaxDuties')->with($this->identicalTo([]))->will($this->returnSelf());
     $this->_taxSession->expects($this->once())->method('setTaxFees')->with($this->identicalTo([]))->will($this->returnSelf());
     // Side-effect test - ensure that when a tax request was made successfully,
     // that a flag indicating that is set in the session.
     $this->_taxSession->expects($this->once())->method('setTaxRequestSuccess')->with($this->identicalTo(false))->will($this->returnSelf());
     $this->setExpectedException('EbayEnterprise_Tax_Exception_Collector_InvalidQuote_Exception');
     $this->_taxCollector->collectTaxes($this->_quote);
 }
 /**
  * Update the address totals with tax amounts.
  *
  * @param Mage_Sales_Model_Quote_Address
  * @return self
  */
 public function collect(Mage_Sales_Model_Quote_Address $address)
 {
     // Necessary for inherited `self::_setAmount` and `self::_setBaseAmount` to behave.
     $this->_setAddress($address);
     $addressId = $address->getId();
     $taxTotal = $this->_totalTaxRecordsCalculatedTaxes($this->_taxCollector->getTaxRecordsByAddressId($addressId));
     $dutyTotal = $this->_totalDuties($this->_taxCollector->getTaxDutiesByAddressId($addressId));
     $feeTotal = $this->_totalFees($this->_taxCollector->getTaxFeesByAddressId($addressId));
     $total = $taxTotal + $dutyTotal + $feeTotal;
     $this->_logger->debug("Collected tax totals of: tax - {$taxTotal}, duty - {$dutyTotal}, fee - {$feeTotal}, total - {$total}.", $this->_logContext->getMetaData(__CLASS__));
     // Always overwrite amounts for this total. The total calculated from
     // the collector's tax records will be the complete tax amount for
     // the address.
     $this->_setAmount($total)->_setBaseAmount($total);
     return $this;
 }