-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (223 loc) · 9.56 KB
/
main.py
File metadata and controls
278 lines (223 loc) · 9.56 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import sys
import fitz
import smtplib
import ssl
import os
import csv
from dotenv import load_dotenv
from datetime import datetime
from email.message import EmailMessage
from PyQt5.QtWidgets import (
QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout,
QHBoxLayout, QPushButton, QComboBox, QFormLayout, QMessageBox
)
from PyQt5.QtCore import QTimer
load_dotenv()
EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
class InvoiceAutomation(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Invoice Automation")
self.setGeometry(100, 100, 400, 500)
self.payment_methods = {
"Main Bank": {
"Recipient": "BH Company",
"Bank": "Bank of Ireland",
"IBAN": "AB12 3456 7891 1234",
"BIC": "IEI44556"
},
"Second Bank": {
"Recipient": "BH Company",
"Bank": "Bank of Scotland",
"IBAN": "SC12 8765 7891 3451",
"BIC": "SCI46536"
},
"Private Bank": {
"Recipient": "Barra Harrison",
"Bank": "Shinhan Bank",
"IBAN": "DF45 9826 7712 8361",
"BIC": "KR128426"
}
}
self.fields = [
("Partner", "partner_entry"),
("Partner Street", "partner_street_entry"),
("Partner ZIP Code", "partner_zip_country_entry"),
("Invoice Number", "invoice_number_entry"),
("Service Description", "service_description_entry"),
("Service Amount", "service_amount_entry"),
("Service Single Price", "service_single_price_entry"),
("Recipient Email", "recipient_email_entry")
]
self.entries = {}
self.init_ui()
self.ready_label = QLabel("")
self.ready_label.setStyleSheet("color: green; font-weight: bold;")
self.layout().addWidget(self.ready_label)
def init_ui(self):
layout = QVBoxLayout()
form_layout = QFormLayout()
for label_text, entry_key in self.fields:
entry = QLineEdit()
self.entries[entry_key] = entry
form_layout.addRow(QLabel(label_text), entry)
self.payment_dropdown = QComboBox()
self.payment_dropdown.addItems(self.payment_methods.keys())
form_layout.addRow(QLabel("Payment Method"), self.payment_dropdown)
self.create_button = QPushButton("Create Invoice")
self.create_button.clicked.connect(self.handle_create_invoice)
layout.addLayout(form_layout)
layout.addWidget(self.create_button)
self.setLayout(layout)
def validate_fields(self):
for label_text, entry_key in self.fields:
entry = self.entries[entry_key]
if not entry.text().strip():
QMessageBox.warning(
self,
"Missing Field",
f"The '{label_text}' field is required."
)
return False
return True
def handle_create_invoice(self):
if self.validate_fields():
self.create_invoice()
def create_invoice(self):
current_date = datetime.today().strftime("%Y-%m-%d")
# Extract payment method details
selected_bank = self.payment_methods[self.payment_dropdown.currentText()]
recipient_email = self.entries["recipient_email_entry"].text()
try:
quantity = float(self.entries["service_amount_entry"].text())
unit_price = float(self.entries["service_single_price_entry"].text())
sub_total = f"${quantity * unit_price:.2f}"
except ValueError:
QMessageBox.warning(self, "Invalid Input", "Please enter valid numbers for amount and price.")
return
# Prepare data for PDF replacement
data = {
"partner_entry": self.entries["partner_entry"].text(),
"partner_street_entry": self.entries["partner_street_entry"].text(),
"partner_zip_country_entry": self.entries["partner_zip_country_entry"].text(),
"invoice_number_entry": self.entries["invoice_number_entry"].text(),
"service_description_entry": self.entries["service_description_entry"].text(),
"service_amount_entry": self.entries["service_amount_entry"].text(),
"service_single_price_entry": self.entries["service_single_price_entry"].text(),
"payment_method": self.payment_dropdown.currentText(),
"current_date": current_date,
"sub_total": sub_total,
"bank_recipient": selected_bank["Recipient"],
"bank_name": selected_bank["Bank"],
"bank_iban": selected_bank["IBAN"],
"bank_bic": selected_bank["BIC"]
}
output_filename = f'invoice_{data["invoice_number_entry"].replace("#", "")}.pdf'
self.generate_invoice_pdf("updated_invoice_template.pdf", output_filename, data)
print(f"Invoice saved as {output_filename}")
QMessageBox.information(self, "Success", f"Invoice saved as {output_filename}")
self.save_invoice_to_csv(data)
print("Invoice data saved to CSV.")
self.send_email_with_attachment(
recipient_email,
subject="Your Invoice",
body="Attached is your invoice. Thank you!",
attachment_path=output_filename
)
self.clear_form()
def save_invoice_to_csv(self, data, filename="invoices_log.csv"):
file_exists = os.path.isfile(filename)
headers = [
"Date", "Invoice Number", "Partner", "Street", "ZIP Code",
"Service Description", "Amount", "Unit Price", "Subtotal", "Payment Method"
]
row = [
data["current_date"],
data["invoice_number_entry"],
data["partner_entry"],
data["partner_street_entry"],
data["partner_zip_country_entry"],
data["service_description_entry"],
data["service_amount_entry"],
data["service_single_price_entry"],
data["sub_total"],
data["payment_method"]
]
with open(filename, mode="a", newline="") as file:
writer = csv.writer(file)
if not file_exists:
writer.writerow(headers)
writer.writerow(row)
def send_email_with_attachment(self, recipient_email, subject, body, attachment_path):
sender_email = EMAIL_ADDRESS
sender_password = EMAIL_PASSWORD
message = EmailMessage()
message["From"] = sender_email
message["To"] = recipient_email
message["Subject"] = subject
message.set_content(body)
with open(attachment_path, "rb") as f:
file_data = f.read()
file_name = os.path.basename(attachment_path)
message.add_attachment(file_data, maintype="application", subtype="pdf", filename=file_name)
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, sender_password)
server.send_message(message)
print("Email sent successfully.")
QMessageBox.information(self, "Email Sent", f"Invoice sent to {recipient_email}")
except Exception as e:
print(f"Error sending email: {e}")
QMessageBox.warning(self, "Email Failed", "There was an error sending the email.")
def generate_invoice_pdf(self, template_path, output_path, replacements):
doc = fitz.open(template_path)
for page in doc:
for key, value in replacements.items():
placeholder = f"{{{key}}}"
text_instances = page.search_for(placeholder)
print(f"Found {len(text_instances)} instance(s) of {placeholder}")
for inst in text_instances:
# Redact the placeholder text
page.add_redact_annot(inst, fill=(1, 1, 1))
page.apply_redactions()
for inst in text_instances:
# Write the new value AFTER redaction is applied
x, y = inst.x0, inst.y0
if key in [
"invoice_number_entry",
"partner_entry",
"partner_zip_country_entry",
"partner_street_entry",
"current_date",
"bank_recipient",
"bank_name",
"bank_iban",
"bank_bic",
"sub_total"
]:
y += 15
font_size = 14
else:
font_size = 12
page.insert_text(
(x, y),
value,
fontname="helv",
fontsize=font_size,
color=(0, 0, 0),
)
doc.save(output_path)
doc.close()
def clear_form(self):
for entry in self.entries.values():
entry.clear()
self.payment_dropdown.setCurrentText("Main Bank")
self.ready_label.setText("✅ Ready for the next invoice!")
QTimer.singleShot(3000, lambda: self.ready_label.setText(""))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = InvoiceAutomation()
window.show()
sys.exit(app.exec_())