Skip to content

Commit ac708d3

Browse files
committed
Implement tests for the 'reconcile' action
1 parent 32282ac commit ac708d3

File tree

3 files changed

+497
-33
lines changed

3 files changed

+497
-33
lines changed

lib/LedgerSMB/Workflow/Action/Reconciliation.pm

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ sub _delete($self, $wf) {
239239
#
240240
#####################################
241241

242-
sub _reconcile_source_id( $stmt, $source_id, $book_todo, $book_done ) {
242+
sub _reconcile_source_id( $stmt_todo, $idx, $source_id, $book_todo, $recon_done ) {
243+
my $stmt = $stmt_todo->[$idx];
243244
my $lc_source_id = lc($source_id);
245+
print STDERR "source_id: $lc_source_id\n";
244246
my $candidates = [
245247
grep {
246248
lc($book_todo->[$_]->{source}) eq $lc_source_id
@@ -249,12 +251,13 @@ sub _reconcile_source_id( $stmt, $source_id, $book_todo, $book_done ) {
249251
];
250252

251253
return unless $candidates->@*;
254+
print STDERR "Have candidates\n";
252255

253256
if (scalar($candidates->@*) == 1) {
254-
my $found = splice $book_todo->@*, $candidates->[0], 1;
255-
$found->{recon_group} = $stmt->{id};
256-
$stmt->{recon_group} = $stmt->{id};
257-
push $book_done->@*, $found;
257+
push $recon_done->@*, {
258+
book => [ splice $book_todo->@*, $candidates->[0], 1 ],
259+
stmt => [ splice $stmt_todo->@*, $idx, 1 ],
260+
};
258261
return;
259262
}
260263

@@ -268,55 +271,62 @@ sub _reconcile_source_id( $stmt, $source_id, $book_todo, $book_done ) {
268271

269272
return unless $candidates->@*;
270273

271-
my $found = splice $book_todo->@*, $candidates->[0], 1;
272-
$found->{recon_group} = $stmt->{id};
273-
$stmt->{recon_group} = $stmt->{id};
274-
push $book_done->@*, $found;
274+
push $recon_done->@*, {
275+
book => [ splice $book_todo->@*, $candidates->[0], 1 ],
276+
stmt => [ splice $stmt_todo->@*, $idx, 1 ],
277+
};
275278
return;
276279
}
277280

278-
sub _reconcile_no_nource_id($stmt, $prefix, $book_todo, $book_done) {
281+
sub _reconcile_no_source_id($stmt_todo, $idx, $prefix,
282+
$book_todo, $recon_done) {
283+
my $stmt = $stmt_todo->[$idx];
279284
my $candidates = [
280285
grep {
281286
$book_todo->[$_]->{amount} == $stmt->{amount}
282287
and $book_todo->[$_]->{post_date} eq $stmt->{post_date}
283-
and $book_todo->[$_]->{source} !~ m/^\Q$prefix\E/
288+
and (not $book_todo->[$_]->{source}
289+
or $book_todo->[$_]->{source} !~ m/^\Q$prefix\E/)
284290
} 0..$book_todo->$#*
285291
];
286292

287293
return unless $candidates->@*;
288294

289-
my $found = splice $book_todo->@*, $candidates->[0], 1;
290-
$found->{recon_group} = $stmt->{id};
291-
$stmt->{recon_group} = $stmt->{id};
292-
push $book_done->@*, $found;
293-
return;
295+
push $recon_done->@*, {
296+
stmt => [ splice $book_todo->@*, $candidates->[0], 1 ],
297+
book => [ splice $stmt_todo->@*, $idx, 1 ],
298+
};
299+
return 1;
294300
}
295301

296302
sub _reconcile($self, $wf) {
297303
my $stmt_todo = $wf->context->param( '_stmt_todo' );
298304
my $book_todo = $wf->context->param( '_book_todo' );
299-
my $book_done = $wf->context->param( '_book_done' );
305+
my $recon_done = $wf->context->param( '_recon_done' );
300306
my $prefix = $wf->context->param( '_prefix' );
301307

302-
for my $stmt ($stmt_todo->@*) {
308+
for my ($idx, $stmt) (indexed $stmt_todo->@*) {
303309
my $source_id;
304-
if (defined $stmt->{source_id}) {
305-
if ($stmt->{source_id} =~ m/^[0-9]+$/) {
306-
$source_id = $prefix . $stmt->{source_id};
310+
if (defined $stmt->{source}) {
311+
if ($stmt->{source} =~ m/^[0-9]+$/) {
312+
$source_id = "$prefix $stmt->{source}";
307313
}
308-
elsif ($stmt->{source_id} ne '') {
309-
$source_id = $stmt->{source_id};
314+
elsif ($stmt->{source} ne '') {
315+
$source_id = $stmt->{source};
310316
}
311317
}
312318

313319
if (defined $source_id) {
314-
_reconcile_source_id( $stmt, $source_id, $book_todo, $book_done );
320+
_reconcile_source_id(
321+
$stmt_todo, $idx, $source_id, $book_todo, $recon_done );
315322
return;
316323
}
317324

318-
_reconcle_no_source_id( $stmt, $prefix, $book_todo, $book_done );
325+
_reconcile_no_source_id(
326+
$stmt_todo, $idx, $prefix, $book_todo, $recon_done );
319327
}
328+
329+
return;
320330
}
321331

322332
#####################################

lib/LedgerSMB/Workflow/Persister/Reconciliation.pm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,35 @@ sub create_workflow($self, $wf) {
5656
return $wf_id;
5757
}
5858

59+
=head2 fetch_workflow
60+
61+
=cut
62+
63+
sub fetch_workflow($self, $wf_id) {
64+
my $wf_info = $self->SUPER::fetch_workflow($wf_id);
65+
return unless $wf_info;
66+
67+
my $dbh = $self->handle;
68+
$wf_info->{context} //= {};
69+
70+
# Retrieve pending book items (acc_trans lines)
71+
$wf_info->{context}->{_pending_items} = [];
72+
73+
# Retrieve todo book lines
74+
$wf_info->{context}->{_book_todo} = [];
75+
76+
# Retrieve todo statement lines
77+
$wf_info->{context}->{_stmt_todo} = [];
78+
79+
# Retrieve reconciled book+statement items
80+
$wf_info->{context}->{_book_done} = [];
81+
82+
# Retrieve draft transaction lines before report closing date
83+
$wf_info->{preceeding_draft_count} = 0;
84+
85+
return $wf_info;
86+
}
87+
5988
=head2 update_workflow
6089
6190
Stores updates to existing workflows.

0 commit comments

Comments
 (0)