HimoolERP/apps/goods/models.py

102 lines
4.6 KiB
Python
Raw Normal View History

2021-11-04 23:49:56 +08:00
from extensions.models import *
2021-11-04 17:35:34 +08:00
2021-11-04 23:49:56 +08:00
2021-11-05 18:49:51 +08:00
class Goods(Model):
"""商品"""
2021-11-06 00:55:40 +08:00
number = CharField(max_length=32, verbose_name='编号')
name = CharField(max_length=64, verbose_name='名称')
2021-11-14 02:33:24 +08:00
barcode = CharField(max_length=32, null=True, blank=True, verbose_name='条码')
2021-11-06 00:55:40 +08:00
category = ForeignKey('data.GoodsCategory', on_delete=SET_NULL, null=True,
related_name='goods_set', verbose_name='商品分类')
unit = ForeignKey('data.GoodsUnit', on_delete=SET_NULL, null=True,
related_name='goods_set', verbose_name='商品单位')
spec = CharField(max_length=64, null=True, blank=True, verbose_name='商品规格')
2021-11-13 01:08:48 +08:00
enable_batch_control = BooleanField(default=False, verbose_name='启用批次控制')
shelf_life_days = IntegerField(null=True, verbose_name='保质期天数')
2021-11-06 00:55:40 +08:00
shelf_life_warning_days = IntegerField(default=0, verbose_name='保质期预警天数')
enable_inventory_warning = BooleanField(default=False, verbose_name='启用库存警告')
inventory_upper = FloatField(null=True, verbose_name='库存上限')
inventory_lower = FloatField(null=True, verbose_name='库存下限')
purchase_price = AmountField(verbose_name='采购价')
retail_price = AmountField(verbose_name='零售价')
level_price1 = AmountField(verbose_name='等级价一')
level_price2 = AmountField(verbose_name='等级价二')
level_price3 = AmountField(verbose_name='等级价三')
2021-11-05 18:49:51 +08:00
2021-11-06 00:55:40 +08:00
remark = CharField(max_length=256, null=True, blank=True, verbose_name='备注')
order = IntegerField(default=100, verbose_name='顺序')
is_active = BooleanField(default=True, verbose_name='激活状态')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='goods_set')
class Meta:
unique_together = [('number', 'team'), ('name', 'team')]
2021-11-12 15:37:43 +08:00
@classmethod
def get_number(cls, team):
instance = cls.objects.filter(team=team).last()
try:
result = re.match('^(.*?)([1-9]+)$', instance.number)
number = result.group(1) + str(int(result.group(2)) + 1)
except AttributeError:
number = 'G000000000001'
return number
2021-11-13 15:15:45 +08:00
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
self.has_stock = self.remain_quantity > 0
if update_fields:
update_fields.append('has_stock')
return super().save(force_insert, force_update, using, update_fields)
2021-11-06 00:55:40 +08:00
2021-11-05 18:49:51 +08:00
class Batch(Model):
"""批次"""
2021-11-06 00:55:40 +08:00
number = CharField(max_length=32, verbose_name='编号')
2021-11-09 23:17:54 +08:00
warehouse = ForeignKey('data.Warehouse', on_delete=CASCADE, related_name='batchs', verbose_name='仓库')
2021-11-06 00:55:40 +08:00
goods = ForeignKey('goods.Goods', on_delete=CASCADE, related_name='batchs', verbose_name='商品')
total_quantity = FloatField(verbose_name='批次数量')
remain_quantity = FloatField(verbose_name='批次剩余数量')
production_date = DateField(null=True, verbose_name='生产日期')
2021-11-13 01:08:48 +08:00
shelf_life_days = IntegerField(null=True, verbose_name='保质期天数')
2021-11-06 00:55:40 +08:00
expiration_date = DateField(null=True, verbose_name='过期日期')
2021-11-11 00:23:25 +08:00
has_stock = BooleanField(default=True, verbose_name='库存状态')
2021-11-06 00:55:40 +08:00
create_time = DateTimeField(auto_now_add=True, verbose_name='创建时间')
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='batchs')
2021-11-09 23:17:54 +08:00
class Meta:
unique_together = [('number', 'warehouse', 'goods', 'team')]
2021-11-12 15:29:38 +08:00
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
self.has_stock = self.remain_quantity > 0
if update_fields:
update_fields.append('has_stock')
return super().save(force_insert, force_update, using, update_fields)
2021-11-05 18:49:51 +08:00
class Inventory(Model):
"""库存"""
2021-11-06 00:55:40 +08:00
warehouse = ForeignKey('data.Warehouse', on_delete=CASCADE, related_name='inventories', verbose_name='仓库')
goods = ForeignKey('goods.Goods', on_delete=CASCADE, related_name='inventories', verbose_name='商品')
2021-11-13 12:56:55 +08:00
initial_quantity = FloatField(default=0, verbose_name='初始库存')
2021-11-07 18:12:52 +08:00
total_quantity = FloatField(default=0, verbose_name='库存总数')
2021-11-11 00:23:25 +08:00
has_stock = BooleanField(default=False, verbose_name='库存状态')
2021-11-06 00:55:40 +08:00
team = ForeignKey('system.Team', on_delete=CASCADE, related_name='inventories')
2021-11-05 18:49:51 +08:00
2021-11-09 23:17:54 +08:00
class Meta:
unique_together = [('warehouse', 'goods', 'team')]
2021-11-12 15:29:38 +08:00
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
self.has_stock = self.total_quantity > 0
if update_fields:
update_fields.append('has_stock')
return super().save(force_insert, force_update, using, update_fields)
2021-11-04 23:49:56 +08:00
2021-11-06 00:55:40 +08:00
__all__ = [
2021-11-09 23:17:54 +08:00
'Goods', 'Batch', 'Inventory',
2021-11-04 23:49:56 +08:00
]