-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathschema.sql
More file actions
1330 lines (997 loc) · 31.8 KB
/
Copy pathschema.sql
File metadata and controls
1330 lines (997 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 16.2 (Postgres.app)
-- Dumped by pg_dump version 16.2 (Postgres.app)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA public;
--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON SCHEMA public IS 'standard public schema';
--
-- Name: amount; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.amount AS (
number numeric,
currency text
);
--
-- Name: lot; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.lot AS (
id integer,
amount public.amount,
cost public.amount,
date date,
label text
);
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: account; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.account (
id integer NOT NULL,
name text NOT NULL,
open_date date NOT NULL,
close_date date,
currencies text[],
meta json DEFAULT '{}'::json NOT NULL
);
--
-- Name: TABLE account; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.account IS 'Combines beancount open and close directives';
--
-- Name: account_change(public.account, daterange); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.account_change(account public.account, range daterange) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT
sum(amount)
FROM
posting
WHERE
account_id = account.id
AND RANGE @> date;
$$;
--
-- Name: FUNCTION account_change(account public.account, range daterange); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.account_change(account public.account, range daterange) IS 'Calculates the total amount of postings for a given account within a specified date range';
--
-- Name: depth(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.depth(a text) RETURNS integer
LANGUAGE sql IMMUTABLE
AS $$
SELECT length(a) - length(replace(a, ':', ''))
$$;
--
-- Name: FUNCTION depth(a text); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.depth(a text) IS 'Find depth of an account name by counting colons';
--
-- Name: reduce_depth(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.reduce_depth(s text) RETURNS text
LANGUAGE sql IMMUTABLE
AS $$
SELECT array_to_string(trim_array(string_to_array(s, ':'), 1),':')
$$;
--
-- Name: FUNCTION reduce_depth(s text); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.reduce_depth(s text) IS 'Reduce depth of an account name by dropping last part of hierarchy';
--
-- Name: account_hierarchy; Type: VIEW; Schema: public; Owner: -
--
CREATE VIEW public.account_hierarchy AS
WITH RECURSIVE names AS (
SELECT account.name,
public.depth(account.name) AS depth
FROM public.account
UNION ALL
SELECT public.reduce_depth(names_1.name) AS name,
public.depth(names_1.name) AS depth
FROM names names_1
WHERE (public.depth(names_1.name) > 0)
)
SELECT DISTINCT name,
depth
FROM names
ORDER BY name;
--
-- Name: VIEW account_hierarchy; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON VIEW public.account_hierarchy IS 'Returns all hierarchial account names and their depth level';
--
-- Name: account_hierarchy_change(public.account_hierarchy, daterange); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.account_hierarchy_change(account_hierarchy public.account_hierarchy, range daterange) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT
sum(amount)
FROM
posting
JOIN account ON account.id = posting.account_id
WHERE
account.name LIKE account_hierarchy.name || ':%'
AND RANGE @> posting.date;
$$;
--
-- Name: FUNCTION account_hierarchy_change(account_hierarchy public.account_hierarchy, range daterange); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.account_hierarchy_change(account_hierarchy public.account_hierarchy, range daterange) IS 'Calculates the total amount of postings for a given account hierarchy within a specified date range';
--
-- Name: assertion; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.assertion (
id integer NOT NULL,
date date NOT NULL,
account_id integer NOT NULL,
amount public.amount NOT NULL
);
--
-- Name: TABLE assertion; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.assertion IS 'Beancount balance directive';
--
-- Name: assertion_is_balanced(public.assertion); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.assertion_is_balanced(a public.assertion) RETURNS boolean
LANGUAGE sql STABLE
AS $$
SELECT
balance_contains_amount (a.amount, posting_balance (posting.*))
FROM
posting
WHERE
posting.account_id = a.account_id
AND posting.date < a.date
ORDER BY
date DESC,
id DESC
LIMIT 1
$$;
--
-- Name: FUNCTION assertion_is_balanced(a public.assertion); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.assertion_is_balanced(a public.assertion) IS 'Checks if a balance directive (assertion) is balanced';
--
-- Name: balance_contains_amount(public.amount, public.amount[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.balance_contains_amount(amount public.amount, balances public.amount[]) RETURNS boolean
LANGUAGE sql IMMUTABLE
AS $$
SELECT EXISTS (
SELECT 1 FROM unnest(balances) as bal
WHERE bal.currency = amount.currency AND bal.number = amount.number
)
$$;
--
-- Name: FUNCTION balance_contains_amount(amount public.amount, balances public.amount[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.balance_contains_amount(amount public.amount, balances public.amount[]) IS 'Given a single amount, assert that the same amount exists in the array of balances. Used to check balance assertions.';
--
-- Name: convert_currency(public.amount, public.amount); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.convert_currency(base public.amount, quote public.amount) RETURNS public.amount
LANGUAGE sql IMMUTABLE
AS $$
SELECT
(
CASE WHEN quote IS NULL THEN
base
ELSE
(base.number * quote.number,
quote.currency)::amount
END)
$$;
--
-- Name: FUNCTION convert_currency(base public.amount, quote public.amount); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.convert_currency(base public.amount, quote public.amount) IS 'Converts base currency to quote currency given the quote value';
--
-- Name: posting; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.posting (
id integer NOT NULL,
date date NOT NULL,
account_id integer NOT NULL,
transaction_id integer NOT NULL,
flag character(1),
amount public.amount NOT NULL,
price public.amount,
cost public.amount,
cost_date date,
cost_label text,
matching_lot_id integer
);
--
-- Name: TABLE posting; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.posting IS 'Transaction posting';
--
-- Name: cost_basis(public.posting[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.cost_basis(postings public.posting[]) RETURNS public.lot[]
LANGUAGE sql STABLE
AS $$
WITH augmentation AS (
SELECT
posting.id,
posting.amount,
posting.cost,
posting.cost_date,
posting.cost_label,
(sum(matching_lot.amount))[1] AS reduction --matching_lost must have same currency
FROM
unnest(postings) AS posting
LEFT JOIN unnest(postings) AS matching_lot ON matching_lot.matching_lot_id = posting.id
WHERE (posting.amount).number > 0
AND posting.cost IS NOT NULL
GROUP BY
posting.id,
posting.amount,
posting.cost,
posting.cost_date,
posting.cost_label
)
SELECT
array_agg(ROW(augmentation.id, ((augmentation.amount).number + coalesce((augmentation.reduction).number, 0), (augmentation.amount).currency)::amount, augmentation.cost, augmentation.cost_date, augmentation.cost_label)::lot)
FROM
augmentation
$$;
--
-- Name: FUNCTION cost_basis(postings public.posting[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.cost_basis(postings public.posting[]) IS 'Calculates the cost basis by matching lots';
--
-- Name: cost_basis_avg(public.posting[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.cost_basis_avg(postings public.posting[]) RETURNS public.lot[]
LANGUAGE sql STABLE
AS $$
WITH summed AS (
-- Sum the total cost and total amount
SELECT
sum((posting.cost).number * (posting.amount).number) AS total_cost,
sum((posting.amount).number) AS total_amount,
min((posting.amount).currency) AS currency
FROM
unnest(postings) AS posting
WHERE
posting.cost IS NOT NULL
GROUP BY
(posting.amount).currency
)
SELECT
array_agg(ROW (NULL::integer, -- Corresponds to the id in the lot
(total_amount, currency)::amount, -- Total amount
(total_cost / total_amount, currency)::amount, -- Average cost
NULL::date, -- Corresponds to `cost_date`
NULL::text)::lot) -- Corresponds to `cost_label`
FROM
summed
$$;
--
-- Name: FUNCTION cost_basis_avg(postings public.posting[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.cost_basis_avg(postings public.posting[]) IS 'Calculates the average cost basis';
--
-- Name: cost_basis_fifo(public.posting[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.cost_basis_fifo(postings public.posting[]) RETURNS public.lot[]
LANGUAGE sql STABLE
AS $$
SELECT cost_basis_lifo_fifo(postings, FALSE)
$$;
--
-- Name: FUNCTION cost_basis_fifo(postings public.posting[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.cost_basis_fifo(postings public.posting[]) IS 'Calculates the cost basis by matching lots using FIFO';
--
-- Name: cost_basis_lifo(public.posting[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.cost_basis_lifo(postings public.posting[]) RETURNS public.lot[]
LANGUAGE sql STABLE
AS $$
SELECT cost_basis_lifo_fifo(postings, true)
$$;
--
-- Name: FUNCTION cost_basis_lifo(postings public.posting[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.cost_basis_lifo(postings public.posting[]) IS 'Calculates the cost basis by matching lots using LIFO';
--
-- Name: cost_basis_lifo_fifo(public.posting[], boolean); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.cost_basis_lifo_fifo(postings public.posting[], is_lifo boolean) RETURNS public.lot[]
LANGUAGE sql IMMUTABLE
AS $$
-- Determine the correct ordering based on is_lifo parameter
WITH reduction AS (
SELECT
(amount).currency AS currency,
sum((amount).number) AS number
FROM
unnest(postings) AS posting
WHERE
(amount).number < 0
AND posting.cost IS NOT NULL
GROUP BY
(amount).currency
),
adjusted_postings AS (
SELECT
posting.*,
reduction.*,
-- Dynamic order direction based on is_lifo parameter
(sum((amount).number) OVER (
PARTITION BY (posting.amount).currency
ORDER BY CASE WHEN is_lifo THEN date END desc,
CASE WHEN not is_lifo THEN date END asc
)) + coalesce(reduction.number, 0) AS adjusted_amount
FROM
unnest(postings) AS posting
LEFT JOIN reduction ON reduction.currency = (posting.amount).currency
WHERE
posting.cost IS NOT NULL
AND (posting.amount).number > 0
)
SELECT
array_agg(
ROW (
id,
(least((adjusted_postings.amount).number, adjusted_amount), (adjusted_postings.amount).currency)::amount,
cost,
cost_date,
cost_label
)::lot
)
FROM
adjusted_postings
WHERE
adjusted_amount > 0
$$;
--
-- Name: FUNCTION cost_basis_lifo_fifo(postings public.posting[], is_lifo boolean); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.cost_basis_lifo_fifo(postings public.posting[], is_lifo boolean) IS 'A generic function for calculating the cost basis using either FIFO or LIFO';
--
-- Name: inventory(public.posting[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.inventory(postings public.posting[]) RETURNS public.lot[]
LANGUAGE sql IMMUTABLE
AS $$
SELECT
ARRAY_AGG(
ROW(
posting.id,
posting.amount,
posting.cost,
posting.cost_date,
posting.cost_label
)::lot -- Convert the row to a `lot` type
)
FROM
UNNEST(postings) AS posting -- Unnest the postings array
WHERE
posting.cost IS NOT NULL; -- Filter rows with non-null cost
$$;
--
-- Name: FUNCTION inventory(postings public.posting[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.inventory(postings public.posting[]) IS 'Create an inventory (lot[]) for a list of postings';
--
-- Name: is_balance_in_tolerance(public.amount[], public.amount[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.is_balance_in_tolerance(balances public.amount[], tolerances public.amount[]) RETURNS boolean
LANGUAGE sql IMMUTABLE
AS $$
SELECT
array_length(balances, 1) = 1
AND array_length(tolerances, 1) = 1
AND balances[1].currency = tolerances[1].currency
AND abs(balances[1].number) < tolerances[1].number
$$;
--
-- Name: FUNCTION is_balance_in_tolerance(balances public.amount[], tolerances public.amount[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.is_balance_in_tolerance(balances public.amount[], tolerances public.amount[]) IS 'Given an array of balances and tolerances assert that there is only a single amount and that it''s value is within the tolerance.';
--
-- Name: market_price(public.amount[], text, date); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.market_price(balance public.amount[], into_currency text, for_date date) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT
sum(market_converted)
FROM
unnest(balance) AS bal,
LATERAL market_price (bal, into_currency, for_date) AS market_converted
$$;
--
-- Name: FUNCTION market_price(balance public.amount[], into_currency text, for_date date); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.market_price(balance public.amount[], into_currency text, for_date date) IS 'Convert a balance into a currency for a given date';
--
-- Name: market_price(public.amount, text, date); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.market_price(convert_amount public.amount, into_currency text, for_date date) RETURNS public.amount
LANGUAGE sql STABLE
AS $$
SELECT
convert_currency (convert_amount, (
SELECT
amount AS quote
FROM price_inverted
WHERE
currency = convert_amount.currency
AND (amount).currency = into_currency
AND date <= for_date ORDER BY date DESC LIMIT 1))
$$;
--
-- Name: FUNCTION market_price(convert_amount public.amount, into_currency text, for_date date); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.market_price(convert_amount public.amount, into_currency text, for_date date) IS 'Convert a amount into a currency for a given date';
--
-- Name: max(public.amount[], public.amount); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.max(state public.amount[], current public.amount) RETURNS public.amount[]
LANGUAGE sql IMMUTABLE
AS $$
SELECT
ARRAY (
SELECT
(max(combined.number),
combined.currency)::public.amount
FROM (
SELECT
number,
currency
FROM
unnest(state)
UNION ALL
SELECT
current.number,
current.currency) AS combined
GROUP BY
currency
ORDER BY
currency)
$$;
--
-- Name: FUNCTION max(state public.amount[], current public.amount); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.max(state public.amount[], current public.amount) IS 'Aggregate function to find max balance of amounts. Used for calculating the max tolerance.';
--
-- Name: posting_balance(public.posting); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.posting_balance(p public.posting) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT
sum(amount)
FROM
posting
WHERE
posting.account_id = p.account_id
AND posting.date <= p.date
AND posting.id <= p.id
$$;
--
-- Name: FUNCTION posting_balance(p public.posting); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.posting_balance(p public.posting) IS 'Calculate running balance for posting';
--
-- Name: sum(public.amount[], public.amount[]); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.sum(state public.amount[], current public.amount[]) RETURNS public.amount[]
LANGUAGE sql IMMUTABLE
AS $$
SELECT
ARRAY (
SELECT
(sum(combined.number),
combined.currency)::public.amount
FROM (
SELECT
number,
currency
FROM
unnest(state)
UNION ALL
SELECT
number,
currency
FROM
unnest(CURRENT)) AS combined
GROUP BY
currency
ORDER BY
currency)
$$;
--
-- Name: FUNCTION sum(state public.amount[], current public.amount[]); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.sum(state public.amount[], current public.amount[]) IS 'Aggregate sum of balances (amount[])';
--
-- Name: sum(public.amount[], public.amount); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.sum(state public.amount[], current public.amount) RETURNS public.amount[]
LANGUAGE sql IMMUTABLE
AS $$
SELECT
ARRAY (
SELECT
(sum(combined.number),
combined.currency)::public.amount
FROM (
SELECT
number,
currency
FROM
unnest(state)
UNION ALL
SELECT
current.number,
current.currency) AS combined
GROUP BY
currency
ORDER BY
currency)
$$;
--
-- Name: FUNCTION sum(state public.amount[], current public.amount); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.sum(state public.amount[], current public.amount) IS 'Aggregate sum of amount';
--
-- Name: sum(public.amount[], public.posting); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.sum(state public.amount[], current public.posting) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT public.sum(
state,
CASE
WHEN current.cost IS NOT NULL THEN convert_currency(current.amount, current.cost)
WHEN current.price IS NOT NULL THEN convert_currency(current.amount, current.price)
ELSE current.amount
END
)
$$;
--
-- Name: FUNCTION sum(state public.amount[], current public.posting); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.sum(state public.amount[], current public.posting) IS 'Aggregate sum of postings accounting for cost and price';
--
-- Name: tolerance(public.amount); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.tolerance(base public.amount) RETURNS public.amount
LANGUAGE sql STABLE
AS $$
SELECT (
COALESCE(
(SELECT power(10, -c.decimal_places)
FROM commodity c
WHERE c.currency = base.currency),
0
),
base.currency
)::amount
$$;
--
-- Name: FUNCTION tolerance(base public.amount); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.tolerance(base public.amount) IS 'Calculate tolerance for an amount';
--
-- Name: tolerance(public.amount[], public.posting); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.tolerance(state public.amount[], current public.posting) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT public.max(
state,
CASE
WHEN current.cost IS NOT NULL THEN public.tolerance(current.amount, current.cost)
WHEN current.price IS NOT NULL THEN public.tolerance(current.price)
ELSE public.tolerance(current.amount)
END
)
$$;
--
-- Name: FUNCTION tolerance(state public.amount[], current public.posting); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.tolerance(state public.amount[], current public.posting) IS 'Aggregate tolerance for a list postings';
--
-- Name: tolerance(public.amount, public.amount); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.tolerance(base public.amount, quote public.amount) RETURNS public.amount
LANGUAGE sql STABLE
AS $$
SELECT ((tolerance(base)).number * quote.number, quote.currency)::amount
$$;
--
-- Name: FUNCTION tolerance(base public.amount, quote public.amount); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.tolerance(base public.amount, quote public.amount) IS 'Calculate tolerance for a cost';
--
-- Name: transaction; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.transaction (
id integer NOT NULL,
flag character(1) NOT NULL,
payee text NOT NULL,
narration text NOT NULL,
tags text NOT NULL,
links text NOT NULL
);
--
-- Name: TABLE transaction; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.transaction IS 'Beancount transaction directive';
--
-- Name: transaction_balance(public.transaction); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.transaction_balance(t public.transaction) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT
sum(posting.*)
FROM
posting
WHERE
posting.transaction_id = t.id
$$;
--
-- Name: FUNCTION transaction_balance(t public.transaction); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.transaction_balance(t public.transaction) IS 'Calculate balance of a transaction';
--
-- Name: transaction_is_balanced(public.transaction); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.transaction_is_balanced(t public.transaction) RETURNS boolean
LANGUAGE sql STABLE
AS $$
SELECT
is_balance_in_tolerance (sum(posting.*), tolerance (posting.*))
FROM
posting
WHERE
posting.transaction_id = t.id
$$;
--
-- Name: FUNCTION transaction_is_balanced(t public.transaction); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.transaction_is_balanced(t public.transaction) IS 'Calculate if a transaction is balanced';
--
-- Name: transaction_tolerance(public.transaction); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.transaction_tolerance(t public.transaction) RETURNS public.amount[]
LANGUAGE sql STABLE
AS $$
SELECT
tolerance (posting.*)
FROM
posting
WHERE
posting.transaction_id = t.id
$$;
--
-- Name: FUNCTION transaction_tolerance(t public.transaction); Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON FUNCTION public.transaction_tolerance(t public.transaction) IS 'Calculate tolerance for a transaction';
--
-- Name: cost_basis(public.posting); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.cost_basis(public.posting) (
SFUNC = array_append,
STYPE = public.posting[],
INITCOND = '{}',
FINALFUNC = public.cost_basis
);
--
-- Name: cost_basis_avg(public.posting); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.cost_basis_avg(public.posting) (
SFUNC = array_append,
STYPE = public.posting[],
INITCOND = '{}',
FINALFUNC = public.cost_basis_avg
);
--
-- Name: cost_basis_fifo(public.posting); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.cost_basis_fifo(public.posting) (
SFUNC = array_append,
STYPE = public.posting[],
INITCOND = '{}',
FINALFUNC = public.cost_basis_fifo
);
--
-- Name: cost_basis_lifo(public.posting); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.cost_basis_lifo(public.posting) (
SFUNC = array_append,
STYPE = public.posting[],
INITCOND = '{}',
FINALFUNC = public.cost_basis_lifo
);
--
-- Name: inventory(public.posting); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.inventory(public.posting) (
SFUNC = array_append,
STYPE = public.posting[],
INITCOND = '{}',
FINALFUNC = public.inventory
);
--
-- Name: sum(public.amount[]); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.sum(public.amount[]) (
SFUNC = public.sum,
STYPE = public.amount[],
INITCOND = '{}'
);
--
-- Name: sum(public.amount); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.sum(public.amount) (
SFUNC = public.sum,
STYPE = public.amount[],
INITCOND = '{}'
);
--
-- Name: sum(public.posting); Type: AGGREGATE; Schema: public; Owner: -
--
CREATE AGGREGATE public.sum(public.posting) (
SFUNC = public.sum,
STYPE = public.amount[],
INITCOND = '{}'
);