-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport_Expenses.py
187 lines (163 loc) · 5.9 KB
/
Export_Expenses.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
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
import datetime
import os
import platform
import subprocess
import tinydb
import Common
def HeaderHTML():
return (
"""<!DOCTYPE html>
<html>
<head>
<title>Expense Report</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
p {
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
}
h1 {
font-size: 24px;
font-family: Arial, Helvetica, sans-serif;
}
h2 {
font-size: 18px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
<link rel="icon" href="file:"""
+ os.path.join(os.path.realpath(os.path.dirname(__file__)), "Icon.png")
+ """" type="image/png">
</head>"""
) # Return the HTML header with the icon set to the local program icon.
def FooterHTML():
return """</html>""" # Return the HTML footer
def BodyHeadHTML():
return (
"""<body>
<img src="file:"""
+ os.path.join(os.path.realpath(os.path.dirname(__file__)), "Icon.png")
+ """" alt="Laser OMS Icon" width="100" style="margin-right: 0">
<h1>Expense Report</h1>
<p> This report was generated by Laser OMS on """
+ str(datetime.datetime.now())
+ """</p>
<hr>"""
) # Return the HTML body header block
def BodyFootHTML():
return """</body>""" # Return the HTML body footer block
def ExpenseToHTML(expenses):
code = ""
total = Common.Decimal(0)
for expense in expenses:
code += "<h2>" + expense["expense_name"] + "</h2> \n"
code += "<p>Expense Date: " + expense["expense_date"] + "</p> <br>\n"
code += (
"<p>Expense Total: $"
+ str(
Common.MonetaryMultiply(expense["expense_quantity"], expense["expense_unit_price"])
)
+ "</p> <br> \n"
)
code += (
"<p>Expense Quantity: "
+ str(expense["expense_quantity"])
+ " at unit price: $"
+ str(expense["expense_unit_price"])
+ "</p> <br> \n"
)
code += "<p>Expense Description: " + expense["expense_notes"] + "</p> <br> \n"
if expense["expense_image_path"] != "": # If there is an image, add it to the report
filetype = expense["expense_image_path"].split(".")[-1]
if filetype.lower() == "pdf":
code += (
'<p>Expense Image: <embed src="file:'
+ expense["expense_image_path"]
+ """" type="application/pdf" width="500" height="500"></p> <br> \n"""
)
elif (
filetype.lower() == "jpg" or filetype.lower() == "jpeg" or filetype.lower() == "png"
):
code += (
'<p>Expense Image: <img src="file:'
+ expense["expense_image_path"]
+ """" alt="Expense Image" width="500"></p> <br> \n"""
)
else:
code += (
'<p>Expense Image: <a href="file:'
+ expense["expense_image_path"]
+ """">Expense Image File</a></p> <br> \n"""
)
code += "<hr> \n"
total.add(
Common.MonetaryMultiply(expense["expense_quantity"], expense["expense_unit_price"])
)
code += "<h2>Total Expenses: $" + str(total) + "</h2> \n"
return code
def MakeExpenseReport(app, database, Year=None, Month=None, ShowNonVerified=False):
# Get the Expenses table from the database
expenses = database.table("Expenses")
if ShowNonVerified:
ActiveExpenses = expenses.search(tinydb.where("process_status") == "UTILIZE")
else:
ActiveExpenses = expenses.search(
(tinydb.where("process_status") == "UTILIZE")
& (~(tinydb.where("expense_image_path") == ""))
)
Year = str(Year) # Convert the year and month to strings for comparison
Month = str(Month)
if Year == "None": # If no year is specified, show all expenses
ShowExpenses = ActiveExpenses
else:
if Month == "None": # If no month is specified, show all expenses for the year
ShowExpenses = [
expense
for expense in ActiveExpenses
if Year in expense["expense_date"].split("-")[2]
]
else: # If a month is specified, show all expenses for the year and month
ShowExpenses = [
expense
for expense in ActiveExpenses
if (Year in expense["expense_date"].split("-")[2])
and (Month in expense["expense_date"].split("-")[0])
]
# Generate the HTML for the report
HTMLData = (
HeaderHTML() + BodyHeadHTML() + ExpenseToHTML(ShowExpenses) + BodyFootHTML() + FooterHTML()
)
NameModifier = "" # create a string to modify the file name based on the year and month
if Year != None:
NameModifier += Year
if Month != None:
NameModifier += Month
# get a save file path from the user
path = app.select_file(
title="Save Expense Report",
folder=".",
filetypes=[["All files", "*.*"]],
save=True,
filename=NameModifier + "ExpenseReport.html",
)
if path != None: # if the user didn't cancel the save dialog
try: # try to save the file
with open(path, "w") as f:
f.write(HTMLData)
f.close()
except:
app.error("Error", "Unable to save file!")
return
# open the file in the default browser
if platform.system() == "Darwin": # macOS
subprocess.call(("open", path))
elif platform.system() == "Windows": # Windows
os.startfile(path)
else: # linux variants
subprocess.call(("xdg-open", path))