Skip to content

Commit

Permalink
Merge pull request #42 from HarvestProfit/do-not-use-split-attribute-…
Browse files Browse the repository at this point in the history
…and-include-percent-check

does not rely on 'split' attribute and includes percent unit check
  • Loading branch information
humphreyja authored May 15, 2021
2 parents 1473c6b + 859378b commit a204c84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harvest-profit/units",
"version": "1.4.2",
"version": "1.4.3",
"description": "Units helper for Harvest Profit javascript applications",
"main": "dist/index.js",
"repository": "https://github.com/HarvestProfit/harvest-profit-units",
Expand Down
22 changes: 20 additions & 2 deletions src/UnitsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,26 @@ export default class UnitsHelper {
static perAcreCost = (product, item, acres) => {
const perUnitCost = UnitsHelper.perUnitCost(product, item);
let acresRatio = 1;
if (item.split) {
acresRatio = parseFloat(item.applied_acres || 0) / acres;

let itemAppliedAcres = parseFloat(item.applied_acres);
function isSplit() {
if (item.applied_acres_units === 'percent') return false;
if (Number.isNaN(itemAppliedAcres)) return false;
if (itemAppliedAcres === acres) return false;
return true;
}

function isRatio() {
if (item.applied_acres_units !== 'percent') return false;
if (Number.isNaN(itemAppliedAcres)) return false;
if (itemAppliedAcres === 100) return false;
return true;
}

if (isSplit()) {
acresRatio = itemAppliedAcres / acres;
} else if (isRatio()) {
acresRatio = itemAppliedAcres / 100;
}
if (item.is_total) {
const total = item.amount * perUnitCost;
Expand Down
9 changes: 8 additions & 1 deletion test/UnitsHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ describe('UnitsHelper', () => {

it('should calculate per acre costs from a given product, input applied to half the field', () => {
const product = productInCustomUnits;
const lineItem = { ...lineIteminCustomUnits, split: true };
const lineItem = lineIteminCustomUnits;
const unitCost = UnitsHelper.perAcreCost(product, lineItem, 200);
expect(unitCost).toBeCloseTo(2000.00);
});

it('should calculate per acre costs from a given product, input applied to half the field', () => {
const product = productInCustomUnits;
const lineItem = { ...lineIteminCustomUnits, applied_acres: 50, applied_acres_units: 'percent' };
const unitCost = UnitsHelper.perAcreCost(product, lineItem, 200);
expect(unitCost).toBeCloseTo(2000.00);
});
Expand Down

0 comments on commit a204c84

Please sign in to comment.