def calc_asset(item): # 计算分销和返利的 """ 分销 """ goods_info = item.goods_backup if BoolConfig.get_bool('bonus_switch'): direct_relation = None if getattr(item.order.user, 'referrer_relations', None): direct_relation = item.order.user.referrer_relations.user # 上线 if direct_relation and direct_relation.has_bonus_right and goods_info.g_bonus > 0: WxUserAccountLog.record(direct_relation, WxUserAccountLog.BONUS, asset=goods_info.g_bonus * goods_info.num, referral=item.order.user, number=item.order_sn, remark='成员消费返利', note=item.order.user.nickname, cost=goods_info.price * goods_info.num) """ 分享返积分返利 """ if goods_info.g_rebate <= 0 or not BoolConfig.get_bool('rebate_switch'): return False if not goods_info.share_user_id: return False share_user = WxUser.objects.filter( id=int(goods_info.share_user_id)).first() if not share_user or not share_user.has_rebate_right: return False WxUserCreditLog.record(share_user, WxUserCreditLog.SHATE, credit=goods_info.g_rebate * goods_info.num, number=item.order_sn, remark='商品分享返利', note=goods_info.goods_name)
def get_queryset(self): if getattr(self.request.user, 'is_staff', False): filter_list = [] if BoolConfig.get_bool('rebate_switch'): filter_list.append(WxUserAccountLog.ASSET) if BoolConfig.get_bool('bonus_switch'): filter_list.append(WxUserAccountLog.BONUS) return WxUserAccountLog.objects.filter(a_type__in=filter_list) return WxUserAccountLog.objects.none()
def compute_amount(order, order_amount, postage_total, use_wallet=False): # 计算订单的金额 order.order_amount = order_amount order.postage_total = postage_total if use_wallet and BoolConfig.get_bool('wallet_switch'): total = order.order_amount + order.postage_total account = getattr(order.user, 'account', None) asset = getattr(account, 'asset', Decimal('0')) recharge = getattr(account, 'recharge', Decimal('0')) if total < asset: order.asset_pay = total order.real_amount = 0 elif total > asset and total < asset + recharge: order.asset_pay = asset order.recharge_pay = total - asset order.real_amount = 0 elif total >= asset + recharge: order.asset_pay = asset order.recharge_pay = recharge order.real_amount = total - asset - recharge remark = '商品购买' number = order.order_sn WxUserAccountLog.record(order.user, WxUserAccountLog.USE, asset=order.asset_pay, balance=order.recharge_pay, remark=remark, note=f'订单:{order.order_sn}', number=number) else: order.real_amount = order.order_amount + order.postage_total order.save() return order
def level_save(sender, **kwargs): for account in WxUserAccount.objects.exclude(total_recharge=Decimal(0)): if BoolConfig.get_bool('wallet_switch'): level = Level.objects.filter(threshold__lte=account.total_recharge ).order_by('-threshold').first() if level: UserLevel.objects.update_or_create(wxuser=account.user, defaults={'level': level}) else: UserLevel.objects.filter(wxuser=account.user).delete()
def update_recharge_level(self, recharge): """更新累计充值金额和level会员等级""" self.total_recharge += recharge self.save() if BoolConfig.get_bool('wallet_switch'): level = Level.objects.filter(threshold__lte=self.total_recharge ).order_by('-threshold').first() if level: UserLevel.objects.update_or_create(wxuser=self.user, defaults={'level': level})
def validate(self, attrs): if BoolConfig.get_value('open_buy') == 'false': raise serializers.ValidationError('正在备货,尚未开放购买,请谅解') request = self.context.get('request') address = attrs.get('address') shop = attrs.get('shop', None) if shop and shop.status != shop.OPEN: raise serializers.ValidationError('该店已经暂时未营业,请选择其他店') if address and request.user != address.user: raise serializers.ValidationError('收货地址设置错误') goodsitems = attrs.get('goodsitems') if attrs.get('fictitious', False) and len(goodsitems) != 1: raise serializers.ValidationError('虚拟商品只能单件购买,无法与其他商品一同下单') for item in goodsitems: goods = item.get('goods') gtype = item.get('gtype') num = item.get('num') if attrs.get('fictitious', False) and not goods.fictitious: raise serializers.ValidationError(f'{goods.name}不是虚拟商品') if not goods.fictitious and ( not item.get('delivery_method') or item.get('delivery_method') not in goods.delivery_method): raise serializers.ValidationError('配送方式选择有误') if shop and goods.shop != shop: raise serializers.ValidationError(f'本店没有{goods.name}这个商品') if goods.model_type != goods.ORD: raise serializers.ValidationError(f'商品{goods.name}是订阅商品!') if gtype not in goods.ord_goods.gtypes.all(): raise serializers.ValidationError(f'商品{goods.name}没有{gtype.content}这个规格') if goods.status in [Goods.NOT_SELL, Goods.NOT_ENOUGH]: raise serializers.ValidationError(f'商品{goods.name}已下架') if not gtype.is_sell: raise serializers.ValidationError(f'{gtype.content}暂不可选,请选择其他类型') if gtype.stock < num: raise serializers.ValidationError(f'{gtype.content}库存不足,请重新选择商品数量') if gtype.buy_limit is not None: # 判断限购 orders = Orders.objects.exclude(status=Orders.CLOSE). \ filter(user=request.user, model_type=Orders.ORD, goods_backup__gtype_id=gtype.id, add_time__gte=gtype.change_limit) has_buy_num = 0 for order in orders: has_buy = order.goods_backup.filter(gtype_id=gtype.id).aggregate(total=Sum('num')) has_buy_num += has_buy.get('total') if has_buy.get('total') else 0 if num + has_buy_num > gtype.buy_limit: can_buy_num = gtype.buy_limit - has_buy_num if (gtype.buy_limit - has_buy_num) >= 0 else 0 raise serializers.ValidationError( f'{gtype.content}限购{gtype.buy_limit}件, 已购{has_buy_num}件,还可购{can_buy_num}件') if attrs['is_pt']: self.validate_pt(goodsitems, attrs['groupbuy']) return attrs
def save(self, **kwargs): request = self.context.get('request') user = request.user address = self.validated_data.pop('address') remark = self.validated_data.get('remark') invoice = Invoice.objects.create(**self.validated_data.pop('invoice')) delivery_address = None if address: delivery_address = DeliveryAddress.create(address) shop = self.validated_data.get('shop') goodsitems = self.validated_data.pop('goodsitems') use_wallet = self.validated_data.pop('use_wallet') is_pt = self.validated_data.get('is_pt') groupbuy = self.validated_data.get('groupbuy') fictitious = self.validated_data.get('fictitious', False) level = getattr(user, 'level', None) discount = 1 if level and BoolConfig.get_bool('wallet_switch') and not is_pt: discount = level.discount order = Orders.create(user, shop, remark, Orders.ORD, discount, invoice, delivery_address, is_pt, fictitious) order_amount_total = 0 postage_total = 0 for item in goodsitems: goods = item.pop('goods') gtype = item.pop('gtype') goodsbackup = GoodsBackup.create(order, goods, gtype, discount=discount, is_pt=is_pt, **item) Items.create(order, goodsbackup) order_amount = goodsbackup.price * goodsbackup.num postage_total += compute_postage(goods, getattr(address, 'location', None), item.get('delivery_method')) order_amount_total += order_amount order = compute_amount(order, order_amount_total, postage_total, use_wallet) if groupbuy: groupbuy.order.add(order) return OrdersSerializers(order, context=self.context).data
def get(self, request, *args, **kwargs): switch = BoolConfig.get_bool('video_switch') return Response({'video_switch': switch})
def create(self, request, *args, **kwargs): if getattr(self.request.user, 'is_wechat', False) \ and (request.user.upload_perm or BoolConfig.get_bool('video_switch')) \ and not BlockWxUser.objects.filter(user=request.user): return super().create(request, *args, **kwargs) return Response('没有权限上传短视频', status=status.HTTP_403_FORBIDDEN)
def save(self, **kwargs): items_list = self.validated_data.get('items_list', {}) address = self.validated_data.get('address') is_pt = self.validated_data.get('is_pt') user = self.context.get('request').user discount = 1 if getattr( user, 'level', None) and BoolConfig.get_bool('wallet_switch') and not is_pt: discount = getattr(user, 'level', None).discount res = [] if address: address = lbs.get_longitude_and_latitude(address) for item in items_list: data = {} goods = Goods.objects.filter( pk=item.get('goodsid'), status__in=[Goods.IS_SELL, Goods.PREVIEW]).first() gtype = GoodType.objects.filter(pk=item.get('gtypeid'), is_sell=True).first() if not goods or not gtype or gtype not in goods.ord_goods.gtypes.all( ): continue data = { 'goodid': goods.id, 'gtypeid': gtype.id, 'goods_name': goods.name, 'gtype_name': gtype.content, 'shop': getattr(goods.shop, 'id', None), 'shop_name': getattr(goods.shop, 'name', ''), 'shop_address': getattr(goods.shop, 'address', ''), 'estimate_time': goods.ord_goods.estimate_time, 'delivery_method': goods.delivery_method, 'market_price': gtype.market_price, 'original_price': gtype.price, 'price': Decimal(gtype.price * discount).quantize(Decimal('0.00')) if not is_pt else gtype.ladder_[0].get('price'), 'stock': gtype.stock, 'discount': discount, 'buy_limit': gtype.buy_limit, 'postage': compute_postage(goods, address), 'image': FileSerializer(instance=goods.banner.first().image, context=self.context).data.get('file') } if getattr(goods.shop, 'entrust', None): # 店铺被委托时返回被委托的店铺信息 data.update({ 'entrust_shop_name': getattr(goods.shop.entrust, 'name', ''), 'entrust_shop_address': getattr(goods.shop.entrust, 'address', ''), }) res.append(data) return res