changes
This commit is contained in:
@@ -468,14 +468,12 @@ def send_accreditation_email(
|
||||
software_name: str = "Invario Inventarsystem",
|
||||
invoice_path: str = None
|
||||
) -> bool:
|
||||
"""Generiert Hauptvertrag und Rechnung temporär als PDF und versendet sie."""
|
||||
"""Generiert Hauptvertrag und Rechnung, speichert die Rechnung dauerhaft und versendet die E-Mail."""
|
||||
|
||||
safe_school_name = "".join([c for c in school_name if c.isalnum() or c in (' ', '_', '-')]).rstrip()
|
||||
|
||||
# Dynamische Rechnungsnummer generieren (z.B. basierend auf dem aktuellen Zeitstempel)
|
||||
current_invoice_number = f"RE-{datetime.now().strftime('%Y%m%d-%H%M')}"
|
||||
|
||||
# 1. Hauptvertrag dynamisch erzeugen
|
||||
# 1. Hauptvertrag dynamisch erzeugen (bleibt temporär)
|
||||
contract_filename = f"Hauptvertrag_Invario_{safe_school_name}.pdf"
|
||||
contract_pdf_path = os.path.join(tempfile.gettempdir(), contract_filename)
|
||||
|
||||
@@ -488,24 +486,35 @@ def send_accreditation_email(
|
||||
output_path=contract_pdf_path
|
||||
)
|
||||
|
||||
# 1b. Rechnung dynamisch mit FPDF erzeugen inkl. SELLER_INFO
|
||||
generated_invoice_path = None
|
||||
if not invoice_path:
|
||||
invoice_filename = f"Rechnung_Invario_{safe_school_name}.pdf"
|
||||
generated_invoice_path = os.path.join(tempfile.gettempdir(), invoice_filename)
|
||||
# 2. Ordner 'static/invoices' ermitteln & sicherstellen, dass er existiert
|
||||
if current_app:
|
||||
static_dir = current_app.static_folder
|
||||
else:
|
||||
static_dir = os.path.join(os.getcwd(), "static")
|
||||
|
||||
invoices_dir = os.path.join(static_dir, "invoices")
|
||||
os.makedirs(invoices_dir, exist_ok=True) # Erstellt den Ordner 'invoices', falls nicht vorhanden
|
||||
|
||||
# 3. Dauerhafte Rechnung im static-Ordner erzeugen
|
||||
invoice_filename = f"Rechnung_{current_invoice_number}_{safe_school_name}.pdf"
|
||||
full_invoice_path = os.path.join(invoices_dir, invoice_filename)
|
||||
relative_invoice_path = f"invoices/{invoice_filename}" # Für den Datenbank-Eintrag
|
||||
|
||||
if not invoice_path:
|
||||
generate_invoice_fpdf(
|
||||
school_name=school_name,
|
||||
address=address,
|
||||
price=price,
|
||||
date=date,
|
||||
software_name=software_name,
|
||||
output_path=generated_invoice_path,
|
||||
output_path=full_invoice_path,
|
||||
invoice_number=current_invoice_number
|
||||
)
|
||||
invoice_to_attach = generated_invoice_path
|
||||
invoice_to_attach = full_invoice_path
|
||||
db_pdf_path = relative_invoice_path
|
||||
else:
|
||||
invoice_to_attach = invoice_path
|
||||
db_pdf_path = relative_invoice_path
|
||||
|
||||
if current_app:
|
||||
static_dir = current_app.static_folder
|
||||
@@ -636,8 +645,8 @@ def send_accreditation_email(
|
||||
def _utc_now_iso():
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# Berechne das Fälligkeitsdatum (14 Tage ab heute)
|
||||
computed_due_date = (datetime.now(timezone.utc) + timedelta(days=14)).isoformat()
|
||||
# Berechne das Fälligkeitsdatum (30 Tage ab heute)
|
||||
computed_due_date = (datetime.now(timezone.utc) + timedelta(days=14)).strftime("%d %B %Y")
|
||||
|
||||
# Collections abrufen
|
||||
req_client, req_col = _get_collection("instance_requests")
|
||||
@@ -655,7 +664,7 @@ def send_accreditation_email(
|
||||
"amount_eur": price,
|
||||
"status": "Zu prüfen",
|
||||
"due_date": computed_due_date,
|
||||
"pdf_path": invoice_to_attach,
|
||||
"pdf_path": db_pdf_path,
|
||||
"created_at": _utc_now_iso(),
|
||||
"source": "booking_payment",
|
||||
"software_name": software_name,
|
||||
@@ -671,7 +680,7 @@ def send_accreditation_email(
|
||||
}}
|
||||
)
|
||||
|
||||
# 5. E-Mail versenden & temporäre Dateien aufräumen
|
||||
# 5. E-Mail versenden & NUR temporäre Dateien löschen
|
||||
try:
|
||||
success = send(recipient, subject, text_body=text_note, html_body=html_note, attachments=attachments)
|
||||
return success
|
||||
@@ -681,9 +690,3 @@ def send_accreditation_email(
|
||||
os.remove(contract_pdf_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
if generated_invoice_path and os.path.exists(generated_invoice_path):
|
||||
try:
|
||||
os.remove(generated_invoice_path)
|
||||
except OSError:
|
||||
pass
|
||||
Reference in New Issue
Block a user