diff --git a/api_v1/test_pre_sales_order_api.py b/api_v1/test_pre_sales_order_api.py index 5e62f91..821f280 100644 --- a/api_v1/test_pre_sales_order_api.py +++ b/api_v1/test_pre_sales_order_api.py @@ -229,6 +229,7 @@ class PreSalesOrderAPITestCase(TestCase): self.assertEqual(convert_resp.data['status'], business_models.SalesOrderStatusEnum.PENDING) sales_order = business_models.SalesOrder.objects.get(id=convert_resp.data['id']) self.assertEqual(sales_order.from_pre_sales_order_id, order_id) + self.assertEqual(sales_order.items.first().quantity_of_rolls, '10') def test_detail_filters_cancelled_allocation_records(self): """单体查询应过滤掉已撤销的配货记录""" diff --git a/business/services.py b/business/services.py index dc97f95..50f561d 100644 --- a/business/services.py +++ b/business/services.py @@ -1130,6 +1130,26 @@ def _normalize_order_items( consume_detail_ids_str = ','.join(str(value) for value in normalized_ids) quantity = _to_positive_int(raw_item.get('quantity'), f'items[{index}].quantity') num_of_rolls = len(normalized_ids) + details_by_id = stock_models.StockChangeDetail.objects.select_related( + 'stock_change_record' + ).filter( + id__in=normalized_ids, + merchant=merchant, + ).in_bulk() + if len(details_by_id) == len(normalized_ids): + quantities: List[str] = [] + for pos, detail_id in enumerate(normalized_ids): + detail = details_by_id[detail_id] + if detail.product_id != product.id: + raise ValueError( + f'items[{index}].consume_detail_ids[{pos}] 产品不匹配' + ) + if detail.stock_change_record.warehouse_id != warehouse.id: + raise ValueError( + f'items[{index}].consume_detail_ids[{pos}] 仓库不匹配' + ) + quantities.append(format(detail.quantity.normalize(), 'f')) + quantity_of_rolls = ','.join(quantities) stock_flow_items.append({ 'product_id': product.id, 'consume_detail_ids': normalized_ids, diff --git a/business/tests/test_pre_sales_order.py b/business/tests/test_pre_sales_order.py index 931a070..e52d1f0 100644 --- a/business/tests/test_pre_sales_order.py +++ b/business/tests/test_pre_sales_order.py @@ -192,6 +192,7 @@ class PreSalesOrderServiceTestCase(TestCase): sales_item = sales_order.items.first() self.assertEqual(sales_item.product_id, self.product.id) self.assertEqual(sales_item.consume_detail_ids, str(stock_detail.id)) + self.assertEqual(sales_item.quantity_of_rolls, '10') class PreSalesOrderSignalTestCase(TransactionTestCase): diff --git a/business/tests/test_sales_order.py b/business/tests/test_sales_order.py index 1ddde96..007d2b6 100644 --- a/business/tests/test_sales_order.py +++ b/business/tests/test_sales_order.py @@ -28,11 +28,32 @@ class SalesOrderServiceTestCase(TestCase): self.relaxed_items = [ {'product_id': self.product.id, 'quantity': 80, 'num_of_rolls': 2, 'price': '28.0', 'unit': '米'} ] + stock_record = stock_models.StockChangeRecord.objects.create( + merchant=self.merchant, + type=stock_models.StockChangeTypeEnum.ADD, + warehouse=self.warehouse_strict_out, + source_type=stock_models.StockChangeSourceEnum.PURCHASE, + created_by=self.user, + ) + stock_detail_1 = stock_models.StockChangeDetail.objects.create( + merchant=self.merchant, + product=self.product, + unit=self.product.unit, + stock_change_record=stock_record, + quantity='6', + ) + stock_detail_2 = stock_models.StockChangeDetail.objects.create( + merchant=self.merchant, + product=self.product, + unit=self.product.unit, + stock_change_record=stock_record, + quantity='4', + ) self.strict_out_items = [ { 'product_id': self.product.id, - 'consume_detail_ids': [1, 2], - 'quantity': 40, + 'consume_detail_ids': [stock_detail_1.id, stock_detail_2.id], + 'quantity': 10, 'price': '32.0', 'unit': '米', } @@ -169,7 +190,9 @@ class SalesOrderServiceTestCase(TestCase): created_by=self.user, ) item = sales_order.items.first() - self.assertEqual(item.consume_detail_ids, '1,2') + consume_ids = [str(value) for value in self.strict_out_items[0]['consume_detail_ids']] + self.assertEqual(item.consume_detail_ids, ','.join(consume_ids)) + self.assertEqual(item.quantity_of_rolls, '6,4') def test_sales_order_outgoing_missing_consume_ids_raises(self): payload = [