Spaces:
Sleeping
Sleeping
File size: 18,764 Bytes
b685682 |
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
import io
# streamlit_invoice_generator.py
import os
import zipfile
from typing import Dict, List
import pandas as pd
import streamlit as st
from PIL import Image
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from PyPDF2 import PdfReader, PdfWriter
from num2words import num2words # ✅ added for number-to-words
# -------------------------
# Utility helpers
# -------------------------
PAGE_W, PAGE_H = A4
def safe_str(x):
if pd.isna(x):
return ""
return str(x)
def format_amount(x):
try:
num = float(x)
return f"{round(num):,}" # ✅ rounds to nearest whole number with commas
except Exception:
return safe_str(x)
def to_words(value): # ✅ helper for number to words
try:
num = float(value)
if num.is_integer():
num = int(num)
return num2words(num, to="cardinal", lang="en").title() + " Only"
except Exception:
return ""
# overlay creation: draws text at specified positions
def create_overlay_pdf(invoice: dict, positions: dict, logo_path: str = None) -> io.BytesIO:
packet = io.BytesIO()
c = canvas.Canvas(packet, pagesize=A4)
# draw fields (all keys in positions)
for field, meta in positions.items():
if field in ["logo", "items_start_y", "items_row_height", "items_columns"]:
continue
if not (isinstance(meta, tuple) and len(meta) == 5):
continue
val = invoice.get(field, "")
x, y, fontname, fontsize, align = meta
c.setFont(fontname, fontsize)
if any(key in field.lower() for key in ["amount", "total"]):
text = format_amount(val)
else:
text = safe_str(val)
if align == "right":
c.drawRightString(x, y, text)
elif align == "center":
c.drawCentredString(x, y, text)
else:
c.drawString(x, y, text)
# draw items table
if "items_start_y" in positions:
y = positions["items_start_y"]
row_height = positions.get("items_row_height", 18)
col_map = positions.get("items_columns", {})
for idx, it in enumerate(invoice.get("Items", []), start=1):
x = col_map.get("sr")
if x is not None:
c.drawString(x, y, str(idx))
x = col_map.get("desc")
if x is not None:
c.drawString(x, y, safe_str(it.get("Service Details", "")))
x = col_map.get("hsn")
if x is not None:
c.drawString(x, y, safe_str(it.get("HSN", "")))
x = col_map.get("taxable")
if x is not None:
c.drawRightString(x, y, format_amount(it.get("Taxable Value", 0)))
x_perc = col_map.get("cgst_perc")
if x_perc is not None:
c.drawRightString(x_perc, y, safe_str(it.get("CGST %", "")))
x_amt = col_map.get("cgst")
if x_amt is not None:
c.drawRightString(x_amt, y, format_amount(it.get("CGST Amount", 0)))
x_perc = col_map.get("sgst_perc")
if x_perc is not None:
c.drawRightString(x_perc, y, safe_str(it.get("SGST %", "")))
x_amt = col_map.get("sgst")
if x_amt is not None:
c.drawRightString(x_amt, y, format_amount(it.get("SGST Amount", 0)))
x_perc = col_map.get("igst_perc")
if x_perc is not None:
c.drawRightString(x_perc, y, safe_str(it.get("IGST %", "")))
x_amt = col_map.get("igst")
if x_amt is not None:
c.drawRightString(x_amt, y, format_amount(it.get("IGST Amount", 0)))
x = col_map.get("total")
if x is not None:
c.drawRightString(x, y, format_amount(it.get("Total Amount", 0)))
y -= row_height
# ---- Draw TOTAL row ----
totals = {
"HSN": "",
"Taxable Value": sum(it.get("Taxable Value", 0) for it in invoice.get("Items", [])),
"CGST Amount": sum(it.get("CGST Amount", 0) for it in invoice.get("Items", [])),
"SGST Amount": sum(it.get("SGST Amount", 0) for it in invoice.get("Items", [])),
"IGST Amount": sum(it.get("IGST Amount", 0) for it in invoice.get("Items", [])),
"Total Amount": sum(it.get("Total Amount", 0) for it in invoice.get("Items", [])),
}
y -= row_height # extra gap before TOTAL row
c.setFont("Helvetica", 9)
x = col_map.get("taxable")
if x is not None:
c.drawRightString(x, y - 150, format_amount(totals["Taxable Value"]))
x = col_map.get("cgst")
if x is not None:
c.drawRightString(x, y - 150, format_amount(totals["CGST Amount"]))
x = col_map.get("sgst")
if x is not None:
c.drawRightString(x, y - 150, format_amount(totals["SGST Amount"]))
x = col_map.get("igst")
if x is not None:
c.drawRightString(x, y - 150, format_amount(totals["IGST Amount"]))
x = col_map.get("total")
if x is not None:
c.drawRightString(x, y - 150, format_amount(totals["Total Amount"]))
# ✅ add "In Words" fields
if "Total Invoice Words" in positions:
total_amt = invoice.get("Total Amount", 0)
x, y, fontname, fontsize, align = positions["Total Invoice Words"]
c.setFont(fontname, fontsize)
c.drawString(x, y, to_words(total_amt))
if "CGST Words" in positions:
cgst_amt = sum(it.get("CGST Amount", 0) for it in invoice.get("Items", []))
x, y, fontname, fontsize, align = positions["CGST Words"]
c.setFont(fontname, fontsize)
c.drawString(x, y, to_words(cgst_amt))
if "SGST Words" in positions:
sgst_amt = sum(it.get("SGST Amount", 0) for it in invoice.get("Items", []))
x, y, fontname, fontsize, align = positions["SGST Words"]
c.setFont(fontname, fontsize)
c.drawString(x, y, to_words(sgst_amt))
if "IGST Words" in positions:
igst_amt = sum(it.get("IGST Amount", 0) for it in invoice.get("Items", []))
x, y, fontname, fontsize, align = positions["IGST Words"]
c.setFont(fontname, fontsize)
c.drawString(x, y, to_words(igst_amt))
# ✅ Always show Rounding Off = 0
if "Rounding Off" in positions:
x, y, fontname, fontsize, align = positions["Rounding Off"]
c.setFont(fontname, fontsize)
c.drawRightString(x, y, "0")
c.save()
packet.seek(0)
return packet
def merge_overlay_with_template(template_bytes: bytes, overlay_bytes: bytes) -> bytes:
template_reader = PdfReader(io.BytesIO(template_bytes))
overlay_reader = PdfReader(io.BytesIO(overlay_bytes))
writer = PdfWriter()
template_page = template_reader.pages[0]
template_page.merge_page(overlay_reader.pages[0])
writer.add_page(template_page)
out = io.BytesIO()
writer.write(out)
out.seek(0)
return out.read()
# -------------------------
# Streamlit UI
# -------------------------
st.set_page_config(page_title="Tax Invoice Generator", layout="wide")
# ====== UI: Styles & Header ======
st.markdown(
"""
<style>
:root {
--brand: #2457f5;
--brand-2: #0ea5e9;
--text: #0f172a;
--muted: #475569;
--bg-soft: #f8fafc;
--card-border: #e2e8f0;
}
.app-header {
background: linear-gradient(90deg, rgba(36,87,245,.08), rgba(14,165,233,.08));
border: 1px solid var(--card-border);
border-radius: 12px;
padding: 18px 20px;
margin-bottom: 10px;
}
.app-title { margin: 0; font-size: 28px; color: var(--text); }
.app-sub { margin: 6px 0 0; color: var(--muted); }
.section-title { font-size: 16px; margin: 10px 0 6px; color: var(--text); }
.tip { color: var(--muted); font-size: 14px; }
/* Uploader */
[data-testid="stFileUploadDropzone"] {
border-radius: 12px;
border: 1px dashed var(--card-border);
background: var(--bg-soft);
}
/* Buttons */
div.stButton > button {
border-radius: 10px;
border: 1px solid var(--card-border);
background: linear-gradient(180deg, white, #f3f6fb);
color: var(--text);
padding: 10px 14px;
}
div.stButton > button:hover { border-color: #cbd5e1; background: #eef2f8; }
/* Download buttons */
[data-testid="stDownloadButton"] > button {
border-radius: 10px; padding: 10px 14px; font-weight: 600;
background: linear-gradient(90deg, var(--brand), var(--brand-2));
border: none; color: white;
}
/* Containers & spacing */
.block-container { padding-top: 1rem; }
</style>
<div class="app-header">
<h1 class="app-title">Tax Invoice Generator</h1>
<p class="app-sub">Professional invoices for Inephos & Srestham from your Excel data</p>
</div>
""",
unsafe_allow_html=True,
)
with st.expander("How it works", expanded=True):
st.markdown(
"- Upload a single-sheet Excel file with your invoice data.\n"
"- Choose the company template and click Generate.\n"
"- Download a ZIP with professionally formatted PDFs."
)
# ✅ Load templates internally
with open("uploads/inephos_template.pdf", "rb") as f:
template_inephos_bytes = f.read()
with open("uploads/srestham_template.pdf", "rb") as f:
template_srestham_bytes = f.read()
# --- Layout: Upload in center, buttons angled outward ---
st.markdown("<div class='section-title'>1) Upload your data</div>", unsafe_allow_html=True)
col1, col2, col3 = st.columns([1,2,1])
with col2:
data_file = st.file_uploader("Upload customer Excel (.xlsx/.xlsm)", type=["xlsx", "xlsm"])
st.caption("Tip: Keep column headers simple like 'invoice no', 'taxable value', 'cgst %', etc.")
# Preview uploaded file (non-destructive; resets pointer afterwards)
if data_file is not None:
try:
preview_df = pd.read_excel(data_file, engine="openpyxl")
st.markdown("**Preview**")
st.dataframe(preview_df.head(5), width='stretch')
detected_cols = ", ".join([str(c).strip().lower() for c in preview_df.columns])
st.caption(f"Detected columns: {detected_cols}")
except Exception as e:
st.warning(f"Could not preview file: {e}")
finally:
try:
data_file.seek(0)
except Exception:
pass
# --- Buttons appear like branches ---
st.markdown("<div class='section-title'>2) Generate invoices</div>", unsafe_allow_html=True)
col1, col2, col3, col4 = st.columns([1.5,1,1,1.5])
# -------------------------
# Invoice generation helpers
# -------------------------
DEFAULT_POSITIONS = {
"Invoice No": (120, 645, "Helvetica", 8, "left"),
"Invoice Date": (368, 645, "Helvetica", 8, "left"),
"Customer Name": (55, 615, "Helvetica", 10, "left"),
"Customer Address": (55, 600, "Helvetica", 9, "left"),
# New fields:
"GSTIN": (362, 621, "Helvetica", 8, "left"),
"PAN": (362, 607, "Helvetica", 8, "left"),
"CIN": (362, 594, "Helvetica", 8, "left"),
"PO No": (362, 582, "Helvetica", 8, "left"),
"State": (362, 569, "Helvetica", 8, "left"),
"items_start_y": 500,
"items_row_height": 30,
"items_columns": {
"sr": 56,
"desc": 75,
"hsn": 225,
"taxable": 298,
"cgst_perc": 311,
"cgst": 345,
"sgst_perc": 357,
"sgst": 396,
"igst_perc": 411,
"igst": 447,
"total": 505
},
"Total Amount": (506, 265, "Helvetica", 9, "right"),
"Total Invoice Words": (228, 251, "Helvetica", 9, "left"),
"CGST Words": (228, 236, "Helvetica", 9, "left"),
"SGST Words": (228, 222, "Helvetica", 9, "left"),
"IGST Words": (228, 208, "Helvetica", 9, "left"),
# ✅ Position for Rounding Off
"Rounding Off": (506, 277, "Helvetica", 9, "right"),
}
def build_invoice_dict(r, group=None, inv_no=None):
"""Helper to build invoice dict with new fields — always sum Total Amount from items"""
if group is not None:
total_amount = 0
for _, row in group.iterrows():
taxable = row.get("taxable value", 0) or 0
cgst_amt = taxable * (row.get("cgst %", 0) or 0) / 100
sgst_amt = taxable * (row.get("sgst %", 0) or 0) / 100
igst_amt = taxable * (row.get("igst %", 0) or 0) / 100
total_amount += taxable + cgst_amt + sgst_amt + igst_amt
else:
taxable = r.get("taxable value", 0) or 0
cgst_amt = taxable * (r.get("cgst %", 0) or 0) / 100
sgst_amt = taxable * (r.get("sgst %", 0) or 0) / 100
igst_amt = taxable * (r.get("igst %", 0) or 0) / 100
total_amount = taxable + cgst_amt + sgst_amt + igst_amt
return {
"Invoice No": safe_str(inv_no if inv_no else r.get("invoice no", "")),
"Invoice Date": safe_str(r.get("invoice date","")),
"Customer Name": safe_str(r.get("customer name","")),
"Customer Address": safe_str(r.get("customer address","")),
"GSTIN": safe_str(r.get("gstin","")),
"PAN": safe_str(r.get("pan","")),
"CIN": safe_str(r.get("cin","")),
"PO No": safe_str(r.get("po no","")),
"State": safe_str(r.get("state","")),
"Total Amount": total_amount,
}
def build_items(group_or_row):
"""Build item rows; calculate tax amounts from % columns"""
taxable_val = group_or_row.get("taxable value", 0) or 0
# --- Get tax % ---
cgst_perc = group_or_row.get("cgst %", 0) or 0
sgst_perc = group_or_row.get("sgst %", 0) or 0
igst_perc = group_or_row.get("igst %", 0) or 0
# --- Calculate tax amounts ---
cgst_amt = taxable_val * cgst_perc / 100
sgst_amt = taxable_val * sgst_perc / 100
igst_amt = taxable_val * igst_perc / 100
# --- Calculate total ---
total_amt = taxable_val + cgst_amt + sgst_amt + igst_amt
return {
"Service Details": safe_str(group_or_row.get("service details","")),
"HSN": safe_str(group_or_row.get("hsn","")),
"Taxable Value": taxable_val,
"CGST %": cgst_perc,
"SGST %": sgst_perc,
"IGST %": igst_perc,
"CGST Amount": cgst_amt,
"SGST Amount": sgst_amt,
"IGST Amount": igst_amt,
"Total Amount": total_amt,
}
# -------------------------
# Invoice generation buttons (angled outward)
# -------------------------
with col2:
if st.button("🏢 Generate Inephos Invoices"):
if not data_file:
st.error("Please upload the customer Excel.")
else:
with st.spinner("Generating Inephos PDF invoices…"):
df = pd.read_excel(data_file, engine="openpyxl")
df.columns = [str(c).strip().lower() for c in df.columns]
invoices = []
if "invoice no" in df.columns:
grouped = df.groupby("invoice no")
for inv_no, group in grouped:
first = group.iloc[0]
items = [build_items(r) for _, r in group.iterrows()]
inv = build_invoice_dict(first, group, inv_no)
inv["Items"] = items
invoices.append(inv)
else:
for i, r in df.iterrows():
inv = build_invoice_dict(r)
inv["Items"] = [build_items(r)]
invoices.append(inv)
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
for inv in invoices:
overlay = create_overlay_pdf(inv, DEFAULT_POSITIONS, logo_path=None)
final_pdf_bytes = merge_overlay_with_template(template_inephos_bytes, overlay.read())
filename = f"{inv.get('Invoice No')}_inephos.pdf"
zf.writestr(filename, final_pdf_bytes)
zip_buffer.seek(0)
st.success(f"✅ Generated {len(invoices)} invoices for Inephos.")
st.download_button("📥 Download ZIP — Inephos", data=zip_buffer, file_name="inephos_invoices.zip")
with col3:
if st.button("🏢 Generate Srestham Invoices"):
if not data_file:
st.error("Please upload the customer Excel.")
else:
with st.spinner("Generating Srestham PDF invoices…"):
df = pd.read_excel(data_file, engine="openpyxl")
df.columns = [str(c).strip().lower() for c in df.columns]
invoices = []
if "invoice no" in df.columns:
grouped = df.groupby("invoice no")
for inv_no, group in grouped:
first = group.iloc[0]
items = [build_items(r) for _, r in group.iterrows()]
inv = build_invoice_dict(first, group, inv_no)
inv["Items"] = items
invoices.append(inv)
else:
for i, r in df.iterrows():
inv = build_invoice_dict(r)
inv["Items"] = [build_items(r)]
invoices.append(inv)
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
for inv in invoices:
overlay = create_overlay_pdf(inv, DEFAULT_POSITIONS, logo_path=None)
final_pdf_bytes = merge_overlay_with_template(template_srestham_bytes, overlay.read())
filename = f"{inv.get('Invoice No')}_srestham.pdf"
zf.writestr(filename, final_pdf_bytes)
zip_buffer.seek(0)
st.success(f"✅ Generated {len(invoices)} invoices for Srestham.")
st.download_button("📥 Download ZIP — Srestham", data=zip_buffer, file_name="srestham_invoices.zip")
# ====== Footer ======
st.divider()
st.caption("Built for reliable invoice creation.")
|