def calTotalInterest(cls,returnType, bidRequestAmount, yearRate, monthes2Return): # cls.cal_set() bidRequestAmount = Decimal(str(bidRequestAmount)) yearRate = Decimal(str(yearRate)) totalInterest = Decimal('0.0000') monthlyRate = cls.getMonthlyRate(yearRate) if returnType == BidConst.GET_RETURN_TYPE_MONTH_INTEREST_PRINCIPAL(): #按月分期 #只借款一个月 if monthes2Return == 1: totalInterest = bidRequestAmount * monthlyRate else: temp1 = bidRequestAmount * monthlyRate temp2 = pow((Decimal('1') + monthlyRate), monthes2Return) temp3 = pow((Decimal('1') + monthlyRate),monthes2Return) - Decimal('1') #计算每月还款 monthToReturnMoney = (temp1 * temp2) / temp3 #计算总还款 totalReturnMoney = monthToReturnMoney * Decimal(str(monthes2Return)) #算出利息 totalInterest = totalReturnMoney - bidRequestAmount elif returnType == BidConst.GET_RETURN_TYPE_MONTH_INTEREST(): #按月到期 monthlyInterest = DecimalFormatUtil.amountformat((bidRequestAmount * monthlyRate)) totalInterest = monthlyInterest * monthes2Return return DecimalFormatUtil.formatBigDecimal(totalInterest, BidConst.STORE_SCALE())
def createPaymentScheduleDetail(self,payment_schedule, br): bids = br.bids.values_list('bidUser') # 取出所有投标用户id, 并去重 bid_user_ids = list(set((list(zip(*bids)))[0])) # // 汇总利息和本金, 用于最后一个投标的用户的利息和本金计算 total_amount = Decimal('0') for idx, bid_user_id in enumerate(bid_user_ids): # 获取所有投资人 bids_temp_query = br.bids.filter(bidUser_id=bid_user_id) #获取单个投资人 bid = bids_temp_query.first() # 获取相同投资人投的标数量 # 统计投资金额 available_amount_total = bids_temp_query.aggregate(Sum('availableAmount')) payment_schedule_detail = PaymentScheduleDetail.objects.create(bidId=bid) payment_schedule_detail.bidAmount = available_amount_total['availableAmount__sum'] payment_schedule_detail.bidRequestId = br payment_schedule_detail.deadline = payment_schedule.deadLine #还款人 payment_schedule_detail.borrower = br.createUser if idx == len(bid_user_ids) - 1: payment_schedule_detail.totalAmount = payment_schedule.totalAmount - total_amount # // 计算利息 interest = DecimalFormatUtil.formatBigDecimal( data=(available_amount_total['availableAmount__sum'] / br.bidRequestAmount) * payment_schedule.interest, scal=BidConst.STORE_SCALE() ) payment_schedule_detail.interest = interest payment_schedule_detail.principal = payment_schedule_detail.totalAmount - payment_schedule_detail.interest else: # // 计算利息 interest = DecimalFormatUtil.formatBigDecimal( data=(available_amount_total['availableAmount__sum'] / br.bidRequestAmount) * payment_schedule.interest, scal=BidConst.STORE_SCALE() ) # // 计算本金 principal = DecimalFormatUtil.formatBigDecimal( data=(available_amount_total['availableAmount__sum'] / br.bidRequestAmount) * payment_schedule.principal, scal=BidConst.STORE_SCALE() ) payment_schedule_detail.interest=interest payment_schedule_detail.principal = principal payment_schedule_detail.totalAmount = interest + principal total_amount = total_amount + payment_schedule_detail.totalAmount payment_schedule_detail.monthIndex = payment_schedule.monthIndex payment_schedule_detail.paymentScheduleId = payment_schedule #//投资人 payment_schedule_detail.investor = bid.bidUser payment_schedule_detail.save()
def calBidInterest(cls, bidRequestAmount, monthes2Return,yearRate, returnType, acturalBidAmount): cls.cal_set() acturalBidAmount =Decimal(str(acturalBidAmount)) #// 借款产生的总利息 totalInterest = cls.calTotalInterest(returnType, bidRequestAmount, yearRate, monthes2Return) #// 所占比例 proportion = acturalBidAmount / bidRequestAmount bidInterest = totalInterest * proportion return DecimalFormatUtil.formatBigDecimal(bidInterest, BidConst.STORE_SCALE())
def calMonthlyInterest(cls, returnType, bidRequestAmount, yearRate, monthIndex, monthes2Return): # cls.cal_set() bidRequestAmount = Decimal(str(bidRequestAmount)) yearRate = Decimal(str(yearRate)) monthlyInterest = Decimal('0.0000') monthlyRate = cls.getMonthlyRate(yearRate) if returnType == BidConst.GET_RETURN_TYPE_MONTH_INTEREST_PRINCIPAL(): #按月分期 if monthes2Return == 1: #只借一个月 monthlyInterest = bidRequestAmount * monthlyRate else: temp1 = bidRequestAmount * monthlyRate temp2 = pow(Decimal('1') + monthlyRate, monthes2Return) temp3 = pow(Decimal('1') + monthlyRate, monthes2Return) - Decimal('1') temp4 = pow(Decimal('1') + monthlyRate, monthIndex-1) #计算每月还款 monthToReturnMoney = (temp1 * temp2) / temp3 #算出总还款 totalReturnMoney = monthToReturnMoney * Decimal(str(monthes2Return)) #算出利息 totalInterest = totalReturnMoney - bidRequestAmount if monthIndex < monthes2Return: monthlyInterest = ((temp1 - monthToReturnMoney) * temp4)+monthToReturnMoney elif monthIndex == monthes2Return: temp6 = Decimal('0.0000') # 汇总最后一期之前所有利息之和 for i in range(1,monthes2Return): temp5 = pow(Decimal('1') + monthlyRate, i-1) monthlyInterest = ((temp1 - monthToReturnMoney) * temp5) + monthToReturnMoney temp6 = temp6 + monthlyInterest monthlyInterest = totalInterest - temp6 # for elif returnType == BidConst.GET_RETURN_TYPE_MONTH_INTEREST(): #按月到期 monthlyInterest = DecimalFormatUtil.amountformat((bidRequestAmount * monthlyRate)) return monthlyInterest
def calMonthToReturnMoney(cls, returnType, bidRequestAmount, yearRate, monthIndex, monthes2Return): # cls.cal_set() bidRequestAmount = Decimal(str(bidRequestAmount)) yearRate = Decimal(str(yearRate)) monthToReturnMoney = Decimal('0.0000') monthlyRate = cls.getMonthlyRate(yearRate) if returnType == BidConst.GET_RETURN_TYPE_MONTH_INTEREST_PRINCIPAL():#按月分期 if monthes2Return == 1: # 只借一个月 monthlyInterest = bidRequestAmount + (bidRequestAmount * monthlyRate) else: temp1 = bidRequestAmount * monthlyRate temp2 = pow(Decimal('1') + monthlyRate, monthes2Return) temp3 = pow(Decimal('1') + monthlyRate, monthes2Return) - Decimal('1') #计算每月还款 monthToReturnMoney = (temp1 * temp2) / temp3 elif returnType == BidConst.GET_RETURN_TYPE_MONTH_INTEREST(): #按月到期 monthlyInterest = bidRequestAmount * monthlyRate if monthIndex == monthes2Return: monthToReturnMoney = bidRequestAmount + monthlyInterest elif monthIndex < monthes2Return: monthToReturnMoney = monthlyInterest return DecimalFormatUtil.formatBigDecimal(monthToReturnMoney,BidConst.STORE_SCALE())
def formate_decimal_show(value): return DecimalFormatUtil.formatBigDecimal(value, BidConst.DISPLAY_SCALE())
def save_models(self): #获取保持对象 obj = self.new_obj obj.save() if obj is not None: bid_audit = obj bid_audit.auditor = self.user.username bid_audit.audiTime = timezone.now() bid_request = bid_audit.bidRequestId #如果审核成功 if bid_audit.state == BitStatesUtils.STATE_AUDIT(): # // 1, 对借款要做什么事情? # // ** 1.1 # 修改借款状态(还款中) bid_request.bidRequestState = BidConst.GET_BIDREQUEST_STATE_PAYING_BACK() #还款状态 bid_request.note = bid_audit.remark #添加满标二审历史 self.createBrAuditHistory(bid_request) # // 2, 对借款人要做什么事情? # // ** 2.1 # 借款人收款操作 borrow_account = Account.objects.get(userProfile=bid_request.createUser.userProfile) # // ** *2.1.1 账户余额增加, borrow_account.usableAmount = borrow_account.usableAmount + bid_request.bidRequestAmount # // ** *2.1.2生成收款流水; self.generateBorrowSuccessFlow(borrow_account=borrow_account,br=bid_request) # // ** *2.1.3修改待还本息; borrow_account.unReturnAmount = borrow_account.unReturnAmount + bid_request.bidRequestAmount + bid_request.totalRewardAmount # // ** *2.1.4修改可用信用额度; borrow_account.remainBorrowLimit = borrow_account.remainBorrowLimit - bid_request.bidRequestAmount # // ** 2.2移除借款人借款进行中状态码; bid_request.createUser.userProfile.removeState(BitStatesUtils.GET_OP_HAS_BIDREQUEST_PROCESS()) bid_request.createUser.userProfile.save() # // ** 2.3支付借款手续费计算 manage_charge_fee = CalculatetUtil.calAccountManagementCharge(bid_request.bidRequestAmount) # // ** *2.3.1可用余额减少 borrow_account.usableAmount = borrow_account.usableAmount - manage_charge_fee borrow_account.save() # // ** *2.3.2生成支付借款手续费流水; self.generateBorrowChargeFeeFlow(borrow_account=borrow_account,br=bid_request,manageChargeFee=manage_charge_fee) # // ** *2.3.3平台收取借款手续费; self.systemReceiveChargeFeeFromBorrow(br=bid_request,manage_charge_fee=manage_charge_fee) # // 3, 对投资人要做什么事情? # // ** 3.1遍历投标; bids = bid_request.bids.values_list('bidUser') # 取出所有投标用户id, 并去重 bid_user_ids = list(set((list(zip(*bids)))[0])) # // 汇总利息, 用于最后一个投标的用户的利息计算 totalBidInterest = Decimal('0') for idx, bid_user_id in enumerate(bid_user_ids): #获取投资人 bids_temp_query = bid_request.bids.filter(bidUser_id=bid_user_id) # 获取相同投资人投的标数量 # 统计投资金额 available_amount_total = bids_temp_query.aggregate(Sum('availableAmount')) # 获取投资人账户 investor_account = Account.objects.get(userProfile=bids_temp_query.first().bidUser.userProfile) # 冻结余额减少 investor_account.freezedAmount = investor_account.freezedAmount - available_amount_total['availableAmount__sum'] # // ** 3.3生成成功投标流水 self.bidSuccessFlow(bid=bids_temp_query.first(),account=investor_account, total_bid_amount=available_amount_total['availableAmount__sum']) # // ** 3.4计算待收利息和待收本金 # // ** 3.4.1待收本金 investor_account.unReceivePrincipal = investor_account.unReceivePrincipal + available_amount_total['availableAmount__sum'] #临时利息变量 bidInterest = Decimal('0') # // ** 3.4.2待收利息 # // 待收利息 = 投标金额 / 借款总金额 * 借款总回报利息 # // 如果当前投标是整个投标列表中的最后一个投标;这个投标的利息 = 借款总回报利息 - 累加的投标利息 if idx == len(bid_user_ids) - 1: #计算最后投标人的利息 bidInterest = bid_request.totalRewardAmount - totalBidInterest else: bidInterest = (available_amount_total['availableAmount__sum'] / bid_request.bidRequestAmount) * bid_request.totalRewardAmount bidInterest = DecimalFormatUtil.formatBigDecimal(bidInterest, BidConst.STORE_SCALE()) #累加总的利息 用于最后利息的计算 totalBidInterest = totalBidInterest + bidInterest investor_account.unReceiveInterest = investor_account.unReceiveInterest + bidInterest investor_account.save() # // 4, 思考满标二审之后的流程(还款)对满标二审有什么影响 # // ** 4生成还款对象和回款对象 self.createPaymentSchedules(bid_request) elif bid_audit.state == BitStatesUtils.STATE_REJECT(): #审核失败,修改借款状态 bid_request.bidRequestState = BidConst.GET_BIDREQUEST_STATE_REJECTED() #满标审核拒绝 #进行退款 self.retureMoney(bid_request) # 审核失败,移出用户招标状态 bid_request.createUser.userProfile.removeState(BitStatesUtils.GET_OP_HAS_BIDREQUEST_PROCESS()) bid_request.createUser.userProfile.save() #保存模型 bid_request.save() bid_audit.save()
def getRemainAmount(self): return DecimalFormatUtil.formatBigDecimal( self.bidRequestAmount - self.currentSum, BidConst.DISPLAY_SCALE())
def calAccountManagementCharge(cls,bidRequestAmount): bidRequestAmount = Decimal(str(bidRequestAmount)) accountManagementCharge = DecimalFormatUtil.formatBigDecimal(bidRequestAmount * cls.__ACCOUNT_MANAGER_CHARGE_RATE, BidConst.STORE_SCALE()) return accountManagementCharge
def calInterestManagerCharge(cls, interest): interest =Decimal(str(interest)) return DecimalFormatUtil.formatBigDecimal(interest * cls.__INTEREST_MANAGER_CHARGE_RATE, BidConst.STORE_SCALE())