Fix/supp ag fb (#1023)
* fix: prevent credit notes from entering payment flow * fix: persist and display customer personal numbers * feat: configure automatic invoice reminder days * fix: issue credit notes through send flow * chore: add repository agent guidance * feat(mcp): route tools across user companies * fix(articles): delete unused register entries * feat(invoices): improve issued invoice actions * feat(supplier-invoices): retain uploaded source documents * docs: record implementation decisions * feat: enhance customer personal number handling and validation - Updated CustomerForm to allow personal numbers in the format of "********-1234" for individual customers. - Added validation to ensure personal numbers are only accepted for individual customers in CreateCustomerSchema. - Implemented masking and encryption for personal numbers to enhance data protection. - Introduced new utility functions for masking and encrypting personal numbers. - Added database migration to enforce unique constraints on credit note relationships and prevent duplicate entries. - Enhanced error handling and logging for credit note issuance and invoice processing. - Updated tests to cover new credit note creation guards and personal number handling. * test: enhance list companies test with supabase query mocks
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
-- Credit notes are adjustments, not customer receivables. They must not enter
|
||||
-- the ordinary customer-payment lifecycle.
|
||||
--
|
||||
-- NOT VALID avoids scanning or rewriting existing rows during deployment. The
|
||||
-- constraint still applies to every new row and every future update, so all
|
||||
-- API, RPC, extension, and legacy write paths share the same invariant.
|
||||
DO $migration$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM pg_constraint
|
||||
WHERE conname = 'invoices_credit_note_not_paid'
|
||||
AND conrelid = 'public.invoices'::regclass
|
||||
) THEN
|
||||
ALTER TABLE public.invoices
|
||||
ADD CONSTRAINT invoices_credit_note_not_paid
|
||||
CHECK (
|
||||
credited_invoice_id IS NULL
|
||||
OR status NOT IN ('paid', 'partially_paid')
|
||||
) NOT VALID;
|
||||
END IF;
|
||||
END
|
||||
$migration$;
|
||||
|
||||
COMMENT ON CONSTRAINT invoices_credit_note_not_paid ON public.invoices IS
|
||||
'Credit notes cannot use the ordinary customer-invoice payment states.';
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,6 @@
|
||||
-- Validate separately from ADD CONSTRAINT so the table scan does not run while
|
||||
-- the stronger ALTER TABLE lock from the first migration is still held.
|
||||
ALTER TABLE public.invoices
|
||||
VALIDATE CONSTRAINT invoices_credit_note_not_paid;
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,26 @@
|
||||
-- Per-company schedule for the three automatic customer invoice reminders.
|
||||
-- Defaults preserve the existing 15, 30, and 45 days overdue behavior.
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ADD COLUMN IF NOT EXISTS reminder_days_level_1 SMALLINT NOT NULL DEFAULT 15,
|
||||
ADD COLUMN IF NOT EXISTS reminder_days_level_2 SMALLINT NOT NULL DEFAULT 30,
|
||||
ADD COLUMN IF NOT EXISTS reminder_days_level_3 SMALLINT NOT NULL DEFAULT 45;
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ADD CONSTRAINT company_settings_reminder_days_check
|
||||
CHECK (
|
||||
reminder_days_level_1 BETWEEN 1 AND 365
|
||||
AND reminder_days_level_2 BETWEEN 1 AND 365
|
||||
AND reminder_days_level_3 BETWEEN 1 AND 365
|
||||
AND reminder_days_level_1 < reminder_days_level_2
|
||||
AND reminder_days_level_2 < reminder_days_level_3
|
||||
) NOT VALID;
|
||||
|
||||
COMMENT ON COLUMN public.company_settings.reminder_days_level_1
|
||||
IS 'Days overdue before the first automatic customer invoice reminder.';
|
||||
COMMENT ON COLUMN public.company_settings.reminder_days_level_2
|
||||
IS 'Days overdue before the second automatic customer invoice reminder.';
|
||||
COMMENT ON COLUMN public.company_settings.reminder_days_level_3
|
||||
IS 'Days overdue before the final automatic customer invoice reminder.';
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,4 @@
|
||||
ALTER TABLE public.company_settings
|
||||
VALIDATE CONSTRAINT company_settings_reminder_days_check;
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
@@ -0,0 +1,20 @@
|
||||
-- Keep partially-created credit notes invisible and prevent duplicate credit
|
||||
-- relationships or posted vouchers under concurrent requests.
|
||||
|
||||
ALTER TABLE public.invoices
|
||||
ADD COLUMN IF NOT EXISTS creation_complete boolean NOT NULL DEFAULT true;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_invoices_company_credited_invoice
|
||||
ON public.invoices (company_id, credited_invoice_id)
|
||||
WHERE credited_invoice_id IS NOT NULL;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_posted_credit_note_journal_source
|
||||
ON public.journal_entries (company_id, source_id)
|
||||
WHERE source_type = 'credit_note'
|
||||
AND source_id IS NOT NULL
|
||||
AND status = 'posted';
|
||||
|
||||
COMMENT ON COLUMN public.invoices.creation_complete IS
|
||||
'False only while a credit-note parent and its items are being assembled. API reads must expose only complete credit notes.';
|
||||
|
||||
NOTIFY pgrst, 'reload schema';
|
||||
Reference in New Issue
Block a user