/** * Create or add taxes for each collection item * * @param Tax[] $arrTaxes * @param IsotopeProductCollection $objCollection * @param ProductCollectionSurcharge[] $arrSurcharges * @param Address[] $arrAddresses */ private static function addTaxesForItems(array &$arrTaxes, IsotopeProductCollection $objCollection, array $arrSurcharges, array $arrAddresses) { foreach ($objCollection->getItems() as $objItem) { // This should never happen, but we can't calculate it if (!$objItem->hasProduct()) { continue; } $objProduct = $objItem->getProduct(); /** @var \Isotope\Model\TaxClass $objTaxClass */ $objTaxClass = $objProduct->getPrice() ? $objProduct->getPrice()->getRelated('tax_class') : null; // Skip products without tax class if (null === $objTaxClass) { continue; } $arrTaxIds = array(); $fltPrice = $objItem->getTotalPrice(); /** @var \Isotope\Model\ProductCollectionSurcharge $objSurcharge */ foreach ($arrSurcharges as $objSurcharge) { $fltPrice += $objSurcharge->getAmountForCollectionItem($objItem); } /** @var \Isotope\Model\TaxRate $objIncludes */ if (($objIncludes = $objTaxClass->getRelated('includes')) !== null) { if ($objIncludes->isApplicable($fltPrice, $arrAddresses)) { $addToTotal = static::getTaxAddState(false); $total = $addToTotal ? $objIncludes->calculateAmountAddedToPrice($fltPrice) : $objIncludes->calculateAmountIncludedInPrice($fltPrice); $arrTaxIds[] = static::addTax($arrTaxes, $objTaxClass->id . '_' . $objIncludes->id, $objTaxClass->getLabel() ?: $objIncludes->getLabel(), $objIncludes->getAmount(), $objIncludes->isPercentage(), $total, $objTaxClass->applyRoundingIncrement, $addToTotal, false); } } if (($objRates = $objTaxClass->getRelated('rates')) !== null) { /** @var \Isotope\Model\TaxRate $objTaxRate */ foreach ($objRates as $objTaxRate) { if ($objTaxRate->isApplicable($fltPrice, $arrAddresses)) { $addToTotal = static::getTaxAddState(true); $total = $addToTotal ? $objTaxRate->calculateAmountAddedToPrice($fltPrice) : $objTaxRate->calculateAmountIncludedInPrice($fltPrice); $arrTaxIds[] = static::addTax($arrTaxes, $objTaxRate->id, $objTaxRate->getLabel(), $objTaxRate->getAmount(), $objTaxRate->isPercentage(), $total, $objTaxClass->applyRoundingIncrement, $addToTotal, false); if ($objTaxRate->stop) { break; } } } } $strTaxId = implode(',', $arrTaxIds); if ($objItem->tax_id != $strTaxId) { $objCollection->updateItem($objItem, array('tax_id' => $strTaxId)); } foreach ($arrSurcharges as $objSurcharge) { if ($objSurcharge->getAmountForCollectionItem($objItem) > 0) { foreach ($arrTaxIds as $taxId) { $objSurcharge->addTaxNumber($taxId); } } } } }