Commit 7421fe4b authored by 李宗熹's avatar 李宗熹

对账单修复controller

parent 2058a29d
...@@ -28,15 +28,15 @@ class StatementController(Resource): ...@@ -28,15 +28,15 @@ class StatementController(Resource):
""".""" """."""
self.parser = reqparse.RequestParser() self.parser = reqparse.RequestParser()
# @login_require @login_require
def get(self): def get(self):
"""""" """"""
self.parser.add_argument('user_id', type=str, required=True, help='用户id不能为空') # self.parser.add_argument('user_id', type=str, required=True, help='用户id不能为空')
self.parser.add_argument('statement_time', required=False, help='对账单时间戳') self.parser.add_argument('statement_time', required=False, help='对账单时间戳')
args = self.parser.parse_args() args = self.parser.parse_args()
print(args.get('statement_time')) print(args.get('statement_time'))
user_id = args.get('user_id') # user_id = args.get('user_id')
# user_id = g.ifa_id user_id = g.ifa_id
print(user_id) print(user_id)
if args.get('statement_time') != '': if args.get('statement_time') != '':
year = datetime.fromtimestamp(int(args.get('statement_time')) / 1000).year year = datetime.fromtimestamp(int(args.get('statement_time')) / 1000).year
......
...@@ -33,7 +33,7 @@ def get_order_info(user_id): ...@@ -33,7 +33,7 @@ def get_order_info(user_id):
cur = tamp_diagnose_app_session.execute(sql_user) cur = tamp_diagnose_app_session.execute(sql_user)
data = cur.fetchall() data = cur.fetchall()
order_df = pd.DataFrame(list(data), order_df = pd.DataFrame(list(data),
columns=['fund_id', 'username', 'customer_name', 'customer_id', 'valueSex', 'type', columns=['fund_id', 'user_name', 'customer_name', 'customer_id', 'value_sex', 'type',
'order_type', 'order_type',
'pay_date', 'pay_date',
'subscription_fee', 'confirm_share_date', 'confirm_share', 'subscription_fee', 'confirm_share_date', 'confirm_share',
...@@ -90,8 +90,9 @@ def type2note(type): ...@@ -90,8 +90,9 @@ def type2note(type):
def cal_total_amount(df): def cal_total_amount(df):
purchase_amount = df[df['order_type'].isin([1, 2])]['confirm_amount'].sum() purchase_amount = df[df['order_type'].isin([1, 2])]['confirm_amount'].sum()
redem_amount = df[df['order_type'] == 3]['confirm_amount'].sum() redemption_amount = df[df['order_type'] == 3]['confirm_amount'].sum()
total_amount = purchase_amount - redem_amount subscription_fee = df['subscription_fee'].sum()
total_amount = purchase_amount - redemption_amount - subscription_fee
return total_amount return total_amount
...@@ -103,8 +104,8 @@ def num2chinese(number): ...@@ -103,8 +104,8 @@ def num2chinese(number):
return num2chinese_dict.get(number) return num2chinese_dict.get(number)
# def get_ifa_name(user): def get_tax_rate():
return 0
class Statement(object): class Statement(object):
...@@ -115,15 +116,32 @@ class Statement(object): ...@@ -115,15 +116,32 @@ class Statement(object):
self.quarter = (self.month - 1) // 3 self.quarter = (self.month - 1) // 3
if self.quarter == 0: if self.quarter == 0:
self.quarter = 4 self.quarter = 4
self.ifa_order = self.order_history() self.ifa_order, self.ifa_name, self.ifa_gender = self.order_history()
self.ifa_provision = self.provision_history() self.ifa_provision = self.provision_history()
def bill_month(self):
return str(self.year) + '年' + str(self.month) + '月'
def ifa_call(self):
if self.ifa_gender == 0:
ifa_gender = '女士'
elif self.ifa_gender == 1:
ifa_gender = '先生'
else:
ifa_gender = ''
return self.ifa_name + ifa_gender
def order_history(self): def order_history(self):
ifa_order = get_order_info(self.user_id) ifa_order = get_order_info(self.user_id)
ifa_name = ifa_order['user_name'].values[0]
try:
ifa_gender = ifa_order['value_sex'].values[0]
except:
ifa_gender = 2
end_date = get_end_date(self.year, self.month) end_date = get_end_date(self.year, self.month)
order_history = ifa_order[ifa_order['pay_date'] < end_date] order_history = ifa_order[ifa_order['pay_date'] < end_date]
# ifa_order['fund_name'] = ifa_order['fund_id'].apply(lambda x: get_fund_name(x, fund_type=2).values[0][0]) # ifa_order['fund_name'] = ifa_order['fund_id'].apply(lambda x: get_fund_name(x, fund_type=2).values[0][0])
return order_history return order_history, ifa_name, ifa_gender
def provision_history(self): def provision_history(self):
ifa_provision = get_provision_info(self.user_id) ifa_provision = get_provision_info(self.user_id)
...@@ -162,23 +180,11 @@ class Statement(object): ...@@ -162,23 +180,11 @@ class Statement(object):
'note': type2note(customer_df['order_type'].values[i]) 'note': type2note(customer_df['order_type'].values[i])
}) })
purchase_amount = customer_df[customer_df['order_type'].isin([1, 2])]['confirm_amount'].sum() total_amount = cal_total_amount(customer_df)
redam_amount = customer_df[customer_df['order_type'] == 3]['confirm_amount'].sum()
total_amount = purchase_amount - redam_amount
trade_record_data.append({'customer': customer_df['customer_name'].values[0], trade_record_data.append({'customer': customer_df['customer_name'].values[0],
'total_amount': Decimal(round(float(total_amount) / 10000.0, 2)), 'total_amount': Decimal(round(float(total_amount) / 10000.0, 2)),
'detail': detail}) 'detail': detail})
# if len(ifa_cus_order_info) == 0:
# trade_record_data = [{'customer': '',
# 'total_amount': '',
# 'detail': [{'fund': '',
# 'pay_date': '',
# 'confirm_share_date': '',
# 'confirm_amount': '',
# 'nav': '',
# 'confirm_share': '',
# 'note': ''}]}]
return trade_record_data return trade_record_data
def month_remain(self): def month_remain(self):
...@@ -209,7 +215,7 @@ class Statement(object): ...@@ -209,7 +215,7 @@ class Statement(object):
bonus_policy.append({'month_achieve': '达到1000万以上', 'bonus_policy_item': '标准管理费分成+0.1%'}) bonus_policy.append({'month_achieve': '达到1000万以上', 'bonus_policy_item': '标准管理费分成+0.1%'})
month_remain_data['bonus_policy'] = bonus_policy month_remain_data['bonus_policy'] = bonus_policy
bonus_image = 'https://tamp-pro.oss-cn-shanghai.aliyuncs.com/upload/CS_FILE_RECORD16081436133319.png' bonus_image = ''
month_remain_data['bonus_image'] = bonus_image month_remain_data['bonus_image'] = bonus_image
return month_remain_data return month_remain_data
...@@ -261,16 +267,7 @@ class Statement(object): ...@@ -261,16 +267,7 @@ class Statement(object):
subscription_data['total_after_tax'] = total_after_tax subscription_data['total_after_tax'] = total_after_tax
subscription_data['customer_detail'] = customer_detail subscription_data['customer_detail'] = customer_detail
# if len(ifa_cus_order_monthly) == 0: return subscription_data, total_after_tax
# subscription_data['customer_detail'] = [{'customer': '',
# 'before_tax': '',
# 'after_tax': '',
# 'detail': [{'fund': '',
# 'confirm_amount': '',
# 'pay_date': '',
# 'subscription_fee': '',
# 'tax': ''}]}]
return subscription_data
def manage_fee(self): def manage_fee(self):
manage_data = {} manage_data = {}
...@@ -280,6 +277,13 @@ class Statement(object): ...@@ -280,6 +277,13 @@ class Statement(object):
processed_provision = self.process_provision() processed_provision = self.process_provision()
for customer_fund in processed_provision: for customer_fund in processed_provision:
customer_id = customer_fund[0][0]
customer_name = self.ifa_order[self.ifa_order['customer_id'] == customer_id]['customer_name'].values[0]
fund_id = customer_fund[0][1]
try:
fund_name = get_fund_name(fund_id).values[0][0]
except:
fund_name = fund_id
customer_fund_df = customer_fund[1] customer_fund_df = customer_fund[1]
df_length = customer_fund_df['predict_date'].count() df_length = customer_fund_df['predict_date'].count()
before_tax = 0 before_tax = 0
...@@ -290,18 +294,16 @@ class Statement(object): ...@@ -290,18 +294,16 @@ class Statement(object):
tax_rate = get_tax_rate() tax_rate = get_tax_rate()
except: except:
tax_rate = 0 tax_rate = 0
detail.append({ detail.append({'date': customer_fund_df['predict_date'].values[i].strftime('%Y-%m-%d'),
'hold_share': customer_fund_df['retention_shares'].values[i], 'hold_share': customer_fund_df['retention_shares'].values[i],
'date': customer_fund_df['predict_date'].values[i].strftime('%Y-%m-%d'), 'nav': customer_fund_df['fund_nav'].values[i],
'manage_fee': customer_fund_df['manage_predict_fee'].values[i], 'manage_fee_day': customer_fund_df['manage_fee'].values[i]})
'tax': customer_fund_df['manage_predict_fee'].values[i] * tax_rate
})
before_tax += customer_fund_df['manage_fee'].values[i] before_tax += customer_fund_df['manage_fee'].values[i]
after_tax += customer_fund_df['manage_fee'].values[i] * (1 - tax_rate) after_tax += customer_fund_df['manage_fee'].values[i] * (1 - tax_rate)
customer_detail.append({'customer': customer_fund_df['customer_id'].values[0], customer_detail.append({'customer': customer_name,
'fund': customer_fund_df['fund_id'].values[0], 'fund': fund_name,
'before_tax': round(float(before_tax), 2), 'before_tax': round(float(before_tax), 2),
'after_tax': round(float(after_tax), 2), 'after_tax': round(float(after_tax), 2),
'detail': detail}) 'detail': detail})
...@@ -312,102 +314,164 @@ class Statement(object): ...@@ -312,102 +314,164 @@ class Statement(object):
manage_data['total_tax'] = total_before_tax - total_after_tax manage_data['total_tax'] = total_before_tax - total_after_tax
manage_data['total_after_tax'] = total_after_tax manage_data['total_after_tax'] = total_after_tax
manage_data['customer_detail'] = customer_detail manage_data['customer_detail'] = customer_detail
return manage_data, total_after_tax
# if len(processed_provision) == 0: def carry(self):
# manage_data['customer_detail'] = [{'customer': '', carry_data = {}
# 'before_tax': '', total_before_tax = 0
# 'after_tax': '', total_after_tax = 0
# 'detail': [{'fund': '', customer_detail = []
# 'confirm_amount': '',
# 'pay_date': '',
# 'manage_fee': '',
# 'tax': ''}]}]
return manage_data
def bill_month(self): year, month_list = get_month_list(self.quarter, self.year)
return str(self.year) + '年' + str(self.month) + '月' start_date = date(year=year, month=min(month_list), day=1)
end_date = get_end_date(year, max(month_list))
provision_quarterly = self.ifa_provision[
(self.ifa_provision['predict_date'] >= start_date) &
(self.ifa_provision['predict_date'] < end_date.date())]
processed_provision = list(provision_quarterly.groupby(provision_quarterly.fund_id))
def user_name(self): for fund in processed_provision:
# user_name = get_ifa_name(user)
return "王菲女士" fund_id = fund[0]
fund_df = fund[1]
carry_sum = fund_df['permc_exc_fee'].sum()
if carry_sum == 0:
continue
try:
fund_name = get_fund_name(fund_id).values[0][0]
except:
fund_name = fund_id
before_tax = 0
after_tax = 0
detail = []
for _, row in fund_df.iterrows():
provision_carry = row['permc_exc_fee']
if provision_carry == 0:
continue
try:
tax_rate = get_tax_rate()
except:
tax_rate = 0
customer_id = row['customer_id']
customer_name = self.ifa_order[self.ifa_order['customer_id'] == customer_id]['customer_name'].values[0]
order_id = row['order_id']
order_type = type2note(self.ifa_order[self.ifa_order['order_id'] == order_id]['order_type'].values[0])
confirm_date = self.ifa_order[self.ifa_order['order_id'] == order_id]['confirm_date'].values[0]
nav = self.ifa_order[self.ifa_order['order_id'] == order_id]['nav'].values[0]
native_amount = nav
provision_date = '2020-12-31'
detail.append({'customer': customer_name,
'order_type': order_type,
'confirm_date': confirm_date,
'native_amount': native_amount,
'nav': nav,
'provision_date': provision_date,
'provision_carry':provision_carry}
)
before_tax += provision_carry
after_tax += provision_carry * (1 - tax_rate)
customer_detail.append({
'fund': fund_name,
'before_tax': round(float(before_tax), 2),
'after_tax': round(float(after_tax), 2),
'detail': detail})
total_before_tax += before_tax
total_after_tax += after_tax
carry_data['total_before_tax'] = total_before_tax
carry_data['total_tax'] = total_before_tax - total_after_tax
carry_data['total_after_tax'] = total_after_tax
carry_data['customer_detail'] = customer_detail
return carry_data, total_after_tax
def incentive(self):
incentive_data = {}
total_before_tax = 0
total_after_tax = 0
customer_detail = []
processed_provision = self.process_provision()
for customer_fund in processed_provision:
customer_id = customer_fund[0][0]
customer_name = self.ifa_order[self.ifa_order['customer_id'] == customer_id]['customer_name'].values[0]
fund_id = customer_fund[0][1]
try:
fund_name = get_fund_name(fund_id).values[0][0]
except:
fund_name = fund_id
customer_fund_df = customer_fund[1]
incentive_rate_sum = customer_fund_df['incentive_fee_ratio'].sum()
if incentive_rate_sum == 0:
continue
df_length = customer_fund_df['predict_date'].count()
before_tax = 0
after_tax = 0
detail = []
for i in range(df_length):
incentive_rate = customer_fund_df['incentive_fee_ratio'].values[i]
if incentive_rate == 0:
continue
try:
tax_rate = get_tax_rate()
except:
tax_rate = 0
detail.append({'date': customer_fund_df['predict_date'].values[i].strftime('%Y-%m-%d'),
'hold_share': customer_fund_df['retention_shares'].values[i],
'nav': customer_fund_df['fund_nav'].values[i],
'incentive_day': customer_fund_df['manage_fee'].values[i] * incentive_rate})
before_tax += customer_fund_df['manage_fee'].values[i] * incentive_rate
after_tax += customer_fund_df['manage_fee'].values[i] * incentive_rate * (1 - tax_rate)
customer_detail.append({'customer': customer_name,
'fund': fund_name,
'before_tax': round(float(before_tax), 2),
'after_tax': round(float(after_tax), 2),
'detail': detail})
total_before_tax += before_tax
total_after_tax += after_tax
incentive_data['total_before_tax'] = total_before_tax
incentive_data['total_tax'] = total_before_tax - total_after_tax
incentive_data['total_after_tax'] = total_after_tax
incentive_data['customer_detail'] = customer_detail
return incentive_data, total_after_tax
def summary(self): def summary(self):
summary_data = {} summary_data = {}
summary_data['bill_month'] = self.bill_month() summary_data['bill_month'] = self.bill_month()
summary_data['user_name'] = self.user_name() summary_data['user_name'] = self.ifa_call()
summary_data['quarter'] = num2chinese(self.quarter) summary_data['quarter'] = num2chinese(self.quarter)
summary_data['trade_record'] = self.trade_record() summary_data['trade_record'] = self.trade_record()
summary_data['month_remain'] = self.month_remain() summary_data['month_remain'] = self.month_remain()
summary_data['subscription_fee'] = self.subscription_fee() summary_data['subscription_fee'], sub_after_tax = self.subscription_fee()
summary_data['manage_fee'] = self.manage_fee() summary_data['manage_fee'], man_after_tax = self.manage_fee()
# summary_data['manage_fee'] = {'total_before_tax': Decimal('20000.00'), summary_data['carry'] = self.carry()
# 'total_after_tax': Decimal('20000.00'), summary_data['incentive'], incen_after_tax = self.incentive()
# 'total_tax': Decimal('0.00'), summary_data['recommend_bonus'] = {'total_before_tax': Decimal('0'),
# 'customer_detail': 'total_after_tax': Decimal('0'),
# [{'customer': "王小帅", 'total_tax': Decimal('0.0'),
# 'before_tax': 2000.01,
# 'detail': [
# {'date': "2020-09-01",
# 'hold_share': Decimal('2000.00'),
# 'nav': Decimal(1.0083),
# 'manage_fee_day': Decimal('10.00')}
# ]}]}
summary_data['carry'] = {'total_before_tax': Decimal('20000.00'),
'total_after_tax': Decimal('20000.00'),
'total_tax': Decimal('0.00'),
'fund_detail':
[{'fund': "无量",
'before_tax': 2000.01,
'detail': [
{'customer': "王小帅",
'order_type': "申购",
'confirm_date': "2020-09-01",
'native_amount': Decimal('10000000.00'),
'nav': Decimal(1.0083),
'provision_date': "2020-09-01",
'provision_carry': Decimal('10000000.00')}]}]}
summary_data['incentive'] = {'total_before_tax': Decimal('20000.00'),
'total_after_tax': Decimal('20000.00'),
'total_tax': Decimal('0.00'),
'customer_detail':
[{
'customer': '熊大',
'fund': "无量",
'before_tax': 2000.01,
'detail': [
{
'date': "2020-09-01",
'hold_share': Decimal('10000000.00'),
'nav': Decimal(1.0083),
'incentive_day': Decimal(10.00)}]}]}
summary_data['recommend_bonus'] = {'total_before_tax': Decimal('20000.00'),
'total_after_tax': Decimal('20000.00'),
'total_tax': Decimal('0.00'),
'referral_detail': 'referral_detail':
[{'referral': "王铁柱", []}
'customer': '熊大',
'fund': "无量", carry_after_tax = 0
'before_tax': 2000.01, summary_after_tax = sub_after_tax + man_after_tax + incen_after_tax
'detail': [ summary_data['summary'] = {'total_after_tax': summary_after_tax,
{ 'detail': [{'item': "已结算申购费用", 'amount': sub_after_tax, 'note': ''},
'date': "2020-09-01", {'item': "已结算管理费用", 'amount': man_after_tax, 'note': ''},
'hold_share': Decimal('10000000.00'), {'item': "已结算业绩报酬费用", 'amount': carry_after_tax, 'note': ''},
'nav': Decimal(1.0083), {'item': "已结算业绩激励奖金", 'amount': incen_after_tax, 'note': ''},
'recommend_bonus_day': Decimal(10.00)}]}]} {'item': "已结算推荐费", 'amount': Decimal('0.00'), 'note': ''},
{'item': "{}年{}季度待结算佣金".format(self.year, num2chinese(self.quarter)),
summary_data['summary'] = {'total_after_tax': Decimal('1000000.00'), 'amount': Decimal('0.00'), 'note': ''}
'detail': [{'item': "已结算申购费用", 'amount': Decimal('100000.00'), 'note': ''},
{'item': "已结算管理费用", 'amount': Decimal('100000.00'), 'note': ''},
{'item': "已结算业绩报酬费用", 'amount': Decimal('100000.00'), 'note': ''},
{'item': "已结算业绩激励奖金", 'amount': Decimal('100000.00'), 'note': ''},
{'item': "已结算推荐费", 'amount': Decimal('100000.00'), 'note': ''},
{'item': "2020年三季度待结算佣金", 'amount': Decimal('100000.00'), 'note': ''}
]} ]}
return summary_data return summary_data
if __name__ == '__main__': if __name__ == '__main__':
ret = Statement(user_id='USER_INFO15914346866762', year=2020, month=12).summary() ret = Statement(user_id='USER_INFO15914346866762', year=2020, month=12).summary()
print(json.dumps(ret, cls=DateEncoder, ensure_ascii=False)) print(json.dumps(ret, cls=DateEncoder, ensure_ascii=False))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment