当前位置:   article > 正文

Django用admin开发的产康中心进销存系统-6_order = models.foreignkey('orderinfo', verbose_nam

order = models.foreignkey('orderinfo', verbose_name='订单', on_delete=models

1.销售(sale)模块

1.1.models.py 设定

1.1.1.订单

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

1.1.2.订单单身

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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1.2.admin.py 设定

1.2.1.OrderAdmin呈现画面

订单完成后就不可以再修改与删除。

"""
订单单身检查
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207

在这里插入图片描述
在这里插入图片描述

1.2.1.OrderDetailAdmin呈现画面

订单明细清单,目前意义不大,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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/170064
推荐阅读
相关标签
  

闽ICP备14008679号