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

(-)a/C4/Members.pm (-55 lines)
Lines 42-49 BEGIN { Link Here
42
    require Exporter;
42
    require Exporter;
43
    @ISA = qw(Exporter);
43
    @ISA = qw(Exporter);
44
    @EXPORT_OK = qw(
44
    @EXPORT_OK = qw(
45
      GetAllIssues
46
47
      GetBorrowersToExpunge
45
      GetBorrowersToExpunge
48
46
49
      IssueSlip
47
      IssueSlip
Lines 231-289 sub patronflags { Link Here
231
    return ( \%flags );
229
    return ( \%flags );
232
}
230
}
233
231
234
=head2 GetAllIssues
235
236
  $issues = &GetAllIssues($borrowernumber, $sortkey, $limit);
237
238
Looks up what the patron with the given borrowernumber has borrowed,
239
and sorts the results.
240
241
C<$sortkey> is the name of a field on which to sort the results. This
242
should be the name of a field in the C<issues>, C<biblio>,
243
C<biblioitems>, or C<items> table in the Koha database.
244
245
C<$limit> is the maximum number of results to return.
246
247
C<&GetAllIssues> an arrayref, C<$issues>, of hashrefs, the keys of which
248
are the fields from the C<issues>, C<biblio>, C<biblioitems>, and
249
C<items> tables of the Koha database.
250
251
=cut
252
253
#'
254
sub GetAllIssues {
255
    my ( $borrowernumber, $order, $limit ) = @_;
256
257
    return unless $borrowernumber;
258
    $order = 'date_due desc' unless $order;
259
260
    my $dbh = C4::Context->dbh;
261
    my $query =
262
'SELECT issues.*, items.*, biblio.*, biblioitems.*, issues.timestamp as issuestimestamp, issues.renewals_count AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp,borrowers.firstname,borrowers.surname
263
  FROM issues
264
  LEFT JOIN items on items.itemnumber=issues.itemnumber
265
  LEFT JOIN borrowers on borrowers.borrowernumber=issues.issuer_id
266
  LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
267
  LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
268
  WHERE issues.borrowernumber=?
269
  UNION ALL
270
  SELECT old_issues.*, items.*, biblio.*, biblioitems.*, old_issues.timestamp as issuestimestamp, old_issues.renewals_count AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp,borrowers.firstname,borrowers.surname
271
  FROM old_issues
272
  LEFT JOIN items on items.itemnumber=old_issues.itemnumber
273
  LEFT JOIN borrowers on borrowers.borrowernumber=old_issues.issuer_id
274
  LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
275
  LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
276
  WHERE old_issues.borrowernumber=? AND old_issues.itemnumber IS NOT NULL
277
  order by ' . $order;
278
    if ($limit) {
279
        $query .= " limit $limit";
280
    }
281
282
    my $sth = $dbh->prepare($query);
283
    $sth->execute( $borrowernumber, $borrowernumber );
284
    return $sth->fetchall_arrayref( {} );
285
}
286
287
=head2 GetBorrowersToExpunge
232
=head2 GetBorrowersToExpunge
288
233
289
  $borrowers = &GetBorrowersToExpunge(
234
  $borrowers = &GetBorrowersToExpunge(
(-)a/t/db_dependent/Members/GetAllIssues.t (-114 lines)
Lines 1-113 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 16;
21
use Test::MockModule;
22
23
use t::lib::TestBuilder;
24
25
use C4::Circulation qw( AddIssue );
26
use C4::Biblio qw( AddBiblio );
27
use C4::Items;
28
use C4::Members qw( GetAllIssues );
29
use Koha::Libraries;
30
use Koha::Patrons;
31
use MARC::Record;
32
33
my $schema = Koha::Database->schema;
34
my $dbh = C4::Context->dbh;
35
$schema->storage->txn_begin;
36
37
my $builder = t::lib::TestBuilder->new;
38
39
$dbh->do(q|DELETE FROM issues|);
40
$dbh->do(q|DELETE FROM borrowers|);
41
$dbh->do(q|DELETE FROM items|);
42
$dbh->do(q|DELETE FROM branches|);
43
$dbh->do(q|DELETE FROM biblio|);
44
$dbh->do(q|DELETE FROM categories|);
45
46
my $branchcode   = $builder->build( { source => 'Branch' } )->{branchcode};
47
my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
48
my $itemtype     = $builder->build( { source => 'Itemtype' } )->{itemtype};
49
50
my %item_infos = (
51
    homebranch    => $branchcode,
52
    holdingbranch => $branchcode,
53
    itype         => $itemtype
54
);
55
56
my ($biblionumber1) = AddBiblio( MARC::Record->new, '' );
57
58
# FIXME These tests will fail if the barcode exists in DB
59
my $itemnumber1 = $builder->build_sample_item({ biblionumber => $biblionumber1, barcode => '0101', %item_infos })->itemnumber;
60
my $itemnumber2 = $builder->build_sample_item({ biblionumber => $biblionumber1, barcode => '0102', %item_infos })->itemnumber;
61
62
my ($biblionumber2) = AddBiblio( MARC::Record->new, '' );
63
my $itemnumber3 = $builder->build_sample_item({ biblionumber => $biblionumber2, barcode => '0202', %item_infos })->itemnumber;
64
65
my $borrowernumber1 =
66
  Koha::Patron->new({ categorycode => $categorycode, branchcode => $branchcode })->store->borrowernumber;
67
my $borrowernumber2 =
68
  Koha::Patron->new({ categorycode => $categorycode, branchcode => $branchcode })->store->borrowernumber;
69
my $borrower1 = Koha::Patrons->find( $borrowernumber1 );
70
my $borrower2 = Koha::Patrons->find( $borrowernumber2 );
71
72
my $module = Test::MockModule->new('C4::Context');
73
$module->mock( 'userenv', sub { { branch => $branchcode } } );
74
75
my $issues = C4::Members::GetAllIssues();
76
is( $issues, undef, 'GetAllIssues without borrower number returns undef' );
77
78
$issues = C4::Members::GetAllIssues($borrowernumber1);
79
is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
80
$issues = C4::Members::GetAllIssues($borrowernumber2);
81
is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
82
83
AddIssue( $borrower1, '0101' );
84
$issues = C4::Members::GetAllIssues($borrowernumber1);
85
my $issues_with_order =
86
  C4::Members::GetAllIssues( $borrowernumber1, 'date_due desc' );
87
is_deeply( $issues, $issues_with_order,
88
'The value by default for the argument order in GellAllIssues is date_due_desc'
89
);
90
is( @$issues, 1, 'GetAllIssues returns the correct number of elements' );
91
is( $issues->[0]->{itemnumber}, $itemnumber1, '' );
92
$issues = C4::Members::GetAllIssues($borrowernumber2);
93
is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
94
95
AddIssue( $borrower1, '0102' );
96
$issues = C4::Members::GetAllIssues($borrowernumber1);
97
is( @$issues, 2, 'GetAllIssues returns the correct number of elements' );
98
is( $issues->[0]->{itemnumber}, $itemnumber1, '' );
99
is( $issues->[1]->{itemnumber}, $itemnumber2, '' );
100
$issues = C4::Members::GetAllIssues($borrowernumber2);
101
is( @$issues, 0, 'GetAllIssues returns the correct number of elements' );
102
103
AddIssue( $borrower2, '0202' );
104
$issues = C4::Members::GetAllIssues($borrowernumber1);
105
is( @$issues, 2, 'GetAllIssues returns the correct number of elements' );
106
is( $issues->[0]->{itemnumber}, $itemnumber1, '' );
107
is( $issues->[1]->{itemnumber}, $itemnumber2, '' );
108
$issues = C4::Members::GetAllIssues($borrowernumber2);
109
is( @$issues, 1, 'GetAllIssues returns the correct number of elements' );
110
is( $issues->[0]->{itemnumber}, $itemnumber3, '' );
111
112
$schema->storage->txn_begin;
113
114
- 

Return to bug 33947