from __future__ import annotations

from sqlalchemy import JSON, BigInteger, Date, DateTime, DECIMAL, ForeignKey, Integer, String, UniqueConstraint, text
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class Supplier(Base):
    __tablename__ = "md_supplier"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    supplier_code: Mapped[str] = mapped_column(String(50))
    supplier_name: Mapped[str] = mapped_column(String(200))
    short_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    contact_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    contact_phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
    address: Mapped[str | None] = mapped_column(String(255), nullable=True)
    lead_time_days: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    default_tax_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Process(Base):
    __tablename__ = "md_process"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    process_code: Mapped[str] = mapped_column(String(50))
    process_name: Mapped[str] = mapped_column(String(100))
    process_type: Mapped[str] = mapped_column(String(32))
    is_report_required: Mapped[int] = mapped_column(Integer, server_default=text("1"))
    is_quality_gate: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class WorkCenter(Base):
    __tablename__ = "md_work_center"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    center_code: Mapped[str] = mapped_column(String(50))
    center_name: Mapped[str] = mapped_column(String(100))
    dept_id: Mapped[int] = mapped_column(ForeignKey("sys_department.id"))
    center_type: Mapped[str] = mapped_column(String(50))
    shift_pattern: Mapped[str | None] = mapped_column(String(50), nullable=True)
    capacity_hours_per_day: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ProcessRoute(Base):
    __tablename__ = "md_process_route"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    route_code: Mapped[str] = mapped_column(String(50))
    route_name: Mapped[str] = mapped_column(String(100))
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    version_no: Mapped[str] = mapped_column(String(50))
    is_default: Mapped[int] = mapped_column(Integer, server_default=text("1"))
    status: Mapped[str] = mapped_column(String(32))
    effective_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    expire_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ProcessRouteOperation(Base):
    __tablename__ = "md_process_route_operation"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    route_id: Mapped[int] = mapped_column(ForeignKey("md_process_route.id"))
    seq_no: Mapped[int] = mapped_column(Integer)
    process_id: Mapped[int] = mapped_column(BigInteger)
    work_center_id: Mapped[int] = mapped_column(ForeignKey("md_work_center.id"))
    operation_name: Mapped[str] = mapped_column(String(100))
    std_setup_minutes: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    std_cycle_seconds: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    std_labor_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    std_machine_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    std_outsource_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    allowed_scrap_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    report_required: Mapped[int] = mapped_column(Integer, server_default=text("1"))
    is_key_operation: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Equipment(Base):
    __tablename__ = "em_equipment"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    equipment_code: Mapped[str] = mapped_column(String(50))
    equipment_name: Mapped[str] = mapped_column(String(100))
    category_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    work_center_id: Mapped[int] = mapped_column(ForeignKey("md_work_center.id"))
    manufacturer: Mapped[str | None] = mapped_column(String(100), nullable=True)
    model_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    install_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    rated_power_kw: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    occupied_area_sqm: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    maintenance_cycle_hours: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    last_maintenance_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    next_maintenance_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MaintenancePlan(Base):
    __tablename__ = "em_maintenance_plan"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    plan_code: Mapped[str] = mapped_column(String(50))
    equipment_id: Mapped[int] = mapped_column(ForeignKey("em_equipment.id"))
    plan_name: Mapped[str] = mapped_column(String(100))
    maintenance_type: Mapped[str] = mapped_column(String(32))
    cycle_hours: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    cycle_days: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    last_execute_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    next_execute_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MaintenanceOrder(Base):
    __tablename__ = "em_maintenance_order"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    order_no: Mapped[str] = mapped_column(String(50))
    equipment_id: Mapped[int] = mapped_column(ForeignKey("em_equipment.id"))
    plan_id: Mapped[int | None] = mapped_column(ForeignKey("em_maintenance_plan.id"), nullable=True)
    maintenance_type: Mapped[str] = mapped_column(String(32))
    start_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    end_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    downtime_minutes: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    owner_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    result_summary: Mapped[str | None] = mapped_column(String(255), nullable=True)
    cost_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseOrder(Base):
    __tablename__ = "po_purchase_order"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    po_no: Mapped[str] = mapped_column(String(50))
    supplier_id: Mapped[int] = mapped_column(ForeignKey("md_supplier.id"))
    order_date: Mapped[object] = mapped_column(Date)
    expected_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    warning_lead_days: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    purchaser_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    target_warehouse_type: Mapped[str] = mapped_column(String(32), server_default=text("'RAW'"))
    tax_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    total_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseOrderSalesOrderLink(Base):
    __tablename__ = "po_purchase_order_sales_order"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    purchase_order_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_order.id"))
    sales_order_id: Mapped[int] = mapped_column(ForeignKey("so_sales_order.id"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseOrderItem(Base):
    __tablename__ = "po_purchase_order_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    purchase_order_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_order.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    source_demand_id: Mapped[int | None] = mapped_column(ForeignKey("mrp_material_demand.id"), nullable=True)
    order_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    order_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    received_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    received_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_price: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    line_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class WarehouseLocation(Base):
    __tablename__ = "wh_location"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    location_code: Mapped[str] = mapped_column(String(50))
    location_name: Mapped[str] = mapped_column(String(100))
    zone_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    is_default: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    is_locked: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseReceipt(Base):
    __tablename__ = "po_receipt"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    receipt_no: Mapped[str] = mapped_column(String(50))
    purchase_order_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_order.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    receipt_date: Mapped[object] = mapped_column(DateTime)
    receiver_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    supplier_delivery_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    logistics_waybill_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    logistics_freight_amount: Mapped[float | None] = mapped_column(DECIMAL(18, 2), nullable=True)
    logistics_photo_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseReceiptItem(Base):
    __tablename__ = "po_receipt_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    receipt_id: Mapped[int] = mapped_column(ForeignKey("po_receipt.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    purchase_order_item_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_order_item.id"))
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    location_id: Mapped[int | None] = mapped_column(ForeignKey("wh_location.id"), nullable=True)
    lot_no: Mapped[str] = mapped_column(String(50))
    merge_to_lot_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    received_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    received_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    accepted_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    accepted_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseReturn(Base):
    __tablename__ = "po_purchase_return"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    return_no: Mapped[str] = mapped_column(String(50), unique=True)
    purchase_order_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_order.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    return_date: Mapped[object] = mapped_column(DateTime)
    operator_user_id: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    logistics_waybill_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    logistics_freight_amount: Mapped[float | None] = mapped_column(DECIMAL(18, 2), nullable=True)
    logistics_photo_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    reason: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class PurchaseReturnItem(Base):
    __tablename__ = "po_purchase_return_item"
    __table_args__ = (
        UniqueConstraint("purchase_return_id", "line_no", name="uk_po_purchase_return_item_line"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    purchase_return_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_return.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    purchase_order_item_id: Mapped[int] = mapped_column(ForeignKey("po_purchase_order_item.id"))
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    lot_id: Mapped[int] = mapped_column(ForeignKey("wh_stock_lot.id"))
    return_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    reason: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class StockLot(Base):
    __tablename__ = "wh_stock_lot"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    lot_no: Mapped[str] = mapped_column(String(50))
    parent_lot_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    lot_role: Mapped[str] = mapped_column(String(32), server_default=text("'INVENTORY'"))
    material_sub_batch_no: Mapped[str | None] = mapped_column(String(80), nullable=True)
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    location_id: Mapped[int | None] = mapped_column(ForeignKey("wh_location.id"), nullable=True)
    source_doc_type: Mapped[str] = mapped_column(String(32))
    source_doc_id: Mapped[int] = mapped_column(BigInteger)
    source_line_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    source_material_lot_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    source_material_sub_batch_no: Mapped[str | None] = mapped_column(String(80), nullable=True)
    source_material_summary: Mapped[str | None] = mapped_column(String(1000), nullable=True)
    logistics_waybill_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    logistics_freight_amount: Mapped[float | None] = mapped_column(DECIMAL(18, 2), nullable=True)
    logistics_photo_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    inbound_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    inbound_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    remaining_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    remaining_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    locked_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    locked_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    production_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    expire_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    quality_status: Mapped[str] = mapped_column(String(32))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class InventoryTxn(Base):
    __tablename__ = "wh_inventory_txn"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    txn_no: Mapped[str] = mapped_column(String(50))
    txn_type: Mapped[str] = mapped_column(String(32))
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    location_id: Mapped[int | None] = mapped_column(ForeignKey("wh_location.id"), nullable=True)
    lot_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stock_lot.id"), nullable=True)
    qty_change: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    weight_change_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    source_doc_type: Mapped[str] = mapped_column(String(32))
    source_doc_id: Mapped[int] = mapped_column(BigInteger)
    source_line_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    logistics_waybill_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    logistics_freight_amount: Mapped[float | None] = mapped_column(DECIMAL(18, 2), nullable=True)
    logistics_photo_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    biz_time: Mapped[object] = mapped_column(DateTime)
    operator_user_id: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class SpecialWarehouseAdjustment(Base):
    __tablename__ = "wh_special_adjustment"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    adjustment_no: Mapped[str] = mapped_column(String(50), unique=True)
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    warehouse_type: Mapped[str] = mapped_column(String(32))
    direction: Mapped[str] = mapped_column(String(32))
    reason: Mapped[str] = mapped_column(String(1000))
    status: Mapped[str] = mapped_column(String(32))
    operator_user_id: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    adjusted_at: Mapped[object] = mapped_column(DateTime)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class SpecialWarehouseAdjustmentLine(Base):
    __tablename__ = "wh_special_adjustment_line"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    adjustment_id: Mapped[int] = mapped_column(ForeignKey("wh_special_adjustment.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    location_id: Mapped[int | None] = mapped_column(ForeignKey("wh_location.id"), nullable=True)
    lot_id: Mapped[int] = mapped_column(ForeignKey("wh_stock_lot.id"))
    inventory_txn_id: Mapped[int | None] = mapped_column(ForeignKey("wh_inventory_txn.id"), nullable=True)
    before_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    change_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    after_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    before_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    change_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    after_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    line_reason: Mapped[str | None] = mapped_column(String(1000), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Stocktake(Base):
    __tablename__ = "wh_stocktake"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    stocktake_no: Mapped[str] = mapped_column(String(50))
    status: Mapped[str] = mapped_column(String(32))
    started_by_user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    imported_by_user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    confirmed_by_user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    canceled_by_user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    started_at: Mapped[object] = mapped_column(DateTime)
    exported_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    imported_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    confirmed_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    canceled_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
    updated_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))


class StocktakeWarehouse(Base):
    __tablename__ = "wh_stocktake_warehouse"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    stocktake_id: Mapped[int] = mapped_column(ForeignKey("wh_stocktake.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    warehouse_type: Mapped[str] = mapped_column(String(32))
    warehouse_name: Mapped[str] = mapped_column(String(100))
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
    updated_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))


class StocktakeLine(Base):
    __tablename__ = "wh_stocktake_line"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    stocktake_id: Mapped[int] = mapped_column(ForeignKey("wh_stocktake.id"))
    stocktake_warehouse_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stocktake_warehouse.id"), nullable=True)
    line_no: Mapped[int] = mapped_column(Integer)
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    warehouse_name: Mapped[str] = mapped_column(String(100))
    warehouse_type: Mapped[str] = mapped_column(String(32))
    location_id: Mapped[int | None] = mapped_column(ForeignKey("wh_location.id"), nullable=True)
    location_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    item_code: Mapped[str] = mapped_column(String(50))
    item_name: Mapped[str] = mapped_column(String(200))
    item_type: Mapped[str] = mapped_column(String(32))
    lot_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stock_lot.id"), nullable=True)
    lot_no: Mapped[str | None] = mapped_column(String(80), nullable=True)
    source_material_sub_batch_no: Mapped[str | None] = mapped_column(String(80), nullable=True)
    snapshot_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    snapshot_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    snapshot_unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    counted_qty: Mapped[float | None] = mapped_column(DECIMAL(18, 6), nullable=True)
    counted_weight_kg: Mapped[float | None] = mapped_column(DECIMAL(18, 6), nullable=True)
    diff_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    diff_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    diff_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    diff_type: Mapped[str] = mapped_column(String(32))
    row_status: Mapped[str] = mapped_column(String(32))
    error_message: Mapped[str | None] = mapped_column(String(500), nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
    updated_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))


class StocktakeAdjustment(Base):
    __tablename__ = "wh_stocktake_adjustment"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    stocktake_id: Mapped[int] = mapped_column(ForeignKey("wh_stocktake.id"))
    stocktake_line_id: Mapped[int] = mapped_column(ForeignKey("wh_stocktake_line.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    lot_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stock_lot.id"), nullable=True)
    adjustment_lot_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stock_lot.id"), nullable=True)
    inventory_txn_id: Mapped[int | None] = mapped_column(ForeignKey("wh_inventory_txn.id"), nullable=True)
    adjustment_type: Mapped[str] = mapped_column(String(32))
    before_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    after_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    before_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    after_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    qty_change: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    weight_change_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    created_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
    updated_at: Mapped[object] = mapped_column(DateTime, server_default=text("CURRENT_TIMESTAMP"))


class WorkOrder(Base):
    __tablename__ = "pp_work_order"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    work_order_no: Mapped[str] = mapped_column(String(80))
    work_order_type: Mapped[str] = mapped_column(String(32))
    source_sales_order_item_id: Mapped[int | None] = mapped_column(ForeignKey("so_sales_order_item.id"), nullable=True)
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    route_id: Mapped[int] = mapped_column(ForeignKey("md_process_route.id"))
    bom_id: Mapped[int] = mapped_column(ForeignKey("md_bom.id"))
    planned_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    released_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    finished_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    planned_start_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    planned_end_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    actual_start_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    actual_end_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    priority_level: Mapped[int] = mapped_column(Integer, server_default=text("3"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class WorkOrderMaterial(Base):
    __tablename__ = "pp_work_order_material"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    work_order_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order.id"))
    bom_item_id: Mapped[int] = mapped_column(ForeignKey("md_bom_item.id"))
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    required_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    required_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    issued_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    issued_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    returned_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    returned_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class WorkOrderMaterialIssue(Base):
    __tablename__ = "pp_work_order_material_issue"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    work_order_material_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order_material.id"))
    work_order_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order.id"))
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    source_lot_id: Mapped[int] = mapped_column(ForeignKey("wh_stock_lot.id"))
    material_sub_batch_no: Mapped[str] = mapped_column(String(80))
    issued_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    issued_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    issue_time: Mapped[object] = mapped_column(DateTime)
    operator_user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ProductionBatchLedger(Base):
    __tablename__ = "pp_production_batch_ledger"
    __table_args__ = (
        UniqueConstraint("material_lot_id", "product_item_id", name="uk_production_batch_ledger_lot_product"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    material_lot_id: Mapped[int] = mapped_column(ForeignKey("wh_stock_lot.id"))
    material_lot_no: Mapped[str] = mapped_column(String(128))
    material_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    status: Mapped[str] = mapped_column(String(32), server_default=text("'在生产'"))
    miniapp_selectable: Mapped[int] = mapped_column(Integer, server_default=text("1"))
    total_issued_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    outside_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    finished_inbound_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    surplus_inbound_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_inbound_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    writeoff_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    first_issue_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    last_issue_time: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    locked_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    reopened_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    lock_count: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ProductionBatchLedgerTxn(Base):
    __tablename__ = "pp_production_batch_ledger_txn"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    production_ledger_id: Mapped[int] = mapped_column(ForeignKey("pp_production_batch_ledger.id"))
    txn_type: Mapped[str] = mapped_column(String(32))
    qty_delta: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    weight_delta_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    outside_weight_after_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    source_doc_type: Mapped[str | None] = mapped_column(String(64), nullable=True)
    source_doc_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    source_line_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    biz_time: Mapped[object] = mapped_column(DateTime)
    operator_user_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class WorkOrderOperation(Base):
    __tablename__ = "pp_work_order_operation"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    work_order_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order.id"))
    route_operation_id: Mapped[int] = mapped_column(ForeignKey("md_process_route_operation.id"))
    seq_no: Mapped[int] = mapped_column(Integer)
    operation_name: Mapped[str] = mapped_column(String(100))
    work_center_id: Mapped[int] = mapped_column(ForeignKey("md_work_center.id"))
    planned_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    report_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    good_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    rework_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    std_labor_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    std_machine_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    std_outsource_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    allowed_scrap_rate: Mapped[float] = mapped_column(DECIMAL(8, 4), server_default=text("0"))
    is_abnormal: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class OperationReport(Base):
    __tablename__ = "pp_operation_report"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    report_no: Mapped[str] = mapped_column(String(50))
    work_order_id: Mapped[int | None] = mapped_column(ForeignKey("pp_work_order.id"), nullable=True)
    work_order_operation_id: Mapped[int | None] = mapped_column(ForeignKey("pp_work_order_operation.id"), nullable=True)
    production_ledger_id: Mapped[int | None] = mapped_column(ForeignKey("pp_production_batch_ledger.id"), nullable=True)
    employee_id: Mapped[int] = mapped_column(ForeignKey("hr_employee.id"))
    equipment_id: Mapped[int | None] = mapped_column(ForeignKey("em_equipment.id"), nullable=True)
    report_source: Mapped[str] = mapped_column(String(32))
    shift_code: Mapped[str | None] = mapped_column(String(32), nullable=True)
    start_time: Mapped[object] = mapped_column(DateTime)
    end_time: Mapped[object] = mapped_column(DateTime)
    report_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    good_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    rework_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_reason: Mapped[str | None] = mapped_column(String(255), nullable=True)
    downtime_minutes: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    extra_cost_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    is_abnormal: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    approved_by: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    approved_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ScrapRecord(Base):
    __tablename__ = "pp_scrap_record"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    scrap_no: Mapped[str] = mapped_column(String(50))
    report_id: Mapped[int] = mapped_column(ForeignKey("pp_operation_report.id"))
    work_order_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order.id"))
    work_order_operation_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order_operation.id"))
    item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    scrap_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_reason: Mapped[str | None] = mapped_column(String(255), nullable=True)
    disposal_type: Mapped[str] = mapped_column(String(32))
    saleable_scrap_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    scrap_cost_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class CompletionReceipt(Base):
    __tablename__ = "pp_completion_receipt"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    receipt_no: Mapped[str] = mapped_column(String(50))
    work_order_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order.id"))
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    receipt_time: Mapped[object] = mapped_column(DateTime)
    receiver_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class CompletionReceiptItem(Base):
    __tablename__ = "pp_completion_receipt_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    completion_receipt_id: Mapped[int] = mapped_column(ForeignKey("pp_completion_receipt.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    location_id: Mapped[int | None] = mapped_column(ForeignKey("wh_location.id"), nullable=True)
    lot_no: Mapped[str] = mapped_column(String(50))
    source_material_lot_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    source_material_sub_batch_no: Mapped[str | None] = mapped_column(String(80), nullable=True)
    source_material_summary: Mapped[str | None] = mapped_column(String(1000), nullable=True)
    receipt_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    receipt_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Delivery(Base):
    __tablename__ = "so_delivery"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    delivery_no: Mapped[str] = mapped_column(String(50))
    sales_order_id: Mapped[int | None] = mapped_column(ForeignKey("so_sales_order.id"), nullable=True)
    customer_id: Mapped[int | None] = mapped_column(ForeignKey("md_customer.id"), nullable=True)
    warehouse_id: Mapped[int] = mapped_column(ForeignKey("wh_warehouse.id"))
    delivery_date: Mapped[object] = mapped_column(DateTime)
    shipper_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    consignee_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    consignee_phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
    delivery_address: Mapped[str | None] = mapped_column(String(255), nullable=True)
    logistics_waybill_no: Mapped[str | None] = mapped_column(String(100), nullable=True)
    logistics_freight_amount: Mapped[float | None] = mapped_column(DECIMAL(18, 2), nullable=True)
    logistics_photo_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class DeliveryItem(Base):
    __tablename__ = "so_delivery_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    delivery_id: Mapped[int] = mapped_column(ForeignKey("so_delivery.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    sales_order_item_id: Mapped[int | None] = mapped_column(ForeignKey("so_sales_order_item.id"), nullable=True)
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    lot_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stock_lot.id"), nullable=True)
    delivery_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    delivery_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    unit_price: Mapped[float] = mapped_column(DECIMAL(18, 4), server_default=text("0"))
    line_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ReturnOrder(Base):
    __tablename__ = "rt_return_order"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    return_no: Mapped[str] = mapped_column(String(50))
    customer_id: Mapped[int] = mapped_column(ForeignKey("md_customer.id"))
    sales_order_id: Mapped[int | None] = mapped_column(ForeignKey("so_sales_order.id"), nullable=True)
    delivery_id: Mapped[int | None] = mapped_column(ForeignKey("so_delivery.id"), nullable=True)
    return_date: Mapped[object] = mapped_column(DateTime)
    return_reason: Mapped[str | None] = mapped_column(String(255), nullable=True)
    handler_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ReturnItem(Base):
    __tablename__ = "rt_return_item"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    return_order_id: Mapped[int] = mapped_column(ForeignKey("rt_return_order.id"))
    line_no: Mapped[int] = mapped_column(Integer)
    sales_order_item_id: Mapped[int | None] = mapped_column(ForeignKey("so_sales_order_item.id"), nullable=True)
    delivery_item_id: Mapped[int | None] = mapped_column(ForeignKey("so_delivery_item.id"), nullable=True)
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    source_lot_id: Mapped[int | None] = mapped_column(ForeignKey("wh_stock_lot.id"), nullable=True)
    return_qty: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    return_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    disposition_status: Mapped[str] = mapped_column(String(32))
    responsibility_type: Mapped[str | None] = mapped_column(String(32), nullable=True)
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class ReturnDisposition(Base):
    __tablename__ = "rt_return_disposition"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    disposition_no: Mapped[str] = mapped_column(String(50))
    return_item_id: Mapped[int] = mapped_column(ForeignKey("rt_return_item.id"))
    disposition_type: Mapped[str] = mapped_column(String(32))
    rework_work_order_id: Mapped[int | None] = mapped_column(ForeignKey("pp_work_order.id"), nullable=True)
    scrap_weight_kg: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    extra_cost_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    scrap_sale_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    closed_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class AccountingPeriod(Base):
    __tablename__ = "fi_accounting_period"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    period_code: Mapped[str] = mapped_column(String(20))
    year_no: Mapped[int] = mapped_column(Integer)
    month_no: Mapped[int] = mapped_column(Integer)
    start_date: Mapped[object] = mapped_column(Date)
    end_date: Mapped[object] = mapped_column(Date)
    close_status: Mapped[str] = mapped_column(String(32))
    closed_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class OverheadEntry(Base):
    __tablename__ = "fi_overhead_entry"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    period_id: Mapped[int] = mapped_column(ForeignKey("fi_accounting_period.id"))
    overhead_type: Mapped[str] = mapped_column(String(32))
    dept_id: Mapped[int | None] = mapped_column(ForeignKey("sys_department.id"), nullable=True)
    amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    allocation_basis: Mapped[str] = mapped_column(String(32))
    description: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_by: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class CostAllocation(Base):
    __tablename__ = "fi_cost_allocation"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    period_id: Mapped[int] = mapped_column(ForeignKey("fi_accounting_period.id"))
    work_order_id: Mapped[int] = mapped_column(ForeignKey("pp_work_order.id"))
    product_item_id: Mapped[int] = mapped_column(ForeignKey("md_item.id"))
    allocation_basis_value: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    overhead_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    material_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    labor_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    machine_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    abnormal_loss_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    total_cost_amount: Mapped[float] = mapped_column(DECIMAL(18, 2), server_default=text("0"))
    unit_cost: Mapped[float] = mapped_column(DECIMAL(18, 6), server_default=text("0"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class StatementSnapshot(Base):
    __tablename__ = "fi_statement_snapshot"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    snapshot_no: Mapped[str] = mapped_column(String(50))
    period_id: Mapped[int] = mapped_column(ForeignKey("fi_accounting_period.id"))
    statement_type: Mapped[str] = mapped_column(String(32))
    report_date: Mapped[object] = mapped_column(Date)
    summary_json: Mapped[dict] = mapped_column(JSON)
    created_by: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)
