diff --git a/.changeset/better-snakes-clap.md b/.changeset/better-snakes-clap.md new file mode 100644 index 000000000..6f4d23ae5 --- /dev/null +++ b/.changeset/better-snakes-clap.md @@ -0,0 +1,5 @@ +--- +"@nmi-agro/fdm-calculator": patch +--- + +Fix nitrogen removal for crop residues: residues left on the field are no longer counted as removed, while residues removed from the field are. diff --git a/fdm-calculator/src/balance/nitrogen/removal/index.test.ts b/fdm-calculator/src/balance/nitrogen/removal/index.test.ts index 97444a5f3..4404a00d5 100644 --- a/fdm-calculator/src/balance/nitrogen/removal/index.test.ts +++ b/fdm-calculator/src/balance/nitrogen/removal/index.test.ts @@ -66,9 +66,9 @@ describe("calculateNitrogenRemoval", () => { cultivationDetailsMap, ) - expect(result.total.toNumber()).toBeCloseTo(-23) // -20 from harvest + -3 from residue + expect(result.total.toNumber()).toBeCloseTo(-20) // -20 from harvest expect(result.harvests.total.toNumber()).toBeCloseTo(-20) - expect(result.residues.total.toNumber()).toBeCloseTo(-3) + expect(result.residues.total.toNumber()).toBeCloseTo(0) // No nitrogen removed by residues since m_cropresidue is true }) it("should handle cases with no harvests or residues", () => { @@ -76,7 +76,7 @@ describe("calculateNitrogenRemoval", () => { { b_lu: "cultivation1", b_lu_catalogue: "catalogue1", - m_cropresidue: false, + m_cropresidue: true, // Residues left on field, not removed b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), b_lu_name: "Cultivation 1", diff --git a/fdm-calculator/src/balance/nitrogen/removal/residue.test.ts b/fdm-calculator/src/balance/nitrogen/removal/residue.test.ts index 490325409..fa9b8538a 100644 --- a/fdm-calculator/src/balance/nitrogen/removal/residue.test.ts +++ b/fdm-calculator/src/balance/nitrogen/removal/residue.test.ts @@ -31,14 +31,14 @@ describe("calculateNitrogenRemovalByResidue", () => { ], ]) - it("should return 0 if no crop residues are left", () => { + it("should return 0 if crop residues are left on the field", () => { const cultivations: FieldInput["cultivations"] = [ { b_lu: "cultivation1", b_lu_catalogue: "catalogue1", b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), - m_cropresidue: false, // No residue left + m_cropresidue: true, // Residues left on field, not removed b_lu_name: "Cultivation 1", b_lu_croprotation: "cereal", }, @@ -64,7 +64,7 @@ describe("calculateNitrogenRemovalByResidue", () => { b_lu_catalogue: "catalogue1", b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), - m_cropresidue: true, + m_cropresidue: false, // Residues removed from field b_lu_name: "Cultivation 1", b_lu_croprotation: "cereal", }, @@ -116,7 +116,7 @@ describe("calculateNitrogenRemovalByResidue", () => { b_lu_catalogue: "catalogue1", b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), - m_cropresidue: true, + m_cropresidue: false, // Residues removed from field b_lu_name: "Cultivation 1", b_lu_croprotation: "cereal", }, @@ -185,7 +185,7 @@ describe("calculateNitrogenRemovalByResidue", () => { b_lu_catalogue: "catalogue1", b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), - m_cropresidue: true, + m_cropresidue: false, // Residues removed from field b_lu_name: "Cultivation 1", b_lu_croprotation: "cereal", }, @@ -228,7 +228,7 @@ describe("calculateNitrogenRemovalByResidue", () => { ) }) - it("should handle null m_cropresidue as false", () => { + it("should handle null m_cropresidue as not removed (return 0)", () => { const cultivations: FieldInput["cultivations"] = [ { b_lu: "cultivation1", @@ -261,7 +261,7 @@ describe("calculateNitrogenRemovalByResidue", () => { b_lu_catalogue: "catalogue1", b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), - m_cropresidue: true, + m_cropresidue: false, // Residues removed from field b_lu_name: "Cultivation 1", b_lu_croprotation: "cereal", }, @@ -295,7 +295,7 @@ describe("calculateNitrogenRemovalByResidue", () => { b_lu_catalogue: "catalogue2", b_lu_start: new Date("2022-01-01"), b_lu_end: new Date("2022-12-31"), - m_cropresidue: true, + m_cropresidue: false, // Residues removed from field b_lu_name: "test", b_lu_croprotation: null, }, diff --git a/fdm-calculator/src/balance/nitrogen/removal/residue.ts b/fdm-calculator/src/balance/nitrogen/removal/residue.ts index 2632cd7bf..fbfecc87e 100644 --- a/fdm-calculator/src/balance/nitrogen/removal/residue.ts +++ b/fdm-calculator/src/balance/nitrogen/removal/residue.ts @@ -39,8 +39,8 @@ export function calculateNitrogenRemovalByResidue( ) } - // If no crop residues are left or if this is not known return 0 for the amount of Nitrogen removed by crop residues - if (!cultivation.m_cropresidue) { + // If crop residues are left or if this is not known return 0 for the amount of Nitrogen removed by crop residues + if (cultivation.m_cropresidue !== false) { return { id: cultivation.b_lu, value: new Decimal(0),