Quotes
quotesQuotes with sections, optional lines, discounts, prices with or without tax, tax per rate and a deposit; published versions; a public link or the signed-in portal the customer accepts, rejects or asks changes through; expiry; and quotes from deals. With catalog, a line that names a product variant takes its price and title.
CRM & salesmanagedcustom
Pipeline
Lead24
Qualified12
Proposal7
Won4
Install
The command also installs updated-at, memberships, permissions, subjects, the modules it needs.
npx better-supabase add quotesConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by quotes; add installs them too.
"updated-at": {},
memberships: {},
permissions: {},
subjects: {},
// Modes: "managed", "custom". The default is "managed".
quotes: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add quotes writes into your declarative schema with the default layout.11 files
supabase/schemas/900_better_supabase_097_quotes.sqlschema2028 lines-- better-supabase module: quotes
-- @bsb-block quotes@3 managed
-- @bsb-hash sha256:b8681ab69f0f312ff964db1fba8c7301f29ce33b0400ade893c2bd2e335bae50
-- Quotes with sections, optional lines, discounts, prices with or without tax, tax per rate and a deposit; published versions; a public link or the signed-in portal the customer accepts, rejects or asks changes through; expiry; and quotes from deals. With catalog, a line that names a product variant takes its price and title.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
-- Quotes: line items, sections, optional lines, discounts and tax per rate,
-- published versions, sending with a public link or to the signed-in portal
-- the customer accepts, rejects or asks changes through, and expiry.
create table "better_supabase"."quotes" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"number" bigint not null,
"identifier" text not null,
"revision" integer not null default 1,
"title" text not null constraint bsb_quotes_title_check check (length("title") >= 1 and length("title") <= 300),
"company_type" text,
"company_id" text,
"contact_type" text,
"contact_id" text,
"owner_id" uuid,
"currency" char(3) not null constraint bsb_quotes_currency_check check ("currency" ~ '^[A-Z]{3}$'),
"status" text not null default 'draft' constraint bsb_quotes_status_check check ("status" in ('draft', 'sent', 'viewed', 'accepted', 'rejected', 'changes_requested', 'expired', 'withdrawn')),
"valid_until" date,
"terms" text,
"note" text,
"origin_type" text,
"origin_id" text,
"token_hash" text unique,
"sent_at" timestamptz,
"viewed_at" timestamptz,
"decided_at" timestamptz,
"decided_by" text,
"decision_note" text,
"decider_id" uuid,
"decided_ip" text,
"decided_user_agent" text,
"prices_include_tax" boolean not null default false,
"discount_bps" integer not null default 0 constraint bsb_quotes_discount_bps_check check ("discount_bps" >= 0 and "discount_bps" <= 10000),
"discount_amount" bigint not null default 0 constraint bsb_quotes_discount_amount_check check ("discount_amount" >= 0),
"deposit_bps" integer constraint bsb_quotes_deposit_bps_check check ("deposit_bps" > 0 and "deposit_bps" < 10000),
"deposit_amount" bigint constraint bsb_quotes_deposit_amount_check check ("deposit_amount" > 0),
"deposit_required" boolean not null default false,
"deposit_due" bigint not null default 0,
"unpublished_changes" boolean not null default false,
"buyer" jsonb,
"variant_of" uuid,
"variant_label" text constraint bsb_quotes_variant_label_check check (length("variant_label") >= 1 and length("variant_label") <= 100),
"is_selected" boolean not null default false,
"created_by" uuid,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
"list_total" bigint not null default 0,
"discount_total" bigint not null default 0,
"subtotal" bigint not null default 0,
"tax_total" bigint not null default 0,
"total" bigint not null default 0,
unique ("tenant_id", "number", "revision"),
constraint bsb_quotes_check check (("company_type" is null) = ("company_id" is null)),
constraint bsb_quotes_check1 check (("origin_type" is null) = ("origin_id" is null))
);
create index quotes_tenant_status_idx on "better_supabase"."quotes" ("tenant_id", "status", "created_at" desc);
create index quotes_variant_idx on "better_supabase"."quotes" ("variant_of") where "variant_of" is not null;
create index quotes_origin_idx on "better_supabase"."quotes" ("origin_type", "origin_id");
alter table "better_supabase"."quotes" enable row level security;
revoke all on "better_supabase"."quotes" from public, anon, authenticated;
grant select on "better_supabase"."quotes" to authenticated;
grant all on "better_supabase"."quotes" to service_role;
create policy bsb_quotes_read on "better_supabase"."quotes"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('quotes.read')));
create table "better_supabase"."quote_versions" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"quote_id" uuid not null references "better_supabase"."quotes" ("id") on delete cascade,
"revision" integer not null,
"snapshot" jsonb not null,
"total" bigint not null,
"currency" char(3) not null,
"published_by" uuid,
"published_at" timestamptz not null default now(),
unique ("quote_id", "revision")
);
alter table "better_supabase"."quote_versions" enable row level security;
revoke all on "better_supabase"."quote_versions" from public, anon, authenticated;
grant select on "better_supabase"."quote_versions" to authenticated;
grant all on "better_supabase"."quote_versions" to service_role;
create policy bsb_quote_versions_read on "better_supabase"."quote_versions"
for select to authenticated
using (exists (select 1 from "better_supabase"."quotes" q where q."id" = "quote_id"));
create or replace function better_supabase.bsb_quote_versions_frozen()
returns trigger
language plpgsql
set search_path = ''
as $$
begin
raise exception 'a published quote version never changes' using errcode = '55000', hint = 'QUOTE_VERSION_FROZEN';
end;
$$;
revoke execute on function better_supabase.bsb_quote_versions_frozen() from public, anon, authenticated;
create trigger "bsb_quote_versions_frozen" before update on "better_supabase"."quote_versions"
for each row execute function better_supabase.bsb_quote_versions_frozen();
create table "better_supabase"."quote_tax_lines" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"parent_id" uuid not null references "better_supabase"."quotes" ("id") on delete cascade,
"tax_rate_bps" integer not null,
"tax_code" text,
"net" bigint not null,
"tax" bigint not null,
"gross" bigint not null,
"discount" bigint not null default 0
);
create index quote_tax_lines_parent_idx on "better_supabase"."quote_tax_lines" ("parent_id");
alter table "better_supabase"."quote_tax_lines" enable row level security;
revoke all on "better_supabase"."quote_tax_lines" from public, anon, authenticated;
grant select on "better_supabase"."quote_tax_lines" to authenticated;
grant all on "better_supabase"."quote_tax_lines" to service_role;
create policy bsb_quote_tax_lines_read on "better_supabase"."quote_tax_lines"
for select to authenticated
using (exists (select 1 from "better_supabase"."quotes" q where q."id" = "parent_id"));
create or replace function "better_supabase"."quote_client_guard"()
returns trigger
language plpgsql
security invoker
set search_path = ''
as $$
begin
if tg_op = 'INSERT' and not (better_supabase.import_run() is not null) and not (auth.role() = 'service_role') then
new."token_hash" := null;
new."decided_at" := null;
new."decided_by" := null;
new."decision_note" := null;
new."decider_id" := null;
new."decided_ip" := null;
new."decided_user_agent" := null;
end if;
if new."variant_of" is not null and (tg_op = 'INSERT' or new."variant_of" is distinct from old."variant_of")
and not exists (select 1 from "better_supabase"."quotes" s where s."id" = new."variant_of" and s."tenant_id" = new."tenant_id") then
raise exception 'a variant belongs to a quote of the same organization' using errcode = '22023', hint = 'QUOTE_VARIANT_TENANT';
end if;
return new;
end;
$$;
revoke execute on function "better_supabase"."quote_client_guard"() from public, anon, authenticated;
create trigger "bsb_quote_client_guard" before insert or update of "variant_of" on "better_supabase"."quotes"
for each row execute function "better_supabase"."quote_client_guard"();
-- Creates a draft quote. attrs: title (required), currency, company and
-- contact ({ type, id }), ownerId, validUntil, terms, note, origin ({ type,
-- id }, the record it was made from), pricesIncludeTax, discountBps,
-- discountAmount (minor units as text), depositBps, or depositAmount (minor
-- units as text) for a fixed deposit, and depositRequired, which makes the
-- customer pay it before they accept. lines: see set_quote_lines.
create or replace function "better_supabase"."create_quote"(tenant uuid, attrs jsonb, lines jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_seq bigint;
v_identifier text;
v_row "better_supabase"."quotes";
v_company_type text := coalesce(create_quote.attrs -> 'company' ->> 'type', 'company');
v_company_id text := create_quote.attrs -> 'company' ->> 'id';
v_contact_type text := coalesce(create_quote.attrs -> 'contact' ->> 'type', 'person');
v_contact_id text := create_quote.attrs -> 'contact' ->> 'id';
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', create_quote.tenant, 'quotes.write'), false)) then
raise exception 'not allowed to change quotes' using errcode = '42501', hint = 'QUOTE_FORBIDDEN';
end if;
if nullif(trim(create_quote.attrs ->> 'title'), '') is null then
raise exception 'a quote needs a title' using errcode = '23502', hint = 'QUOTE_ATTRS_INVALID';
end if;
if v_company_id is not null and not false then
raise exception 'the company is unknown or not readable in this organization' using errcode = '42501', hint = 'QUOTE_COMPANY_FORBIDDEN';
end if;
if v_contact_id is not null and not false then
raise exception 'the contact is unknown or not readable in this organization' using errcode = '42501', hint = 'QUOTE_CONTACT_FORBIDDEN';
end if;
if (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'quote' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end) is not null then
v_identifier := (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'quote' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end);
if better_supabase.import_run() is null then
raise exception 'a fixed number is only accepted during an import' using errcode = '42501', hint = 'NUMBER_FIXED_FORBIDDEN';
end if;
v_seq := coalesce(substring(v_identifier from '([0-9]+)$')::bigint, 0);
perform set_config('better_supabase.fixed_number', '', true);
else
perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(create_quote.tenant::text || ':quote', 0));
select coalesce(max(x."number"), 0) + 1 into v_seq from "better_supabase"."quotes" x where x."tenant_id" = create_quote.tenant;
v_identifier := 'Q-' || v_seq::text;
end if;
begin
insert into "better_supabase"."quotes" ("tenant_id", "number", "identifier", "title", "company_type", "company_id", "contact_type", "contact_id",
"owner_id", "currency", "valid_until", "terms", "note", "origin_type", "origin_id", "prices_include_tax", "discount_bps", "discount_amount", "deposit_bps", "deposit_amount", "deposit_required", "created_by")
values (create_quote.tenant, v_seq, v_identifier, trim(create_quote.attrs ->> 'title'),
case when v_company_id is null then null else v_company_type end, v_company_id,
case when v_contact_id is null then null else v_contact_type end, v_contact_id,
coalesce((create_quote.attrs ->> 'ownerId')::uuid, auth.uid()), upper(coalesce(create_quote.attrs ->> 'currency', 'EUR')),
coalesce((create_quote.attrs ->> 'validUntil')::date, current_date + 30),
create_quote.attrs ->> 'terms', create_quote.attrs ->> 'note',
create_quote.attrs -> 'origin' ->> 'type', create_quote.attrs -> 'origin' ->> 'id',
coalesce((create_quote.attrs ->> 'pricesIncludeTax')::boolean, false), coalesce((create_quote.attrs ->> 'discountBps')::integer, 0), coalesce((create_quote.attrs ->> 'discountAmount')::bigint, 0), (create_quote.attrs ->> 'depositBps')::integer, (create_quote.attrs ->> 'depositAmount')::bigint, coalesce((create_quote.attrs ->> 'depositRequired')::boolean, false), auth.uid())
returning * into v_row;
exception when check_violation or invalid_text_representation or datetime_field_overflow or numeric_value_out_of_range then
raise exception 'a quote field has an invalid value' using errcode = '22023', hint = 'QUOTE_ATTRS_INVALID';
end;
if v_row."deposit_bps" is not null and v_row."deposit_amount" is not null then
raise exception 'a deposit is a share (depositBps) or an amount (depositAmount), not both' using errcode = '22023', hint = 'QUOTE_ATTRS_INVALID';
end if;
if create_quote.lines is not null then
perform "better_supabase"."set_quote_lines"(v_row."id", create_quote.lines);
end if;
if v_row."origin_id" is not null then
end if;
null;
return (select (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) from "better_supabase"."quotes" x where x."id" = v_row."id");
end;
$$;
revoke execute on function "better_supabase"."create_quote"(uuid, jsonb, jsonb) from public, anon;
grant execute on function "better_supabase"."create_quote"(uuid, jsonb, jsonb) to authenticated, service_role;
-- Changes a draft quote, or one the customer asked changes on: title,
-- company, contact, currency, ownerId, validUntil, terms, note,
-- pricesIncludeTax, discountBps, discountAmount, depositBps, depositAmount
-- and depositRequired. The totals follow the new pricing.
create or replace function "better_supabase"."update_quote"(quote uuid, attrs jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = update_quote.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false));
if v_row."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if not (v_row."status" in ('draft', 'changes_requested')) then
raise exception 'only a draft quote, or one with changes requested, can change' using errcode = '55000', hint = 'QUOTE_NOT_EDITABLE';
end if;
if (update_quote.attrs -> 'company' ->> 'id') is not null and not false then
raise exception 'the company is unknown or not readable in this organization' using errcode = '42501', hint = 'QUOTE_COMPANY_FORBIDDEN';
end if;
if (update_quote.attrs -> 'contact' ->> 'id') is not null and not false then
raise exception 'the contact is unknown or not readable in this organization' using errcode = '42501', hint = 'QUOTE_CONTACT_FORBIDDEN';
end if;
begin
update "better_supabase"."quotes" x set
"title" = coalesce(nullif(trim(update_quote.attrs ->> 'title'), ''), x."title"),
"currency" = coalesce(upper(update_quote.attrs ->> 'currency'), x."currency"),
"owner_id" = case when update_quote.attrs ? 'ownerId' then (update_quote.attrs ->> 'ownerId')::uuid else x."owner_id" end,
"valid_until" = case when update_quote.attrs ? 'validUntil' then (update_quote.attrs ->> 'validUntil')::date else x."valid_until" end,
"terms" = case when update_quote.attrs ? 'terms' then update_quote.attrs ->> 'terms' else x."terms" end,
"note" = case when update_quote.attrs ? 'note' then update_quote.attrs ->> 'note' else x."note" end,
"prices_include_tax" = case when update_quote.attrs ? 'pricesIncludeTax' then coalesce((update_quote.attrs ->> 'pricesIncludeTax')::boolean, false) else x."prices_include_tax" end,
"discount_bps" = case when update_quote.attrs ? 'discountBps' then coalesce((update_quote.attrs ->> 'discountBps')::integer, 0) else x."discount_bps" end,
"discount_amount" = case when update_quote.attrs ? 'discountAmount' then coalesce((update_quote.attrs ->> 'discountAmount')::bigint, 0) else x."discount_amount" end,
"deposit_bps" = case when update_quote.attrs ? 'depositBps' then (update_quote.attrs ->> 'depositBps')::integer else x."deposit_bps" end,
"deposit_amount" = case when update_quote.attrs ? 'depositAmount' then (update_quote.attrs ->> 'depositAmount')::bigint else x."deposit_amount" end,
"deposit_required" = case when update_quote.attrs ? 'depositRequired' then coalesce((update_quote.attrs ->> 'depositRequired')::boolean, false) else x."deposit_required" end,
"company_type" = case when update_quote.attrs ? 'company' then case when update_quote.attrs -> 'company' ->> 'id' is null then null else coalesce(update_quote.attrs -> 'company' ->> 'type', 'company') end else x."company_type" end,
"company_id" = case when update_quote.attrs ? 'company' then update_quote.attrs -> 'company' ->> 'id' else x."company_id" end,
"contact_type" = case when update_quote.attrs ? 'contact' then case when update_quote.attrs -> 'contact' ->> 'id' is null then null else coalesce(update_quote.attrs -> 'contact' ->> 'type', 'person') end else x."contact_type" end,
"contact_id" = case when update_quote.attrs ? 'contact' then update_quote.attrs -> 'contact' ->> 'id' else x."contact_id" end,
"updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
exception when check_violation or invalid_text_representation or datetime_field_overflow or numeric_value_out_of_range then
raise exception 'a quote field has an invalid value' using errcode = '22023', hint = 'QUOTE_ATTRS_INVALID';
end;
if v_row."deposit_bps" is not null and v_row."deposit_amount" is not null then
raise exception 'a deposit is a share (depositBps) or an amount (depositAmount), not both' using errcode = '22023', hint = 'QUOTE_ATTRS_INVALID';
end if;
perform "better_supabase"."set_quote_lines_recount"(v_row."id");
select * into v_row from "better_supabase"."quotes" x where x."id" = v_row."id";
null;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke execute on function "better_supabase"."update_quote"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."update_quote"(uuid, jsonb) to authenticated, service_role;
create table "better_supabase"."quote_lines" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"parent_id" uuid not null references "better_supabase"."quotes" ("id") on delete cascade,
"product_type" text,
"product_id" text,
"description" text not null constraint bsb_quote_lines_description_check check (length("description") >= 1 and length("description") <= 2000),
"quantity" numeric(18, 4) not null constraint bsb_quote_lines_quantity_check check ("quantity" > 0),
"unit_amount" bigint not null,
"discount_bps" integer not null default 0 constraint bsb_quote_lines_discount_bps_check check ("discount_bps" >= 0 and "discount_bps" <= 10000),
"tax_rate_bps" integer not null default 0 constraint bsb_quote_lines_tax_rate_bps_check check ("tax_rate_bps" >= 0 and "tax_rate_bps" <= 100000),
"tax_code" text,
"tax_code_id" uuid,
"unit_cost" bigint,
"discount_amount" bigint not null default 0 constraint bsb_quote_lines_discount_amount_check check ("discount_amount" >= 0),
"cost_source" text constraint bsb_quote_lines_cost_source_check check ("cost_source" in ('catalog', 'average', 'manual', 'imported')),
"location_id" uuid,
"bin_id" uuid,
"billing_party" text not null default 'customer' constraint bsb_quote_lines_billing_party_check check ("billing_party" in ('customer', 'internal', 'own_warranty', 'supplier_warranty', 'cost_account')),
"billing_party_id" text,
"billing_account_id" uuid,
"section" text constraint bsb_quote_lines_section_check check (length("section") >= 1 and length("section") <= 300),
"optional" boolean not null default false,
"selected" boolean not null default true,
"periods" integer default 1 constraint bsb_quote_lines_periods_check check ("periods" > 0),
"position" integer not null,
"created_at" timestamptz not null default now(),
constraint bsb_quote_lines_check check (("product_type" is null) = ("product_id" is null))
);
alter table "better_supabase"."quote_lines" add column "unit" text constraint "bsb_quote_lines_unit" check (length("unit") >= 1 and length("unit") <= 40);
alter table "better_supabase"."quote_lines" add column "discount_kind" text constraint "bsb_quote_lines_discount_kind" check ("discount_kind" in ('percentage', 'fixed'));
create index "quote_lines_parent_idx" on "better_supabase"."quote_lines" ("parent_id", "position");
alter table "better_supabase"."quote_lines" enable row level security;
revoke all on "better_supabase"."quote_lines" from public, anon, authenticated;
grant select on "better_supabase"."quote_lines" to authenticated;
grant all on "better_supabase"."quote_lines" to service_role;
create policy bsb_quote_lines_read on "better_supabase"."quote_lines" for select to authenticated
using (exists (select 1 from "better_supabase"."quotes" x where x."id" = "parent_id"));
-- Writes a record's totals from its counted lines: an optional line counts
-- only when the customer took it. The record discount is spread over the tax
-- rates in proportion to their amounts, and tax is counted once per rate, so
-- the totals and the tax per rate always add up.
create or replace function "better_supabase"."set_quote_lines_recount"(parent uuid)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
v_inclusive boolean;
v_record_bps integer;
v_record_amount bigint;
v_restriction integer;
v_list bigint;
v_line_discount bigint;
v_net bigint;
v_record_discount bigint;
v_groups jsonb;
v_discount bigint;
v_subtotal bigint;
v_tax bigint;
v_total bigint;
begin
select x."tenant_id", x."prices_include_tax", x."discount_bps", x."discount_amount", 0
into v_tenant, v_inclusive, v_record_bps, v_record_amount, v_restriction
from "better_supabase"."quotes" x where x."id" = set_quote_lines_recount.parent;
select coalesce(sum(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount")), 0), coalesce(sum((round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0) + least(coalesce(l."discount_amount", 0), greatest(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0), 0)))), 0)
into v_list, v_line_discount
from "better_supabase"."quote_lines" l where l."parent_id" = set_quote_lines_recount.parent and (not l."optional" or l."selected");
v_net := v_list - v_line_discount;
v_record_discount := case
when v_net > 0 then least(v_net, round(v_net * coalesce(v_record_bps, 0) / 10000.0)::bigint + coalesce(v_record_amount, 0))
else 0
end;
with grouped as (
select l."tax_rate_bps" as rate, l."tax_code" as code, sum(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - (round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0) + least(coalesce(l."discount_amount", 0), greatest(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0), 0)))) as amount,
sum(round((round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - (round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0) + least(coalesce(l."discount_amount", 0), greatest(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0), 0)))) * (10000 - v_restriction) * l."tax_rate_bps" / (10000.0 * (case when v_inclusive then 10000.0 + l."tax_rate_bps" else 10000.0 end)))) as tax
from "better_supabase"."quote_lines" l where l."parent_id" = set_quote_lines_recount.parent and (not l."optional" or l."selected")
group by 1, 2
), shared as (
select g.rate, g.code, g.amount, g.tax,
case when v_net > 0
then round(v_record_discount * sum(g.amount) over w / v_net) - round(v_record_discount * (sum(g.amount) over w - g.amount) / v_net)
else 0
end as share
from grouped g
window w as (order by g.rate, g.code nulls first rows between unbounded preceding and current row)
), taxed as (
select s.rate, s.code, s.share, s.amount - s.share as amount,
s.tax - round(s.share * (10000 - v_restriction) * s.rate / (10000.0 * (case when v_inclusive then 10000.0 + s.rate else 10000.0 end))) as tax
from shared s
)
select coalesce(jsonb_agg(jsonb_build_object(
'rate', x.rate,
'code', x.code,
'net', case when v_inclusive then x.amount - x.tax else x.amount end,
'tax', x.tax,
'discount', x.share,
'gross', case when v_inclusive then x.amount else x.amount + x.tax end
) order by x.rate, x.code nulls first), '[]'::jsonb)
into v_groups
from taxed x;
select coalesce(sum(r.net), 0), coalesce(sum(r.tax), 0), coalesce(sum(r.gross), 0)
into v_subtotal, v_tax, v_total
from jsonb_to_recordset(v_groups) r(net bigint, tax bigint, gross bigint);
v_discount := v_line_discount + v_record_discount;
delete from "better_supabase"."quote_tax_lines" x where x."parent_id" = set_quote_lines_recount.parent;
insert into "better_supabase"."quote_tax_lines" ("tenant_id", "parent_id", "tax_rate_bps", "tax_code", "net", "tax", "gross", "discount")
select v_tenant, set_quote_lines_recount.parent, r.rate, r.code, r.net, r.tax, r.gross, r.discount
from jsonb_to_recordset(v_groups) r(rate integer, code text, net bigint, tax bigint, gross bigint, discount bigint);
update "better_supabase"."quotes" x set
"list_total" = v_list,
"discount_total" = v_discount,
"subtotal" = v_subtotal,
"tax_total" = v_tax,
"total" = v_total,
"deposit_due" = case when x."deposit_amount" is not null then least(greatest(v_total, 0), x."deposit_amount") when x."deposit_bps" is not null then round(v_total * x."deposit_bps" / 10000.0)::bigint else 0 end
where x."id" = set_quote_lines_recount.parent;
end;
$$;
revoke execute on function "better_supabase"."set_quote_lines_recount"(uuid) from public, anon, authenticated;
-- Recounts each record a statement touched once, in id order so concurrent
-- statements lock the records in the same order. A transition table needs a
-- trigger per event, so there are three triggers on one function. The
-- transition tables are read through execute because plpgsql_check (db lint)
-- doesn't know them.
create or replace function better_supabase.bsb_quote_lines_totals()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
v_parent uuid;
begin
for v_parent in execute case tg_op
when 'INSERT' then 'select distinct n."parent_id" from new_rows n order by 1'
when 'DELETE' then 'select distinct o."parent_id" from old_rows o order by 1'
else 'select parent from (
select n."parent_id" as parent from new_rows n
join old_rows o on o."id" = n."id"
where n."quantity" is distinct from o."quantity"
or n."unit_amount" is distinct from o."unit_amount"
or n."selected" is distinct from o."selected"
or n."optional" is distinct from o."optional"
union
select n."parent_id" from new_rows n
where not exists (select 1 from old_rows o where o."id" = n."id")
union
select o."parent_id" from old_rows o
where not exists (select 1 from new_rows n where n."id" = o."id")
) changed order by 1'
end loop
perform "better_supabase"."set_quote_lines_recount"(v_parent);
end loop;
return null;
end;
$$;
revoke execute on function better_supabase.bsb_quote_lines_totals() from public, anon, authenticated;
create trigger bsb_quote_lines_totals_insert after insert on "better_supabase"."quote_lines"
referencing new table as new_rows
for each statement execute function better_supabase.bsb_quote_lines_totals();
create trigger bsb_quote_lines_totals_update after update on "better_supabase"."quote_lines"
referencing old table as old_rows new table as new_rows
for each statement execute function better_supabase.bsb_quote_lines_totals();
create trigger bsb_quote_lines_totals_delete after delete on "better_supabase"."quote_lines"
referencing old table as old_rows
for each statement execute function better_supabase.bsb_quote_lines_totals();
-- Replaces a record's lines with lines: a list of { description, quantity,
-- unitAmount (minor units as text), currency, discountBps, taxRateBps,
-- taxCode, product: { type, id } }. A line's currency must be the record's.
-- Returns the record's totals as text, with its currency.
create or replace function "better_supabase"."set_quote_lines"(parent uuid, lines jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_parent uuid := set_quote_lines.parent;
v_tenant uuid;
v_currency text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."tenant_id", x."currency" into v_tenant, v_currency from "better_supabase"."quotes" x where x."id" = v_parent and not (not (x."status" in ('draft', 'changes_requested')));
if v_tenant is null then
raise exception 'no such record, or its lines are locked' using errcode = 'P0002', hint = 'LINES_RECORD_INVALID';
end if;
if not exists (select 1 from "better_supabase"."quotes" x where x."id" = v_parent and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false))) then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if jsonb_typeof(coalesce(set_quote_lines.lines, '[]')) <> 'array' then
raise exception 'lines must be a list' using errcode = '22023', hint = 'LINES_INVALID';
end if;
if exists (select 1 from jsonb_array_elements(set_quote_lines.lines) x where x.value ? 'currency' and x.value ->> 'currency' is distinct from v_currency) then
raise exception 'every line must be in the record''s currency' using errcode = '22023', hint = 'LINES_CURRENCY';
end if;
delete from "better_supabase"."quote_lines" l where l."parent_id" = v_parent;
begin
insert into "better_supabase"."quote_lines" ("tenant_id", "parent_id", "product_type", "product_id", "description", "quantity", "unit_amount", "discount_bps", "tax_rate_bps", "tax_code", "tax_code_id", "unit_cost", "discount_amount", "cost_source", "location_id", "bin_id", "billing_party", "billing_party_id", "billing_account_id", "section", "optional", "selected", "periods", "unit", "discount_kind", "position")
select v_tenant,
v_parent,
x.value -> 'product' ->> 'type',
x.value -> 'product' ->> 'id',
x.value ->> 'description',
coalesce((x.value ->> 'quantity')::numeric, 1),
(x.value ->> 'unitAmount')::bigint,
coalesce((x.value ->> 'discountBps')::integer, 0),
coalesce((x.value ->> 'taxRateBps')::integer, 0),
x.value ->> 'taxCode',
nullif(x.value ->> 'taxCodeId', '')::uuid,
(x.value ->> 'unitCost')::bigint,
coalesce((x.value ->> 'discountAmount')::bigint, 0),
nullif(x.value ->> 'costSource', ''),
nullif(x.value ->> 'locationId', '')::uuid,
nullif(x.value ->> 'binId', '')::uuid,
coalesce(nullif(x.value ->> 'billingParty', ''), 'customer'),
nullif(x.value ->> 'billingPartyId', ''),
nullif(x.value ->> 'billingAccountId', '')::uuid,
nullif(trim(x.value ->> 'section'), ''),
coalesce((x.value ->> 'optional')::boolean, false),
coalesce((x.value ->> 'selected')::boolean, not coalesce((x.value ->> 'optional')::boolean, false)),
case when x.value ? 'periods' then (x.value ->> 'periods')::integer else 1 end,
case when x.value ? 'unit' then nullif(trim(x.value ->> 'unit'), '') else better_supabase.column_default('"better_supabase"."quote_lines"', 'unit', (null::"better_supabase"."quote_lines")."unit") end,
case when x.value ? 'discountKind' then nullif(x.value ->> 'discountKind', '') else better_supabase.column_default('"better_supabase"."quote_lines"', 'discount_kind', (null::"better_supabase"."quote_lines")."discount_kind") end,
x.ordinality::integer
from jsonb_array_elements(coalesce(set_quote_lines.lines, '[]')) with ordinality x;
exception when check_violation or not_null_violation or invalid_text_representation or numeric_value_out_of_range then
raise exception 'every line needs a description, a positive quantity and a unit amount' using errcode = '22023', hint = 'LINES_INVALID';
end;
perform "better_supabase"."set_quote_lines_recount"(v_parent);
return (select jsonb_build_object(
'listTotal', x."list_total"::text, 'discountTotal', x."discount_total"::text,
'subtotal', x."subtotal"::text, 'taxTotal', x."tax_total"::text, 'total', x."total"::text,
'currency', x."currency")
from "better_supabase"."quotes" x where x."id" = v_parent);
end;
$$;
revoke execute on function "better_supabase"."set_quote_lines"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."set_quote_lines"(uuid, jsonb) to authenticated, service_role;
-- Sends a quote: it gets a public link the customer opens and a published
-- version, a snapshot that never changes. The quote can't change until it is
-- revised or the customer asks for changes; sending it again then publishes
-- the next revision. Returns the quote with the link's token, which is stored
-- only as a hash and returned this once.
-- actor: who sent it, for the state machine's history.
create or replace function "better_supabase"."send_quote"(quote uuid, actor jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_token text := encode(extensions.gen_random_bytes(24), 'hex');
v_version uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = send_quote.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false)) for update;
if v_row."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if not (v_row."status" in ('draft', 'changes_requested')) then
raise exception 'the quote was already sent; revise it to send a new version' using errcode = '55000', hint = 'QUOTE_NOT_EDITABLE';
end if;
if not exists (select 1 from "better_supabase"."quote_lines" x where x."parent_id" = v_row."id") then
raise exception 'a quote needs a line before it is sent' using errcode = '23514', hint = 'QUOTE_EMPTY';
end if;
if send_quote.actor is not null then
if jsonb_typeof(send_quote.actor) <> 'object' then
raise exception 'actor must be an object of id, name, email, ip and userAgent' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end if;
begin
perform nullif(send_quote.actor ->> 'ip', '')::inet, nullif(send_quote.actor ->> 'id', '')::uuid;
exception when invalid_text_representation then
raise exception 'actor.ip must be an IP address and actor.id a user id' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end;
end if;
perform set_config('better_supabase.transition_actor', coalesce(send_quote.actor::text, ''), true);
declare
v_hook regprocedure := to_regprocedure('"public"."before_quote_send"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
update "better_supabase"."quotes" x set "status" = 'sent', "sent_at" = now(), "token_hash" = encode(extensions.digest(v_token, 'sha256'), 'hex'),
"revision" = case when exists (select 1 from "better_supabase"."quote_versions" v where v."quote_id" = x."id" and v."revision" = x."revision") then x."revision" + 1 else x."revision" end,
"decided_at" = null, "decided_by" = null, "decision_note" = null, "decider_id" = null, "decided_ip" = null, "decided_user_agent" = null, "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
insert into "better_supabase"."quote_versions" ("tenant_id", "quote_id", "revision", "snapshot", "total", "currency", "published_by")
values (v_row."tenant_id", v_row."id", v_row."revision", (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb)), v_row."total", v_row."currency", coalesce(auth.uid(), nullif(send_quote.actor ->> 'id', '')::uuid))
returning "id" into v_version;
null;
declare
v_hook regprocedure := to_regprocedure('"public"."after_quote_send"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb)) || jsonb_build_object('token', v_token);
end;
$$;
revoke execute on function "better_supabase"."send_quote"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."send_quote"(uuid, jsonb) to authenticated, service_role;
-- Opens a sent or declined quote again as a draft of its next revision: the
-- same quote, number and lines, a fresh valid-until date, and its link ends.
-- The versions already published stay as they were. An accepted quote is
-- kept as the customer accepted it; start a new quote instead.
create or replace function "better_supabase"."revise_quote"(quote uuid, actor jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = revise_quote.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false)) for update;
if v_row."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if v_row."status" = 'draft' then
raise exception 'a draft is edited, not revised' using errcode = '55000', hint = 'QUOTE_NOT_SENT';
end if;
if v_row."status" = 'accepted' then
raise exception 'an accepted quote stays as accepted; start a new quote' using errcode = '55000', hint = 'QUOTE_ACCEPTED';
end if;
if revise_quote.actor is not null then
if jsonb_typeof(revise_quote.actor) <> 'object' then
raise exception 'actor must be an object of id, name, email, ip and userAgent' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end if;
begin
perform nullif(revise_quote.actor ->> 'ip', '')::inet, nullif(revise_quote.actor ->> 'id', '')::uuid;
exception when invalid_text_representation then
raise exception 'actor.ip must be an IP address and actor.id a user id' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end;
end if;
perform set_config('better_supabase.transition_actor', coalesce(revise_quote.actor::text, ''), true);
update "better_supabase"."quotes" x set "status" = 'draft', "revision" = x."revision" + 1, "token_hash" = null, "sent_at" = null, "viewed_at" = null,
"valid_until" = greatest(x."valid_until", current_date + 30),
"decided_at" = null, "decided_by" = null, "decision_note" = null, "decider_id" = null, "decided_ip" = null, "decided_user_agent" = null, "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
null;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke execute on function "better_supabase"."revise_quote"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."revise_quote"(uuid, jsonb) to authenticated, service_role;
-- Takes back a sent or expired quote the customer hasn't accepted: it can't
-- be answered any more until it is revised and sent again. The actor and
-- reason go to the state machine's history.
create or replace function "better_supabase"."withdraw_quote"(quote uuid, actor jsonb default null, reason text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = withdraw_quote.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false)) for update;
if v_row."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if v_row."status" not in ('sent', 'viewed', 'changes_requested', 'expired') then
raise exception 'only a sent quote that wasn''t answered is withdrawn' using errcode = '55000', hint = 'QUOTE_NOT_OPEN';
end if;
if withdraw_quote.actor is not null then
if jsonb_typeof(withdraw_quote.actor) <> 'object' then
raise exception 'actor must be an object of id, name, email, ip and userAgent' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end if;
begin
perform nullif(withdraw_quote.actor ->> 'ip', '')::inet, nullif(withdraw_quote.actor ->> 'id', '')::uuid;
exception when invalid_text_representation then
raise exception 'actor.ip must be an IP address and actor.id a user id' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end;
end if;
perform set_config('better_supabase.transition_actor', coalesce(withdraw_quote.actor::text, ''), true);
perform set_config('better_supabase.transition_reason', coalesce(nullif(trim(withdraw_quote.reason), ''), ''), true);
update "better_supabase"."quotes" x set "status" = 'withdrawn', "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
perform set_config('better_supabase.transition_reason', '', true);
null;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke execute on function "better_supabase"."withdraw_quote"(uuid, jsonb, text) from public, anon;
grant execute on function "better_supabase"."withdraw_quote"(uuid, jsonb, text) to authenticated, service_role;
-- Records the customer's answer taken another way (by phone, by email):
-- decision is accepted, rejected or changes_requested; by is their name,
-- and without it or an actor the answer names no one. The note is also the
-- reason in the state machine's history.
-- selected lists the positions of the optional lines they took. actor is
-- the customer when the app takes the answer in its own portal: id, name,
-- email, ip and userAgent, kept with the answer and in the state machine's
-- history; a quote past its valid date then expires instead, and the expired
-- quote is returned.
create or replace function "better_supabase"."decide_quote"(quote uuid, decision text, note text, by text, selected jsonb default null, actor jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_claims text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = decide_quote.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false)) for update;
if v_row."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if decide_quote.decision not in ('accepted', 'rejected', 'changes_requested') then
raise exception 'answer accepted, rejected or changes_requested' using errcode = '22023', hint = 'QUOTE_DECISION_INVALID';
end if;
if v_row."status" not in ('sent', 'viewed') then
raise exception 'this quote no longer takes an answer' using errcode = '55000', hint = 'QUOTE_NOT_OPEN';
end if;
if decide_quote.actor is not null then
if jsonb_typeof(decide_quote.actor) <> 'object' then
raise exception 'actor must be an object of id, name, email, ip and userAgent' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end if;
begin
perform nullif(decide_quote.actor ->> 'ip', '')::inet, nullif(decide_quote.actor ->> 'id', '')::uuid;
exception when invalid_text_representation then
raise exception 'actor.ip must be an IP address and actor.id a user id' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end;
end if;
perform set_config('better_supabase.transition_actor', coalesce(decide_quote.actor::text, ''), true);
if (decide_quote.actor is not null) and v_row."valid_until" < current_date then
update "better_supabase"."quotes" x set "status" = 'expired', "updated_at" = now() where x."id" = v_row."id"
returning * into v_row;
null;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end if;
if decide_quote.decision = 'accepted' then
if decide_quote.selected is not null then
if jsonb_typeof(decide_quote.selected) <> 'array' or exists (
select 1 from jsonb_array_elements(decide_quote.selected) e
where jsonb_typeof(e.value) not in ('number') or not exists (
select 1 from "better_supabase"."quote_lines" ql
where ql."parent_id" = v_row."id" and ql."optional" and ql."position" = (e.value #>> '{}')::numeric
)
) then
raise exception 'pick optional lines of this quote by position' using errcode = '22023', hint = 'QUOTE_SELECTION_INVALID';
end if;
update "better_supabase"."quote_lines" ql set "selected" = ql."position" in (select ((e.value #>> '{}')::numeric)::integer from jsonb_array_elements(decide_quote.selected) e)
where ql."parent_id" = v_row."id" and ql."optional";
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_quote_accept"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
end if;
perform set_config('better_supabase.transition_reason', coalesce(nullif(trim(decide_quote.note), ''), ''), true);
update "better_supabase"."quotes" x set "status" = decide_quote.decision, "decided_at" = now(), "decided_by" = nullif(trim(coalesce(nullif(trim(decide_quote.by), ''), nullif(trim(decide_quote.actor ->> 'name'), ''), decide_quote.actor ->> 'email')), ''),
"decision_note" = nullif(trim(decide_quote.note), ''), "decider_id" = coalesce(nullif(decide_quote.actor ->> 'id', '')::uuid, auth.uid()), "decided_ip" = left(decide_quote.actor ->> 'ip', 64), "decided_user_agent" = left(decide_quote.actor ->> 'userAgent', 512), "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
perform set_config('better_supabase.transition_reason', '', true);
case decide_quote.decision
when 'accepted' then
null;
when 'rejected' then
null;
else
null;
end case;
if decide_quote.decision = 'accepted' then
declare
v_hook regprocedure := to_regprocedure('"public"."after_quote_accept"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
end if;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke execute on function "better_supabase"."decide_quote"(uuid, text, text, text, jsonb, jsonb) from public, anon;
grant execute on function "better_supabase"."decide_quote"(uuid, text, text, text, jsonb, jsonb) to authenticated, service_role;
-- What the customer sees through the link: the quote without internal
-- notes. Opening a sent quote marks it viewed. Callable without signing in.
create or replace function "better_supabase"."public_quote"(token text)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_headers jsonb := nullif(current_setting('request.headers', true), '')::jsonb;
v_ip inet;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."token_hash" = encode(extensions.digest(public_quote.token, 'sha256'), 'hex');
if v_row."id" is null then
raise exception 'this link is not valid' using errcode = 'P0002', hint = 'QUOTE_LINK_INVALID';
end if;
begin
v_ip := left(nullif(trim(reverse(split_part(reverse(v_headers ->> 'x-forwarded-for'), ',', 1))), ''), 64)::inet;
exception when invalid_text_representation then
v_ip := null;
end;
perform set_config('better_supabase.transition_actor', jsonb_strip_nulls(jsonb_build_object('email', auth.jwt() ->> 'email', 'ip', v_ip::text, 'userAgent', left(v_headers ->> 'user-agent', 512)))::text, true);
if v_row."status" = 'sent' then
update "better_supabase"."quotes" x set "status" = 'viewed', "viewed_at" = now() where x."id" = v_row."id"
returning * into v_row;
null;
end if;
return jsonb_build_object(
'identifier', v_row."identifier", 'revision', v_row."revision", 'title', v_row."title",
'status', v_row."status", 'currency', v_row."currency", 'validUntil', v_row."valid_until",
'terms', v_row."terms", 'pricesIncludeTax', v_row."prices_include_tax",
'discountBps', v_row."discount_bps", 'discountAmount', v_row."discount_amount"::text,
'depositBps', v_row."deposit_bps", 'depositAmount', v_row."deposit_amount"::text, 'depositRequired', v_row."deposit_required",
'depositDue', v_row."deposit_due"::text,
'listTotal', v_row."list_total"::text, 'subtotal', v_row."subtotal"::text, 'discountTotal', v_row."discount_total"::text,
'taxTotal', v_row."tax_total"::text, 'total', v_row."total"::text,
'lines', (select coalesce(jsonb_agg(e.value - array['taxCodeId', 'unitCost', 'costSource', 'locationId', 'binId', 'billingParty', 'billingPartyId', 'billingAccountId'] order by e.ordinality), '[]'::jsonb) from jsonb_array_elements(coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb)) with ordinality e),
'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke all on function "better_supabase"."public_quote"(text) from public, anon, authenticated;
grant execute on function "better_supabase"."public_quote"(text) to anon, authenticated, service_role;
-- The customer's answer through the link: accepted, rejected or
-- changes_requested, with their name, a note and the optional lines they
-- took. Their address and browser are kept with the answer. A quote past its
-- valid date expires instead.
create or replace function "better_supabase"."respond_to_quote"(token text, decision text, name text, note text, selected jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_headers jsonb := nullif(current_setting('request.headers', true), '')::jsonb;
v_ip inet;
v_claims text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."token_hash" = encode(extensions.digest(respond_to_quote.token, 'sha256'), 'hex') for update;
if v_row."id" is null then
raise exception 'this link is not valid' using errcode = 'P0002', hint = 'QUOTE_LINK_INVALID';
end if;
if respond_to_quote.decision not in ('accepted', 'rejected', 'changes_requested') then
raise exception 'answer accepted, rejected or changes_requested' using errcode = '22023', hint = 'QUOTE_DECISION_INVALID';
end if;
if v_row."status" not in ('sent', 'viewed') then
raise exception 'this quote no longer takes an answer' using errcode = '55000', hint = 'QUOTE_NOT_OPEN';
end if;
begin
v_ip := left(nullif(trim(reverse(split_part(reverse(v_headers ->> 'x-forwarded-for'), ',', 1))), ''), 64)::inet;
exception when invalid_text_representation then
v_ip := null;
end;
perform set_config('better_supabase.transition_actor', jsonb_strip_nulls(jsonb_build_object('name', nullif(trim(respond_to_quote.name), ''), 'email', auth.jwt() ->> 'email', 'ip', v_ip::text, 'userAgent', left(v_headers ->> 'user-agent', 512)))::text, true);
if (true) and v_row."valid_until" < current_date then
update "better_supabase"."quotes" x set "status" = 'expired', "updated_at" = now() where x."id" = v_row."id"
returning * into v_row;
null;
return jsonb_build_object('status', 'expired');
end if;
if nullif(trim(respond_to_quote.name), '') is null then
raise exception 'say who answers' using errcode = '23502', hint = 'QUOTE_NAME_REQUIRED';
end if;
if respond_to_quote.decision = 'accepted' then
if v_row."deposit_required" and v_row."deposit_due" > 0 then
raise exception 'pay the deposit before accepting; the app records the acceptance once it is paid' using errcode = '55000', hint = 'QUOTE_DEPOSIT_REQUIRED';
end if;
if respond_to_quote.selected is not null then
if jsonb_typeof(respond_to_quote.selected) <> 'array' or exists (
select 1 from jsonb_array_elements(respond_to_quote.selected) e
where jsonb_typeof(e.value) not in ('number') or not exists (
select 1 from "better_supabase"."quote_lines" ql
where ql."parent_id" = v_row."id" and ql."optional" and ql."position" = (e.value #>> '{}')::numeric
)
) then
raise exception 'pick optional lines of this quote by position' using errcode = '22023', hint = 'QUOTE_SELECTION_INVALID';
end if;
update "better_supabase"."quote_lines" ql set "selected" = ql."position" in (select ((e.value #>> '{}')::numeric)::integer from jsonb_array_elements(respond_to_quote.selected) e)
where ql."parent_id" = v_row."id" and ql."optional";
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_quote_accept"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
end if;
update "better_supabase"."quotes" x set "status" = respond_to_quote.decision, "decided_at" = now(), "decided_by" = nullif(trim(respond_to_quote.name), ''),
"decision_note" = nullif(trim(respond_to_quote.note), ''), "decider_id" = auth.uid(), "decided_ip" = left(nullif(trim(reverse(split_part(reverse(v_headers ->> 'x-forwarded-for'), ',', 1))), ''), 64), "decided_user_agent" = left(v_headers ->> 'user-agent', 512), "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
case respond_to_quote.decision
when 'accepted' then
null;
when 'rejected' then
null;
else
null;
end case;
if respond_to_quote.decision = 'accepted' then
declare
v_hook regprocedure := to_regprocedure('"public"."after_quote_accept"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
end if;
return jsonb_build_object('status', v_row."status", 'identifier', v_row."identifier", 'revision', v_row."revision");
end;
$$;
revoke all on function "better_supabase"."respond_to_quote"(text, text, text, text, jsonb) from public, anon, authenticated;
grant execute on function "better_supabase"."respond_to_quote"(text, text, text, text, jsonb) to anon, authenticated, service_role;
-- A quote shared with the signed-in guest, as the customer sees it: through
-- a grant on the quote or as a guest of its company or contact. Opening a
-- sent quote marks it viewed. No link token is needed. The service role
-- reads it too, for an app's own portal; actor is then the customer who
-- opened it, for the state machine's history.
create or replace function "better_supabase"."portal_quote"(quote uuid, actor jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_headers jsonb := nullif(current_setting('request.headers', true), '')::jsonb;
v_ip inet;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if portal_quote.actor is not null and not (auth.role() = 'service_role') then
raise exception 'only the service role names who opened a quote' using errcode = '42501', hint = 'QUOTE_ACTOR_FORBIDDEN';
end if;
if portal_quote.actor is not null then
if jsonb_typeof(portal_quote.actor) <> 'object' then
raise exception 'actor must be an object of id, name, email, ip and userAgent' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end if;
begin
perform nullif(portal_quote.actor ->> 'ip', '')::inet, nullif(portal_quote.actor ->> 'id', '')::uuid;
exception when invalid_text_representation then
raise exception 'actor.ip must be an IP address and actor.id a user id' using errcode = '22023', hint = 'QUOTE_ACTOR_INVALID';
end;
end if;
perform set_config('better_supabase.transition_actor', coalesce(portal_quote.actor::text, ''), true);
if portal_quote.actor is null then
begin
v_ip := left(nullif(trim(reverse(split_part(reverse(v_headers ->> 'x-forwarded-for'), ',', 1))), ''), 64)::inet;
exception when invalid_text_representation then
v_ip := null;
end;
perform set_config('better_supabase.transition_actor', jsonb_strip_nulls(jsonb_build_object('email', auth.jwt() ->> 'email', 'ip', v_ip::text, 'userAgent', left(v_headers ->> 'user-agent', 512)))::text, true);
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = portal_quote.quote and (false or ((auth.role() = 'service_role') and x."status" <> 'draft'));
if v_row."id" is null then
raise exception 'no such quote shared with you' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if v_row."status" = 'sent' then
update "better_supabase"."quotes" x set "status" = 'viewed', "viewed_at" = now() where x."id" = v_row."id"
returning * into v_row;
null;
end if;
return jsonb_build_object(
'identifier', v_row."identifier", 'revision', v_row."revision", 'title', v_row."title",
'status', v_row."status", 'currency', v_row."currency", 'validUntil', v_row."valid_until",
'terms', v_row."terms", 'pricesIncludeTax', v_row."prices_include_tax",
'discountBps', v_row."discount_bps", 'discountAmount', v_row."discount_amount"::text,
'depositBps', v_row."deposit_bps", 'depositAmount', v_row."deposit_amount"::text, 'depositRequired', v_row."deposit_required",
'depositDue', v_row."deposit_due"::text,
'listTotal', v_row."list_total"::text, 'subtotal', v_row."subtotal"::text, 'discountTotal', v_row."discount_total"::text,
'taxTotal', v_row."tax_total"::text, 'total', v_row."total"::text,
'lines', (select coalesce(jsonb_agg(e.value - array['taxCodeId', 'unitCost', 'costSource', 'locationId', 'binId', 'billingParty', 'billingPartyId', 'billingAccountId'] order by e.ordinality), '[]'::jsonb) from jsonb_array_elements(coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb)) with ordinality e),
'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke execute on function "better_supabase"."portal_quote"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."portal_quote"(uuid, jsonb) to authenticated, service_role;
-- The signed-in guest's answer to a quote shared with them, like
-- respond_to_quote without a token. name defaults to their email; the
-- answer keeps who they are, their address and their browser.
create or replace function "better_supabase"."respond_to_portal_quote"(quote uuid, decision text, name text, note text, selected jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_headers jsonb := nullif(current_setting('request.headers', true), '')::jsonb;
v_ip inet;
v_claims text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = respond_to_portal_quote.quote and false for update;
if v_row."id" is null then
raise exception 'no such quote shared with you' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if respond_to_portal_quote.decision not in ('accepted', 'rejected', 'changes_requested') then
raise exception 'answer accepted, rejected or changes_requested' using errcode = '22023', hint = 'QUOTE_DECISION_INVALID';
end if;
if v_row."status" not in ('sent', 'viewed') then
raise exception 'this quote no longer takes an answer' using errcode = '55000', hint = 'QUOTE_NOT_OPEN';
end if;
begin
v_ip := left(nullif(trim(reverse(split_part(reverse(v_headers ->> 'x-forwarded-for'), ',', 1))), ''), 64)::inet;
exception when invalid_text_representation then
v_ip := null;
end;
perform set_config('better_supabase.transition_actor', jsonb_strip_nulls(jsonb_build_object('name', nullif(trim(coalesce(nullif(trim(respond_to_portal_quote.name), ''), auth.jwt() ->> 'email')), ''), 'email', auth.jwt() ->> 'email', 'ip', v_ip::text, 'userAgent', left(v_headers ->> 'user-agent', 512)))::text, true);
if (true) and v_row."valid_until" < current_date then
update "better_supabase"."quotes" x set "status" = 'expired', "updated_at" = now() where x."id" = v_row."id"
returning * into v_row;
null;
return jsonb_build_object('status', 'expired');
end if;
if nullif(trim(coalesce(nullif(trim(respond_to_portal_quote.name), ''), auth.jwt() ->> 'email')), '') is null then
raise exception 'say who answers' using errcode = '23502', hint = 'QUOTE_NAME_REQUIRED';
end if;
if respond_to_portal_quote.decision = 'accepted' then
if v_row."deposit_required" and v_row."deposit_due" > 0 then
raise exception 'pay the deposit before accepting; the app records the acceptance once it is paid' using errcode = '55000', hint = 'QUOTE_DEPOSIT_REQUIRED';
end if;
if respond_to_portal_quote.selected is not null then
if jsonb_typeof(respond_to_portal_quote.selected) <> 'array' or exists (
select 1 from jsonb_array_elements(respond_to_portal_quote.selected) e
where jsonb_typeof(e.value) not in ('number') or not exists (
select 1 from "better_supabase"."quote_lines" ql
where ql."parent_id" = v_row."id" and ql."optional" and ql."position" = (e.value #>> '{}')::numeric
)
) then
raise exception 'pick optional lines of this quote by position' using errcode = '22023', hint = 'QUOTE_SELECTION_INVALID';
end if;
update "better_supabase"."quote_lines" ql set "selected" = ql."position" in (select ((e.value #>> '{}')::numeric)::integer from jsonb_array_elements(respond_to_portal_quote.selected) e)
where ql."parent_id" = v_row."id" and ql."optional";
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_quote_accept"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
end if;
update "better_supabase"."quotes" x set "status" = respond_to_portal_quote.decision, "decided_at" = now(), "decided_by" = nullif(trim(coalesce(nullif(trim(respond_to_portal_quote.name), ''), auth.jwt() ->> 'email')), ''),
"decision_note" = nullif(trim(respond_to_portal_quote.note), ''), "decider_id" = auth.uid(), "decided_ip" = left(nullif(trim(reverse(split_part(reverse(v_headers ->> 'x-forwarded-for'), ',', 1))), ''), 64), "decided_user_agent" = left(v_headers ->> 'user-agent', 512), "updated_at" = now()
where x."id" = v_row."id"
returning * into v_row;
case respond_to_portal_quote.decision
when 'accepted' then
null;
when 'rejected' then
null;
else
null;
end case;
if respond_to_portal_quote.decision = 'accepted' then
declare
v_hook regprocedure := to_regprocedure('"public"."after_quote_accept"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_row."id";
end if;
end;
end if;
return jsonb_build_object('status', v_row."status", 'identifier', v_row."identifier", 'revision', v_row."revision");
end;
$$;
revoke execute on function "better_supabase"."respond_to_portal_quote"(uuid, text, text, text, jsonb) from public, anon;
grant execute on function "better_supabase"."respond_to_portal_quote"(uuid, text, text, text, jsonb) to authenticated, service_role;
-- The versions of a quote that were sent, oldest first, without their
-- snapshots.
create or replace function "better_supabase"."quote_versions"(quote uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
begin
select * into v_row from "better_supabase"."quotes" x where x."id" = quote_versions.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.read'), false));
if v_row."id" is null then
raise exception 'no such quote you can read' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
return coalesce((select jsonb_agg(jsonb_build_object('revision', x."revision", 'total', x."total"::text, 'currency', x."currency",
'publishedAt', x."published_at", 'publishedBy', x."published_by") order by x."revision")
from "better_supabase"."quote_versions" x where x."quote_id" = v_row."id"), '[]'::jsonb);
end;
$$;
revoke execute on function "better_supabase"."quote_versions"(uuid) from public, anon;
grant execute on function "better_supabase"."quote_versions"(uuid) to authenticated, service_role;
-- The quote as it was sent at revision: the snapshot published then.
create or replace function "better_supabase"."quote_version"(quote uuid, revision integer)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_version "better_supabase"."quote_versions";
begin
select * into v_row from "better_supabase"."quotes" x where x."id" = quote_version.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.read'), false));
if v_row."id" is null then
raise exception 'no such quote you can read' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
select * into v_version from "better_supabase"."quote_versions" x where x."quote_id" = v_row."id" and x."revision" = quote_version.revision;
if v_version."id" is null then
raise exception 'this quote has no such version' using errcode = 'P0002', hint = 'QUOTE_VERSION_NOT_FOUND';
end if;
return v_version."snapshot" || jsonb_build_object('revision', v_version."revision", 'publishedAt', v_version."published_at");
end;
$$;
revoke execute on function "better_supabase"."quote_version"(uuid, integer) from public, anon;
grant execute on function "better_supabase"."quote_version"(uuid, integer) to authenticated, service_role;
-- Expires sent quotes past their valid date. Run it daily from a job.
create or replace function "better_supabase"."expire_quotes"()
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_count integer := 0;
begin
perform set_config('better_supabase.transition_actor', '', true);
for v_row in
update "better_supabase"."quotes" x set "status" = 'expired', "updated_at" = now()
where x."status" in ('sent', 'viewed') and x."valid_until" < current_date
returning *
loop
null;
v_count := v_count + 1;
end loop;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."expire_quotes"() from public, anon, authenticated;
grant execute on function "better_supabase"."expire_quotes"() to service_role;
-- The quotes that name a record as their company or contact, newest first.
create or replace function "better_supabase"."list_quotes_for_subject"(tenant uuid, subject_type text, subject_id text, page_size integer default 50)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
begin
if not ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', list_quotes_for_subject.tenant, 'quotes.read'), false)) then
raise exception 'not allowed to read this organization''s quotes' using errcode = '42501', hint = 'QUOTE_FORBIDDEN';
end if;
return coalesce((
select jsonb_agg(z.item order by z.created desc, z.id desc)
from (
select (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as item, x."created_at" as created, x."id" as id
from "better_supabase"."quotes" x
where x."tenant_id" = list_quotes_for_subject.tenant
and ((x."company_type" = list_quotes_for_subject.subject_type and x."company_id"::text = list_quotes_for_subject.subject_id) or (x."contact_type" = list_quotes_for_subject.subject_type and x."contact_id"::text = list_quotes_for_subject.subject_id))
order by x."created_at" desc, x."id" desc
limit least(greatest(coalesce(list_quotes_for_subject.page_size, 50), 1), 200)
) z
), '[]'::jsonb);
end;
$$;
revoke execute on function "better_supabase"."list_quotes_for_subject"(uuid, text, text, integer) from public, anon;
grant execute on function "better_supabase"."list_quotes_for_subject"(uuid, text, text, integer) to authenticated, service_role;
-- The organization's quotes as a keyset page, newest first (ADR 1077). Needs
-- quotes.read. filters is { status, ownerId, currency, companyId, contactId
-- (each a value or a list), search (title, identifier), sort (createdAt,
-- validUntil, title), direction, facet, count, limit, after, offset, paged,
-- total, totalCap }.
create or replace function "better_supabase"."page_quotes"(tenant uuid, filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_filters jsonb := coalesce(page_quotes.filters, '{}'::jsonb);
v_search text := case when nullif(trim(v_filters ->> 'search'), '') is null then null
else '%' || replace(replace(replace(trim(v_filters ->> 'search'), '\', '\\'), '%', '\%'), '_', '\_') || '%' end;
v_limit integer := least(greatest(coalesce((v_filters ->> 'limit')::integer, 50), 1), 200);
v_offset integer := greatest(coalesce((v_filters ->> 'offset')::integer, 0), 0);
v_after jsonb := v_filters -> 'after';
v_paged boolean := coalesce((v_filters ->> 'paged')::boolean, false);
v_total_cap integer := least(greatest(coalesce((v_filters ->> 'totalCap')::integer, 10000), 1), 100000);
v_items jsonb;
v_more boolean;
v_next jsonb;
v_total integer;
v_sort text := nullif(v_filters ->> 'sort', '');
v_dir text := nullif(v_filters ->> 'direction', '');
v_facet text := nullif(v_filters ->> 'facet', '');
v_count boolean := coalesce((v_filters ->> 'count')::boolean, false);
v_result jsonb;
begin
if not ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', page_quotes.tenant, 'quotes.read'), false)) then
raise exception 'not allowed to read this organization''s quotes' using errcode = '42501', hint = 'QUOTE_FORBIDDEN';
end if;
if v_filters ? 'after' and v_filters ? 'offset' then
raise exception 'after and offset are two ways to page; pass one' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('status', 'currency', 'ownerId') then
raise exception 'facet is one of status, currency, ownerId' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
with s as (
select (case v_facet when 'status' then (x."status"::text)::text when 'currency' then (x."currency")::text when 'ownerId' then (coalesce(x."owner_id"::text, ''))::text end) as v
from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
limit v_total_cap + 1
)
select jsonb_build_object('facet', v_facet, 'values', coalesce(jsonb_agg(jsonb_build_object('value', g.v, 'count', jsonb_build_object('value', g.n, 'exact', g.exact)) order by g.n desc, g.v), '[]'::jsonb))
into v_result
from (
select s.v, count(*) as n, (select count(*) from s) <= v_total_cap as exact
from s where s.v is not null group by s.v order by n desc, s.v limit 100
) g;
return v_result;
end if;
if v_count then
select count(*) into v_total from (
select 1 from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
limit v_total_cap + 1
) c;
return jsonb_build_object('value', least(v_total, v_total_cap), 'exact', v_total <= v_total_cap);
end if;
if v_sort is not null and v_sort not in ('createdAt', 'validUntil', 'title') then
raise exception 'sort is one of createdAt, validUntil, title' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
if v_dir is not null and v_dir not in ('asc', 'desc') then
raise exception 'direction is asc or desc' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
if v_sort is null and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (x."created_at", x."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."created_at" asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort is null and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (x."created_at" <= (v_after ->> 0)::timestamptz and ((x."created_at" < (v_after ->> 0)::timestamptz) or (x."created_at" = (v_after ->> 0)::timestamptz and x."id" > (v_after ->> 1)::uuid))))
order by x."created_at" desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'createdAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (x."created_at", x."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."created_at" asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'createdAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (x."created_at" <= (v_after ->> 0)::timestamptz and ((x."created_at" < (v_after ->> 0)::timestamptz) or (x."created_at" = (v_after ->> 0)::timestamptz and x."id" > (v_after ->> 1)::uuid))))
order by x."created_at" desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'validUntil' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((coalesce(x."valid_until", date '9999-12-31'))::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (coalesce(x."valid_until", date '9999-12-31'), x."id") > ((v_after ->> 0)::date, (v_after ->> 1)::uuid))
order by coalesce(x."valid_until", date '9999-12-31') asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'validUntil' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((coalesce(x."valid_until", date '9999-12-31'))::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (coalesce(x."valid_until", date '9999-12-31') <= (v_after ->> 0)::date and ((coalesce(x."valid_until", date '9999-12-31') < (v_after ->> 0)::date) or (coalesce(x."valid_until", date '9999-12-31') = (v_after ->> 0)::date and x."id" > (v_after ->> 1)::uuid))))
order by coalesce(x."valid_until", date '9999-12-31') desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'title' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((lower(x."title"))::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (lower(x."title"), x."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by lower(x."title") asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'title' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'QUOTE_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) as j, jsonb_build_array((lower(x."title"))::text, (x."id")::text) as k
from (
select * from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
and (v_after is null or (lower(x."title") <= (v_after ->> 0)::text and ((lower(x."title") < (v_after ->> 0)::text) or (lower(x."title") = (v_after ->> 0)::text and x."id" > (v_after ->> 1)::uuid))))
order by lower(x."title") desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
end if;
if not v_paged then
return v_items;
end if;
if coalesce((v_filters ->> 'total')::boolean, false) then
select count(*) into v_total from (
select 1 from "better_supabase"."quotes" x
where x."tenant_id" = page_quotes.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'ownerId' is not null and jsonb_typeof(v_filters -> 'ownerId') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerId') = 'array' and jsonb_array_length(v_filters -> 'ownerId') = 0)) or x."owner_id"::text = any((case when jsonb_typeof(v_filters -> 'ownerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerId')) else array[v_filters ->> 'ownerId'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (not (v_filters -> 'companyId' is not null and jsonb_typeof(v_filters -> 'companyId') <> 'null' and not (jsonb_typeof(v_filters -> 'companyId') = 'array' and jsonb_array_length(v_filters -> 'companyId') = 0)) or x."company_id"::text = any((case when jsonb_typeof(v_filters -> 'companyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'companyId')) else array[v_filters ->> 'companyId'] end)::text[]))
and (not (v_filters -> 'contactId' is not null and jsonb_typeof(v_filters -> 'contactId') <> 'null' and not (jsonb_typeof(v_filters -> 'contactId') = 'array' and jsonb_array_length(v_filters -> 'contactId') = 0)) or x."contact_id"::text = any((case when jsonb_typeof(v_filters -> 'contactId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'contactId')) else array[v_filters ->> 'contactId'] end)::text[]))
and (v_search is null or x."title" ilike v_search or x."identifier" ilike v_search)
limit v_total_cap + 1
) c;
end if;
return jsonb_build_object('items', v_items, 'hasMore', v_more, 'nextCursor', case when v_more then v_next end)
|| case when v_total is null then '{}'::jsonb
else jsonb_build_object('total', least(v_total, v_total_cap), 'totalCapped', v_total > v_total_cap) end;
end;
$$;
revoke execute on function "better_supabase"."page_quotes"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."page_quotes"(uuid, jsonb) to authenticated, service_role;
-- One quote by id, or a not-found error. Needs quotes.read.
create or replace function "better_supabase"."get_quote"(quote uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
begin
select * into v_row from "better_supabase"."quotes" x where x."id" = get_quote.quote;
if v_row."id" is null or not ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'quotes.read'), false)) then
raise exception 'no quote you can read has this id' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
return (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb));
end;
$$;
revoke execute on function "better_supabase"."get_quote"(uuid) from public, anon;
grant execute on function "better_supabase"."get_quote"(uuid) to authenticated, service_role;
create or replace function "better_supabase"."clone_quote"(source uuid, overrides jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_src "better_supabase"."quotes";
v_created jsonb;
v_id uuid;
v_tenant uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_src from "better_supabase"."quotes" x where x."id" = clone_quote.source;
if not found then
raise exception 'no such quote' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
v_tenant := coalesce(nullif(coalesce(clone_quote.overrides, '{}'::jsonb) ->> 'tenantId', '')::uuid, v_src."tenant_id");
if v_tenant is distinct from v_src."tenant_id" and not ((auth.role() = 'service_role') or false) then
raise exception 'not allowed to change quotes' using errcode = '42501', hint = 'QUOTE_FORBIDDEN';
end if;
if not ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', v_tenant, 'quotes.write'), false)) then
raise exception 'not allowed to change quotes' using errcode = '42501', hint = 'QUOTE_FORBIDDEN';
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_quote_clone"(uuid, jsonb)');
begin
if v_hook is not null then
execute format('select %s($1::uuid, $2::jsonb)', v_hook::oid::regproc)
using clone_quote.source, clone_quote.overrides;
end if;
end;
v_created := "better_supabase"."create_quote"(v_tenant, jsonb_strip_nulls(jsonb_build_object('terms', v_src."terms", 'note', v_src."note", 'pricesIncludeTax', v_src."prices_include_tax", 'discountBps', v_src."discount_bps", 'discountAmount', v_src."discount_amount", 'depositBps', v_src."deposit_bps", 'depositAmount', v_src."deposit_amount", 'depositRequired', v_src."deposit_required", 'company', case when v_src."company_id" is null then null else jsonb_build_object('type', v_src."company_type", 'id', v_src."company_id") end, 'contact', case when v_src."contact_id" is null then null else jsonb_build_object('type', v_src."contact_type", 'id', v_src."contact_id") end, 'title', v_src."title", 'currency', v_src."currency", 'templateId', null, 'sourceTemplateId', null)) || (coalesce(clone_quote.overrides, '{}'::jsonb) - 'templateId') || jsonb_strip_nulls(jsonb_build_object('templateId', null, 'sourceTemplateId', null)), null);
v_id := (v_created ->> 'id')::uuid;
insert into "better_supabase"."quote_lines"
select (jsonb_populate_record(null::"better_supabase"."quote_lines", to_jsonb(l) || jsonb_build_object('id', gen_random_uuid(), 'parent_id', v_id, 'tenant_id', v_tenant))).*
from "better_supabase"."quote_lines" l where l."parent_id" = clone_quote.source;
declare
v_hook regprocedure := to_regprocedure('"public"."after_quote_clone"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_id;
end if;
end;
return v_created;
end;
$$;
revoke execute on function "better_supabase"."clone_quote"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."clone_quote"(uuid, jsonb) to authenticated, service_role;
-- Makes another variant of a quote: a new draft quote with the same pricing,
-- party and lines, grouped under the same root. label names it (A, B, Basic,
-- Premium) and is unique within the group; attrs change the new quote like
-- create_quote's. The first variant gives the quote it came from the label
-- Original when it has none.
create or replace function "better_supabase"."create_quote_variant"(quote uuid, label text, attrs jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_src "better_supabase"."quotes";
v_root uuid;
v_label text := nullif(trim(create_quote_variant.label), '');
v_made jsonb;
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_src from "better_supabase"."quotes" x where x."id" = create_quote_variant.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false));
if v_src."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
if v_label is null or length(v_label) > 100 then
raise exception 'a variant needs a label of up to 100 characters' using errcode = '22023', hint = 'QUOTE_VARIANT_INVALID';
end if;
v_root := coalesce(v_src."variant_of", v_src."id");
if exists (select 1 from "better_supabase"."quotes" x where (x."id" = v_root or x."variant_of" = v_root) and lower(x."variant_label") = lower(v_label)) then
raise exception 'another variant of this quote has that label' using errcode = '23505', hint = 'QUOTE_VARIANT_LABEL_TAKEN';
end if;
v_made := "better_supabase"."create_quote"(v_src."tenant_id", jsonb_strip_nulls(jsonb_build_object(
'title', v_src."title", 'currency', v_src."currency", 'validUntil', v_src."valid_until",
'company', case when v_src."company_id" is null then null else jsonb_build_object('type', v_src."company_type", 'id', v_src."company_id") end, 'contact', case when v_src."contact_id" is null then null else jsonb_build_object('type', v_src."contact_type", 'id', v_src."contact_id") end, 'terms', v_src."terms", 'note', v_src."note", 'pricesIncludeTax', v_src."prices_include_tax", 'discountBps', v_src."discount_bps", 'discountAmount', v_src."discount_amount"::text, 'depositBps', v_src."deposit_bps", 'depositAmount', v_src."deposit_amount"::text, 'depositRequired', v_src."deposit_required")) || coalesce(create_quote_variant.attrs, '{}'::jsonb), null);
v_id := (v_made ->> 'id')::uuid;
insert into "better_supabase"."quote_lines" ("tenant_id", "parent_id", "product_type", "product_id", "description", "quantity", "unit_amount", "discount_bps", "tax_rate_bps", "tax_code", "tax_code_id", "unit_cost", "discount_amount", "cost_source", "location_id", "bin_id", "billing_party", "billing_party_id", "billing_account_id", "position", "section", "optional", "selected", "periods", "unit", "discount_kind")
select x."tenant_id", v_id, x."product_type", x."product_id", x."description", x."quantity", x."unit_amount", x."discount_bps", x."tax_rate_bps", x."tax_code", x."tax_code_id", x."unit_cost", x."discount_amount", x."cost_source", x."location_id", x."bin_id", x."billing_party", x."billing_party_id", x."billing_account_id", x."position", x."section", x."optional", x."selected", x."periods", x."unit", x."discount_kind" from "better_supabase"."quote_lines" x where x."parent_id" = v_src."id";
update "better_supabase"."quotes" x set "variant_label" = coalesce(x."variant_label", 'Original')
where x."id" = v_root;
update "better_supabase"."quotes" x set "variant_of" = v_root, "variant_label" = v_label, "is_selected" = false
where x."id" = v_id;
return (select (to_jsonb(x) - 'token_hash') || jsonb_build_object('number', x."number"::text, 'list_total', x."list_total"::text, 'discount_total', x."discount_total"::text, 'subtotal', x."subtotal"::text, 'tax_total', x."tax_total"::text, 'total', x."total"::text, 'discount_amount', x."discount_amount"::text, 'deposit_due', x."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = x."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = x."id"), '[]'::jsonb)) from "better_supabase"."quotes" x where x."id" = v_id);
end;
$$;
revoke execute on function "better_supabase"."create_quote_variant"(uuid, text, jsonb) from public, anon;
grant execute on function "better_supabase"."create_quote_variant"(uuid, text, jsonb) to authenticated, service_role;
-- The quotes of one group of variants, the root first. isSelected tells which
-- one the customer chose.
create or replace function "better_supabase"."list_quote_variants"(quote uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_src "better_supabase"."quotes";
begin
select * into v_src from "better_supabase"."quotes" x where x."id" = list_quote_variants.quote;
if v_src."id" is null or not ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', v_src."tenant_id", 'quotes.read'), false)) then
raise exception 'no such quote you can read' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
return (select coalesce(jsonb_agg(jsonb_build_object(
'id', x."id", 'identifier', x."identifier", 'variantLabel', x."variant_label",
'isSelected', x."is_selected", 'status', x."status",
'currency', x."currency", 'total', x."total"::text, 'isRoot', x."variant_of" is null)
order by (x."variant_of" is not null), x."number"), '[]'::jsonb)
from "better_supabase"."quotes" x where (x."id" = coalesce(v_src."variant_of", v_src."id") or x."variant_of" = coalesce(v_src."variant_of", v_src."id")));
end;
$$;
revoke execute on function "better_supabase"."list_quote_variants"(uuid) from public, anon;
grant execute on function "better_supabase"."list_quote_variants"(uuid) to authenticated, service_role;
-- Selects one variant of a quote and marks the others of its group not
-- selected; only the selected variant becomes an order or an invoice. A
-- withdrawn, rejected or expired variant can't be selected. options.forward
-- is order or invoice to copy the selected variant forward in the same call
-- (the quote must be accepted, as for order_from_quote and invoice_from_quote);
-- options.attrs go to order_from_quote and options.depositBps to
-- invoice_from_quote.
create or replace function "better_supabase"."select_quote_variant"(quote uuid, options jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."quotes";
v_root uuid;
v_forward text := nullif(select_quote_variant.options ->> 'forward', '');
v_order jsonb;
v_invoice jsonb;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = select_quote_variant.quote and ((auth.role() = 'service_role') or coalesce(better_supabase.can('tenant', x."tenant_id", 'quotes.write'), false)) for update;
if v_row."id" is null then
raise exception 'no such quote you can edit' using errcode = 'P0002', hint = 'QUOTE_NOT_FOUND';
end if;
v_root := coalesce(v_row."variant_of", v_row."id");
if v_row."variant_of" is null and not exists (select 1 from "better_supabase"."quotes" x where x."variant_of" = v_row."id") then
raise exception 'this quote has no variants' using errcode = '22023', hint = 'QUOTE_VARIANT_INVALID';
end if;
if v_row."status" in ('withdrawn', 'rejected', 'expired') then
raise exception 'a withdrawn, rejected or expired variant can''t be selected' using errcode = '55000', hint = 'QUOTE_VARIANT_NOT_SELECTABLE';
end if;
if v_forward is not null and v_forward not in ('') then
raise exception 'forward is order or invoice' using errcode = '22023', hint = 'QUOTE_VARIANT_INVALID';
end if;
perform 1 from "better_supabase"."quotes" x where (x."id" = v_root or x."variant_of" = v_root) for update;
update "better_supabase"."quotes" x set "is_selected" = (x."id" = v_row."id"), "updated_at" = now()
where (x."id" = v_root or x."variant_of" = v_root) and x."is_selected" is distinct from (x."id" = v_row."id");
if v_forward is not null and v_order is null and v_invoice is null then
raise exception 'the organization has no block to copy the variant forward to' using errcode = '22023', hint = 'QUOTE_VARIANT_INVALID';
end if;
select * into v_row from "better_supabase"."quotes" x where x."id" = v_row."id";
return jsonb_build_object('quote', (to_jsonb(v_row) - 'token_hash') || jsonb_build_object('number', v_row."number"::text, 'list_total', v_row."list_total"::text, 'discount_total', v_row."discount_total"::text, 'subtotal', v_row."subtotal"::text, 'tax_total', v_row."tax_total"::text, 'total', v_row."total"::text, 'discount_amount', v_row."discount_amount"::text, 'deposit_due', v_row."deposit_due"::text) || jsonb_build_object('lines', coalesce((select jsonb_agg(jsonb_build_object('position', ql."position", 'description', ql."description", 'quantity', ql."quantity", 'unitAmount', ql."unit_amount"::text, 'discountBps', ql."discount_bps", 'taxRateBps', ql."tax_rate_bps", 'taxCode', ql."tax_code", 'section', ql."section", 'optional', ql."optional", 'selected', ql."selected", 'periods', ql."periods", 'unit', ql."unit", 'discountKind', ql."discount_kind", 'taxCodeId', ql."tax_code_id", 'unitCost', ql."unit_cost"::text, 'discountAmount', ql."discount_amount"::text, 'costSource', ql."cost_source", 'locationId', ql."location_id", 'binId', ql."bin_id", 'billingParty', ql."billing_party", 'billingPartyId', ql."billing_party_id", 'billingAccountId', ql."billing_account_id", 'product', case when ql."product_type" is null then null else jsonb_build_object('type', ql."product_type", 'id', ql."product_id") end) order by ql."position") from "better_supabase"."quote_lines" ql where ql."parent_id" = v_row."id"), '[]'::jsonb), 'taxLines', coalesce((select jsonb_agg(jsonb_build_object('taxRateBps', tl."tax_rate_bps", 'taxCode', tl."tax_code", 'net', tl."net"::text, 'tax', tl."tax"::text, 'gross', tl."gross"::text, 'discount', tl."discount"::text) order by tl."tax_rate_bps", tl."tax_code" nulls first) from "better_supabase"."quote_tax_lines" tl where tl."parent_id" = v_row."id"), '[]'::jsonb)), 'variants', (select coalesce(jsonb_agg(jsonb_build_object(
'id', x."id", 'identifier', x."identifier", 'variantLabel', x."variant_label",
'isSelected', x."is_selected", 'status', x."status",
'currency', x."currency", 'total', x."total"::text, 'isRoot', x."variant_of" is null)
order by (x."variant_of" is not null), x."number"), '[]'::jsonb)
from "better_supabase"."quotes" x where (x."id" = v_root or x."variant_of" = v_root)), 'order', v_order, 'invoice', v_invoice);
end;
$$;
revoke execute on function "better_supabase"."select_quote_variant"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."select_quote_variant"(uuid, jsonb) to authenticated, service_role;
-- The import target of the quote entity (ADR 0141): item.data is { number,
-- status?, title?, currency, validUntil?, createdAt?, sentAt?, viewedAt?,
-- decidedAt?, decidedBy?, decisionNote?, company?, contact?, origin?,
-- pricesIncludeTax?, attrs?, legalEntityId?, buyer?, lines, taxLines?,
-- totals? }. A quote that is already imported is kept as it is, unless
-- item.data.update is 'apply': then its fields, lines, stored tax and totals
-- are replaced, whatever status the quote is in.
create or replace function "better_supabase"."import_quote"(run uuid, item jsonb, subject_id text)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid := (import_quote.item ->> 'tenant')::uuid;
v_data jsonb := import_quote.item -> 'data';
v_number text := nullif(btrim(v_data ->> 'number'), '');
v_made jsonb;
v_id uuid;
v_row "better_supabase"."quotes";
v_warnings jsonb := '[]'::jsonb;
v_computed jsonb;
v_given jsonb;
v_same boolean;
begin
if import_quote.subject_id is not null then
select * into v_row from "better_supabase"."quotes" y where y."id" = import_quote.subject_id::uuid and y."tenant_id" = v_tenant;
if not found then
raise exception 'the quote no longer exists' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if coalesce(v_data ->> 'update', 'keep') is distinct from 'apply' then
return jsonb_build_object('subjectId', v_row."id"::text, 'updatedAt', v_row."updated_at", 'warnings', jsonb_build_array(jsonb_build_object('code', 'IMPORT_DOCUMENT_CHANGED', 'message', 'the source quote changed after it was imported; an imported quote is kept as imported, so import the correction as a new quote')));
end if;
v_id := v_row."id";
v_same := (select count(*) from "better_supabase"."quote_lines" l where l."parent_id" = v_id) = jsonb_array_length(coalesce(v_data -> 'lines', '[]'::jsonb));
-- A quote that was sent or decided can't change its lines. The import
-- reopens it for the replacement, then the update below restores the
-- status it had, so a historical quote keeps its decided state.
if not v_same and jsonb_typeof(v_data -> 'lines') = 'array' then
update "better_supabase"."quotes" y set "status" = 'draft' where y."id" = v_id;
perform "better_supabase"."set_quote_lines"(v_id, v_data -> 'lines');
end if;
update "better_supabase"."quotes" y set
"status" = coalesce(v_data ->> 'status', 'draft'),
"created_at" = coalesce(nullif(v_data ->> 'createdAt', '')::timestamptz, y."created_at"),
"sent_at" = nullif(v_data ->> 'sentAt', '')::timestamptz,
"viewed_at" = nullif(v_data ->> 'viewedAt', '')::timestamptz,
"decided_at" = nullif(v_data ->> 'decidedAt', '')::timestamptz,
"decided_by" = nullif(v_data ->> 'decidedBy', ''),
"decision_note" = nullif(v_data ->> 'decisionNote', ''),
"buyer" = case when jsonb_typeof(v_data -> 'buyer') = 'object' then v_data -> 'buyer' end,
"updated_at" = now(),
"title" = coalesce(nullif(v_data ->> 'title', ''), y."title"),
"currency" = coalesce(nullif(v_data ->> 'currency', ''), y."currency"),
"valid_until" = coalesce(nullif(v_data ->> 'validUntil', '')::date, y."valid_until"),
"company_id" = coalesce(nullif(v_data ->> 'companyId', ''), y."company_id")
where y."id" = v_id;
if v_same then
update "better_supabase"."quote_lines" l set
"description" = coalesce(nullif(e.l ->> 'title', ''), nullif(e.l ->> 'description', ''), l."description"),
"tax_rate_bps" = coalesce((e.l ->> 'taxRateBps')::integer, l."tax_rate_bps"),
"tax_code" = coalesce(nullif(e.l ->> 'taxCode', ''), l."tax_code")
from jsonb_array_elements(v_data -> 'lines') with ordinality as e(l, n)
where l."parent_id" = v_id and l."position" = e.n::integer;
end if;
if jsonb_typeof(v_data -> 'taxLines') = 'array' then
v_computed := coalesce((
select jsonb_object_agg(k.key, jsonb_build_object('net', k.net, 'tax', k.tax))
from (select coalesce(z."tax_code", '') || ':' || z."tax_rate_bps"::text as key, sum(z."net") as net, sum(z."tax") as tax
from "better_supabase"."quote_tax_lines" z where z."parent_id" = v_id group by 1) k
), '{}'::jsonb);
v_given := coalesce((
select jsonb_object_agg(k.key, jsonb_build_object('net', k.net, 'tax', k.tax))
from (select coalesce(g.value ->> 'taxCode', '') || ':' || coalesce((g.value ->> 'taxRateBps')::integer, 0)::text as key,
sum((g.value ->> 'net')::bigint) as net, sum((g.value ->> 'tax')::bigint) as tax
from jsonb_array_elements(v_data -> 'taxLines') g group by 1) k
), '{}'::jsonb);
if v_computed <> v_given then
v_warnings := v_warnings || jsonb_build_array(jsonb_build_object('code', 'IMPORT_TAX_MISMATCH', 'message', 'the stored tax per code differs from the tax computed from the lines; the stored amounts were kept', 'computed', v_computed, 'given', v_given));
end if;
delete from "better_supabase"."quote_tax_lines" z where z."parent_id" = v_id;
insert into "better_supabase"."quote_tax_lines" ("tenant_id", "parent_id", "tax_rate_bps", "tax_code", "net", "tax", "gross", "discount")
select v_tenant, v_id, coalesce((g.value ->> 'taxRateBps')::integer, 0), nullif(g.value ->> 'taxCode', ''), (g.value ->> 'net')::bigint, (g.value ->> 'tax')::bigint,
coalesce((g.value ->> 'gross')::bigint, (g.value ->> 'net')::bigint + (g.value ->> 'tax')::bigint), coalesce((g.value ->> 'discount')::bigint, 0)
from jsonb_array_elements(v_data -> 'taxLines') g;
end if;
if jsonb_typeof(v_data -> 'totals') = 'object' then
select * into v_row from "better_supabase"."quotes" y where y."id" = v_id;
if (v_data -> 'totals' ->> 'subtotal' is not null and (v_data -> 'totals' ->> 'subtotal')::bigint <> v_row."subtotal")
or (v_data -> 'totals' ->> 'tax' is not null and (v_data -> 'totals' ->> 'tax')::bigint <> v_row."tax_total")
or (v_data -> 'totals' ->> 'total' is not null and (v_data -> 'totals' ->> 'total')::bigint <> v_row."total") then
v_warnings := v_warnings || jsonb_build_array(jsonb_build_object('code', 'IMPORT_TOTALS_MISMATCH', 'message', 'the stored totals differ from the totals computed from the lines; the stored totals were kept',
'computed', jsonb_build_object('subtotal', v_row."subtotal"::text, 'tax', v_row."tax_total"::text, 'total', v_row."total"::text),
'given', v_data -> 'totals'));
end if;
update "better_supabase"."quotes" y set
"subtotal" = coalesce((v_data -> 'totals' ->> 'subtotal')::bigint, y."subtotal"),
"tax_total" = coalesce((v_data -> 'totals' ->> 'tax')::bigint, y."tax_total"),
"total" = coalesce((v_data -> 'totals' ->> 'total')::bigint, y."total")
where y."id" = v_id;
end if;
select * into v_row from "better_supabase"."quotes" y where y."id" = v_id;
return jsonb_build_object('subjectId', v_id::text, 'updatedAt', v_row."updated_at", 'warnings', v_warnings);
end if;
if v_number is null then
raise exception 'a quote needs its number' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if coalesce(v_data ->> 'status', 'draft') not in ('draft', 'sent', 'viewed', 'accepted', 'rejected', 'changes_requested', 'expired', 'withdrawn') then
raise exception 'status is draft, sent, viewed, accepted, rejected, changes_requested, expired or withdrawn' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if jsonb_typeof(v_data -> 'lines') is distinct from 'array' or jsonb_array_length(v_data -> 'lines') = 0 then
raise exception 'a quote needs at least one line' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if coalesce(v_data ->> 'onTaken', 'fail') not in ('fail', 'next_free') then
raise exception 'onTaken is fail or next_free' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if v_data ->> 'onTaken' = 'next_free' and exists (select 1 from "better_supabase"."quotes" y where y."tenant_id" = v_tenant and y."identifier" = v_number) then
v_warnings := v_warnings || jsonb_build_array(jsonb_build_object('code', 'IMPORT_NUMBER_REASSIGNED', 'message', 'number ' || v_number || ' was taken; the next free number was issued'));
v_number := null;
end if;
if v_number is not null then
perform set_config('better_supabase.fixed_number', 'quote' || chr(31) || v_number, true);
end if;
v_made := "better_supabase"."create_quote"(v_tenant, coalesce(v_data -> 'attrs', '{}'::jsonb) || jsonb_strip_nulls(jsonb_build_object(
'title', coalesce(nullif(v_data ->> 'title', ''), replace('Quote {number}', '{number}', coalesce(v_number, ''))),
'currency', v_data -> 'currency',
'validUntil', v_data -> 'validUntil',
'terms', v_data -> 'terms',
'note', v_data -> 'note',
'company', v_data -> 'company',
'contact', v_data -> 'contact',
'origin', v_data -> 'origin',
'pricesIncludeTax', v_data -> 'pricesIncludeTax',
'discountBps', v_data -> 'discountBps',
'discountAmount', v_data -> 'discountAmount',
'depositBps', v_data -> 'depositBps',
'depositAmount', v_data -> 'depositAmount')), v_data -> 'lines');
perform set_config('better_supabase.fixed_number', '', true);
v_id := (v_made ->> 'id')::uuid;
update "better_supabase"."quotes" y set
"status" = coalesce(v_data ->> 'status', 'draft'),
"created_at" = coalesce(nullif(v_data ->> 'createdAt', '')::timestamptz, y."created_at"),
"sent_at" = nullif(v_data ->> 'sentAt', '')::timestamptz,
"viewed_at" = nullif(v_data ->> 'viewedAt', '')::timestamptz,
"decided_at" = nullif(v_data ->> 'decidedAt', '')::timestamptz,
"decided_by" = nullif(v_data ->> 'decidedBy', ''),
"decision_note" = nullif(v_data ->> 'decisionNote', ''),
"buyer" = case when jsonb_typeof(v_data -> 'buyer') = 'object' then v_data -> 'buyer' end,
"updated_at" = now()
where y."id" = v_id;
if jsonb_typeof(v_data -> 'taxLines') = 'array' then
v_computed := coalesce((
select jsonb_object_agg(k.key, jsonb_build_object('net', k.net, 'tax', k.tax))
from (select coalesce(z."tax_code", '') || ':' || z."tax_rate_bps"::text as key, sum(z."net") as net, sum(z."tax") as tax
from "better_supabase"."quote_tax_lines" z where z."parent_id" = v_id group by 1) k
), '{}'::jsonb);
v_given := coalesce((
select jsonb_object_agg(k.key, jsonb_build_object('net', k.net, 'tax', k.tax))
from (select coalesce(g.value ->> 'taxCode', '') || ':' || coalesce((g.value ->> 'taxRateBps')::integer, 0)::text as key,
sum((g.value ->> 'net')::bigint) as net, sum((g.value ->> 'tax')::bigint) as tax
from jsonb_array_elements(v_data -> 'taxLines') g group by 1) k
), '{}'::jsonb);
if v_computed <> v_given then
v_warnings := v_warnings || jsonb_build_array(jsonb_build_object('code', 'IMPORT_TAX_MISMATCH', 'message', 'the stored tax per code differs from the tax computed from the lines; the stored amounts were kept', 'computed', v_computed, 'given', v_given));
end if;
delete from "better_supabase"."quote_tax_lines" z where z."parent_id" = v_id;
insert into "better_supabase"."quote_tax_lines" ("tenant_id", "parent_id", "tax_rate_bps", "tax_code", "net", "tax", "gross", "discount")
select v_tenant, v_id, coalesce((g.value ->> 'taxRateBps')::integer, 0), nullif(g.value ->> 'taxCode', ''), (g.value ->> 'net')::bigint, (g.value ->> 'tax')::bigint,
coalesce((g.value ->> 'gross')::bigint, (g.value ->> 'net')::bigint + (g.value ->> 'tax')::bigint), coalesce((g.value ->> 'discount')::bigint, 0)
from jsonb_array_elements(v_data -> 'taxLines') g;
end if;
if jsonb_typeof(v_data -> 'totals') = 'object' then
select * into v_row from "better_supabase"."quotes" y where y."id" = v_id;
if (v_data -> 'totals' ->> 'subtotal' is not null and (v_data -> 'totals' ->> 'subtotal')::bigint <> v_row."subtotal")
or (v_data -> 'totals' ->> 'tax' is not null and (v_data -> 'totals' ->> 'tax')::bigint <> v_row."tax_total")
or (v_data -> 'totals' ->> 'total' is not null and (v_data -> 'totals' ->> 'total')::bigint <> v_row."total") then
v_warnings := v_warnings || jsonb_build_array(jsonb_build_object('code', 'IMPORT_TOTALS_MISMATCH', 'message', 'the stored totals differ from the totals computed from the lines; the stored totals were kept',
'computed', jsonb_build_object('subtotal', v_row."subtotal"::text, 'tax', v_row."tax_total"::text, 'total', v_row."total"::text),
'given', v_data -> 'totals'));
end if;
update "better_supabase"."quotes" y set
"subtotal" = coalesce((v_data -> 'totals' ->> 'subtotal')::bigint, y."subtotal"),
"tax_total" = coalesce((v_data -> 'totals' ->> 'tax')::bigint, y."tax_total"),
"total" = coalesce((v_data -> 'totals' ->> 'total')::bigint, y."total")
where y."id" = v_id;
end if;
select * into v_row from "better_supabase"."quotes" y where y."id" = v_id;
return jsonb_build_object('subjectId', v_id::text, 'updatedAt', v_row."updated_at", 'warnings', v_warnings);
end;
$$;
revoke execute on function "better_supabase"."import_quote"(uuid, jsonb, text) from public, anon, authenticated;
grant execute on function "better_supabase"."import_quote"(uuid, jsonb, text) to service_role;
-- When a quote last changed, for the conflict check of imports.
create or replace function "better_supabase"."quote_updated_at"(subject_id text)
returns timestamptz
language sql
stable
security definer
set search_path = ''
as $$
select y."updated_at" from "better_supabase"."quotes" y where y."id" = $1::uuid
$$;
revoke execute on function "better_supabase"."quote_updated_at"(text) from public, anon, authenticated;
grant execute on function "better_supabase"."quote_updated_at"(text) to service_role;
-- Totals and cost of one record's lines per billing party (ADR 0141). It runs
-- as the caller, so only lines the caller can read count.
create or replace function "better_supabase"."quote_totals_by_billing_party"(quote uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(g.row order by g.party, g.party_id, g.account_id), '[]'::jsonb)
from (
select l."billing_party" as party, l."billing_party_id" as party_id, l."billing_account_id"::text as account_id,
jsonb_build_object(
'billingParty', l."billing_party", 'billingPartyId', l."billing_party_id", 'billingAccountId', l."billing_account_id"::text,
'lines', count(*), 'net', sum((round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - (round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0) + least(coalesce(l."discount_amount", 0), greatest(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") - round(round(l."quantity" * coalesce(l."periods", 1) * l."unit_amount") * l."discount_bps" / 10000.0), 0)))))::text, 'cost', sum(round(l."quantity" * l."unit_cost"))::text
) as row
from "better_supabase"."quote_lines" l
where l."parent_id" = quote and (not l."optional" or l."selected")
group by 1, 2, 3
) g
$$;
revoke execute on function "better_supabase"."quote_totals_by_billing_party"(uuid) from public, anon;
grant execute on function "better_supabase"."quote_totals_by_billing_party"(uuid) to authenticated, service_role;
-- Tenant isolation (ADR 0099): permissive policies combine with or, so one
-- the app adds would widen the block's. These restrictive policies keep each
-- table to the caller's organizations and what the block's policies grant.
create policy bsb_tenant_scope_select on "better_supabase"."quotes" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('quotes.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."quotes" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."quotes" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."quotes" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."quote_lines" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or (exists (select 1 from "better_supabase"."quotes" x where x."id" = "parent_id"))
);
create policy bsb_tenant_scope_insert on "better_supabase"."quote_lines" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."quote_lines" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."quote_lines" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."quote_versions" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or (exists (select 1 from "better_supabase"."quotes" q where q."id" = "quote_id"))
);
create policy bsb_tenant_scope_insert on "better_supabase"."quote_versions" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."quote_versions" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."quote_versions" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."quote_tax_lines" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or (exists (select 1 from "better_supabase"."quotes" q where q."id" = "parent_id"))
);
create policy bsb_tenant_scope_insert on "better_supabase"."quote_tax_lines" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."quote_tax_lines" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."quote_tax_lines" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
This block has no React entry yet. Its TypeScript side is in better-supabase/blocks/quotes, which runs on the server.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- quote.accepted
- quote.changes_requested
- quote.created
- quote.expired
- quote.rejected
- quote.revised
- quote.sent
- quote.updated
- quote.viewed
- quote.withdrawn
Dependencies
- Required: the install command adds it too
- Works with: optional
Recipes
More in CRM & sales
Partiescontacts Companies and people an organization works with: lifecycle stages, owners, party roles (customer, supplier and so on), payment terms with early-payment discounts and credit restrictions, credit limits with a soft or hard block, a language, delivery preferences per document type and a supplier status, who works where with a role and a primary contact, each person's addresses on channels (the inbox's contacts), domain and email dedupe, merges and keyset-paged search. Activitiesactivities Calls, emails, meetings, notes and tasks logged on one or more registered records, with an owner, a due time and an outcome, a record's timeline, its next step and a member's open tasks. Dealsdeals Deals on pipelines: amounts in minor units, a company and contacts, a deal team, line items, forecast calls with attainment, close-date slips, a next step, locking and batched edits with per-change rejections. Targetsgoals Goals per period for a member, a team or the organization, with progress the database computes for won deals, completed work items and paid invoices, or a value the app keeps. Contractscontracts Contracts with counterparties from draft through review and signature to active, expired or terminated, built on signatures, versions and reminders. Values are bigint minor units; renewal terms (auto-renew, renewal period, notice days) schedule a reminder before the notice deadline; obligations carry due dates and owners. Mail syncmail-sync Gmail, Outlook and other mailboxes synced per member through their own OAuth grant: threads and messages linked to the people and companies they involve, logged as email activities, and a record of the mail members send, with opens and clicks per message and per template version.