Skip to content

Commit 1f49e96

Browse files
committed
Fix database level payment tests
1 parent a4ccbe3 commit 1f49e96

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
lines changed

sql/modules/Payment.sql

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ DROP TYPE IF EXISTS payment_invoice CASCADE;
194194

195195
CREATE TYPE payment_invoice AS (
196196
invoice_id int,
197+
open_item_id int,
197198
invnumber text,
198199
invoice bool,
199200
invoice_date date,
@@ -221,7 +222,7 @@ RETURNS SETOF payment_invoice AS
221222
$$
222223
BEGIN
223224
RETURN QUERY EXECUTE $sql$
224-
SELECT a.id AS invoice_id, a.invnumber AS invnumber,a.invoice AS invoice,
225+
SELECT a.id AS invoice_id, a.open_item_id, a.invnumber AS invnumber,a.invoice AS invoice,
225226
a.transdate AS invoice_date, a.amount_bc AS amount,
226227
a.amount_tc,
227228
(CASE WHEN (c.discount_terms||' days')::interval < age(coalesce($8, current_date), a.transdate)
@@ -242,16 +243,13 @@ RETURN QUERY EXECUTE $sql$
242243
END) AS due_fx,
243244
null::numeric AS exchangerate,
244245
a.description
245-
--TODO HV prepare drop entity_id from ap,ar
246-
--FROM (SELECT id, invnumber, transdate, amount, entity_id,
247-
FROM (SELECT txn.id, invnumber, invoice, txn.transdate, amount_bc,
246+
FROM (SELECT txn.id, open_item_id, invnumber, invoice, txn.transdate, amount_bc,
248247
amount_tc,
249248
1 as invoice_class, curr,
250249
entity_credit_account, txn.approved, description
251250
FROM ap JOIN transactions txn ON txn.id = ap.id
252251
UNION
253-
--SELECT id, invnumber, transdate, amount, entity_id,
254-
SELECT txn.id, invnumber, invoice, txn.transdate, amount_bc,
252+
SELECT txn.id, open_item_id, invnumber, invoice, txn.transdate, amount_bc,
255253
amount_tc,
256254
2 AS invoice_class, curr,
257255
entity_credit_account, txn.approved, description
@@ -286,8 +284,8 @@ RETURN QUERY EXECUTE $sql$
286284
OR $7 IS NULL)
287285
AND due <> 0
288286
AND a.approved = true
289-
GROUP BY a.invnumber, a.transdate, a.amount_bc, amount_tc,
290-
discount, discount_tc, ac.due, ac.due_fx, a.id, c.discount_terms,
287+
GROUP BY a.id, a.open_item_id, a.invnumber, a.transdate, a.amount_bc, amount_tc,
288+
discount, discount_tc, ac.due, ac.due_fx, c.discount_terms,
291289
a.curr, a.invoice, a.description
292290
$sql$
293291
USING in_account_class, in_entity_credit_id, in_curr, in_datefrom,
@@ -554,6 +552,7 @@ BEGIN
554552

555553
CREATE TEMPORARY TABLE bulk_payments_in (
556554
id int, -- AR/AP id
555+
open_item_id int, -- open_item.id
557556
payment_id int, -- payment.id
558557
eca_id int, -- entity_credit_account.id
559558
entry_id int, -- acc_trans.entry_id
@@ -572,9 +571,10 @@ BEGIN
572571
LOOP
573572
-- Fill the bulk payments table
574573
IF in_transactions[out_count][2] <> 0 THEN
575-
INSERT INTO bulk_payments_in(id, amount_tc)
576-
VALUES (in_transactions[out_count][1],
577-
in_transactions[out_count][2]);
574+
INSERT INTO bulk_payments_in(id, open_item_id, amount_tc)
575+
VALUES (in_transactions[out_count][1],
576+
in_transactions[out_count][2],
577+
in_transactions[out_count][3]);
578578
END IF;
579579
END LOOP;
580580

@@ -747,15 +747,15 @@ BEGIN
747747
SET entry_id = nextval('acc_trans_entry_id_seq');
748748
INSERT INTO acc_trans
749749
(trans_id, chart_id, amount_bc, curr, amount_tc, approved,
750-
voucher_id, transdate, source, entry_id)
750+
voucher_id, transdate, source, entry_id, open_item_id)
751751
SELECT bpi.id, t_ar_ap_id,
752752
(bpi.amount_tc + bpi.disc_amount_tc)
753753
* t_cash_sign * -1 * bpi.fxrate, in_currency,
754754
(bpi.amount_tc + bpi.disc_amount_tc)
755755
* t_cash_sign * -1,
756756
CASE WHEN t_voucher_id IS NULL THEN true
757757
ELSE false END,
758-
t_voucher_id, in_payment_date, in_source, entry_id
758+
t_voucher_id, in_payment_date, in_source, entry_id, open_item_id
759759
FROM bulk_payments_in bpi
760760
JOIN entity_credit_account eca ON bpi.eca_id = eca.id;
761761
INSERT INTO payment_links (payment_id, entry_id, type)
@@ -816,6 +816,7 @@ CREATE OR REPLACE FUNCTION payment_post
816816
in_source text[],
817817
in_memo text[],
818818
in_transaction_id int[],
819+
in_open_item_id int[],
819820
in_op_amount numeric[],
820821
in_op_cash_account_id int[],
821822
in_op_source text[],
@@ -938,9 +939,9 @@ BEGIN
938939
WHERE trans_id = in_transaction_id[out_count]
939940
AND ( l.description in ('AR', 'AP'));
940941

941-
-- Now we post the AP/AR transaction
942+
-- Now we post the AP/AR account
942943
INSERT INTO acc_trans (chart_id, amount_bc, curr, amount_tc,
943-
trans_id, transdate, approved, source, memo)
944+
trans_id, transdate, approved, source, memo, open_item_id)
944945
VALUES (var_account_id,
945946
in_amount[out_count]*old_exchangerate*sign*-1,
946947
in_curr,
@@ -949,7 +950,8 @@ BEGIN
949950
in_datepaid,
950951
coalesce(in_approved, true),
951952
in_source[out_count],
952-
in_memo[out_count]);
953+
in_memo[out_count],
954+
in_open_item_id[out_count]);
953955
-- Link the ledger line to the payment record
954956
INSERT INTO payment_links
955957
VALUES (var_payment_id, currval('acc_trans_entry_id_seq'), 1);
@@ -1064,6 +1066,7 @@ COMMENT ON FUNCTION payment_post
10641066
in_source text[],
10651067
in_memo text[],
10661068
in_transaction_id int[],
1069+
in_open_item_id int[],
10671070
in_op_amount numeric[],
10681071
in_op_cash_account_id int[],
10691072
in_op_source text[],
@@ -1282,13 +1285,14 @@ BEGIN
12821285
INSERT INTO acc_trans (trans_id, chart_id, transdate, source,
12831286
cleared, memo, invoice_id, approved,
12841287
amount_bc, amount_tc, curr,
1285-
voucher_id)
1288+
voucher_id, open_item_id)
12861289
SELECT trans_id, chart_id, in_payment_date, source,
12871290
false, memo, null, coalesce(in_approved, true),
12881291
-1 * amount_bc, -1 * amount_tc, curr,
12891292
(select id from voucher v
12901293
where a.trans_id = v.trans_id
1291-
and v.batch_id = in_batch_id) as voucher_id
1294+
and v.batch_id = in_batch_id) as voucher_id,
1295+
open_item_id
12921296
FROM acc_trans a
12931297
WHERE exists (select 1 from payment_links pl
12941298
where pl.payment_id = in_payment_id

xt/42-payment-fx.pg

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ BEGIN;
4444
insert into session (session_id, users_id, token, last_used)
4545
values (-200, -200, md5(random()::text), now());
4646

47-
WITH a (accno, description, category) as (
48-
values ('00001'::text, 'testing AP'::text, 'L'::text ),
49-
('00002', 'testing AP cost', 'E'),
50-
('00003', 'testing cash', 'A'),
51-
('00004', 'fx gain', 'I'),
52-
('00005', 'fx loss', 'E')
47+
WITH a (accno, description, category, open_item_managed) as (
48+
values ('00001'::text, 'testing AP'::text, 'L'::text, true),
49+
('00002', 'testing AP cost', 'E', false),
50+
('00003', 'testing cash', 'A', false),
51+
('00004', 'fx gain', 'I', false),
52+
('00005', 'fx loss', 'E', false)
5353
)
54-
INSERT INTO account (accno, description, category, heading)
55-
SELECT a.accno, a.description, a.category, h.id
54+
INSERT INTO account (accno, description, category, heading, open_item_managed)
55+
SELECT a.accno, a.description, a.category, h.id, a.open_item_managed
5656
FROM account_heading h
5757
JOIN a ON h.accno = '000000000000000000000';
5858

@@ -81,9 +81,9 @@ BEGIN;
8181
VALUES ('inv_test1', 'ap', (select id from account where accno = '00001'));
8282
INSERT INTO ap (id, open_item_id, amount_bc, amount_tc, invnumber, curr, entity_credit_account)
8383
VALUES (-11, currval('open_item_id_seq'), 100, 110, 'inv_test1', 'xts', -101);
84-
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id)
85-
VALUES (-11, '1901-01-01', 100, 'xts', 110, 't', (select id from account where accno = '00001')),
86-
(-11, '1901-01-01', -100, 'xts', -110, 't', (select id from account where accno = '00002'));
84+
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id, open_item_id)
85+
VALUES (-11, '1901-01-01', 100, 'xts', 110, 't', (select id from account where accno = '00001'), currval('open_item_id_seq')),
86+
(-11, '1901-01-01', -100, 'xts', -110, 't', (select id from account where accno = '00002'), null);
8787

