-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice_maker.py
116 lines (97 loc) · 3.13 KB
/
invoice_maker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'''
-Headers: "Content-Type: application/json"
-Content: '{"from":"Invoiced, Inc.", "to":"Parag",
"logo":"https://invoiced.com/img/logo-invoice.png",
"number":1,
"items":[{"name":"Starter plan","quantity":1,"unit_cost":99}],
"notes":"Thanks for your business!"}'
'''
import typer
import requests
import os
import csv
from dataclasses import dataclass
from typing import List
import random
import string
@dataclass
class Invoice:
from_who: str
to_who: str
logo: str
number: str
date: str
due_date: str
items: List[dict]
notes: str
class CSVParser:
def __init__(self, csv_file):
self.field_names = (
'from_who',
'to_who',
'logo',
'number',
'date',
'due_date',
'items',
'notes'
)
self.csv_file = csv_file
def get_invoices_from_csv(self):
with open(self.csv_file, 'r') as f:
reader = csv.DictReader(f, self.field_names)
header = True
invoices = []
for line in reader:
if header:
header = False
continue
invoice_item = Invoice(**line)
invoice_item.items = eval(invoice_item.items)
invoice_item.number = ''.join(random.choices(
string.digits, k=6))
invoices.append(invoice_item)
return invoices
class MakeInvoice:
def __init__(self):
self.endpoint = "https://invoice-generator.com"
self.invoice_dir = f"{os.path.dirname(os.path.abspath(__file__))}" \
+ '/invoices'
self.headers = {'Content-Type': 'application/json',
'Accept-Language': 'el-GR'}
def convertJSON(self, invoice: Invoice):
parsed = {
'from': invoice.from_who,
'to': invoice.to_who,
'logo': invoice.logo,
'number': invoice.number,
'date': invoice.date,
'due_date': invoice.due_date,
'items': invoice.items,
'notes': invoice.notes
}
return parsed
def save_to_pdf(self, content, invoice: Invoice):
invoice_name = f"{invoice.number}_invoice.pdf"
invoice_path = f"{self.invoice_dir}/{invoice_name}"
with open(invoice_path, 'wb') as f:
typer.echo(f"Making invoice {invoice_name}")
f.write(content)
def get_invoice(self, invoice):
r = requests.post(self.endpoint,
json=self.convertJSON(invoice),
headers=self.headers)
if r.status_code == 200:
self.save_to_pdf(r.content, invoice)
typer.echo("File Saved!")
else:
typer.echo(f"Fail: {r.text}")
def main(csv_file: str = typer.Argument('invoices.csv')):
typer.echo(f"Running script with {csv_file}")
maker = MakeInvoice()
parser = CSVParser(csv_file)
invoices = parser.get_invoices_from_csv()
for invoice in invoices:
maker.get_invoice(invoice)
if __name__ == "__main__":
typer.run(main)