fix: pending/paid invoices

This commit is contained in:
Sergei Poljanski 2026-06-08 19:00:03 +03:00
commit 37a491cf79
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
4 changed files with 35 additions and 9 deletions

View file

@ -27,6 +27,17 @@ class Invoice < Sequel::Model(:invoices)
paid? ? 'paid' : 'pending'
end
# Both status variants are pre-rendered at creation; `pdf_key` is the base
# path (no suffix). The download route picks a variant by current status.
def pdf_key_for(status)
base = pdf_key.sub(/\.pdf\z/, '')
"#{base}-#{status}.pdf"
end
def current_pdf_key
pdf_key_for(status)
end
def total
BigDecimal(subtotal.to_s)
end

View file

@ -31,12 +31,15 @@ class InvoicePdf
COLOR_RULE = 'DADADA'.freeze
COLOR_ACCENT = '1A1A1A'.freeze
def self.render(invoice)
new(invoice).render
# `status` overrides the status shown in the PDF (e.g. to pre-render a "paid"
# copy before any payment date exists). Defaults to the invoice's live status.
def self.render(invoice, status: nil)
new(invoice, status: status).render
end
def initialize(invoice)
def initialize(invoice, status: nil)
@invoice = invoice
@status = status || invoice.status
end
def render
@ -155,7 +158,7 @@ class InvoicePdf
['ISSUED', @invoice.issued_on.strftime('%Y-%m-%d')],
['DUE', @invoice.due_on.strftime('%Y-%m-%d')],
['CURRENCY', @invoice.currency],
['STATUS', @invoice.status.upcase]
['STATUS', @status.upcase]
]
col_w = pdf.bounds.width / cells.size.to_f
top = pdf.cursor

View file

@ -40,6 +40,13 @@ module S3
key
end
def exists?(key)
client.head_object(bucket: bucket, key: key)
true
rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
false
end
def presigned_url(key, expires_in: 300, filename: nil)
params = { bucket: bucket, key: key }
if filename