|
|
@ -12,13 +12,20 @@ import click |
|
|
|
import pyperclip |
|
|
|
import pyperclip |
|
|
|
from bs4 import BeautifulSoup |
|
|
|
from bs4 import BeautifulSoup |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RECIPIENT_MAP = { |
|
|
|
|
|
|
|
"calvino": "Céline", |
|
|
|
|
|
|
|
"pappas": "Babis", |
|
|
|
|
|
|
|
"slesarenko": "Slava", |
|
|
|
|
|
|
|
"makro": "Makro", |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def open_invoice(file_path): |
|
|
|
def open_invoice(file_path): |
|
|
|
with open(file_path, "r") as handle: |
|
|
|
with open(file_path, "r") as handle: |
|
|
|
return BeautifulSoup(handle, "xml") |
|
|
|
return BeautifulSoup(handle, "xml") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def drill_down(soup, tags): |
|
|
|
def _drill_down(soup, tags): |
|
|
|
current_tag, *rest_tags = tags |
|
|
|
current_tag, *rest_tags = tags |
|
|
|
findings = soup.find_all(current_tag) |
|
|
|
findings = soup.find_all(current_tag) |
|
|
|
if not findings: |
|
|
|
if not findings: |
|
|
@ -30,11 +37,25 @@ def drill_down(soup, tags): |
|
|
|
yield from drill_down(child, rest_tags) |
|
|
|
yield from drill_down(child, rest_tags) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def drill_down(soup, *tag_lists): |
|
|
|
|
|
|
|
for tags in tag_lists: |
|
|
|
|
|
|
|
result = list(_drill_down(soup, tags)) |
|
|
|
|
|
|
|
if result != [None]: |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_recipient(soup): |
|
|
|
def get_recipient(soup): |
|
|
|
results = drill_down( |
|
|
|
results = drill_down( |
|
|
|
soup, ["cac:DeliveryParty", "cac:PartyName", "cbc:Name"] |
|
|
|
soup, |
|
|
|
|
|
|
|
["cac:DeliveryParty", "cac:PartyName", "cbc:Name"], |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
"ram:ApplicableHeaderTradeDelivery", |
|
|
|
|
|
|
|
"ram:ShipToTradeParty", |
|
|
|
|
|
|
|
"ram:Name", |
|
|
|
|
|
|
|
], |
|
|
|
) |
|
|
|
) |
|
|
|
return next(results) |
|
|
|
return results[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_recipient_short_name(soup): |
|
|
|
def get_recipient_short_name(soup): |
|
|
@ -42,18 +63,23 @@ def get_recipient_short_name(soup): |
|
|
|
if full_text is None: |
|
|
|
if full_text is None: |
|
|
|
return "+++ UNKNOWN +++" |
|
|
|
return "+++ UNKNOWN +++" |
|
|
|
full_text = full_text.lower() |
|
|
|
full_text = full_text.lower() |
|
|
|
if "calvino" in full_text: |
|
|
|
for key, value in RECIPIENT_MAP.items(): |
|
|
|
return "Céline" |
|
|
|
if key in full_text: |
|
|
|
if "pappas" in full_text: |
|
|
|
return value |
|
|
|
return "Babis" |
|
|
|
|
|
|
|
if "slesarenko" in full_text: |
|
|
|
|
|
|
|
return "Slava" |
|
|
|
|
|
|
|
return "CPI" |
|
|
|
return "CPI" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_items(soup): |
|
|
|
def get_items(soup): |
|
|
|
result = drill_down(soup, ["cac:InvoiceLine", "cac:Item", "cbc:Name"]) |
|
|
|
results = drill_down( |
|
|
|
return list(result) |
|
|
|
soup, |
|
|
|
|
|
|
|
["cac:InvoiceLine", "cac:Item", "cbc:Name"], |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
"ram:IncludedSupplyChainTradeLineItem", |
|
|
|
|
|
|
|
"ram:SpecifiedTradeProduct", |
|
|
|
|
|
|
|
"ram:Name", |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
return results |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse(file_path): |
|
|
|
def parse(file_path): |
|
|
@ -61,7 +87,7 @@ def parse(file_path): |
|
|
|
recipient = get_recipient_short_name(soup) |
|
|
|
recipient = get_recipient_short_name(soup) |
|
|
|
items = get_items(soup) |
|
|
|
items = get_items(soup) |
|
|
|
lines = [f"for {recipient}:"] + items |
|
|
|
lines = [f"for {recipient}:"] + items |
|
|
|
text = "\n".join(lines) |
|
|
|
text = "\n".join((str(line) for line in lines)) |
|
|
|
pyperclip.copy(text) |
|
|
|
pyperclip.copy(text) |
|
|
|
return text |
|
|
|
return text |
|
|
|
|
|
|
|
|
|
|
|