Created
August 13, 2016 19:29
-
-
Save mxfactorial/2c688ca79946f1305589e017c2e315d9 to your computer and use it in GitHub Desktop.
Transaction Rule Script (TRS) adding a 9% per item California State sales tax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addNinePercentPerItemCaliforniaStateSalesTax(transaction) { | |
var cr_account = arguments.length <= 1 || arguments[1] === undefined ? "DannysMarket" : arguments[1]; | |
// Test if cr_account is not defined in nested objects, if true return original object | |
var cr_accountNotDefine = false; | |
transaction.transaction_item.forEach(function (item) { | |
if (item.cr_account == null) { | |
cr_accountNotDefine = true; | |
} | |
}); | |
if (cr_accountNotDefine) { | |
return transaction; | |
} | |
// Remove any existing “9% state sales tax” item to avoid duplicating objects in the array | |
var cr_accountItems = transaction.transaction_item.filter(function (item) { | |
return item.name !== "9% state sales tax"; | |
}); | |
// Add 9% sales tax. | |
var salesTaxValue = 0; | |
cr_accountItems.forEach(function (item) { | |
if (item.cr_account == cr_account) { | |
salesTaxValue = salesTaxValue + item.value * item.quantity * 0.09; | |
} | |
}); | |
// Add “9% state sales tax” item | |
cr_accountItems.push({ | |
db_account: transaction.db_author, | |
cr_account: "StateOfCalifornia", | |
quantity: "1", | |
units_measured: "", | |
unit_of_measurement: "", | |
value: salesTaxValue.toFixed(3), | |
name: "9% state sales tax" | |
}); | |
transaction.transaction_item = cr_accountItems; | |
// Test if db_account of all transaction items match db_author of transaction | |
transaction.transaction_item.forEach(function (item) { | |
if (item.db_account != transaction.db_author) { | |
item.db_account = transaction.db_author; | |
} | |
}); | |
return transaction; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
got it .. thank you