1
0
forked from erp-dev/erp
Files
erpnew/business/tasks.py

57 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
from typing import Any, Dict, List
from celery import shared_task
from django.contrib.auth import get_user_model
from business import models as business_models
from stock import models as stock_models
from stock.services import StockFlowService
logger = logging.getLogger(__name__)
@shared_task(bind=True)
def create_purchase_order_stock_entries(
self,
*,
purchase_order_id: int,
warehouse_id: int,
items: List[Dict[str, Any]],
created_by_id: int | None = None,
):
"""
为采购单创建入库记录。仅支持入库StockFlowService.stock_in
"""
try:
purchase_order = business_models.PurchaseOrder.objects.select_related('merchant').get(id=purchase_order_id)
except business_models.PurchaseOrder.DoesNotExist:
logger.error('PurchaseOrder %s 不存在,无法创建入库单', purchase_order_id)
return {'error': 'purchase_order_not_found', 'purchase_order_id': purchase_order_id}
merchant = purchase_order.merchant
created_by = None
if created_by_id:
UserModel = get_user_model()
created_by = UserModel.objects.filter(id=created_by_id).first()
service = StockFlowService(merchant=merchant, created_by=created_by)
record, details, created_count = service.stock_in(
warehouse_id=warehouse_id,
source_type=stock_models.StockChangeSourceEnum.PURCHASE,
source_id=purchase_order.id,
items=items,
)
payload = {
'task_id': self.request.id,
'purchase_order_id': purchase_order.id,
'stock_change_record_id': getattr(record, 'id', None),
'created_details_count': created_count,
}
logger.info('采购单 %s 入库任务完成: %s', purchase_order.id, payload)
return payload