Skip to content

Commit

Permalink
Now identify Invoice Tax Rate from Database (No more Rounding)
Browse files Browse the repository at this point in the history
  • Loading branch information
BadPixxel committed Feb 23, 2018
1 parent 2b78615 commit 76bc781
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion modules/splashsync/splashsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
24 changes: 24 additions & 0 deletions modules/splashsync/src/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
//====================================================================//
Expand Down
10 changes: 8 additions & 2 deletions modules/splashsync/src/Objects/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 76bc781

Please sign in to comment.