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