Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
amazon_reports
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
common
amazon_reports
Commits
207c8d15
Commit
207c8d15
authored
Nov 13, 2024
by
邱阿朋
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
payment 优化
parent
a3bdb79d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
77 deletions
+89
-77
requirements.txt
requirements.txt
+2
-1
payment.py
src/payment.py
+87
-76
No files found.
requirements.txt
View file @
207c8d15
...
...
@@ -4,4 +4,5 @@ xlrd==2.0.1
pandas
==2.0.3
openpyxl
==3.1.5
pyinstaller
==6.11.0
requests
==2.32.3
\ No newline at end of file
requests
==2.32.3
redis
==5.0.8
\ No newline at end of file
src/payment.py
View file @
207c8d15
# coding: utf-8
# 回款明细
import
json
import
os
import
re
import
urllib.parse
import
warnings
from
decimal
import
Decimal
import
pandas
as
pd
from
DrissionPage
import
ChromiumPage
from
DrissionPage.errors
import
ElementNotFoundError
from
lxml
import
etree
from
pandas.errors
import
ParserError
from
helper
import
helper
,
excel
,
file
,
domain
,
logger
from
helper
import
helper
,
excel
,
file
,
domain
,
logger
,
redisx
country
=
None
payeeCode
=
None
log
=
logger
.
ConsoleLog
()
rdb
=
redisx
.
RedisClient
()
page
=
ChromiumPage
()
page
.
set
.
load_mode
.
normal
()
...
...
@@ -79,7 +81,7 @@ def export_list_read_data():
return
merged_df
def
export_details_read_data
(
origin_invoice_number
,
invoice_number
):
def
invoice_details
(
invoice_number
):
params
=
{
"invoiceNumber"
:
invoice_number
,
"payeeCode"
:
payeeCode
,
...
...
@@ -90,6 +92,8 @@ def export_details_read_data(origin_invoice_number, invoice_number):
full_url
=
f
"hz/vendor/members/inv-mgmt/invoice-details?"
+
query_string
page_get
(
full_url
)
def
export_details_read_data
(
origin_invoice_number
,
invoice_number
):
# # 读取详情内容
file_name
=
f
"payment
\\
{origin_invoice_number}.csv"
if
os
.
path
.
isfile
(
file_name
):
...
...
@@ -98,6 +102,7 @@ def export_details_read_data(origin_invoice_number, invoice_number):
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
)
file
.
wait_for_downloads
(
file_name
)
excel
.
remove_last_comma
(
file_name
)
...
...
@@ -123,6 +128,14 @@ def get_po_code(index, po_id) -> dict:
"po_id"
:
po_id
}
cache_key
=
"payment"
payment_cache
=
rdb
.
get_client
()
.
hget
(
cache_key
,
po_id
)
if
payment_cache
:
cache_value
=
json
.
loads
(
payment_cache
)
result
[
"vendor"
]
=
cache_value
[
'vendor'
]
result
[
"payment_terms"
]
=
cache_value
[
'payment_terms'
]
return
result
page_get
(
f
"po/vendor/members/po-mgmt/order?poId={po_id}"
)
po_table
=
page
.
ele
(
"#po-header"
,
timeout
=
5
)
.
html
...
...
@@ -131,7 +144,6 @@ def get_po_code(index, po_id) -> dict:
# 获取 Vendor 内容
result
[
"vendor"
]
=
get_content
(
tree
,
2
,
2
)
# 正则表达式查找数字和%之间的内容
match
=
re
.
search
(
r'Payment Terms.*?(\d+
%
)'
,
po_table
)
if
match
:
...
...
@@ -139,6 +151,10 @@ def get_po_code(index, po_id) -> dict:
else
:
result
[
"payment_terms"
]
=
0
cache_value
=
result
.
copy
()
del
cache_value
[
'index'
]
rdb
.
get_client
()
.
hset
(
cache_key
,
po_id
,
json
.
dumps
(
cache_value
))
return
result
...
...
@@ -180,49 +196,48 @@ def price_extract_data(html_content):
return
data_list
def
click_get_price_data
():
try
:
# 获取 Amounts 表格html
page_html
=
page
.
ele
(
".a-box-inner"
,
timeout
=
3
)
.
html
# 使用 lxml 解析 HTML
tree
=
etree
.
HTML
(
page_html
)
# 提取第二个 class="a-column a-span4" 下的第三个 class="a-row invoice-property-field-row" 下的 span 的值
price_variance_amount
=
tree
.
xpath
(
'(//div[@class="a-column a-span4"])[2]//div[@class="a-row invoice-property-field-row"][3]//div[@class="a-column a-span6 a-span-last"]/span/text()'
)
# 检查内容是否有效
if
price_variance_amount
and
price_variance_amount
[
0
]
.
strip
()
!=
"-"
:
while
True
:
try
:
page
.
ele
(
"#pd"
,
timeout
=
5
)
.
click
()
log
.
debug
(
"等待争议数据加载,5秒后获取表单数据"
)
page
.
wait
(
5
)
table_html
=
page
.
ele
(
"#priceDiscrepancyWithDMSGridForm"
,
timeout
=
5
)
.
html
price_data
=
price_extract_data
(
table_html
)
return
price_data
except
ElementNotFoundError
:
log
.
warning
(
"未获取到表数据"
)
break
return
[]
except
ElementNotFoundError
:
page
.
refresh
()
click_get_price_data
()
def
handle_price_data
(
price_data
,
detail_data
):
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
)
while
True
:
try
:
# 进入详情页
invoice_details
(
invoice_number
)
# 点击争议价tab
page
.
ele
(
"#pd"
)
.
click
()
log
.
debug
(
"等待争议数据加载,5秒后获取表单数据"
)
page
.
wait
(
5
)
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
(
"未获取到表数据"
)
break
def
handle_price_data
(
price_data_list
,
invoice_amount
):
result
=
{}
invoice_amount
=
float
(
f
"{invoice_amount:.2f}"
)
"""处理争议数据"""
for
price
in
price_data
:
if
price
[
'ASIN'
]
==
detail_data
.
get
(
'ASIN'
):
result
=
detail_data
.
to_dict
()
result
[
'Quantity received'
]
=
price
[
'QUANTITY'
]
if
price
[
'RESOLUTION_DECISION'
]
==
"Approved"
:
for
data_list
in
price_data_list
:
# 计算差异金额单价
unit_cost_float
=
Decimal
(
data_list
[
'INVOICE_COST'
])
-
Decimal
(
data_list
[
'INITIAL_RESEARCH_COST'
])
unit_cost
=
unit_cost_float
*
int
(
data_list
[
'QUANTITY'
])
amount
=
float
(
f
"{unit_cost:.2f}"
)
if
amount
==
invoice_amount
:
result
=
data_list
result
[
'Quantity received'
]
=
data_list
[
'QUANTITY'
]
result
[
'UnitCost'
]
=
unit_cost_float
result
[
'Amount'
]
=
f
"${amount:.2f}"
if
data_list
[
'RESOLUTION_DECISION'
]
==
"Approved"
:
result
[
'Shortage quantity'
]
=
0
result
[
'Amount'
]
=
price
[
'RESOLUTION_COST'
]
else
:
result
[
'Shortage quantity'
]
=
1
amount
=
(
float
(
price
[
'INVOICE_COST'
])
-
float
(
price
[
'INITIAL_RESEARCH_COST'
]))
*
int
(
price
[
'QUANTITY'
])
result
[
'Amount'
]
=
f
"${amount:.2f}"
break
return
result
...
...
@@ -264,6 +279,9 @@ def main():
for
_
,
data
in
list_data
.
iterrows
():
i
+=
1
origin_invoice_number
=
data
.
get
(
"Invoice Number"
)
invoice_amount
=
data
.
get
(
"Invoice Amount"
)
invoice_number
=
origin_invoice_number
# 获取当前订单的Payee和优惠比例
...
...
@@ -274,47 +292,39 @@ def main():
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
:
last_two
=
invoice_number
[
-
2
:]
# 取后两位
last_three
=
invoice_number
[
-
3
:]
# 取后三位
# 检查后两位是否在测试列表中
if
last_two
in
[
"PC"
,
"MA"
,
"SC"
]:
invoice_number
=
invoice_number
[:
-
2
]
# 去掉后两位
if
last_three
in
[
"PCR"
,
"+SC"
,
"SC-"
]:
invoice_number
=
invoice_number
[:
-
3
]
# 去掉最后三位
# 下载excel文件并读取数据
detail_data
=
export_details_read_data
(
origin_invoice_number
,
invoice_number
)
if
detail_data
is
None
:
log
.
error
(
"数据存在问题,请手动处理"
)
continue
# 获取争议数据
all_price_data
=
click_get_price_data
()
# 初始化列表存储新字段数据
normal_pay_data
=
[]
price_pay_data
=
[]
for
index
,
detail_datum
in
detail_data
.
iterrows
():
# 正常回款数据
success_data
=
handle_data
(
detail_datum
,
vendor
,
deduction_points
)
# 将处理后的记录添加到临时列表
normal_pay_data
.
append
(
success_data
)
if
all_price_data
:
# 争议回款
handle_after_price_data
=
handle_price_data
(
all_price_data
,
detail_datum
)
if
handle_after_price_data
:
price_data
=
handle_data
(
handle_after_price_data
,
vendor
,
deduction_points
)
# 将处理后的记录添加到临时列表
price_pay_data
.
append
(
price_data
)
# 添加到汇总列表
all_normal_pay_data
.
append
(
pd
.
DataFrame
(
normal_pay_data
))
if
len
(
price_pay_data
)
>
0
:
all_price_pay_data
.
append
(
pd
.
DataFrame
(
price_pay_data
))
# 判断是否为争议订单
if
last_three
==
"PCR"
:
# 获取争议数据
all_price_data
=
click_get_price_data
(
invoice_number
)
# 争议回款
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
]))
else
:
# 下载excel文件并读取数据
detail_data
=
export_details_read_data
(
origin_invoice_number
,
invoice_number
)
if
detail_data
is
None
:
log
.
error
(
"数据存在问题,请手动处理"
)
continue
# 初始化列表存储新字段数据
normal_pay_data
=
[]
for
index
,
detail_datum
in
detail_data
.
iterrows
():
# 正常回款数据
success_data
=
handle_data
(
detail_datum
,
vendor
,
deduction_points
)
# 将处理后的记录添加到临时列表
normal_pay_data
.
append
(
success_data
)
# 添加到汇总列表
all_normal_pay_data
.
append
(
pd
.
DataFrame
(
normal_pay_data
))
if
all_normal_pay_data
:
# 将所有数据合并为一个 DataFrame
...
...
@@ -325,6 +335,7 @@ def main():
price_pay_summary
=
pd
.
concat
(
all_price_pay_data
,
ignore_index
=
True
)
excel
.
save_xls
(
price_pay_summary
,
"回款数据.xlsx"
,
"Price导出明细"
)
if
__name__
==
'__main__'
:
try
:
country
=
helper
.
get_input_with_default
(
"国家(目前支持[DE,FR,JP,CA,UK,US])"
,
"US"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment