test_pl_plan_print_service.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """送(提)货单按客户聚合打印"""
  2. from datetime import datetime
  3. from decimal import Decimal
  4. from app.schemas.pl.plan import PlanDeliveryRead, PlanStopProductRead
  5. from app.services.pl_plan_print_service import (
  6. build_delivery_slip_print,
  7. list_delivery_print_customers,
  8. )
  9. def _stop_product(**kwargs) -> PlanStopProductRead:
  10. now = datetime(2026, 8, 26, 10, 0, 0)
  11. return PlanStopProductRead(
  12. id=kwargs.get("id", "sp1"),
  13. plan_id="p1",
  14. product_id=kwargs["product_id"],
  15. product_no=kwargs.get("product_no", "PN"),
  16. product_name=kwargs.get("product_name", "产品"),
  17. unit=kwargs.get("unit", "吨"),
  18. plan_qty=kwargs.get("plan_qty"),
  19. outbound_qty=kwargs.get("outbound_qty"),
  20. tenant_no="t1",
  21. create_time=now,
  22. update_time=now,
  23. )
  24. def _delivery(**kwargs) -> PlanDeliveryRead:
  25. now = datetime(2026, 8, 26, 10, 0, 0)
  26. return PlanDeliveryRead(
  27. id=kwargs["id"],
  28. plan_id="p1",
  29. seq=kwargs.get("seq", 1),
  30. customer_id=kwargs.get("customer_id"),
  31. customer_name=kwargs.get("customer_name"),
  32. address=kwargs.get("address"),
  33. contact=kwargs.get("contact"),
  34. products=kwargs.get("products") or [],
  35. tenant_no="t1",
  36. create_time=now,
  37. update_time=now,
  38. )
  39. def test_list_delivery_print_customers_three_distinct():
  40. deliveries = [
  41. _delivery(id="d1", seq=1, customer_id="c1", customer_name="客户A"),
  42. _delivery(id="d2", seq=2, customer_id="c2", customer_name="客户B"),
  43. _delivery(id="d3", seq=3, customer_id="c3", customer_name="客户C"),
  44. ]
  45. rows = list_delivery_print_customers(deliveries)
  46. assert len(rows) == 3
  47. assert [r.customer_id for r in rows] == ["c1", "c2", "c3"]
  48. assert all(r.delivery_count == 1 for r in rows)
  49. def test_list_delivery_print_customers_dedupes_by_customer_id():
  50. deliveries = [
  51. _delivery(id="d1", seq=2, customer_id="c1", customer_name="客户A"),
  52. _delivery(id="d2", seq=1, customer_id="c1", customer_name="客户A"),
  53. _delivery(id="d3", seq=3, customer_id="c2", customer_name="客户B"),
  54. ]
  55. rows = list_delivery_print_customers(deliveries)
  56. assert len(rows) == 2
  57. assert rows[0].customer_id == "c1"
  58. assert rows[0].delivery_count == 2
  59. assert rows[1].customer_id == "c2"
  60. def test_build_delivery_slip_print_merges_same_customer_and_uses_min_seq_contact(monkeypatch):
  61. deliveries = [
  62. _delivery(
  63. id="d2",
  64. seq=2,
  65. customer_id="c1",
  66. customer_name="客户A",
  67. address="地址B",
  68. contact="联系B",
  69. products=[
  70. _stop_product(id="sp2", product_id="prod1", plan_qty=Decimal("5"), outbound_qty=Decimal("5"))
  71. ],
  72. ),
  73. _delivery(
  74. id="d1",
  75. seq=1,
  76. customer_id="c1",
  77. customer_name="客户A",
  78. address="地址A",
  79. contact="联系A",
  80. products=[
  81. _stop_product(id="sp1", product_id="prod1", plan_qty=Decimal("10"), outbound_qty=Decimal("10"))
  82. ],
  83. ),
  84. ]
  85. monkeypatch.setattr(
  86. "app.services.pl_plan_print_service.load_plan_stops_grouped",
  87. lambda db, tenant_no, plan_ids: {"p1": (None, deliveries)},
  88. )
  89. result = build_delivery_slip_print(
  90. db=None, # type: ignore[arg-type]
  91. tenant_no="t1",
  92. plan_id="p1",
  93. plan_no="PL001",
  94. plan_date="2026-08-26",
  95. customer_id="c1",
  96. )
  97. assert result.receiving_unit == "客户A"
  98. assert result.receive_contact == "联系A"
  99. assert result.delivery_address == "地址A"
  100. assert len(result.products) == 1
  101. assert result.products[0].plan_qty == Decimal("15")
  102. assert result.products[0].outbound_qty == Decimal("15")