tempo-to-invoice-generator/invoice.py

79 lines
1.8 KiB
Python
Raw Normal View History

2024-12-15 15:02:08 -06:00
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2024 Jacob Babor <jacob@babor.tech>
#
# Distributed under terms of the MIT license.
from csv import DictReader
2024-12-15 15:11:11 -06:00
import datetime
2024-12-15 15:02:08 -06:00
import glob
import json
import os
2024-12-15 15:02:08 -06:00
import re
import requests
import textwrap
2024-12-15 15:11:11 -06:00
# Config globals
2024-12-15 15:02:08 -06:00
rate = float(41.00)
timesheet = {}
2024-12-15 15:11:11 -06:00
# First, acquire a CSV to look at
2024-12-15 15:02:08 -06:00
csv = glob.glob('*.csv')
if not csv:
print('No csv found!')
quit()
2024-12-15 15:11:11 -06:00
# Then, open that CSV and collect its data, storing it in timesheet
2024-12-15 15:02:08 -06:00
with open(csv[0], 'r') as csv:
items = DictReader(csv)
for row in items:
issue = row["Issue Key"]
2024-12-15 15:11:11 -06:00
date = datetime.datetime.strptime(row["Work date"], '%Y-%m-%d %H:%M').strftime('%Y-%m-%d')
2024-12-15 15:02:08 -06:00
hours = float(row["Hours"])
if not date in timesheet.keys():
timesheet[date] = {}
if not issue in timesheet[date].keys():
timesheet[date][issue] = 0
timesheet[date][issue] += hours
2024-12-15 15:11:11 -06:00
# Next, construct args for the request we'll make to invoice-generator.com's API
requestbody = {
"from": "JACOB",
"to": "CLIENT",
"number": 9999,
"date": "CURRENT_DATE",
"due_date": "DUE_DATE",
"terms": "Net 14 days",
"items": []
}
2024-12-15 15:02:08 -06:00
for k,v in timesheet.items():
print(k)
for i,h in v.items():
requestbody["items"].append({
"name": f"{ k } - { i }",
"quantity": h,
"unit_cost": rate
})
print(f' { i } - { h }')
2024-12-15 15:11:11 -06:00
# Submit said request
resp = requests.post(
url = 'https://invoice-generator.com',
json = json.dumps(requestbody),
headers = {
"Authorization": f"Bearer { os.environ['INVOICE_GENERATOR_APIKEY'] }"
}
)
if not resp.status_code == 200:
print(resp)
quit()
with open('out.pdf', 'w') as f:
f.write(resp.text)