赞
踩
STATUS_CHOICES = (
('1', '正常'),
('2', '作废'),
)
class Order(models.Model):
created = models.DateTimeField(auto_now_add=True, verbose_name='建立时间')
store = models.ForeignKey(Store, verbose_name='交易门店', on_delete=models.PROTECT)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='1', verbose_name='状态')
employee = models.ForeignKey(Employee, verbose_name='销售人员', on_delete=models.PROTECT,
related_name='order_employee')
customer = models.ForeignKey(Customer, verbose_name='顾客', on_delete=models.PROTECT)
amount = models.DecimalField(max_digits=16, decimal_places=2, default=0, verbose_name='售价')
notes = models.CharField(max_length=64, blank=True, verbose_name='其他说明')
create_user = models.ForeignKey('auth.User', verbose_name='建立人员', on_delete=models.PROTECT,
related_name='order_create_user')
updated = models.DateTimeField(verbose_name='异动时间', null=True)
update_user = models.ForeignKey('auth.User', verbose_name='异动人员', on_delete=models.PROTECT,
related_name='order_update_user', null=True)
def __str__(self):
return '{}'.format(self.pk)
class Meta:
verbose_name = '订单'
verbose_name_plural = verbose_name
class OrderDetail(models.Model):
order = models.ForeignKey(Order, verbose_name='订单单头', on_delete=models.PROTECT)
good = models.ForeignKey(Good, verbose_name='商品', on_delete=models.PROTECT)
quantity = models.PositiveIntegerField(verbose_name='数量')
unit = models.ForeignKey(Unit, verbose_name='单位', null=True, blank=True, on_delete=models.PROTECT)
def __str__(self):
return '{}-{}-{}-{}'.format(self.order.id, self.id, self.order.customer.title, self.good.title)
class Meta:
verbose_name = '订单单身'
verbose_name_plural = verbose_name
订单完成后就不可以再修改与删除。
"""
订单单身检查
1.订单至少要有一笔单身
2.单身的数量必须都大于0
3.1[产品]/4[套盒]类商品店内仓库库存量必须大于等于单身数量
4.[有效期间服务]类型商品一次只能卖出一个
5.[套盒]类商品必须有内容
"""
class OrderDetailCheckInlineFormset(forms.models.BaseInlineFormSet):
def clean(self):
count = 0
for form in self.forms:
if form.cleaned_data:
count += 1
quantity = form.cleaned_data.get('quantity')
order = form.cleaned_data.get('order')
good = form.cleaned_data.get('good')
if quantity is None or quantity <= 0:
raise forms.ValidationError("单身的数量必须都大于0。")
#如果是[1产品][4套盒]类商品则检查库存是否足够
if good.type == '1' or good.type == '4':
distribs = Distrib.objects.filter(good=good, store=order.store, quantity__gte=quantity)
if distribs.count() == 0:
raise forms.ValidationError("[{}]无该商品库存或是库存数量少于单身数量。".format(order.store.title))
if good.type == '4':
composes = Compose.objects.filter(main=good)
if composes.count() == 0:
raise forms.ValidationError("请先新增[{}-{}]套盒类商品内容。".format(good.id, good.title))
elif good.type == '3':
if quantity > 1:
raise forms.ValidationError("[{}]是有效期间服务类型商品一次只能卖出一个。".format(good.title))
if count < 1:
raise forms.ValidationError("订单至少要有一笔单身。")
class OrderDetailInline(admin.TabularInline):
model = OrderDetail
formset = OrderDetailCheckInlineFormset
fields = ['order', 'good', 'quantity']
extra = 1
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "good":
kwargs["queryset"] = Good.objects.filter(sale=True, status='1')
return super().formfield_for_foreignkey(db_field, request, **kwargs)
def has_edit_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
"""
订单检查
1.[交易门店]必须有值
"""
class OrderCheckForm(forms.ModelForm):
def clean(self):
super(OrderCheckForm, self).clean()
store = self.cleaned_data.get('store')
if store is None:
raise forms.ValidationError('请先设定[使用者档案],使用者必须先设定[可编辑门店]。')
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'created', 'store', 'status', 'employee', 'customer', 'amount']
fields = ['created', 'store', 'status', 'employee', 'customer',
'amount', 'notes', 'create_user', 'updated', 'update_user']
readonly_fields = ['status', 'created', 'create_user', 'updated', 'update_user']
form = OrderCheckForm
inlines = [OrderDetailInline]
view_on_site = False
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'store':
profiles = Profile.objects.filter(user=request.user)
if profiles.count() > 0:
kwargs["queryset"] = Store.objects.filter(id=profiles.first().store.id)
kwargs['initial'] = profiles.first().store.id
else:
kwargs["queryset"] = Store.objects.filter(id=-1)
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
else:
obj.update_user = request.user
obj.updated = datetime.now()
super().save_model(request, obj, form, change)
def save_formset(self, request, form, formset, change):
super().save_formset(request, form, formset, change)
order_details = formset.save(commit=False)
for order_detail in order_details:
order_detail.unit = order_detail.good.unit
order_detail.price = order_detail.good.price
#产品/套盒
if order_detail.good.type == '1' or order_detail.good.type == '4':
#新增出货单
ship = Ship()
ship.order_detail = order_detail
ship.store = order_detail.order.store
ship.good = order_detail.good
ship.quantity = order_detail.quantity
ship.create_user = request.user
ship.save()
distrib = Distrib.objects.filter(store=order_detail.order.store, good=order_detail.good,
quantity__gte=order_detail.quantity).first()
#新增[商品库存异动]
tran = Tran()
tran.good = order_detail.good
tran.source_form = '1'
tran.source_id = ship.id
tran.store = order_detail.order.store
tran.storage = distrib.storage
tran.from_quantity = distrib.quantity
tran.tran_quantity = order_detail.quantity
tran.to_quantity = distrib.quantity - order_detail.quantity
tran.create_user = request.user
tran.save()
#修改[商品库存分布]
distrib.quantity -= order_detail.quantity
distrib.save()
if order_detail.good.type == '4':
main = order_detail.good
composes = Compose.objects.filter(main=main)
if composes.count() > 0:
for compose in composes:
content = compose.content
#产品类商品不管
# 论次服务
if content.type == '2':
cnt = 1
while cnt <= compose.quantity * order_detail.quantity:
# 新增论次服务记录
cervice = Cervice()
cervice.order_detail = order_detail
cervice.good = content
cervice.store = order_detail.order.store
cervice.customer = order_detail.order.customer
cervice.created = datetime.now()
cervice.create_user = order_detail.order.create_user
cervice.save()
cnt += 1
elif content.type == '3':
# 新增有效期间服务
pervice = Pervice()
pervice.order_detail = order_detail
pervice.good = content
pervice.customer = order_detail.order.customer
pervice.create_user = order_detail.order.create_user
pervice.save()
#论次服务
elif order_detail.good.type == '2':
cnt = 1
while cnt <= order_detail.quantity:
# 新增论次服务记录
cervice = Cervice()
cervice.order_detail = order_detail
cervice.good = order_detail.good
cervice.store = order_detail.order.store
cervice.customer = order_detail.order.customer
cervice.created = datetime.now()
cervice.create_user = order_detail.order.create_user
cervice.save()
cnt += 1
#有效期间服务
elif order_detail.good.type == '3':
#新增有效期间服务
pervice = Pervice()
pervice.order_detail = order_detail
pervice.good = order_detail.good
pervice.customer = order_detail.order.customer
pervice.create_user = order_detail.order.create_user
pervice.save()
order_detail.save()
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(OrderDetail)
class OrderDetailAdmin(admin.ModelAdmin):
list_display = ['id', 'order', 'good', 'quantity', 'unit']
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。