/** * Prepare FIS params * @param Order * @return array */ private function prepareFISParams($objOrder) { $objBillingAddress = $objOrder->getBillingAddress(); $objShippingAddress = $objOrder->getShippingAddress(); $arrInvoice = array('ECOM_BILLTO_POSTAL_NAME_FIRST' => substr($objBillingAddress->firstname, 0, 50), 'ECOM_BILLTO_POSTAL_NAME_LAST' => substr($objBillingAddress->lastname, 0, 50), 'ECOM_SHIPTO_POSTAL_STREET_LINE1' => $objShippingAddress->street_1, 'ECOM_SHIPTO_POSTAL_POSTALCODE' => $objShippingAddress->postal, 'ECOM_SHIPTO_POSTAL_CITY' => $objShippingAddress->city, 'ECOM_SHIPTO_POSTAL_COUNTRYCODE' => strtoupper($objShippingAddress->country), 'ECOM_SHIPTO_DOB' => date('d/m/Y', $objShippingAddress->dateOfBirth), 'REF_CUSTOMERID' => substr('psp_' . $this->id . '_' . $objOrder->id . '_' . $objOrder->uniqid, 0, 17), 'ECOM_CONSUMER_GENDER' => $objBillingAddress->gender == 'male' ? 'M' : 'F'); $arrOrder = array(); $i = 1; // Need to take the items from the cart as they're not transferred to the order here yet foreach (Isotope::getCart()->getItems() as $objItem) { $objPrice = $objItem->getProduct()->getPrice(); $fltVat = Isotope::roundPrice(100 / $objPrice->getNetAmount() * $objPrice->getGrossAmount() - 100, false); $arrOrder['ITEMID' . $i] = $objItem->id; $arrOrder['ITEMNAME' . $i] = substr($objItem->getName(), 40); $arrOrder['ITEMPRICE' . $i] = $objPrice->getNetAmount(); $arrOrder['ITEMQUANT' . $i] = $objItem->quantity; $arrOrder['ITEMVATCODE' . $i] = $fltVat . '%'; $arrOrder['ITEMVAT' . $i] = Isotope::roundPrice($objPrice->getGrossAmount() - $objPrice->getNetAmount(), false); $arrOrder['FACEXCL' . $i] = $objPrice->getNetAmount(); $arrOrder['FACTOTAL' . $i] = $objPrice->getGrossAmount(); ++$i; } return array_merge($arrInvoice, $arrOrder); }
/** * Sum tax free total of items and surcharges * @return float */ public function getTaxFreeTotal() { if ($this->isLocked()) { return $this->tax_free_total; } if (!isset($this->arrCache['taxFreeTotal'])) { $fltAmount = $this->getTaxFreeSubtotal(); $arrSurcharges = $this->getSurcharges(); foreach ($arrSurcharges as $objSurcharge) { if ($objSurcharge->addToTotal) { $fltAmount += $objSurcharge->tax_free_total_price; } } $this->arrCache['taxFreeTotal'] = $fltAmount > 0 ? Isotope::roundPrice($fltAmount) : 0; } return $this->arrCache['taxFreeTotal']; }
/** * Generate surhcharges for a collection * @param IsotopeProductCollection * @return array */ public static function findForCollection(IsotopeProductCollection $objCollection) { $arrPreTax = array(); $arrPostTax = array(); // !HOOK: get collection surcharges if (isset($GLOBALS['ISO_HOOKS']['findSurchargesForCollection']) && is_array($GLOBALS['ISO_HOOKS']['findSurchargesForCollection'])) { foreach ($GLOBALS['ISO_HOOKS']['findSurchargesForCollection'] as $callback) { $objCallback = \System::importStatic($callback[0]); $arrResult = $objCallback->{$callback[1]}($objCollection); foreach ($arrResult as $objSurcharge) { if (!$objSurcharge instanceof IsotopeProductCollectionSurcharge || $objSurcharge instanceof Tax) { throw new \InvalidArgumentException('Instance of ' . get_class($objSurcharge) . ' is not a valid product collection surcharge.'); } if ($objSurcharge->hasTax()) { $arrPreTax[] = $objSurcharge; } else { $arrPostTax[] = $objSurcharge; } } } } /** @var \Isotope\Model\ProductCollection\Order $objCollection */ /** @var Tax[] $arrTaxes */ $arrTaxes = array(); $arrAddresses = array('billing' => $objCollection->getBillingAddress(), 'shipping' => $objCollection->getShippingAddress()); 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 ($arrPreTax as $objSurcharge) { $fltPrice += $objSurcharge->getAmountForCollectionItem($objItem); } /** @var \Isotope\Model\TaxRate $objIncludes */ if (($objIncludes = $objTaxClass->getRelated('includes')) !== null) { if ($objIncludes->isApplicable($fltPrice, $arrAddresses)) { $fltTax = $objIncludes->calculateAmountIncludedInPrice($fltPrice); if (!isset($arrTaxes[$objTaxClass->id . '_' . $objIncludes->id])) { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id] = new Tax(); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->label = $objTaxClass->getLabel() ?: $objIncludes->getLabel(); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->price = $objIncludes->getAmount() . ($objIncludes->isPercentage() ? '%' : ''); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price = Isotope::roundPrice($fltTax, $objTaxClass->applyRoundingIncrement); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->addToTotal = false; } else { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price = Isotope::roundPrice($arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price + $fltTax, $objTaxClass->applyRoundingIncrement); if (is_numeric($arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->price) && is_numeric($objIncludes->getAmount())) { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->price += $objIncludes->getAmount(); } } $taxId = array_search($objTaxClass->id . '_' . $objIncludes->id, array_keys($arrTaxes)) + 1; $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->addTaxNumber($taxId); $arrTaxIds[] = $taxId; } } if (($objRates = $objTaxClass->getRelated('rates')) !== null) { /** @var \Isotope\Model\TaxRate $objTaxRate */ foreach ($objRates as $objTaxRate) { if ($objTaxRate->isApplicable($fltPrice, $arrAddresses)) { $fltTax = $objTaxRate->calculateAmountAddedToPrice($fltPrice); if (!isset($arrTaxes[$objTaxRate->id])) { $arrTaxes[$objTaxRate->id] = new Tax(); $arrTaxes[$objTaxRate->id]->label = $objTaxRate->getLabel(); $arrTaxes[$objTaxRate->id]->price = $objTaxRate->getAmount() . ($objTaxRate->isPercentage() ? '%' : ''); $arrTaxes[$objTaxRate->id]->total_price = Isotope::roundPrice($fltTax, $objTaxClass->applyRoundingIncrement); $arrTaxes[$objTaxRate->id]->addToTotal = true; } else { $arrTaxes[$objTaxRate->id]->total_price = Isotope::roundPrice($arrTaxes[$objTaxRate->id]->total_price + $fltTax, $objTaxClass->applyRoundingIncrement); if (is_numeric($arrTaxes[$objTaxRate->id]->price) && is_numeric($objTaxRate->getAmount())) { $arrTaxes[$objTaxRate->id]->price += $objTaxRate->getAmount(); } } $taxId = array_search($objTaxRate->id, array_keys($arrTaxes)) + 1; $arrTaxes[$objTaxRate->id]->addTaxNumber($taxId); $arrTaxIds[] = $taxId; if ($objTaxRate->stop) { break; } } } } $strTaxId = implode(',', $arrTaxIds); if ($objItem->tax_id != $strTaxId) { $objCollection->updateItem($objItem, array('tax_id' => $strTaxId)); } foreach ($arrPreTax as $objSurcharge) { if ($objSurcharge->getAmountForCollectionItem($objItem) > 0) { foreach ($arrTaxIds as $taxId) { $objSurcharge->addTaxNumber($taxId); } } } } /** @var \Isotope\Model\ProductCollectionSurcharge $objSurcharge */ foreach ($arrPreTax as $objSurcharge) { /** @var \Isotope\Model\TaxClass $objTaxClass */ $objTaxClass = TaxClass::findByPk($objSurcharge->tax_class); // Skip products without tax class if (null === $objTaxClass) { continue; } $fltPrice = $objSurcharge->total_price; /** @var \Isotope\Model\TaxRate $objIncludes */ if (($objIncludes = $objTaxClass->getRelated('includes')) !== null) { if ($objIncludes->isApplicable($fltPrice, $arrAddresses)) { $fltTax = $objIncludes->calculateAmountIncludedInPrice($fltPrice); if (!isset($arrTaxes[$objTaxClass->id . '_' . $objIncludes->id])) { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id] = new Tax(); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->label = $objTaxClass->getLabel() ?: $objIncludes->getLabel(); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->price = $objIncludes->getAmount() . ($objIncludes->isPercentage() ? '%' : ''); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price = Isotope::roundPrice($fltTax, $objTaxClass->applyRoundingIncrement); $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->addToTotal = false; } else { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price = Isotope::roundPrice($arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price + $fltTax, $objTaxClass->applyRoundingIncrement); if (is_numeric($arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->price) && is_numeric($objIncludes->getAmount())) { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->price += $objIncludes->getAmount(); } } if ($objTaxClass->notNegative && $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price < 0) { $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->total_price = 0; } $taxId = array_search($objTaxClass->id . '_' . $objIncludes->id, array_keys($arrTaxes)) + 1; $arrTaxes[$objTaxClass->id . '_' . $objIncludes->id]->addTaxNumber($taxId); $objSurcharge->addTaxNumber($taxId); } } if (($objRates = $objTaxClass->getRelated('rates')) !== null) { /** @var \Isotope\Model\TaxRate $objTaxRate */ foreach ($objRates as $objTaxRate) { if ($objTaxRate->isApplicable($fltPrice, $arrAddresses)) { $fltTax = $objTaxRate->calculateAmountAddedToPrice($fltPrice); if (!isset($arrTaxes[$objTaxRate->id])) { $arrTaxes[$objTaxRate->id] = new Tax(); $arrTaxes[$objTaxRate->id]->label = $objTaxRate->getLabel(); $arrTaxes[$objTaxRate->id]->price = $objTaxRate->getAmount() . ($objTaxRate->isPercentage() ? '%' : ''); $arrTaxes[$objTaxRate->id]->total_price = Isotope::roundPrice($fltTax, $objTaxClass->applyRoundingIncrement); $arrTaxes[$objTaxRate->id]->addToTotal = true; } else { $arrTaxes[$objTaxRate->id]->total_price = Isotope::roundPrice($arrTaxes[$objTaxRate->id]->total_price + $fltTax, $objTaxClass->applyRoundingIncrement); if (is_numeric($arrTaxes[$objTaxRate->id]->price) && is_numeric($objTaxRate->getAmount())) { $arrTaxes[$objTaxRate->id]->price += $objTaxRate->getAmount(); } } if ($objTaxClass->notNegative && $arrTaxes[$objTaxRate->id]->total_price < 0) { $arrTaxes[$objTaxRate->id]->total_price = 0; } $taxId = array_search($objTaxRate->id, array_keys($arrTaxes)) + 1; $arrTaxes[$objTaxRate->id]->addTaxNumber($taxId); $objSurcharge->addTaxNumber($taxId); if ($objTaxRate->stop) { break; } } } } } return array_merge($arrPreTax, $arrTaxes, $arrPostTax); }