Spaces:
Runtime error
Runtime error
Update invoice.py
Browse files- invoice.py +63 -18
invoice.py
CHANGED
|
@@ -112,15 +112,30 @@ def _render_pdf_reportlab(payload: Dict[str, Any], pdf_path: str) -> bool:
|
|
| 112 |
margin = 15 * mm
|
| 113 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 114 |
|
| 115 |
-
# Header with
|
| 116 |
y = H - margin
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Tax Invoice title
|
| 126 |
c.setFillColor(colors.black)
|
|
@@ -140,20 +155,25 @@ def _render_pdf_reportlab(payload: Dict[str, Any], pdf_path: str) -> bool:
|
|
| 140 |
y -= 12
|
| 141 |
y -= 10
|
| 142 |
|
| 143 |
-
#
|
|
|
|
|
|
|
|
|
|
| 144 |
c.setFont("Helvetica-Bold", 10)
|
| 145 |
c.drawString(margin, y, f"Customer: {payload.get('customer','Customer')}")
|
| 146 |
y -= 12
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
y -= 15
|
| 153 |
|
| 154 |
# Table header
|
| 155 |
c.setFont("Helvetica-Bold", 10)
|
| 156 |
-
table_y = y
|
| 157 |
cols = [("Item", margin), ("Description", margin + 50), ("Qty/Hours", margin + 300), ("Unit Price", margin + 380), ("Price", margin + 460)]
|
| 158 |
|
| 159 |
# Draw header background
|
|
@@ -165,12 +185,12 @@ def _render_pdf_reportlab(payload: Dict[str, Any], pdf_path: str) -> bool:
|
|
| 165 |
c.drawString(x + 3, y - 12, title)
|
| 166 |
y -= 20
|
| 167 |
|
| 168 |
-
#
|
| 169 |
c.setFont("Helvetica", 10)
|
| 170 |
modules = payload.get("modules", 1)
|
| 171 |
unit = float(payload.get("unit_price", 0.0))
|
| 172 |
|
| 173 |
-
# Draw table
|
| 174 |
c.rect(margin, y-15, W-2*margin, 15, fill=0, stroke=1)
|
| 175 |
|
| 176 |
c.drawString(margin + 15, y - 12, str(modules))
|
|
@@ -180,15 +200,38 @@ def _render_pdf_reportlab(payload: Dict[str, Any], pdf_path: str) -> bool:
|
|
| 180 |
c.drawRightString(margin + 510, y - 12, f"{unit:.2f}")
|
| 181 |
y -= 20
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# Third-party admin fee (if applicable)
|
| 184 |
admin = float(payload.get("admin_fee", 0.0) or 0.0)
|
| 185 |
if admin != 0:
|
| 186 |
c.rect(margin, y-15, W-2*margin, 15, fill=0, stroke=1)
|
|
|
|
| 187 |
c.drawString(margin + 53, y - 12, "JC Auditing administration fee")
|
| 188 |
c.drawString(margin + 315, y - 12, "1")
|
| 189 |
c.drawRightString(margin + 510, y - 12, f"{admin:.2f}")
|
| 190 |
y -= 20
|
| 191 |
-
|
| 192 |
y -= 20
|
| 193 |
|
| 194 |
# Payment information section (left side)
|
|
@@ -680,6 +723,7 @@ def do_generate(file_obj, inv_type, modules, audit_type, audit_date, name, addre
|
|
| 680 |
inv_dt = today_str()
|
| 681 |
admin, subtotal, gst, total = compute_totals(int(modules or 1), inv_type, unit)
|
| 682 |
|
|
|
|
| 683 |
payload = {
|
| 684 |
"invoice_number": inv_num,
|
| 685 |
"invoice_date": inv_dt,
|
|
@@ -697,6 +741,7 @@ def do_generate(file_obj, inv_type, modules, audit_type, audit_date, name, addre
|
|
| 697 |
"tax_rate": "10%",
|
| 698 |
"abn": "646 382 464 92",
|
| 699 |
"audit_date": audit_date or "",
|
|
|
|
| 700 |
}
|
| 701 |
global _last_payload_cache
|
| 702 |
_last_payload_cache = payload.copy()
|
|
|
|
| 112 |
margin = 15 * mm
|
| 113 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 114 |
|
| 115 |
+
# Header with logo (if exists)
|
| 116 |
y = H - margin
|
| 117 |
+
if os.path.exists(LOGO_PATH):
|
| 118 |
+
try:
|
| 119 |
+
c.drawImage(LOGO_PATH, margin, y - 50, width=W - 2*margin, height=40, preserveAspectRatio=True, mask='auto')
|
| 120 |
+
y -= 60
|
| 121 |
+
except Exception:
|
| 122 |
+
# Fallback to text header
|
| 123 |
+
c.setFont("Helvetica-Bold", 20)
|
| 124 |
+
c.setFillColor(colors.HexColor("#6FB643"))
|
| 125 |
+
c.drawString(margin, y, "SPRINGY CONSULTING SERVICES")
|
| 126 |
+
y -= 15
|
| 127 |
+
c.setFont("Helvetica-Bold", 10)
|
| 128 |
+
c.drawString(margin, y, "HEAVY VEHICLE AUDITING & COMPLIANCE")
|
| 129 |
+
y -= 25
|
| 130 |
+
else:
|
| 131 |
+
# Text header
|
| 132 |
+
c.setFont("Helvetica-Bold", 20)
|
| 133 |
+
c.setFillColor(colors.HexColor("#6FB643"))
|
| 134 |
+
c.drawString(margin, y, "SPRINGY CONSULTING SERVICES")
|
| 135 |
+
y -= 15
|
| 136 |
+
c.setFont("Helvetica-Bold", 10)
|
| 137 |
+
c.drawString(margin, y, "HEAVY VEHICLE AUDITING & COMPLIANCE")
|
| 138 |
+
y -= 25
|
| 139 |
|
| 140 |
# Tax Invoice title
|
| 141 |
c.setFillColor(colors.black)
|
|
|
|
| 155 |
y -= 12
|
| 156 |
y -= 10
|
| 157 |
|
| 158 |
+
# Get invoice type from payload
|
| 159 |
+
inv_type = payload.get('inv_type', 'springy') # Default to springy if not specified
|
| 160 |
+
|
| 161 |
+
# Customer block - behavior changes based on invoice type
|
| 162 |
c.setFont("Helvetica-Bold", 10)
|
| 163 |
c.drawString(margin, y, f"Customer: {payload.get('customer','Customer')}")
|
| 164 |
y -= 12
|
| 165 |
+
|
| 166 |
+
# For Springy invoices - show customer details under Customer
|
| 167 |
+
if inv_type == "springy":
|
| 168 |
+
c.setFont("Helvetica", 9)
|
| 169 |
+
for line in [payload.get("address",""), payload.get("email",""), payload.get("phone",""), payload.get("audit_date","")]:
|
| 170 |
+
if line:
|
| 171 |
+
c.drawString(margin, y, str(line))
|
| 172 |
+
y -= 11
|
| 173 |
y -= 15
|
| 174 |
|
| 175 |
# Table header
|
| 176 |
c.setFont("Helvetica-Bold", 10)
|
|
|
|
| 177 |
cols = [("Item", margin), ("Description", margin + 50), ("Qty/Hours", margin + 300), ("Unit Price", margin + 380), ("Price", margin + 460)]
|
| 178 |
|
| 179 |
# Draw header background
|
|
|
|
| 185 |
c.drawString(x + 3, y - 12, title)
|
| 186 |
y -= 20
|
| 187 |
|
| 188 |
+
# First table row - main item
|
| 189 |
c.setFont("Helvetica", 10)
|
| 190 |
modules = payload.get("modules", 1)
|
| 191 |
unit = float(payload.get("unit_price", 0.0))
|
| 192 |
|
| 193 |
+
# Draw table row border
|
| 194 |
c.rect(margin, y-15, W-2*margin, 15, fill=0, stroke=1)
|
| 195 |
|
| 196 |
c.drawString(margin + 15, y - 12, str(modules))
|
|
|
|
| 200 |
c.drawRightString(margin + 510, y - 12, f"{unit:.2f}")
|
| 201 |
y -= 20
|
| 202 |
|
| 203 |
+
# For Third Party invoices - show customer details in table rows
|
| 204 |
+
if inv_type == "third_party":
|
| 205 |
+
customer_details = [
|
| 206 |
+
f"{payload.get('customer','')} NHVR audit {payload.get('audit_date','')}".strip(),
|
| 207 |
+
payload.get("address",""),
|
| 208 |
+
payload.get("email",""),
|
| 209 |
+
payload.get("phone","")
|
| 210 |
+
]
|
| 211 |
+
|
| 212 |
+
for detail in customer_details:
|
| 213 |
+
if detail:
|
| 214 |
+
c.rect(margin, y-15, W-2*margin, 15, fill=0, stroke=1)
|
| 215 |
+
c.setFont("Helvetica", 9)
|
| 216 |
+
c.drawString(margin + 53, y - 12, detail)
|
| 217 |
+
y -= 15
|
| 218 |
+
|
| 219 |
+
# Add empty rows to match Excel table structure (show complete table)
|
| 220 |
+
empty_rows = 8 - (4 if inv_type == "third_party" else 0) # Adjust based on customer detail rows
|
| 221 |
+
for i in range(empty_rows):
|
| 222 |
+
c.rect(margin, y-15, W-2*margin, 15, fill=0, stroke=1)
|
| 223 |
+
y -= 15
|
| 224 |
+
|
| 225 |
# Third-party admin fee (if applicable)
|
| 226 |
admin = float(payload.get("admin_fee", 0.0) or 0.0)
|
| 227 |
if admin != 0:
|
| 228 |
c.rect(margin, y-15, W-2*margin, 15, fill=0, stroke=1)
|
| 229 |
+
c.setFont("Helvetica", 10)
|
| 230 |
c.drawString(margin + 53, y - 12, "JC Auditing administration fee")
|
| 231 |
c.drawString(margin + 315, y - 12, "1")
|
| 232 |
c.drawRightString(margin + 510, y - 12, f"{admin:.2f}")
|
| 233 |
y -= 20
|
| 234 |
+
|
| 235 |
y -= 20
|
| 236 |
|
| 237 |
# Payment information section (left side)
|
|
|
|
| 723 |
inv_dt = today_str()
|
| 724 |
admin, subtotal, gst, total = compute_totals(int(modules or 1), inv_type, unit)
|
| 725 |
|
| 726 |
+
# In do_generate function, add this line to the payload:
|
| 727 |
payload = {
|
| 728 |
"invoice_number": inv_num,
|
| 729 |
"invoice_date": inv_dt,
|
|
|
|
| 741 |
"tax_rate": "10%",
|
| 742 |
"abn": "646 382 464 92",
|
| 743 |
"audit_date": audit_date or "",
|
| 744 |
+
"inv_type": inv_type, # <-- ADD THIS LINE
|
| 745 |
}
|
| 746 |
global _last_payload_cache
|
| 747 |
_last_payload_cache = payload.copy()
|