Bug 24902: Join different mc- limits with AND (elasticsearch)
[koha.git] / members / housebound.pl
blob0584685f397f9a28568374781c0a0d1e6d1a8d09
1 #!/usr/bin/perl
3 # Copyright 2016 PTFS-Europe Ltd
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 =head1 housebound.pl
22 Script to handle housebound management for patrons. This single script
23 handles display, creation, deletion and management of profiles and visits.
25 =cut
27 use Modern::Perl;
28 use CGI;
29 use C4::Auth;
30 use C4::Context;
31 use C4::Output;
32 use DateTime;
33 use Koha::DateUtils;
34 use Koha::Libraries;
35 use Koha::Patrons;
36 use Koha::Patron::Categories;
37 use Koha::Patron::HouseboundProfile;
38 use Koha::Patron::HouseboundVisit;
39 use Koha::Patron::HouseboundVisits;
41 my $input = CGI->new;
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => 'members/housebound.tt',
46 query => $input,
47 type => 'intranet',
48 authnotrequired => 0,
49 flagsrequired => { borrowers => 'edit_borrowers' },
53 my @messages; # For error messages.
54 my $method = $input->param('method') // q{};
55 my $visit_id = $input->param('visit_id') // q{};
57 # Get patron
58 my $borrowernumber = $input->param('borrowernumber');
59 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
60 my $patron = Koha::Patrons->find($borrowernumber);
61 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63 # Get supporting cast
64 my ( $houseboundprofile, $visit );
65 if ( $patron ) { # FIXME This test is not needed - output_and_exit_if_error handles it
66 $houseboundprofile = $patron->housebound_profile;
68 if ( $visit_id ) {
69 $visit = eval {
70 return Koha::Patron::HouseboundVisits->find($visit_id);
72 push @messages, { type => 'error', code => 'error_on_visit_load' }
73 if ( $@ or !$visit );
76 # Main processing
77 my ( $deliverers, $choosers, $houseboundvisit );
79 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
80 # We have received the input from the profile edit form. We must save the
81 # changes, and return to simple display.
82 $houseboundprofile->set({
83 day => scalar $input->param('day') // q{},
84 frequency => scalar $input->param('frequency') // q{},
85 fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
86 fav_subjects => scalar $input->param('fav_subjects') // q{},
87 fav_authors => scalar $input->param('fav_authors') // q{},
88 referral => scalar $input->param('referral') // q{},
89 notes => scalar $input->param('notes') // q{},
90 });
91 my $success = eval { return $houseboundprofile->store };
92 push @messages, { type => 'error', code => 'error_on_profile_store' }
93 if ( $@ or !$success );
94 $method = undef;
95 } elsif ( $method eq 'createconfirm' ) {
96 # We have received the input necessary to create a new profile. We must
97 # save it, and return to simple display.
98 $houseboundprofile = Koha::Patron::HouseboundProfile->new({
99 borrowernumber => $patron->borrowernumber,
100 day => scalar $input->param('day') // q{},
101 frequency => scalar $input->param('frequency') // q{},
102 fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
103 fav_subjects => scalar $input->param('fav_subjects') // q{},
104 fav_authors => scalar $input->param('fav_authors') // q{},
105 referral => scalar $input->param('referral') // q{},
106 notes => scalar $input->param('notes') // q{},
108 my $success = eval { return $houseboundprofile->store };
109 push @messages, { type => 'error', code => 'error_on_profile_create' }
110 if ( $@ or !$success );
111 $method = undef;
112 } elsif ( $method eq 'visit_update_or_create' ) {
113 # We want to edit, edit a visit, so we must pass its details.
114 $deliverers = Koha::Patrons->search_housebound_deliverers;
115 $choosers = Koha::Patrons->search_housebound_choosers;
116 $houseboundvisit = $visit;
117 } elsif ( $method eq 'visit_delete' and $visit ) {
118 # We want ot delete a specific visit.
119 my $success = eval { return $visit->delete };
120 push @messages, { type => 'error', code => 'error_on_visit_delete' }
121 if ( $@ or !$success );
122 $method = undef;
123 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
124 # We have received input for editing a visit. We must store and return to
125 # simple display.
126 $visit->set({
127 borrowernumber => scalar $input->param('borrowernumber') // q{},
128 appointment_date => dt_from_string($input->param('date') // q{}),
129 day_segment => scalar $input->param('segment') // q{},
130 chooser_brwnumber => scalar $input->param('chooser') // q{},
131 deliverer_brwnumber => scalar $input->param('deliverer') // q{},
133 my $success = eval { return $visit->store };
134 push @messages, { type => 'error', code => 'error_on_visit_store' }
135 if ( $@ or !$success );
136 $method = undef;
137 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
138 # We have received input for creating a visit. We must store and return
139 # to simple display.
140 my $visit = Koha::Patron::HouseboundVisit->new({
141 borrowernumber => scalar $input->param('borrowernumber') // q{},
142 appointment_date => dt_from_string($input->param('date') // q{}),
143 day_segment => scalar $input->param('segment') // q{},
144 chooser_brwnumber => scalar $input->param('chooser') // q{},
145 deliverer_brwnumber => scalar $input->param('deliverer') // q{},
147 my $success = eval { return $visit->store };
148 push @messages, { type => 'error', code => 'error_on_visit_create' }
149 if ( $@ or !$success );
150 $method = undef;
153 # We don't have any profile information, so we must display a creation form.
154 $method = 'update_or_create' if ( !$houseboundprofile );
156 # Ensure template has all patron details.
157 $template->param( patron => $patron );
159 $template->param(
160 housebound_profile => $houseboundprofile,
161 visit => $houseboundvisit,
162 messages => \@messages,
163 method => $method,
164 choosers => $choosers,
165 deliverers => $deliverers,
166 houseboundview => 'on',
169 output_html_with_http_headers $input, $cookie, $template->output;
171 =head1 AUTHOR
173 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
175 =cut