def impose_fine(self, request, pk): money = request.data.get('money') remark = request.data.get('remark') first_report = self.get_object() user = request.session.get('user') first_report_fine = CourseReportFine() first_report_fine.user_id = user.get('id') first_report_fine.report_id = first_report.id first_report_fine.type = CourseReportFine.FIRST_REPORT first_report_fine.money = money first_report_fine.remark = remark first_report_fine.save() now_time = timezone.now() rate = ExchangeRate.objects.filter(currency='CNY', valid_start__lte=now_time, valid_end__gt=now_time).first() balance_change = BalanceChange() balance_change.role = BalanceChange.TEACHER balance_change.user_id = first_report.tutor_user.id balance_change.reference = first_report.virtual_class.id balance_change.amount = 0 - (money/rate.rate) balance_change.reason = BalanceChange.NO_SHOW_PENALTY balance_change.adviser_user_id = user.get('id') balance_change.save() return JsonResponse(code=0, msg='success', status=status.HTTP_200_OK)
def first_course_record(self, sheet): for cases in list(sheet.rows)[2:]: adviser_name = str(cases[0].value) refer_name = str(cases[1].value).strip() parent_name = str(cases[2].value).strip() create_time = cases[3].value adviser = UserInfo.objects.filter(username=adviser_name).first() parent = UserParentInfo.objects.filter( Q(username=parent_name) | Q(email=parent_name) | Q(phone=parent_name)).first() refer = UserParentInfo.objects.filter( Q(username=refer_name) | Q(email=refer_name) | Q(phone=refer_name)).first() if not refer or not parent: print('未找到推荐人或被推荐人', adviser_name, refer_name, parent_name, create_time) continue account_balance = AccountBalanceNew() account_balance.parent_user_id = refer.id account_balance.type = AccountBalanceNew.BONUS_AMOUNT account_balance.account_class = AccountBalanceNew.NORMAL_ACCOUNT account_balance.status = 0 account_balance.balance = 0 account_balance.rate = 0 account_balance.save() balance_change = BalanceChange() balance_change.role = BalanceChange.PARENT balance_change.user_id = parent.id balance_change.parent_user_id = refer.id balance_change.reference = 0 balance_change.amount = 1 balance_change.normal_amount = 0 balance_change.reason = 19 balance_change.balance_id = account_balance.id if adviser: balance_change.adviser_user_id = adviser.id balance_change.save()
def give_out_bonus(self, request, pk): group_info = ActivityGroupInfo.objects.get(id=pk) if group_info.grant_award == ActivityGroupInfo.GRANT: return JsonResponse(code=1, msg='已发放奖励', status=status.HTTP_200_OK) group_recharges = ActivityGroupRecharge.objects.filter(group_id=group_info.id).all() if len(group_recharges) == 0: return JsonResponse(code=1, msg='没有成员,不能发放奖励', status=status.HTTP_200_OK) for group_recharge in group_recharges: order_no = group_recharge.order_no recharge_balance_change = BalanceChange.objects.filter(reference=order_no, parent_user_id=group_recharge.parent_user_id, reason=3).first() if not recharge_balance_change: logger.debug('not found recharge record where order_no={}, parent_user_id={}'.format(order_no, group_recharge.parent_user_id)) parent_user = group_recharge.parent_user balance_change = BalanceChange() balance_change.reason = BalanceChange.BONUS balance_change.adviser_user_id = parent_user.adviser_user_id balance_change.xg_user_id = parent_user.xg_user_id balance_change.amount = group_recharge.bonus_amount balance_change.normal_amount = 0 balance_change.reference = group_recharge.order_no balance_change.parent_user_id = group_recharge.parent_user_id balance_change.user_id = parent_user.id balance_change.role = BalanceChange.PARENT balance_change.balance_id = recharge_balance_change.balance_id balance_change.save() # recharge_order = RechargeOrder.objects.filter(order_no=group_recharge.order_no).first() # recharge_order.incentive_amount = group_recharge.bonus_amount # recharge_order.save() group_info.grant_award = ActivityGroupInfo.GRANT group_info.save() cms_user = request.session.get('user') logger.debug('give out bonus, user_id:{}, group_info_id:{}'.format(cms_user.get('id'), group_info.id)) return JsonResponse(code=0, msg='success', status=status.HTTP_200_OK)
def handle(self, *args, **kwargs): file_name = kwargs.get('file_name') sheet_name = kwargs.get('sheet_name', None) path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) path = os.path.join(path, 'files') file_path = os.path.join(path, file_name) print(file_path) now_time = timezone.now() if not sheet_name: print('not found sheet: {}'.format(sheet_name)) return # try: wb = openpyxl.load_workbook(file_path) sh = wb[sheet_name] for cases in list(sh.rows)[2:]: try: print(cases[0].value) topup_date = cases[1].value parent_user_name = str(cases[2].value).strip() currency = str(cases[3].value).strip().upper() price = cases[4].value amount = cases[5].value topup_amount = cases[6].value code = cases[7].value payment_method = cases[8].value reference = cases[9].value remark = cases[10].value if remark: distinct = remark * 100 else: distinct = None exchange_rate = ExchangeRate.objects.filter(currency=currency, valid_end__gt=now_time, valid_start__lte=now_time).first() if not exchange_rate: print('not found currency: {}'.format(currency)) continue parent_user = UserParentInfo.objects.filter(Q(username=parent_user_name)|Q(email=parent_user_name)|Q(phone=parent_user_name)).first() if not parent_user: print('not found user: {}'.format(parent_user_name)) continue with transaction.atomic(): recharge_order = RechargeOrder() recharge_order.create_time = topup_date recharge_order.update_time = topup_date recharge_order.parent_user_id = parent_user.id recharge_order.recharge_type = RechargeOrder.PACKAGE recharge_order.code = code recharge_order.recharge_amount = amount recharge_order.incentive_amount = 0 recharge_order.total_amount = amount recharge_order.currency_id = exchange_rate.id recharge_order.currency = exchange_rate.currency recharge_order.rate = price recharge_order.origin_total_price = price * amount recharge_order.save_price = price * amount - topup_amount recharge_order.discount = distinct recharge_order.total_price = topup_amount recharge_order.reference = reference recharge_order.status = RechargeOrder.PAID before_recharge_order = RechargeOrder.objects.filter(parent_user_id=parent_user.id, status=1).first() if before_recharge_order: recharge_order.first_recharge = RechargeOrder.NOT_FIRST_RECHARGE else: recharge_order.first_recharge = RechargeOrder.FIRST_RECHARGE recharge_order.pay_type = RechargeOrder.CARD recharge_order.save() account_balance = AccountBalance() account_balance.parent_user_id = parent_user.id account_balance.type = AccountBalance.TOPUP_AMOUNT account_balance.account_class = AccountBalance.PRIVATE_ACCOUNT account_balance.rate = recharge_order.total_price/recharge_order.origin_total_price account_balance.balance = 0 account_balance.status = 0 account_balance.save() balance_change = BalanceChange() balance_change.role = BalanceChange.PARENT balance_change.reason = BalanceChange.TOP_UP balance_change.user_id = parent_user.id balance_change.amount = recharge_order.total_amount balance_change.normal_amount = 0 balance_change.reference = recharge_order.order_no balance_change.parent_user_id = parent_user.id if parent_user.adviser_user_id: balance_change.adviser_user_id = parent_user.adviser_user_id if parent_user.xg_user_id: balance_change.xg_user_id = parent_user.xg_user_id balance_change.balance_id = account_balance.id balance_change.save() payment = Payment() payment.order_id = recharge_order.id payment.amount = recharge_order.total_price payment.reference = payment_method payment.type = recharge_order.pay_type payment.status = 1 payment.channel = 'stripe' payment.success_time = recharge_order.create_time payment.save() except Exception as e: print(e) print('{} error'.format(cases[0].value)) # except Exception as e: # print(e) # finally: wb.close()
def handle(self, *args, **kwargs): file_name = kwargs.get('file_name') sheet_name = kwargs.get('sheet_name', None) sheet_name2 = kwargs.get('sheet_name2', None) path = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) path = os.path.join(path, 'files') file_path = os.path.join(path, file_name) print(file_path) # 读取excle wb = openpyxl.load_workbook(file_path) # 选择sheet if sheet_name: sh = wb[sheet_name] # ce = sh.cell(row=1, column=1) # 读取第一行, for cases in list(sh.rows)[1:]: adviser_name = str(cases[0].value).strip() parent_name = str(cases[1].value).strip() create_time = cases[2].value adviser = UserInfo.objects.filter( username=adviser_name).first() parent = UserParentInfo.objects.filter( Q(username=parent_name) | Q(email=parent_name) | Q(phone=parent_name)).first() if not parent: print('未找到该用户', adviser_name, parent_name, create_time) continue account_balance = AccountBalanceNew() account_balance.balance = 0 account_balance.type = AccountBalanceNew.BONUS_AMOUNT account_balance.account_class = AccountBalanceNew.NORMAL_ACCOUNT account_balance.rate = 0 account_balance.status = 0 account_balance.parent_user_id = parent.id account_balance.save() balance_change = BalanceChange() balance_change.role = BalanceChange.PARENT balance_change.user_id = parent.id balance_change.parent_user_id = parent.id balance_change.amount = 1 balance_change.normal_amount = 0 balance_change.reason = BalanceChange.COMPENSATION balance_change.reference = 0 balance_change.balance_id = account_balance.id if adviser: balance_change.adviser_user_id = adviser.id balance_change.save() # sheet2 = wb['Sheet2'] # if sheet_name2: sheet2 = wb[sheet_name2] self.first_course_record(sheet2) wb.close()