Commit 8bc14769 authored by 邱阿朋's avatar 邱阿朋

payment 优化

parent 32f102c2
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -5,6 +5,7 @@ import os
import re
import urllib.parse
import warnings
from datetime import datetime
from decimal import Decimal
import pandas as pd
......@@ -81,7 +82,19 @@ def export_list_read_data():
return merged_df
def invoice_details(invoice_number):
def invoice_details(invoice_number,last_two,last_three):
if len(invoice_number) > 8:
# 检查后两位是否在测试列表中
if last_two in ["MA", "PC"]:
invoice_number = invoice_number[:-2] # 去掉后两位
if last_three in ["PCR"]:
invoice_number = invoice_number[:-3] # 去掉最后三位
if last_three in ["+SC", "SC-"]:
invoice_number = invoice_number[:-3] # 去掉最后三位
invoice_number = invoice_number + 'SCR'
if last_two == "SC":
invoice_number = invoice_number + 'R'
params = {
"invoiceNumber": invoice_number,
"payeeCode": payeeCode,
......@@ -93,17 +106,11 @@ def invoice_details(invoice_number):
page_get(full_url)
def export_details_read_data(invoice_number):
# # 读取详情内容
file_name = f"payment\\{invoice_number}.csv"
if os.path.isfile(file_name):
return pd.read_csv(file_name)
def export_details_read_data(file_name):
count = 0
while True:
try:
invoice_details(invoice_number)
page.ele("#line-items-export-to-spreadsheet-announce", timeout=3).click.to_download(rename=file_name)
page.ele("#line-items-export-to-spreadsheet-announce").click.to_download(rename=file_name)
file.wait_for_downloads(file_name)
excel.remove_last_comma(file_name)
break
......@@ -128,6 +135,8 @@ def get_po_code(index, po_id) -> dict:
"po_id": po_id
}
po_id = po_id[:8]
cache_key = "payment"
payment_cache = rdb.get_client().hget(cache_key, po_id)
if payment_cache:
......@@ -196,16 +205,9 @@ def price_extract_data(html_content):
return data_list
def click_get_price_data(invoice_number):
cache_key = "price_data"
cache_value = rdb.get_client().hget(cache_key, invoice_number)
if cache_value:
return json.loads(cache_value)
def click_get_price_data():
while True:
try:
# 进入详情页
invoice_details(invoice_number)
# 点击争议价tab
page.ele("#pd").click()
log.debug("等待争议数据加载,5秒后获取表单数据")
......@@ -213,7 +215,6 @@ def click_get_price_data(invoice_number):
table_html = page.ele("#priceDiscrepancyWithDMSGridForm", timeout=5).html
# 抓取表单数据
price_data = price_extract_data(table_html)
rdb.get_client().hset(cache_key, invoice_number, json.dumps(price_data))
return price_data
except ElementNotFoundError:
log.warning("未获取到表数据")
......@@ -259,6 +260,7 @@ def handle_data(detail_datum, vendor, deduction_points):
# 复制原始行数据,避免直接修改
record = detail_datum.copy()
record.update({"Amount": amount})
record["IsFinished"] = is_finished
record["DeductionPoints"] = f"{deduction_points}%" # 拼接百分号
record["Code"] = vendor
......@@ -266,11 +268,17 @@ def handle_data(detail_datum, vendor, deduction_points):
return record
def main():
list_data = export_list_read_data()
# list_data = list_data[25:]
excel.save_xls(list_data, "回款数据.xlsx", "Remittance payments")
# 获取当前日期和时间并格式化
current_datetime = datetime.now().strftime('%Y-%m-%d_%H-%M') # 格式化为 'YYYY-MM-DD_HH-MM-SS'
# 原文件名
file_name = "回款数据.xlsx"
# 拼接新的文件名
new_file_name = f"{current_datetime}_{file_name}"
excel.save_xls(list_data, new_file_name, "Remittance payments")
log.info(f"共计:{len(list_data)} 订单")
all_normal_pay_data = []
......@@ -278,43 +286,50 @@ def main():
i = 0
for _, data in list_data.iterrows():
i += 1
origin_invoice_number = data.get("Invoice Number")
invoice_number = data.get("Invoice Number")
invoice_amount = data.get("Invoice Amount")
invoice_number = origin_invoice_number
# 获取当前订单的Payee和优惠比例
vendor_payment_terms = get_po_code(i, invoice_number[:8])
vendor_payment_terms['invoice_number'] = origin_invoice_number
vendor_payment_terms = get_po_code(i, invoice_number)
log.info(vendor_payment_terms)
vendor = vendor_payment_terms['vendor']
deduction_points = int(vendor_payment_terms['payment_terms'])
# 处理单号主要为了进入详情页
last_two = invoice_number[-2:] # 取后两位
last_three = invoice_number[-3:] # 取后三位
if len(invoice_number) > 8:
# 检查后两位是否在测试列表中
if last_two in ["MA", "PC"]:
invoice_number = invoice_number[:-2] # 去掉后两位
if last_three in ["PCR", "+SC", "SC-"]:
invoice_number = invoice_number[:-3] # 去掉最后三位
if last_two == "SC":
invoice_number = invoice_number + 'R'
# 判断是否为争议订单
if last_three == "PCR" or last_two == "PC":
# 获取争议数据
all_price_data = click_get_price_data(invoice_number)
cache_key = "price_data"
price_data = rdb.get_client().hget(cache_key, invoice_number)
if price_data is None:
# 进入详情页
invoice_details(invoice_number, last_two, last_three)
# 获取争议数据
price_data = click_get_price_data()
# 缓存数据
rdb.get_client().hset(cache_key, invoice_number, json.dumps(price_data))
else:
price_data = json.loads(price_data)
# 争议回款
handle_after_price_data = handle_price_data(all_price_data, invoice_amount)
price_data = handle_data(handle_after_price_data, vendor, deduction_points)
all_price_pay_data.append(pd.DataFrame(price_data, index=[0]))
handle_after_price_data = handle_price_data(price_data, invoice_amount)
format_price_data = handle_data(handle_after_price_data, vendor, deduction_points)
all_price_pay_data.append(pd.DataFrame(format_price_data, index=[0]))
else:
# 下载excel文件并读取数据
detail_data = export_details_read_data(invoice_number)
if detail_data is None:
log.error("数据存在问题,请手动处理")
continue
file_name = f"payment\\{invoice_number}.csv"
if os.path.isfile(file_name):
detail_data = pd.read_csv(file_name)
else:
# 进入详情页
invoice_details(invoice_number, last_two, last_three)
# 下载excel文件并读取数据
detail_data = export_details_read_data(file_name)
if detail_data is None:
log.error("数据存在问题,请手动处理")
continue
# 初始化列表存储新字段数据
normal_pay_data = []
......@@ -329,11 +344,11 @@ def main():
if all_normal_pay_data:
# 将所有数据合并为一个 DataFrame
normal_pay_summary = pd.concat(all_normal_pay_data, ignore_index=True)
excel.save_xls(normal_pay_summary, "回款数据.xlsx", "正常回款导出明细")
excel.save_xls(normal_pay_summary, new_file_name, "正常回款导出明细")
if all_price_pay_data:
price_pay_summary = pd.concat(all_price_pay_data, ignore_index=True)
excel.save_xls(price_pay_summary, "回款数据.xlsx", "Price导出明细")
excel.save_xls(price_pay_summary, new_file_name, "Price导出明细")
if __name__ == '__main__':
......
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