Bug 29562: Adjust CanItemBeReserved and checkHighHolds to take objects
[srvgit] / Koha / Club / Hold.pm
1 package Koha::Club::Hold;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22
23 use Koha::Database;
24
25 use Koha::Club::Template::Fields;
26
27 use base qw(Koha::Object);
28 use Koha::Exceptions;
29 use Koha::Exceptions::ClubHold;
30 use Koha::Club::Hold::PatronHold;
31 use Koha::Clubs;
32 use Koha::Patrons;
33
34 use List::Util qw( shuffle );
35
36 =head1 NAME
37
38 Koha::Club::Hold
39
40 Represents a hold made for every member of club
41
42 =head1 API
43
44 =head2 Class methods
45
46 =cut
47
48 =head3 add
49
50 Class (static) method that returns a new Koha::Club::Hold instance
51
52 =cut
53
54 sub add {
55     my ( $params ) = @_;
56     my $itemnumber = $params->{item_id};
57
58     # check for mandatory params
59     my @mandatory = ( 'biblio_id', 'club_id' );
60     for my $param (@mandatory) {
61         unless ( defined( $params->{$param} ) ) {
62             Koha::Exceptions::MissingParameter->throw(
63                 error => "The $param parameter is mandatory" );
64         }
65     }
66
67     my $club = Koha::Clubs->find($params->{club_id});
68     my @enrollments = $club->club_enrollments->as_list;
69
70     Koha::Exceptions::ClubHold::NoPatrons->throw()
71         unless scalar @enrollments;
72
73     my $biblio = Koha::Biblios->find($params->{biblio_id});
74
75     my $club_params = {
76         club_id   => $params->{club_id},
77         biblio_id => $params->{biblio_id},
78         item_id   => $params->{item_id}
79     };
80
81     my $club_hold = Koha::Club::Hold->new($club_params)->store();
82     $club_hold->discard_changes;
83
84     @enrollments = shuffle(@enrollments);
85
86     foreach my $enrollment (@enrollments) {
87         my $patron_id = $enrollment->borrowernumber;
88         my $pickup_id = $params->{pickup_library_id};
89
90         my $can_place_hold;
91         my $patron = Koha::Patrons->find($patron_id);
92         my $item = $itemnumber ? Koha::Items->find( $itemnumber ) : undef;
93         if($params->{default_patron_home}) {
94             my $patron_home = $patron->branchcode;
95             $can_place_hold = $itemnumber
96                 ? C4::Reserves::CanItemBeReserved( $patron, $item, $patron_home )
97                 : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $patron_home );
98             $pickup_id = $patron_home if $can_place_hold->{status} eq 'OK';
99             unless ( $can_place_hold->{status} eq 'OK' ) {
100                 warn "Patron(".$patron_id.") Hold cannot be placed with patron's homebranch ($patron_home). Reason: " . $can_place_hold->{status};
101             }
102         }
103
104         unless ( defined $can_place_hold && $can_place_hold->{status} eq 'OK' ) {
105             $can_place_hold = $itemnumber
106                 ? C4::Reserves::CanItemBeReserved( $patron, $item, $pickup_id )
107                 : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $pickup_id );
108         }
109
110         unless ( $can_place_hold->{status} eq 'OK' ) {
111             warn "Patron(".$patron_id.") Hold cannot be placed. Reason: " . $can_place_hold->{status};
112             Koha::Club::Hold::PatronHold->new({
113                 patron_id => $patron_id,
114                 club_hold_id => $club_hold->id,
115                 error_code => $can_place_hold->{status}
116             })->store();
117             next;
118         }
119
120         my $priority = C4::Reserves::CalculatePriority($params->{biblio_id});
121
122         my $hold_id = C4::Reserves::AddReserve(
123             {
124                 branchcode      => $pickup_id,
125                 borrowernumber  => $patron_id,
126                 biblionumber    => $params->{biblio_id},
127                 priority        => $priority,
128                 expiration_date => $params->{expiration_date},
129                 notes           => $params->{notes},
130                 title           => $biblio->title,
131                 itemnumber      => $params->{item_id},
132                 found           => undef,                       # TODO: Why not?
133                 itemtype        => $params->{item_type},
134             }
135         );
136         if ($hold_id) {
137             Koha::Club::Hold::PatronHold->new({
138                 patron_id => $patron_id,
139                 club_hold_id => $club_hold->id,
140                 hold_id => $hold_id
141             })->store();
142         } else {
143             warn "Could not create hold for Patron(".$patron_id.")";
144             Koha::Club::Hold::PatronHold->new({
145                 patron_id => $patron_id,
146                 club_hold_id => $club_hold->id,
147                 error_message => "Could not create hold for Patron(".$patron_id.")"
148             })->store();
149         }
150     }
151
152     return $club_hold;
153
154 }
155
156
157 =head3 to_api_mapping
158
159 This method returns the mapping for representing a Koha::Club::Hold object
160 on the API.
161
162 =cut
163
164 sub to_api_mapping {
165     return {
166         id => 'club_hold_id'
167     };
168 }
169
170 =head2 Internal methods
171
172 =head3 _type
173
174 =cut
175
176 sub _type {
177     return 'ClubHold';
178 }
179
180 =head1 AUTHOR
181
182 Agustin Moyano <agustinmoyano@theke.io>
183
184 =cut
185
186 1;