From 62c2f6a63caf5436bbd21913839164256a4320b8 Mon Sep 17 00:00:00 2001 From: csawatzky Date: Fri, 6 Mar 2026 10:24:12 -0600 Subject: [PATCH] finished updating for US tons --- package-lock.json | 2 +- src/bin/BinCardV2.tsx | 6 +- src/bin/BinSettings.tsx | 16 ++-- src/bin/BinTransactions.tsx | 4 +- src/bin/BinVisualizerV2.tsx | 13 ++- src/bin/graphs/BinLevelOverTime.tsx | 20 ++--- src/contract/contractSettings.tsx | 26 ++++-- src/contract/contractTransactionGraph.tsx | 8 +- src/grain/CustomGrainForm.tsx | 97 +++++---------------- src/grain/GrainTransaction.tsx | 13 +-- src/grainBag/grainBagInventoryGraph.tsx | 35 +++++--- src/grainBag/grainBagSettings.tsx | 50 +++++++---- src/grainBag/grainBagVisualizer.tsx | 20 ++++- src/models/Bin.ts | 47 ++-------- src/models/Contract.ts | 30 +++++-- src/transactions/transactionDataDisplay.tsx | 4 +- 16 files changed, 180 insertions(+), 211 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3c23e65..fefa0aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11967,7 +11967,7 @@ }, "node_modules/protobuf-ts": { "version": "1.0.0", - "resolved": "git+https://gitlab+deploy-token-50627:hv8mB4WkyvtjBpJKU1rN@gitlab.com/brandx/protobuf-ts.git#8281f64aa2c8db09a73f5a2eb8a1b97a63bcc653", + "resolved": "git+https://gitlab+deploy-token-50627:hv8mB4WkyvtjBpJKU1rN@gitlab.com/brandx/protobuf-ts.git#932bdfbc7637f23032ac542a7d059af1e0a88401", "dependencies": { "protobufjs": "^6.8.8" } diff --git a/src/bin/BinCardV2.tsx b/src/bin/BinCardV2.tsx index f8fa5fb..5244db9 100644 --- a/src/bin/BinCardV2.tsx +++ b/src/bin/BinCardV2.tsx @@ -446,14 +446,14 @@ export default function BinCard(props: Props) { if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1) { return ( - bin.grainTonnes().toLocaleString() + + bin.grainInventory().toLocaleString() + " mT " + bin.fillPercent() + "%" ); - } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1) { + } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1) { return ( - bin.grainTons().toLocaleString() + + bin.grainInventory().toLocaleString() + " t " + bin.fillPercent() + "%" diff --git a/src/bin/BinSettings.tsx b/src/bin/BinSettings.tsx index df9a6fe..58669d6 100644 --- a/src/bin/BinSettings.tsx +++ b/src/bin/BinSettings.tsx @@ -239,21 +239,18 @@ export default function BinSettings(props: Props) { if (!initForm.inventory.bushelsPerTonne) { initForm.inventory.bushelsPerTonne = grain.bushelsPerTonne; } - if (!initForm.inventory.bushelsPerTon) { - initForm.inventory.bushelsPerTon = grain.bushelsPerTon; - } if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ setBushelConversion(initForm.inventory.bushelsPerTonne) }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ - setBushelConversion(initForm.inventory.bushelsPerTon) + setBushelConversion(initForm.inventory.bushelsPerTonne * 0.907) } if (initForm.inventory.customGrain){ setCustomGrain(initForm.inventory.customGrain) if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ setBushelConversion(initForm.inventory.customGrain.bushelsPerTonne) }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ - setBushelConversion(initForm.inventory.customGrain.bushelsPerTon) + setBushelConversion(initForm.inventory.customGrain.bushelsPerTonne * 0.907) } } //if the target temp is not set (older bins) make it the midpoint of the high and low temps assuming they are set otherwise make it 15 @@ -310,13 +307,13 @@ export default function BinSettings(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ weight = (gb/initForm.inventory.bushelsPerTonne).toFixed(2) }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ - weight = (gb/initForm.inventory.bushelsPerTon).toFixed(2) + weight = (gb/(initForm.inventory.bushelsPerTonne * 0.907)).toFixed(2) } if(initForm.inventory.customGrain){ if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ weight = (gb/initForm.inventory.customGrain.bushelsPerTonne).toFixed(2) }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ - weight = (gb/initForm.inventory.customGrain.bushelsPerTon).toFixed(2) + weight = (gb/(initForm.inventory.customGrain.bushelsPerTonne * 0.907)).toFixed(2) } } } @@ -927,7 +924,6 @@ export default function BinSettings(props: Props) { ...form.inventory, grainType: pond.Grain.GRAIN_CUSTOM, bushelsPerTonne: 0, - bushelsPerTon: 0 }) ); updateFormExtension("grainType", null); @@ -1186,14 +1182,13 @@ export default function BinSettings(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ conversion = newGrainSettings.bushelsPerTonne }else if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ - conversion = newGrainSettings.bushelsPerTon + conversion = newGrainSettings.bushelsPerTonne * 0.907 } updateForm( "inventory", pond.BinInventory.create({ ...form.inventory, bushelsPerTonne: newGrainSettings.bushelsPerTonne, - bushelsPerTon: newGrainSettings.bushelsPerTon }) ); setBushelConversion(conversion) @@ -1220,7 +1215,6 @@ export default function BinSettings(props: Props) { ...form.inventory, grainType: newGrainType, bushelsPerTonne: GrainDescriber(newGrainType).bushelsPerTonne, - bushelsPerTon: GrainDescriber(newGrainType).bushelsPerTon }) ); let conversion = 0 diff --git a/src/bin/BinTransactions.tsx b/src/bin/BinTransactions.tsx index 7cfe8fc..4c9d598 100644 --- a/src/bin/BinTransactions.tsx +++ b/src/bin/BinTransactions.tsx @@ -202,8 +202,8 @@ export default function BinTransactions(props: Props){ let tonneWeight = bushels / grainTransaction.bushelsPerTonne return tonneWeight + " mT" } - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainTransaction.bushelsPerTon > 1){ - let tonneWeight = bushels / grainTransaction.bushelsPerTon + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainTransaction.bushelsPerTonne > 1){ + let tonneWeight = bushels / (grainTransaction.bushelsPerTonne * 0.907) return tonneWeight + " t" } return bushels + " bushels" diff --git a/src/bin/BinVisualizerV2.tsx b/src/bin/BinVisualizerV2.tsx index ae4026c..2e2f987 100644 --- a/src/bin/BinVisualizerV2.tsx +++ b/src/bin/BinVisualizerV2.tsx @@ -497,8 +497,8 @@ export default function BinVisualizer(props: Props) { } else { if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1) { return Math.round((val / bin.bushelsPerTonne()) * 100) / 100; - } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1) { - return Math.round((val / bin.bushelsPerTon()) * 100) / 100; + } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1) { + return Math.round((val / (bin.bushelsPerTonne()*0.907)) * 100) / 100; } } return val; @@ -515,7 +515,7 @@ export default function BinVisualizer(props: Props) { } if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1) { return "mT (" + bin.fillPercent() + "%)"; - } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1) { + } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1) { return "t (" + bin.fillPercent() + "%)"; } else { return " / " + capacity.toLocaleString() + " bu"; @@ -1332,10 +1332,10 @@ export default function BinVisualizer(props: Props) { } } - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1) { - diffDisplay = diffDisplay / bin.bushelsPerTon(); + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1) { + diffDisplay = diffDisplay / (bin.bushelsPerTonne()*0.907); if (pendingDisplay) { - pendingDisplay = pendingDisplay / bin.bushelsPerTon(); + pendingDisplay = pendingDisplay / (bin.bushelsPerTonne()*0.907); } } @@ -1999,7 +1999,6 @@ export default function BinVisualizer(props: Props) { b.settings.inventory.grainType = grainType; b.settings.inventory.customTypeName = customTypeName; b.settings.inventory.bushelsPerTonne = GrainDescriber(grainType).bushelsPerTonne - b.settings.inventory.bushelsPerTon = GrainDescriber(grainType).bushelsPerTon // b.settings.inventory.bushelsPerTonne = isNaN(parseFloat(bushPerTonne)) // ? 0 // : parseFloat(bushPerTonne); diff --git a/src/bin/graphs/BinLevelOverTime.tsx b/src/bin/graphs/BinLevelOverTime.tsx index a691fe6..5fe36bc 100644 --- a/src/bin/graphs/BinLevelOverTime.tsx +++ b/src/bin/graphs/BinLevelOverTime.tsx @@ -73,7 +73,7 @@ export default function BinLevelOverTime(props: Props) { cap = cap / bin.bushelsPerTonne(); } if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && cap) { - cap = cap / bin.bushelsPerTon(); + cap = cap / (bin.bushelsPerTonne()*0.907); } setCapacity(cap); binAPI @@ -92,8 +92,8 @@ export default function BinLevelOverTime(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1){ grainDisplay = Math.round((bushels / bin.bushelsPerTonne()) * 100) / 100; } - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1){ - grainDisplay = Math.round((bushels / bin.bushelsPerTon()) * 100) / 100; + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1){ + grainDisplay = Math.round((bushels / (bin.bushelsPerTonne()*0.907)) * 100) / 100; } let newData: InventoryAt = { timestamp: moment(hist.timestamp).valueOf(), @@ -124,8 +124,8 @@ export default function BinLevelOverTime(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1){ grainDisplay = Math.round((bushels / bin.bushelsPerTonne()) * 100) / 100; } - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1){ - grainDisplay = Math.round((bushels / bin.bushelsPerTon()) * 100) / 100; + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1){ + grainDisplay = Math.round((bushels / (bin.bushelsPerTonne()*0.907)) * 100) / 100; } data.push({ value: fertilizerBin @@ -160,7 +160,7 @@ export default function BinLevelOverTime(props: Props) { cap = cap / bin.bushelsPerTonne(); } if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && cap) { - cap = cap / bin.bushelsPerTon(); + cap = cap / (bin.bushelsPerTonne()*0.907); } setCapacity(cap); let autoBarData: BarData[] = []; @@ -183,8 +183,8 @@ export default function BinLevelOverTime(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1){ grainDisplay = Math.round((grainDisplay / bin.bushelsPerTonne()) * 100) / 100; } - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1){ - grainDisplay = Math.round((grainDisplay / bin.bushelsPerTon()) * 100) / 100; + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1){ + grainDisplay = Math.round((grainDisplay / (bin.bushelsPerTonne()*0.907)) * 100) / 100; } autoBarData.push({ value: grainDisplay, @@ -202,8 +202,8 @@ export default function BinLevelOverTime(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && bin.bushelsPerTonne() > 1){ grainDisplay = Math.round((bushels / bin.bushelsPerTonne()) * 100) / 100; } - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTon() > 1){ - grainDisplay = Math.round((bushels / bin.bushelsPerTon()) * 100) / 100; + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && bin.bushelsPerTonne() > 1){ + grainDisplay = Math.round((bushels / (bin.bushelsPerTonne()*0.907)) * 100) / 100; } autoBarData.push({ value: fertilizerBin diff --git a/src/contract/contractSettings.tsx b/src/contract/contractSettings.tsx index 692a481..13116b0 100644 --- a/src/contract/contractSettings.tsx +++ b/src/contract/contractSettings.tsx @@ -92,9 +92,14 @@ export default function ContractSettings(props: Props) { setContractValue(contract.settings.totalValue.toString()); setCommodity(contract.settings.commodity); setContractDate(contract.settings.contractDate); - setConversionValue( - contract.settings.conversionValue > 0 ? contract.settings.conversionValue.toString() : "1" - ); + let conv = 1 + if(contract.settings.conversionValue > 1){ + conv = contract.settings.conversionValue + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ + conv = conv * 0.907 + } + } + setConversionValue(conv.toFixed(2)); setUseCustom(contract.isCustom()); } }, [contract]); @@ -158,17 +163,21 @@ export default function ContractSettings(props: Props) { const submit = () => { let conVal = isNaN(parseFloat(conversionValue)) ? 1 : parseFloat(conversionValue); - if(contractType === pond.ContractType.CONTRACT_TYPE_GRAIN){ - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ - conVal = conVal/1.102 //convert the ton they entered to mT to be sotred in the contract - } - } + //the toStoredUnit will convert what they entered for amount and size using the conversion that they entered + //we will assume that both of those entries are using the same unit ie both US ton or both metric tonne const deliveredVal = isNaN(parseFloat(deliveredAmount)) ? 0 : Math.round(Contract.toStoredUnit(parseFloat(deliveredAmount), contractType, conVal)); const sizeVal = isNaN(parseFloat(contractSize)) ? 0 : Math.round(Contract.toStoredUnit(parseFloat(contractSize), contractType, conVal)); + + //then after the conversion to bushels change the conversion value to metric to be stored in the contract + if(contractType === pond.ContractType.CONTRACT_TYPE_GRAIN){ + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ + conVal = conVal / 0.907 //convert the ton they entered to mT to be stored in the contracts conversion value + } + } if (contract) { let settings: pond.ContractSettings = contract.settings; settings.commodity = commodity; @@ -215,7 +224,6 @@ export default function ContractSettings(props: Props) { settings.type = contractType; settings.conversionValue = conVal; settings.contractDate = contractDate; - contractAPI .addContract(name, settings, as) .then(resp => { diff --git a/src/contract/contractTransactionGraph.tsx b/src/contract/contractTransactionGraph.tsx index f6084a9..5eae1c3 100644 --- a/src/contract/contractTransactionGraph.tsx +++ b/src/contract/contractTransactionGraph.tsx @@ -27,11 +27,17 @@ export default function ContractTransactionGraph(props: Props) { if (transaction?.grainTransaction) { value = transaction.grainTransaction.bushels; if ( - getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && + getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && transaction.grainTransaction.bushelsPerTonne > 1 ) { value = value / transaction.grainTransaction.bushelsPerTonne; } + if ( + getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && + transaction.grainTransaction.bushelsPerTonne > 1 + ) { + value = value / (transaction.grainTransaction.bushelsPerTonne * 0.907); + } } d.push({ timestamp: time.valueOf(), diff --git a/src/grain/CustomGrainForm.tsx b/src/grain/CustomGrainForm.tsx index 0bf1bf7..a13478c 100644 --- a/src/grain/CustomGrainForm.tsx +++ b/src/grain/CustomGrainForm.tsx @@ -2,6 +2,7 @@ import {MenuItem, TextField } from "@mui/material" import { cloneDeep } from "lodash" import { pond } from "protobuf-ts/pond" import React, { useEffect, useRef, useState } from "react" +import { getGrainUnit } from "utils" interface Props { grainSettings?: pond.GrainSettings @@ -18,9 +19,7 @@ export default function CustomGrainForm(props: Props) { const [constantB, setConstantB] = useState("0") const [constantC, setConstantC] = useState("0") const [kgPerBushel, setKgPerBushel] = useState("0") - const [bushelsPerTonne, setBushelsPerTonne] = useState("0")//this is metric - const [lbPerBushel, setLbPerBushel] = useState("0") - const [bushelsPerTon, setBushelsPerTon] = useState("0")//this is US ton + const [bushelsConversion, setBushelConversion] = useState("0")//this is either per tonne or per US ton depending on user preferences const valid = useRef(false) useEffect(()=>{ @@ -32,9 +31,7 @@ export default function CustomGrainForm(props: Props) { setConstantB(grainSettings.b.toString()) setConstantC(grainSettings.c.toString()) setKgPerBushel(grainSettings.kgPerBushel.toString()) - setBushelsPerTonne(grainSettings.bushelsPerTonne.toString()) - setLbPerBushel(grainSettings.poundsPerBushel.toString()) - setBushelsPerTon(grainSettings.bushelsPerTon.toString()) + setBushelConversion(grainSettings.bushelsPerTonne.toString()) setNewGrainSettings(grainSettings) } },[grainSettings]) @@ -46,18 +43,14 @@ export default function CustomGrainForm(props: Props) { constB: string, constC: string, kgPerBushel: string, - bushelsPerTonne: string, - lbPerBushel: string, - bushelsPerTon: string) => { + bushelsPerTonne: string) => { if (name === "") return false if (group === "") return false if (isNaN(parseFloat(constA))) return false if (isNaN(parseFloat(constB))) return false if (isNaN(parseFloat(constC))) return false if (isNaN(parseFloat(kgPerBushel))) return false - if (isNaN(parseFloat(lbPerBushel))) return false if (isNaN(parseFloat(bushelsPerTonne))) return false - if (isNaN(parseFloat(bushelsPerTon))) return false return true } @@ -79,7 +72,7 @@ export default function CustomGrainForm(props: Props) { setName(name) let settings = cloneDeep(newGrainSettings) settings.name = name - valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon) + valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsConversion) settingsChanged(settings) }}/> @@ -136,7 +129,7 @@ export default function CustomGrainForm(props: Props) { onChange={(e) => { let val = e.target.value setConstantA(val) - valid.current = validate(name, group, val, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon) + valid.current = validate(name, group, val, constantB, constantC, kgPerBushel, bushelsConversion) let settings = cloneDeep(newGrainSettings) if(!isNaN(parseFloat(val))){ settings.a = parseFloat(val) @@ -154,7 +147,7 @@ export default function CustomGrainForm(props: Props) { onChange={(e) => { let val = e.target.value setConstantB(val) - valid.current = validate(name, group, constantA, val, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon) + valid.current = validate(name, group, constantA, val, constantC, kgPerBushel, bushelsConversion) let settings = cloneDeep(newGrainSettings) if(!isNaN(parseFloat(val))){ settings.b = parseFloat(val) @@ -172,7 +165,7 @@ export default function CustomGrainForm(props: Props) { onChange={(e) => { let val = e.target.value setConstantC(val) - valid.current = validate(name, group, constantA, constantB, val, kgPerBushel, bushelsPerTonne, lbPerBushel, bushelsPerTon) + valid.current = validate(name, group, constantA, constantB, val, kgPerBushel, bushelsConversion) let settings = cloneDeep(newGrainSettings) if(!isNaN(parseFloat(val))){ settings.c = parseFloat(val) @@ -190,14 +183,10 @@ export default function CustomGrainForm(props: Props) { onChange={(e) => { let val = e.target.value setKgPerBushel(val) - valid.current = validate(name, group, constantA, constantB, constantC, val, bushelsPerTonne, lbPerBushel, bushelsPerTon) + valid.current = validate(name, group, constantA, constantB, constantC, val, bushelsConversion) let settings = cloneDeep(newGrainSettings) if(!isNaN(parseFloat(val))){ settings.kgPerBushel = parseFloat(val) - //then calculate the kilograms to pounds and update it as well - let newLb = Math.round((parseFloat(val)*2.205)*100)/100 - settings.poundsPerBushel = newLb - setLbPerBushel(newLb.toString()) } settingsChanged(settings) }} @@ -206,66 +195,24 @@ export default function CustomGrainForm(props: Props) { sx={{marginBottom: 2}} fullWidth type="number" - value={lbPerBushel} - error={isNaN(parseFloat(lbPerBushel))} - helperText={isNaN(parseFloat(lbPerBushel)) ? "Must be a valid number" : ""} + value={bushelsConversion} + error={isNaN(parseFloat(bushelsConversion))} + helperText={isNaN(parseFloat(bushelsConversion)) ? "Must be a valid number" : ""} onChange={(e) => { let val = e.target.value - setLbPerBushel(val) - valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, val, bushelsPerTon) + setBushelConversion(val) + valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, val) let settings = cloneDeep(newGrainSettings) if(!isNaN(parseFloat(val))){ - settings.poundsPerBushel = parseFloat(val) - //then calculate the pounds to kilograms and update it as well - let newKg = Math.round((parseFloat(val)/2.205)*100)/100 - settings.kgPerBushel = newKg - setKgPerBushel(newKg.toString()) + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ + settings.bushelsPerTonne = parseFloat(val)/0.907 + }else{ + settings.bushelsPerTonne = parseFloat(val) + } } settingsChanged(settings) }} - label="lb per Bushel"/> - { - let val = e.target.value - setBushelsPerTonne(val) - valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, val, lbPerBushel, bushelsPerTon) - let settings = cloneDeep(newGrainSettings) - if(!isNaN(parseFloat(val))){ - settings.bushelsPerTonne = parseFloat(val) - let newTon = Math.round((parseFloat(val)*0.907)*100)/100 - settings.bushelsPerTon = newTon - setBushelsPerTon(newTon.toString()) - } - settingsChanged(settings) - }} - label="Bushels per Tonne"/> - { - let val = e.target.value - setBushelsPerTon(val) - valid.current = validate(name, group, constantA, constantB, constantC, kgPerBushel, bushelsPerTonne, lbPerBushel, val) - let settings = cloneDeep(newGrainSettings) - if(!isNaN(parseFloat(val))){ - settings.bushelsPerTon = parseFloat(val) - let newTonne = Math.round((parseFloat(val)/0.907)*100)/100 - settings.bushelsPerTonne = newTonne - setBushelsPerTonne(newTonne.toString()) - } - settingsChanged(settings) - }} - label="Bushels per Ton"/> + label={"Bushels per " + (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE ? " Tonne" : "Ton")}/> ) } diff --git a/src/grain/GrainTransaction.tsx b/src/grain/GrainTransaction.tsx index 954f8fb..5605b3a 100644 --- a/src/grain/GrainTransaction.tsx +++ b/src/grain/GrainTransaction.tsx @@ -230,14 +230,8 @@ export default function GrainTransaction(props: Props) { //use the sources bushel per tonne if one was selected otherwise use the destination if (source) { bPerTonne = source.value.bushelsPerTonne(); - if(source.value.bushelsPerTon){ - bPerTon = source.value.bushelsPerTon() - } } else if (dest) { bPerTonne = dest.value.bushelsPerTonne(); - if(dest.value.bushelsPerTon){ - bPerTon = dest.value.bushelsPerTon() - } } let newTransaction = pond.Transaction.create({ @@ -247,7 +241,6 @@ export default function GrainTransaction(props: Props) { bushels: finalVal, message: grainMessage, bushelsPerTonne: bPerTonne, - bushelsPerTon: bPerTonne * 0.907 }) }) }); @@ -542,18 +535,14 @@ export default function GrainTransaction(props: Props) { grainVal = (+grainVal * selectedDestination.value.bushelsPerTonne()).toFixed(2); } } - }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE) { + }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON) { if (!isNaN(parseFloat(e.target.value))) { if (selectedSource) { if(selectedSource.value.bushelsPerTonne){ - grainVal = (+grainVal * selectedSource.value.bushelsPerTon()).toFixed(2); - }else{ grainVal = (+grainVal * (selectedSource.value.bushelsPerTonne() * 0.907)).toFixed(2); } } else if (selectedDestination) { if(selectedDestination.value.bushelsPerTonne){ - grainVal = (+grainVal * selectedDestination.value.bushelsPerTon()).toFixed(2); - }else{ grainVal = (+grainVal * (selectedDestination.value.bushelsPerTonne() * 0.907)).toFixed(2); } } diff --git a/src/grainBag/grainBagInventoryGraph.tsx b/src/grainBag/grainBagInventoryGraph.tsx index 305e84b..6a661a2 100644 --- a/src/grainBag/grainBagInventoryGraph.tsx +++ b/src/grainBag/grainBagInventoryGraph.tsx @@ -36,11 +36,14 @@ export default function GrainBagInventoryGraph(props: Props) { //let time = hist.timestamp; let val = hist.object.grainBagSettings.currentBushels ?? 0; if ( - getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && + getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && grainBag.bushelsPerTonne() > 1 ) { val = Math.round((val / grainBag.bushelsPerTonne()) * 100) / 100; } + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainBag.bushelsPerTonne() > 1){ + val = Math.round((val / (grainBag.bushelsPerTonne() * 0.907)) * 100) / 100; + } if (val !== lastBushels) { lastBushels = val; barData.push({ @@ -51,13 +54,16 @@ export default function GrainBagInventoryGraph(props: Props) { } }); if (barData.length === 0) { + let val = grainBag.bushels() + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && grainBag.bushelsPerTonne() > 1){ + val = grainBag.bushels() / grainBag.bushelsPerTonne() + } + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainBag.bushelsPerTonne() > 1){ + val = grainBag.bushels() / (grainBag.bushelsPerTonne() * 0.907) + } barData.push({ timestamp: moment.now().valueOf(), - value: - getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && - grainBag.bushelsPerTonne() > 1 - ? Math.round((grainBag.bushels() / grainBag.bushelsPerTonne()) * 100) / 100 - : grainBag.bushels() + value: Math.round(val*100)/100 }); } setData(barData); @@ -71,6 +77,17 @@ export default function GrainBagInventoryGraph(props: Props) { } }, [grainBagAPI, grainBag, startDate, endDate, openSnack]); //eslint-disable-line react-hooks/exhaustive-deps + const maxYAxis = () => { + let val = grainBag.capacity() + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ + val = Math.round((val / grainBag.bushelsPerTonne()) * 100) / 100; + } + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainBag.bushelsPerTonne() > 1){ + val = Math.round((val / (grainBag.bushelsPerTonne() * 0.907)) * 100) / 100; + } + return val + } + return ( @@ -87,11 +104,7 @@ export default function GrainBagInventoryGraph(props: Props) { } data={data} yMin={0} - yMax={ - getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT - ? grainBag.capacity() / grainBag.bushelsPerTonne() - : grainBag.capacity() - } + yMax={maxYAxis()} customHeight={customHeight} useGradient labels diff --git a/src/grainBag/grainBagSettings.tsx b/src/grainBag/grainBagSettings.tsx index 240c3d9..cf5f703 100644 --- a/src/grainBag/grainBagSettings.tsx +++ b/src/grainBag/grainBagSettings.tsx @@ -66,7 +66,7 @@ export default function GrainBagSettings(props: Props) { const theme = useTheme(); const [grainUpdate, setGrainUpdate] = useState(false); const [grainDiff, setGrainDiff] = useState(0); - const [bushelsPerTonne, setBushelsPerTonne] = useState(0); + const [bushelConversion, setBushelConversion] = useState(0); useEffect(() => { if (grainBag) { @@ -84,8 +84,10 @@ export default function GrainBagSettings(props: Props) { : grainBag.settings.length ); let grainVal = grainBag.bushels(); - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT) { + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE) { grainVal = grainBag.bushels() / grainBag.bushelsPerTonne(); + }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON) { + grainVal = grainBag.bushels() / (grainBag.bushelsPerTonne() * 0.907) } setGrainFill(grainVal.toFixed(2)); setBagName(grainBag.name()); @@ -117,6 +119,10 @@ export default function GrainBagSettings(props: Props) { const submit = () => { //if a bag was passed in do an update + let tonneConversion = bushelConversion + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ + tonneConversion = bushelConversion / 0.907 + } if (grainBag) { grainBag.title = bagName; grainBag.settings.initialMoisture = initialMoisture; @@ -127,7 +133,7 @@ export default function GrainBagSettings(props: Props) { grainBag.settings.bushelCapacity = grainCapacity; grainBag.settings.grainSubtype = grainSubtype; grainBag.settings.fillDate = fillDate; - grainBag.settings.bushelsPerTonne = bushelsPerTonne; + grainBag.settings.bushelsPerTonne = tonneConversion; grainBag.settings.storageType = isCustom ? pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN : pond.BinStorage.BIN_STORAGE_SUPPORTED_GRAIN; @@ -161,7 +167,7 @@ export default function GrainBagSettings(props: Props) { settings.currentBushels = grainBushels; settings.grainSubtype = grainSubtype; settings.fillDate = fillDate; - settings.bushelsPerTonne = bushelsPerTonne; + settings.bushelsPerTonne = tonneConversion; settings.storageType = isCustom ? pond.BinStorage.BIN_STORAGE_UNSUPPORTED_GRAIN : pond.BinStorage.BIN_STORAGE_SUPPORTED_GRAIN; @@ -206,10 +212,10 @@ export default function GrainBagSettings(props: Props) { /> { - setBushelsPerTonne(+event.target.value); + setBushelConversion(+event.target.value); }} fullWidth className={classes.bottomSpacing} @@ -228,7 +234,7 @@ export default function GrainBagSettings(props: Props) { ? pond.Grain[option.value as keyof typeof pond.Grain] : pond.Grain.GRAIN_INVALID; setSupportedGrainType(newGrainType); - setBushelsPerTonne(GrainDescriber(newGrainType).bushelsPerTonne); + setBushelConversion(GrainDescriber(newGrainType).bushelsPerTonne); setSelectedGrainOp(option); }} group @@ -247,6 +253,18 @@ export default function GrainBagSettings(props: Props) { return invalid; }; + const grainUnitDisplay = () => { + if(bushelConversion > 1){ + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE){ + return "mT" + }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON){ + return "t" + } + }else{ + return "bu" + } + } + return ( 0 - ? "Weight" - : "Bushels") - } + label={"Current Inventory"} fullWidth type="number" variant="outlined" InputProps={{ endAdornment: ( - {getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && bushelsPerTonne > 0 - ? "mT" - : "bu"} + {grainUnitDisplay()} ) }} value={grainFill} onChange={e => { let bushelVal = +e.target.value; - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT) { - //convert the number as a weight into the bushel value - bushelVal = +e.target.value * (bushelsPerTonne > 0 ? bushelsPerTonne : 1); + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE || getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON) { + //convert the number as a weight into the bushel value, no changing or conversions necessary + // since these are both user entered fields and should be the same unit (ton or tonne) + bushelVal = +e.target.value * (bushelConversion > 0 ? bushelConversion : 1); } if (bushelVal > grainCapacity) { bushelVal = grainCapacity; diff --git a/src/grainBag/grainBagVisualizer.tsx b/src/grainBag/grainBagVisualizer.tsx index 3c76f1f..1d0b5d8 100644 --- a/src/grainBag/grainBagVisualizer.tsx +++ b/src/grainBag/grainBagVisualizer.tsx @@ -83,12 +83,19 @@ export default function GrainBagVisualizer(props: Props) { const grainOverlay = () => { let displayPending = pendingGrainAmount; let displayDiff = grainDiff; - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && grainBag.bushelsPerTonne() > 1) { + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && grainBag.bushelsPerTonne() > 1) { if (displayPending && displayDiff) { displayPending = Math.round((displayPending / grainBag.bushelsPerTonne()) * 100) / 100; displayDiff = Math.round((displayDiff / grainBag.bushelsPerTonne()) * 100) / 100; } } + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainBag.bushelsPerTonne() > 1) { + if (displayPending && displayDiff) { + displayPending = Math.round((displayPending / (grainBag.bushelsPerTonne() * 0.907)) * 100) / 100; + displayDiff = Math.round((displayDiff / (grainBag.bushelsPerTonne()* 0.907)) * 100) / 100; + } + } + return ( {grainDiff !== undefined && grainDiff > 0 && ( @@ -141,7 +148,7 @@ export default function GrainBagVisualizer(props: Props) { }; const grainAmountDisplay = () => { - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && grainBag.bushelsPerTonne() > 1) { + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && grainBag.bushelsPerTonne() > 1) { return (
@@ -150,6 +157,15 @@ export default function GrainBagVisualizer(props: Props) { ({grainBag.fillPercent()}%)
); + } else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && grainBag.bushelsPerTonne() > 1) { + return ( +
+ + {(grainBag.bushels() / (grainBag.bushelsPerTonne()*0.907)).toFixed(2) + " t"} + + ({grainBag.fillPercent()}%) +
+ ); } else { return (
diff --git a/src/models/Bin.ts b/src/models/Bin.ts index cf0f5a6..e720fec 100644 --- a/src/models/Bin.ts +++ b/src/models/Bin.ts @@ -2,6 +2,7 @@ import GrainDescriber from "grain/GrainDescriber"; import { cloneDeep } from "lodash"; import { MarkerData } from "maps/mapMarkers/Markers"; import { pond } from "protobuf-ts/pond"; +import { getGrainUnit } from "utils"; import { stringToMaterialColour } from "utils/strings"; import { or } from "utils/types"; @@ -235,46 +236,14 @@ export class Bin { return bpt; } - /** - * returns the bushels per ton set in the bins settings, if not set will return 1 - * @returns 1 or bushels per tonne - */ - public bushelsPerTon(): number { - //trying to avoid a divide by 0 error by only returning a value greater than 0 - //since to get the weight you divide the current bushels by the bushels per tonne - let bpt = 1; - if (this.settings.inventory) { - if (this.settings.inventory.bushelsPerTon > 0) { - bpt = this.settings.inventory.bushelsPerTon; - } + public grainInventory(): number { + let grain = this.bushels() + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && this.bushelsPerTonne() > 1){ + grain = this.bushels() / this.bushelsPerTonne() + }else if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && this.bushelsPerTonne() > 1){ + grain = this.bushels() / (this.bushelsPerTonne() * 0.907) } - return bpt; - } - - /** - * gets the weight in tonnes (metric) for the grain inventory provided the bushels per tonne is set - * if it is not set it will divide by 1 effectively returning the bushels - * @returns grain weight - */ - public grainTonnes(): number { - let weight = 0; - if (this.settings.inventory) { - weight = this.settings.inventory.grainBushels / this.bushelsPerTonne(); - } - return Math.round(weight * 100) / 100; - } - - /** - * gets the weight in tons (imperial) for the grain inventory provided the bushels per ton is set - * if it is not set it will divide by 1 effectively returning the bushels - * @returns grain weight - */ - public grainTons(): number { - let weight = 0; - if (this.settings.inventory) { - weight = this.settings.inventory.grainBushels / this.bushelsPerTon(); - } - return Math.round(weight * 100) / 100; + return Math.round(grain*100)/100 } /** diff --git a/src/models/Contract.ts b/src/models/Contract.ts index 804f12f..ce55093 100644 --- a/src/models/Contract.ts +++ b/src/models/Contract.ts @@ -29,7 +29,13 @@ export class Contract { private measurementUnit(): string { if (this.settings.type === pond.ContractType.CONTRACT_TYPE_GRAIN) { - return getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT ? "mT" : "bu"; + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && this.conversionValue() > 1){ + return "mT" + } + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && this.conversionValue() > 1){ + return "t" + } + return "bu"; } return ""; } @@ -202,9 +208,12 @@ export class Contract { let size = this.settings.size; switch (this.settings.type) { case pond.ContractType.CONTRACT_TYPE_GRAIN: - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && this.conversionValue() > 1) { + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && this.conversionValue() > 1) { size = size / this.conversionValue(); } + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && this.conversionValue() > 1){ + size = size / (this.conversionValue() * 0.907); //the conversion for grain will be stored in metric tonnes, this will convert it to US tons + } } return Math.round(size * 100) / 100; } @@ -213,26 +222,33 @@ export class Contract { let del = this.settings.delivered; switch (this.settings.type) { case pond.ContractType.CONTRACT_TYPE_GRAIN: - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && this.conversionValue() > 1) { + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && this.conversionValue() > 1) { del = del / this.conversionValue(); } + if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && this.conversionValue() > 1){ + del = del / (this.conversionValue() * 0.907); //the conversion for grain will be stored in metric tonnes, this will convert it to US tons + } } return Math.round(del * 100) / 100; } + + public static toStoredUnit( secondaryUnitVal: number, contractType: pond.ContractType, conversionValue: number ): number { - let storedunitVal = secondaryUnitVal; + let storedUnitVal = secondaryUnitVal; switch (contractType) { + //use the value and conversion they entered directly, it is safe to assume they are both the same unit (ton vs tonne) so dont need to convert anything + //before converting to bushels case pond.ContractType.CONTRACT_TYPE_GRAIN: - if (getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_WEIGHT && conversionValue > 1) { - storedunitVal = secondaryUnitVal * conversionValue; + if ((getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE || getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON) && conversionValue > 1) { + storedUnitVal = secondaryUnitVal * conversionValue; } } - return storedunitVal; + return storedUnitVal; } public grainName(): string { diff --git a/src/transactions/transactionDataDisplay.tsx b/src/transactions/transactionDataDisplay.tsx index 873192b..b64db64 100644 --- a/src/transactions/transactionDataDisplay.tsx +++ b/src/transactions/transactionDataDisplay.tsx @@ -17,8 +17,8 @@ export default function TransactionDataDisplay(props: Props) { if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TONNE && gt.bushelsPerTonne > 1){ return "Weight: " + Math.round(gt.bushels / gt.bushelsPerTonne*100)/100 + " mT" } - if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && gt.bushelsPerTon > 1){ - return "Weight: " + Math.round(gt.bushels / gt.bushelsPerTon*100)/100 + " t" + if(getGrainUnit() === pond.GrainUnit.GRAIN_UNIT_TON && gt.bushelsPerTonne > 1){ + return "Weight: " + Math.round(gt.bushels / (gt.bushelsPerTonne*0.907)*100)/100 + " t" } return "Bushels: " + gt.bushels }