8888
-- Pay the invoice in full
8989
SELECT * FROM
@@ -99,6 +99,7 @@ BEGIN;
9999
ARRAY['cash '], -- source
100100
ARRAY[NULL], -- memo
101101
ARRAY[-11], -- transaction_id
102+
ARRAY[currval('open_item_id_seq')::int],
102103
NULL, -- op_amount
103104
NULL, -- op_cash_account_id
104105
NULL, -- op_source
@@ -135,12 +136,12 @@ BEGIN;
135136
VALUES ('inv_test2', 'ap', (select id from account where accno = '00001'));
136137
INSERT INTO ap (id, open_item_id, amount_bc, amount_tc, invnumber, curr, entity_credit_account)
137138
VALUES (-12, currval('open_item_id_seq'), 100, 110, 'inv_test2', 'xts', -101);
138-
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id)
139-
VALUES (-12, '1901-01-02', 100, 'xts', 110, 't', (select id from account where accno = '00001')),
140-
(-12, '1901-01-02', -100, 'xts', -110, 't', (select id from account where accno = '00002'));
139+
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id, open_item_id)
140+
VALUES (-12, '1901-01-02', 100, 'xts', 110, 't', (select id from account where accno = '00001'), currval('open_item_id_seq')),
141+
(-12, '1901-01-02', -100, 'xts', -110, 't', (select id from account where accno = '00002'), null);
141142

