View | Details | Raw Unified | Return to bug 11983
Collapse All | Expand All

(-)a/Koha/Patrons.pm (+442 lines)
Lines 65-70 sub search_limited { Link Here
65
    return $self->search( $params, $attributes );
65
    return $self->search( $params, $attributes );
66
}
66
}
67
67
68
=head3 filter_by_is_guarantor
69
70
    Koha::Patrons->filter_by_is_guarantor(1);
71
72
Returns patrons filtered by whether they have a guarantor, are a guarantor or are not a guarantor.
73
74
=head4 options
75
76
=over 4
77
78
=item undef - do not apply filter
79
80
=item 1 - boolean to limit to NOT guarantors
81
82
=item 0 - boolean limit to ONLY guarantors
83
84
=back
85
86
=cut
87
88
sub filter_by_is_guarantor {
89
    my ( $self, $option ) = @_;
90
91
    return $self unless defined($option);
92
93
    my $guarantorList = Koha::Patron::Relationships->search(
94
        {},
95
        {
96
            columns  => ['guarantor_id'],
97
            distinct => 1
98
        }
99
    )->_resultset->as_query;
100
101
    # Patrons who are guarantors
102
    return $self->search(
103
        { 'me.borrowernumber' => { '-in' => $guarantorList } } )
104
      if $option;
105
106
    # Patrons who are not guarantors
107
    return $self->search(
108
        { 'me.borrowernumber' => { '-not_in' => $guarantorList } } );
109
}
110
111
=head3 filter_by_has_guarantor
112
113
    Koha::Patrons->filter_by_has_guarantor(1);
114
115
Returns patrons filtered by whether they have a guarantor, are a guarantor or are not a guarantor.
116
117
=head4 options
118
119
=over 4
120
121
=item undef - do not apply filter
122
123
=item 1 - boolean to limit to users who have a guarantor
124
125
=item 0 - boolean to limit to users who do not have a guarantor
126
127
=back
128
129
=cut
130
131
sub filter_by_has_guarantor {
132
    my ( $self, $option ) = @_;
133
134
    return $self unless defined($option);
135
136
    my $guaranteeList = Koha::Patron::Relationships->search(
137
        {},
138
        {
139
            columns  => ['guarantee_id'],
140
            distinct => 1
141
        }
142
    )->_resultset->as_query;
143
144
    # Patrons who have guarantors
145
    return $self->search(
146
        { 'me.borrowernumber' => { '-in' => $guaranteeList } } )
147
      if $option;
148
149
    # Patrons who do not have guarantors
150
    return $self->search(
151
        { 'me.borrowernumber' => { '-not_in' => $guaranteeList } } );
152
}
153
154
=head3 filter_by_last_issued
155
156
    Koha::Patrons->filter_by_last_issued( { after => DateTime, before => DateTime } );
157
158
Returns patrons filtered by whether their last issue falls betwen the passed limits.
159
160
=head4 arguments hashref
161
162
=over 4
163
164
=item before (optional) - filter out patrons whose last_issue was since DateTime
165
166
=item after (optional) - filter out patrons whose last_issue has been since DateTime
167
168
=back
169
170
=cut
171
172
sub filter_by_last_issued {
173
    my ( $self, $options ) = @_;
174
175
    return $self
176
      unless ( defined($options)
177
        && ( $options->{before} || $options->{after} ) );
178
179
    my $where = {};
180
    my $group_by = [ map { 'me.' . $_ } $self->_resultset->result_source->columns ];
181
    my $attrs = {
182
        join     => 'old_issues',
183
        group_by => $group_by,
184
        order_by => { '-desc' => 'old_issues.timestamp' }
185
    };
186
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
187
188
    push @{ $where->{'-and'} },
189
      { 'old_issues.timestamp' =>
190
          { '<' => $dtf->format_datetime( $options->{before} ) } }
191
      if $options->{before};
192
    push @{ $where->{'-and'} },
193
      { 'old_issues.timestamp' =>
194
          { '>' => $dtf->format_datetime( $options->{after} ) } }
195
      if $options->{after};
196
197
    return $self->search( $where, $attrs );
198
}
199
200
# NOTE: We can't use a HAVING clause with SQL Strict modes and mysql 5.5 and 5.6 :(
201
# See https://bugs.mysql.com/bug.php?id=80455
202
#sub filter_by_last_issued {
203
#    my ( $self, $options ) = @_;
204
#
205
#    return $self
206
#      unless ( defined($options)
207
#        && ( $options->{before} || $options->{after} ) );
208
#
209
#    my $where = {};
210
#    my $attrs = {
211
#        join      => 'old_issues',
212
#        group_by  => 'me.borrowernumber',
213
#        '+select' => [ { max => 'old_issues.timestamp', '-as' => 'last_issued' } ],
214
#        '+as'     => [ 'last_issued' ]
215
#    };
216
#    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
217
#
218
#    push @{ $attrs->{'having'}->{'-and'} },
219
#      { 'last_issued' => { '<' => $dtf->format_datetime( $options->{before} ) }
220
#      }
221
#      if $options->{before};
222
#
223
#    push @{ $attrs->{'having'}->{'-and'} },
224
#      { 'last_issued' => { '>' => $dtf->format_datetime( $options->{after} ) } }
225
#      if $options->{after};
226
#
227
#    return $self->search( $where, $attrs );
228
#}
229
230
=head3 filter_by_has_issues
231
232
    Koha::Patrons->filter_by_has_issues(1);
