Purchase Records Module

SupplierPurchaseRecord entity, purchase status lifecycle, document tracking fields, delivery tracking, and automatic CO₂ calculation.

Entity: SupplierPurchaseRecord

FieldTypeDB ColumnDescription
idLongidAuto-generated PK
supplierIdLongsupplier_idFK to Supplier (required)
supplierProductIdLongsupplier_product_idFK to SupplierProduct (optional)
userIdLonguser_idOperator who created the record
purchaseDateLocalDateTimepurchase_dateFull datetime of purchase (replaces purchase_year)
productDescriptionStringproduct_descriptionFree-text description
quantityKgDoublequantity_kgQuantity in kg
transportModeStringtransport_modeROAD, SEA, AIR, RAIL
distanceKmDoubledistance_kmTransport distance in km
weightKgDoubleweight_kgShipment weight in kg
co2EmissionsKgDoubleco2_emissions_kgCalculated from transport fields
purchaseOrderNumberStringpurchase_order_numberPO reference, max 100 **
invoiceNumberStringinvoice_numberInvoice reference, max 100 **
ddtNumberStringddt_numberDDT delivery note, max 100 **
transactionCertificateNumberStringtransaction_certificate_numberGOTS/GRS TC number, max 100 **
batchNumberStringbatch_numberBatch/lot ID — unique per supplier_id **
deliveryDateLocalDateTimedelivery_dateActual delivery datetime **
expectedDeliveryDateLocalDateTimeexpected_delivery_dateExpected delivery datetime **
deliveryNotesStringdelivery_notesDelivery notes (max 2000) **
purchaseStatusPurchaseStatuspurchase_statusStatus enum, default ORDERED **
notesStringnotesGeneral notes
createdAtLocalDateTimecreated_atAuto-set
updatedAtLocalDateTimeupdated_atAuto-set

Purchase Status Lifecycle

Shell
ORDERED → IN_TRANSIT → DELIVERED
↘ ↗
CANCELLED ←──
StatusMeaning
ORDEREDPurchase placed, awaiting shipment. Default.
IN_TRANSITGoods dispatched from supplier
DELIVEREDGoods received at destination
CANCELLEDOrder cancelled

Entity: PurchaseStatusChangeLog

— Created automatically by the service whenever purchaseStatus changes. Not exposed via a REST endpoint yet.

FieldTypeDescription
purchaseIdLongFK to SupplierPurchaseRecord.id
fromStatusPurchaseStatusPrevious status (nullable for initial set)
toStatusPurchaseStatusNew status
changedByUserIdLongFrom X-User-Id header
changedAtLocalDateTimeAuto-set via @CreationTimestamp

Business Validations

  • purchaseDate must be between 1900-01-01T00:00:00 and 2100-12-31T23:59:59
  • deliveryDate must not be before purchaseDate
  • Document reference fields (purchaseOrderNumber, etc.) must match ^[\p{L}\p{N}][\p{L}\p{N} ./_-]{0,99}$
  • batchNumber is unique per supplier_id (enforced at DB level)

Automatic CO₂ Calculation

When transport_mode, distance_km, and weight_kg are provided, the service auto-calculates:

Java
co2 = emissionFactor[transportMode] × distanceKm × (weightKg / 1000.0)
ModeFactor (kg CO₂/tonne·km)
ROAD0.000096
SEA0.000011
AIR0.000602
RAIL0.000028