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

Commit 51de055

Browse files
committed
Added tax calculation with product_tax_id and user group id
1 parent 2dc690f commit 51de055

File tree

6 files changed

+85
-37
lines changed

6 files changed

+85
-37
lines changed

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: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,21 @@ export default ({config, db}) => function (req, res, body) {
6868
url = config.elasticsearch.protocol + '://' + url
6969
}
7070

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

82-
delete requestBody.groupToken
83-
}
84-
8584
let auth = null;
86-
85+
8786
// Only pass auth if configured
8887
if(config.elasticsearch.user || config.elasticsearch.password) {
8988
auth = {

src/api/user.js

Lines changed: 6 additions & 8 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}) => {

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: 15 additions & 5 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
const es = require('elasticsearch')
44
const bodybuilder = require('bodybuilder')
55
import 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)

src/platform/magento2/tax.js

Lines changed: 15 additions & 5 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
const es = require('elasticsearch')
44
const bodybuilder = require('bodybuilder')
55
import 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){
@@ -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) {
@@ -81,7 +84,7 @@ class TaxProxy extends AbstractTaxProxy {
8184
if (this._config.elasticsearch.user) {
8285
esConfig.httpAuth = this._config.elasticsearch.user + ':' + this._config.elasticsearch.password
8386
}
84-
87+
8588
let client = new es.Client(esConfig)
8689
const esQuery = {
8790
index: this._indexName,
@@ -91,7 +94,14 @@ class TaxProxy extends AbstractTaxProxy {
9194
client.search(esQuery).then(function (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)