当前位置:   article > 正文

Django用admin开发的产康中心进销存系统-5_django开启admin二次开发进销存

django开启admin二次开发进销存

1.存貨(inventory)模块

1.1.models.py 设定

1.1.1.出货单

订单售出产品类商品或套盒类商品时,在有库存的情况下,系统会自动抛出货单

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

1.1.2.商品库存分布

记录哪些商品在哪些合作门店仓库内的储存数量。

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

1.1.3.商品库存异动

记录商品数量的异动过程。

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
  • 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

1.1.4.商品库存异动申请单

因本系统尚未开发进货单,如需要库存则先用本单据。

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1.2.admin.py 设定

1.2.1.ShipAdmin呈现画面

出货单资料是订单完成时自动产生,故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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

1.2.2.DistribAdmin呈现画面

哪些商品在哪些仓库有多少库存是由系统自动计算后得到的,故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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

1.2.3.TranAdmin呈现画面

记录商品仓库的异动过程,由系统自动记录,故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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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

1.2.4.ReshuffleAdmin呈现画面

可人工使用商品库存异动申请单新增商品商品库存分布,新增后则不可修改与删除。

"""
商品库存异动申请单检查
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
  • 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

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

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

闽ICP备14008679号