152 lines
6.2 KiB
Python
152 lines
6.2 KiB
Python
"""APMC Mandi Statutory Transit Permits (Anugya Patra) and Tax Ledger Models.
|
|
|
|
Fulfills Phase 2 requirements BRD-FR-056, BRD-FR-057, and BRD-FR-058 (UC-028).
|
|
Adheres to UP-AI v1.5 standards.
|
|
"""
|
|
from datetime import datetime, timezone
|
|
from decimal import Decimal, ROUND_HALF_UP
|
|
from typing import Optional, Dict, Any
|
|
import uuid
|
|
|
|
try:
|
|
from sqlalchemy import Column, String, DateTime, Numeric, Integer, Boolean
|
|
from backend.app.models.user import Base
|
|
except ImportError:
|
|
class Base:
|
|
pass
|
|
Column = String = DateTime = Numeric = Integer = Boolean = lambda *args, **kwargs: None
|
|
|
|
PERMIT_STATUSES = {"ISSUED", "IN_TRANSIT", "DELIVERED", "EXPIRED", "CANCELLED"}
|
|
|
|
|
|
class MandiTransitPermit(Base):
|
|
"""Statutory Mandi Samiti Transit Permit (Anugya Patra / Form M)."""
|
|
__tablename__ = "mandi_transit_permits"
|
|
__table_args__ = {"schema": "public"}
|
|
|
|
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
tenant_id = Column(String(36), nullable=False, index=True)
|
|
permit_number = Column(String(32), nullable=False, unique=True, index=True)
|
|
mandi_samiti_code = Column(String(32), nullable=False)
|
|
dispatch_id = Column(String(36), nullable=False, index=True)
|
|
vehicle_reg_no = Column(String(16), nullable=False)
|
|
commodity_id = Column(String(36), nullable=False)
|
|
quantity_mt = Column(Numeric(12, 3), nullable=False)
|
|
bag_count = Column(Integer, nullable=False)
|
|
destination_city = Column(String(64), nullable=False)
|
|
issued_at = Column(DateTime, nullable=False)
|
|
valid_until = Column(DateTime, nullable=False)
|
|
tax_paid_status = Column(String(32), default="SHULK_CHHUKTA")
|
|
status = Column(String(32), default="ISSUED")
|
|
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
|
|
|
|
def __init__(
|
|
self,
|
|
tenant_id: str,
|
|
permit_number: str,
|
|
mandi_samiti_code: str,
|
|
dispatch_id: str,
|
|
vehicle_reg_no: str,
|
|
commodity_id: str,
|
|
quantity_mt: Decimal,
|
|
bag_count: int,
|
|
destination_city: str,
|
|
issued_at: datetime,
|
|
valid_until: datetime,
|
|
tax_paid_status: str = "SHULK_CHHUKTA",
|
|
permit_id: Optional[str] = None,
|
|
):
|
|
if quantity_mt <= Decimal("0.000"):
|
|
raise ValueError("Permit quantity MT must be > 0.0")
|
|
if bag_count <= 0:
|
|
raise ValueError("Permit bag count must be > 0")
|
|
if valid_until <= issued_at:
|
|
raise ValueError("Permit valid_until must be strictly after issued_at")
|
|
|
|
self.id = permit_id or str(uuid.uuid4())
|
|
self.tenant_id = tenant_id
|
|
self.permit_number = permit_number
|
|
self.mandi_samiti_code = mandi_samiti_code
|
|
self.dispatch_id = dispatch_id
|
|
self.vehicle_reg_no = vehicle_reg_no.replace("-", "").replace(" ", "").upper()
|
|
self.commodity_id = commodity_id
|
|
self.quantity_mt = quantity_mt
|
|
self.bag_count = bag_count
|
|
self.destination_city = destination_city
|
|
self.issued_at = issued_at
|
|
self.valid_until = valid_until
|
|
self.tax_paid_status = tax_paid_status
|
|
self.status = "ISSUED"
|
|
self.created_at = datetime.now(timezone.utc)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"id": self.id,
|
|
"tenant_id": self.tenant_id,
|
|
"permit_number": self.permit_number,
|
|
"mandi_samiti_code": self.mandi_samiti_code,
|
|
"dispatch_id": self.dispatch_id,
|
|
"vehicle_reg_no": self.vehicle_reg_no,
|
|
"commodity_id": self.commodity_id,
|
|
"quantity_mt": float(self.quantity_mt),
|
|
"bag_count": self.bag_count,
|
|
"destination_city": self.destination_city,
|
|
"issued_at": self.issued_at.isoformat() if self.issued_at else None,
|
|
"valid_until": self.valid_until.isoformat() if self.valid_until else None,
|
|
"tax_paid_status": self.tax_paid_status,
|
|
"status": self.status,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
}
|
|
|
|
|
|
class MandiTaxPaidLedger(Base):
|
|
"""Running ledger tracking single-point Mandi Tax Paid (Shulk Chhukta) volume."""
|
|
__tablename__ = "mandi_tax_paid_ledger"
|
|
__table_args__ = {"schema": "public"}
|
|
|
|
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
tenant_id = Column(String(36), nullable=False, index=True)
|
|
commodity_id = Column(String(36), nullable=False, index=True)
|
|
opening_tax_paid_mt = Column(Numeric(12, 3), nullable=False)
|
|
inward_tax_paid_mt = Column(Numeric(12, 3), default=0.0)
|
|
dispatched_tax_paid_mt = Column(Numeric(12, 3), default=0.0)
|
|
closing_tax_paid_mt = Column(Numeric(12, 3), nullable=False)
|
|
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
|
|
|
|
def __init__(
|
|
self,
|
|
tenant_id: str,
|
|
commodity_id: str,
|
|
opening_tax_paid_mt: Decimal,
|
|
inward_tax_paid_mt: Decimal = Decimal("0.000"),
|
|
dispatched_tax_paid_mt: Decimal = Decimal("0.000"),
|
|
ledger_id: Optional[str] = None,
|
|
):
|
|
closing = opening_tax_paid_mt + inward_tax_paid_mt - dispatched_tax_paid_mt
|
|
if closing < Decimal("0.000"):
|
|
raise ValueError(
|
|
f"Tax-paid stock invariant violated: closing balance {closing} MT cannot be negative"
|
|
)
|
|
|
|
self.id = ledger_id or str(uuid.uuid4())
|
|
self.tenant_id = tenant_id
|
|
self.commodity_id = commodity_id
|
|
self.opening_tax_paid_mt = opening_tax_paid_mt
|
|
self.inward_tax_paid_mt = inward_tax_paid_mt
|
|
self.dispatched_tax_paid_mt = dispatched_tax_paid_mt
|
|
self.closing_tax_paid_mt = closing
|
|
self.created_at = datetime.now(timezone.utc)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"id": self.id,
|
|
"tenant_id": self.tenant_id,
|
|
"commodity_id": self.commodity_id,
|
|
"opening_tax_paid_mt": float(self.opening_tax_paid_mt),
|
|
"inward_tax_paid_mt": float(self.inward_tax_paid_mt),
|
|
"dispatched_tax_paid_mt": float(self.dispatched_tax_paid_mt),
|
|
"closing_tax_paid_mt": float(self.closing_tax_paid_mt),
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
}
|
|
|