Bugzilla – Attachment 122528 Details for
Bug 15156
Get all Borrowers with pending/unpaid fines/accountlines
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 15156: [ALT] Add filter_by_amount_owed that's chainable
Bug-15156-ALT-Add-filterbyamountowed-thats-chainab.patch (text/plain), 8.41 KB, created by
Martin Renvoize (ashimema)
on 2021-07-02 08:09:30 UTC
(
hide
)
Description:
Bug 15156: [ALT] Add filter_by_amount_owed that's chainable
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2021-07-02 08:09:30 UTC
Size:
8.41 KB
patch
obsolete
>From 502b9ca8a348a0b375af1e0bdf8871d52db1b9b9 Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Fri, 2 Jul 2021 09:07:13 +0100 >Subject: [PATCH] Bug 15156: [ALT] Add filter_by_amount_owed that's chainable > >This patch acts as an alternative here. We pull out the code from bug >11983 and extend it to support filtering of debts by branchcode and >debit_type. > >The advantage of this approach is that we can add the filter into the >middle of a search chain to filter down an pre-existing search. >--- > Koha/Patrons.pm | 76 +++++++++++++++++++ > t/db_dependent/Koha/Patrons.t | 135 +++++++++++++++++++++++++++++++++- > 2 files changed, 210 insertions(+), 1 deletion(-) > >diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm >index 616fefacba..140a591356 100644 >--- a/Koha/Patrons.pm >+++ b/Koha/Patrons.pm >@@ -482,6 +482,82 @@ sub filter_by_attribute_value { > return Koha::Patrons->_new_from_dbic($rs); > } > >+=head3 filter_by_amount_owed >+ >+ Koha::Patrons->filter_by_amount_owed( >+ { >+ less_than => '2.00', >+ more_than => '0.50', >+ debit_type => $debit_type_code, >+ library => $branchcode >+ } >+ ); >+ >+Returns patrons filtered by how much money they owe, between passed limits. >+ >+Optionally limit to debts of a particular debit_type or/and owed to a particular library. >+ >+=head4 arguments hashref >+ >+=over 4 >+ >+=item less_than (optional) - filter out patrons who owe less than Amount >+ >+=item more_than (optional) - filter out patrons who owe more than Amount >+ >+=item debit_type (optional) - filter the amount owed by debit type >+ >+=item library (optional) - filter the amount owed to a particular branch >+ >+=back >+ >+=cut >+ >+sub filter_by_amount_owed { >+ my ( $self, $options ) = @_; >+ >+ return $self >+ unless ( defined($options) >+ && ( $options->{less_than} || $options->{more_than} ) ); >+ >+ my $where = {}; >+ my $group_by = >+ [ map { 'me.' . $_ } $self->_resultset->result_source->columns ]; >+ >+ my $attrs = { >+ join => 'accountlines', >+ group_by => $group_by, >+ '+select' => >+ { sum => 'accountlines.amountoutstanding', '-as' => 'outstanding' }, >+ '+as' => 'outstanding' >+ }; >+ >+ $where->{'accountlines.debit_type_code'} = $options->{debit_type} >+ if defined( $options->{debit_type} ); >+ >+ $where->{'accountlines.branchcode'} = $options->{branchcode} >+ if defined( $options->{branchcode} ); >+ >+ $attrs->{'having'} = [ >+ { 'outstanding' => { '<' => $options->{less_than} } }, >+ { 'outstanding' => undef } >+ ] >+ if ( defined( $options->{less_than} ) >+ && !defined( $options->{more_than} ) ); >+ >+ $attrs->{'having'} = { 'outstanding' => { '>' => $options->{more_than} } } >+ if (!defined( $options->{less_than} ) >+ && defined( $options->{more_than} ) ); >+ >+ $attrs->{'having'}->{'-and'} = [ >+ { 'outstanding' => { '>' => $options->{more_than} } }, >+ { 'outstanding' => { '<' => $options->{less_than} } } >+ ] >+ if ( defined( $options->{less_than} ) >+ && defined( $options->{more_than} ) ); >+ >+ return $self->search( $where, $attrs ); >+} > > =head3 _type > >diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t >index 120e981324..9b0dd5533f 100755 >--- a/t/db_dependent/Koha/Patrons.t >+++ b/t/db_dependent/Koha/Patrons.t >@@ -19,7 +19,7 @@ > > use Modern::Perl; > >-use Test::More tests => 42; >+use Test::More tests => 43; > use Test::Warn; > use Test::Exception; > use Test::MockModule; >@@ -2184,4 +2184,137 @@ subtest 'queue_notice' => sub { > is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode"); > }; > >+subtest 'filter_by_amount_owed' => sub { >+ plan tests => 4; >+ >+ my $library = $builder->build({source => 'Branch' }); >+ my $category = $builder->build({source => 'Category' }); >+ >+ my $new_patron_cf_1 = Koha::Patron->new( >+ { >+ cardnumber => 'test_cn_cf_1', >+ branchcode => $library->{branchcode}, >+ categorycode => $category->{categorycode}, >+ surname => 'surname for patron1', >+ firstname => 'firstname for patron1', >+ userid => 'a_nonexistent_userid_cf_1', >+ } >+ )->store; >+ my $new_patron_cf_2 = Koha::Patron->new( >+ { >+ cardnumber => 'test_cn_cf_2', >+ branchcode => $library->{branchcode}, >+ categorycode => $category->{categorycode}, >+ surname => 'surname for patron2', >+ firstname => 'firstname for patron2', >+ userid => 'a_nonexistent_userid_cf_2', >+ } >+ )->store; >+ my $new_patron_cf_3 = Koha::Patron->new( >+ { >+ cardnumber => 'test_cn_cf_3', >+ branchcode => $library->{branchcode}, >+ categorycode => $category->{categorycode}, >+ surname => 'surname for patron3', >+ firstname => 'firstname for patron3', >+ userid => 'a_nonexistent_userid_cf_3', >+ } >+ )->store; >+ >+ my $results = Koha::Patrons->search( >+ { >+ 'me.borrowernumber' => [ >+ $new_patron_cf_1->borrowernumber, >+ $new_patron_cf_2->borrowernumber, >+ $new_patron_cf_3->borrowernumber >+ ] >+ } >+ ); >+ >+ my $fine1 = $builder->build( >+ { >+ source => 'Accountline', >+ value => { >+ borrowernumber => $new_patron_cf_1->borrowernumber, >+ amountoutstanding => 12.00, >+ amount => 12.00, >+ debit_type_code => 'OVERDUE', >+ branchcode => $library->{branchcode} >+ }, >+ } >+ ); >+ my $fine2 = $builder->build( >+ { >+ source => 'Accountline', >+ value => { >+ borrowernumber => $new_patron_cf_2->borrowernumber, >+ amountoutstanding => 8.00, >+ amount => 8.00, >+ debit_type_code => 'OVERDUE', >+ branchcode => $library->{branchcode} >+ >+ }, >+ } >+ ); >+ my $fine3 = $builder->build( >+ { >+ source => 'Accountline', >+ value => { >+ borrowernumber => $new_patron_cf_2->borrowernumber, >+ amountoutstanding => 10.00, >+ amount => 10.00, >+ debit_type_code => 'OVERDUE', >+ branchcode => $library->{branchcode} >+ }, >+ } >+ ); >+ >+ my $filtered = $results->filter_by_amount_owed(); >+ is( ref($filtered), 'Koha::Patrons', >+'Koha::Patrons->filter_by_amount_owed should return a Koha::Patrons result set in a scalar context' >+ ); >+ >+ my $lower_limit = 12.00; >+ my $upper_limit = 16.00; >+ >+ # Catch user with 1 x 12.00 fine and user with no fines. >+ $filtered = >+ $results->filter_by_amount_owed( { less_than => $upper_limit } ); >+ is( $filtered->_resultset->as_subselect_rs->count, 2, >+"filter_by_amount_owed({ less_than => $upper_limit }) found two patrons" >+ ); >+ >+ # Catch user with 1 x 8.00 and 1 x 10.00 fine >+ $filtered = >+ $results->filter_by_amount_owed( { more_than => $lower_limit } ); >+ is( $filtered->_resultset->as_subselect_rs->count, 1, >+"filter_by_amount_owed({ more_than => $lower_limit }) found two patrons" >+ ); >+ >+ # User with 2 fines falls above upper limit, >+ # user with 1 fine falls below lower limit >+ # and user with no fines falls below lower limit. >+ $filtered = $results->filter_by_amount_owed( >+ { more_than => $lower_limit, less_than => $upper_limit } ); >+ is( $filtered->_resultset->as_subselect_rs->count, 0, >+"filter_by_amount_owed({ more_than => $lower_limit, less_than => $upper_limit }) found one patrons" >+ ); >+ >+ # FIXME: Add tests for branchcode and debit_type filtering here >+ my $library2 = $builder->build({source => 'Branch' }); >+ my $fine4 = $builder->build( >+ { >+ source => 'Accountline', >+ value => { >+ borrowernumber => $new_patron_cf_2->borrowernumber, >+ amountoutstanding => 10.00, >+ amount => 10.00, >+ debit_type_code => 'HOLD', >+ branchcode => $library->{branchcode} >+ }, >+ } >+ ); >+ >+}; >+ > $schema->storage->txn_rollback; >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 15156
:
44640
|
44642
|
46232
|
99981
|
105656
|
109593
|
110886
|
122355
|
122392
|
122528
|
122534
|
124497
|
124508
|
124876
|
124978