Skip to content

Commit

Permalink
Merge pull request #4816 from ehuelsmann/1.7-overpayment-reversal
Browse files Browse the repository at this point in the history
1.7 overpayment reversal
  • Loading branch information
ehuelsmann committed Jul 25, 2020
2 parents 1faa419 + f3e7a46 commit d634711
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/LedgerSMB/Scripts/reports.pm
Expand Up @@ -228,7 +228,7 @@ Reverses overpayments selected from the search overpayments screen.

sub reverse_overpayment {
my ($request) = @_;
my $payment = LedgerSMB::DBObject::Payment->new(%$request);
my $payment = LedgerSMB::DBObject::Payment->new({ base => $request });
for my $rc (1 .. $request->{rowcount_}){
next unless $request->{"select_$rc"};
my $args = {id => $request->{"select_$rc"}};
Expand Down
2 changes: 2 additions & 0 deletions lib/LedgerSMB/Scripts/vouchers.pm
Expand Up @@ -208,6 +208,7 @@ sub add_vouchers {
$request->{account_class} = 1;
if ($request->{overpayment}){
$request->{report_name} = 'overpayments';
$request->{post_date} = $request->{batch_date};
return LedgerSMB::Scripts::reports::start_report($request);
} else {
return LedgerSMB::Scripts::payment::get_search_criteria($request, $custom_batch_types);
Expand All @@ -220,6 +221,7 @@ sub add_vouchers {
$request->{account_class} = 2;
if ($request->{overpayment}){
$request->{report_name} = 'overpayments';
$request->{post_date} = $request->{batch_date};
return LedgerSMB::Scripts::reports::start_report($request);
} else {
return LedgerSMB::Scripts::payment::get_search_criteria($request, $custom_batch_types);
Expand Down
3 changes: 1 addition & 2 deletions old/lib/LedgerSMB/DBObject/Payment.pm
Expand Up @@ -873,8 +873,7 @@ sub overpayment_reverse {
$args->{post_date},
$args->{batch_id},
$args->{account_class},
$args->{exchangerate},
$args->{curr}] );
] );
}

=item init
Expand Down
52 changes: 34 additions & 18 deletions sql/modules/Payment.sql
Expand Up @@ -1474,8 +1474,12 @@ DROP FUNCTION IF EXISTS overpayment__reverse
(in_id int, in_transdate date, in_batch_id int, in_account_class int,
in_cash_accno text, in_exchangerate numeric, in_curr char(3));

DROP FUNCTION IF EXISTS overpayment__reverse
(in_id int, in_transdate date, in_batch_id int, in_account_class int, in_exchangerate numeric, in_curr char(3));


CREATE OR REPLACE FUNCTION overpayment__reverse
(in_id int, in_transdate date, in_batch_id int, in_account_class int, in_exchangerate numeric, in_curr char(3))
(in_id int, in_transdate date, in_batch_id int, in_account_class int)
returns bool LANGUAGE PLPGSQL AS
$$
declare t_id int;
Expand All @@ -1498,27 +1502,39 @@ t_id := currval('id');
INSERT INTO voucher (batch_id, trans_id, batch_class)
VALUES (in_batch_id, t_id, CASE WHEN in_account_class = 1 THEN 4 ELSE 7 END);

INSERT INTO acc_trans (transdate, trans_id, chart_id, amount)
SELECT in_transdate, t_id, chart_id, amount * -1
INSERT INTO acc_trans (transdate, trans_id, chart_id,
amount_bc, curr, amount_tc)
SELECT in_transdate, t_id, chart_id, amount_bc * -1, curr, amount_tc * -1
FROM acc_trans
WHERE trans_id = in_id;

PERFORM * FROM payment__overpayments_list(null, null, null, null, null)
WHERE available<>amount and payment_id = in_id;

IF FOUND THEN
RAISE 'Cannot reverse used overpayment: reverse payments first';
END IF;

-- reverse overpayment usage
PERFORM payment__reverse(ac.source, ac.transdate, eca.id, at.accno,
in_transdate, eca.entity_class, in_batch_id, null,
in_exchangerate, in_curr)
FROM acc_trans ac
JOIN account at ON ac.chart_id = at.id
JOIN account_link al ON at.id = al.account_id AND al.description like 'A%paid'
JOIN (select id, entity_credit_account FROM ar UNION
select id, entity_credit_account from ap) a ON a.id = ac.trans_id
JOIN entity_credit_account eca ON a.entity_credit_account = eca.id
JOIN payment_links pl ON pl.entry_id = ac.entry_id
JOIN overpayments op ON op.payment_id = pl.payment_id
JOIN payment p ON p.id = op.payment_id
WHERE p.gl_id = in_id
GROUP BY ac.source, ac.transdate, eca.id, eca.entity_class,
at.accno, al.description;
--
-- The query below will automatically do what the above simply bails out on.
-- However, it doesn't work and I don't understand it enough - right now -
-- to fix it.
-- PERFORM payment__reverse(ac.source, ac.transdate, eca.id, at.accno,
-- in_transdate, eca.entity_class, in_batch_id, null,
-- in_exchangerate, in_curr)
-- FROM acc_trans ac
-- JOIN account at ON ac.chart_id = at.id
-- JOIN account_link al ON at.id = al.account_id AND al.description like 'A%paid'
-- JOIN (select id, entity_credit_account FROM ar UNION
-- select id, entity_credit_account from ap) a ON a.id = ac.trans_id
-- JOIN entity_credit_account eca ON a.entity_credit_account = eca.id
-- JOIN payment_links pl ON pl.entry_id = ac.entry_id
-- JOIN overpayments op ON op.payment_id = pl.payment_id
-- JOIN payment p ON p.id = op.payment_id
-- WHERE p.gl_id = in_id
-- GROUP BY ac.source, ac.transdate, eca.id, eca.entity_class,
-- at.accno, al.description;

RETURN TRUE;
END;
Expand Down
3 changes: 1 addition & 2 deletions sql/modules/Voucher.sql
Expand Up @@ -363,8 +363,7 @@ $$

UPDATE gl SET approved = true
WHERE id IN (select trans_id FROM voucher
WHERE batch_id = in_batch_id
AND batch_class = 5);
WHERE batch_id = in_batch_id);

UPDATE acc_trans SET approved = true
WHERE voucher_id IN (select id FROM voucher
Expand Down
2 changes: 1 addition & 1 deletion xt/42-payment.pg
Expand Up @@ -21,7 +21,7 @@ BEGIN;
-- Validate required functions

SELECT has_function('business_type__list','{}'::text[]);
SELECT has_function('overpayment__reverse',ARRAY['integer','date','integer','integer','numeric','character']);
SELECT has_function('overpayment__reverse',ARRAY['integer','date','integer','integer']);
SELECT has_function('payment_bulk_post',ARRAY['numeric[]','integer','text','numeric','text','text','date','integer','numeric','text']);
SELECT has_function('payment_gather_header_info',ARRAY['integer','integer']);
SELECT has_function('payment_gather_line_info',ARRAY['integer','integer']);
Expand Down

0 comments on commit d634711

Please sign in to comment.