diff --git a/modules/splashsync/splashsync.php b/modules/splashsync/splashsync.php index 76f33ec8..605f99fb 100644 --- a/modules/splashsync/splashsync.php +++ b/modules/splashsync/splashsync.php @@ -41,7 +41,7 @@ public function __construct() // Init Module Main Information Fields $this->name = 'splashsync'; $this->tab = 'administration'; - $this->version = '1.1.0'; + $this->version = '1.1.1'; $this->author = 'SplashSync'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.7'); diff --git a/modules/splashsync/src/Local.php b/modules/splashsync/src/Local.php index d9178585..ac37cc05 100644 --- a/modules/splashsync/src/Local.php +++ b/modules/splashsync/src/Local.php @@ -569,6 +569,30 @@ public function getTaxRateGroupId($TaxRate,$CountryId=null) return False; } + /** + * @abstract Identify Best Tax Rate from Raw Computed Value + * @param float $TaxRate Product Tax Rate in Percent + * @param int $TaxRateGroupId Product Tax Rate Group Id + * @return TaxRule + */ + public function getBestTaxRateInGroup($TaxRate, $TaxRateGroupId) + { + //====================================================================// + // Get default Language Id + $LangId = Context::getContext()->language->id; + //====================================================================// + // For All Tax Rules of This Group, Search for Closest Rate + $BestRate = 0; + foreach ( \TaxRule::getTaxRulesByGroupId($LangId, $TaxRateGroupId) as $TaxRule) { + + if ( abs($TaxRate - $TaxRule["rate"]) < abs($TaxRate - $BestRate) ) { + $BestRate = $TaxRule["rate"]; + } + + } + return $BestRate; + } + //====================================================================// // Prestashop Getters & Setters //====================================================================// diff --git a/modules/splashsync/src/Objects/Invoice.php b/modules/splashsync/src/Objects/Invoice.php index 1a0cd5c5..09ed1859 100644 --- a/modules/splashsync/src/Objects/Invoice.php +++ b/modules/splashsync/src/Objects/Invoice.php @@ -908,7 +908,6 @@ private function getProductsLineFields($Key,$FieldName) // Fill List with Data foreach ($this->Products as $key => $Product) { -//Splash::Log()->www("Product", $Product); //====================================================================// // READ Fields switch ($ListFieldName) @@ -938,7 +937,14 @@ private function getProductsLineFields($Key,$FieldName) //====================================================================// // Manually Compute Tax Rate if ( $Product["unit_price_tax_excl"] != $Product["unit_price_tax_incl"] ) { - $Product["tax_rate"] = round(100 * ( ($Product["unit_price_tax_incl"] - $Product["unit_price_tax_excl"]) / $Product["unit_price_tax_excl"] ), 2); + $RawTaxRate = (100 * ( ($Product["unit_price_tax_incl"] - $Product["unit_price_tax_excl"]) / $Product["unit_price_tax_excl"] )); + } + //====================================================================// + // If Tax Rate Group is Defined => Search for Best Tax Rate + if ( !empty($Product["id_tax_rules_group"]) ) { + $Product["tax_rate"] = Splash::Local()->getBestTaxRateInGroup($RawTaxRate, $Product["id_tax_rules_group"]); + } else { + $Product["tax_rate"] = round( $RawTaxRate , 2); } //====================================================================// // Build Price Array