1
0
forked from erp-dev/erp

feat: auto complete stock change

This commit is contained in:
2025-11-10 09:01:25 +08:00
parent 37c2e7d723
commit b2078dfa46
82 changed files with 1071 additions and 1336 deletions

View File

@@ -4,7 +4,7 @@ from decimal import Decimal
from unittest.mock import patch, MagicMock
import logging
from basic_info.models import Product, WareHouse, Supplier, ProductUnitEnum, ProductCategory
from basic_info.models import Product, WareHouse, Supplier, ProductUnitEnum, ProductCategory, Merchant, MerchantTypeEnum
from . import models, services
@@ -18,8 +18,18 @@ class StockServicesTestCase(TestCase):
cls.unit_meter = ProductUnitEnum.METER
cls.unit_kg = ProductUnitEnum.KG
# 创建测试商户
cls.merchant = Merchant.objects.create(
name="测试商户",
type=MerchantTypeEnum.STORE,
area="测试地区",
mobile="13800138000",
contact="测试联系人"
)
# 创建产品类别
cls.fabric_category = ProductCategory.objects.create(
merchant=cls.merchant,
name="布料类",
description="各种布料产品",
product_prefix="FAB"
@@ -27,16 +37,19 @@ class StockServicesTestCase(TestCase):
# 创建仓库
cls.warehouse_main = WareHouse.objects.create(
merchant=cls.merchant,
name="主仓库",
location="主仓库地址"
)
cls.warehouse_backup = WareHouse.objects.create(
merchant=cls.merchant,
name="备用仓库",
location="备用仓库地址"
)
# 创建供应商
cls.supplier = Supplier.objects.create(
merchant=cls.merchant,
name="测试供应商",
contact="张三",
mobile="13800138000"
@@ -44,6 +57,7 @@ class StockServicesTestCase(TestCase):
# 创建产品
cls.product_fabric_a = Product.objects.create(
merchant=cls.merchant,
category=cls.fabric_category,
name="布料A",
human_id="FABRIC-A-001",
@@ -54,6 +68,7 @@ class StockServicesTestCase(TestCase):
unit=cls.unit_meter
)
cls.product_fabric_b = Product.objects.create(
merchant=cls.merchant,
category=cls.fabric_category,
name="布料B",
human_id="FABRIC-B-001",
@@ -68,6 +83,7 @@ class StockServicesTestCase(TestCase):
"""每个测试方法执行前的设置"""
# 创建采购单
self.purchase_order = models.PurchaseOrder.objects.create(
merchant=self.merchant,
supplier=self.supplier,
order_date=timezone.now().date(),
total_amount=Decimal('1000.00'),
@@ -76,6 +92,7 @@ class StockServicesTestCase(TestCase):
# 创建库存变动记录(入库)
self.stock_change_in = models.StockChangeRecord.objects.create(
merchant=self.merchant,
type=models.StockChangeTypeEnum.ADD,
source_type=models.StockChangeSourceEnum.PURCHASE,
source_id=self.purchase_order.id,
@@ -85,6 +102,7 @@ class StockServicesTestCase(TestCase):
# 创建库存变动记录(出库)
self.stock_change_out = models.StockChangeRecord.objects.create(
merchant=self.merchant,
type=models.StockChangeTypeEnum.REMOVE,
source_type=models.StockChangeSourceEnum.SALES,
source_id=1,
@@ -94,6 +112,7 @@ class StockServicesTestCase(TestCase):
# 创建库存变动明细
self.stock_detail_in = models.StockChangeDetail.objects.create(
merchant=self.merchant,
stock_change_record=self.stock_change_in,
product=self.product_fabric_a,
quantity=Decimal('100'),
@@ -101,6 +120,7 @@ class StockServicesTestCase(TestCase):
)
self.stock_detail_out = models.StockChangeDetail.objects.create(
merchant=self.merchant,
stock_change_record=self.stock_change_out,
product=self.product_fabric_a,
quantity=Decimal('50'),
@@ -115,6 +135,7 @@ class FindInventoryTestCase(StockServicesTestCase):
"""测试查找已存在的库存记录"""
# 先创建一个库存记录
inventory = models.Inventory.objects.create(
merchant=self.merchant,
product=self.product_fabric_a,
warehouse=self.warehouse_main,
quantity=50,
@@ -140,11 +161,11 @@ class FindInventoryTestCase(StockServicesTestCase):
)
self.assertIsNone(result)
def test_find_inventory_wrong_warehouse(self):
"""测试在错误的仓库中查找库存"""
# 在主仓库创建库存
models.Inventory.objects.create(
merchant=self.merchant,
product=self.product_fabric_a,
warehouse=self.warehouse_main,
quantity=50,
@@ -162,11 +183,11 @@ class FindInventoryTestCase(StockServicesTestCase):
class CreateStockSnapshotTestCase(StockServicesTestCase):
"""测试 create_stock_snapshot 函数"""
def setUp(self):
super().setUp()
# 创建库存记录
self.inventory = models.Inventory.objects.create(
merchant=self.merchant,
product=self.product_fabric_a,
warehouse=self.warehouse_main,
quantity=150,
@@ -213,6 +234,7 @@ class CreateStockSnapshotTestCase(StockServicesTestCase):
# 测试未保存的明细记录
unsaved_detail = models.StockChangeDetail(
merchant=self.merchant,
stock_change_record=self.stock_change_in,
product=self.product_fabric_a,
quantity=50
@@ -231,6 +253,7 @@ class MakeStockChangeCompletedTestCase(StockServicesTestCase):
# 测试未保存的记录
unsaved_record = models.StockChangeRecord(
merchant=self.merchant,
type=models.StockChangeTypeEnum.ADD,
warehouse=self.warehouse_main
)
@@ -272,12 +295,12 @@ class MakeStockChangeCompletedTestCase(StockServicesTestCase):
product_id=self.product_fabric_a.id,
warehouse_id=self.warehouse_main.id
)
mock_snapshot.assert_called_once()
def test_complete_inbound_record_existing_inventory(self):
"""测试完成入库记录(更新现有库存)"""
# 创建现有库存
existing_inventory = models.Inventory.objects.create(
merchant=self.merchant,
product=self.product_fabric_a,
warehouse=self.warehouse_main,
quantity=50,
@@ -304,6 +327,7 @@ class MakeStockChangeCompletedTestCase(StockServicesTestCase):
"""测试完成出库记录"""
# 创建现有库存
existing_inventory = models.Inventory.objects.create(
merchant=self.merchant,
product=self.product_fabric_a,
warehouse=self.warehouse_main,
quantity=100,
@@ -326,6 +350,7 @@ class MakeStockChangeCompletedTestCase(StockServicesTestCase):
"""测试完成包含多个明细的记录"""
# 添加另一个明细
models.StockChangeDetail.objects.create(
merchant=self.merchant,
stock_change_record=self.stock_change_in,
product=self.product_fabric_b,
quantity=Decimal('200'),