Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 87280b6

Browse files
authored
Merge pull request #307 from gastrohero/feature/add-usergroupid-for-product-tax-calculation
Added tax calculation with product_tax_id and user group id
2 parents 27552d0 + 271c0cd commit 87280b6

File tree

7 files changed

+85
-43
lines changed

7 files changed

+85
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Added eslint config from vue-storefront so we have the same config and in both repos typescript support - @resubaka (#320)
1313
- Added jest support - @resubaka (#321)
1414
- Added caching factory and action factory for the image endpoint. This gives the possibility to use other services for caching or image processing - @resubaka (#317, #315)
15+
- Added support for tax calculation where the values from customer_tax_class_ids is used - @resubaka (#307)
1516

1617
### Fixed
1718
- The `product.price_*` fields have been normalized with the backward compatibility support (see `config.tax.deprecatedPriceFieldsSupport` which is by default true) - @pkarw (#289)

config/default.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
"deprecatedPriceFieldsSupport": true,
7373
"calculateServerSide": true,
7474
"sourcePriceIncludesTax": false,
75-
"finalPriceIncludesTax": true
75+
"finalPriceIncludesTax": true,
76+
"userGroupId": null,
77+
"useOnlyDefaultUserGroupId": false
7678
},
7779
"i18n": {
7880
"fullCountryName": "Germany",
@@ -104,7 +106,9 @@
104106
"calculateServerSide": true,
105107
"sourcePriceIncludesTax": false,
106108
"deprecatedPriceFieldsSupport": true,
107-
"finalPriceIncludesTax": true
109+
"finalPriceIncludesTax": true,
110+
"userGroupId": null,
111+
"useOnlyDefaultUserGroupId": false
108112
},
109113
"i18n": {
110114
"fullCountryName": "Italy",
@@ -132,7 +136,9 @@
132136
"setConfigurableProductOptions": true,
133137
"sourcePriceIncludesTax": false,
134138
"deprecatedPriceFieldsSupport": true,
135-
"finalPriceIncludesTax": true
139+
"finalPriceIncludesTax": true,
140+
"userGroupId": null,
141+
"useOnlyDefaultUserGroupId": false
136142
},
137143
"bodyLimit": "100kb",
138144
"corsHeaders": [

src/api/catalog.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ export default ({config, db}) => function (req, res, body) {
6565
url = config.elasticsearch.protocol + '://' + url
6666
}
6767

68-
// Check price tiers
69-
if (config.usePriceTiers) {
70-
const userToken = requestBody.groupToken
71-
72-
// Decode token and get group id
73-
if (userToken && userToken.length > 10) {
74-
const decodeToken = jwt.decode(userToken, config.authHashSecret ? config.authHashSecret : config.objHashSecret)
75-
groupId = decodeToken.group_id || groupId
76-
}
77-
78-
delete requestBody.groupToken
68+
const userToken = requestBody.groupToken
69+
70+
// Decode token and get group id
71+
if (userToken && userToken.length > 10) {
72+
const decodeToken = jwt.decode(userToken, config.authHashSecret ? config.authHashSecret : config.objHashSecret)
73+
groupId = decodeToken.group_id || groupId
74+
} else if (requestBody.groupId) {
75+
groupId = requestBody.groupId || groupId
7976
}
8077

78+
delete requestBody.groupToken
79+
delete requestBody.groupId
80+
8181
let auth = null;
8282

8383
// Only pass auth if configured

src/api/user.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ function addUserGroupToken (config, result) {
1111
/**
1212
* Add group id to token
1313
*/
14-
if (config.usePriceTiers) {
15-
const data = {
16-
group_id: result.group_id,
17-
id: result.id,
18-
user: result.email
19-
}
20-
21-
result.groupToken = jwt.encode(data, config.authHashSecret ? config.authHashSecret : config.objHashSecret)
14+
const data = {
15+
group_id: result.group_id,
16+
id: result.id,
17+
user: result.email
2218
}
19+
20+
result.groupToken = jwt.encode(data, config.authHashSecret ? config.authHashSecret : config.objHashSecret)
2321
}
2422

2523
export default ({config, db}) => {
@@ -65,17 +63,9 @@ export default ({config, db}) => {
6563

6664
userProxy.login(req.body).then((result) => {
6765
/**
68-
* Second request for more user info
69-
*/
70-
if (config.usePriceTiers) {
71-
userProxy.me(result).then((resultMe) => {
72-
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(req.body, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
73-
}).catch(err => {
74-
apiError(res, err);
75-
})
76-
} else {
77-
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(req.body, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
78-
}
66+
* Second request for more user info
67+
*/
68+
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(req.body, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
7969
}).catch(err => {
8070
apiError(res, err);
8171
})

src/lib/taxcalc.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,19 @@ export function updateProductPrices (product, rate, sourcePriceInclTax = false,
206206
}
207207
}
208208

209-
export function calculateProductTax (product, taxClasses, taxCountry = 'PL', taxRegion = '', sourcePriceInclTax = false, deprecatedPriceFieldsSupport = false, finalPriceInclTax = true) {
209+
export function calculateProductTax (product, taxClasses, taxCountry = 'PL', taxRegion = '', sourcePriceInclTax = false, deprecatedPriceFieldsSupport = false, finalPriceInclTax = true, userGroupId = null, _storeConfigTax) {
210210
let rateFound = false
211211
if (product.tax_class_id > 0) {
212-
let taxClass = taxClasses.find((el) => el.product_tax_class_ids.indexOf(parseInt(product.tax_class_id) >= 0))
212+
let taxClass
213+
if (checkIfTaxWithUserGroupIsActive(_storeConfigTax) && typeof userGroupId === 'number') {
214+
taxClass = taxClasses.find((el) =>
215+
el.product_tax_class_ids.indexOf(parseInt(product.tax_class_id)) >= 0 &&
216+
el.customer_tax_class_ids.indexOf(userGroupId) >= 0
217+
)
218+
} else {
219+
taxClass = taxClasses.find((el) => el.product_tax_class_ids.indexOf(parseInt(product.tax_class_id) >= 0))
220+
}
221+
213222
if (taxClass) {
214223
for (let rate of taxClass.rates) { // TODO: add check for zip code ranges (!)
215224
if (rate.tax_country_id === taxCountry && (rate.region_name === taxRegion || rate.tax_region_id === 0 || !rate.region_name)) {
@@ -256,3 +265,19 @@ export function calculateProductTax (product, taxClasses, taxCountry = 'PL', tax
256265
}
257266
}
258267
}
268+
269+
export function checkIfTaxWithUserGroupIsActive (configTax) {
270+
if (typeof configTax.userGroupId === 'number') {
271+
return true
272+
}
273+
274+
return false
275+
}
276+
277+
export function getUserGroupIdToUse (userGroupId, configTax) {
278+
if (configTax.useOnlyDefaultUserGroupId) {
279+
return configTax.userGroupId
280+
}
281+
282+
return userGroupId
283+
}

src/platform/magento1/tax.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import AbstractTaxProxy from '../abstract/tax'
2-
import { calculateProductTax } from '../../lib/taxcalc'
2+
import { calculateProductTax, checkIfTaxWithUserGroupIsActive, getUserGroupIdToUse } from '../../lib/taxcalc'
33
import TierHelper from '../../helpers/priceTiers'
44
const es = require('elasticsearch')
55
const bodybuilder = require('bodybuilder')
@@ -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) {
@@ -92,7 +95,14 @@ class TaxProxy extends AbstractTaxProxy {
9295
client.search(esQuery).then((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)

src/platform/magento2/tax.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import AbstractTaxProxy from '../abstract/tax'
2-
import { calculateProductTax } from '../../lib/taxcalc'
2+
import { calculateProductTax, checkIfTaxWithUserGroupIsActive, getUserGroupIdToUse } from '../../lib/taxcalc';
33
import TierHelper from '../../helpers/priceTiers'
44
const es = require('elasticsearch')
55
const bodybuilder = require('bodybuilder')
@@ -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) {
@@ -22,6 +24,7 @@ class TaxProxy extends AbstractTaxProxy {
2224
taxCountry = store.tax.defaultCountry
2325
sourcePriceInclTax = store.tax.sourcePriceIncludesTax
2426
finalPriceInclTax = store.tax.finalPriceIncludesTax
27+
this._storeConfigTax = store.tax
2528
break;
2629
}
2730
}
@@ -50,8 +53,8 @@ class TaxProxy extends AbstractTaxProxy {
5053
this.taxFor = this.taxFor.bind(this)
5154
}
5255

53-
taxFor (product) {
54-
return calculateProductTax(product, this._taxClasses, this._taxCountry, this._taxRegion, this._sourcePriceInclTax, this._deprecatedPriceFieldsSupport, this._finalPriceInclTax)
56+
taxFor (product, groupId) {
57+
return calculateProductTax(product, this._taxClasses, this._taxCountry, this._taxRegion, this._sourcePriceInclTax, this._deprecatedPriceFieldsSupport, this._finalPriceInclTax, groupId, this._storeConfigTax)
5558
}
5659

5760
applyTierPrices (productList, groupId) {
@@ -91,7 +94,14 @@ class TaxProxy extends AbstractTaxProxy {
9194
client.search(esQuery).then((taxClasses) => { // we're always trying to populate cache - when online
9295
inst._taxClasses = taxClasses.hits.hits.map(el => { return el._source })
9396
for (let item of productList) {
94-
inst.taxFor(item._source)
97+
const isActive = checkIfTaxWithUserGroupIsActive(inst._storeConfigTax)
98+
if (isActive) {
99+
groupId = getUserGroupIdToUse(inst._userGroupId, inst._storeConfigTax)
100+
} else {
101+
groupId = null
102+
}
103+
104+
inst.taxFor(item._source, groupId)
95105
}
96106

97107
resolve(productList)

0 commit comments

Comments
 (0)