Fix UI issues 2
This commit is contained in:
@@ -162,7 +162,8 @@ const EnvironmentalConstants = {
|
||||
LAND_USE_PER_KG: 3.4, // m² per kg
|
||||
TREES_PER_TON_CO2: 50,
|
||||
SDG_TARGET_REDUCTION: 0.50, // 50% reduction target
|
||||
EU_BAKERY_BASELINE_WASTE: 0.25 // 25% baseline
|
||||
EU_BAKERY_BASELINE_WASTE: 0.25, // 25% baseline
|
||||
MINIMUM_PRODUCTION_KG: 50 // Minimum production to show meaningful metrics
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -320,6 +321,78 @@ function assessGrantReadiness(sdgCompliance: SDGCompliance): GrantReadiness {
|
||||
|
||||
// ===== MAIN AGGREGATION FUNCTION =====
|
||||
|
||||
/**
|
||||
* Get default metrics for insufficient data state
|
||||
*/
|
||||
function getInsufficientDataMetrics(
|
||||
totalProductionKg: number,
|
||||
startDate?: string,
|
||||
endDate?: string
|
||||
): SustainabilityMetrics {
|
||||
return {
|
||||
period: {
|
||||
start_date: startDate || new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(),
|
||||
end_date: endDate || new Date().toISOString(),
|
||||
days: 30
|
||||
},
|
||||
waste_metrics: {
|
||||
total_waste_kg: 0,
|
||||
production_waste_kg: 0,
|
||||
expired_waste_kg: 0,
|
||||
waste_percentage: 0,
|
||||
waste_by_reason: {}
|
||||
},
|
||||
environmental_impact: {
|
||||
co2_emissions: { kg: 0, tons: 0, trees_to_offset: 0 },
|
||||
water_footprint: { liters: 0, cubic_meters: 0 },
|
||||
land_use: { square_meters: 0, hectares: 0 },
|
||||
human_equivalents: { car_km_equivalent: 0, smartphone_charges: 0, showers_equivalent: 0, trees_planted: 0 }
|
||||
},
|
||||
sdg_compliance: {
|
||||
sdg_12_3: {
|
||||
baseline_waste_percentage: 0,
|
||||
current_waste_percentage: 0,
|
||||
reduction_achieved: 0,
|
||||
target_reduction: 50,
|
||||
progress_to_target: 0,
|
||||
status: 'baseline',
|
||||
status_label: 'Collecting Baseline Data',
|
||||
target_waste_percentage: 0
|
||||
},
|
||||
baseline_period: 'not_available',
|
||||
certification_ready: false,
|
||||
improvement_areas: ['start_production_tracking']
|
||||
},
|
||||
avoided_waste: {
|
||||
waste_avoided_kg: 0,
|
||||
ai_assisted_batches: 0,
|
||||
environmental_impact_avoided: { co2_kg: 0, water_liters: 0 },
|
||||
methodology: 'insufficient_data'
|
||||
},
|
||||
financial_impact: {
|
||||
waste_cost_eur: 0,
|
||||
cost_per_kg: 3.50,
|
||||
potential_monthly_savings: 0,
|
||||
annual_projection: 0
|
||||
},
|
||||
grant_readiness: {
|
||||
overall_readiness_percentage: 0,
|
||||
grant_programs: {
|
||||
life_circular_economy: { eligible: false, confidence: 'low', requirements_met: false, funding_eur: 73_000_000 },
|
||||
horizon_europe_cluster_6: { eligible: false, confidence: 'low', requirements_met: false, funding_eur: 880_000_000 },
|
||||
fedima_sustainability_grant: { eligible: false, confidence: 'low', requirements_met: false, funding_eur: 20_000 },
|
||||
eit_food_retail: { eligible: false, confidence: 'low', requirements_met: false, funding_eur: 45_000 },
|
||||
un_sdg_certified: { eligible: false, confidence: 'low', requirements_met: false, funding_eur: 0 }
|
||||
},
|
||||
recommended_applications: [],
|
||||
spain_compliance: { law_1_2025: false, circular_economy_strategy: false }
|
||||
},
|
||||
data_sufficient: false,
|
||||
minimum_production_required_kg: EnvironmentalConstants.MINIMUM_PRODUCTION_KG,
|
||||
current_production_kg: totalProductionKg
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get comprehensive sustainability metrics by aggregating production and inventory data
|
||||
*/
|
||||
@@ -337,11 +410,22 @@ export async function getSustainabilityMetrics(
|
||||
getProductionAIImpact(tenantId, startDate, endDate)
|
||||
]);
|
||||
|
||||
// Calculate total production
|
||||
const totalProductionKg = productionData.total_planned || 0;
|
||||
|
||||
// Check if we have sufficient data for meaningful metrics
|
||||
// Minimum: 50kg production to avoid false metrics on empty accounts
|
||||
const hasDataSufficient = totalProductionKg >= EnvironmentalConstants.MINIMUM_PRODUCTION_KG;
|
||||
|
||||
// If insufficient data, return a "collecting data" state
|
||||
if (!hasDataSufficient) {
|
||||
return getInsufficientDataMetrics(totalProductionKg, startDate, endDate);
|
||||
}
|
||||
|
||||
// Aggregate waste metrics
|
||||
const productionWaste = (productionData.total_production_waste || 0) + (productionData.total_defects || 0);
|
||||
const totalWasteKg = productionWaste + (inventoryData.inventory_waste_kg || 0);
|
||||
|
||||
const totalProductionKg = productionData.total_planned || 0;
|
||||
const wastePercentage = totalProductionKg > 0
|
||||
? (totalWasteKg / totalProductionKg) * 100
|
||||
: 0;
|
||||
@@ -403,7 +487,10 @@ export async function getSustainabilityMetrics(
|
||||
sdg_compliance: sdgCompliance,
|
||||
avoided_waste: avoidedWaste,
|
||||
financial_impact: financialImpact,
|
||||
grant_readiness: grantReadiness
|
||||
grant_readiness: grantReadiness,
|
||||
data_sufficient: true,
|
||||
minimum_production_required_kg: EnvironmentalConstants.MINIMUM_PRODUCTION_KG,
|
||||
current_production_kg: totalProductionKg
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
|
||||
@@ -114,6 +114,10 @@ export interface SustainabilityMetrics {
|
||||
avoided_waste: AvoidedWaste;
|
||||
financial_impact: FinancialImpact;
|
||||
grant_readiness: GrantReadiness;
|
||||
// Data sufficiency flags
|
||||
data_sufficient: boolean;
|
||||
minimum_production_required_kg?: number;
|
||||
current_production_kg?: number;
|
||||
}
|
||||
|
||||
export interface SustainabilityWidgetData {
|
||||
|
||||
Reference in New Issue
Block a user