c_agri_trade_code/sandbox/weighbridge_mock.py

68 lines
3.0 KiB
Python

"""Wave 0 Weighbridge Scale Mock Adapter for Iteration 03.
Simulates offline and automated digital scale captures for incoming agricultural produce trucks,
including bag tare calculation (Bardana Katai) with 3-decimal MT precision.
"""
from typing import Dict, Any, Tuple
from decimal import Decimal, ROUND_HALF_UP
BAG_TARE_RATES_KG = {
"JUTE_GUNNY_NEW": Decimal("0.650"),
"JUTE_GUNNY_OLD": Decimal("0.700"),
"HDPE_PLASTIC": Decimal("0.120"),
"BULK_LOOSE": Decimal("0.000")
}
class WeighbridgeMock:
"""Mock weighing terminal calculating gross/tare weighments and packaging deductions."""
@classmethod
def calculate_bag_tare(cls, packaging_type: str, bag_count: int, override_rate_kg: Decimal = None) -> Decimal:
"""Calculates bag tare (Bardana Katai) deduction in metric tonnes with 3-decimal precision."""
if packaging_type == "BULK_LOOSE" or bag_count <= 0:
return Decimal("0.000")
rate_kg = override_rate_kg if override_rate_kg is not None else BAG_TARE_RATES_KG.get(packaging_type, Decimal("0.650"))
total_kg = Decimal(str(bag_count)) * rate_kg
tare_mt = total_kg / Decimal("1000.0")
return tare_mt.quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
@classmethod
def process_weighment(cls, gross_weight_mt: Decimal, tare_weight_mt: Decimal,
packaging_type: str, bag_count: int,
billed_weight_mt: Decimal,
tolerance_pct: Decimal = Decimal("0.50")) -> Dict[str, Any]:
"""Processes complete weighbridge calculation."""
gross = Decimal(str(gross_weight_mt))
tare = Decimal(str(tare_weight_mt))
if tare >= gross:
raise ValueError("Tare weight must be strictly less than Gross weight")
unloaded_mt = (gross - tare).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
bag_tare_mt = cls.calculate_bag_tare(packaging_type, bag_count)
final_net_mt = (unloaded_mt - bag_tare_mt).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
billed = Decimal(str(billed_weight_mt))
shortage_mt = (final_net_mt - billed).quantize(Decimal("0.001"), rounding=ROUND_HALF_UP)
shortage_pct = ((shortage_mt / billed) * Decimal("100.0")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) if billed > Decimal("0.000") else Decimal("0.00")
# Negative shortage means weight loss in transit
is_exceeded = (shortage_mt < Decimal("0.000")) and (abs(shortage_pct) > tolerance_pct)
return {
"gross_weight_mt": float(gross),
"tare_weight_mt": float(tare),
"unloaded_weight_mt": float(unloaded_mt),
"bag_packaging_type": packaging_type,
"actual_bag_count": bag_count,
"bag_tare_deduction_mt": float(bag_tare_mt),
"final_net_weight_mt": float(final_net_mt),
"shortage_weight_mt": float(shortage_mt),
"shortage_pct": float(shortage_pct),
"is_shortage_exceeded": is_exceeded
}