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.
Files changed (86) hide show
  1. package/README.md +8 -1
  2. package/dist/assets/expected-objects.json +324 -4
  3. package/dist/assets/migrations/20261107100000_a_control_account_reads_contact_by_contact.sql +269 -0
  4. package/dist/assets/migrations/20261108120000_an_electronic_invoice_arrives_by_the_same_door.sql +150 -0
  5. package/dist/assets/migrations/20261108152128_a_bank_account_keeps_the_profile_of_its_csv_statements.sql +26 -0
  6. package/dist/assets/migrations/20261110124117_the_socle_policies_ask_once.sql +177 -0
  7. package/dist/assets/migrations/20261115070000_a_financial_year_opens_on_any_month.sql +86 -0
  8. package/dist/assets/migrations/20261115080000_the_portfolio_reads_what_is_overdue.sql +119 -0
  9. package/dist/assets/migrations/20261115100000_a_declaration_has_a_short_name.sql +256 -0
  10. package/dist/assets/migrations/20261118144012_a_quote_carries_its_answer.sql +397 -0
  11. package/dist/assets/migrations/20261118170000_schema_version_0_26_0.sql +49 -0
  12. package/dist/assets/modules/budgets/supabase/migrations/20261109153212_budgets_policies_ask_once.sql +93 -0
  13. package/dist/assets/modules/consolidation/supabase/migrations/20261109154107_consolidation_policies_ask_once.sql +93 -0
  14. package/dist/assets/modules/contracts/supabase/migrations/20261109154842_contracts_policies_ask_once.sql +93 -0
  15. package/dist/assets/modules/corporate-tax/supabase/migrations/20261109160318_tax_policies_ask_once.sql +93 -0
  16. package/dist/assets/modules/crm/supabase/migrations/20261109120000_crm_reads_hold_at_volume.sql +95 -0
  17. package/dist/assets/modules/crm/supabase/migrations/20261115090000_crm_contact_vcard.sql +159 -0
  18. package/dist/assets/modules/crm/supabase/migrations/20261118145000_crm_deal_documents.sql +569 -0
  19. package/dist/assets/modules/einvoicing/supabase/migrations/20261109155021_einvoicing_policies_ask_once.sql +93 -0
  20. package/dist/assets/modules/fixed-assets/supabase/migrations/20261109155512_assets_policies_ask_once.sql +93 -0
  21. package/dist/assets/seed/00_currencies.sql +5 -0
  22. package/dist/assets/seed/00_territories.sql +57 -0
  23. package/dist/assets/seed/10_pack_be.sql +1 -1
  24. package/dist/assets/seed/11_pack_fr.sql +1 -1
  25. package/dist/assets/seed/13_pack_ee.sql +9 -4
  26. package/dist/assets/seed/15_pack_us.sql +1 -1
  27. package/dist/assets/seed/220_pack_ad.sql +666 -0
  28. package/dist/assets/seed/224_pack_pw.sql +642 -0
  29. package/dist/assets/seed/226_pack_sb.sql +614 -0
  30. package/dist/assets/seed/43_pack_pt.sql +14 -5
  31. package/dist/assets/seed/500_pack_nr.sql +468 -0
  32. package/dist/assets/seed/61_pack_se.sql +9 -5
  33. package/dist/assets/seed/64_pack_pl.sql +5 -2
  34. package/dist/assets/seed/65_pack_dk.sql +7 -4
  35. package/dist/assets/seed/67_pack_fi.sql +7 -2
  36. package/dist/assets/seed/68_pack_cz.sql +8 -2
  37. package/dist/assets/seed/70_pack_hu.sql +11 -7
  38. package/dist/assets/seed/72_pack_ro.sql +11 -7
  39. package/dist/cli.d.ts +1 -1
  40. package/dist/cli.d.ts.map +1 -1
  41. package/dist/cli.js +44 -4
  42. package/dist/cli.js.map +1 -1
  43. package/dist/commands/bank.d.ts +16 -0
  44. package/dist/commands/bank.d.ts.map +1 -0
  45. package/dist/commands/bank.js +82 -0
  46. package/dist/commands/bank.js.map +1 -0
  47. package/dist/commands/close.d.ts +33 -0
  48. package/dist/commands/close.d.ts.map +1 -0
  49. package/dist/commands/close.js +213 -0
  50. package/dist/commands/close.js.map +1 -0
  51. package/dist/commands/crm.d.ts +5 -4
  52. package/dist/commands/crm.d.ts.map +1 -1
  53. package/dist/commands/crm.js +70 -5
  54. package/dist/commands/crm.js.map +1 -1
  55. package/dist/commands/document.d.ts +2 -1
  56. package/dist/commands/document.d.ts.map +1 -1
  57. package/dist/commands/document.js +58 -4
  58. package/dist/commands/document.js.map +1 -1
  59. package/dist/commands/import.d.ts +3 -0
  60. package/dist/commands/import.d.ts.map +1 -1
  61. package/dist/commands/import.js +81 -6
  62. package/dist/commands/import.js.map +1 -1
  63. package/dist/company-choices.d.ts +2 -1
  64. package/dist/company-choices.d.ts.map +1 -1
  65. package/dist/company-choices.js +2 -1
  66. package/dist/company-choices.js.map +1 -1
  67. package/dist/inventory.d.ts.map +1 -1
  68. package/dist/inventory.js +8 -0
  69. package/dist/inventory.js.map +1 -1
  70. package/dist/output.d.ts.map +1 -1
  71. package/dist/output.js +5 -2
  72. package/dist/output.js.map +1 -1
  73. package/dist/pack/certification.js +1 -1
  74. package/dist/pack/certification.js.map +1 -1
  75. package/dist/pack/compile.js +10 -2
  76. package/dist/pack/compile.js.map +1 -1
  77. package/dist/pack/describe.d.ts +3 -1
  78. package/dist/pack/describe.d.ts.map +1 -1
  79. package/dist/pack/describe.js +3 -2
  80. package/dist/pack/describe.js.map +1 -1
  81. package/dist/pack/read.d.ts +7 -0
  82. package/dist/pack/read.d.ts.map +1 -1
  83. package/dist/pack/read.js +1 -0
  84. package/dist/pack/read.js.map +1 -1
  85. package/package.json +2 -2
  86. package/schema/output.1.json +105 -0
