-- Template migration for each organization schema.
-- Replace {schema} with the real schema name, e.g. company_<organization_id>.

alter table {schema}.outgoing_invoices
    add column if not exists source_quote_id uuid references {schema}.quotes(id);

alter table {schema}.outgoing_invoices
    add column if not exists customer_discount_percent numeric(7, 4) not null default 0;

alter table {schema}.outgoing_invoices
    drop constraint if exists outgoing_invoices_customer_discount_valid;

alter table {schema}.outgoing_invoices
    add constraint outgoing_invoices_customer_discount_valid check (
        customer_discount_percent >= 0 and customer_discount_percent <= 100
    );

create index if not exists idx_outgoing_invoices_source_quote
    on {schema}.outgoing_invoices (source_quote_id);
