from __future__ import annotations

import unittest
from datetime import datetime
from decimal import Decimal
from tempfile import TemporaryDirectory
from unittest.mock import patch

from sqlalchemy import BigInteger, create_engine, select
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.orm import Session, sessionmaker


@compiles(BigInteger, "sqlite")
def _compile_big_integer_for_sqlite(type_, compiler, **kw) -> str:
    _ = type_, compiler, kw
    return "INTEGER"


import app.models.document_archive  # noqa: E402,F401
import app.models.master_data  # noqa: E402,F401
import app.models.miniapp  # noqa: E402,F401
import app.models.operations  # noqa: E402,F401
import app.models.org  # noqa: E402,F401
import app.models.planning  # noqa: E402,F401
import app.models.sales  # noqa: E402,F401
from app.models.base import Base  # noqa: E402
from app.models.document_archive import DocumentArchive  # noqa: E402
from app.models.master_data import Item, Warehouse  # noqa: E402
from app.models.operations import CompletionReceipt, InventoryTxn, ProductionBatchLedger, ProductionBatchLedgerTxn, StockLot, WorkOrder  # noqa: E402
from app.api.routes.inventory import _should_archive_production_inbound  # noqa: E402
from app.api.routes.production import _production_ledger_txn_read, list_production_batch_ledger  # noqa: E402
from app.services.document_archives import (  # noqa: E402
    ARCHIVE_STATUS_READY,
    DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT,
    DOCUMENT_TYPE_PRODUCTION_MATERIAL_OUT,
    collect_production_inbound_settlement_archive_context,
    collect_production_material_out_archive_context,
    generate_document_archive,
)


