CREATE TABLE animal_surgeries ( id INTEGER PRIMARY KEY AUTOINCREMENT, animal_id INTEGER NOT NULL REFERENCES animals(id) ON DELETE CASCADE, category TEXT NOT NULL, procedure_name TEXT NOT NULL, surgery_date TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'completed' CHECK(status IN ('planned','completed','cancelled')), clinic_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL, veterinarian_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL, tariff_id INTEGER REFERENCES clinic_tariffs(id) ON DELETE SET NULL, amount_cents INTEGER CHECK(amount_cents IS NULL OR amount_cents>=0), anesthesia TEXT, notes TEXT, follow_up TEXT, created_by INTEGER REFERENCES users(id) ON DELETE SET NULL, created_at TEXT NOT NULL DEFAULT(datetime('now')), updated_at TEXT NOT NULL DEFAULT(datetime('now')) ); CREATE INDEX idx_animal_surgeries_animal_date ON animal_surgeries(animal_id,surgery_date); CREATE TABLE accounting_invoices ( id INTEGER PRIMARY KEY AUTOINCREMENT, vendor_contact_id INTEGER REFERENCES directory_contacts(id) ON DELETE SET NULL, reference TEXT NOT NULL COLLATE NOCASE, invoice_date TEXT NOT NULL, due_date TEXT, document_type TEXT NOT NULL DEFAULT 'invoice' CHECK(document_type IN ('invoice','credit','adjustment')), status TEXT NOT NULL DEFAULT 'to_pay' CHECK(status IN ('to_pay','partial','paid','disputed','cancelled','credit')), payment_method TEXT CHECK(payment_method IS NULL OR payment_method IN ('transfer','card','check','cash','direct_debit','other')), paid_on TEXT, total_ht_cents INTEGER, total_ttc_cents INTEGER NOT NULL, amount_due_cents INTEGER, notes TEXT, source TEXT, original_filename TEXT, stored_filename TEXT, mime_type TEXT, file_size INTEGER, created_by INTEGER REFERENCES users(id) ON DELETE SET NULL, created_at TEXT NOT NULL DEFAULT(datetime('now')), updated_at TEXT NOT NULL DEFAULT(datetime('now')), UNIQUE(vendor_contact_id,reference) ); CREATE INDEX idx_accounting_invoice_date ON accounting_invoices(invoice_date,status); CREATE INDEX idx_accounting_invoice_vendor ON accounting_invoices(vendor_contact_id,invoice_date); ALTER TABLE role_permissions RENAME TO role_permissions_legacy; CREATE TABLE role_permissions ( role TEXT NOT NULL CHECK(role IN ('admin','responsable','benevole','lecture')), module TEXT NOT NULL CHECK(module IN ('dashboard','animals','medical','care','directory','agenda','statistics','administrative','accounting','grants')), can_view INTEGER NOT NULL DEFAULT 0 CHECK(can_view IN (0,1)), can_edit INTEGER NOT NULL DEFAULT 0 CHECK(can_edit IN (0,1)), updated_by INTEGER REFERENCES users(id) ON DELETE SET NULL, updated_at TEXT NOT NULL DEFAULT(datetime('now')), PRIMARY KEY(role,module) ); INSERT INTO role_permissions SELECT * FROM role_permissions_legacy; DROP TABLE role_permissions_legacy; INSERT INTO role_permissions(role,module,can_view,can_edit) VALUES ('admin','accounting',1,1),('responsable','accounting',1,1),('benevole','accounting',0,0),('lecture','accounting',0,0);