-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.sql
153 lines (145 loc) · 5.66 KB
/
2.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
CREATE OR REPLACE FUNCTION transaction_first_activity(id VARCHAR(10), before_after CHAR(1)) RETURNS TABLE (trndate DATE, trntime TIME) AS $$
BEGIN
RETURN QUERY SELECT S.trndate, S.trntime
FROM trn_src_des AS S,
(
SELECT *
FROM trn_src_des
WHERE voucherid = id
) AS T
WHERE
CASE
WHEN before_after = 'B' THEN
s.desdep = t.sourcedep
AND (s.trndate < t.trndate
OR (s.trndate = t.trndate
AND s.trntime <= t.trntime))
WHEN before_after = 'A' THEN
s.sourcedep = t.desdep
AND (s.trndate > t.trndate
OR (s.trndate = t.trndate
AND s.trntime >= t.trntime))
END
ORDER BY
CASE WHEN before_after = 'A' THEN S.trndate END ASC,
CASE WHEN before_after = 'A' THEN S.trntime END ASC,
CASE WHEN before_after = 'B' THEN S.trndate END DESC,
CASE WHEN before_after = 'B' THEN S.trntime END DESC
LIMIT 1;
END;
$$ LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION is_deposit_registered_in_bank(id int) RETURNS bool AS $$
BEGIN
IF EXISTS (SELECT * FROM deposit WHERE dep_id = id) THEN RETURN true;
ELSE RETURN FALSE;
END IF;
END;
$$ LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION get_related_transactions(id VARCHAR(10), before_after CHAR(1)) RETURNS TABLE (voucherid VARCHAR(10),
trndate DATE, trntime TIME,
amount BIGINT, sourcedep INT,
desdep INT, branch_id INT,
trn_desc VARCHAR(255)) AS $$
DECLARE sum INT;
DECLARE original_transaction RECORD;
DECLARE row RECORD;
BEGIN
sum := 0;
SELECT trn_src_des.* INTO original_transaction FROM trn_src_des WHERE trn_src_des.voucherid = id;
CREATE TEMP TABLE first_day_transactions AS
(SELECT S.*
FROM
trn_src_des AS S,
(SELECT * from transaction_first_activity(id,before_after)) AS activity
WHERE
CASE
WHEN before_after = 'B' THEN
S.desdep = original_transaction.sourcedep
WHEN before_after = 'A' THEN
S.sourcedep = original_transaction.desdep
END
AND S.trndate = activity.trndate);
RETURN QUERY SELECT * FROM first_day_transactions;
sum := sum + (SELECT SUM(F.amount) FROM first_day_transactions AS F WHERE F.amount<>original_transaction.amount);
DROP TABLE first_day_transactions;
FOR row IN SELECT S.*
FROM
trn_src_des AS S,
(SELECT * from transaction_first_activity(id,before_after)) AS activity
WHERE
CASE
WHEN before_after = 'B' THEN
(s.desdep = original_transaction.sourcedep
AND S.trndate < activity.trndate)
WHEN before_after = 'A' THEN
(S.sourcedep = original_transaction.desdep
AND S.trndate > activity.trndate)
END
ORDER BY
CASE WHEN before_after = 'B' THEN S.trndate END DESC,
CASE WHEN before_after = 'B' THEN S.trntime END DESC,
CASE WHEN before_after = 'A' THEN S.trndate END ASC,
CASE WHEN before_after = 'A' THEN S.trntime END ASC
LOOP
IF sum + row.amount <= original_transaction.amount*1.1
THEN
voucherid := row.voucherid;
trndate := row.trndate;
trntime := row.trntime;
amount := row.amount;
sourcedep := row.sourcedep;
desdep := row.desdep;
branch_id := row.branch_id;
trn_desc := row.trn_desc;
sum := sum + row.amount;
RETURN NEXT;
ELSE EXIT;
END IF;
END LOOP;
END;
$$ LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION get_all_related_transactions(id VARCHAR(10), before_after CHAR(1)) RETURNS TABLE (voucherid VARCHAR(10),
trndate DATE, trntime TIME,
amount BIGINT, sourcedep INT,
desdep INT, branch_id INT, trn_desc VARCHAR(255)) AS $$
DECLARE row RECORD;
BEGIN
FOR row IN SELECT *
FROM get_related_transactions(id,before_after)
LOOP
IF before_after = 'B' AND row.sourcedep IS NOT NULL AND is_deposit_registered_in_bank(row.sourcedep) THEN
RETURN QUERY SELECT * FROM get_all_related_transactions(row.voucherid,before_after);
END IF;
IF before_after = 'A' AND row.desdep IS NOT NULL AND is_deposit_registered_in_bank(row.desdep) THEN
RETURN QUERY SELECT * FROM get_all_related_transactions(row.voucherid,before_after);
END IF;
voucherid := row.voucherid;
trndate := row.trndate;
trntime := row.trntime;
amount := row.amount;
sourcedep := row.sourcedep;
desdep := row.desdep;
branch_id := row.branch_id;
trn_desc := row.trn_desc;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION trace_transaction(id VARCHAR(10)) RETURNS TABLE (voucherid VARCHAR(10),
trndate DATE, trntime TIME,
amount BIGINT, sourcedep INT,
desdep INT, branch_id INT, trn_desc VARCHAR(255)) AS $$
BEGIN
RETURN QUERY SELECT * FROM (
(SELECT * FROM trn_src_des WHERE trn_src_des.voucherid = id)
UNION
(SELECT *
FROM
get_all_related_transactions(id,'B'))
UNION
(SELECT *
FROM
get_all_related_transactions(id,'A')))
AS trace ORDER BY trace.trndate ASC, trace.trntime ASC;
END;
$$ LANGUAGE PLPGSQL;