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

(-)a/C4/Members.pm (-88 / +1 lines)
Lines 62-68 BEGIN { Link Here
62
    #Get data
62
    #Get data
63
    push @EXPORT, qw(
63
    push @EXPORT, qw(
64
64
65
        &GetPendingIssues
66
        &GetAllIssues
65
        &GetAllIssues
67
66
68
        &GetBorrowersToExpunge
67
        &GetBorrowersToExpunge
Lines 598-689 sub fixup_cardnumber { Link Here
598
    return $cardnumber;     # just here as a fallback/reminder 
597
    return $cardnumber;     # just here as a fallback/reminder 
599
}
598
}
600
599
601
=head2 GetPendingIssues
602
603
  my $issues = &GetPendingIssues(@borrowernumber);
604
605
Looks up what the patron with the given borrowernumber has borrowed.
606
607
C<&GetPendingIssues> returns a
608
reference-to-array where each element is a reference-to-hash; the
609
keys are the fields from the C<issues>, C<biblio>, and C<items> tables.
610
The keys include C<biblioitems> fields.
611
612
=cut
613
614
sub GetPendingIssues {
615
    my @borrowernumbers = @_;
616
617
    unless (@borrowernumbers ) { # return a ref_to_array
618
        return \@borrowernumbers; # to not cause surprise to caller
619
    }
620
621
    # Borrowers part of the query
622
    my $bquery = '';
623
    for (my $i = 0; $i < @borrowernumbers; $i++) {
624
        $bquery .= ' issues.borrowernumber = ?';
625
        if ($i < $#borrowernumbers ) {
626
            $bquery .= ' OR';
627
        }
628
    }
629
630
    # FIXME: namespace collision: each table has "timestamp" fields.  Which one is "timestamp" ?
631
    # FIXME: circ/ciculation.pl tries to sort by timestamp!
632
    # FIXME: namespace collision: other collisions possible.
633
    # FIXME: most of this data isn't really being used by callers.
634
    my $query =
635
   "SELECT issues.*,
636
            items.*,
637
           biblio.*,
638
           biblioitems.volume,
639
           biblioitems.number,
640
           biblioitems.itemtype,
641
           biblioitems.isbn,
642
           biblioitems.issn,
643
           biblioitems.publicationyear,
644
           biblioitems.publishercode,
645
           biblioitems.volumedate,
646
           biblioitems.volumedesc,
647
           biblioitems.lccn,
648
           biblioitems.url,
649
           borrowers.firstname,
650
           borrowers.surname,
651
           borrowers.cardnumber,
652
           issues.timestamp AS timestamp,
653
           issues.renewals  AS renewals,
654
           issues.borrowernumber AS borrowernumber,
655
            items.renewals  AS totalrenewals
656
    FROM   issues
657
    LEFT JOIN items       ON items.itemnumber       =      issues.itemnumber
658
    LEFT JOIN biblio      ON items.biblionumber     =      biblio.biblionumber
659
    LEFT JOIN biblioitems ON items.biblioitemnumber = biblioitems.biblioitemnumber
660
    LEFT JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
661
    WHERE
662
      $bquery
663
    ORDER BY issues.issuedate"
664
    ;
665
666
    my $sth = C4::Context->dbh->prepare($query);
667
    $sth->execute(@borrowernumbers);
668
    my $data = $sth->fetchall_arrayref({});
669
    my $today = dt_from_string;
670
    foreach (@{$data}) {
671
        if ($_->{issuedate}) {
672
            $_->{issuedate} = dt_from_string($_->{issuedate}, 'sql');
673
        }
674
        $_->{date_due_sql} = $_->{date_due};
675
        # FIXME no need to have this value
676
        $_->{date_due} or next;
677
        $_->{date_due_sql} = $_->{date_due};
678
        # FIXME no need to have this value
679
        $_->{date_due} = dt_from_string($_->{date_due}, 'sql');
680
        if ( DateTime->compare($_->{date_due}, $today) == -1 ) {
681
            $_->{overdue} = 1;
682
        }
683
    }
684
    return $data;
685
}
686
687
=head2 GetAllIssues
600
=head2 GetAllIssues
688
601
689
  $issues = &GetAllIssues($borrowernumber, $sortkey, $limit);
602
  $issues = &GetAllIssues($borrowernumber, $sortkey, $limit);
Lines 937-943 sub GetBorrowersToExpunge { Link Here
937
         <<issues.*>>
850
         <<issues.*>>
938
      </checkedout>
851
      </checkedout>
939
852
940
  NOTE: Not all table fields are available, pleasee see GetPendingIssues for a list of available fields.
853
  NOTE: Fields from tables issues, items, biblio and biblioitems are available
941
854
942
=cut
855
=cut
943
856
(-)a/t/db_dependent/Members/GetPendingIssues.t (-131 lines)
Lines 1-130 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 => 20;
21
use Test::MockModule;
22
23
use t::lib::TestBuilder;
24
25
use C4::Biblio;
26
use C4::Items;
27
use C4::Members;
28
use C4::Circulation;
29
use Koha::Library;
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
57
my ($biblionumber1) = AddBiblio( MARC::Record->new, '' );
58
my $itemnumber1 =
59
  AddItem( { barcode => '0101', %item_infos }, $biblionumber1 );
60
my $itemnumber2 =
61
  AddItem( { barcode => '0102', %item_infos }, $biblionumber1 );
62
63
my ($biblionumber2) = AddBiblio( MARC::Record->new, '' );
64
my $itemnumber3 =
65
  AddItem( { barcode => '0203', %item_infos }, $biblionumber2 );
66
67
my $borrowernumber1 =
68
  AddMember( categorycode => $categorycode, branchcode => $branchcode );
69
my $borrowernumber2 =
70
  AddMember( categorycode => $categorycode, branchcode => $branchcode );
71
my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
72
my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
73
74
my $module = new Test::MockModule('C4::Context');
75
$module->mock( 'userenv', sub { { branch => $branchcode } } );
76
77
my $issues =
78
  C4::Members::GetPendingIssues( $borrowernumber1, $borrowernumber2 );
79
is( @$issues, 0, 'GetPendingIssues returns the correct number of elements' );
80
81
AddIssue( $borrower1, '0101' );
82
$issues = C4::Members::GetPendingIssues($borrowernumber1);
83
is( @$issues, 1, 'GetPendingIssues returns the correct number of elements' );
84
is( $issues->[0]->{itemnumber},
85
    $itemnumber1, 'GetPendingIssues returns the itemnumber correctly' );
86
my $issues_bis =
87
  C4::Members::GetPendingIssues( $borrowernumber1, $borrowernumber2 );
88
is_deeply( $issues, $issues_bis, 'GetPendingIssues functions correctly' );
89
$issues = C4::Members::GetPendingIssues($borrowernumber2);
90
is( @$issues, 0, 'GetPendingIssues returns the correct number of elements' );
91
92
AddIssue( $borrower1, '0102' );
93
$issues = C4::Members::GetPendingIssues($borrowernumber1);
94
is( @$issues, 2, 'GetPendingIssues returns the correct number of elements' );
95
is( $issues->[0]->{itemnumber},
96
    $itemnumber1, 'GetPendingIssues returns the itemnumber correctly' );
97
is( $issues->[1]->{itemnumber},
98
    $itemnumber2, 'GetPendingIssues returns the itemnumber correctly' );
99
$issues_bis =
100
  C4::Members::GetPendingIssues( $borrowernumber1, $borrowernumber2 );
101
is_deeply( $issues, $issues_bis, 'GetPendingIssues functions correctly' );
102
$issues = C4::Members::GetPendingIssues($borrowernumber2);
103
is( @$issues, 0, 'GetPendingIssues returns the correct number of elements' );
104
105
AddIssue( $borrower2, '0203' );
106
$issues = C4::Members::GetPendingIssues($borrowernumber2);
107
is( @$issues, 1, 'GetAllIssues returns the correct number of elements' );
108
is( $issues->[0]->{itemnumber},
109
    $itemnumber3, 'GetPendingIssues returns the itemnumber correctly' );
110
$issues = C4::Members::GetPendingIssues($borrowernumber1);
111
is( @$issues, 2, 'GetPendingIssues returns the correct number of elements' );
112
is( $issues->[0]->{itemnumber},
113
    $itemnumber1, 'GetPendingIssues returns the itemnumber correctly' );
114
is( $issues->[1]->{itemnumber},
115
    $itemnumber2, 'GetPendingIssues returns the itemnumber correctly' );
116
$issues = C4::Members::GetPendingIssues( $borrowernumber1, $borrowernumber2 );
117
is( @$issues, 3, 'GetPendingIssues returns the correct number of elements' );
118
is( $issues->[0]->{itemnumber},
119
    $itemnumber1, 'GetPendingIssues returns the itemnumber correctly' );
120
is( $issues->[1]->{itemnumber},
121
    $itemnumber2, 'GetPendingIssues returns the itemnumber correctly' );
122
is( $issues->[2]->{itemnumber},
123
    $itemnumber3, 'GetPendingIssues returns the itemnumber correctly' );
124
125
$issues = C4::Members::GetPendingIssues();
126
is( @$issues, 0,
127
    'GetPendingIssues without borrower numbers returns an empty array' );
128
129
$schema->storage->txn_begin;
130
131
- 

Return to bug 19935