-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Assets.sql
1356 lines (1178 loc) · 49.6 KB
/
Assets.sql
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
set client_min_messages = 'warning';
/*
asset_report__generate_gl - creates GL transaction upon approval of depreciation report
asset_report__disposal_gl - creates GL transaction upon approval of a disposal (sale/abandonment)
asset_disposal__approve - creates GL transaction upon approval of *partial* disposal (sale/abandonment)
asset__import_from_disposal - creates new assets from partially disposed assets, upon approval
asset_report__get_disposal - returns a line per disposed asset in a disposal report with disposal details
call tree:
asset_report__approve
report_class of
'1': (depreciation)
asset_report__generate_gl
'2': (full disposal)
asset_report__disposal_gl
asset_report__get_disposal
'4': (partial disposal)
asset_disposal__approve
asset__import_from_disposal
*/
BEGIN;
DROP FUNCTION IF EXISTS asset_dep__straight_line_base
(numeric, numeric, numeric, numeric, numeric)
CASCADE;
CREATE OR REPLACE FUNCTION asset_dep__straight_line_base
(in_base_life numeric, in_used numeric, in_basis numeric, in_dep_to_date numeric)
returns numeric as $$
SELECT CASE WHEN in_used/in_base_life * in_basis < in_basis - in_dep_to_date
THEN in_used/in_base_life * in_basis
ELSE in_basis - in_dep_to_date
END;
$$ language sql;
COMMENT ON FUNCTION asset_dep__straight_line_base
(in_base_life numeric, in_used numeric, in_basis numeric,
in_dep_to_date numeric) IS
$$ This function is a basic function which does the actual calculation for
straight line depreciation.$$;
CREATE OR REPLACE FUNCTION asset_dep__used_months
(in_last_dep date, in_dep_date date, in_usable_life numeric)
RETURNS numeric AS
$$
select CASE WHEN extract('MONTHS' FROM (date_trunc('day', in_dep_date) - date_trunc('day', in_last_dep)))
> in_usable_life
THEN in_usable_life
ELSE extract('MONTHS' FROM (date_trunc('day', in_dep_date) - date_trunc('day', in_last_dep)))::numeric
END;
$$ language sql;
COMMENT ON FUNCTION asset_dep__used_months
(in_last_dep date, in_dep_date date, in_usable_life numeric) IS
$$ This checks the interval between the two dates, and if longer than the
usable life, returns the months in that interval. Otherwise returns the
usable life.$$;
CREATE OR REPLACE FUNCTION asset_dep_get_usable_life_yr
(in_usable_life numeric, in_start_date date, in_dep_date date)
returns numeric as
$$
SELECT CASE WHEN in_dep_date IS NULL
or get_fractional_year(in_start_date, in_dep_date) > in_usable_life
then in_usable_life
WHEN get_fractional_year(in_start_date, in_dep_date) < 0
THEN 0
ELSE get_fractional_year(in_start_date, in_dep_date)
END;
$$ language sql;
COMMENT ON FUNCTION asset_dep_get_usable_life_yr
(in_usable_life numeric, in_start_date date, in_dep_date date) IS
$$If the interval is less than 0 then 0. If the interval is greater than the
usable life, then the usable life. Otherwise, return the interval as a
fractional year.$$;
CREATE OR REPLACE FUNCTION months_passed (in_start timestamp, in_end timestamp)
returns int as
$$
-- The addition of one day is so that it will return '1' when run on the end
-- day of consecutive months.
select (extract (months from age(in_end + '1 day', in_start + '1 day'))
+ extract (years from age(in_end, in_start)) * 12)::int;
$$ language sql;
COMMENT ON FUNCTION months_passed (in_start timestamp, in_end timestamp) IS
$$ Returns the number of months between in_start and in_end.$$;
CREATE OR REPLACE FUNCTION asset_dep_straight_line_yr_d
(in_asset_ids int[], in_report_date date, in_report_id int)
RETURNS bool AS
$$
INSERT INTO asset_report_line (asset_id, report_id, amount, department_id,
warehouse_id)
SELECT ai.id, in_report_id,
asset_dep__straight_line_base(
ai.usable_life, -- years
get_fractional_year(coalesce(max(report_date),
start_depreciation),
in_report_date),
purchase_value - salvage_value,
coalesce(sum(l.amount), 0)),
ai.department_id, ai.location_id
FROM asset_item ai
LEFT JOIN asset_report_line l ON (l.asset_id = ai.id and l.amount > 0)
LEFT JOIN asset_report r ON (l.report_id = r.id)
WHERE ai.id = ANY(in_asset_ids)
GROUP BY ai.id, ai.start_depreciation, ai.purchase_date, ai.purchase_value,
ai.salvage_value, ai.department_id, ai.location_id, ai.usable_life;
UPDATE asset_report SET report_class = 1 WHERE id = in_report_id;
select true;
$$ language sql;
CREATE OR REPLACE FUNCTION asset_dep_straight_line_yr_m
(in_asset_ids int[], in_report_date date, in_report_id int)
RETURNS bool AS
$$
INSERT INTO asset_report_line (asset_id, report_id, amount, department_id,
warehouse_id)
SELECT ai.id, in_report_id,
asset_dep__straight_line_base(
ai.usable_life * 12, --months
months_passed(coalesce(max(report_date),
start_depreciation),
in_report_date),
purchase_value - salvage_value,
coalesce(sum(l.amount), 0)),
ai.department_id, ai.location_id
FROM asset_item ai
LEFT JOIN asset_report_line l ON (l.asset_id = ai.id and l.amount > 0)
LEFT JOIN asset_report r ON (l.report_id = r.id)
WHERE ai.id = ANY(in_asset_ids)
GROUP BY ai.id, ai.start_depreciation, ai.purchase_date, ai.purchase_value,
ai.salvage_value, ai.department_id, ai.location_id, ai.usable_life;
UPDATE asset_report SET report_class = 1 WHERE id = in_report_id;
select true;
$$ language sql;
COMMENT ON FUNCTION asset_dep_straight_line_yr_m
(in_asset_ids int[], in_report_date date, in_report_id int) is
$$ Performs straight line depreciation on a set of selected assets, selecting
the depreciation values into a report.
Assumes the usable life is measured in years, and is depreciated eavenly every
month.$$;
CREATE OR REPLACE FUNCTION asset_dep_straight_line_month
(in_asset_ids int[], in_report_date date, in_report_id int)
RETURNS bool AS
$$
INSERT INTO asset_report_line (asset_id, report_id, amount, department_id,
warehouse_id)
SELECT ai.id, in_report_id,
asset_dep__straight_line_base(
ai.usable_life, --months
months_passed(coalesce(max(report_date),
start_depreciation),
in_report_date),
purchase_value - salvage_value,
coalesce(sum(l.amount), 0)),
ai.department_id, ai.location_id
FROM asset_item ai
LEFT JOIN asset_report_line l ON (l.asset_id = ai.id and l.amount > 0)
LEFT JOIN asset_report r ON (l.report_id = r.id)
WHERE ai.id = ANY(in_asset_ids)
GROUP BY ai.id, ai.start_depreciation, ai.purchase_date, ai.purchase_value,
ai.salvage_value, ai.department_id, ai.location_id, ai.usable_life;
UPDATE asset_report SET report_class = 1 WHERE id = in_report_id;
select true;
$$ language sql;
COMMENT ON FUNCTION asset_dep_straight_line_month
(in_asset_ids int[], in_report_date date, in_report_id int) IS
$$ Performs straight line depreciation, selecting depreciation amounts, etc.
into a report for further review and approval. Usable life is in months, and
depreciation is an equal amount every month.$$;
CREATE OR REPLACE FUNCTION asset_report__generate_gl(in_report_id int, in_accum_account_id int)
RETURNS INT AS
$$
DECLARE
t_report_dept record;
t_dep_amount numeric;
Begin
INSERT INTO gl (reference, description, transdate,
approved, trans_type_code)
SELECT setting_increment('glnumber'),
'Asset Report ' || asset_report.id,
report_date,
coalesce((select value::boolean from defaults
where setting_key = 'debug_fixed_assets'), true),
'fa'
FROM asset_report
JOIN asset_report_line
ON (asset_report.id = asset_report_line.report_id)
JOIN asset_item
ON (asset_report_line.asset_id = asset_item.id)
WHERE asset_report.id = in_report_id
GROUP BY asset_report.id, asset_report.report_date;
INSERT INTO acc_trans (trans_id, chart_id, transdate, approved,
amount_bc, curr, amount_tc)
SELECT gl.id, a.exp_account_id, r.report_date, true, sum(amount) * -1,
defaults_get_defaultcurrency(), sum(amount) * -1
FROM asset_report r
JOIN asset_report_line l ON (r.id = l.report_id)
JOIN asset_item a ON (l.asset_id = a.id)
JOIN gl ON (gl.description = 'Asset Report ' || l.report_id)
WHERE r.id = in_report_id
GROUP BY gl.id, r.report_date, a.exp_account_id;
INSERT INTO acc_trans (trans_id, chart_id, transdate, approved,
amount_bc, curr, amount_tc)
SELECT gl.id, a.dep_account_id, r.report_date, true, sum(amount),
defaults_get_defaultcurrency(), sum(amount)
FROM asset_report r
JOIN asset_report_line l ON (r.id = l.report_id)
JOIN asset_item a ON (l.asset_id = a.id)
JOIN gl ON (gl.description = 'Asset Report ' || l.report_id)
WHERE r.id = in_report_id
GROUP BY gl.id, a.dep_account_id, r.report_date, a.tag, a.description;
RETURN in_report_id;
END;
$$ language plpgsql;
COMMENT ON FUNCTION asset_report__generate_gl
(in_report_id int, in_accum_account_id int) IS
$$ Generates a GL transaction when the Asset report is approved.
Create approved transactions, unless the value of the setting_key
'debug_fixed_assets' evaluates to false
$$;
CREATE OR REPLACE FUNCTION asset_class__get (in_id int) RETURNS asset_class AS
$$
SELECT * FROM asset_class WHERE id = in_id;
$$ language sql;
COMMENT ON FUNCTION asset_class__get (in_id int) IS
$$ returns the row from asset_class identified by in_id.$$;
CREATE OR REPLACE FUNCTION asset_class__list() RETURNS SETOF asset_class AS
$$
SELECT * FROM asset_class ORDER BY label;
$$ LANGUAGE SQL;
COMMENT ON FUNCTION asset_class__list() is
$$ Returns an alphabetical list of asset classes.$$;
DROP TYPE IF EXISTS asset_class_result CASCADE;
CREATE TYPE asset_class_result AS (
id int,
asset_account_id int,
asset_accno text,
asset_description text,
dep_account_id int,
dep_accno text,
dep_description text,
method text,
method_id int,
label text
);
CREATE OR REPLACE FUNCTION asset_class__search
(in_asset_account_id int, in_dep_account_id int,
in_method int, in_label text)
RETURNS SETOF asset_class_result AS
$$
BEGIN
RETURN QUERY EXECUTE $sql$
SELECT ac.id, ac.asset_account_id, aa.accno, aa.description,
ac.dep_account_id, ad.accno, ad.description,
m.method, ac.method,
ac.label
FROM asset_class ac
LEFT JOIN account aa ON (aa.id = ac.asset_account_id)
LEFT JOIN account ad ON (ad.id = ac.dep_account_id)
JOIN asset_dep_method m ON (ac.method = m.id)
WHERE
($1 is null
or $1 = ac.asset_account_id)
AND ($2 is null OR
$2 = ac.dep_account_id)
AND ($3 is null OR $3 = ac.method)
AND ($4 IS NULL OR ac.label LIKE
'%' || $4 || '%')
ORDER BY label
$sql$
USING in_asset_account_id, in_dep_account_id, in_method, in_label;
END
$$ LANGUAGE PLPGSQL;
COMMENT ON FUNCTION asset_class__search
(in_asset_account_id int, in_dep_account_id int,
in_method int, in_label text) IS
$$ Returns a list of matching asset classes. The account id's are exact matches
as is the method, but the label is a partial match. NULL's match all.$$;
CREATE OR REPLACE FUNCTION asset_class__get_dep_methods()
RETURNS SETOF asset_dep_method as $$
SELECT * FROM asset_dep_method ORDER BY method;
$$ LANGUAGE sql;
COMMENT ON FUNCTION asset_class__get_dep_methods() IS
$$ Returns a set of asset_dep_methods ordered by the method label.$$;
CREATE OR REPLACE FUNCTION asset_class__save
(in_id int, in_asset_account_id int, in_dep_account_id int,
in_method int, in_label text, in_unit_label text)
RETURNS asset_class AS
$$
DECLARE ret_val asset_class;
BEGIN
UPDATE asset_class
SET asset_account_id = in_asset_account_id,
dep_account_id = in_dep_account_id,
method = in_method,
label = in_label
WHERE id = in_id;
IF FOUND THEN
SELECT * INTO ret_val FROM asset_class where id = in_id;
RETURN ret_val;
END IF;
INSERT INTO asset_class (asset_account_id, dep_account_id, method,
label)
VALUES (in_asset_account_id, in_dep_account_id, in_method,
in_label);
SELECT * INTO ret_val FROM asset_class
WHERE id = currval('asset_class_id_seq');
RETURN ret_val;
END;
$$ language plpgsql;
COMMENT ON FUNCTION asset_class__save
(in_id int, in_asset_account_id int, in_dep_account_id int,
in_method int, in_label text, in_unit_label text) IS
$$ Saves this data as an asset_class record. If in_id is NULL or is not found
in the table, inserts a new row. Returns the row saved.$$;
CREATE OR REPLACE FUNCTION asset__get (in_id int, in_tag text)
RETURNS asset_item AS
$$
SELECT * from asset_item WHERE id = in_id OR in_tag = tag
ORDER BY id desc limit 1;
$$ language sql;
COMMENT ON FUNCTION asset__get (in_id int, in_tag text) IS
$$ Retrieves a given asset either by id or tag. Both are complete matches.
Note that the behavior is undefined if both id and tag are provided.$$;
CREATE OR REPLACE FUNCTION asset__search
(in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric)
RETURNS SETOF asset_item AS
$$
BEGIN
RETURN QUERY EXECUTE $sql$
SELECT * FROM asset_item
WHERE ($1 is null
or asset_class_id = $1)
AND ($2 is null or description
LIKE '%' || $2 || '%')
and ($3 is null or tag like '%'||$3||'%')
AND ($4 is null
or purchase_date = $4)
AND ($5 is null
or $5 = purchase_value)
AND ($6 is null
or $6 = usable_life)
AND ($7 is null
OR $7 = salvage_value)
$sql$
USING in_asset_class, in_description, in_tag, in_purchase_date,
in_purchase_value, in_usable_life, in_salvage_value;
END
$$ LANGUAGE PLPGSQL;
COMMENT ON FUNCTION asset__search
(in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric) IS
$$Searches for assets. Nulls match all records. Asset class is exact,
as is purchase date, purchase value, and salvage value. Tag and description
are partial matches.$$;
CREATE OR REPLACE FUNCTION asset_class__get_asset_accounts()
RETURNS SETOF account AS $$
SELECT * FROM account
WHERE id IN
(select account_id from account_link where description = 'Fixed_Asset')
ORDER BY accno;
$$ LANGUAGE SQL;
COMMENT ON FUNCTION asset_class__get_asset_accounts()
IS
$$ Returns a list of fixed asset accounts, ordered by account number$$;
CREATE OR REPLACE FUNCTION asset_class__get_dep_accounts()
RETURNS SETOF account AS $$
SELECT * FROM account
WHERE id IN
(select account_id from account_link where description = 'Asset_Dep')
ORDER BY accno;
$$ LANGUAGE SQL;
COMMENT ON FUNCTION asset_class__get_dep_accounts() IS
$$ Returns a list of asset depreciation accounts, ordered by account number$$;
DROP FUNCTION IF EXISTS asset__save
(in_id int, in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric,
in_start_depreciation date, in_warehouse_id int,
in_department_id int, in_invoice_id int,
in_asset_account_id int, in_dep_account_id int, in_exp_account_id int);
CREATE OR REPLACE FUNCTION asset__save
(in_id int, in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric,
in_start_depreciation date, in_warehouse_id int,
in_department_id int, in_invoice_id int,
in_asset_account_id int, in_dep_account_id int, in_exp_account_id int,
in_obsolete_by int)
returns asset_item AS
$$
DECLARE ret_val asset_item;
BEGIN
UPDATE asset_item
SET asset_class_id = in_asset_class,
description = in_description,
tag = in_tag,
purchase_date = in_purchase_date,
purchase_value = in_purchase_value,
usable_life = in_usable_life,
location_id = in_warehouse_id,
department_id = in_department_id,
invoice_id = in_invoice_id,
salvage_value = in_salvage_value,
asset_account_id = in_asset_account_id,
exp_account_id = in_exp_account_id,
start_depreciation =
coalesce(in_start_depreciation, in_purchase_date),
dep_account_id = in_dep_account_id,
obsolete_by = in_obsolete_by
WHERE id = in_id;
IF FOUND THEN
SELECT * INTO ret_val FROM asset_item WHERE id = in_id;
return ret_val;
END IF;
INSERT INTO asset_item (asset_class_id, description, tag, purchase_date,
purchase_value, usable_life, salvage_value, department_id,
location_id, invoice_id, asset_account_id, dep_account_id,
start_depreciation, exp_account_id, obsolete_by)
VALUES (in_asset_class, in_description, in_tag, in_purchase_date,
in_purchase_value, in_usable_life, in_salvage_value,
in_department_id, in_warehouse_id, in_invoice_id,
in_asset_account_id, in_dep_account_id,
coalesce(in_start_depreciation, in_purchase_date),
in_exp_account_id, in_obsolete_by);
SELECT * INTO ret_val FROM asset_item
WHERE id = currval('asset_item_id_seq');
RETURN ret_val;
END;
$$ language plpgsql;
COMMENT ON FUNCTION asset__save
(in_id int, in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric,
in_start_depreciation date, in_warehouse_id int,
in_department_id int, in_invoice_id int,
in_asset_account_id int, in_dep_account_id int, in_exp_account_id int,
in_obsolete_by int) IS
$$ Saves the asset with the information provided. If the id is provided,
overwrites the record with the id. Otherwise, or if that record is not found,
inserts. Returns the row inserted or updated.
$$;
CREATE OR REPLACE FUNCTION asset_item__search
(in_id int, in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric,
in_start_depreciation date, in_warehouse_id int,
in_department_id int, in_invoice_id int,
in_asset_account_id int, in_dep_account_id int)
returns setof asset_item as
$$
BEGIN
RETURN QUERY EXECUTE $sql$
SELECT * FROM asset_item
WHERE (id = $1 or $1 is null)
and (asset_class_id = $2 or $2 is null)
and (description like '%'||$3||'%'
or $3 is null)
and (tag like '%' || $4 || '%' or $4 is null)
and (purchase_value = $6
or $6 is null)
and ($5 = purchase_date
or $5 is null)
and (start_depreciation = $9
or $9 is null)
and ($10 = location_id OR $10 is null)
and (department_id = $11
or $11 is null)
and ($12 = invoice_id OR $12 IS NULL)
and (asset_account_id = $13
or $13 is null)
and (dep_account_id = $14
or $14 is null)
$sql$
USING in_id, in_asset_class, in_description, in_tag,
in_purchase_date, in_purchase_value, in_usable_life, in_salvage_value,
in_start_depreciation, in_warehouse_id, in_department_id, in_invoice_id,
in_asset_account_id, in_dep_account_id;
END
$$ LANGUAGE PLPGSQL;
COMMENT ON FUNCTION asset_item__search
(in_id int, in_asset_class int, in_description text, in_tag text,
in_purchase_date date, in_purchase_value numeric,
in_usable_life numeric, in_salvage_value numeric,
in_start_depreciation date, in_warehouse_id int,
in_department_id int, in_invoice_id int,
in_asset_account_id int, in_dep_account_id int) IS
$$ Returns a list of matching asset items. Nulls match all records.
Tag and description allow for partial match. All other matches are exact.$$;
CREATE OR REPLACE FUNCTION asset_class__get_dep_method (in_asset_class int)
RETURNS asset_dep_method AS $$
SELECT * from asset_dep_method
WHERE id = (select method from asset_class where id = in_asset_class);
$$ language sql;
COMMENT ON FUNCTION asset_class__get_dep_method (in_asset_class int) IS
$$Returns the depreciation method associated with the asset class.$$;
CREATE OR REPLACE FUNCTION asset_report__save
(in_id int, in_report_date date, in_report_class int, in_asset_class int,
in_submit bool)
RETURNS asset_report AS
$$
DECLARE
ret_val asset_report;
item record;
method_text text;
BEGIN
UPDATE asset_report
set asset_class = in_asset_class,
report_class = in_report_class,
report_date = in_report_date,
submitted = (in_submit or submitted)
WHERE id = in_id;
IF FOUND THEN
SELECT * INTO ret_val FROM asset_report WHERE id = in_id;
ELSE
INSERT INTO asset_report(report_class, asset_class, report_date,
submitted)
values (in_report_class, in_asset_class, in_report_date,
coalesce(in_submit, true));
SELECT * INTO ret_val FROM asset_report
WHERE id = currval('asset_report_id_seq');
END IF;
RETURN ret_val;
END;
$$ language plpgsql;
COMMENT ON FUNCTION asset_report__save
(in_id int, in_report_date date, in_report_class int, in_asset_class int,
in_submit bool) IS
$$ Creates or updates an asset report with the information presented. Note that
approval values are not set here, and that one cannot unsubmit a report though
this function.$$;
CREATE OR REPLACE FUNCTION asset_report__dispose
(in_id int, in_asset_id int, in_amount numeric, in_dm int,
in_percent_disposed numeric)
returns bool AS
$$
BEGIN
INSERT
INTO asset_report_line (report_id, asset_id, amount)
values (in_id, in_asset_id, in_amount);
INSERT
INTO asset_rl_to_disposal_method
(report_id, asset_id, disposal_method_id, percent_disposed)
VALUES (in_id, in_asset_id, in_dm, in_percent_disposed);
RETURN TRUE;
END;
$$ language PLPGSQL;
COMMENT ON FUNCTION asset_report__dispose
(in_id int, in_asset_id int, in_amount numeric, in_dm int,
in_percent_disposed numeric) IS
$$ Disposes of an asset. in_dm is the disposal method id.$$;
DROP TYPE IF EXISTS asset_disposal_report_line CASCADE;
CREATE TYPE asset_disposal_report_line
AS (
id int,
tag text,
description text,
start_dep date,
disposed_on date,
dm char(1),
purchase_value numeric,
accum_depreciation numeric,
disposal_amt numeric,
adj_basis numeric,
gain_loss numeric
);
CREATE OR REPLACE FUNCTION asset_report__get_disposal (in_id int)
returns setof asset_disposal_report_line AS
$$
SELECT ai.id, ai.tag, ai.description, ai.start_depreciation, r.report_date,
dm.short_label, ai.purchase_value,
sum (CASE WHEN pr.report_class in (1,3) THEN prl.amount ELSE 0 END)
as accum_dep,
l.amount,
ai.purchase_value - sum(CASE WHEN pr.report_class in (1,3)
THEN prl.amount
ELSE 0
END) as adjusted_basis,
l.amount - ai.purchase_value + sum(CASE WHEN pr.report_class in (1,3)
THEN prl.amount
ELSE 0
END) as gain_loss
FROM asset_item ai
JOIN asset_report_line l ON (l.report_id = in_id AND ai.id = l.asset_id)
JOIN asset_report r ON (l.report_id = r.id)
LEFT JOIN asset_rl_to_disposal_method adm
USING (report_id, asset_id)
JOIN asset_disposal_method dm
ON (adm.disposal_method_id = dm.id)
LEFT JOIN asset_report_line prl ON (prl.report_id <> in_id
AND ai.id = prl.asset_id)
LEFT JOIN asset_report pr ON (prl.report_id = pr.id)
GROUP BY ai.id, ai.tag, ai.description, ai.start_depreciation, r.report_date,
ai.purchase_value, l.amount, dm.short_label
ORDER BY ai.id, ai.tag;
$$ language sql;
COMMENT ON FUNCTION asset_report__get_disposal (in_id int) IS
$$ Returns a set of lines of disposed assets in a disposal report, specified
by the report id.$$;
DROP TYPE IF EXISTS asset_nbv_line CASCADE;
CREATE TYPE asset_nbv_line AS (
id int,
tag text,
description text,
begin_depreciation date,
method text,
remaining_life numeric,
basis numeric,
salvage_value numeric,
through_date date,
accum_depreciation numeric,
net_book_value numeric
);
CREATE OR REPLACE FUNCTION asset_nbv_report ()
returns setof asset_nbv_line AS
$$
BEGIN
RETURN QUERY EXECUTE $sql$
SELECT ai.id, ai.tag, ai.description, ai.start_depreciation,
adm.short_name, ai.usable_life
- months_passed(ai.start_depreciation,
coalesce(max(r.report_date),
ai.start_depreciation))/(case when adm.unit_label='in years' then 12 else 1 end),
ai.purchase_value - ai.salvage_value, ai.salvage_value, max(r.report_date),
sum(rl.amount), ai.purchase_value - coalesce(sum(rl.amount), 0)
FROM asset_item ai
JOIN asset_class ac ON (ai.asset_class_id = ac.id)
JOIN asset_dep_method adm ON (adm.id = ac.method)
LEFT JOIN (select arl.*
from asset_report_line arl
join asset_report ar on arl.report_id = ar.id
where approved_at is not null) rl ON (ai.id = rl.asset_id)
LEFT JOIN asset_report r on (rl.report_id = r.id)
WHERE r.id IS NULL OR r.approved_at IS NOT NULL
GROUP BY ai.id, ai.tag, ai.description, ai.start_depreciation, ai.purchase_date,
adm.short_name, adm.unit_label, ai.usable_life, ai.purchase_value, salvage_value
HAVING (NOT 2 = ANY(array_agg(r.report_class)))
AND (NOT 4 = ANY(array_agg(r.report_class)))
OR max(r.report_class) IS NULL
ORDER BY ai.id, ai.tag, ai.description
$sql$;
END
$$ LANGUAGE PLPGSQL;
COMMENT ON FUNCTION asset_nbv_report () IS
$$ Returns the current net book value report.$$;
DROP TYPE IF EXISTS partial_disposal_line CASCADE;
CREATE TYPE partial_disposal_line AS (
id int,
tag text,
description text,
start_dep date,
purchase_value numeric,
disposal_date date,
percent_disposed numeric,
disposed_acquired_value numeric,
percent_remaining numeric,
remaining_aquired_value numeric
);
CREATE OR REPLACE FUNCTION asset_report_partial_disposal_details(in_id int)
RETURNS SETOF PARTIAL_DISPOSAL_LINE AS
$$
SELECT ai.id, ai.tag, ai.description, ai.start_depreciation, ai.purchase_value,
ar.report_date, arld.percent_disposed,
(arld.percent_disposed / 100) * ai.purchase_value,
100 - arld.percent_disposed,
((100 - arld.percent_disposed)/100) * ai.purchase_value
FROM asset_item ai
JOIN asset_report_line l ON (ai.id = l.asset_id)
JOIN asset_report ar ON (ar.id = l.report_id)
JOIN asset_rl_to_disposal_method arld
ON ((arld.report_id, arld.asset_id) = (l.report_id, l.asset_id))
WHERE ar.id = in_id;
$$ LANGUAGE SQL;
COMMENT ON FUNCTION asset_report_partial_disposal_details(in_id int) IS
$$ Returns the partial disposal details for a partial disposal report.$$;
DROP FUNCTION IF EXISTS asset_report__approve(int, int, int, int);
CREATE OR REPLACE FUNCTION asset_report__approve
(in_id int, in_expense_acct int, in_gain_acct int, in_loss_acct int, in_cash_acct int)
RETURNS asset_report AS
$$
DECLARE ret_val asset_report;
BEGIN
UPDATE asset_report
SET approved_at = now(),
approved_by = person__get_my_entity_id()
where id = in_id;
SELECT * INTO ret_val FROM asset_report WHERE id = in_id;
if ret_val.dont_approve is not true then
if ret_val.report_class = 1 THEN
PERFORM asset_report__generate_gl(in_id, in_expense_acct);
ELSIF ret_val.report_class = 2 THEN
PERFORM asset_report__disposal_gl(
in_id, in_gain_acct, in_loss_acct, in_cash_acct);
ELSIF ret_val.report_class = 4 THEN
PERFORM asset_disposal__approve(in_id, in_gain_acct, in_loss_acct, (select asset_account_id from asset_class
where id = ret_val.asset_class)
);
ELSE RAISE EXCEPTION 'Invalid report class';
END IF;
end if;
SELECT * INTO ret_val FROM asset_report WHERE id = in_id;
RETURN ret_val;
end;
$$ language plpgsql;
revoke execute on function asset_report__approve(int, int, int, int, int) from public;
COMMENT ON function asset_report__approve(int, int, int, int, int) is
$$ This function approves an asset report (whether depreciation or disposal).
Also generates relevant GL drafts for review and posting.$$;
DROP FUNCTION IF EXISTS asset_report__disposal_gl(int, int, int);
CREATE OR REPLACE FUNCTION asset_report__disposal_gl
(in_id int, in_gain_acct int, in_loss_acct int, in_cash_acct int)
RETURNS bool AS
$$
INSERT INTO gl (reference, description, transdate, approved, trans_type_code)
SELECT setting_increment('glnumber'), 'Asset Report ' || asset_report.id,
report_date, false, 'fd'
FROM asset_report
WHERE asset_report.id = in_id;
-- Clear cumulative depreciation account
INSERT
INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc,
approved, transdate)
SELECT a.dep_account_id, currval('id')::int, sum(r.accum_depreciation) * -1,
defaults_get_defaultcurrency(), sum(r.accum_depreciation) * -1,
TRUE, r.disposed_on
FROM asset_report__get_disposal(in_id) r
JOIN asset_item a ON (r.id = a.id)
GROUP BY a.dep_account_id, r.disposed_on
HAVING sum(r.accum_depreciation) <> 0;
-- Add cash from sale(=disposal)
INSERT
INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc,
approved, transdate)
SELECT in_cash_acct, currval('id')::int, sum(r.disposal_amt) * -1,
defaults_get_defaultcurrency(), sum(r.disposal_amt) * -1,
TRUE, r.disposed_on
FROM asset_report__get_disposal(in_id) r
JOIN asset_item ai ON (r.id = ai.id)
GROUP BY r.disposed_on
HAVING sum(r.disposal_amt) <> 0;
-- GAIN is negative since it is a debit
INSERT
INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc,
approved, transdate)
SELECT case when sum(r.gain_loss) > 0 THEN in_loss_acct else in_gain_acct end,
currval('id')::int, sum(r.gain_loss), defaults_get_defaultcurrency(),
sum(r.gain_loss),
TRUE, r.disposed_on
FROM asset_report__get_disposal(in_id) r
JOIN asset_item ai ON (r.id = ai.id)
GROUP BY r.disposed_on;
-- Clear asset from asset account
INSERT
INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc,
approved, transdate)
SELECT a.asset_account_id, currval('id')::int, sum(r.purchase_value),
defaults_get_defaultcurrency(), sum(r.purchase_value),
TRUE, r.disposed_on
FROM asset_report__get_disposal(in_id) r
JOIN asset_item a ON (r.id = a.id)
GROUP BY a.asset_account_id, r.disposed_on
HAVING sum(r.purchase_value) <> 0;
SELECT TRUE;
$$ language sql;
COMMENT ON FUNCTION asset_report__disposal_gl
(in_id int, in_gain_acct int, in_loss_acct int, in_cash_acct int) IS
$$ Generates GL transactions for ful disposal reports.$$;
CREATE OR REPLACE FUNCTION asset_item__add_note(in_id int, in_subject text, in_note text)
RETURNS asset_note AS
$$
INSERT INTO asset_note (ref_key, subject, note)
values (in_id, in_subject, in_note);
SELECT * FROM asset_note WHERE id = currval('note_id_seq');
$$ language sql;
COMMENT ON FUNCTION asset_item__add_note(in_id int, in_subject text,
in_note text) IS $$ Adds a note to an asset item$$;
CREATE OR REPLACE FUNCTION asset_report__get_expense_accts()
RETURNS SETOF account
AS $$
SELECT * FROM account__get_by_link_desc('asset_expense');
$$ language sql;
COMMENT ON FUNCTION asset_report__get_expense_accts() IS
$$ Lists all asset expense reports.$$;
CREATE OR REPLACE FUNCTION asset_report__get_gain_accts()
RETURNS SETOF account
AS $$
SELECT * FROM account__get_by_link_desc('asset_gain');
$$ language sql;
COMMENT ON FUNCTION asset_report__get_gain_accts() IS
$$ Returns a list of gain accounts for asset depreciation and disposal reports.
$$;
CREATE OR REPLACE FUNCTION asset_report__get_loss_accts()
RETURNS SETOF account
AS $$
SELECT * FROM account__get_by_link_desc('asset_loss');
$$ language sql;
CREATE OR REPLACE FUNCTION asset_report__get_cash_accts()
RETURNS SETOF account
AS $$
SELECT * FROM account where not obsolete;
$$ language sql;
COMMENT ON FUNCTION asset_report__get_loss_accts() IS
$$ Returns a list of loss accounts for asset depreciation and disposal reports.
$$;
CREATE OR REPLACE FUNCTION asset_report__get(in_id int)
RETURNS asset_report
AS
$$
select * from asset_report where id = in_id;
$$ language sql;
COMMENT ON FUNCTION asset_report__get(in_id int) IS
$$ Returns the asset_report line identified by id.$$;
DROP TYPE IF EXISTS asset_report_line_result CASCADE;
CREATE TYPE asset_report_line_result AS(
tag text,
start_depreciation date,
purchase_value numeric,
method_short_name text,
usable_life numeric,
basis numeric,
prior_through date,
prior_dep numeric,
dep_this_time numeric,
dep_ytd numeric,
dep_total numeric,
description text,
purchase_date date
);
CREATE OR REPLACE FUNCTION asset_report__get_lines(in_id int)
RETURNS SETOF asset_report_line_result
as $$
select ai.tag, ai.start_depreciation, ai.purchase_value, m.short_name,
ai.usable_life,
ai.purchase_value - ai.salvage_value, max(pr.report_date),
sum(case when pr.report_date < r.report_date then prl.amount
else 0
end),
rl.amount,
sum (case when extract(year from pr.report_date)
= extract(year from r.report_date)
AND pr.report_date < r.report_date
then prl.amount
else 0
end),
sum(prl.amount),
ai.description, ai.purchase_date
FROM asset_item ai
JOIN asset_class c ON (ai.asset_class_id = c.id)
JOIN asset_dep_method m ON (c.method = m.id)
JOIN asset_report_line rl ON (rl.asset_id = ai.id)
JOIN asset_report r ON (rl.report_id = r.id)
LEFT JOIN asset_report_line prl ON (prl.asset_id = ai.id)
LEFT JOIN asset_report pr ON (prl.report_id = pr.id)
WHERE rl.report_id = in_id
GROUP BY ai.tag, ai.start_depreciation, ai.purchase_value, m.short_name,
ai.usable_life, ai.salvage_value, r.report_date, rl.amount,
ai.description, ai.purchase_date;
$$ language sql;
COMMENT ON FUNCTION asset_report__get_lines(in_id int) IS
$$ Returns the lines of an asset depreciation report.$$;
DROP TYPE IF EXISTS asset_report_result CASCADE;
CREATE TYPE asset_report_result AS (
id int,
report_date date,
gl_id bigint,
asset_class bigint,
report_class int,
entered_by bigint,
approved_by bigint,
entered_at timestamp,
approved_at timestamp,
depreciated_qty numeric,
dont_approve bool,
submitted bool,
total numeric
);
CREATE OR REPLACE FUNCTION asset_report__search