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 add_student_fbc(self, vc, students, reason): for student in students: parent_user = student.parent_user BalanceChange.save_balance_change( BalanceChange.CHILDREN, student.id, vc.id, reason, 0, parent_user_id=parent_user.id, adviser_user_id=parent_user.adviser_user_id, xg_user_id=parent_user.xg_user_id, balance_id=None)
def save_balance_change(self, vc, student_user, parent_user, student_amount, balance_reason, account_balance): BalanceChange.save_balance_change( BalanceChange.CHILDREN, student_user.id, vc.id, balance_reason, student_amount, parent_user_id=parent_user.id, adviser_user_id=parent_user.adviser_user_id, xg_user_id=parent_user.xg_user_id, balance_id=account_balance.id)
def add_student_balance_change(self, student: User, student_info: UserStudentInfo): '''添加学生充值记录''' a_b_cs = AccountBalanceChange.objects.filter(user=student, reason__in=(AccountBalanceChange.TOP_UP, AccountBalanceChange.BONUS, AccountBalanceChange.REFERRAL, AccountBalanceChange.REFERRAL_INCENTIVE, AccountBalanceChange.REDEEM)) for a_b_c in a_b_cs: balance_change = BalanceChange() balance_change.amount = a_b_c.amount balance_change.role = BalanceChange.PARENT balance_change.user_id = student_info.parent_user.id balance_change.reason = a_b_c.reason balance_change.reference = a_b_c.reference course_adviser_student = CourseAdviserStudent.objects.filter(start_time__lte=a_b_c.created_on, student_id=a_b_c.user_id).order_by('-start_time').first() learn_manager_student = LearnManagerStudent.objects.filter(start_time__lte=a_b_c.created_on, student_id=a_b_c.user_id).order_by('-start_time').first() if learn_manager_student: balance_change.xg_user_id = learn_manager_student.cms_user.id if course_adviser_student: balance_change.adviser_user_id = course_adviser_student.cms_user.id balance_change.create_time = a_b_c.created_on balance_change.update_time = a_b_c.updated_on balance_change.save() # 添加充值订单 self.add_sale_order(student, student_info)
def compensation_student(self, vc, students, student_amount, account_class): for student in students: parent_user = student.parent_user account_balance = self.create_account( parent_user, AccountBalance.ACTIVITY_AMOUNT, account_class) BalanceChange.save_balance_change( BalanceChange.CHILDREN, student.id, vc.id, BalanceChange.NO_SHOW_COMPENSATION, student_amount, parent_user_id=parent_user.id, adviser_user_id=parent_user.adviser_user_id, xg_user_id=parent_user.xg_user_id, balance_id=account_balance.id) return True
def add_tutor_balancechange(self, vc: VirtualClass, vc_info: VirtualclassInfo): ''' 添加老师上课工资 :param vc: :param vc_info: :return: ''' hosts = vc.appointment.hosts.first() account_balancechanges = AccountBalanceChange.objects.filter( reference=vc.id, user_id=hosts.id).all() for balance in account_balancechanges: balance_change = BalanceChange() # balance_change.id = balance.id balance_change.user_id = vc_info.tutor_user.id balance_change.role = BalanceChange.TEACHER balance_change.reason = balance.reason balance_change.amount = balance.amount balance_change.reference = vc_info.id balance_change.create_time = vc.created_on balance_change.update_time = vc.updated_on balance_change.save()
def handle(self, *args, **options): transfer_infos = Transfer.objects.all() NewTransfer.objects.all().delete() for transfer_info in transfer_infos: transfer_parent = UserParentInfo.objects.filter(username=transfer_info.transfer_user.username).first() if not transfer_parent: continue recipient_parent = UserParentInfo.objects.filter(username=transfer_info.recipient.username).first() if not recipient_parent: continue transfer = NewTransfer() transfer.id = transfer_info.id transfer.transfer_user_id = transfer_parent.id transfer.recipient_user_id = recipient_parent.id transfer.amount = transfer_info.amount transfer.save() abc_list = AccountBalanceChange.objects.filter(reason__in=(AccountBalanceChange.TRANSFER_IN, AccountBalanceChange.TRANSFER_OUT)).all() BalanceChange.objects.filter(reason__in=(BalanceChange.TRANSFER_IN, BalanceChange.TRANSFER_OUT)).delete() for abc in abc_list: parent = UserParentInfo.objects.filter(username=abc.user.username).first() if not parent: print('user not exist', abc.id, abc.user_id, abc.user.username) continue bc = BalanceChange() bc.user_id = parent.id bc.reason = abc.reason bc.reference = abc.reference bc.amount = abc.amount bc.role = BalanceChange.PARENT bc.create_time = abc.created_on bc.update_time = abc.updated_on bc.save()
def handle(self, *args, **options): compensations = AccountBalanceChange.objects.filter( reason=AccountBalanceChange.COMPENSATION).all() for compensation in compensations: user = compensation.user parent = UserParentInfo.objects.filter( username=user.username).first() if parent: print() print("insert ", compensation.id) balance_change = BalanceChange() balance_change.user_id = parent.id balance_change.role = UserParentInfo.PARENT balance_change.reason = BalanceChange.COMPENSATION balance_change.parent_user_id = parent.id balance_change.create_time = compensation.created_on balance_change.update_time = compensation.updated_on balance_change.amount = compensation.amount balance_change.save() else: print('not insert ', compensation.id)
def add_virtualclass_homework_and_student_balance( self, vc: VirtualClass, vc_info: VirtualclassInfo, student_dict, static_url): ''' 添加学生家庭作业跟扣除课时记录 :param vc: :param vc_info: :param student_dict: :param static_url: :return: ''' invitees = vc.appointment.invitees.all() invitee_dict = {} for invitee in invitees: invitee_dict[invitee.username] = invitee session = vc.course_session if not session: return for invitee in invitees: homeworks = Homework.objects.filter(session=session).all() homework_results = HomeworkResult.objects.filter( user=invitee, homework__in=homeworks).all() for homework_result in homework_results: virtualclass_homework_result = VirtualclassHomeworkResult() virtualclass_homework_result.virtual_class = vc_info virtualclass_homework_result.homework_id = homework_result.homework_id virtualclass_homework_result.student_user = student_dict[ invitee.username] virtualclass_homework_result.tutor_user = vc_info.tutor_user virtualclass_homework_result.score = homework_result.score virtualclass_homework_result.comment_zh = homework_result.comment virtualclass_homework_result.save() # 添加家庭作业附件 done_homeworks = Donehomework.objects.filter( homeresult=homework_result).all() if len(done_homeworks) == 0: virtualclass_homework_result.delete() for done_homework in done_homeworks: vc_homework_attachment = VirtualclassHomeworkAttachment() vc_homework_attachment.virtual_class = vc_info vc_homework_attachment.homework_result = virtualclass_homework_result vc_homework_attachment.attachment = static_url + done_homework.result_content vc_homework_attachment.save() # 添加本节课课时 account_balancechanges = AccountBalanceChange.objects.filter( reference=vc.id, user_id=invitee.id).all() for balance in account_balancechanges: balance_change = BalanceChange() # balance_change.id = balance.id balance_change.user_id = student_dict[invitee.username].id balance_change.reason = balance.reason balance_change.role = BalanceChange.CHILDREN balance_change.amount = balance.amount balance_change.reference = vc_info.id balance_change.parent_user_id = student_dict[ invitee.username].parent_user_id adviser = CourseAdviserStudent.objects.filter( start_time__lte=balance.created_on, student_id=balance.user_id).order_by( '-start_time').first() if adviser: balance_change.adviser_user_id = adviser.cms_user.id learn_manager = LearnManagerStudent.objects.filter( start_time__lte=balance.created_on, student_id=balance.user_id).order_by( '-start_time').first() if learn_manager: balance_change.xg_user_id = learn_manager.cms_user.id balance_change.create_time = balance.created_on balance_change.update_time = balance.updated_on balance_change.save()
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:]: if not cases[0].value: break transfer_username = str(cases[0].value).strip() amount = float(cases[1].value) recipte_username = str(cases[2].value).strip() xg_username = str(cases[4].value).strip() create_time = timezone.now() xg_user = UserInfo.objects.filter(username=xg_username).first() transfer_parent = UserParentInfo.objects.filter( Q(username=transfer_username) | Q(email=transfer_username) | Q(phone=transfer_username)).first() if not transfer_parent: print('未找到转出课时用户', transfer_username) continue recipte_parent = UserParentInfo.objects.filter( Q(username=recipte_username) | Q(email=recipte_username) | Q(phone=recipte_username)).first() if not recipte_parent: print('未找到转入课时用户', recipte_username) continue transfer_parent_balance = AccountBalance.objects.filter( parent_user_id=transfer_parent.id, state=AccountBalance.NOT_DELETE, status=0, account_class=AccountBalance.NORMAL_ACCOUNT).values( 'parent_user_id').annotate( balance_sum=Sum('balance')).values( 'parent_user_id', 'balance_sum').first() # if transfer_parent_balance.get('balance_sum') < amount: # print('转出账户课时余额不够', transfer_username) # continue transfer_data = Transfer() transfer_data.amount = amount transfer_data.transfer_user_id = transfer_parent.id transfer_data.recipient_user_id = recipte_parent.id print( """insert into finance_transfer(id, amount, transfer_user_id, recipient_user_id, create_time, update_time) values({},{},{},{},'{}','{}')""" .format(2102, amount, transfer_parent.id, recipte_parent.id, '2020-07-17 08:10:10', '2020-07-17 08:10:10')) account_balance = AccountBalance() account_balance.balance = 0 - amount account_balance.type = AccountBalance.BONUS_AMOUNT account_balance.account_class = AccountBalance.NORMAL_ACCOUNT account_balance.rate = 0 account_balance.status = 0 account_balance.parent_user_id = transfer_parent.id print( """insert into accounts_balance(balance, type, account_class, rate, status, parent_user_id, state, create_time, update_time) values({},{},{},{},{},{},{},'{}','{}')""" .format(account_balance.balance, account_balance.type, account_balance.account_class, account_balance.rate, account_balance.status, account_balance.parent_user_id, 0, '2020-07-17 08:10:10', '2020-07-17 08:10:10')) recipte_account_balance = AccountBalance() recipte_account_balance.balance = amount recipte_account_balance.type = AccountBalance.BONUS_AMOUNT recipte_account_balance.account_class = AccountBalance.NORMAL_ACCOUNT recipte_account_balance.rate = 0 recipte_account_balance.status = 0 recipte_account_balance.parent_user_id = recipte_parent.id print( """insert into accounts_balance(balance, type, account_class, rate, status, parent_user_id, state, create_time, update_time) values({},{},{},{},{},{},{},'{}','{}')""" .format(recipte_account_balance.balance, recipte_account_balance.type, recipte_account_balance.account_class, recipte_account_balance.rate, recipte_account_balance.status, recipte_account_balance.parent_user_id, 0, '2020-07-17 08:10:10', '2020-07-17 08:10:10')) balance_change = BalanceChange() balance_change.role = BalanceChange.PARENT balance_change.user_id = transfer_parent.id balance_change.parent_user_id = transfer_parent.id balance_change.amount = 0 - amount balance_change.normal_amount = 0 balance_change.reason = BalanceChange.TRANSFER_OUT balance_change.reference = 2102 balance_change.balance_id = account_balance.id recipte_balance_change = BalanceChange() balance_change.role = BalanceChange.PARENT recipte_balance_change.user_id = recipte_parent.id recipte_balance_change.parent_user_id = recipte_parent.id recipte_balance_change.amount = amount recipte_balance_change.normal_amount = 0 recipte_balance_change.reason = BalanceChange.TRANSFER_OUT recipte_balance_change.reference = 2102 recipte_balance_change.balance_id = account_balance.id if xg_user: balance_change.xg_user_id = xg_user.id recipte_balance_change.xg_user_id = xg_user.id print( """insert into finance_balance_change(role, user_id, parent_user_id, amount, normal_amount, reason, reference, balance_id, xg_user_id, 'create_time', 'update_time') values({},{},{},{},{},{},{},{},'{}','{}')""" .format(balance_change.role, balance_change.user_id, balance_change.parent_user_id, balance_change.amount, balance_change.normal_amount, balance_change.reason, balance_change.reference, balance_change.balance_id, balance_change.xg_user_id, '2020-07-17 08:10:10', '2020-07-17 08:10:10')) print( """insert into finance_balance_change(role, user_id, parent_user_id, amount, normal_amount, reason, reference, balance_id, xg_user_id, 'create_time', 'update_time') values({},{},{},{},{},{},{},{},'{}','{}')""" .format(recipte_balance_change.role, recipte_balance_change.user_id, recipte_balance_change.parent_user_id, recipte_balance_change.amount, recipte_balance_change.normal_amount, recipte_balance_change.reason, recipte_balance_change.reference, recipte_balance_change.balance_id, recipte_balance_change.xg_user_id, '2020-07-17 08:10:10', '2020-07-17 08:10:10')) # sheet2 = wb['Sheet2'] # if sheet_name2: sheet2 = wb[sheet_name2] self.first_course_record(sheet2) 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()
def check_exception(self, request, pk): try: result = int(request.data.get('result')) student_amount = Decimal(request.data.get('student_amount', 0)) teacher_amount = Decimal(request.data.get('teacher_amount', 0)) description = request.data.get('description', None) if student_amount > 200 or student_amount < 0: raise ArithmeticError('参数超出范围') if teacher_amount > 200 or teacher_amount < 0: raise ArithmeticError('参数超出范围') except Exception as e: logger.error(e) return JsonResponse(code=1, msg='参数错误', status=status.HTTP_200_OK) try: vc = VirtualclassInfo.objects.get(id=pk) if vc.reason == VirtualclassInfo.NORMAL and vc.status == VirtualclassInfo.FINISH_NOMAL: return JsonResponse(code=1, msg='该课堂正常结束,不能审核', status=status.HTTP_200_OK) except ObjectDoesNotExist as e: return JsonResponse(code=1, msg='该课堂不存在', status=status.HTTP_200_OK) user = request.session.get('user') logger.debug( 'check_exception, operator:{}, result:{}, student_amount:{}, teacher_amount:{}, description:{}, virtual_class_id:{}' .format(user.get('id'), result, student_amount, teacher_amount, description, pk)) now_time = timezone.now() members = vc.virtual_class_member.all() students = [member.student_user for member in members] teacher = vc.tutor_user vc_exception = VirtualclassException.objects.filter( virtual_class_id=vc.id).first() if vc_exception: return JsonResponse(code=1, msg='该条数据已审核,请勿重复审核', status=status.HTTP_200_OK) account_balance_change = BalanceChange.objects.filter( reason__in=( BalanceChange.ABSENCE_PENALTY, # 学生缺席罚金 BalanceChange.NO_SHOW_COMPENSATION, # 导师不出席对学生的补偿 BalanceChange.ABSENCE_COMPENSATION, # 学生缺席老师奖励 BalanceChange.NO_SHOW_PENALTY, # 老师缺席罚金 ), reference=vc.id).all() if len(account_balance_change) > 1: return JsonResponse(code=1, msg='该条数据已审核,请勿重复审核', status=status.HTTP_200_OK) vc_exception = VirtualclassException() vc_exception.result = result vc_exception.description = description vc_exception.cms_user_id = request.session.get('user').get('id') vc_exception.virtual_class_id = vc.id exchange = ExchangeRate.objects.filter( currency='CNY', valid_start__lt=now_time, valid_end__gte=now_time).first() account_class = AccountBalance.NORMAL_ACCOUNT class_type_price = ClasstypePrice.objects.filter( class_type_id=vc.class_type_id, course_edition_id=vc.lesson.course.course_edition.id).first() if class_type_price: account_class = class_type_price.account_class if result == VirtualclassException.STUDENT_ABSENCE: # 学生缺席 '''学生缺席,对老师进行补偿,对学生进行扣款''' vc.reason = VirtualclassInfo.STUDENT_ABSENCE vc.status = VirtualclassInfo.FINISH_ABNOMAL vc.save() if student_amount: student_amount = 0 - (student_amount / exchange.rate) self.absence_penalty(vc, students, student_amount, account_class) else: self.add_student_fbc(vc, students, BalanceChange.ABSENCE_PENALTY) teacher_amount = teacher_amount / exchange.rate BalanceChange.save_balance_change( BalanceChange.TEACHER, teacher.id, vc.id, BalanceChange.ABSENCE_COMPENSATION, teacher_amount) elif result == VirtualclassException.TEACHER_ABSENCE: # 老师缺席 '''老师缺席,对学生进行补偿,对老师进行扣款''' vc.reason = VirtualclassInfo.TUTOR_ABSENCE vc.status = VirtualclassInfo.FINISH_ABNOMAL vc.save() if student_amount: student_amount = student_amount / exchange.rate self.compensation_student(vc, students, student_amount, account_class) else: self.add_student_fbc(vc, students, BalanceChange.NO_SHOW_COMPENSATION) teacher_amount = 0 - (teacher_amount / exchange.rate) BalanceChange.save_balance_change(BalanceChange.TEACHER, teacher.id, vc.id, BalanceChange.NO_SHOW_PENALTY, teacher_amount) elif result == VirtualclassException.TEACHER_AND_STUDENT_ABSENCE: # 老师学生都缺席 '''老师学生都缺席,对学生老师进行扣款''' vc.status = VirtualclassInfo.FINISH_ABNOMAL vc.reason = VirtualclassInfo.CLASS_NOONE vc.save() teacher_amount = 0 - (teacher_amount / exchange.rate) BalanceChange.save_balance_change(BalanceChange.TEACHER, teacher.id, vc.id, BalanceChange.NO_SHOW_PENALTY, teacher_amount) if student_amount: student_amount = 0 - (student_amount / exchange.rate) self.absence_penalty(vc, students, student_amount, account_class) else: self.add_student_fbc(vc, students, BalanceChange.ABSENCE_PENALTY) vc_exception.save() return JsonResponse(code=0, msg='success', status=status.HTTP_200_OK)
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) # 读取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)[0:]: parent_name = str(cases[0].value).strip() parent = UserParentInfo.objects.filter( Q(username=parent_name) | Q(email=parent_name) | Q(phone=parent_name)).only('id').first() if not parent: print('未找到该用户--', parent_name) continue recharge_count = RechargeOrder.objects.filter( parent_user_id=parent.id, status=RechargeOrder.PAID).count() if not recharge_count: print('未充值用户--', parent_name) continue print('已充值用户--', parent_name) 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(force_insert=True) 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 balance_change.save(force_insert=True) wb.close()
def handle(self, *args, **options): for parent_name in all_parent_names: parent_user = UserParentInfo.objects.filter( Q(username=parent_name) | Q(email=parent_name) | Q(phone=parent_name)).first() if not parent_user: print('not found {}'.format(parent_name)) continue balance_change = BalanceChange() balance_change.role = BalanceChange.PARENT balance_change.user_id = parent_user.id balance_change.reason = BalanceChange.COMPENSATION balance_change.amount = 1 balance_change.reference = 0 balance_change.normal_amount = 0 balance_change.parent_user_id = parent_user.id balance_change.save()
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 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()