233
234
Returns patrons filtered by whether they have current issues or not.
235
236
=head4 options
237
238
=over 4
239
240
=item undef - do not apply filter
241
242
=item 0 - limit to patrons WITHOUT current issues
243
244
=item 1 - limit to patrons WITH current issues
245
246
=back
247
248
=cut
249
250
sub filter_by_has_issues {
251
    my ( $self, $option ) = @_;
252
253
    return $self
254
      unless defined($option);
255
256
    my $issueList = Koha::Checkouts->search(
257
        {},
258
        {
259
            columns  => ['borrowernumber'],
260
            distinct => 1
261
        }
262
    )->_resultset->as_query;
263
264
    # Patrons who have current issues
265
    return $self->search( { 'me.borrowernumber' => { '-in' => $issueList } } )
266
      if $option;
267
268
    # Patrons who do not have current issues
269
    return $self->search(
270
        { 'me.borrowernumber' => { '-not_in' => $issueList } } );
271
}
272
273
# NOTE: We can't use a HAVING clause with SQL Strict modes and mysql 5.5 and 5.6 :(
274
# See https://bugs.mysql.com/bug.php?id=80455
275
#sub filter_by_has_issues {
276
#    my ( $self, $option ) = @_;
277
#
278
#    return $self
279
#      unless defined($option);
280
#
281
#    my $where = {};
282
#    my $group_by = [ map { 'me.' . $_ } $self->_resultset->result_source->columns ];
283
#    my $attrs = {
284
#        join      => 'issues',
285
#        group_by  => $group_by,
286
#        '+select' => [ { count => 'issues.issue_id', -as => 'current_issues' } ],
287
#        '+as'     => 'current_issues'
288
#    };
289
#
290
#    # Patrons who have current issues
291
#    if ($option) {
292
#        $attrs->{'having'} = { 'current_issues' => { '>' => 0 } };
293
#    }
294
#
295
#    # Patrons who do not have current issues
296
#    else {
297
#        $attrs->{'having'} = { 'current_issues' => 0 };
298
#    }
299
#
300
#    return $self->search($where,$attrs);
301
#}
302
303
=head3 filter_by_when_expired
304
305
    Koha::Patrons->filter_by_when_expired( { after => DateTime, before => DateTime );
306
307
Returns patrons filtered by whether their accounts expired between between the passed limits.
308
309
=head4 arguments hashref
310
311
=over 4
312
313
=item before (optional) - filter out patrons whose expired_date was since DateTime
314
315
=item after (optional) - filter out patrons whose expired_date has been since DateTime
316
317
=back
318
319
=cut
320
321
sub filter_by_when_expired {
322
    my ( $self, $options ) = @_;
323
324
    return $self
325
      unless ( defined($options)
326
        && ( $options->{before} || $options->{after} ) );
327
328
    my $where = {};
329
    my $attrs = {};
330
    my $dtf   = Koha::Database->new->schema->storage->datetime_parser;
331
332
    # Before only
333
    $where->{'me.dateexpiry'} =
334
      { '<' => $dtf->format_datetime( $options->{before} ) }
335
      if ( $options->{before} && !$options->{after} );
336
337
    # After only (includes null)
338
    $where->{'me.dateexpiry'} =
339
      [ undef, { '>' => $dtf->format_datetime( $options->{after} ) } ]
340
      if ( !$options->{before} && $options->{after} );
341
342
    # Before and After (between)
343
    $where->{'me.dateexpiry'} = [
344
        '-and',
345
        { '<' => $dtf->format_datetime( $options->{before} ) },
346
        { '>' => $dtf->format_datetime( $options->{after} ) }
347
      ]
348
      if ( $options->{before} && $options->{after} );
349
350
    return $self->search( $where, $attrs );
351
}
352
353
354
=head3 filter_by_amount_owed
355
356
    Koha::Patrons->filter_by_amount_owed( { less_than => '2.00', more_than => '0.50' } );
357
358
Returns patrons filtered by how much money they owe, between passed limits.
359
360
=head4 arguments hashref
361
362
=over 4
363
364
=item less_than (optional) - filter out patrons who owe less than Amount
365
366
=item more_than (optional) - filter out patrons who owe more than Amount
367
368
=back
369
370
=cut
371
372
#sub filter_by_amount_owed {
373
#    my ( $self, $options ) = @_;
374
#
375
#    return $self
376
#      unless ( defined($options)
377
#        && ( $options->{less_than} || $options->{more_than} ) );
378
#
379
#    my $where = {};
380
#    $where = [
381
#        \[ 'SUM(amountoutstanding) < ?', $options->{less_than} ],
382
#        \['SUM(amountoutstanding) = ?', '0']
383
#      ]
384
#      if ( defined( $options->{less_than} )
385
#        && !defined( $options->{more_than} ) );
386
#
387
#    $where = \['SUM(amountoutstanding) > ?', $options->{more_than} ]
388
#      if (!defined( $options->{less_than} )
389
#        && defined( $options->{more_than} ) );
390
#
391
#    $where = {
392
#        '-and' => [
393
#            \[ 'SUM(amountoutstanding) > ?', $options->{more_than} ],
394
#            \[ 'SUM(amountoutstanding) < ?', $options->{less_than} ]
395
#        ]
396
#      }
397
#      if ( defined( $options->{less_than} )
398
#        && defined( $options->{more_than} ) );
399
#
400
#    my $accountsList = Koha::Account::Lines->search(
401
#        $where,
402
#        {
403
#            columns  => ['borrowernumber'],
404
#            group_by => 'me.borrowernumber',
405
#        }
406
#    )->_resultset->as_query;
407
#
408
#    return $self->search( { 'me.borrowernumber' => { '-in' => $accountsList } } )
409
#}
410
411
# NOTE: We can't use a HAVING clause with SQL Strict modes and mysql 5.5 and 5.6 :(
412
# See https://bugs.mysql.com/bug.php?id=80455
413
sub filter_by_amount_owed {
414
    my ( $self, $options ) = @_;
415
416
    return $self
417
      unless ( defined($options)
418
        && ( $options->{less_than} || $options->{more_than} ) );
419
420
    my $where = {};
421
    my $group_by = [ map { 'me.' . $_ } $self->_resultset->result_source->columns ];
422
    #push @{$group_by}, 'outstanding';
423
    my $attrs = {
424
        join     => 'accountlines',
425
        group_by => $group_by,
426
        '+select' =>
427
          { sum => 'accountlines.amountoutstanding', '-as' => 'outstanding' },
428
        '+as' => 'outstanding'
429
    };
430
431
    $attrs->{'having'} = [
432
        { 'outstanding' => { '<' => $options->{less_than} } },
433
        { 'outstanding' => undef }
434
      ]
435
      if ( defined( $options->{less_than} )
436
        && !defined( $options->{more_than} ) );
437
438
    $attrs->{'having'} = { 'outstanding' => { '>' => $options->{more_than} } }
439
      if (!defined( $options->{less_than} )
440
        && defined( $options->{more_than} ) );
441
442
    $attrs->{'having'}->{'-and'} = [
443
        { 'outstanding' => { '>' => $options->{more_than} } },
444
        { 'outstanding' => { '<' => $options->{less_than} } }
445
      ]
446
      if ( defined( $options->{less_than} )
447
        && defined( $options->{more_than} ) );
448
449
    return $self->search( $where, $attrs );
450
}
451
452
=head3 filter_by_in_lists
453
454
    Koha::Patrons->filter_by_in_lists(\@lists);
455
456
Returns patrons filtered by whether that appear in ANY of the lists specified.
457
458
=head4 arguments arrayref
459
460
Arrayref of list ids
461
462
=back
463
464
=cut
465
466
sub filter_by_in_lists {
467
    my ( $self, $lists ) = @_;
468
469
    return $self
470
      unless defined($lists);
471
472
    my $where = {};
473
    my $attrs = { join => 'patron_list_patrons' };
474
475
    push @{ $where->{'-and'} },
476
      { 'patron_list_patrons.patron_list_id' => { '-in' => $lists } };
477
478
    return $self->search( $where, $attrs );
479
}
480
481
=head3 filter_by_not_in_lists
482
483
    Koha::Patrons->filter_by_not_in_lists(\@lists);
484
485
Returns patrons filtered by whether they do not appear in ANY of the lists specified.
486
487
=head4 arguments arrayref
488
489
Arrayref of list ids
490
491
=back
492
493
=cut
494
495
sub filter_by_not_in_lists {
496
    my ( $self, $lists ) = @_;
497
498
    return $self
499
      unless defined($lists);
500
501
    my $where = {};
502
    my $attrs = { join => 'patron_list_patrons' };
503
504
    push @{ $where->{'-and'} },
505
      { 'patron_list_patrons.patron_list_id' => { '-not_in' => $lists } };
506
507
    return $self->search( $where, $attrs );
508
}
509
68
=head3 search_housebound_choosers
510
=head3 search_housebound_choosers
69
511
70
Returns all Patrons which are Housebound choosers.
512
Returns all Patrons which are Housebound choosers.
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +350 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 41;
22
use Test::More tests => 42;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 1602-1607 subtest 'BorrowersLog tests' => sub { Link Here
1602
    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
1602
    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
1603
};
1603
};
1604
1604
1605
subtest 'complex filters' => sub {
1606
    plan tests => 7;
1607
1608
    my $now             = DateTime->now;
1609
    my $new_patron_cf_1 = Koha::Patron->new(
1610
        {
1611
            cardnumber   => 'test_cn_cf_1',
1612
            branchcode   => $library->{branchcode},
1613
            categorycode => $category->{categorycode},
1614
            surname      => 'surname for patron1',
1615
            firstname    => 'firstname for patron1',
1616
            userid       => 'a_nonexistent_userid_cf_1',
1617
            dateexpiry   => $now->clone->subtract( days => 14 )
1618
        }
1619
    )->store;
1620
    my $new_patron_cf_2 = Koha::Patron->new(
1621
        {
1622
            cardnumber   => 'test_cn_cf_2',
1623
            branchcode   => $library->{branchcode},
1624
            categorycode => $category->{categorycode},
1625
            surname      => 'surname for patron2',
1626
            firstname    => 'firstname for patron2',
1627
            userid       => 'a_nonexistent_userid_cf_2',
1628
            dateexpiry   => $now->clone->subtract( days => 7 )
1629
        }
1630
    )->store;
1631
    my $new_patron_cf_3 = Koha::Patron->new(
1632
        {
1633
            cardnumber   => 'test_cn_cf_3',
1634
            branchcode   => $library->{branchcode},
1635
            categorycode => $category->{categorycode},
1636
            surname      => 'surname for patron3',
1637
            firstname    => 'firstname for patron3',
1638
            userid       => 'a_nonexistent_userid_cf_3',
1639
        }
1640
    )->store;
1641
1642
    my $results = Koha::Patrons->search(
1643
        {
1644
            'me.borrowernumber' => [
1645
                $new_patron_cf_1->borrowernumber,
1646
                $new_patron_cf_2->borrowernumber,
1647
                $new_patron_cf_3->borrowernumber
1648
            ]
1649
        }
1650
    );
1651
1652
    # Set new_patron_cf_1 to be the guarantor for new_patron_cf_2
1653
    $new_patron_cf_2->add_guarantor(
1654
        {
1655
            guarantor_id => $new_patron_cf_1->borrowernumber,
1656
            relationship => 'test'
1657
        }
1658
    );
1659
1660
    subtest 'filter_by_is_guarantor' => sub {
1661
        plan tests => 6;
1662
1663
        my $filtered = $results->filter_by_is_guarantor();
1664
        is( ref($filtered), 'Koha::Patrons',
1665
'Koha::Patrons->filter_by_is_guarantor should return a Koha::Patrons result set in a scalar context'
1666
        );
1667
        is( $filtered->count, 3,
1668
            'filter_by_is_guarantor() found both patrons' );
1669
1670
        $filtered = $results->filter_by_is_guarantor(1);
1671
        is( $filtered->count, 1, 'filter_by_is_guarantor(1) found one patron' );
1672
        is( $filtered->next->guarantee_relationships->count,
1673
            1, 'filter_by_is_guarantor(1) found the correct patron' );
1674
1675
        $filtered = $results->filter_by_is_guarantor(0);
1676
        is( $filtered->count, 2,
1677
            'filter_by_is_guarantor(0) found two patrons' );
1678
        is( $filtered->next->guarantee_relationships->count,
1679
            0, 'filter_by_is_guarantor(0) found the correct patrons' );
1680
    };
1681
1682
    subtest 'filter_by_has_guarantor' => sub {
1683
        plan tests => 6;
1684
1685
        my $filtered = $results->filter_by_has_guarantor();
1686
        is( ref($filtered), 'Koha::Patrons',
1687
'filter_by_has_guarantor() should return a Koha::Patrons result set in a scalar context'
1688
        );
1689
        is( $filtered->count, 3,
1690
            'filter_by_has_guarantor() found both patrons' );
1691
1692
        $filtered = $results->filter_by_has_guarantor(1);
1693
        is( $filtered->count, 1,
1694
            'filter_by_has_guarantor(1) found one patron' );
1695
        is(
1696
            $filtered->next->borrowernumber,
1697
            $new_patron_cf_2->borrowernumber,
1698
            'filter_by_has_guarantor(1) found the correct patron'
1699
        );
1700
1701
        $filtered = $results->filter_by_has_guarantor(0);
1702
        is( $filtered->count, 2,
1703
            'filter_by_has_guarantor(0) found two patrons' );
1704
        is_deeply(
1705
            [
1706
                sort( $filtered->next->borrowernumber,
1707
                    $filtered->next->borrowernumber )
1708
            ],
1709
            [
1710
                sort ( $new_patron_cf_1->borrowernumber,
1711
                    $new_patron_cf_3->borrowernumber )
1712
            ],
1713
            'filter_by_has_guarantor(0) found the correct patrons'
1714
        );
1715
    };
1716
1717
    subtest 'filter_by_last_issued' => sub {
1718
        plan tests => 4;
1719
1720
        my $first = DateTime->now()->subtract( days => 5 );
1721
        my $last = $first->clone->add( days => 2 );
1722
        my $old_issue_1 = $builder->build(
1723
            {
1724
                source => 'OldIssue',
1725
                value  => {
1726
                    borrowernumber => $new_patron_cf_1->borrowernumber,
1727
                    timestamp      => $first->clone->subtract( days => 1 )
1728
                },
1729
            }
1730
        );
1731
        my $old_issue_2 = $builder->build(
1732
            {
1733
                source => 'OldIssue',
1734
                value  => {
1735
                    borrowernumber => $new_patron_cf_2->borrowernumber,
1736
                    timestamp      => $last->clone->add( days => 1 )
1737
                },
1738
            }
1739
        );
1740
        my $old_issue_3 = $builder->build(
1741
            {
1742
                source => 'OldIssue',
1743
                value  => {
1744
                    borrowernumber => $new_patron_cf_3->borrowernumber,
1745
                    timestamp      => $first->clone->add( days => 1 )
1746
                },
1747
            }
1748
        );
1749
1750
        my $filtered = $results->filter_by_last_issued();
1751
        is( ref($filtered), 'Koha::Patrons',
1752
'filter_by_last_issued() should return a Koha::Patrons result set in a scalar context'
1753
        );
1754
1755
        $filtered = $results->filter_by_last_issued( { before => $last } );
1756
        is( $filtered->_resultset->as_subselect_rs->count,
1757
            2, "filter_by_last_issued({ before => $last }) found two patrons" );
1758
1759
        $filtered = $results->filter_by_last_issued( { after => $first } );
1760
        is( $filtered->_resultset->as_subselect_rs->count,
1761
            2, "filter_by_last_issued({ after => $first }) found two patrons" );
1762
1763
        $filtered = $results->filter_by_last_issued(
1764
            { after => $first, before => $last } );
1765
        is( $filtered->_resultset->as_subselect_rs->count, 1,
1766
"filter_by_last_issued({ after => $first, before => $last }) found two patrons"
1767
        );
1768
    };
1769
1770
    subtest 'filter_by_has_issues' => sub {
1771
        plan tests => 3;
1772
1773
        my $issue_1 = $builder->build(
1774
            {
1775
                source => 'Issue',
1776
                value  => {
1777
                    borrowernumber => $new_patron_cf_1->borrowernumber
1778
                },
1779
            }
1780
        );
1781
1782
        my $filtered = $results->filter_by_has_issues();
1783
        is( ref($filtered), 'Koha::Patrons',
1784
'filter_by_has_issues() should return a Koha::Patrons result set in a scalar context'
1785
        );
1786
1787
        $filtered = $results->filter_by_has_issues(1);
1788
        is( $filtered->_resultset->as_subselect_rs->count,
1789
            1, "filter_by_has_issues(1) found one patron" );
1790
1791
        $filtered = $results->filter_by_has_issues(0);
1792
        is( $filtered->_resultset->as_subselect_rs->count,
1793
            2, "filter_by_has_issues(0) found two patrons" );
1794
    };
1795
1796
    subtest 'filter_by_when_expired' => sub {
1797
        plan tests => 4;
1798
1799
        my $first  = $now->clone->subtract( days => 12 );
1800
        my $second = $now->clone->subtract( days => 4 );
1801
1802
        my $filtered = $results->filter_by_when_expired();
1803
        is( ref($filtered), 'Koha::Patrons',
1804
'Koha::Patrons->filter_by_when_expired should return a Koha::Patrons result set in a scalar context'
1805
        );
1806
1807
        $filtered = $results->filter_by_when_expired( { before => $second } );
1808
        is( $filtered->_resultset->as_subselect_rs->count,
1809
            2,
1810
            "filter_by_when_expired({ before => $second }) found two patrons" );
1811
1812
        $filtered = $results->filter_by_when_expired( { after => $first } );
1813
        is( $filtered->_resultset->as_subselect_rs->count,
1814
            2,
1815
            "filter_by_when_expired({ after => $first }) found two patrons" );
1816
1817
        $filtered = $results->filter_by_when_expired(
1818
            { after => $first, before => $second } );
1819
        is( $filtered->_resultset->as_subselect_rs->count, 1,
1820
"filter_by_when_expired({ after => $first, before => $second }) found one patrons"
1821
        );
1822
    };
1823
1824
    subtest 'filter_by_amount_owed' => sub {
1825
        plan tests => 4;
1826
1827
        my $fine1 = $builder->build(
1828
            {
1829
                source => 'Accountline',
1830
                value  => {
1831
                    borrowernumber    => $new_patron_cf_1->borrowernumber,
1832
                    amountoutstanding => 12.00
1833
                },
1834
            }
1835
        );
1836
        my $fine2 = $builder->build(
1837
            {
1838
                source => 'Accountline',
1839
                value  => {
1840
                    borrowernumber    => $new_patron_cf_2->borrowernumber,
1841
                    amountoutstanding => 8.00
1842
                },
1843
            }
1844
        );
1845
        my $fine3 = $builder->build(
1846
            {
1847
                source => 'Accountline',
1848
                value  => {
1849
                    borrowernumber    => $new_patron_cf_2->borrowernumber,
1850
                    amountoutstanding => 10.00
1851
                },
1852
            }
1853
        );
1854
1855
        my $filtered = $results->filter_by_amount_owed();
1856
        is( ref($filtered), 'Koha::Patrons',
1857
'Koha::Patrons->filter_by_amount_owed should return a Koha::Patrons result set in a scalar context'
1858
        );
1859
1860
        my $lower_limit = 12.00;
1861
        my $upper_limit = 16.00;
1862
1863
        # Catch user with 1 x 12.00 fine and user with no fines.
1864
        $filtered =
1865
          $results->filter_by_amount_owed( { less_than => $upper_limit } );
1866
        is( $filtered->_resultset->as_subselect_rs->count, 2,
1867
"filter_by_amount_owed({ less_than => $upper_limit }) found two patrons"
1868
        );
1869
1870
        # Catch user with 1 x 8.00 and 1 x 10.00 fine
1871
        $filtered =
1872
          $results->filter_by_amount_owed( { more_than => $lower_limit } );
1873
        is( $filtered->_resultset->as_subselect_rs->count, 1,
1874
"filter_by_amount_owed({ more_than => $lower_limit }) found two patrons"
1875
        );
1876
1877
        # User with 2 fines falls above upper limit,
1878
        # user with 1 fine falls below lower limit
1879
        # and user with no fines falls below lower limit.
1880
        $filtered = $results->filter_by_amount_owed(
1881
            { more_than => $lower_limit, less_than => $upper_limit } );
1882
        is( $filtered->_resultset->as_subselect_rs->count, 0,
1883
"filter_by_amount_owed({ more_than => $lower_limit, less_than => $upper_limit }) found one patrons"
1884
        );
1885
    };
1886
1887
    subtest 'filter_by_lists' => sub {
1888
        plan tests => 6;
1889
1890
        my $patronListPatron1 = $builder->build(
1891
            {
1892
                source => 'PatronListPatron',
1893
                value  => {
1894
                    borrowernumber => $new_patron_cf_1->borrowernumber
1895
                },
1896
            }
1897
        );
1898
        my $patronListPatron2 = $builder->build(
1899
            {
1900
                source => 'PatronListPatron',
1901
                value  => {
1902
                    borrowernumber => $new_patron_cf_2->borrowernumber
1903
                },
1904
            }
1905
        );
1906
        #my $patronListPatron3 = $builder->build(
1907
        #    {
1908
        #        source => 'PatronListPatron',
1909
        #        value  => {
1910
        #            borrowernumber => $new_patron_cf_2->borrowernumber,
1911
        #            patron_list_id => $patronListPatron1->patron_list_id
1912
        #        },
1913
        #    }
1914
        #);
1915
1916
        my $filtered = $results->filter_by_in_lists();
1917
        is( ref($filtered), 'Koha::Patrons',
1918
'Koha::Patrons->filter_by_in_lists should return a Koha::Patrons result set in a scalar context'
1919
        );
1920
1921
        $filtered = $results->filter_by_not_in_lists();
1922
        is( ref($filtered), 'Koha::Patrons',
1923
'Koha::Patrons->filter_by_not_in_lists should return a Koha::Patrons result set in a scalar context'
1924
        );
1925
1926
        $filtered =
1927
          $results->filter_by_in_lists(
1928
            [ $patronListPatron1->{patron_list_id} ] );
1929
        is( $filtered->_resultset->as_subselect_rs->count, 1,
1930
"filter_by_in_lists([$patronListPatron1->patron_list_id]) found one patron"
1931
        );
1932
        is( $filtered->next->borrowernumber, $new_patron_cf_1->borrowernumber,
1933
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found the correct patron"
1934
        );
1935
1936
        $filtered =
1937
          $results->filter_by_in_lists(
1938
            [ $patronListPatron1->{patron_list_id} ] );
1939
        is( $filtered->_resultset->as_subselect_rs->count, 1,
1940
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found one patron"
1941
        );
1942
        is( $filtered->next->borrowernumber, $new_patron_cf_1->borrowernumber,
1943
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found the correct patron"
1944
        );
1945
1946
    };
1947
1948
    #    subtest 'chaining' => sub {
1949
    #    plan tests => 0;
1950
    #
1951
    #};
1952
};
1953
1605
$schema->storage->txn_rollback;
1954
$schema->storage->txn_rollback;
1606
1955
1607
subtest 'Test Koha::Patrons::merge' => sub {
1956
subtest 'Test Koha::Patrons::merge' => sub {
1608
- 

Return to bug 11983