46f9f319330a6fa1a81828f8139d6ad58bdb2484
[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 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;
30 use Koha::Exceptions::ClubHold;
31 use Koha::Club::Hold::PatronHold;
32 use Koha::Clubs;
33 use Koha::Patrons;
34
35 use List::Util '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
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         if($params->{default_patron_home}) {
92             my $patron = Koha::Patrons->find($patron_id);
93             my $patron_home = $patron->branchcode;
94             $can_place_hold = $params->{item_id}
95                 ? C4::Reserves::CanItemBeReserved( $patron_id, $params->{item_id}, $patron_home )
96                 : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $patron_home );
97             $pickup_id = $patron_home if $can_place_hold->{status} eq 'OK';
98             unless ( $can_place_hold->{status} eq 'OK' ) {
99                 warn "Patron(".$patron_id.") Hold cannot be placed with patron's homebranch ($patron_home). Reason: " . $can_place_hold->{status};
100             }
101         }
102
103         unless ( defined $can_place_hold && $can_place_hold->{status} eq 'OK' ) {
104             $can_place_hold = $params->{item_id}
105                 ? C4::Reserves::CanItemBeReserved( $patron_id, $params->{item_id}, $pickup_id )
106                 : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id}, $pickup_id );
107         }
108
109         unless ( $can_place_hold->{status} eq 'OK' ) {
110             warn "Patron(".$patron_id.") Hold cannot be placed. Reason: " . $can_place_hold->{status};
111             Koha::Club::Hold::PatronHold->new({
112                 patron_id => $patron_id,
113                 club_hold_id => $club_hold->id,
114                 error_code => $can_place_hold->{status}
115             })->store();
116             next;
117         }
118
119         my $priority = C4::Reserves::CalculatePriority($params->{biblio_id});
120
121         my $hold_id = C4::Reserves::AddReserve(
122             {
123                 branchcode      => $pickup_id,
124                 borrowernumber  => $patron_id,
125                 biblionumber    => $params->{biblio_id},
126                 priority        => $priority,
127                 expiration_date => $params->{expiration_date},
128                 notes           => $params->{notes},
129                 title           => $biblio->title,
130                 itemnumber      => $params->{item_id},
131                 found           => undef,                       # TODO: Why not?
132                 itemtype        => $params->{item_type},
133             }
134         );
135         if ($hold_id) {
136             Koha::Club::Hold::PatronHold->new({
137                 patron_id => $patron_id,
138                 club_hold_id => $club_hold->id,
139                 hold_id => $hold_id
140             })->store();
141         } else {
142             warn "Could not create hold for Patron(".$patron_id.")";
143             Koha::Club::Hold::PatronHold->new({
144                 patron_id => $patron_id,
145                 club_hold_id => $club_hold->id,
146                 error_message => "Could not create hold for Patron(".$patron_id.")"
147             })->store();
148         }
149     }
150
151     return $club_hold;
152
153 }
154
155
156 =head3 to_api_mapping
157
158 This method returns the mapping for representing a Koha::Club::Hold object
159 on the API.
160
161 =cut
162
163 sub to_api_mapping {
164     return {
165         id => 'club_hold_id'
166     };
167 }
168
169 =head2 Internal methods
170
171 =head3 _type
172
173 =cut
174
175 sub _type {
176     return 'ClubHold';
177 }
178
179 =head1 AUTHOR
180
181 Agustin Moyano <agustinmoyano@theke.io>
182
183 =cut
184
185 1;