ekwo-os 0.25.2 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/dist/assets/expected-objects.json +324 -4
- package/dist/assets/migrations/20261107100000_a_control_account_reads_contact_by_contact.sql +269 -0
- package/dist/assets/migrations/20261108120000_an_electronic_invoice_arrives_by_the_same_door.sql +150 -0
- package/dist/assets/migrations/20261108152128_a_bank_account_keeps_the_profile_of_its_csv_statements.sql +26 -0
- package/dist/assets/migrations/20261110124117_the_socle_policies_ask_once.sql +177 -0
- package/dist/assets/migrations/20261115070000_a_financial_year_opens_on_any_month.sql +86 -0
- package/dist/assets/migrations/20261115080000_the_portfolio_reads_what_is_overdue.sql +119 -0
- package/dist/assets/migrations/20261115100000_a_declaration_has_a_short_name.sql +256 -0
- package/dist/assets/migrations/20261118144012_a_quote_carries_its_answer.sql +397 -0
- package/dist/assets/migrations/20261118170000_schema_version_0_26_0.sql +49 -0
- package/dist/assets/modules/budgets/supabase/migrations/20261109153212_budgets_policies_ask_once.sql +93 -0
- package/dist/assets/modules/consolidation/supabase/migrations/20261109154107_consolidation_policies_ask_once.sql +93 -0
- package/dist/assets/modules/contracts/supabase/migrations/20261109154842_contracts_policies_ask_once.sql +93 -0
- package/dist/assets/modules/corporate-tax/supabase/migrations/20261109160318_tax_policies_ask_once.sql +93 -0
- package/dist/assets/modules/crm/supabase/migrations/20261109120000_crm_reads_hold_at_volume.sql +95 -0
- package/dist/assets/modules/crm/supabase/migrations/20261115090000_crm_contact_vcard.sql +159 -0
- package/dist/assets/modules/crm/supabase/migrations/20261118145000_crm_deal_documents.sql +569 -0
- package/dist/assets/modules/einvoicing/supabase/migrations/20261109155021_einvoicing_policies_ask_once.sql +93 -0
- package/dist/assets/modules/fixed-assets/supabase/migrations/20261109155512_assets_policies_ask_once.sql +93 -0
- package/dist/assets/seed/00_currencies.sql +5 -0
- package/dist/assets/seed/00_territories.sql +57 -0
- package/dist/assets/seed/10_pack_be.sql +1 -1
- package/dist/assets/seed/11_pack_fr.sql +1 -1
- package/dist/assets/seed/13_pack_ee.sql +9 -4
- package/dist/assets/seed/15_pack_us.sql +1 -1
- package/dist/assets/seed/220_pack_ad.sql +666 -0
- package/dist/assets/seed/224_pack_pw.sql +642 -0
- package/dist/assets/seed/226_pack_sb.sql +614 -0
- package/dist/assets/seed/43_pack_pt.sql +14 -5
- package/dist/assets/seed/500_pack_nr.sql +468 -0
- package/dist/assets/seed/61_pack_se.sql +9 -5
- package/dist/assets/seed/64_pack_pl.sql +5 -2
- package/dist/assets/seed/65_pack_dk.sql +7 -4
- package/dist/assets/seed/67_pack_fi.sql +7 -2
- package/dist/assets/seed/68_pack_cz.sql +8 -2
- package/dist/assets/seed/70_pack_hu.sql +11 -7
- package/dist/assets/seed/72_pack_ro.sql +11 -7
- package/dist/cli.d.ts +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +44 -4
- package/dist/cli.js.map +1 -1
- package/dist/commands/bank.d.ts +16 -0
- package/dist/commands/bank.d.ts.map +1 -0
- package/dist/commands/bank.js +82 -0
- package/dist/commands/bank.js.map +1 -0
- package/dist/commands/close.d.ts +33 -0
- package/dist/commands/close.d.ts.map +1 -0
- package/dist/commands/close.js +213 -0
- package/dist/commands/close.js.map +1 -0
- package/dist/commands/crm.d.ts +5 -4
- package/dist/commands/crm.d.ts.map +1 -1
- package/dist/commands/crm.js +70 -5
- package/dist/commands/crm.js.map +1 -1
- package/dist/commands/document.d.ts +2 -1
- package/dist/commands/document.d.ts.map +1 -1
- package/dist/commands/document.js +58 -4
- package/dist/commands/document.js.map +1 -1
- package/dist/commands/import.d.ts +3 -0
- package/dist/commands/import.d.ts.map +1 -1
- package/dist/commands/import.js +81 -6
- package/dist/commands/import.js.map +1 -1
- package/dist/company-choices.d.ts +2 -1
- package/dist/company-choices.d.ts.map +1 -1
- package/dist/company-choices.js +2 -1
- package/dist/company-choices.js.map +1 -1
- package/dist/inventory.d.ts.map +1 -1
- package/dist/inventory.js +8 -0
- package/dist/inventory.js.map +1 -1
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +5 -2
- package/dist/output.js.map +1 -1
- package/dist/pack/certification.js +1 -1
- package/dist/pack/certification.js.map +1 -1
- package/dist/pack/compile.js +10 -2
- package/dist/pack/compile.js.map +1 -1
- package/dist/pack/describe.d.ts +3 -1
- package/dist/pack/describe.d.ts.map +1 -1
- package/dist/pack/describe.js +3 -2
- package/dist/pack/describe.js.map +1 -1
- package/dist/pack/read.d.ts +7 -0
- package/dist/pack/read.d.ts.map +1 -1
- package/dist/pack/read.js +1 -0
- package/dist/pack/read.js.map +1 -1
- package/package.json +2 -2
- package/schema/output.1.json +105 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
-- Ekwo OS — a control account reads contact by contact.
|
|
2
|
+
--
|
|
3
|
+
-- An accountant reads a receivable or a payable account the way it is
|
|
4
|
+
-- audited: contact by contact, each with the balance brought forward, the
|
|
5
|
+
-- debits and credits of the period and the balance it closes on, the sum of
|
|
6
|
+
-- the contacts being the account itself, and the lines that name nobody
|
|
7
|
+
-- counted apart. Until now the schema gave the account (`trial_balance()`),
|
|
8
|
+
-- its lines (`general_ledger()`) and one contact across its accounts
|
|
9
|
+
-- (`contact_statement()`), and a screen that wanted the subsidiary ledger
|
|
10
|
+
-- added the lines up itself — one more reading of the ledger, free to
|
|
11
|
+
-- disagree with the others.
|
|
12
|
+
--
|
|
13
|
+
-- `subledger(company, from, to, accounts)` — one row per account and
|
|
14
|
+
-- contact: the opening balance before `from`, the debit and the credit
|
|
15
|
+
-- of the period, the closing balance at `to`, the number of lines of
|
|
16
|
+
-- the period and the number of lines still open today; and one row per
|
|
17
|
+
-- account for the lines that name no contact, `contact_id` null.
|
|
18
|
+
--
|
|
19
|
+
-- `general_ledger(company, from, to, accounts, contacts)` — a fifth
|
|
20
|
+
-- argument, `p_contact_ids`, keeps the lines of those contacts only,
|
|
21
|
+
-- each contact's lines with the balance it carried forward on the
|
|
22
|
+
-- account and its own running balance: the detail of a row of the
|
|
23
|
+
-- subsidiary ledger, from the function that already reads the lines.
|
|
24
|
+
--
|
|
25
|
+
-- **The accounts are told by their type, not by their code.** Left null,
|
|
26
|
+
-- `p_account_ids` reads every account of type `asset_receivable` or
|
|
27
|
+
-- `liability_payable` — the types the pack gives the accounts, the ones
|
|
28
|
+
-- `aged_balance()` and `contact_statement()` read — so the same call works on
|
|
29
|
+
-- every chart. (`accounts.internal_group` says asset or liability, which is
|
|
30
|
+
-- not enough to tell a receivable from a bank account.) Named, the accounts
|
|
31
|
+
-- are read whatever their type: a subsidiary ledger of an employee account or
|
|
32
|
+
-- of a suspense account is the same reading.
|
|
33
|
+
--
|
|
34
|
+
-- **The rows add up to the trial balance.** The lines are the posted ones,
|
|
35
|
+
-- dated up to `to`, as `trial_balance()` reads them: per account, the opening
|
|
36
|
+
-- balances, the debits, the credits and the closing balances of the rows
|
|
37
|
+
-- sum to the account's own figures in `trial_balance()`, to the cent. A
|
|
38
|
+
-- contact with nothing brought forward, nothing in the period and nothing
|
|
39
|
+
-- open is left out; it adds nothing to the sums.
|
|
40
|
+
--
|
|
41
|
+
-- **The open items are today's.** `open_items` counts the lines dated up to
|
|
42
|
+
-- `to` whose matching has not settled them yet, below half a unit of the
|
|
43
|
+
-- currency being nothing, as `aged_balance()` counts them: the matching of a
|
|
44
|
+
-- line has no date, so a subsidiary ledger read for a past period says what
|
|
45
|
+
-- of it is still open now, as `contact_statement()` does.
|
|
46
|
+
--
|
|
47
|
+
-- **Security.** `subledger()` is `security invoker`, refused by name to whoever
|
|
48
|
+
-- may not read the ledger of the company, exactly as `contact_statement()`:
|
|
49
|
+
-- unknown_company, not_allowed (entries.read), invalid_period, and
|
|
50
|
+
-- unknown_account for an account the company does not have.
|
|
51
|
+
-- `general_ledger()` stays what it was — SQL, under the caller's rights, with
|
|
52
|
+
-- no path of its own so that a report folds it into one plan.
|
|
53
|
+
--
|
|
54
|
+
-- **A call that names four arguments is answered as before.** The function is
|
|
55
|
+
-- dropped and created again because an argument is added; it defaults to
|
|
56
|
+
-- null, which reads every contact with the running balance of the account,
|
|
57
|
+
-- so the command line, the MCP server and the Cloud of the previous release
|
|
58
|
+
-- — and PostgREST resolving a call without `p_contact_ids` — get exactly what
|
|
59
|
+
-- they got. An overload beside the former signature would make every call
|
|
60
|
+
-- with four arguments or fewer ambiguous, which is why there is none.
|
|
61
|
+
|
|
62
|
+
-- ---------------------------------------------------------------------------
|
|
63
|
+
-- general_ledger — the lines of an account, of some contacts if asked
|
|
64
|
+
-- ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
drop function if exists ekwo.general_ledger(uuid, date, date, uuid[]);
|
|
67
|
+
|
|
68
|
+
create function ekwo.general_ledger(
|
|
69
|
+
p_company_id uuid,
|
|
70
|
+
p_from date,
|
|
71
|
+
p_to date,
|
|
72
|
+
p_account_ids uuid[] default null,
|
|
73
|
+
p_contact_ids uuid[] default null
|
|
74
|
+
)
|
|
75
|
+
returns table (
|
|
76
|
+
account_id uuid,
|
|
77
|
+
account_code text,
|
|
78
|
+
account_name text,
|
|
79
|
+
entry_line_id uuid,
|
|
80
|
+
entry_id uuid,
|
|
81
|
+
entry_number text,
|
|
82
|
+
entry_date date,
|
|
83
|
+
journal_code text,
|
|
84
|
+
reference text,
|
|
85
|
+
line_name text,
|
|
86
|
+
contact_id uuid,
|
|
87
|
+
contact_name text,
|
|
88
|
+
debit numeric,
|
|
89
|
+
credit numeric,
|
|
90
|
+
matching_number text,
|
|
91
|
+
running_balance numeric
|
|
92
|
+
)
|
|
93
|
+
language sql
|
|
94
|
+
stable
|
|
95
|
+
as $$
|
|
96
|
+
-- `party` is the contact a balance runs for: none when every contact is
|
|
97
|
+
-- read — one running balance per account, as always — and the line's
|
|
98
|
+
-- contact when some are asked for.
|
|
99
|
+
with opening as (
|
|
100
|
+
select l.account_id,
|
|
101
|
+
case when p_contact_ids is null then null else l.contact_id end as party,
|
|
102
|
+
sum(l.balance) as balance
|
|
103
|
+
from entry_lines l
|
|
104
|
+
join entries e on e.id = l.entry_id
|
|
105
|
+
where l.company_id = p_company_id
|
|
106
|
+
and e.state = 'posted'
|
|
107
|
+
and e.entry_date < p_from
|
|
108
|
+
and (p_contact_ids is null or l.contact_id = any (p_contact_ids))
|
|
109
|
+
group by 1, 2
|
|
110
|
+
),
|
|
111
|
+
movements as (
|
|
112
|
+
select l.id, l.account_id, l.entry_id, e.number, e.entry_date, j.code as journal_code,
|
|
113
|
+
e.reference, l.name, c.id as contact_id, c.name as contact_name, l.debit, l.credit, l.balance,
|
|
114
|
+
l.matching_number, l.sequence,
|
|
115
|
+
case when p_contact_ids is null then null else l.contact_id end as party
|
|
116
|
+
from entry_lines l
|
|
117
|
+
join entries e on e.id = l.entry_id
|
|
118
|
+
join journals j on j.id = e.journal_id
|
|
119
|
+
left join contacts c on c.id = l.contact_id
|
|
120
|
+
where l.company_id = p_company_id
|
|
121
|
+
and e.state = 'posted'
|
|
122
|
+
and e.entry_date between p_from and p_to
|
|
123
|
+
and (p_contact_ids is null or l.contact_id = any (p_contact_ids))
|
|
124
|
+
)
|
|
125
|
+
select a.id, a.code, a.name,
|
|
126
|
+
m.id, m.entry_id, m.number, m.entry_date, m.journal_code, m.reference,
|
|
127
|
+
m.name, m.contact_id, m.contact_name, m.debit, m.credit, m.matching_number,
|
|
128
|
+
(coalesce(o.balance, 0)
|
|
129
|
+
+ sum(m.balance) over (partition by a.id, m.party
|
|
130
|
+
order by m.entry_date, m.number, m.sequence, m.id
|
|
131
|
+
rows between unbounded preceding and current row))::numeric
|
|
132
|
+
from movements m
|
|
133
|
+
join accounts a on a.id = m.account_id
|
|
134
|
+
left join opening o on o.account_id = a.id and o.party is not distinct from m.party
|
|
135
|
+
where p_account_ids is null or a.id = any (p_account_ids)
|
|
136
|
+
-- Every contact read: the order of the ledger, as always. Some asked
|
|
137
|
+
-- for: each contact's lines together, by name, within the account.
|
|
138
|
+
order by a.code,
|
|
139
|
+
case when p_contact_ids is null then null else m.contact_name end,
|
|
140
|
+
m.party,
|
|
141
|
+
m.entry_date, m.number, m.sequence, m.id;
|
|
142
|
+
$$;
|
|
143
|
+
|
|
144
|
+
comment on function ekwo.general_ledger(uuid, date, date, uuid[], uuid[]) is
|
|
145
|
+
'Posted lines of a period per account, with the balance carried forward from before the period. Each line names its contact (contact_id, contact_name) when it has one. p_contact_ids keeps the lines of those contacts only, each contact with its own balance carried forward on the account and its own running balance — the detail of a row of subledger(); left null, every line, with one running balance per account.';
|
|
146
|
+
|
|
147
|
+
revoke execute on function ekwo.general_ledger(uuid, date, date, uuid[], uuid[]) from public, anon;
|
|
148
|
+
grant execute on function ekwo.general_ledger(uuid, date, date, uuid[], uuid[]) to authenticated, service_role;
|
|
149
|
+
|
|
150
|
+
-- ---------------------------------------------------------------------------
|
|
151
|
+
-- subledger — a control account, contact by contact
|
|
152
|
+
-- ---------------------------------------------------------------------------
|
|
153
|
+
|
|
154
|
+
create or replace function ekwo.subledger(
|
|
155
|
+
p_company_id uuid,
|
|
156
|
+
p_from date,
|
|
157
|
+
p_to date,
|
|
158
|
+
p_account_ids uuid[] default null
|
|
159
|
+
)
|
|
160
|
+
returns table (
|
|
161
|
+
account_id uuid,
|
|
162
|
+
account_code text,
|
|
163
|
+
account_name text,
|
|
164
|
+
account_type ekwo.account_type,
|
|
165
|
+
contact_id uuid,
|
|
166
|
+
contact_name text,
|
|
167
|
+
opening_balance numeric,
|
|
168
|
+
debit numeric,
|
|
169
|
+
credit numeric,
|
|
170
|
+
closing_balance numeric,
|
|
171
|
+
line_count integer,
|
|
172
|
+
open_items integer
|
|
173
|
+
)
|
|
174
|
+
language plpgsql
|
|
175
|
+
stable
|
|
176
|
+
security invoker
|
|
177
|
+
set search_path = ekwo, pg_temp
|
|
178
|
+
as $$
|
|
179
|
+
#variable_conflict use_column
|
|
180
|
+
declare
|
|
181
|
+
-- The sums are the ledger added up, so they are written with the decimals
|
|
182
|
+
-- of the currency it is kept in: a sum of nothing is 0.00, not 0.
|
|
183
|
+
v_round money_rounding;
|
|
184
|
+
v_unknown uuid;
|
|
185
|
+
begin
|
|
186
|
+
if not exists (select 1 from companies c where c.id = p_company_id) then
|
|
187
|
+
raise exception 'unknown_company: %', p_company_id;
|
|
188
|
+
end if;
|
|
189
|
+
|
|
190
|
+
if not is_installer() and not coalesce(has_capability(p_company_id, 'entries.read'), false) then
|
|
191
|
+
raise exception 'not_allowed: reading the subsidiary ledger needs entries.read'
|
|
192
|
+
using errcode = '42501';
|
|
193
|
+
end if;
|
|
194
|
+
|
|
195
|
+
if p_from is null or p_to is null or p_from > p_to then
|
|
196
|
+
raise exception 'invalid_period: a subsidiary ledger reads a period from its first day to its last, and % to % is not one',
|
|
197
|
+
coalesce(p_from::text, 'nothing'), coalesce(p_to::text, 'nothing')
|
|
198
|
+
using errcode = '22023';
|
|
199
|
+
end if;
|
|
200
|
+
|
|
201
|
+
select x.id into v_unknown
|
|
202
|
+
from unnest(p_account_ids) as x(id)
|
|
203
|
+
where not exists (select 1 from accounts a where a.id = x.id and a.company_id = p_company_id)
|
|
204
|
+
limit 1;
|
|
205
|
+
if v_unknown is not null then
|
|
206
|
+
raise exception 'unknown_account: this company has no account %', v_unknown
|
|
207
|
+
using hint = 'list_accounts (ekwo account list) gives the id of an account.';
|
|
208
|
+
end if;
|
|
209
|
+
|
|
210
|
+
v_round := rounding_of(p_company_id);
|
|
211
|
+
|
|
212
|
+
return query
|
|
213
|
+
with scope as (
|
|
214
|
+
select a.id, a.code, a.name, a.account_type
|
|
215
|
+
from accounts a
|
|
216
|
+
where a.company_id = p_company_id
|
|
217
|
+
and case when p_account_ids is null
|
|
218
|
+
then a.account_type in ('asset_receivable', 'liability_payable')
|
|
219
|
+
else a.id = any (p_account_ids) end
|
|
220
|
+
),
|
|
221
|
+
-- Posted, dated up to `to`: the lines `trial_balance()` adds up.
|
|
222
|
+
posted as (
|
|
223
|
+
select l.account_id, l.contact_id, e.entry_date, l.debit, l.credit, l.balance, l.matched_amount
|
|
224
|
+
from entry_lines l
|
|
225
|
+
join entries e on e.id = l.entry_id
|
|
226
|
+
join scope s on s.id = l.account_id
|
|
227
|
+
where l.company_id = p_company_id
|
|
228
|
+
and e.state = 'posted'
|
|
229
|
+
and e.entry_date <= p_to
|
|
230
|
+
),
|
|
231
|
+
by_contact as (
|
|
232
|
+
select p.account_id, p.contact_id,
|
|
233
|
+
coalesce(sum(p.balance) filter (where p.entry_date < p_from), 0) as opening,
|
|
234
|
+
coalesce(sum(p.debit) filter (where p.entry_date >= p_from), 0) as debit,
|
|
235
|
+
coalesce(sum(p.credit) filter (where p.entry_date >= p_from), 0) as credit,
|
|
236
|
+
coalesce(sum(p.balance), 0) as closing,
|
|
237
|
+
count(*) filter (where p.entry_date >= p_from) as lines,
|
|
238
|
+
-- Half a unit of the currency: below that, nothing is open.
|
|
239
|
+
count(*) filter (where abs(p.balance) - p.matched_amount > currency_unit(v_round) / 2) as open_lines
|
|
240
|
+
from posted p
|
|
241
|
+
group by p.account_id, p.contact_id
|
|
242
|
+
)
|
|
243
|
+
select s.id, s.code, s.name, s.account_type,
|
|
244
|
+
b.contact_id, c.name,
|
|
245
|
+
round_amount(b.opening, v_round),
|
|
246
|
+
round_amount(b.debit, v_round),
|
|
247
|
+
round_amount(b.credit, v_round),
|
|
248
|
+
round_amount(b.closing, v_round),
|
|
249
|
+
b.lines::integer,
|
|
250
|
+
b.open_lines::integer
|
|
251
|
+
from by_contact b
|
|
252
|
+
join scope s on s.id = b.account_id
|
|
253
|
+
left join contacts c on c.id = b.contact_id
|
|
254
|
+
where b.opening <> 0 or b.lines > 0 or b.open_lines > 0
|
|
255
|
+
order by s.code, c.name nulls last, b.contact_id nulls last;
|
|
256
|
+
end;
|
|
257
|
+
$$;
|
|
258
|
+
|
|
259
|
+
comment on function ekwo.subledger(uuid, date, date, uuid[]) is
|
|
260
|
+
'The subsidiary ledger of control accounts over a period: one row per account and contact with the balance brought forward before p_from (opening_balance), the debit and credit of the period, the closing balance at p_to, the number of lines of the period (line_count) and the number of lines dated up to p_to that a matching has not settled today (open_items); one row per account with contact_id null for the lines that name no contact. Debit minus credit, in the company currency. p_account_ids null reads every asset_receivable and liability_payable account. Posted entries only; per account, the rows sum to trial_balance() to the cent. A contact with nothing brought forward, no line in the period and nothing open is left out. Security invoker: entries.read. Refused by name: unknown_company, not_allowed, invalid_period, unknown_account.';
|
|
261
|
+
|
|
262
|
+
revoke execute on function ekwo.subledger(uuid, date, date, uuid[]) from public, anon;
|
|
263
|
+
grant execute on function ekwo.subledger(uuid, date, date, uuid[]) to authenticated, service_role;
|
|
264
|
+
|
|
265
|
+
-- The revoke every file of this directory ends with, and the Data API told
|
|
266
|
+
-- the functions are there.
|
|
267
|
+
revoke execute on all functions in schema ekwo from public;
|
|
268
|
+
|
|
269
|
+
notify pgrst, 'reload schema';
|
package/dist/assets/migrations/20261108120000_an_electronic_invoice_arrives_by_the_same_door.sql
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
-- Ekwo OS — an electronic invoice received arrives by the same door as a
|
|
2
|
+
-- page sent from a phone (decision 0091).
|
|
3
|
+
--
|
|
4
|
+
-- `receive_purchase_document()` is the one call a received purchase document
|
|
5
|
+
-- enters the books by: its pages are put in the private bucket `attachments`
|
|
6
|
+
-- under `<company_id>/inbox/<reference>/`, then announced, and the call
|
|
7
|
+
-- writes one draft purchase with no contact and no line, its pages as
|
|
8
|
+
-- attachments. Until now it took images and PDF only, the pages a phone
|
|
9
|
+
-- photographs.
|
|
10
|
+
--
|
|
11
|
+
-- A structured electronic invoice — a UBL or CII file received from an
|
|
12
|
+
-- e-invoicing network through an access point — is one XML page. Whoever
|
|
13
|
+
-- reads the inbox reads that page exactly, with the readers of the core, and
|
|
14
|
+
-- turns it into the draft's supplier, lines and taxes; the books only keep
|
|
15
|
+
-- it, as they keep a photograph. So the door now takes `application/xml` and
|
|
16
|
+
-- `text/xml` beside images and PDF, and nothing else changes: the same
|
|
17
|
+
-- rights (`documents.write`, for a person or a machine key alike), the same
|
|
18
|
+
-- paths, the same idempotency on the reference, the same refusals by name —
|
|
19
|
+
-- `inbox_file_type` now says « images, PDF and XML ».
|
|
20
|
+
--
|
|
21
|
+
-- The function is replaced whole, as `create or replace` asks; its body is
|
|
22
|
+
-- the one of `20261028152338_a_purchase_arrives_from_the_phone.sql` but for
|
|
23
|
+
-- the check of the type. Its privileges are restated, as every file of this
|
|
24
|
+
-- directory does.
|
|
25
|
+
|
|
26
|
+
create or replace function receive_purchase_document(
|
|
27
|
+
p_company_id uuid,
|
|
28
|
+
p_reference text,
|
|
29
|
+
p_taken_at timestamptz,
|
|
30
|
+
p_files jsonb
|
|
31
|
+
)
|
|
32
|
+
returns uuid
|
|
33
|
+
language plpgsql
|
|
34
|
+
security invoker
|
|
35
|
+
set search_path = ekwo, pg_temp
|
|
36
|
+
as $$
|
|
37
|
+
declare
|
|
38
|
+
v_folder text;
|
|
39
|
+
v_document documents;
|
|
40
|
+
v_currency char(3);
|
|
41
|
+
v_zone text;
|
|
42
|
+
v_file record;
|
|
43
|
+
v_path text;
|
|
44
|
+
v_type text;
|
|
45
|
+
v_name text;
|
|
46
|
+
v_size bigint;
|
|
47
|
+
v_found boolean;
|
|
48
|
+
begin
|
|
49
|
+
if p_company_id is null or not may_know_of_company(p_company_id) then
|
|
50
|
+
raise exception 'unknown_company: %', p_company_id using errcode = 'no_data_found';
|
|
51
|
+
end if;
|
|
52
|
+
if not has_capability(p_company_id, 'documents.write') then
|
|
53
|
+
raise exception 'not_allowed: adding a purchase to this company needs documents.write'
|
|
54
|
+
using errcode = '42501';
|
|
55
|
+
end if;
|
|
56
|
+
|
|
57
|
+
if p_reference is null or p_reference <> btrim(p_reference) or length(p_reference) not between 1 and 200
|
|
58
|
+
or p_reference ~ '[/\\]' or p_reference in ('.', '..') then
|
|
59
|
+
raise exception 'inbox_reference_invalid: the reference of a document sent to the inbox is one to 200 characters, without a slash or surrounding spaces'
|
|
60
|
+
using errcode = '22023';
|
|
61
|
+
end if;
|
|
62
|
+
if p_files is null or jsonb_typeof(p_files) <> 'array' or jsonb_array_length(p_files) = 0 then
|
|
63
|
+
raise exception 'inbox_no_files: a document sent to the inbox carries at least one page, as a JSON array of {storage_path, file_name, mime_type, byte_size}'
|
|
64
|
+
using errcode = '22023';
|
|
65
|
+
end if;
|
|
66
|
+
|
|
67
|
+
-- Every page is checked before anything is written.
|
|
68
|
+
v_folder := p_company_id::text || '/inbox/' || p_reference || '/';
|
|
69
|
+
for v_file in select value as page, ordinality as n from jsonb_array_elements(p_files) with ordinality loop
|
|
70
|
+
if jsonb_typeof(v_file.page) <> 'object' then
|
|
71
|
+
raise exception 'inbox_no_files: page % is not a JSON object', v_file.n using errcode = '22023';
|
|
72
|
+
end if;
|
|
73
|
+
v_path := v_file.page ->> 'storage_path';
|
|
74
|
+
if v_path is null or left(v_path, length(v_folder)) <> v_folder or length(v_path) = length(v_folder)
|
|
75
|
+
or right(v_path, 1) = '/' or v_path like '%/../%' or v_path like '%/./%' or v_path ~ '//' then
|
|
76
|
+
raise exception 'inbox_path_outside: page % is stored at %, which is not a file under %', v_file.n, coalesce(v_path, 'no path'), v_folder
|
|
77
|
+
using errcode = '22023';
|
|
78
|
+
end if;
|
|
79
|
+
v_type := lower(coalesce(v_file.page ->> 'mime_type', ''));
|
|
80
|
+
if v_type !~ '^image/[a-z0-9][a-z0-9.+-]*$' and v_type not in ('application/pdf', 'application/xml', 'text/xml') then
|
|
81
|
+
raise exception 'inbox_file_type: page % is %, and the inbox takes images, PDF and XML', v_file.n, coalesce(nullif(v_type, ''), 'of no type')
|
|
82
|
+
using errcode = '22023';
|
|
83
|
+
end if;
|
|
84
|
+
if v_file.page ? 'byte_size' and jsonb_typeof(v_file.page -> 'byte_size') <> 'null'
|
|
85
|
+
and (jsonb_typeof(v_file.page -> 'byte_size') <> 'number' or (v_file.page ->> 'byte_size')::numeric < 0
|
|
86
|
+
or (v_file.page ->> 'byte_size')::numeric <> trunc((v_file.page ->> 'byte_size')::numeric)) then
|
|
87
|
+
raise exception 'inbox_no_files: page % gives a byte_size that is not a whole number of bytes', v_file.n
|
|
88
|
+
using errcode = '22023';
|
|
89
|
+
end if;
|
|
90
|
+
-- Where the installation has Supabase Storage, the page is there.
|
|
91
|
+
if to_regclass('storage.objects') is not null then
|
|
92
|
+
execute 'select exists (select 1 from storage.objects where bucket_id = $1 and name = $2)'
|
|
93
|
+
into v_found using 'attachments', v_path;
|
|
94
|
+
if not v_found then
|
|
95
|
+
raise exception 'inbox_file_missing: page % is not in the bucket attachments at %. Upload it first, then announce it.', v_file.n, v_path
|
|
96
|
+
using errcode = 'no_data_found';
|
|
97
|
+
end if;
|
|
98
|
+
end if;
|
|
99
|
+
end loop;
|
|
100
|
+
|
|
101
|
+
select * into v_document from documents
|
|
102
|
+
where company_id = p_company_id and client_ref = p_reference;
|
|
103
|
+
|
|
104
|
+
if found and v_document.doc_type not in ('purchase_invoice', 'purchase_credit_note') then
|
|
105
|
+
raise exception 'inbox_reference_taken: the reference % already names another kind of document in this company', p_reference
|
|
106
|
+
using errcode = '23505';
|
|
107
|
+
end if;
|
|
108
|
+
|
|
109
|
+
if not found then
|
|
110
|
+
select c.currency_code into v_currency from companies c where c.id = p_company_id;
|
|
111
|
+
-- The day it was taken, where the person is: their own time zone when
|
|
112
|
+
-- they set one, else the day of the instant as it was sent.
|
|
113
|
+
select p.timezone into v_zone from user_preferences p
|
|
114
|
+
where p.user_id = auth.uid() and p.timezone in (select name from pg_timezone_names);
|
|
115
|
+
insert into documents (company_id, doc_type, contact_id, document_date, currency_code, client_ref)
|
|
116
|
+
values (p_company_id, 'purchase_invoice', null,
|
|
117
|
+
(coalesce(p_taken_at, now()) at time zone coalesce(v_zone, 'UTC'))::date,
|
|
118
|
+
v_currency, p_reference)
|
|
119
|
+
returning * into v_document;
|
|
120
|
+
end if;
|
|
121
|
+
|
|
122
|
+
-- The pages it does not hold yet, in the order they were sent.
|
|
123
|
+
for v_file in select value as page, ordinality as n from jsonb_array_elements(p_files) with ordinality loop
|
|
124
|
+
v_path := v_file.page ->> 'storage_path';
|
|
125
|
+
continue when exists (select 1 from attachments a
|
|
126
|
+
where a.entity_type = 'document' and a.entity_id = v_document.id
|
|
127
|
+
and a.storage_path = v_path);
|
|
128
|
+
v_name := nullif(btrim(coalesce(v_file.page ->> 'file_name', '')), '');
|
|
129
|
+
v_size := case when jsonb_typeof(v_file.page -> 'byte_size') = 'number' then (v_file.page ->> 'byte_size')::bigint end;
|
|
130
|
+
insert into attachments (company_id, entity_type, entity_id, file_name, mime_type, byte_size, storage_path, created_at)
|
|
131
|
+
values (p_company_id, 'document', v_document.id,
|
|
132
|
+
coalesce(v_name, regexp_replace(v_path, '^.*/', '')),
|
|
133
|
+
lower(v_file.page ->> 'mime_type'), v_size, v_path, clock_timestamp());
|
|
134
|
+
end loop;
|
|
135
|
+
|
|
136
|
+
return v_document.id;
|
|
137
|
+
end;
|
|
138
|
+
$$;
|
|
139
|
+
|
|
140
|
+
comment on function receive_purchase_document(uuid, text, timestamptz, jsonb) is
|
|
141
|
+
'A purchase document sent to the inbox of a company — pages from a phone, or the XML file of an electronic invoice received: one draft purchase_invoice, client_ref the reference its sender gave it, dated the day it was taken (the caller''s time zone when they set one), in the company''s currency, with no contact and no line yet, and one attachments row per page on it, in the order given. p_files is [{storage_path, file_name, mime_type, byte_size}]; every path is a file under <company_id>/inbox/<reference>/ of the bucket attachments, and an object there where the installation has Storage (inbox_path_outside, inbox_file_missing); every type an image, a PDF, or XML — the file of an electronic invoice received (inbox_file_type). Needs documents.write (not_allowed, 42501). Idempotent on (company, reference): a second call returns the same document and adds only the pages it does not hold, by path. Security invoker. Decision 0091.';
|
|
142
|
+
|
|
143
|
+
revoke execute on function receive_purchase_document(uuid, text, timestamptz, jsonb) from public, anon;
|
|
144
|
+
grant execute on function receive_purchase_document(uuid, text, timestamptz, jsonb) to authenticated, service_role;
|
|
145
|
+
|
|
146
|
+
-- `create or replace` keeps the privileges the function already had, and the
|
|
147
|
+
-- revoke is what every file of this directory ends with.
|
|
148
|
+
revoke execute on all functions in schema ekwo from public;
|
|
149
|
+
|
|
150
|
+
notify pgrst, 'reload schema';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
-- Ekwo OS — a bank account keeps the profile of its CSV statements.
|
|
2
|
+
--
|
|
3
|
+
-- A statement exported as CSV says nothing about its own lay-out: which
|
|
4
|
+
-- column is the date, the amount, the balance, how a date and a number are
|
|
5
|
+
-- written. `@ekwo-ai/csv-statement` reads it with a profile that says so, and
|
|
6
|
+
-- until now every caller had to give that profile at every import, so each
|
|
7
|
+
-- surface kept it where it could — a file beside the exports, the memory of
|
|
8
|
+
-- one person in one browser.
|
|
9
|
+
--
|
|
10
|
+
-- The lay-out belongs to the bank's export, so it belongs to the account the
|
|
11
|
+
-- file is of. `bank_accounts.statement_profile` holds it: the
|
|
12
|
+
-- `CsvStatementProfile` of `@ekwo-ai/csv-statement`, as JSON, as that package
|
|
13
|
+
-- describes it. The core writes it when an import given a profile succeeds,
|
|
14
|
+
-- and reads it when a CSV is imported without one; a person or an agent may
|
|
15
|
+
-- also set or clear it on the account. Null is an account whose CSV lay-out
|
|
16
|
+
-- nobody has described yet, which is every account until somebody does.
|
|
17
|
+
--
|
|
18
|
+
-- Additive: one nullable column, no default, no check — the reader checks a
|
|
19
|
+
-- profile when it reads a file with it. The policies of `bank_accounts` apply
|
|
20
|
+
-- to it as they apply to the rest of the row.
|
|
21
|
+
|
|
22
|
+
alter table bank_accounts
|
|
23
|
+
add column if not exists statement_profile jsonb;
|
|
24
|
+
|
|
25
|
+
comment on column bank_accounts.statement_profile is
|
|
26
|
+
'How the CSV statements of this account are laid out: the CsvStatementProfile of @ekwo-ai/csv-statement (delimiter, date format, decimal mark, which column says what). Written by the core when a CSV import given a profile succeeds, or set on the account; read when a CSV is imported without one. Optional: null until somebody describes the lay-out, and never used by the other statement formats.';
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
-- Ekwo OS — the socle's last policies ask once per statement, not once per row.
|
|
2
|
+
--
|
|
3
|
+
-- `20260918141627_a_policy_asks_once.sql` rewrote every policy of the socle
|
|
4
|
+
-- whose `using` was exactly `has_capability(company_id, '<capability>')` into
|
|
5
|
+
--
|
|
6
|
+
-- using (company_id = any ((select companies_with_capability('<capability>'))::uuid[]))
|
|
7
|
+
--
|
|
8
|
+
-- — a sub-select that depends on no column, an InitPlan, run once per
|
|
9
|
+
-- statement — and left the policies that were not that shape. The modules
|
|
10
|
+
-- followed (`modules/*/supabase/migrations/*_policies_ask_once.sql`), and
|
|
11
|
+
-- `tests/policies_ask_once.test.ts` now refuses any `using` that calls a
|
|
12
|
+
-- function of the database with a column of the row. Seventeen policies of
|
|
13
|
+
-- the socle were left on its list. This file rewrites fifteen of them; the
|
|
14
|
+
-- two it leaves are named at the end, with the reason.
|
|
15
|
+
--
|
|
16
|
+
-- ---------------------------------------------------------------------------
|
|
17
|
+
-- The shapes, and what each becomes
|
|
18
|
+
--
|
|
19
|
+
-- **`is_company_member(company_id)`** had no "companies where" counterpart.
|
|
20
|
+
-- `companies_of_caller()` is that counterpart: the companies where
|
|
21
|
+
-- `is_company_member()` answers yes, as an array. It does not restate what
|
|
22
|
+
-- membership is. It lists the only companies where the answer can be yes —
|
|
23
|
+
-- those the caller is a member of, and the one a presented machine key
|
|
24
|
+
-- belongs to — and asks `is_company_member()` about each, exactly as
|
|
25
|
+
-- `companies_with_capability()` lists the same candidates and asks
|
|
26
|
+
-- `has_capability()`. A change to what membership means changes both.
|
|
27
|
+
-- Five select policies read only that (`company_filing_periods`,
|
|
28
|
+
-- `company_modules`, `company_tax_regimes`, `company_vat_schemes`,
|
|
29
|
+
-- `matching_settings`), and two more combine it with something else
|
|
30
|
+
-- (`companies`, `company_members`).
|
|
31
|
+
--
|
|
32
|
+
-- **A term that depends on no column** — `is_instance_admin()` — stays a
|
|
33
|
+
-- term, `or`-ed with the InitPlan form. It is written `(select
|
|
34
|
+
-- is_instance_admin())`, which is the same boolean asked once per statement
|
|
35
|
+
-- rather than once per row a scan visits: it is security definer and probes
|
|
36
|
+
-- an index each time it is called. The `or` is unchanged.
|
|
37
|
+
--
|
|
38
|
+
-- **`user_id = auth.uid()`** and **`uploaded_by = auth.uid()`** are kept as
|
|
39
|
+
-- they are: `auth.uid()` takes no column, reads the claims of the session and
|
|
40
|
+
-- no table, and the inventory does not count it as a per-row question.
|
|
41
|
+
--
|
|
42
|
+
-- **A parent row in a correlated sub-select** — `exists (select 1 from
|
|
43
|
+
-- journals j where j.id = journal_sequences.journal_id and
|
|
44
|
+
-- has_capability(j.company_id, …))` — becomes
|
|
45
|
+
--
|
|
46
|
+
-- journal_id in (select j.id from journals j
|
|
47
|
+
-- where j.company_id = any ((select companies_with_capability(…))::uuid[]))
|
|
48
|
+
--
|
|
49
|
+
-- The sub-select no longer refers to the row: Postgres runs it once, hashes
|
|
50
|
+
-- the ids it returns, and probes the hash for each row (a "hashed SubPlan"),
|
|
51
|
+
-- with `companies_with_capability()` an InitPlan inside it. Postgres 17
|
|
52
|
+
-- already turned the `exists` into a hashed sub-plan, but that sub-plan
|
|
53
|
+
-- called `has_capability()` for every row of the parent it read — every
|
|
54
|
+
-- journal, every filing of every company the caller is on; it now calls
|
|
55
|
+
-- nothing per row. The parent table
|
|
56
|
+
-- is read under its own policies in both forms, as the invoker, so a row of
|
|
57
|
+
-- the parent the caller may not see admits nothing before or after. It
|
|
58
|
+
-- applies to `journal_sequences`, `tax_filing_boxes` (read and write) and
|
|
59
|
+
-- `tax_filing_deposits`.
|
|
60
|
+
--
|
|
61
|
+
-- **The rest of the `has_capability()` policies** that an `or` or an `and`
|
|
62
|
+
-- kept out of the first rewrite — `api_keys`, `companies` (update),
|
|
63
|
+
-- `company_invitations`, `attachments_select_own_deposit` — get the same
|
|
64
|
+
-- form as the first rewrite, the other term carried over.
|
|
65
|
+
--
|
|
66
|
+
-- `with check` is left as it was everywhere: it is evaluated once per row
|
|
67
|
+
-- written, which is what a check is for, and `alter policy … using (…)`
|
|
68
|
+
-- does not touch it.
|
|
69
|
+
--
|
|
70
|
+
-- **Rights are unchanged.** `companies_with_capability(c)` contains a company
|
|
71
|
+
-- exactly when `has_capability(company, c)` is true —
|
|
72
|
+
-- `tests/capabilities.test.ts` compares the two over every preset and a
|
|
73
|
+
-- machine key — and `companies_of_caller()` exactly when
|
|
74
|
+
-- `is_company_member(company)` is true. `x = any (array)` is false, never
|
|
75
|
+
-- null, when the array is empty, and `in (…)` over a set of ids is true
|
|
76
|
+
-- exactly when the correlated `exists` was. `tests/socle_policies_ask_once.test.ts`
|
|
77
|
+
-- builds the database without this file, records the rows a member, a
|
|
78
|
+
-- member who holds no capability, a member of another company, a person of
|
|
79
|
+
-- no company, an instance administrator and two machine keys read, update
|
|
80
|
+
-- and delete in every table touched here, applies this file, and finds the
|
|
81
|
+
-- same; it also reads the plan of each table as a member.
|
|
82
|
+
--
|
|
83
|
+
-- ---------------------------------------------------------------------------
|
|
84
|
+
-- Left as they are, and why
|
|
85
|
+
--
|
|
86
|
+
-- `attachments_select` and `attachments_write` choose the capability with
|
|
87
|
+
-- `case when entity_type = 'tax_filing' or attachment_is_filing_deposit_file(id)`.
|
|
88
|
+
-- That function is a security definer that answers about one attachment —
|
|
89
|
+
-- deliberately, for callers who may not read the deposits (decision 0065) —
|
|
90
|
+
-- and it has no set-returning counterpart; writing it as a sub-select of the
|
|
91
|
+
-- policy would read the deposits as the invoker, which is a different right.
|
|
92
|
+
-- Both stay on the list of `tests/policies_ask_once.test.ts`; their two
|
|
93
|
+
-- branches already compare `company_id` against an InitPlan.
|
|
94
|
+
|
|
95
|
+
create or replace function companies_of_caller()
|
|
96
|
+
returns uuid[]
|
|
97
|
+
language sql
|
|
98
|
+
stable
|
|
99
|
+
security definer
|
|
100
|
+
set search_path = ekwo, pg_temp
|
|
101
|
+
as $$
|
|
102
|
+
select coalesce(array_agg(x.company_id), '{}'::uuid[])
|
|
103
|
+
from (
|
|
104
|
+
select m.company_id
|
|
105
|
+
from company_members m
|
|
106
|
+
where m.user_id = auth.uid()
|
|
107
|
+
union
|
|
108
|
+
select k.company_id
|
|
109
|
+
from api_keys k
|
|
110
|
+
where k.key_hash = nullif(current_setting('ekwo.api_key', true), '')
|
|
111
|
+
) x
|
|
112
|
+
where is_company_member(x.company_id);
|
|
113
|
+
$$;
|
|
114
|
+
|
|
115
|
+
comment on function companies_of_caller() is
|
|
116
|
+
'The companies the current caller is on — the question is_company_member() answers, asked once for all of them: a member of it, or the holder of a machine key that still reaches something there. It is what a row level security policy compares company_id against, inside a sub-select, so that the answer is worked out once per statement instead of once per row. The counterpart of companies_with_capability().';
|
|
117
|
+
|
|
118
|
+
revoke execute on function companies_of_caller() from public, anon;
|
|
119
|
+
grant execute on function companies_of_caller() to authenticated, service_role;
|
|
120
|
+
|
|
121
|
+
-- is_company_member(), and `or is_instance_admin()` / `auth.uid()`.
|
|
122
|
+
alter policy companies_select on companies
|
|
123
|
+
using (id = any ((select companies_of_caller())::uuid[]) or (select is_instance_admin()));
|
|
124
|
+
|
|
125
|
+
alter policy company_members_select on company_members
|
|
126
|
+
using (user_id = auth.uid()
|
|
127
|
+
or company_id = any ((select companies_of_caller())::uuid[])
|
|
128
|
+
or (select is_instance_admin()));
|
|
129
|
+
|
|
130
|
+
alter policy company_filing_periods_select on company_filing_periods
|
|
131
|
+
using (company_id = any ((select companies_of_caller())::uuid[]));
|
|
132
|
+
|
|
133
|
+
alter policy company_modules_select on company_modules
|
|
134
|
+
using (company_id = any ((select companies_of_caller())::uuid[]));
|
|
135
|
+
|
|
136
|
+
alter policy company_tax_regimes_select on company_tax_regimes
|
|
137
|
+
using (company_id = any ((select companies_of_caller())::uuid[]));
|
|
138
|
+
|
|
139
|
+
alter policy company_vat_schemes_select on company_vat_schemes
|
|
140
|
+
using (company_id = any ((select companies_of_caller())::uuid[]));
|
|
141
|
+
|
|
142
|
+
alter policy matching_settings_select on matching_settings
|
|
143
|
+
using (company_id = any ((select companies_of_caller())::uuid[]));
|
|
144
|
+
|
|
145
|
+
-- has_capability(), with another term.
|
|
146
|
+
alter policy companies_update on companies
|
|
147
|
+
using (id = any ((select companies_with_capability('company.write'))::uuid[]) or (select is_instance_admin()));
|
|
148
|
+
|
|
149
|
+
alter policy api_keys_select on api_keys
|
|
150
|
+
using (company_id = any ((select companies_with_capability('members.manage'))::uuid[]) or (select is_instance_admin()));
|
|
151
|
+
|
|
152
|
+
alter policy company_invitations_select on company_invitations
|
|
153
|
+
using (company_id = any ((select companies_with_capability('members.manage'))::uuid[]) or (select is_instance_admin()));
|
|
154
|
+
|
|
155
|
+
alter policy attachments_select_own_deposit on attachments
|
|
156
|
+
using (uploaded_by = auth.uid()
|
|
157
|
+
and company_id = any ((select companies_with_capability('documents.deposit'))::uuid[]));
|
|
158
|
+
|
|
159
|
+
-- A parent row: the set of the parent's ids, worked out once.
|
|
160
|
+
alter policy journal_sequences_select on journal_sequences
|
|
161
|
+
using (journal_id in (select j.id from journals j
|
|
162
|
+
where j.company_id = any ((select companies_with_capability('entries.read'))::uuid[])));
|
|
163
|
+
|
|
164
|
+
alter policy tax_filing_boxes_select on tax_filing_boxes
|
|
165
|
+
using (filing_id in (select f.id from tax_filings f
|
|
166
|
+
where f.company_id = any ((select companies_with_capability('filings.read'))::uuid[])));
|
|
167
|
+
|
|
168
|
+
alter policy tax_filing_boxes_write on tax_filing_boxes
|
|
169
|
+
using (filing_id in (select f.id from tax_filings f
|
|
170
|
+
where f.company_id = any ((select companies_with_capability('filings.write'))::uuid[])));
|
|
171
|
+
|
|
172
|
+
alter policy tax_filing_deposits_select on tax_filing_deposits
|
|
173
|
+
using (filing_id in (select f.id from tax_filings f
|
|
174
|
+
where f.company_id = any ((select companies_with_capability('filings.read'))::uuid[])));
|
|
175
|
+
|
|
176
|
+
-- The revoke every file of this directory ends with.
|
|
177
|
+
revoke execute on all functions in schema ekwo from public;
|