@@ -0,0 +1,86 @@
1
+ -- Ekwo OS — a financial year opens on any of the twelve months.
2
+ --
3
+ -- `country_defaults.fiscal_year_default` named four opening months —
4
+ -- `calendar`, `april`, `july`, `october` — where the law of most countries
5
+ -- names twelve. The United States lets a company end its year on the last
6
+ -- day of any month; so do many countries whose packs chose `calendar` as the
7
+ -- ordinary answer and wrote that the law leaves the choice open. A pack whose
8
+ -- country fixes another month (a year opening on 1 March, on 1 September)
9
+ -- had nothing to declare it with. The pack us named the gap.
10
+ --
11
+ -- The vocabulary becomes the twelve months: `calendar` keeps its meaning,
12
+ -- the year that opens on 1 January — every pack written so far says it, and
13
+ -- one month keeps one name — and the eleven others are named in English,
14
+ -- `february` to `december`. `april`, `july` and `october` mean what they
15
+ -- meant. Nothing a pack declared changes, and no year already opened moves:
16
+ -- `fiscal_years` holds what each company keeps, and this default is only
17
+ -- what a first year is offered.
18
+ --
19
+ -- **The reader and the check say the same list.** The constraint is dropped
20
+ -- and written again with the twelve names, and `fiscal_year_bounds()` reads
21
+ -- the month by its place in the same list, so a name the check admits is a
22
+ -- name the function opens on.
23
+ --
24
+ -- **Still out of reach:** the 52-53-week year, which does not open on the
25
+ -- first of a month at all. `fiscal_years` takes any two dates, and a company
26
+ -- that keeps one names them (`--fiscal-year-start`); a pack cannot yet
27
+ -- propose one.
28
+ --
29
+ -- Additive in what it accepts: every value admitted before is admitted
30
+ -- after, and the function keeps its signature, its grants and its errors.
31
+
32
+ alter table ekwo.country_defaults
33
+ drop constraint if exists country_defaults_fiscal_year_default_known;
34
+
35
+ alter table ekwo.country_defaults
36
+ add constraint country_defaults_fiscal_year_default_known
37
+ check (fiscal_year_default is null or fiscal_year_default in (
38
+ 'calendar', 'february', 'march', 'april', 'may', 'june',
39
+ 'july', 'august', 'september', 'october', 'november', 'december'
40
+ ));
41
+
42
+ comment on column ekwo.country_defaults.fiscal_year_default is
43
+ 'Month the financial year usually opens on: calendar (January), february, march, april, may, june, july, august, september, october, november or december. A default offered, never imposed — fiscal_years holds what a company actually keeps.';
44
+
45
+ create or replace function ekwo.fiscal_year_bounds(
46
+ p_country char(2),
47
+ p_year integer,
48
+ p_start date default null,
49
+ out start_date date,
50
+ out end_date date
51
+ )
52
+ language plpgsql
53
+ stable
54
+ as $$
55
+ declare
56
+ v_opening text;
57
+ v_month integer;
58
+ begin
59
+ if p_start is not null then
60
+ start_date := p_start;
61
+ else
62
+ select fiscal_year_default into v_opening
63
+ from ekwo.country_defaults where country = p_country;
64
+
65
+ if v_opening is null then
66
+ raise exception 'no_fiscal_year_default: the % pack declares no defaults.fiscal_year_default, so there is no month to open the first year on. Name the first day instead', p_country;
67
+ end if;
68
+
69
+ v_month := array_position(
70
+ array['calendar', 'february', 'march', 'april', 'may', 'june',
71
+ 'july', 'august', 'september', 'october', 'november', 'december'],
72
+ v_opening
73
+ );
74
+ if v_month is null then
75
+ raise exception 'unknown_fiscal_year_default: % is not one of calendar, february, march, april, may, june, july, august, september, october, november, december', v_opening;
76
+ end if;
77
+
78
+ start_date := make_date(p_year, v_month, 1);
79
+ end if;
80
+
81
+ end_date := (start_date + interval '1 year' - interval '1 day')::date;
82
+ end;
83
+ $$;
84
+
85
+ comment on function ekwo.fiscal_year_bounds(char, integer, date) is
86
+ 'The first and last day of a financial year opening in a given calendar year, on the month the country pack declares (calendar for January, or one of the eleven other months by name) — or on a day the caller names. Raises rather than assuming January.';
@@ -0,0 +1,119 @@
1
+ -- Ekwo OS — what customers and suppliers owe past their date, across the
2
+ -- portfolio, one row per company.
3
+ --
4
+ -- A firm asks two questions of all the books it keeps on a Monday morning:
5
+ -- which returns fall due (`portfolio_upcoming_filings()`), and who is paying
6
+ -- late, on either side. The second was asked company by company:
7
+ -- `aged_balance()` once for the customers and once for the suppliers of each
8
+ -- company, and the sums worked out by whoever called. `portfolio_overview()`
9
+ -- asks it once, with the same answer.
10
+ --
11
+ -- **Built on `aged_balance()`, never beside it** (decision 0040). Each company
12
+ -- of the portfolio is read through the per-company function in a lateral
13
+ -- join, for both groups, so "overdue" means here exactly what the ageing
14
+ -- shows: what is still open on a reconcilable receivable or payable account,
15
+ -- after the matching, past its due date on `p_on`. Nothing is recomputed: the
16
+ -- open amount is the ageing's `total`, the overdue amount its four buckets
17
+ -- after the due date, the amount over ninety days its last bucket. The
18
+ -- figures are the ageing's, already at the decimals of the company's
19
+ -- currency, and the currency is on the row: a portfolio holds companies that
20
+ -- keep their books in different currencies, and two of its rows are never
21
+ -- added together.
22
+ --
23
+ -- **The portfolio is the companies the caller holds `entries.read` on**, the
24
+ -- capability the ageing reads the ledger with. Decision 0040 filters the
25
+ -- calendar of returns on `filings.read`, the capability that reading needs,
26
+ -- because a company's row is visible to people its declarations are closed
27
+ -- to; the same reasoning, applied to the ledger, filters on `entries.read`.
28
+ -- A company whose ledger the caller may not read is absent, rather than
29
+ -- listed with nothing owed.
30
+ --
31
+ -- **Silence is not an output.** Every company of the portfolio is a row,
32
+ -- with zeros where nothing is open: a company left out would be
33
+ -- indistinguishable from one that owes nothing. A count says how many
34
+ -- parties are late on each side; the open lines that name nobody count as
35
+ -- one party, the way the ageing groups them.
36
+ --
37
+ -- `security invoker`: it reads what the caller could have read company by
38
+ -- company, through the same policies.
39
+
40
+ create function ekwo.portfolio_overview(
41
+ p_on date default current_date
42
+ )
43
+ returns table (
44
+ company_id uuid,
45
+ company_name text,
46
+ currency_code text,
47
+ customers_open numeric,
48
+ customers_overdue numeric,
49
+ customers_overdue_over_90 numeric,
50
+ customers_late integer,
51
+ suppliers_open numeric,
52
+ suppliers_overdue numeric,
53
+ suppliers_overdue_over_90 numeric,
54
+ suppliers_late integer
55
+ )
56
+ language plpgsql
57
+ stable
58
+ security invoker
59
+ set search_path = ekwo, pg_temp
60
+ as $$
61
+ begin
62
+ if p_on is null then
63
+ raise exception 'invalid_date: the day the portfolio is read on is required';
64
+ end if;
65
+
66
+ return query
67
+ with portfolio as (
68
+ select c.id, c.name, c.currency_code::text as currency_code
69
+ from companies c
70
+ where c.id = any (companies_with_capability('entries.read'))
71
+ ),
72
+ -- One row per party and side: the ageing groups by contact and account,
73
+ -- and a party late on two accounts is one late party.
74
+ parties as (
75
+ select p.id as company_id,
76
+ g.side,
77
+ a.contact_id,
78
+ sum(coalesce(a.total, 0)) as open_amount,
79
+ sum(coalesce(a.days_1_30, 0) + coalesce(a.days_31_60, 0)
80
+ + coalesce(a.days_61_90, 0) + coalesce(a.days_over_90, 0)) as overdue,
81
+ sum(coalesce(a.days_over_90, 0)) as over_90
82
+ from portfolio p
83
+ cross join (values ('receivable'), ('payable')) as g(side)
84
+ cross join lateral aged_balance(p.id, p_on, g.side) a
85
+ group by p.id, g.side, a.contact_id
86
+ ),
87
+ sides as (
88
+ select x.company_id,
89
+ x.side,
90
+ sum(x.open_amount) as open_amount,
91
+ sum(x.overdue) as overdue,
92
+ sum(x.over_90) as over_90,
93
+ (count(*) filter (where x.overdue > 0))::integer as late
94
+ from parties x
95
+ group by x.company_id, x.side
96
+ )
97
+ select p.id,
98
+ p.name,
99
+ p.currency_code,
100
+ coalesce(r.open_amount, 0),
101
+ coalesce(r.overdue, 0),
102
+ coalesce(r.over_90, 0),
103
+ coalesce(r.late, 0),
104
+ coalesce(s.open_amount, 0),
105
+ coalesce(s.overdue, 0),
106
+ coalesce(s.over_90, 0),
107
+ coalesce(s.late, 0)
108
+ from portfolio p
109
+ left join sides r on r.company_id = p.id and r.side = 'receivable'
110
+ left join sides s on s.company_id = p.id and s.side = 'payable'
111
+ order by p.name, p.id;
112
+ end;
113
+ $$;
114
+
115
+ comment on function ekwo.portfolio_overview(date) is
116
+ 'What customers and suppliers owe past their due date on a day, one row per company the caller holds entries.read on, and for no other: aged_balance() of each company, for both groups, summed. open is the ageing''s total, overdue the four buckets after the due date, overdue_over_90 the last one, late the number of parties with something overdue (the open lines that name nobody count as one). Amounts are in the company''s currency_code, never added across rows. Every company of the portfolio is a row, with zeros where nothing is open. Invoker: it reads what the caller could have read company by company.';
117
+
118
+ revoke execute on function ekwo.portfolio_overview(date) from public, anon;
119
+ grant execute on function ekwo.portfolio_overview(date) to authenticated, service_role;
@@ -0,0 +1,256 @@
1
+ -- Ekwo OS — a declaration has a short name, and the calendar says it.
2
+ --
3
+ -- A form of `tax_report_templates` has a code (`BE-VAT-PERIODIC`) and its
4
+ -- official name in the language of the country ("Déclaration périodique à la
5
+ -- TVA", "Déclaration de TVA 3310-CA3"). The code is for machines and the
6
+ -- official name is long: a screen that lists forty companies' deadlines
7
+ -- shows one or the other, and neither is how an accountant of the country
8
+ -- says the declaration out loud. `short_name` is that: a few words, in the
9
+ -- language of the country, declared by the pack where it knows them — read on
10
+ -- the portal or the notice of the administration, never made up.
11
+ --
12
+ -- **Optional, and null is the ordinary answer.** A pack that names no short
13
+ -- name compiles a seed that does not mention the column, so it still loads
14
+ -- on a schema from before this file, and a screen shows the official name
15
+ -- where the short one is null.
16
+ --
17
+ -- **The calendar says both.** `upcoming_filings()` and
18
+ -- `portfolio_upcoming_filings()` answer with two columns more, `report_name`
19
+ -- and `report_short_name`, read from the form of the company's fiscal
20
+ -- country (its other country where only that one carries the code, as the
21
+ -- forms of `upcoming_filings()` are found). They are added at the end of the
22
+ -- row, so a caller that read the columns by name or by position reads the
23
+ -- same values as before; the functions are dropped and created again
24
+ -- because the shape of their answer changes, with the same arguments, the
25
+ -- same body otherwise, the same comments completed, the same grants.
26
+
27
+ alter table ekwo.tax_report_templates
28
+ add column if not exists short_name text;
29
+
30
+ alter table ekwo.tax_report_templates
31
+ drop constraint if exists tax_report_templates_short_name_length;
32
+ alter table ekwo.tax_report_templates
33
+ add constraint tax_report_templates_short_name_length
34
+ check (short_name is null or char_length(btrim(short_name)) between 1 and 40);
35
+
36
+ comment on column ekwo.tax_report_templates.short_name is
37
+ 'How the accountants of the country name this declaration in a few words, in its language — what a calendar or a list of deadlines shows. Declared by the pack where it knows it, read on the portal or the notice of the administration; null where it does not, and a screen then shows `name`, the official one.';
38
+
39
+ -- ---------------------------------------------------------------------------
40
+ -- The two readers, dropped together: the portfolio reads the company's.
41
+ -- ---------------------------------------------------------------------------
42
+
43
+ drop function if exists ekwo.portfolio_upcoming_filings(date, date);
44
+ drop function if exists ekwo.upcoming_filings(uuid, date, date);
45
+
46
+ create function ekwo.upcoming_filings(
47
+ p_company_id uuid,
48
+ p_from date,
49
+ p_to date
50
+ )
51
+ returns table (
52
+ report_code text,
53
+ period_start date,
54
+ period_end date,
55
+ due_date date,
56
+ state tax_filing_state,
57
+ filing_id uuid,
58
+ instalment smallint,
59
+ report_name text,
60
+ report_short_name text
61
+ )
62
+ language sql
63
+ stable
64
+ security invoker
65
+ as $$
66
+ with forms as (
67
+ -- Every form this company has a cadence for, and the periodic return it
68
+ -- files even when nobody has written a cadence down for it.
69
+ select p.report_code, p.period as cadence
70
+ from company_filing_periods p
71
+ where p.company_id = p_company_id
72
+ union
73
+ select periodic_return_code(p_company_id),
74
+ coalesce(
75
+ (select p.period from company_filing_periods p
76
+ where p.company_id = p_company_id
77
+ and p.report_code = periodic_return_code(p_company_id)),
78
+ (select t.period_default from tax_report_templates t
79
+ join companies c on c.id = p_company_id
80
+ where t.code = periodic_return_code(p_company_id)
81
+ and t.country in (c.country, c.fiscal_country)))
82
+ where periodic_return_code(p_company_id) is not null
83
+ ),
84
+ periods as (
85
+ select f.report_code,
86
+ g.start::date as period_start,
87
+ (g.start + make_interval(months => declaration_period_months(f.cadence))
88
+ - interval '1 day')::date as period_end
89
+ from forms f
90
+ cross join lateral generate_series(
91
+ declaration_period_start(f.cadence, p_from)::timestamp,
92
+ p_to::timestamp,
93
+ make_interval(months => declaration_period_months(f.cadence))
94
+ ) as g(start)
95
+ where f.cadence is not null
96
+ ),
97
+ returns as (
98
+ select p.report_code,
99
+ p.period_start,
100
+ p.period_end,
101
+ filing_deadline(p_company_id, p.report_code, p.period_end) as due_date,
102
+ f.state,
103
+ f.id as filing_id,
104
+ null::smallint as instalment
105
+ from periods p
106
+ left join tax_filings f
107
+ on f.company_id = p_company_id
108
+ and f.report_code = p.report_code
109
+ and f.period_start = p.period_start
110
+ and f.period_end = p.period_end
111
+ and f.state <> 'superseded'
112
+ -- A company under a regime that replaces VAT for the whole period does not
113
+ -- file the periodic VAT return of that period.
114
+ where not (p.report_code is not distinct from periodic_return_code(p_company_id)
115
+ and coalesce('vat'::tax_kind = any ((turnover_tax_regime_at(p_company_id, p.period_start)).replaces), false)
116
+ and coalesce('vat'::tax_kind = any ((turnover_tax_regime_at(p_company_id, p.period_end)).replaces), false))
117
+ -- Nor a form its scheme does not file.
118
+ and vat_scheme_files(p_company_id, p.report_code, p.period_end)
119
+ ),
120
+ -- The instalments of every year the window touches, from the scheme the
121
+ -- company is on at the end of that year.
122
+ years as (
123
+ select g.y::date as year_start, (g.y + interval '1 year' - interval '1 day')::date as year_end
124
+ from generate_series(date_trunc('year', p_from), p_to::timestamp, interval '1 year') as g(y)
125
+ ),
126
+ instalments as (
127
+ select i.report_code,
128
+ i.due_month as period_start,
129
+ (i.due_month + interval '1 month' - interval '1 day')::date as period_end,
130
+ i.due_date,
131
+ null::tax_filing_state as state,
132
+ null::uuid as filing_id,
133
+ i.sequence as instalment
134
+ from years y
135
+ cross join lateral instalment_plan(p_company_id, y.year_start, y.year_end) i
136
+ where (i.due_month + interval '1 month' - interval '1 day')::date >= p_from
137
+ and i.due_month <= p_to
138
+ and coalesce(i.reason, '') <> 'below_minimum'
139
+ ),
140
+ listed as (
141
+ select * from returns
142
+ union all
143
+ select * from instalments
144
+ )
145
+ -- The names of the form, from the company's fiscal country first.
146
+ select l.report_code,
147
+ l.period_start,
148
+ l.period_end,
149
+ l.due_date,
150
+ l.state,
151
+ l.filing_id,
152
+ l.instalment,
153
+ n.name,
154
+ n.short_name
155
+ from listed l
156
+ left join lateral (
157
+ select t.name, t.short_name
158
+ from tax_report_templates t
159
+ join companies c on c.id = p_company_id
160
+ where t.code = l.report_code
161
+ and t.country in (c.fiscal_country, c.country)
162
+ order by (t.country = c.fiscal_country) desc
163
+ limit 1
164
+ ) n on true
165
+ order by 3, 1, 7 nulls first;
166
+ $$;
167
+
168
+ comment on function ekwo.upcoming_filings(uuid, date, date) is
169
+ 'What this company has to file and pay on account between two dates: the periods its cadences produce — any whole number of months, anchored on 1 January — the day each is due where the pack says, and the declaration already prepared or sent against it; and the instalments its scheme asks for, one row each with instalment set to its sequence and the month it falls due in as its period. report_name is the official name of the form and report_short_name how the accountants of the country name it, null where the pack does not say. The periodic return is left out of the periods a regime of turnover_tax_regimes that replaces VAT covers entirely, and a form is left out of the periods the company''s VAT scheme does not file it in. Read-only; sending the reminder is somebody else''s job, because a reminder needs a channel and somebody to operate it.';
170
+
171
+ revoke execute on function ekwo.upcoming_filings(uuid, date, date) from public, anon;
172
+ grant execute on function ekwo.upcoming_filings(uuid, date, date) to authenticated, service_role;
173
+
174
+ create function ekwo.portfolio_upcoming_filings(
175
+ p_from date,
176
+ p_to date
177
+ )
178
+ returns table (
179
+ company_id uuid,
180
+ company_name text,
181
+ report_code text,
182
+ period_start date,
183
+ period_end date,
184
+ due_date date,
185
+ state tax_filing_state,
186
+ filing_id uuid,
187
+ reason text,
188
+ instalment smallint,
189
+ report_name text,
190
+ report_short_name text
191
+ )
192
+ language sql
193
+ stable
194
+ security invoker
195
+ as $$
196
+ with portfolio as (
197
+ select c.id, c.name
198
+ from companies c
199
+ where has_capability(c.id, 'filings.read')
200
+ ),
201
+ -- A return is due in the month that follows its period, plus the days an
202
+ -- administration grants. So the periods that can fall due from `p_from` on
203
+ -- ended at most two months and that many days before it, and the calendar
204
+ -- of a company is asked for once, from there.
205
+ reach as (
206
+ select (p_from - interval '2 month'
207
+ - make_interval(days => coalesce(max(t.deadline_plus_days), 0)))::date as start
208
+ from tax_report_templates t
209
+ ),
210
+ listed as (
211
+ select p.id as company_id, u.*
212
+ from portfolio p
213
+ cross join reach r
214
+ cross join lateral upcoming_filings(p.id, r.start, p_to) u
215
+ where case
216
+ when u.due_date is not null then u.due_date between p_from and p_to
217
+ else u.period_end < p_to
218
+ and (date_trunc('month', u.period_end + interval '2 month')
219
+ - interval '1 day')::date >= p_from
220
+ end
221
+ )
222
+ select p.id,
223
+ p.name,
224
+ l.report_code,
225
+ l.period_start,
226
+ l.period_end,
227
+ l.due_date,
228
+ l.state,
229
+ l.filing_id,
230
+ case
231
+ when l.company_id is not null and l.due_date is null then 'no_deadline_rule'
232
+ when l.company_id is not null then null
233
+ when periodic_return_code(p.id) is null
234
+ and not exists (select 1 from company_filing_periods f
235
+ where f.company_id = p.id) then 'no_form'
236
+ else 'nothing_due'
237
+ end,
238
+ l.instalment,
239
+ l.report_name,
240
+ l.report_short_name
241
+ from portfolio p
242
+ left join listed l on l.company_id = p.id
243
+ order by l.due_date nulls last, l.period_end nulls last, p.name, p.id, l.report_code, l.instalment nulls first;
244
+ $$;
245
+
246
+ comment on function ekwo.portfolio_upcoming_filings(date, date) is
247
+ 'What falls due between two dates in every company the caller holds filings.read on, and in no other: the periods and the instalments upcoming_filings() produces, kept when the day they are due is in the window, with the official and the short name of each form. Every company of the portfolio is in the answer at least once, and a row without a date says why in `reason` — no_deadline_rule where the pack names no day for the form, nothing_due where nothing of the company falls in the window, no_form where the installation carries no return for it. instalment is set on a payment on account and null on a return. Invoker: it reads what the caller could have read company by company.';
248
+
249
+ revoke execute on function ekwo.portfolio_upcoming_filings(date, date) from public, anon;
250
+ grant execute on function ekwo.portfolio_upcoming_filings(date, date) to authenticated, service_role;
251
+
252
+ -- The revoke every file of this directory ends with, and the Data API told
253
+ -- the functions are there.
254
+ revoke execute on all functions in schema ekwo from public;
255
+
256
+ notify pgrst, 'reload schema';