Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fdm-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -137,7 +140,7 @@ export default function FarmFieldsOverviewBlock() {
</div>
<div className="flex justify-end">
<NavLink
to={`/farm/${loaderData.b_id_farm}/field/${loaderData.cultivation.b_id}/cultivation/${loaderData.cultivation.b_lu}`}
to={`/farm/${loaderData.b_id_farm}/${loaderData.calendar}/field/${loaderData.cultivation.b_id}/cultivation/${loaderData.cultivation.b_lu}`}
className={"ml-auto"}
>
<Button>{"Terug"}</Button>
Expand Down Expand Up @@ -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(
Expand All @@ -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! 🎉",
},
Expand Down
2 changes: 1 addition & 1 deletion fdm-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@svenvw/fdm-app",
"version": "0.19.5",
"version": "0.19.6",
"private": true,
"sideEffects": false,
"type": "module",
Expand Down
6 changes: 6 additions & 0 deletions fdm-calculator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion fdm-calculator/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions fdm-calculator/src/balance/nitrogen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand All @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions fdm-calculator/src/balance/nitrogen/removal/residue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
2 changes: 1 addition & 1 deletion fdm-calculator/src/balance/nitrogen/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 6 additions & 4 deletions fdm-calculator/src/balance/nitrogen/volatization/residues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down