Skip to content

Commit

Permalink
Merge pull request #3 from Vandebron/feature/export-all-pdfs-for-run
Browse files Browse the repository at this point in the history
feature/export all pdfs for run
  • Loading branch information
SamTheisens authored Apr 2, 2022
2 parents 15722fd + 609de50 commit 6722a23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ pyinstaller = "==4.8"

[requires]
python_version = "3.9"

[scripts]
export = "python nmbrs_hibob.py"
19 changes: 10 additions & 9 deletions nmbrs_hibob.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ def get_employee(employee_id):
</emp:PersonalInfo_GetCurrent>"""


def do_request(body, service):
def do_request(body, service, print_response=False):
req = create_request(user_arg, password_arg, body, service)
# print(req)
response = requests.post(f"{service['url']}.asmx", data=req,
headers={'content-type': 'text/xml; charset=utf-8'})
if not response.ok:
sys.exit(response.text)
# print(response.text)
if print_response:
print(response.text)
return ET.ElementTree(ET.fromstring(response.text))


Expand Down Expand Up @@ -230,7 +231,7 @@ def find_employees_for_year(year):
def fetch_annual_statements(year):
employees = find_employees_for_year(year)
zip_file_name = f"yearly_statements_{year}.zip"
with ShadyBar("Fetching salary slip for employees", max=len(employees)) as bar:
with ShadyBar("Fetching annual statement for employees", max=len(employees)) as bar:
with ZipFile(zip_file_name, 'w') as zip_file:
for i, employee in enumerate(employees):
employee_response = do_request(get_employee(employee.id), emp)
Expand All @@ -239,12 +240,12 @@ def fetch_annual_statements(year):
request_body = get_annual_statement(employee.id, year)

response = do_request(request_body, emp)
pdf = response.find(f'.//emp:PDF', namespaces=ns)
pdfs = response.findall(f'.//emp:PDF', namespaces=ns)

if pdf is not None:
for index, pdf in enumerate(pdfs):
description = "Annual_Statement" if description_arg is None else description_arg
folder_name = employee_details.email if email_arg else employee_details.number
file_name = f"{folder_name}/{year}_{description}.pdf"
file_name = f"{folder_name}/{year}_{description}_{index}.pdf"
zip_file.writestr(file_name, base64.b64decode(pdf.text))

bar.next()
Expand All @@ -263,12 +264,12 @@ def fetch_salary_slips(year, run):

request_body = get_payslip(employee.id, run_arg, year)
response = do_request(request_body, com)
pdf = response.find(f'.//cs:PDF', namespaces=ns)
pdfs = response.findall(f'.//cs:PDF', namespaces=ns)

if pdf is not None:
for index, pdf in enumerate(pdfs):
description = run_info.description.replace(' ', '_') if description_arg is None else description_arg
folder_name = employee_details.email if email_arg else employee_details.number
file_name = f"{folder_name}/{year}_{run_info.number}_{description}.pdf"
file_name = f"{folder_name}/{year}_{run_info.number}_{description}_{index}.pdf"
zip_file.writestr(file_name, base64.b64decode(pdf.text))

bar.next()
Expand Down

0 comments on commit 6722a23

Please sign in to comment.