fix(invoice): resolve duplicate invoice_no integrity collision on new orders
This commit is contained in:
parent
4ad1c6ef3f
commit
427d266bfb
3 changed files with 44 additions and 16 deletions
|
|
@ -139,18 +139,11 @@ def generate_invoice_for_order(
|
|||
igst = 0.0
|
||||
|
||||
# Allocate a 16-char GST invoice number
|
||||
# Format: C1P2-26-SEQ
|
||||
# Format: C1P1-26-SEQ
|
||||
today = datetime.now()
|
||||
fy = today.strftime("%y") # financial year seq, e.g. 26
|
||||
last_inv = db.query(Invoice).order_by(Invoice.created_at.desc()).first()
|
||||
if not last_inv:
|
||||
seq = 1
|
||||
else:
|
||||
try:
|
||||
seq = int(last_inv.invoice_no.split("-")[-1]) + 1
|
||||
except Exception:
|
||||
seq = 1
|
||||
invoice_no = f"C1P1-{fy}-{seq:06d}"
|
||||
from app.core.invoice_generator import get_next_invoice_number
|
||||
invoice_no = get_next_invoice_number(db, prefix="C1P1")
|
||||
|
||||
invoice = Invoice(
|
||||
invoice_id=str(ulid.ULID()),
|
||||
|
|
|
|||
|
|
@ -272,12 +272,8 @@ def get_order_invoice_pdf(
|
|||
|
||||
today = datetime.now()
|
||||
fy = today.strftime("%y")
|
||||
last_inv = db.query(Invoice).order_by(Invoice.created_at.desc()).first()
|
||||
try:
|
||||
seq = int(last_inv.invoice_no.split("-")[-1]) + 1 if last_inv else 1
|
||||
except Exception:
|
||||
seq = 1
|
||||
invoice_no = f"C1P1-{fy}-{seq:06d}"
|
||||
from app.core.invoice_generator import get_next_invoice_number
|
||||
invoice_no = get_next_invoice_number(db, prefix="C1P1")
|
||||
|
||||
invoice = Invoice(
|
||||
invoice_id=str(ulid.ULID()),
|
||||
|
|
|
|||
|
|
@ -654,3 +654,42 @@ def generate_service_invoice_pdf(job, customer, payments, db=None) -> bytes:
|
|||
|
||||
doc.build(story)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def get_next_invoice_number(db, prefix: str = "C1P1") -> str:
|
||||
"""
|
||||
Safely allocate the next monotonically increasing unique invoice number.
|
||||
Extracts the highest numeric sequence from existing invoices matching prefix-YY-,
|
||||
and verifies non-collision before returning.
|
||||
"""
|
||||
from sqlalchemy import func
|
||||
from app.models.InvoiceModel import Invoice
|
||||
|
||||
today = datetime.now()
|
||||
fy = today.strftime("%y")
|
||||
prefix_pattern = f"{prefix}-{fy}-"
|
||||
|
||||
candidates = db.query(Invoice.invoice_no).filter(
|
||||
Invoice.invoice_no.like(f"{prefix_pattern}%")
|
||||
).order_by(
|
||||
func.length(Invoice.invoice_no).desc(),
|
||||
Invoice.invoice_no.desc()
|
||||
).limit(50).all()
|
||||
|
||||
max_seq = 0
|
||||
for (inv_no,) in candidates:
|
||||
try:
|
||||
val = int(inv_no.split("-")[-1])
|
||||
if val > max_seq:
|
||||
max_seq = val
|
||||
except (ValueError, IndexError):
|
||||
continue
|
||||
|
||||
next_seq = max_seq + 1
|
||||
while db.query(Invoice.invoice_id).filter(
|
||||
Invoice.invoice_no == f"{prefix_pattern}{next_seq:06d}"
|
||||
).first() is not None:
|
||||
next_seq += 1
|
||||
|
||||
return f"{prefix_pattern}{next_seq:06d}"
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue