Bug 24584: Rewrite optional/parameters to YAML
[koha.git] / Koha / Club / Hold.pm
blob567a3149bc71140b7331da3f0540c36bac7d41b2
1 package Koha::Club::Hold;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
26 use Koha::Club::Template::Fields;
28 use base qw(Koha::Object);
29 use Koha::Exceptions::ClubHold;
30 use Koha::Club::Hold::PatronHold;
31 use Koha::Clubs;
33 use List::Util 'shuffle';
35 =head1 NAME
37 Koha::Club::Hold
39 Represents a hold made for every member of club
41 =head1 API
43 =head2 Class methods
45 =cut
47 =head3 add
49 Class (static) method that returns a new Koha::Club::Hold instance
51 =cut
53 sub add {
54 my ( $params ) = @_;
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;
61 my $biblio = Koha::Biblios->find($params->{biblio_id});
63 my $club_params = {
64 club_id => $params->{club_id},
65 biblio_id => $params->{biblio_id},
66 item_id => $params->{item_id}
69 my $club_hold = Koha::Club::Hold->new($club_params)->store();
71 @enrollments = shuffle(@enrollments);
73 foreach my $enrollment (@enrollments) {
74 my $patron_id = $enrollment->borrowernumber;
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} );
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;
91 my $priority = C4::Reserves::CalculatePriority($params->{biblio_id});
93 my $hold_id = C4::Reserves::AddReserve(
95 branchcode => $params->{pickup_library_id},
96 borrowernumber => $patron_id,
97 biblionumber => $params->{biblio_id},
98 priority => $priority,
99 expiration_date => $params->{expiration_date},
100 notes => $params->{notes},
101 title => $biblio->title,
102 itemnumber => $params->{item_id},
103 found => undef, # TODO: Why not?
104 itemtype => $params->{item_type},
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();
124 return $club_hold;
129 =head3 to_api_mapping
131 This method returns the mapping for representing a Koha::Club::Hold object
132 on the API.
134 =cut
136 sub to_api_mapping {
137 return {
138 id => 'club_hold_id',
139 club_id => 'club_id',
140 biblio_id => 'biblio_id',
141 item_id => 'item_id'
145 =head2 Internal methods
147 =head3 _type
149 =cut
151 sub _type {
152 return 'ClubHold';
155 =head1 AUTHOR
157 Agustin Moyano <agustinmoyano@theke.io>
159 =cut