"""Create inward receiving, weighbridge slips, and goods receipt notes tables with RLS. Revision ID: 007_inward_receiving Revises: 006_procurement_contracts Create Date: 2026-10-12 Standard: UP-AI v1.5 / LLD-003 / database_design.md Fulfills: FR-INWARDRECEIVING-001..005 (UC-005) """ from typing import Sequence, Union try: from alembic import op import sqlalchemy as sa except ImportError: op = None sa = None revision: str = "007_inward_receiving" down_revision: Union[str, None] = "006_procurement_contracts" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: if op and sa: # 1. Inward Gate Entries op.create_table( "inward_gate_entries", sa.Column("id", sa.String(36), primary_key=True), sa.Column("tenant_id", sa.String(36), nullable=False), sa.Column("gate_pass_number", sa.String(32), nullable=False, unique=True), sa.Column("gate_in_timestamp", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.Column("vehicle_number", sa.String(16), nullable=False), sa.Column("eway_bill_number", sa.String(12), nullable=False), sa.Column("driver_name", sa.String(100), nullable=False), sa.Column("driver_mobile", sa.String(10), nullable=False), sa.Column("sauda_id", sa.String(36), nullable=False), sa.Column("supplier_invoice_no", sa.String(64), nullable=False), sa.Column("supplier_dispatch_weight_mt", sa.Numeric(12, 3), nullable=False), sa.Column("supplier_dispatch_bags", sa.Integer(), nullable=False), sa.Column("is_seal_intact", sa.Boolean(), server_default=sa.text("true")), sa.Column("status", sa.String(32), server_default="GATE_IN") ) op.create_index("ix_gate_tenant", "inward_gate_entries", ["tenant_id"]) op.create_index("ix_gate_vehicle", "inward_gate_entries", ["vehicle_number"]) op.create_index("ix_gate_sauda", "inward_gate_entries", ["sauda_id"]) # 2. Weighbridge Slips op.create_table( "weighbridge_slips", sa.Column("id", sa.String(36), primary_key=True), sa.Column("tenant_id", sa.String(36), nullable=False), sa.Column("slip_number", sa.String(32), nullable=False, unique=True), sa.Column("gate_pass_id", sa.String(36), nullable=False), sa.Column("gross_weight_mt", sa.Numeric(12, 3), nullable=False), sa.Column("gross_timestamp", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.Column("tare_weight_mt", sa.Numeric(12, 3), nullable=True), sa.Column("tare_timestamp", sa.DateTime(timezone=True), nullable=True), sa.Column("unloaded_weight_mt", sa.Numeric(12, 3), nullable=True), sa.Column("bag_packaging_type", sa.String(32), nullable=True), sa.Column("bag_tare_rate_kg", sa.Numeric(6, 3), nullable=True), sa.Column("actual_bag_count", sa.Integer(), server_default="0"), sa.Column("bag_tare_deduction_mt", sa.Numeric(12, 3), server_default="0.0"), sa.Column("final_net_weight_mt", sa.Numeric(12, 3), nullable=True), sa.Column("shortage_weight_mt", sa.Numeric(12, 3), nullable=True), sa.Column("shortage_pct", sa.Numeric(5, 2), nullable=True), sa.Column("is_shortage_exceeded", sa.Boolean(), server_default=sa.text("false")) ) op.create_index("ix_weighment_tenant", "weighbridge_slips", ["tenant_id"]) op.create_index("ix_weighment_gate_pass", "weighbridge_slips", ["gate_pass_id"]) # 3. Goods Receipt Notes op.create_table( "goods_receipt_notes", sa.Column("id", sa.String(36), primary_key=True), sa.Column("tenant_id", sa.String(36), nullable=False), sa.Column("grn_number", sa.String(32), nullable=False, unique=True), sa.Column("grn_date", sa.DateTime(timezone=True), server_default=sa.func.now()), sa.Column("weighment_slip_id", sa.String(36), nullable=False), sa.Column("sauda_id", sa.String(36), nullable=False), sa.Column("godown_id", sa.String(36), nullable=False), sa.Column("godown_stack_id", sa.String(36), nullable=False), sa.Column("accepted_quantity_mt", sa.Numeric(12, 3), nullable=False), sa.Column("accepted_bags", sa.Integer(), nullable=False), sa.Column("status", sa.String(32), server_default="POSTED") ) op.create_index("ix_grn_tenant", "goods_receipt_notes", ["tenant_id"]) op.create_index("ix_grn_sauda", "goods_receipt_notes", ["sauda_id"]) conn = op.get_bind() if conn.dialect.name == "postgresql": for tbl in ["inward_gate_entries", "weighbridge_slips", "goods_receipt_notes"]: op.execute(f"ALTER TABLE {tbl} ENABLE ROW LEVEL SECURITY;") op.execute(f"ALTER TABLE {tbl} FORCE ROW LEVEL SECURITY;") op.execute(f""" CREATE POLICY {tbl}_tenant_isolation ON {tbl} FOR ALL USING (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')) WITH CHECK (tenant_id = NULLIF(current_setting('app.current_tenant_id', true), '')); """) def downgrade() -> None: if op: op.drop_table("goods_receipt_notes") op.drop_table("weighbridge_slips") op.drop_table("inward_gate_entries")