From f2b1fc6195fef711366fc430f2b63250826ed315 Mon Sep 17 00:00:00 2001 From: Sven Verweij <37927107+SvenVw@users.noreply.github.com> Date: Wed, 18 Jun 2025 15:53:30 +0200 Subject: [PATCH 1/4] fix: redirects at harvest details page --- .changeset/vast-onions-judge.md | 5 +++++ ...d.$b_id.cultivation.$b_lu.harvest.$b_id_harvesting.tsx | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .changeset/vast-onions-judge.md diff --git a/.changeset/vast-onions-judge.md b/.changeset/vast-onions-judge.md new file mode 100644 index 000000000..fe5d6ca00 --- /dev/null +++ b/.changeset/vast-onions-judge.md @@ -0,0 +1,5 @@ +--- +"@svenvw/fdm-app": patch +--- + +Fixes redirects at harvest details page diff --git a/fdm-app/app/routes/farm.$b_id_farm.$calendar.field.$b_id.cultivation.$b_lu.harvest.$b_id_harvesting.tsx b/fdm-app/app/routes/farm.$b_id_farm.$calendar.field.$b_id.cultivation.$b_lu.harvest.$b_id_harvesting.tsx index 07585ad7b..bc70b6a07 100644 --- a/fdm-app/app/routes/farm.$b_id_farm.$calendar.field.$b_id.cultivation.$b_lu.harvest.$b_id_harvesting.tsx +++ b/fdm-app/app/routes/farm.$b_id_farm.$calendar.field.$b_id.cultivation.$b_lu.harvest.$b_id_harvesting.tsx @@ -17,6 +17,7 @@ import { clientConfig } from "~/lib/config" import { handleActionError, handleLoaderError } from "~/lib/error" import { fdm } from "~/lib/fdm.server" import { extractFormValuesFromRequest } from "~/lib/form" +import { getCalendar } from "../lib/calendar" // Meta export const meta: MetaFunction = () => { @@ -80,6 +81,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) { // Get the session const session = await getSession(request) + const calendar = getCalendar(params) // Get details of cultivation const cultivation = await getCultivation( @@ -106,6 +108,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) { cultivation: cultivation, harvest: harvest, b_id_farm: b_id_farm, + calendar: calendar, } } catch (error) { throw handleLoaderError(error) @@ -137,7 +140,7 @@ export default function FarmFieldsOverviewBlock() {
@@ -191,6 +194,7 @@ export async function action({ request, params }: ActionFunctionArgs) { // Get the session const session = await getSession(request) + const calendar = await getCalendar(params) // Collect form entry const formValues = await extractFormValuesFromRequest( @@ -209,7 +213,7 @@ export async function action({ request, params }: ActionFunctionArgs) { ) return redirectWithSuccess( - `/farm/${b_id_farm}/field/${b_id}/cultivation/${b_lu}`, + `/farm/${b_id_farm}/${calendar}/field/${b_id}/cultivation/${b_lu}`, { message: "Oogst is toegevoegd! 🎉", }, From 94a82f6c2cce8a536b4c7a890c081ef2fe82c10a Mon Sep 17 00:00:00 2001 From: Sven Verweij <37927107+SvenVw@users.noreply.github.com> Date: Wed, 18 Jun 2025 16:19:16 +0200 Subject: [PATCH 2/4] Fix: at balance calculation to convert null values to 0 and prevent exception due to Decimal --- .changeset/cyan-peaches-wait.md | 5 +++++ fdm-calculator/src/balance/nitrogen/index.ts | 4 ++-- fdm-calculator/src/balance/nitrogen/removal/residue.ts | 8 ++++---- .../src/balance/nitrogen/supply/fertilizers/compost.ts | 4 ++-- .../src/balance/nitrogen/supply/fertilizers/manure.ts | 4 ++-- .../src/balance/nitrogen/supply/fertilizers/mineral.ts | 4 ++-- .../src/balance/nitrogen/supply/fertilizers/other.ts | 4 ++-- .../src/balance/nitrogen/supply/mineralization.ts | 2 +- fdm-calculator/src/balance/nitrogen/target.ts | 2 +- .../src/balance/nitrogen/volatization/residues.ts | 10 ++++++---- 10 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 .changeset/cyan-peaches-wait.md diff --git a/.changeset/cyan-peaches-wait.md b/.changeset/cyan-peaches-wait.md new file mode 100644 index 000000000..86ce02081 --- /dev/null +++ b/.changeset/cyan-peaches-wait.md @@ -0,0 +1,5 @@ +--- +"@svenvw/fdm-calculator": patch +--- + +Fix at balance calculation to convert null values to 0 and prevent exception due to Decimal diff --git a/fdm-calculator/src/balance/nitrogen/index.ts b/fdm-calculator/src/balance/nitrogen/index.ts index 9d01bad33..551fdbee7 100644 --- a/fdm-calculator/src/balance/nitrogen/index.ts +++ b/fdm-calculator/src/balance/nitrogen/index.ts @@ -197,7 +197,7 @@ export function calculateNitrogenBalancesFieldToFarm( // Explicitly state it returns the Decimal version // Calculate the total farm area const totalFarmArea = fields.reduce( - (sum, field) => sum.add(new Decimal(field.field.b_area)), + (sum, field) => sum.add(new Decimal(field.field.b_area ?? 0)), Decimal(0), ) @@ -219,7 +219,7 @@ export function calculateNitrogenBalancesFieldToFarm( ) continue // Skip this iteration if fieldInput is not found } - const fieldArea = new Decimal(fieldInput.field.b_area) + const fieldArea = new Decimal(fieldInput.field.b_area ?? 0) totalFarmSupply = totalFarmSupply.add( fieldBalance.supply.total.times(fieldArea), diff --git a/fdm-calculator/src/balance/nitrogen/removal/residue.ts b/fdm-calculator/src/balance/nitrogen/removal/residue.ts index 64375563a..dfd28f2c5 100644 --- a/fdm-calculator/src/balance/nitrogen/removal/residue.ts +++ b/fdm-calculator/src/balance/nitrogen/removal/residue.ts @@ -64,7 +64,7 @@ export function calculateNitrogenRemovalByResidue( const analysisWithYield = harvest.harvestable.harvestable_analyses.find( (analysis: { b_lu_yield: number | undefined }) => - analysis.b_lu_yield !== undefined, + analysis.b_lu_yield !== undefined || analysis.b_lu_yield !== null, ) if (analysisWithYield) { yieldForThisHarvest = new Decimal( @@ -87,17 +87,17 @@ export function calculateNitrogenRemovalByResidue( // Get the average yield for the cultivation if (harvestCount === 0) { // Return default yield from cultivation catalogue - b_lu_yield = new Decimal(cultivationDetail.b_lu_yield) + b_lu_yield = new Decimal(cultivationDetail.b_lu_yield ?? 0) } else { b_lu_yield = totalYield.dividedBy(harvestCount) } // Get the harvest for crop residues - const b_lu_hi = new Decimal(cultivationDetail.b_lu_hi) + const b_lu_hi = new Decimal(cultivationDetail.b_lu_hi ?? 0) const b_lu_hi_res = new Decimal(1).minus(b_lu_hi) // Get the Nitrogen content of the crop residues - const b_lu_n_residue = new Decimal(cultivationDetail.b_lu_n_residue) + const b_lu_n_residue = new Decimal(cultivationDetail.b_lu_n_residue ?? 0) // Calculate the amount of Nitrogen removed by crop residues of this cultivation const removal = b_lu_yield diff --git a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/compost.ts b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/compost.ts index 367d9ac97..b50f28c45 100644 --- a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/compost.ts +++ b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/compost.ts @@ -38,7 +38,7 @@ export function calculateNitrogenSupplyByCompost( } const p_type_compost = fertilizerDetail.p_type_compost - const p_n_rt = new Decimal(fertilizerDetail.p_n_rt).dividedBy(1000) // Convert from g N / kg to kg N / kg + const p_n_rt = new Decimal(fertilizerDetail.p_n_rt ?? 0).dividedBy(1000) // Convert from g N / kg to kg N / kg // If the fertilizer used is not of the type compost if (p_type_compost === false) { @@ -49,7 +49,7 @@ export function calculateNitrogenSupplyByCompost( } // Calculate for this application the amount of Nitrogen supplied by compost - const p_app_amount = new Decimal(application.p_app_amount) + const p_app_amount = new Decimal(application.p_app_amount ?? 0) const applicationValue = p_app_amount.times(p_n_rt) return { diff --git a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/manure.ts b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/manure.ts index 6dffaab02..c0c760af2 100644 --- a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/manure.ts +++ b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/manure.ts @@ -37,7 +37,7 @@ export function calculateNitrogenSupplyByManure( ) } const p_type_manure = fertilizerDetail.p_type_manure - const p_n_rt = new Decimal(fertilizerDetail.p_n_rt) + const p_n_rt = new Decimal(fertilizerDetail.p_n_rt ?? 0) // If the fertilizer used is not of the type manure if (p_type_manure === false) { @@ -48,7 +48,7 @@ export function calculateNitrogenSupplyByManure( } // Calculate for this application the amount of Nitrogen supplied by manure - const p_app_amount = new Decimal(application.p_app_amount) + const p_app_amount = new Decimal(application.p_app_amount ?? 0) const applicationValue = p_app_amount.times(p_n_rt).dividedBy(1000) // convert from g N to kg N return { diff --git a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/mineral.ts b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/mineral.ts index 6a55dd2a1..0d69d1aed 100644 --- a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/mineral.ts +++ b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/mineral.ts @@ -36,7 +36,7 @@ export function calculateNitrogenSupplyByMineralFertilizers( ) } const p_type_mineral = fertilizerDetail.p_type_mineral - const p_n_rt = new Decimal(fertilizerDetail.p_n_rt).dividedBy(1000) // Convert from g N / kg to kg N / kg + const p_n_rt = new Decimal(fertilizerDetail.p_n_rt ?? 0).dividedBy(1000) // Convert from g N / kg to kg N / kg // If the fertilizer used is not of the type mineral if (p_type_mineral === false) { @@ -47,7 +47,7 @@ export function calculateNitrogenSupplyByMineralFertilizers( } /** Calculate for this application the amount of Nitrogen supplied with the mineral fertilizer */ - const p_app_amount = new Decimal(application.p_app_amount) + const p_app_amount = new Decimal(application.p_app_amount ?? 0) const applicationValue = p_app_amount.times(p_n_rt) return { diff --git a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/other.ts b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/other.ts index c97b09c6e..1e7164ee1 100644 --- a/fdm-calculator/src/balance/nitrogen/supply/fertilizers/other.ts +++ b/fdm-calculator/src/balance/nitrogen/supply/fertilizers/other.ts @@ -33,7 +33,7 @@ export function calculateNitrogenSupplyByOtherFertilizers( const p_type_manure = fertilizerDetail.p_type_manure const p_type_mineral = fertilizerDetail.p_type_mineral const p_type_compost = fertilizerDetail.p_type_compost - const p_n_rt = new Decimal(fertilizerDetail.p_n_rt) + const p_n_rt = new Decimal(fertilizerDetail.p_n_rt ?? 0) // If the fertilizer used is not of the type other fertilizers if ( @@ -48,7 +48,7 @@ export function calculateNitrogenSupplyByOtherFertilizers( } // Calculate for this application the amount of Nitrogen supplied by other fertilizers - const p_app_amount = new Decimal(application.p_app_amount) + const p_app_amount = new Decimal(application.p_app_amount ?? 0) const applicationValue = p_app_amount.times(p_n_rt).dividedBy(1000) // convert from g N to kg N return { diff --git a/fdm-calculator/src/balance/nitrogen/supply/mineralization.ts b/fdm-calculator/src/balance/nitrogen/supply/mineralization.ts index c83afe482..e83211150 100644 --- a/fdm-calculator/src/balance/nitrogen/supply/mineralization.ts +++ b/fdm-calculator/src/balance/nitrogen/supply/mineralization.ts @@ -43,7 +43,7 @@ export function calculateNitrogenSupplyBySoilMineralization( return { total: new Decimal(0) } } const timeFrameFraction = timeFrameDays.add(1).dividedBy(365) - const mineralization = new Decimal(mineralizationValue).times( + const mineralization = mineralizationValue.times( timeFrameFraction, ) diff --git a/fdm-calculator/src/balance/nitrogen/target.ts b/fdm-calculator/src/balance/nitrogen/target.ts index 62aa64126..ac04dd4e6 100644 --- a/fdm-calculator/src/balance/nitrogen/target.ts +++ b/fdm-calculator/src/balance/nitrogen/target.ts @@ -126,7 +126,7 @@ export function calculateTargetForNitrogenBalance( return new Decimal(0) } const timeFrameFraction = timeFrameDays.add(1).dividedBy(365) - const target = new Decimal(targetValue).times(timeFrameFraction) + const target = new Decimal(targetValue ?? 0).times(timeFrameFraction) return target } diff --git a/fdm-calculator/src/balance/nitrogen/volatization/residues.ts b/fdm-calculator/src/balance/nitrogen/volatization/residues.ts index f5b25c5fb..31b8406c4 100644 --- a/fdm-calculator/src/balance/nitrogen/volatization/residues.ts +++ b/fdm-calculator/src/balance/nitrogen/volatization/residues.ts @@ -75,7 +75,9 @@ export function calculateNitrogenVolatizationViaAmmoniaByResidue( // Fallback to default yield from cultivation_catalogue // Fallback to default yield from cultivation catalogue if (yieldForThisHarvest === null) { - yieldForThisHarvest = new Decimal(cultivationDetail.b_lu_yield) + yieldForThisHarvest = new Decimal( + cultivationDetail.b_lu_yield ?? 0, + ) } if (yieldForThisHarvest !== null) { @@ -87,17 +89,17 @@ export function calculateNitrogenVolatizationViaAmmoniaByResidue( // Get the average yield for the cultivation if (harvestCount === 0) { // Return default yield from cultivation catalogue - b_lu_yield = new Decimal(cultivationDetail.b_lu_yield) + b_lu_yield = new Decimal(cultivationDetail.b_lu_yield ?? 0) } else { b_lu_yield = totalYield.dividedBy(harvestCount) } // Get the harvest index for crop residues - const b_lu_hi = new Decimal(cultivationDetail.b_lu_hi) + const b_lu_hi = new Decimal(cultivationDetail.b_lu_hi ??0) const b_lu_hi_res = new Decimal(1).minus(b_lu_hi) // Get the Nitrogen content of the crop residues - const b_lu_n_residue = new Decimal(cultivationDetail.b_lu_n_residue) + const b_lu_n_residue = new Decimal(cultivationDetail.b_lu_n_residue ?? 0) // Calculate the Emission Factor let emissionFactor = new Decimal(0.41).times(b_lu_n_residue).minus(5.42) From 0c9fd3d1320bd05c3e782993a29497489ef2abc5 Mon Sep 17 00:00:00 2001 From: Sven Verweij <37927107+SvenVw@users.noreply.github.com> Date: Wed, 18 Jun 2025 17:00:46 +0200 Subject: [PATCH 3/4] fix: incorrect conditional --- fdm-calculator/src/balance/nitrogen/removal/residue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fdm-calculator/src/balance/nitrogen/removal/residue.ts b/fdm-calculator/src/balance/nitrogen/removal/residue.ts index dfd28f2c5..0b0023d79 100644 --- a/fdm-calculator/src/balance/nitrogen/removal/residue.ts +++ b/fdm-calculator/src/balance/nitrogen/removal/residue.ts @@ -64,7 +64,7 @@ export function calculateNitrogenRemovalByResidue( const analysisWithYield = harvest.harvestable.harvestable_analyses.find( (analysis: { b_lu_yield: number | undefined }) => - analysis.b_lu_yield !== undefined || analysis.b_lu_yield !== null, + analysis.b_lu_yield !== undefined && analysis.b_lu_yield !== null, ) if (analysisWithYield) { yieldForThisHarvest = new Decimal( From 01596bafee817f372bbc3bbce7c1508eb7031a64 Mon Sep 17 00:00:00 2001 From: Sven Verweij <37927107+SvenVw@users.noreply.github.com> Date: Wed, 18 Jun 2025 17:13:24 +0200 Subject: [PATCH 4/4] chore: bump version number and update changelog --- .changeset/cyan-peaches-wait.md | 5 ----- .changeset/vast-onions-judge.md | 5 ----- fdm-app/CHANGELOG.md | 8 ++++++++ fdm-app/package.json | 2 +- fdm-calculator/CHANGELOG.md | 6 ++++++ fdm-calculator/package.json | 2 +- 6 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 .changeset/cyan-peaches-wait.md delete mode 100644 .changeset/vast-onions-judge.md diff --git a/.changeset/cyan-peaches-wait.md b/.changeset/cyan-peaches-wait.md deleted file mode 100644 index 86ce02081..000000000 --- a/.changeset/cyan-peaches-wait.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@svenvw/fdm-calculator": patch ---- - -Fix at balance calculation to convert null values to 0 and prevent exception due to Decimal diff --git a/.changeset/vast-onions-judge.md b/.changeset/vast-onions-judge.md deleted file mode 100644 index fe5d6ca00..000000000 --- a/.changeset/vast-onions-judge.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@svenvw/fdm-app": patch ---- - -Fixes redirects at harvest details page diff --git a/fdm-app/CHANGELOG.md b/fdm-app/CHANGELOG.md index 9e8a82a12..2263b6583 100644 --- a/fdm-app/CHANGELOG.md +++ b/fdm-app/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog fdm-app +## 0.19.6 + +### Patch Changes + +- f2b1fc6: Fixes redirects at harvest details page +- Updated dependencies [94a82f6] + - @svenvw/fdm-calculator@0.3.3 + ## 0.19.5 ### Patch Changes diff --git a/fdm-app/package.json b/fdm-app/package.json index 83c87d151..1c80c9443 100644 --- a/fdm-app/package.json +++ b/fdm-app/package.json @@ -1,6 +1,6 @@ { "name": "@svenvw/fdm-app", - "version": "0.19.5", + "version": "0.19.6", "private": true, "sideEffects": false, "type": "module", diff --git a/fdm-calculator/CHANGELOG.md b/fdm-calculator/CHANGELOG.md index 9fea7788b..e949f208c 100644 --- a/fdm-calculator/CHANGELOG.md +++ b/fdm-calculator/CHANGELOG.md @@ -1,5 +1,11 @@ # fdm-calculator +## 0.3.3 + +### Patch Changes + +- 94a82f6: Fix at balance calculation to convert null values to 0 and prevent exception due to Decimal + ## 0.3.2 ### Patch Changes diff --git a/fdm-calculator/package.json b/fdm-calculator/package.json index c9445ea33..faa172991 100644 --- a/fdm-calculator/package.json +++ b/fdm-calculator/package.json @@ -1,7 +1,7 @@ { "name": "@svenvw/fdm-calculator", "private": false, - "version": "0.3.2", + "version": "0.3.3", "description": "Calculate various insights based on the Farm Data Model", "license": "MIT", "homepage": "https://github.com/SvenVw/fdm",