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