Bug 34438: Database update
[koha.git] / 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::Club::Holds;
32 use Koha::Clubs;
33 use Koha::Patrons;
34
35 use List::Util qw( shuffle );
36
37 =head1 NAME
38
39 Koha::Club::Hold
40
41 Represents a hold made for every member of club
42
43 =head1 API
44
45 =head2 Class methods
46
47 =cut
48
49 =head3 add
50
51 Class (static) method that returns a new Koha::Club::Hold instance
52
53 =cut
54
55 sub add {
56     my ( $params ) = @_;
57     my $itemnumber = $params->{item_id};
58
59     # check for mandatory params
60     my @mandatory = ( 'biblio_id', 'club_id' );
61     for my $param (@mandatory) {
62         unless ( defined( $params->{$param} ) ) {
63             Koha::Exceptions::MissingParameter->throw(
64                 error => "The $param parameter is mandatory" );
65         }
66     }
67
68     my $club = Koha::Clubs->find($params->{club_id});
69     my @enrollments = $club->club_enrollments->as_list;
70
71     Koha::Exceptions::ClubHold::NoPatrons->throw()
72         unless scalar @enrollments;
73
74     my $biblio = Koha::Biblios->find($params->{biblio_id});
75
76     my $club_params = {
77         club_id   => $params->{club_id},
78         biblio_id => $params->{biblio_id},
79         item_id   => $params->{item_id}
80     };
81
82     my $club_hold = Koha::Club::Hold->new($club_params)->store();
83     $club_hold->discard_changes;
84
85     @enrollments = shuffle(@enrollments);
86
87     foreach my $enrollment (@enrollments) {
88         my $patron_id = $enrollment->borrowernumber;
89         my $pickup_id = $params->{pickup_library_id};
90
91         my $can_place_hold;
92         my $patron = Koha::Patrons->find($patron_id);
93         my $item = $itemnumber ? Koha::Items->find( $itemnumber ) : undef;
94         if($params->{default_patron_home}) {
95             my $patron_home = $patron->branchcode;
96             $can_place_hold = $itemnumber
97                 ? C4::Reserves::CanItemBeReserved( $patron, $item, $patron_home )
98                 : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $patron_home );
99             $pickup_id = $patron_home if $can_place_hold->{status} eq 'OK';
100             unless ( $can_place_hold->{status} eq 'OK' ) {
101                 warn "Patron(".$patron_id.") Hold cannot be placed with patron's homebranch ($patron_home). Reason: " . $can_place_hold->{status};
102             }
103         }
104
105         unless ( defined $can_place_hold && $can_place_hold->{status} eq 'OK' ) {
106             $can_place_hold = $itemnumber
107                 ? C4::Reserves::CanItemBeReserved( $patron, $item, $pickup_id )
108                 : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $pickup_id );
109         }
110
111         unless ( $can_place_hold->{status} eq 'OK' ) {
112             warn "Patron(".$patron_id.") Hold cannot be placed. Reason: " . $can_place_hold->{status};
113             Koha::Club::Hold::PatronHold->new({
114                 patron_id => $patron_id,
115                 club_hold_id => $club_hold->id,
116                 error_code => $can_place_hold->{status}
117             })->store();
118             next;
119         }
120
121         my $priority = C4::Reserves::CalculatePriority($params->{biblio_id});
122
123         my $hold_id = C4::Reserves::AddReserve(
124             {
125                 branchcode      => $pickup_id,
126                 borrowernumber  => $patron_id,
127                 biblionumber    => $params->{biblio_id},
128                 priority        => $priority,
129                 expiration_date => $params->{expiration_date},
130                 notes           => $params->{notes},
131                 title           => $biblio->title,
132                 itemnumber      => $params->{item_id},
133                 found           => undef,                       # TODO: Why not?
134                 itemtype        => $params->{item_type},
135             }
136         );
137         if ($hold_id) {
138             Koha::Club::Hold::PatronHold->new({
139                 patron_id => $patron_id,
140                 club_hold_id => $club_hold->id,
141                 hold_id => $hold_id
142             })->store();
143         } else {
144             warn "Could not create hold for Patron(".$patron_id.")";
145             Koha::Club::Hold::PatronHold->new({
146                 patron_id => $patron_id,
147                 club_hold_id => $club_hold->id,
148                 error_message => "Could not create hold for Patron(".$patron_id.")"
149             })->store();
150         }
151     }
152
153     return $club_hold;
154
155 }
156
157
158 =head3 to_api_mapping
159
160 This method returns the mapping for representing a Koha::Club::Hold object
161 on the API.
162
163 =cut
164
165 sub to_api_mapping {
166     return {
167         id => 'club_hold_id'
168     };
169 }
170
171 =head2 Internal methods
172
173 =head3 _type
174
175 =cut
176
177 sub _type {
178     return 'ClubHold';
179 }
180
181 =head1 AUTHOR
182
183 Agustin Moyano <agustinmoyano@theke.io>
184
185 =cut
186
187 1;