11import AbstractTaxProxy from '../abstract/tax'
2- import { calculateProductTax } from ' ../../lib/taxcalc'
2+ import { calculateProductTax , checkIfTaxWithUserGroupIsActive , getUserGroupIdToUse } from " ../../lib/taxcalc" ;
33const es = require ( 'elasticsearch' )
44const bodybuilder = require ( 'bodybuilder' )
55import TierHelper from '../../helpers/priceTiers'
@@ -11,6 +11,8 @@ class TaxProxy extends AbstractTaxProxy {
1111 this . _indexName = indexName
1212 this . _sourcePriceInclTax = sourcePriceInclTax
1313 this . _finalPriceInclTax = finalPriceInclTax
14+ this . _userGroupId = this . _config . tax . userGroupId
15+ this . _storeConfigTax = this . _config . tax
1416
1517 if ( this . _config . storeViews && this . _config . storeViews . multistore ) {
1618 for ( let storeCode in this . _config . storeViews ) {
@@ -23,6 +25,7 @@ class TaxProxy extends AbstractTaxProxy {
2325 taxCountry = store . tax . defaultCountry
2426 sourcePriceInclTax = store . tax . sourcePriceIncludesTax
2527 finalPriceInclTax = store . tax . finalPriceIncludesTax
28+ this . _storeConfigTax = store . tax
2629 break ;
2730 }
2831 }
@@ -51,8 +54,8 @@ class TaxProxy extends AbstractTaxProxy {
5154 this . taxFor = this . taxFor . bind ( this )
5255 }
5356
54- taxFor ( product ) {
55- return calculateProductTax ( product , this . _taxClasses , this . _taxCountry , this . _taxRegion , this . _sourcePriceInclTax , this . _deprecatedPriceFieldsSupport , this . _finalPriceInclTax )
57+ taxFor ( product , groupId ) {
58+ return calculateProductTax ( product , this . _taxClasses , this . _taxCountry , this . _taxRegion , this . _sourcePriceInclTax , this . _deprecatedPriceFieldsSupport , this . _finalPriceInclTax , groupId , this . _storeConfigTax )
5659 }
5760
5861 applyTierPrices ( productList , groupId ) {
@@ -82,7 +85,7 @@ class TaxProxy extends AbstractTaxProxy {
8285 if ( this . _config . elasticsearch . user ) {
8386 esConfig . httpAuth = this . _config . elasticsearch . user + ':' + this . _config . elasticsearch . password
8487 }
85-
88+
8689 const client = new es . Client ( esConfig )
8790 const esQuery = {
8891 index : this . _indexName ,
@@ -92,7 +95,14 @@ class TaxProxy extends AbstractTaxProxy {
9295 client . search ( esQuery ) . then ( function ( taxClasses ) { // we're always trying to populate cache - when online
9396 inst . _taxClasses = taxClasses . hits . hits . map ( el => { return el . _source } )
9497 for ( let item of productList ) {
95- inst . taxFor ( item . _source )
98+ const isActive = checkIfTaxWithUserGroupIsActive ( inst . _storeConfigTax )
99+ if ( isActive ) {
100+ groupId = getUserGroupIdToUse ( inst . _userGroupId , inst . _storeConfigTax )
101+ } else {
102+ groupId = null
103+ }
104+
105+ inst . taxFor ( item . _source , groupId )
96106 }
97107
98108 resolve ( productList )
0 commit comments