Traceability Module

Track product lifecycle through TraceabilityRecords and ProcessingStages with automatic CO₂ and distance aggregation.

Overview

The traceability module provides two complementary mechanisms:

  1. TraceabilityRecord — lightweight records linking a product to a batch, origin, destination, and compliance status
  2. ProductTraceability + ProcessingStage — detailed supply chain tracking with ordered processing stages, automatic distance and CO₂ aggregation

Entities

TraceabilityRecord

Simple traceability records for tracking product movement and compliance.

FieldTypeDescription
idLongAuto PK
productIdStringAssociated product identifier
batchNumberStringBatch/lot number
originStringOrigin location
destinationStringDestination location
supplierIdLongFK to Supplier.id
processingFacilityStringName of processing facility
certificationsStringCertifications held
isCompliantBooleanCompliance flag
userIdLongCreator user ID
companyIdLongCompany ID
notesStringNotes
metadataMap<String, Object>Custom JSON metadata
createdAt / updatedAtLocalDateTimeTimestamps

ProductTraceability

The aggregate root for detailed product traceability with processing stages.

FieldTypeDescription
idLongAuto PK
userIdLongCreator user ID
productIdStringAssociated product ID
pfcrCalculationIdStringExternal calculation reference
traceabilityNameStringName for this traceability record
descriptionStringDescription
totalCo2PerKgDoubleSum of all stage CO₂ (auto-recalculated)
totalDistanceKmDoubleSum of all stage distances (auto-recalculated)
processingStagesList<ProcessingStage>Ordered child stages (@OneToMany)
metadataMap<String, Object>Custom JSON metadata
createdAt / updatedAtLocalDateTimeTimestamps

ProcessingStage

Individual processing steps in a product's supply chain.

FieldTypeDescription
idLongAuto PK
traceabilityIdLongFK to ProductTraceability.id
userIdLongCreator user ID
sequenceOrderIntegerPosition in the processing chain
stageNameStringe.g. Spinning, Dyeing, Cutting
companyTypeStringType of company performing the stage
supplierIdLongSupplier performing this stage
companyIdLongCompany performing this stage
internalProcessBooleanWhether this is an internal process
transportModeStringTransport mode to this stage
startsFromCompanyBooleanWhether transport starts from company
distanceKmDoubleTransport distance from previous stage
co2EmissionsDoubleCO₂ emissions for this stage
certificationsSnapshotStringCertifications at this stage
createdAt / updatedAtLocalDateTimeTimestamps

Automatic Totals Recalculation

Whenever a ProcessingStage is added, updated, or deleted, ProductTraceabilityCalculationService automatically recalculates the parent ProductTraceability totals:

  • totalDistanceKm = sum of all child distanceKm
  • totalCo2PerKg = sum of all child co2Emissions

This happens transparently in the service layer — no manual recalculation is needed.

Distance & Emission Calculation

The GeolocationService computes distances using the Haversine formula when company/supplier coordinates are available. The EmissionCalculationService applies configurable emission factors per transport mode:

Transport ModeDefault Factor (g CO₂ / tonne·km)
TRUCK96
SHIP11
TRAIN28
PLANE602

These defaults are configured in application.properties under traceability.emission.factor.*.

API Reference