Bug 19618: Add api endpoint for club holds
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
33 use List::Util 'shuffle';
34
35 =head1 NAME
36
37 Koha::Club::Hold
38
39 Represents a hold made for every member of club
40
41 =head1 API
42
43 =head2 Class Methods
44
45 =cut
46
47 =head3 add
48
49 Class (static) method that returns a new Koha::Club::Hold instance
50
51 =cut
52
53 sub add {
54     my ( $params ) = @_;
55
56     throw Koha::Exceptions::ClubHold unless $params->{club_id} && $params->{biblio_id};
57     my $club = Koha::Clubs->find($params->{club_id});
58     my @enrollments = $club->club_enrollments->as_list;
59     throw Koha::Exceptions::ClubHold::NoPatrons() unless scalar @enrollments;
60
61     my $biblio = Koha::Biblios->find($params->{biblio_id});
62
63     my $club_params = {
64         club_id => $params->{club_id},
65         biblio_id => $params->{biblio_id},
66         item_id => $params->{item_id}
67     };
68
69     my $club_hold = Koha::Club::Hold->new($club_params)->store();
70
71     @enrollments = shuffle(@enrollments);
72
73     foreach my $enrollment (@enrollments) {
74         my $patron_id = $enrollment->borrowernumber;
75
76         my $can_place_hold
77         = $params->{item_id}
78         ? C4::Reserves::CanItemBeReserved( $patron_id, $params->{club_id} )
79         : C4::Reserves::CanBookBeReserved( $patron_id, $params->{biblio_id} );
80
81         unless ( $can_place_hold->{status} eq 'OK' ) {
82             warn "Patron(".$patron_id.") Hold cannot be placed. Reason: " . $can_place_hold->{status};
83             Koha::Club::Hold::PatronHold->new({
84                 patron_id => $patron_id,
85                 club_hold_id => $club_hold->id,
86                 error_code => $can_place_hold->{status}
87             })->store();
88             next;
89         }
90
91         my $priority = C4::Reserves::CalculatePriority($params->{biblio_id});
92
93         my $hold_id = C4::Reserves::AddReserve(
94             $params->{pickup_library_id},
95             $patron_id,
96             $params->{biblio_id},
97             undef,    # $bibitems param is unused
98             $priority,
99             undef,    # hold date, we don't allow it currently
100             $params->{expiration_date},
101             $params->{notes},
102             $biblio->title,
103             $params->{item_id},
104             undef,    # TODO: Why not?
105             $params->{item_type}
106         );
107         if ($hold_id) {
108             Koha::Club::Hold::PatronHold->new({
109                 patron_id => $patron_id,
110                 club_hold_id => $club_hold->id,
111                 hold_id => $hold_id
112             })->store();
113         } else {
114             warn "Could not create hold for Patron(".$patron_id.")";
115             Koha::Club::Hold::PatronHold->new({
116                 patron_id => $patron_id,
117                 club_hold_id => $club_hold->id,
118                 error_message => "Could not create hold for Patron(".$patron_id.")"
119             })->store();
120         }
121
122     }
123
124     return $club_hold;
125
126 }
127
128 =head3 type
129
130 =cut
131
132 sub _type {
133     return 'ClubHold';
134 }
135
136 =head1 AUTHOR
137
138 Agustin Moyano <agustinmoyano@theke.io>
139
140 =cut
141
142 1;