142143

143-
SELECT payment_bulk_post(ARRAY[ARRAY[-12,110]],
144+
SELECT payment_bulk_post(ARRAY[ARRAY[-12,currval('open_item_id_seq'),110]],
144145
batch_create('TestBatch', 'TestBatch',
145146
'payment', -- payment
146147
'1901-01-03'::date), 'source',
@@ -175,9 +176,9 @@ BEGIN;
175176
VALUES ('inv_test1/2', 'ap', (select id from account where accno = '00001'));
176177
INSERT INTO ap (id, open_item_id, amount_bc, amount_tc, invnumber, curr, entity_credit_account)
177178
VALUES (-13, currval('open_item_id_seq'), 100, 110, 'inv_test1', 'xts', -101);
178-
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id)
179-
VALUES (-13, '1901-01-01', 100, 'xts', 110, 't', (select id from account where accno = '00001')),
180-
(-13, '1901-01-01', -100, 'xts', -110, 't', (select id from account where accno = '00002'));
179+
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id, open_item_id)
180+
VALUES (-13, '1901-01-01', 100, 'xts', 110, 't', (select id from account where accno = '00001'), currval('open_item_id_seq')),
181+
(-13, '1901-01-01', -100, 'xts', -110, 't', (select id from account where accno = '00002'), null);
181182

182183

183184
PREPARE test AS
@@ -194,6 +195,7 @@ BEGIN;
194195
ARRAY['cash '], -- source
195196
ARRAY[NULL], -- memo
196197
ARRAY[-13], -- transaction_id
198+
ARRAY[currval('open_item_id_seq')::int], -- open_item_id
197199
NULL, -- op_amount
198200
NULL, -- op_cash_account_id
199201
NULL, -- op_source
@@ -214,13 +216,13 @@ BEGIN;
214216
VALUES ('inv_test2/2', 'ap', (select id from account where accno = '00001'));
215217
INSERT INTO ap (id, open_item_id, amount_bc, amount_tc, invnumber, curr, entity_credit_account)
216218
VALUES (-14, currval('open_item_id_seq'), 100, 110, 'inv_test2', 'xts', -101);
217-
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id)
218-
VALUES (-14, '1901-01-02', 100, 'xts', 110, 't', (select id from account where accno = '00001')),
219-
(-14, '1901-01-02', -100, 'xts', -110, 't', (select id from account where accno = '00002'));
219+
INSERT INTO acc_trans (trans_id, transdate, amount_bc, curr, amount_tc, approved, chart_id, open_item_id)
220+
VALUES (-14, '1901-01-02', 100, 'xts', 110, 't', (select id from account where accno = '00001'), currval('open_item_id_seq')),
221+
(-14, '1901-01-02', -100, 'xts', -110, 't', (select id from account where accno = '00002'), null);
220222

