1
0
forked from erp-dev/erp

feat: added n-to-1 relation between sales_order_item and printing_job model

This commit is contained in:
2025-12-11 18:09:10 +08:00
parent 633031d7cb
commit 1a8c446365
30 changed files with 41736 additions and 40 deletions

View File

@@ -4,7 +4,7 @@ import logging
from collections import OrderedDict
from datetime import date, datetime
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
from typing import Any, Dict, Iterable, List, Tuple
from typing import Any, Dict, Iterable, List, Tuple, Optional
from django.contrib.auth import get_user_model
from django.db import transaction
@@ -260,6 +260,7 @@ def create_sales_order(
warehouse=warehouse,
items=items,
is_outgoing=True,
customer=customer,
)
with transaction.atomic():
@@ -286,6 +287,7 @@ def create_sales_order(
consume_detail_ids=item_data.get('consume_detail_ids'),
batch_number=item_data.get('batch_number'),
remarks=item_data.get('remarks'),
printing_job=item_data.get('printing_job'),
)
for item_data in sales_items
]
@@ -455,7 +457,7 @@ def create_payment_order(
创建付款单(资金流出)。
"""
normalized_date = _normalize_order_date(payment_date)
normalized_amount = _ensure_positive_amount(amount, 'amount')
normalized_amount = _ensure_non_zero_amount(amount, 'amount')
normalized_discount = _ensure_non_negative_amount(discount_amount, 'discount_amount')
if bank_account and bank_account.merchant_id != merchant.id:
@@ -493,7 +495,7 @@ def create_receipt_order(
创建收款单(资金流入)。
"""
normalized_date = _normalize_order_date(receipt_date)
normalized_amount = _ensure_positive_amount(amount, 'amount')
normalized_amount = _ensure_non_zero_amount(amount, 'amount')
normalized_discount = _ensure_non_negative_amount(discount_amount, 'discount_amount')
if bank_account and bank_account.merchant_id != merchant.id:
@@ -740,6 +742,7 @@ def _normalize_order_items(
warehouse: basic_info_models.WareHouse,
items: List[Dict[str, Any]],
is_outgoing: bool,
customer: Optional[basic_info_models.Customer] = None,
) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
"""
根据仓库模式校验订单明细,并返回:
@@ -768,6 +771,25 @@ def _normalize_order_items(
remarks = raw_item.get('remarks')
spec = raw_item.get('spec')
unit = raw_item.get('unit') or product.get_unit_display() or ''
printing_job = None
raw_printing_job_id = raw_item.get('printing_job') or raw_item.get('printing_job_id')
if raw_printing_job_id is not None:
try:
printing_job_id = int(raw_printing_job_id)
except (TypeError, ValueError):
raise ValueError(f'items[{index}].printing_job 必须为数字')
from printing import models as printing_models
try:
printing_job = printing_models.PrintingJob.objects.select_related(
'product', 'printing_order__customer'
).get(id=printing_job_id)
except printing_models.PrintingJob.DoesNotExist as exc:
raise ValueError(f'items[{index}].printing_job 不存在或已删除') from exc
if printing_job.product_id != product.id:
raise ValueError(f'items[{index}].printing_job 对应的产品与当前明细不一致')
if customer and printing_job.printing_order and printing_job.printing_order.customer_id != customer.id:
raise ValueError(f'items[{index}].printing_job 客户不匹配')
quantity = 0
num_of_rolls = 0
@@ -844,6 +866,7 @@ def _normalize_order_items(
'remarks': remarks,
'spec': spec,
'consume_detail_ids': consume_detail_ids_str,
'printing_job': printing_job,
})
return normalized_items, stock_flow_items
@@ -1409,10 +1432,10 @@ def _to_decimal(value, field_name: str) -> Decimal:
raise ValueError(f'{field_name} 必须是合法数值') from exc
def _ensure_positive_amount(value, field_name: str) -> Decimal:
def _ensure_non_zero_amount(value, field_name: str) -> Decimal:
amount = _to_decimal(value, field_name)
if amount <= 0:
raise ValueError(f'{field_name} 必须大于 0')
if amount == 0:
raise ValueError(f'{field_name} 不能为 0')
return amount