赞
踩
当订单售出产品类商品或套盒类商品时,在有库存的情况下,系统会自动抛出货单。
class Ship(models.Model):
order_detail = models.ForeignKey(OrderDetail, verbose_name='对应订单单身', on_delete=models.PROTECT)
store = models.ForeignKey(Store, verbose_name='所属门店', on_delete=models.PROTECT)
good = models.ForeignKey(Good, verbose_name='商品', on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=0, verbose_name='商品数量')
created = models.DateTimeField(auto_now_add=True, verbose_name='出货时间')
create_user = models.ForeignKey('auth.User', verbose_name='建立人员', on_delete=models.PROTECT)
def __str__(self):
return '{}'.format(self.id)
class Meta:
verbose_name = '出货单'
verbose_name_plural = verbose_name
记录哪些商品在哪些合作门店的仓库内的储存数量。
class Distrib(models.Model):
good = models.ForeignKey(Good, verbose_name='商品', on_delete=models.CASCADE)
store = models.ForeignKey(Store, verbose_name='所属门店', on_delete=models.PROTECT)
storage = models.ForeignKey(Storage, verbose_name='仓库', on_delete=models.PROTECT)
quantity = models.PositiveIntegerField(default=0, verbose_name='商品数量')
def __str__(self):
return '{}'.format(self.id)
class Meta:
verbose_name = '商品库存分布'
verbose_name_plural = verbose_name
记录商品数量的异动过程。
TRAIN_FORM_CHOICES = (
('1', '出货单'),
('2', '出货退回单'),
('3', '商品库存异动申请单'),
)
class Tran(models.Model):
good = models.ForeignKey(Good, verbose_name='商品', on_delete=models.CASCADE)
source_form = models.CharField(max_length=1, choices=TRAIN_FORM_CHOICES, verbose_name='来源单类别')
source_id = models.PositiveIntegerField(verbose_name='来源单号')
store = models.ForeignKey(Store, verbose_name='所属门店', on_delete=models.PROTECT)
storage = models.ForeignKey(Storage, verbose_name='异动仓库', on_delete=models.CASCADE)
from_quantity = models.PositiveIntegerField(default=0, verbose_name='异动前数量')
tran_quantity = models.IntegerField(default=0, verbose_name='异动数量')
to_quantity = models.PositiveIntegerField(default=0, verbose_name='异动后数量')
created = models.DateTimeField(auto_now=True, verbose_name='异动日期')
create_user = models.ForeignKey('auth.User', verbose_name='异动人员', on_delete=models.PROTECT)
def __str__(self):
return '{}'.format(self.id)
class Meta:
verbose_name = '商品库存异动'
verbose_name_plural = verbose_name
因本系统尚未开发进货单,如需要库存则先用本单据。
class Reshuffle(models.Model):
good = models.ForeignKey(Good, verbose_name='商品', on_delete=models.PROTECT)
store = models.ForeignKey(Store, verbose_name='所属门店', on_delete=models.PROTECT)
from_storage = models.ForeignKey(Storage, verbose_name='移出仓库', on_delete=models.CASCADE,
related_name='reshuffle_from_storage', blank=True, null=True)
to_storage = models.ForeignKey(Storage, verbose_name='移入仓库', on_delete=models.CASCADE,
related_name='reshuffle_to_storage', blank=True, null=True)
quantity = models.PositiveIntegerField(verbose_name='异动数量',)
notes = models.CharField(max_length=64, verbose_name='申请原因')
created = models.DateTimeField(auto_now=True, verbose_name='建立日期')
create_user = models.ForeignKey('auth.User', verbose_name='建立人员', on_delete=models.PROTECT)
def __str__(self):
return '{}'.format(self.id)
class Meta:
verbose_name = '商品库存异动申请单'
verbose_name_plural = verbose_name
出货单资料是订单完成时自动产生,故add/change/delete权限都是关闭的。
@admin.register(Ship)
class ShipAdmin(admin.ModelAdmin):
list_display = ['id', 'order_detail', 'store', 'good', 'quantity', 'created', 'create_user']
fields = ['order_detail', 'store', 'good', 'quantity', 'created', 'create_user']
view_on_site = False
def has_add_permission(self, request, obj=None):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
哪些商品在哪些仓库有多少库存是由系统自动计算后得到的,故add/change/delete权限都是关闭的。
@admin.register(Distrib)
class DistribAdmin(admin.ModelAdmin):
list_display = ['id', 'good', 'store', 'storage', 'quantity']
fields = ['good', 'storage', 'store', 'quantity']
view_on_site = False
def has_add_permission(self, request, obj=None):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
记录商品在仓库的异动过程,由系统自动记录,故add/change/delete权限都是关闭的。
@admin.register(Tran)
class TranAdmin(admin.ModelAdmin):
list_display = ['id', 'good', 'source_form', 'source_id', 'store', 'storage', 'from_quantity',
'tran_quantity', 'to_quantity']
fields = ['good', 'source_form', 'source_id', 'store', 'storage', 'from_quantity', 'tran_quantity',
'to_quantity', 'created', 'create_user']
view_on_site = False
def has_add_permission(self, request, obj=None):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
可人工使用商品库存异动申请单新增商品的商品库存分布,新增后则不可修改与删除。
"""
商品库存异动申请单检查
1.异动数量不得小于等于0
2.移出/移入仓库不得同时为空
3.移出/移入仓库不得相同
4.当移出/移入仓库不为空时,都必须是该门店的仓库(不允许跨店异动)
5.移出仓库不为空时,异动数量不得大于移出仓库的库存数量
"""
class ReshuffleCheckForm(forms.ModelForm):
def clean(self):
super(ReshuffleCheckForm, self).clean()
quantity = self.cleaned_data.get('quantity')
from_storage = self.cleaned_data.get('from_storage')
to_storage = self.cleaned_data.get('to_storage')
store = self.cleaned_data.get('store')
good = self.cleaned_data.get('good')
if quantity <= 0:
raise forms.ValidationError('异动数量不得小于等于0。')
if from_storage is None and to_storage is None:
raise forms.ValidationError('移出/移入仓库不得同时为空。')
if from_storage == to_storage:
raise forms.ValidationError('移出/移入仓库不得相同。')
if from_storage is not None and from_storage.store != store:
raise forms.ValidationError('移出仓库不是[{}]门店的仓库。'.format(store.title))
if to_storage is not None and to_storage.store != store:
raise forms.ValidationError('移入仓库不是[{}]门店的仓库。'.format(store.title))
if from_storage is not None:
distribs = Distrib.objects.filter(storage=from_storage, good=good)
if distribs.count() == 0:
raise forms.ValidationError('移出仓库无该商品的库存数量。')
if distribs.first().quantity < quantity:
raise forms.ValidationError('异动数量不得大于移出仓库的库存数量。')
@admin.register(Reshuffle)
class ReshuffleAdmin(admin.ModelAdmin):
list_display = ['id', 'good', 'store', 'from_storage', 'to_storage', 'quantity', 'notes']
fields = ['good', 'store', 'from_storage', 'to_storage', 'quantity', 'notes', 'created', 'create_user']
readonly_fields = ['created', 'create_user']
form = ReshuffleCheckForm
view_on_site = False
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "good":
kwargs["queryset"] = Good.objects.filter(Q(type='1') | Q(type='4'))
return super().formfield_for_foreignkey(db_field, request, **kwargs)
def save_model(self, request, obj, form, change):
#只能新增不能修改
if not change:
obj.create_user = request.user
super().save_model(request, obj, form, change)
#如果移出储位不为空时
if obj.from_storage is not None:
from_distrib = Distrib.objects.filter(good=obj.good, storage=obj.from_storage).first()
# 新增[商品库存异动]
from_tran = Tran()
from_tran.good = obj.good
from_tran.source_form = '3'
from_tran.source_id = obj.id
from_tran.store = obj.store
from_tran.storage = obj.from_storage
from_tran.from_quantity = from_distrib.quantity
from_tran.tran_quantity = obj.quantity * -1
from_tran.to_quantity = from_distrib.quantity - obj.quantity
from_tran.create_user = request.user
from_tran.save()
# 修改[商品库存分布]
from_distrib.quantity -= obj.quantity
from_distrib.save()
# 如果移入储位不为空时
if obj.to_storage is not None:
# 新增[商品库存异动]
to_tran = Tran()
to_tran.good = obj.good
to_tran.source_form = '3'
to_tran.source_id = obj.id
to_tran.store = obj.store
to_tran.storage = obj.to_storage
to_tran.tran_quantity = obj.quantity
to_distribs = Distrib.objects.filter(good=obj.good, storage=obj.to_storage)
if to_distribs.count() > 0:
to_distrib = to_distribs.first()
to_tran.from_quantity = to_distrib.quantity
to_tran.to_quantity = to_distrib.quantity + obj.quantity
# 修改[商品库存分布]
to_distrib.quantity += obj.quantity
to_distrib.save()
else:
to_tran.from_quantity = 0
to_tran.to_quantity = obj.quantity
# 新增[商品库存分布]
to_distrib = Distrib()
to_distrib.good = obj.good
to_distrib.store = obj.store
to_distrib.storage = obj.to_storage
to_distrib.quantity = obj.quantity
to_distrib.save()
to_tran.create_user = request.user
to_tran.save()
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。