Skip to content

Close invoicing gaps: correctness, credit notes, dunning, payments, and API - #876

Merged
evilguy4000 merged 9 commits into
developfrom
feat/close-invoicing-gaps
Nov 14, 2026
Merged

evilguy4000 merged 9 commits into
developfrom
feat/close-invoicing-gaps

Conversation

@evilguy4000

Copy link
Copy Markdown
Collaborator

Summary

  • Harden invoice correctness: separate lifecycle vs payment status, lock financial edits on sent invoices, fix overdue selection (including the dashboard PARTIALLY_PAID crash), and keep amount_paid driven by the payment ledger with explicit overpayment handling.
  • Add commercial depth: per-line tax/discounts/ordering, end-to-end credit notes (PDF + Peppol UBL 381 / CII 381 / XRechnung / FatturaPA TD04), and multi-level reminder policies with late fees.
  • Finish collection and integrations: Mollie webhooks, PayPal signature verification, provider refunds, receipt emails, client-portal PDF download, API send-email/XML exports, and lifecycle webhook events (invoice.sent/paid/updated/deleted/overdue, credit_note.created, payment.refunded).

Migrations

  • 210_invoice_line_tax_discounts
  • 211_invoice_reminder_policies
  • 212_credit_notes_extended

Test plan

  • Open dashboard — overdue invoice stats load without InvoiceStatus.PARTIALLY_PAID error
  • Create draft invoice with mixed line tax rates + discounts; totals and PDF/UBL/FatturaPA look correct
  • Send invoice, confirm financial fields are locked; notes/due date still editable
  • Create full and amount credit notes from invoice view; download PDF and UBL 381 / FatturaPA TD04
  • Configure a reminder policy with two levels + late fee; run/trigger reminder job for an overdue unpaid invoice
  • Record overpayment — warning shown and status overpaid
  • Stripe/PayPal/Mollie checkout paths; refund a completed payment; portal invoice PDF download
  • API: POST /api/v1/invoices/<id>/send-email, GET .../ubl|xrechnung|fatturapa; webhook delivery for invoice.paid / invoice.sent
  • pytest tests/test_credit_notes.py tests/test_invoice_line_tax_discounts.py tests/test_invoice_reminders.py tests/test_phase5_features.py tests/test_invoice_events.py tests/test_api_invoices_v1.py -q

Separate lifecycle status from payment settlement, stop marking drafts overdue,
recompute paid amounts from the payment ledger, and treat overpayments explicitly
so the dashboard and payment flows stay consistent.
Support mixed VAT rates and allowances on invoice lines, keep a tax breakdown
for Peppol/CII/FatturaPA/XRechnung exports, and lock financial edits on
non-draft invoices while still allowing notes and due-date updates.
Introduce reusable reminder levels with optional fixed or annual late fees,
wire the daily overdue job to send each due level once, and expose admin and
per-client policy controls plus invoice reminder history.
Extend credit notes with line items and numbering, add create/issue/PDF UI,
and export Peppol UBL 381, CII 381, XRechnung, and FatturaPA TD04 documents
linked back to the original invoice.
Add Mollie webhooks, verify PayPal signatures, implement provider refunds,
send payment receipt emails, and let clients download invoice PDFs from the
portal with a provider-neutral checkout page.
Expose send-email plus UBL/XRechnung/FatturaPA downloads on API v1, emit
invoice and credit-note webhook events consistently, and document the new
scopes and payload surface.
return redirect(url_for("credit_notes.view_credit_note", cn_id=cn.id))
except Exception as exc:
db.session.rollback()
logger.exception("Failed to create credit note for invoice %s", invoice_id)
from app.utils.credit_note_pdf import generate_credit_note_pdf
pdf_bytes = generate_credit_note_pdf(cn)
except Exception as exc:
logger.exception("Credit note PDF generation failed for cn=%s", cn_id)
)
xml_str, _ = build_peppol_ubl_credit_note_xml(cn, invoice, supplier, customer)
except Exception as exc:
logger.exception("UBL credit note XML build failed for cn=%s", cn_id)
client = Client.query.get(invoice.client_id) if invoice.client_id else None
xml_bytes, filename = build_fatturapa_credit_note_xml(cn, invoice, settings, client)
except Exception as exc:
logger.exception("FatturaPA credit note XML build failed for cn=%s", cn_id)
class _Concrete(PaymentProvider):
provider_name = "dummy"

def create_checkout_session(self, **kw):
Comment on lines +8 to +20
from flask import (
Blueprint,
Response,
abort,
current_app,
flash,
jsonify,
redirect,
render_template,
request,
send_file,
url_for,
)
Comment thread app/models/payments.py Fixed
"""PayPal Orders v2 payment provider."""

import base64
import hashlib
"""Models for invoice reminder policies and policy levels."""

from datetime import datetime
from decimal import Decimal
Comment thread tests/test_invoice_reminders.py Fixed
Comment thread tests/test_invoice_reminders.py Fixed
@@ -0,0 +1,448 @@
"""Phase 5 tests: Mollie, PayPal signature, refunds, portal PDF, pay.html, receipt email."""

import io
Comment thread tests/test_phase5_features.py Fixed
Format with black/isort, add the reminder-policy edit script nonce, align
position/late_fee server defaults, and make reminder-policy FK adds SQLite-safe
with batch_alter_table plus the policy_id index.
Comment thread app/models/payments.py
@@ -1,3 +1,4 @@
from datetime import date as _date
from factories import ClientFactory, ProjectFactory, UserFactory

from app import db
from app.models import Client, Invoice, InvoiceItem, Project, Settings, User

from __future__ import annotations

from datetime import date, datetime
from datetime import date, datetime
from decimal import Decimal

import pytest
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest
Sort reminder_policy imports in models and factories-before-app in the
overdue scheduled invoice report test so the isort pre-commit hook passes.
Split the suite across eight jobs using pytest-split least_duration, cache
merged timings between runs, drop verbose coverage reports on shards, and
raise the per-job timeout to 30 minutes as a safety margin.
@evilguy4000
evilguy4000 merged commit fa17956 into develop Nov 14, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants