Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix emission allocation bugs identified in UAT #2637

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ interface FormData {
allocation_methodology: string;
allocation_other_methodology_description: string;
}
// Function that makes sure the percentage does not show 100 when it is not exactly 100
const handlePercentageNearHundred = (value: number) => {
let res;
if (value > 100.0 && value < 100.01) {
res = 100.01;
} else if (value < 100.0 && value > 99.99) {
res = 99.99;
} else {
res = value;
}

return parseFloat(res.toFixed(4));
};

// 🛠️ Function to calculate category products allocation sum and set total sum in products_emission_allocation_sum
const calculateEmissionData = (category: EmissionAllocationData) => {
Expand All @@ -52,7 +65,8 @@ const calculateEmissionData = (category: EmissionAllocationData) => {
);

const emissionTotal = Number(category.emission_total) || 1;
const percentage = (sum / emissionTotal) * 100;

const percentage = handlePercentageNearHundred((sum / emissionTotal) * 100);

return {
...category,
Expand Down Expand Up @@ -135,10 +149,9 @@ export default function FacilityEmissionAllocationForm({
// 🛠️ Handle changes to the form data, validates emissions, and updates the error state and submit button state.
const handleChange = useCallback((e: IChangeEvent) => {
const updatedFormData = e.formData;
const updatedDataKeys = [
"basic_emission_allocation_data",
"fuel_excluded_emission_allocation_data",
];
const BASIC_CATEGORY_DATA = "basic_emission_allocation_data";
const EXCLUDED_CATEGORY_DATA = "fuel_excluded_emission_allocation_data";
const updatedDataKeys = [BASIC_CATEGORY_DATA, EXCLUDED_CATEGORY_DATA];
let errorMessage;

// Initialize a map to store total allocated quantities by report_product_id
Expand All @@ -153,11 +166,12 @@ export default function FacilityEmissionAllocationForm({
const allocatedQuantity =
parseFloat(product.allocated_quantity as any) || 0;

// Accumulate the allocated quantity for this report_product_id
productAllocations[product.report_product_id] =
(productAllocations[product.report_product_id] || 0) +
allocatedQuantity;

// Accumulate the reportable allocated quantity for this report_product_id
if (key == BASIC_CATEGORY_DATA) {
productAllocations[product.report_product_id] =
(productAllocations[product.report_product_id] || 0) +
allocatedQuantity;
}
return {
...product,
allocated_quantity: String(allocatedQuantity),
Expand Down
Loading