221223

222224
PREPARE test AS
223-
SELECT payment_bulk_post(ARRAY[ARRAY[-14,110]],
225+
SELECT payment_bulk_post(ARRAY[ARRAY[-14,currval('open_item_id_seq'),110]],
224226
batch_create('TestBatch', 'TestBatch',
225227
'payment', -- payment
226228
'1901-01-03'::date), 'source',

xt/42-payment.pg

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ BEGIN;
3737
SELECT has_function('payment_get_unused_overpayment',ARRAY['integer','integer','integer']);
3838
SELECT has_function('payment_get_vc_info',ARRAY['integer','integer']);
3939
SELECT has_function('payment__overpayments_list',ARRAY['date','date','text','text','text']);
40-
SELECT has_function('payment_post',ARRAY['date','integer','integer','character','numeric','text','text','integer[]','numeric[]','text[]','text[]','integer[]','numeric[]','integer[]','text[]','text[]','integer[]','integer[]','boolean']);
40+
SELECT has_function('payment_post',ARRAY['date','integer','integer','character','numeric','text','text','integer[]','numeric[]','text[]','text[]','integer[]','integer[]','numeric[]','integer[]','text[]','text[]','integer[]','integer[]','boolean']);
4141
SELECT has_function('payment__reverse',ARRAY['integer','date','boolean','integer']);
4242
SELECT has_function('payment__search',ARRAY['text','date','date','integer','text','integer','character','text']);
4343
SELECT has_function('payments_get_open_currencies',ARRAY['integer']);
@@ -340,7 +340,7 @@ BEGIN;
340340
values (currval('transactions_id_seq'),
341341
currval('open_item_id_seq'), 'test_reverse', -101, '1', '1', '1',
342342
'XTS')
343-
returning id as invoice_id
343+
returning id as invoice_id, open_item_id
344344
\gset
345345

346346
INSERT INTO acc_trans (approved, transdate, amount_bc, curr, amount_tc,
@@ -366,6 +366,7 @@ BEGIN;
366366
array[1]::numeric[],
367367
array['pmt-rev-src1']::text[], NULL,
368368
array[:invoice_id]::int[],
369+
array[:open_item_id]::int[],
369370
-- overpayments section
370371
null, null, null, null, null, null,
371372
-- approved:
@@ -410,15 +411,15 @@ BEGIN;
410411
INSERT INTO transactions (transdate, table_name, trans_type_code, approved)
411412
VALUES (now(), 'ap', 'ap', true);
412413
INSERT INTO open_item (item_number, item_type, account_id)
413-
VALUES ('test_bulk', 'ap', (select id from account where accno = '-2000000000'));
414+
VALUES ('test_bulk', 'ap', (select id from account where accno = '00001'));
414415
INSERT INTO ap (id, open_item_id, invnumber, entity_credit_account,
415416
amount_bc, amount_tc, netamount_bc, curr)
416417
values (currval('transactions_id_seq'),
417418
currval('open_item_id_seq'), 'test_bulk', -101, '1', '1', '1',
418419
'XTS')
419-
returning id as invoice_id
420+
returning id as invoice_id, open_item_id
420421
\gset
421-
SELECT payment_bulk_post(ARRAY[ARRAY[(:invoice_id)::numeric, 1::numeric]]::numeric[],
422+
SELECT payment_bulk_post(ARRAY[ARRAY[(:invoice_id)::numeric, (:open_item_id)::numeric, 1::numeric]]::numeric[],
422423
(:batch_id)::int, 'bulk'::text, '00001'::text, '00003'::text, now()::date,
423424
1::int, 1::numeric, 'XTS'::text);
424425
SELECT currval('payment_id_seq') as payment_id

0 commit comments

Comments
 (0)