forked from erp-dev/erp
feat: /api/v1/shipment/sales-items/by-printing-order/KD20135142/
This commit is contained in:
@@ -319,7 +319,7 @@ urlpatterns = [
|
||||
name="sales_items_by_customer",
|
||||
),
|
||||
path(
|
||||
"shipment/sales-items/by-printing-order/<int:printing_order_id>/",
|
||||
"shipment/sales-items/by-printing-order/<str:printing_order_id>/",
|
||||
SalesItemByPrintingOrderView.as_view(),
|
||||
name="sales_items_by_printing_order",
|
||||
),
|
||||
|
||||
@@ -241,6 +241,56 @@ class SalesItemByPrintingOrderAPITestCase(TestCase):
|
||||
self.assertEqual(item["shipment_id"], self.shipment.id)
|
||||
self.assertEqual(item["shipment_date"], "2026-01-14")
|
||||
|
||||
def test_get_sales_items_by_external_order_id(self):
|
||||
"""测试通过 external_order_id 查询销售品"""
|
||||
url = "/api/v1/shipment/sales-items/by-printing-order/EXT-PO-001/"
|
||||
response = self.client.get(url)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(data["count"], 2)
|
||||
item_ids = [item["id"] for item in data["results"]]
|
||||
self.assertIn(self.sales_item1.id, item_ids)
|
||||
self.assertIn(self.sales_item2.id, item_ids)
|
||||
self.assertNotIn(self.sales_item3.id, item_ids)
|
||||
|
||||
def test_get_sales_items_by_external_order_id_multiple_matches(self):
|
||||
"""测试 external_order_id 匹配多个生产订单时报错"""
|
||||
other_order = printing_models.PrintingOrder.objects.create(
|
||||
merchant=self.merchant,
|
||||
customer=self.customer,
|
||||
fabric="测试面料3",
|
||||
width="160cm",
|
||||
process=self.process,
|
||||
created_by=self.user,
|
||||
external_order_id="EXT-DUPLICATED",
|
||||
)
|
||||
printing_models.PrintingOrder.objects.create(
|
||||
merchant=self.merchant,
|
||||
customer=self.customer,
|
||||
fabric="测试面料4",
|
||||
width="170cm",
|
||||
process=self.process,
|
||||
created_by=self.user,
|
||||
external_order_id="EXT-DUPLICATED",
|
||||
)
|
||||
printing_models.PrintingJob.objects.create(
|
||||
merchant=self.merchant,
|
||||
printing_order=other_order,
|
||||
product=self.product,
|
||||
quantity=50,
|
||||
unit="米",
|
||||
created_by=self.user,
|
||||
)
|
||||
|
||||
response = self.client.get(
|
||||
"/api/v1/shipment/sales-items/by-printing-order/EXT-DUPLICATED/"
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn("匹配到多个生产订单", response.json()["detail"])
|
||||
|
||||
def test_get_sales_items_printing_order_not_found(self):
|
||||
"""测试生产订单不存在"""
|
||||
url = "/api/v1/shipment/sales-items/by-printing-order/99999/"
|
||||
|
||||
@@ -361,6 +361,7 @@ class SalesItemByPrintingOrderView(APIView):
|
||||
GET /api/v1/shipment/sales-items/by-printing-order/<printing_order_id>/
|
||||
|
||||
返回与该 PrintingOrder 下所有 PrintingJob 关联的 SalesItem 列表。
|
||||
路径参数既支持内部 ID,也支持 external_order_id。
|
||||
|
||||
查询参数:
|
||||
- include_already_has_shipment: 是否包含已关联出货单的销售品(true/false),默认 false
|
||||
@@ -393,12 +394,43 @@ class SalesItemByPrintingOrderView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request, printing_order_id):
|
||||
# 验证生产订单是否存在
|
||||
from printing.models import PrintingOrder
|
||||
|
||||
try:
|
||||
printing_order = PrintingOrder.objects.get(id=printing_order_id)
|
||||
except PrintingOrder.DoesNotExist:
|
||||
user = request.user
|
||||
if getattr(user, "is_superuser", False):
|
||||
base_queryset = PrintingOrder.objects.all()
|
||||
else:
|
||||
emp = getattr(user, "employee", None)
|
||||
merchant = getattr(emp, "merchant", None) if emp else None
|
||||
if not merchant:
|
||||
return Response(
|
||||
{"detail": f"生产订单 {printing_order_id} 不存在"},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
base_queryset = PrintingOrder.objects.filter(merchant=merchant)
|
||||
|
||||
printing_order = None
|
||||
if printing_order_id.isdigit():
|
||||
printing_order = base_queryset.filter(id=int(printing_order_id)).first()
|
||||
|
||||
if printing_order is None:
|
||||
matched_orders = list(
|
||||
base_queryset.filter(external_order_id=printing_order_id).only("id")[:2]
|
||||
)
|
||||
if len(matched_orders) > 1:
|
||||
return Response(
|
||||
{
|
||||
"detail": (
|
||||
f"external_order_id {printing_order_id} 匹配到多个生产订单,"
|
||||
"请改用内部ID查询"
|
||||
)
|
||||
},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if len(matched_orders) == 1:
|
||||
printing_order = matched_orders[0]
|
||||
|
||||
if printing_order is None:
|
||||
return Response(
|
||||
{"detail": f"生产订单 {printing_order_id} 不存在"},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
|
||||
Reference in New Issue
Block a user