class ProductionWarehouseDocumentArchiveTest(unittest.TestCase):
    def setUp(self) -> None:
        engine = create_engine("sqlite+pysqlite:///:memory:", future=True)
        Base.metadata.create_all(engine)
        self.SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
        self.db: Session = self.SessionLocal()
        self.now = datetime(2026, 6, 11, 9, 0, 0)
        self.biz_time = datetime(2026, 6, 11, 10, 30, 0)
        self._seed_archive_context_data()

    def tearDown(self) -> None:
        self.db.close()

    def _seed_archive_context_data(self) -> None:
        self.raw_warehouse = Warehouse(
            id=1,
            warehouse_code="RAW",
            warehouse_name="原材料库",
            warehouse_type="RAW",
            status="ACTIVE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.finished_warehouse = Warehouse(
            id=2,
            warehouse_code="FINISHED",
            warehouse_name="成品库",
            warehouse_type="FINISHED",
            status="ACTIVE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.scrap_warehouse = Warehouse(
            id=3,
            warehouse_code="SCRAP",
            warehouse_name="废料库",
            warehouse_type="SCRAP",
            status="ACTIVE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.material_item = Item(
            id=10,
            item_code="RM-ARCHIVE-001",
            item_name="测试冷轧板",
            item_type="RAW_MATERIAL",
            specification="1.0mm",
            unit_weight_kg=Decimal("1"),
            status="ACTIVE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.product_item = Item(
            id=20,
            item_code="FG-ARCHIVE-001",
            item_name="测试钢制碗",
            item_type="FINISHED_GOOD",
            specification="碗型",
            unit_weight_kg=Decimal("0.4"),
            status="ACTIVE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.raw_lot = StockLot(
            id=100,
            lot_no="YL0001",
            lot_role="RAW_MATERIAL",
            item_id=self.material_item.id,
            warehouse_id=self.raw_warehouse.id,
            source_doc_type="期初入库",
            source_doc_id=1,
            inbound_qty=Decimal("0"),
            inbound_weight_kg=Decimal("500"),
            remaining_qty=Decimal("0"),
            remaining_weight_kg=Decimal("300"),
            locked_qty=Decimal("0"),
            locked_weight_kg=Decimal("0"),
            unit_cost=Decimal("6.6"),
            quality_status="PASS",
            status="AVAILABLE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.ledger = ProductionBatchLedger(
            id=200,
            material_lot_id=self.raw_lot.id,
            material_lot_no="YL0001",
            material_item_id=self.material_item.id,
            product_item_id=self.product_item.id,
            status="在生产",
            miniapp_selectable=1,
            total_issued_weight_kg=Decimal("200"),
            outside_weight_kg=Decimal("80"),
            finished_inbound_qty=Decimal("198"),
            surplus_inbound_weight_kg=Decimal("60"),
            scrap_inbound_weight_kg=Decimal("2"),
            writeoff_weight_kg=Decimal("0"),
            first_issue_time=self.biz_time,
            last_issue_time=self.biz_time,
            created_at=self.now,
            updated_at=self.now,
        )
        self.issue_txn = InventoryTxn(
            id=400,
            txn_no="TXN-OUT-001",
            txn_type="PRODUCTION_OUT",
            item_id=self.material_item.id,
            warehouse_id=self.raw_warehouse.id,
            lot_id=self.raw_lot.id,
            qty_change=Decimal("0"),
            weight_change_kg=Decimal("-200"),
            unit_cost=Decimal("6.6"),
            amount=Decimal("1320.00"),
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            biz_time=self.biz_time,
            remark="生产领料出库",
            created_at=self.now,
            updated_at=self.now,
        )
        self.ledger_txn = ProductionBatchLedgerTxn(
            id=300,
            production_ledger_id=self.ledger.id,
            txn_type="生产出库",
            qty_delta=Decimal("0"),
            weight_delta_kg=Decimal("200"),
            outside_weight_after_kg=Decimal("80"),
            source_doc_type="库存流水",
            source_doc_id=self.issue_txn.id,
            biz_time=self.biz_time,
            operator_user_id=1,
            remark="生产出库累计到生产台账",
            created_at=self.now,
            updated_at=self.now,
        )
        self.finished_lot = StockLot(
            id=101,
            lot_no="FG0001",
            lot_role="FINISHED_GOOD",
            item_id=self.product_item.id,
            warehouse_id=self.finished_warehouse.id,
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            source_material_lot_id=self.raw_lot.id,
            inbound_qty=Decimal("198"),
            inbound_weight_kg=Decimal("79.2"),
            remaining_qty=Decimal("198"),
            remaining_weight_kg=Decimal("79.2"),
            locked_qty=Decimal("0"),
            locked_weight_kg=Decimal("0"),
            unit_cost=Decimal("6.6667"),
            quality_status="PASS",
            status="AVAILABLE",
            created_at=self.now,
            updated_at=self.now,
        )
        self.finished_txn = InventoryTxn(
            id=401,
            txn_no="TXN-FG-001",
            txn_type="FG_IN",
            item_id=self.product_item.id,
            warehouse_id=self.finished_warehouse.id,
            lot_id=self.finished_lot.id,
            qty_change=Decimal("198"),
            weight_change_kg=Decimal("79.2"),
            unit_cost=Decimal("6.6667"),
            amount=Decimal("1320.00"),
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            biz_time=self.biz_time,
            remark="偏差说明：称重复核通过；单据批次号：生产结算20260611103000000001",
            created_at=self.now,
            updated_at=self.now,
        )
        self.surplus_txn = InventoryTxn(
            id=402,
            txn_no="TXN-SURPLUS-001",
            txn_type="PRODUCTION_SURPLUS_IN",
            item_id=self.material_item.id,
            warehouse_id=self.raw_warehouse.id,
            lot_id=self.raw_lot.id,
            qty_change=Decimal("0"),
            weight_change_kg=Decimal("60"),
            unit_cost=Decimal("6.6"),
            amount=Decimal("396.00"),
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            biz_time=self.biz_time,
            remark="偏差说明：称重复核通过；单据批次号：生产结算20260611103000000001",
            created_at=self.now,
            updated_at=self.now,
        )
        self.db.add_all(
            [
                self.raw_warehouse,
                self.finished_warehouse,
                self.scrap_warehouse,
                self.material_item,
                self.product_item,
                self.raw_lot,
                self.ledger,
                self.issue_txn,
                self.ledger_txn,
                self.finished_lot,
                self.finished_txn,
                self.surplus_txn,
            ]
        )
        self.db.commit()

    def test_collect_material_out_context_uses_production_ledger(self) -> None:
        context = collect_production_material_out_archive_context(self.db, self.ledger.id)

        self.assertEqual(context.document_type, DOCUMENT_TYPE_PRODUCTION_MATERIAL_OUT)
        self.assertEqual(context.business_id, self.ledger.id)
        self.assertEqual(context.document_no, "生产领料-YL0001-测试钢制碗")
        self.assertEqual(context.partner_label, "生产产品")
        self.assertEqual(context.partner_name, "测试钢制碗")
        self.assertEqual(context.address, "原材料库")
        self.assertIn("材料库存批次号：YL0001", context.remark)
        self.assertEqual(len(context.lines), 1)
        line = context.lines[0]
        self.assertEqual(line.item_name, "测试冷轧板")
        self.assertEqual(Decimal(str(line.quantity)), Decimal("200"))
        self.assertEqual(Decimal(str(line.line_amount)), Decimal("1320.00"))

    def test_collect_inbound_settlement_context_groups_same_save_action(self) -> None:
        context = collect_production_inbound_settlement_archive_context(self.db, self.finished_txn.id)

        self.assertEqual(context.document_type, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT)
        self.assertEqual(context.business_id, self.finished_txn.id)
        self.assertEqual(context.document_no, "生产入库结算-TXN-FG-001")
        self.assertEqual(context.partner_label, "生产产品")
        self.assertEqual(context.partner_name, "测试钢制碗")
        self.assertIn("材料库存批次号：YL0001", context.remark)
        self.assertEqual(len(context.lines), 2)
        first_chunks = [str(line.remark or "").split("；")[0] for line in context.lines]
        self.assertEqual(first_chunks, ["入库类型：成品入库", "入库类型：生产余料入库"])

    def test_collect_inbound_settlement_context_labels_all_related_warehouses(self) -> None:
        scrap_lot = StockLot(
            id=102,
            lot_no="SCRAP0001",
            lot_role="PRODUCTION_SCRAP",
            item_id=self.material_item.id,
            warehouse_id=self.scrap_warehouse.id,
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            source_material_lot_id=self.raw_lot.id,
            inbound_qty=Decimal("0"),
            inbound_weight_kg=Decimal("2"),
            remaining_qty=Decimal("0"),
            remaining_weight_kg=Decimal("2"),
            locked_qty=Decimal("0"),
            locked_weight_kg=Decimal("0"),
            unit_cost=Decimal("6.6"),
            quality_status="SCRAP",
            status="AVAILABLE",
            created_at=self.now,
            updated_at=self.now,
        )
        scrap_txn = InventoryTxn(
            id=403,
            txn_no="TXN-SCRAP-001",
            txn_type="PRODUCTION_SCRAP_IN",
            item_id=self.material_item.id,
            warehouse_id=self.scrap_warehouse.id,
            lot_id=scrap_lot.id,
            qty_change=Decimal("0"),
            weight_change_kg=Decimal("2"),
            unit_cost=Decimal("6.6"),
            amount=Decimal("13.20"),
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            biz_time=self.biz_time,
            remark="偏差说明：称重复核通过；单据批次号：生产结算20260611103000000001",
            created_at=self.now,
            updated_at=self.now,
        )
        self.db.add_all([scrap_lot, scrap_txn])
        self.db.commit()

        context = collect_production_inbound_settlement_archive_context(self.db, self.finished_txn.id)

        self.assertEqual(context.address_label, "涉及仓库")
        self.assertEqual(context.address, "成品库、原材料库、废料库")
        self.assertIn("涉及仓库：成品库、原材料库、废料库", context.remark)
        self.assertEqual([str(line.remark or "").split("；")[0] for line in context.lines], ["入库类型：成品入库", "入库类型：生产余料入库", "入库类型：生产废料入库"])

    def test_collect_work_order_inbound_settlement_groups_receipt_and_work_order_txns(self) -> None:
        work_order = WorkOrder(
            id=500,
            work_order_no="WO-ARCHIVE-001",
            work_order_type="正常生产",
            product_item_id=self.product_item.id,
            route_id=1,
            bom_id=1,
            planned_qty=Decimal("100"),
            released_qty=Decimal("100"),
            finished_qty=Decimal("0"),
            scrap_qty=Decimal("0"),
            priority_level=3,
            status="RELEASED",
            created_at=self.now,
            updated_at=self.now,
        )
        receipt = CompletionReceipt(
            id=600,
            receipt_no="FGI-ARCHIVE-001",
            work_order_id=work_order.id,
            warehouse_id=self.finished_warehouse.id,
            receipt_time=self.biz_time,
            status="POSTED",
            remark="旧工单入库",
            created_at=self.now,
            updated_at=self.now,
        )
        finished_txn = InventoryTxn(
            id=601,
            txn_no="TXN-WO-FG-001",
            txn_type="FG_IN",
            item_id=self.product_item.id,
            warehouse_id=self.finished_warehouse.id,
            lot_id=self.finished_lot.id,
            qty_change=Decimal("50"),
            weight_change_kg=Decimal("20"),
            unit_cost=Decimal("6.6"),
            amount=Decimal("330.00"),
            source_doc_type="FG_RECEIPT",
            source_doc_id=receipt.id,
            biz_time=self.biz_time,
            remark="旧工单成品入库",
            created_at=self.now,
            updated_at=self.now,
        )
        surplus_txn = InventoryTxn(
            id=602,
            txn_no="TXN-WO-SURPLUS-001",
            txn_type="PRODUCTION_SURPLUS_IN",
            item_id=self.material_item.id,
            warehouse_id=self.raw_warehouse.id,
            lot_id=self.raw_lot.id,
            qty_change=Decimal("0"),
            weight_change_kg=Decimal("12"),
            unit_cost=Decimal("6.6"),
            amount=Decimal("79.20"),
            source_doc_type="WORK_ORDER",
            source_doc_id=work_order.id,
            biz_time=self.biz_time,
            remark="旧工单余料入库",
            created_at=self.now,
            updated_at=self.now,
        )
        self.db.add_all([work_order, receipt, finished_txn, surplus_txn])
        self.db.commit()

        context = collect_production_inbound_settlement_archive_context(self.db, finished_txn.id)

        self.assertEqual(context.partner_name, "测试钢制碗")
        self.assertEqual(len(context.lines), 2)
        self.assertEqual([line.remark.split("；")[0] for line in context.lines], ["入库类型：成品入库", "入库类型：生产余料入库"])

    def test_generic_finished_inbound_without_production_source_does_not_archive(self) -> None:
        self.assertFalse(
            _should_archive_production_inbound(
                "PRODUCTION_FINISHED",
                production_ledger_id=None,
                production_work_order_id=None,
            )
        )
        self.assertTrue(
            _should_archive_production_inbound(
                "PRODUCTION_FINISHED",
                production_ledger_id=self.ledger.id,
                production_work_order_id=None,
            )
        )
        self.assertTrue(
            _should_archive_production_inbound(
                "PRODUCTION_FINISHED",
                production_ledger_id=None,
                production_work_order_id=500,
            )
        )

    def test_production_out_ledger_txn_does_not_expose_inbound_settlement_archive(self) -> None:
        row = _production_ledger_txn_read(self.ledger_txn)

        self.assertEqual(row["txn_type"], "生产出库")
        self.assertIsNone(row["archive_business_id"])
        self.assertIsNone(row["archive_document_type"])
        self.assertIsNone(row["archive_status"])

    def test_production_ledger_inbound_txns_share_main_settlement_archive_id(self) -> None:
        finished_ledger_txn = ProductionBatchLedgerTxn(
            id=301,
            production_ledger_id=self.ledger.id,
            txn_type="成品入库",
            qty_delta=Decimal("198"),
            weight_delta_kg=Decimal("-118.8"),
            outside_weight_after_kg=Decimal("81.2"),
            source_doc_type="库存流水",
            source_doc_id=self.finished_txn.id,
            source_line_id=self.finished_lot.id,
            biz_time=self.biz_time,
            operator_user_id=1,
            remark="成品入库联动生产台账；单据批次号：生产结算20260611103000000001",
            created_at=self.now,
            updated_at=self.now,
        )
        surplus_ledger_txn = ProductionBatchLedgerTxn(
            id=302,
            production_ledger_id=self.ledger.id,
            txn_type="生产余料入库",
            qty_delta=Decimal("0"),
            weight_delta_kg=Decimal("-60"),
            outside_weight_after_kg=Decimal("21.2"),
            source_doc_type="库存流水",
            source_doc_id=self.surplus_txn.id,
            source_line_id=self.raw_lot.id,
            biz_time=self.biz_time,
            operator_user_id=1,
            remark="生产余料入库联动生产台账；单据批次号：生产结算20260611103000000001",
            created_at=self.now,
            updated_at=self.now,
        )
        self.db.add_all([finished_ledger_txn, surplus_ledger_txn])
        self.db.commit()
        with TemporaryDirectory() as tmp_dir, patch("app.services.document_archives.default_archive_root", return_value=tmp_dir):
            generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT, self.finished_txn.id)

        rows = list_production_batch_ledger(production_ledger_id=self.ledger.id, status=None, limit=20, db=self.db)
        txn_by_type = {txn.txn_type: txn for txn in rows[0].txns}

        self.assertEqual(txn_by_type["成品入库"].archive_business_id, self.finished_txn.id)
        self.assertEqual(txn_by_type["生产余料入库"].archive_business_id, self.finished_txn.id)
        self.assertEqual(txn_by_type["成品入库"].archive_status, ARCHIVE_STATUS_READY)
        self.assertEqual(txn_by_type["生产余料入库"].archive_status, ARCHIVE_STATUS_READY)

    def test_same_second_different_document_batches_do_not_share_archive_id(self) -> None:
        first_finished_ledger_txn = ProductionBatchLedgerTxn(
            id=311,
            production_ledger_id=self.ledger.id,
            txn_type="成品入库",
            qty_delta=Decimal("198"),
            weight_delta_kg=Decimal("-118.8"),
            outside_weight_after_kg=Decimal("81.2"),
            source_doc_type="库存流水",
            source_doc_id=self.finished_txn.id,
            source_line_id=self.finished_lot.id,
            biz_time=self.biz_time,
            operator_user_id=1,
            remark="成品入库联动生产台账；单据批次号：生产结算20260611103000000001",
            created_at=self.now,
            updated_at=self.now,
        )
        second_surplus_txn = InventoryTxn(
            id=403,
            txn_no="TXN-SURPLUS-002",
            txn_type="PRODUCTION_SURPLUS_IN",
            item_id=self.material_item.id,
            warehouse_id=self.raw_warehouse.id,
            lot_id=self.raw_lot.id,
            qty_change=Decimal("0"),
            weight_change_kg=Decimal("12"),
            unit_cost=Decimal("6.6"),
            amount=Decimal("79.20"),
            source_doc_type="生产台账",
            source_doc_id=self.ledger.id,
            biz_time=self.biz_time,
            remark="同秒第二次余料入库；单据批次号：生产结算20260611103000000002",
            created_at=self.now,
            updated_at=self.now,
        )
        second_surplus_ledger_txn = ProductionBatchLedgerTxn(
            id=312,
            production_ledger_id=self.ledger.id,
            txn_type="生产余料入库",
            qty_delta=Decimal("0"),
            weight_delta_kg=Decimal("-12"),
            outside_weight_after_kg=Decimal("69.2"),
            source_doc_type="库存流水",
            source_doc_id=second_surplus_txn.id,
            source_line_id=self.raw_lot.id,
            biz_time=self.biz_time,
            operator_user_id=1,
            remark="同秒第二次余料入库联动生产台账；单据批次号：生产结算20260611103000000002",
            created_at=self.now,
            updated_at=self.now,
        )
        self.db.add_all([first_finished_ledger_txn, second_surplus_txn, second_surplus_ledger_txn])
        self.db.commit()
        with TemporaryDirectory() as tmp_dir, patch("app.services.document_archives.default_archive_root", return_value=tmp_dir):
            generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT, self.finished_txn.id)
            generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT, second_surplus_txn.id)

        rows = list_production_batch_ledger(production_ledger_id=self.ledger.id, status=None, limit=20, db=self.db)
        txn_by_id = {txn.production_ledger_txn_id: txn for txn in rows[0].txns}

        self.assertEqual(txn_by_id[first_finished_ledger_txn.id].archive_business_id, self.finished_txn.id)
        self.assertEqual(txn_by_id[second_surplus_ledger_txn.id].archive_business_id, second_surplus_txn.id)

    def test_generate_production_material_out_archive_pdf(self) -> None:
        with TemporaryDirectory() as tmp_dir, patch("app.services.document_archives.default_archive_root", return_value=tmp_dir):
            result = generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_MATERIAL_OUT, self.ledger.id)

        self.assertEqual(result.archive_status, ARCHIVE_STATUS_READY)
        archive = self.db.scalar(
            select(DocumentArchive).where(
                DocumentArchive.document_type == DOCUMENT_TYPE_PRODUCTION_MATERIAL_OUT,
                DocumentArchive.business_id == self.ledger.id,
                DocumentArchive.archive_version == 1,
            )
        )
        self.assertIsNotNone(archive)

    def test_generate_production_inbound_settlement_archive_pdf(self) -> None:
        with TemporaryDirectory() as tmp_dir, patch("app.services.document_archives.default_archive_root", return_value=tmp_dir):
            result = generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT, self.finished_txn.id)

        self.assertEqual(result.archive_status, ARCHIVE_STATUS_READY)
        archive = self.db.scalar(
            select(DocumentArchive).where(
                DocumentArchive.document_type == DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT,
                DocumentArchive.business_id == self.finished_txn.id,
                DocumentArchive.archive_version == 1,
            )
        )
        self.assertIsNotNone(archive)

    def test_archive_generation_failure_keeps_existing_business_rows(self) -> None:
        with patch("app.services.document_archives.render_pdf_archive", side_effect=RuntimeError("PDF服务异常")):
            result = generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT, self.finished_txn.id)

        self.assertEqual(result.archive_status, "归档失败")
        self.assertIsNotNone(self.db.get(InventoryTxn, self.finished_txn.id))
        archive = self.db.scalar(
            select(DocumentArchive).where(
                DocumentArchive.document_type == DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT,
                DocumentArchive.business_id == self.finished_txn.id,
            )
        )
        self.assertIsNotNone(archive)
        self.assertEqual(archive.status, "归档失败")

    def test_latest_archive_rows_can_be_joined_by_business_id(self) -> None:
        with TemporaryDirectory() as tmp_dir, patch("app.services.document_archives.default_archive_root", return_value=tmp_dir):
            generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_MATERIAL_OUT, self.ledger.id)
            generate_document_archive(self.db, DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT, self.finished_txn.id)

        material_archive = self.db.scalar(
            select(DocumentArchive).where(DocumentArchive.document_type == DOCUMENT_TYPE_PRODUCTION_MATERIAL_OUT)
        )
        settlement_archive = self.db.scalar(
            select(DocumentArchive).where(DocumentArchive.document_type == DOCUMENT_TYPE_PRODUCTION_INBOUND_SETTLEMENT)
        )
        self.assertIsNotNone(material_archive)
        self.assertIsNotNone(settlement_archive)
        self.assertEqual(material_archive.business_id, self.ledger.id)
        self.assertEqual(settlement_archive.business_id, self.finished_txn.id)


if __name__ == "__main__":
    unittest.main()
