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 ...@@ -5,6 +5,7 @@ import os
import re import re
import urllib.parse import urllib.parse
import warnings import warnings
from datetime import datetime
from decimal import Decimal from decimal import Decimal
import pandas as pd import pandas as pd
...@@ -81,7 +82,19 @@ def export_list_read_data(): ...@@ -81,7 +82,19 @@ def export_list_read_data():
return merged_df 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 = { params = {
"invoiceNumber": invoice_number, "invoiceNumber": invoice_number,
"payeeCode": payeeCode, "payeeCode": payeeCode,
...@@ -93,17 +106,11 @@ def invoice_details(invoice_number): ...@@ -93,17 +106,11 @@ def invoice_details(invoice_number):
page_get(full_url) page_get(full_url)
def export_details_read_data(invoice_number): def export_details_read_data(file_name):
# # 读取详情内容
file_name = f"payment\\{invoice_number}.csv"
if os.path.isfile(file_name):
return pd.read_csv(file_name)
count = 0 count = 0
while True: while True:
try: try:
invoice_details(invoice_number) page.ele("#line-items-export-to-spreadsheet-announce").click.to_download(rename=file_name)
page.ele("#line-items-export-to-spreadsheet-announce", timeout=3).click.to_download(rename=file_name)
file.wait_for_downloads(file_name) file.wait_for_downloads(file_name)
excel.remove_last_comma(file_name) excel.remove_last_comma(file_name)
break break
...@@ -128,6 +135,8 @@ def get_po_code(index, po_id) -> dict: ...@@ -128,6 +135,8 @@ def get_po_code(index, po_id) -> dict:
"po_id": po_id "po_id": po_id
} }
po_id = po_id[:8]
cache_key = "payment" cache_key = "payment"
payment_cache = rdb.get_client().hget(cache_key, po_id) payment_cache = rdb.get_client().hget(cache_key, po_id)
if payment_cache: if payment_cache:
...@@ -196,16 +205,9 @@ def price_extract_data(html_content): ...@@ -196,16 +205,9 @@ def price_extract_data(html_content):
return data_list return data_list
def click_get_price_data(invoice_number): def click_get_price_data():
cache_key = "price_data"
cache_value = rdb.get_client().hget(cache_key, invoice_number)
if cache_value:
return json.loads(cache_value)
while True: while True:
try: try:
# 进入详情页
invoice_details(invoice_number)
# 点击争议价tab # 点击争议价tab
page.ele("#pd").click() page.ele("#pd").click()
log.debug("等待争议数据加载,5秒后获取表单数据") log.debug("等待争议数据加载,5秒后获取表单数据")
...@@ -213,7 +215,6 @@ def click_get_price_data(invoice_number): ...@@ -213,7 +215,6 @@ def click_get_price_data(invoice_number):
table_html = page.ele("#priceDiscrepancyWithDMSGridForm", timeout=5).html table_html = page.ele("#priceDiscrepancyWithDMSGridForm", timeout=5).html
# 抓取表单数据 # 抓取表单数据
price_data = price_extract_data(table_html) price_data = price_extract_data(table_html)
rdb.get_client().hset(cache_key, invoice_number, json.dumps(price_data))
return price_data return price_data
except ElementNotFoundError: except ElementNotFoundError:
log.warning("未获取到表数据") log.warning("未获取到表数据")
...@@ -259,6 +260,7 @@ def handle_data(detail_datum, vendor, deduction_points): ...@@ -259,6 +260,7 @@ def handle_data(detail_datum, vendor, deduction_points):
# 复制原始行数据,避免直接修改 # 复制原始行数据,避免直接修改
record = detail_datum.copy() record = detail_datum.copy()
record.update({"Amount": amount})
record["IsFinished"] = is_finished record["IsFinished"] = is_finished
record["DeductionPoints"] = f"{deduction_points}%" # 拼接百分号 record["DeductionPoints"] = f"{deduction_points}%" # 拼接百分号
record["Code"] = vendor record["Code"] = vendor
...@@ -266,11 +268,17 @@ def handle_data(detail_datum, vendor, deduction_points): ...@@ -266,11 +268,17 @@ def handle_data(detail_datum, vendor, deduction_points):
return record return record
def main(): def main():
list_data = export_list_read_data() list_data = export_list_read_data()
# list_data = list_data[25:] # 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)} 订单") log.info(f"共计:{len(list_data)} 订单")
all_normal_pay_data = [] all_normal_pay_data = []
...@@ -278,40 +286,47 @@ def main(): ...@@ -278,40 +286,47 @@ def main():
i = 0 i = 0
for _, data in list_data.iterrows(): for _, data in list_data.iterrows():
i += 1 i += 1
origin_invoice_number = data.get("Invoice Number") invoice_number = data.get("Invoice Number")
invoice_amount = data.get("Invoice Amount") invoice_amount = data.get("Invoice Amount")
invoice_number = origin_invoice_number
# 获取当前订单的Payee和优惠比例 # 获取当前订单的Payee和优惠比例
vendor_payment_terms = get_po_code(i, invoice_number[:8]) vendor_payment_terms = get_po_code(i, invoice_number)
vendor_payment_terms['invoice_number'] = origin_invoice_number
log.info(vendor_payment_terms) log.info(vendor_payment_terms)
vendor = vendor_payment_terms['vendor'] vendor = vendor_payment_terms['vendor']
deduction_points = int(vendor_payment_terms['payment_terms']) deduction_points = int(vendor_payment_terms['payment_terms'])
# 处理单号主要为了进入详情页
last_two = invoice_number[-2:] # 取后两位 last_two = invoice_number[-2:] # 取后两位
last_three = invoice_number[-3:] # 取后三位 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": if last_three == "PCR" or last_two == "PC":
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)
# 获取争议数据 # 获取争议数据
all_price_data = click_get_price_data(invoice_number) 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) handle_after_price_data = handle_price_data(price_data, invoice_amount)
price_data = handle_data(handle_after_price_data, vendor, deduction_points) format_price_data = handle_data(handle_after_price_data, vendor, deduction_points)
all_price_pay_data.append(pd.DataFrame(price_data, index=[0])) all_price_pay_data.append(pd.DataFrame(format_price_data, index=[0]))
else: else:
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文件并读取数据 # 下载excel文件并读取数据
detail_data = export_details_read_data(invoice_number) detail_data = export_details_read_data(file_name)
if detail_data is None: if detail_data is None:
log.error("数据存在问题,请手动处理") log.error("数据存在问题,请手动处理")
continue continue
...@@ -329,11 +344,11 @@ def main(): ...@@ -329,11 +344,11 @@ def main():
if all_normal_pay_data: if all_normal_pay_data:
# 将所有数据合并为一个 DataFrame # 将所有数据合并为一个 DataFrame
normal_pay_summary = pd.concat(all_normal_pay_data, ignore_index=True) 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: if all_price_pay_data:
price_pay_summary = pd.concat(all_price_pay_data, ignore_index=True) 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__': 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