Bug 26145: (QA follow-up) Add missing filters
[koha.git] / members / housebound.pl
blobe3c06225b2151945cfa6d13602c59ddd88029e3e
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 flagsrequired => { borrowers => 'edit_borrowers' },
52 my @messages; # For error messages.
53 my $method = $input->param('method') // q{};
54 my $visit_id = $input->param('visit_id') // q{};
56 # Get patron
57 my $borrowernumber = $input->param('borrowernumber');
58 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
59 my $patron = Koha::Patrons->find($borrowernumber);
60 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
62 # Get supporting cast
63 my ( $houseboundprofile, $visit );
64 if ( $patron ) { # FIXME This test is not needed - output_and_exit_if_error handles it
65 $houseboundprofile = $patron->housebound_profile;
67 if ( $visit_id ) {
68 $visit = eval {
69 return Koha::Patron::HouseboundVisits->find($visit_id);
71 push @messages, { type => 'error', code => 'error_on_visit_load' }
72 if ( $@ or !$visit );
75 # Main processing
76 my ( $deliverers, $choosers, $houseboundvisit );
78 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
79 # We have received the input from the profile edit form. We must save the
80 # changes, and return to simple display.
81 $houseboundprofile->set({
82 day => scalar $input->param('day') // q{},
83 frequency => scalar $input->param('frequency') // q{},
84 fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
85 fav_subjects => scalar $input->param('fav_subjects') // q{},
86 fav_authors => scalar $input->param('fav_authors') // q{},
87 referral => scalar $input->param('referral') // q{},
88 notes => scalar $input->param('notes') // q{},
89 });
90 my $success = eval { return $houseboundprofile->store };
91 push @messages, { type => 'error', code => 'error_on_profile_store' }
92 if ( $@ or !$success );
93 $method = undef;
94 } elsif ( $method eq 'createconfirm' ) {
95 # We have received the input necessary to create a new profile. We must
96 # save it, and return to simple display.
97 $houseboundprofile = Koha::Patron::HouseboundProfile->new({
98 borrowernumber => $patron->borrowernumber,
99 day => scalar $input->param('day') // q{},
100 frequency => scalar $input->param('frequency') // q{},
101 fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
102 fav_subjects => scalar $input->param('fav_subjects') // q{},
103 fav_authors => scalar $input->param('fav_authors') // q{},
104 referral => scalar $input->param('referral') // q{},
105 notes => scalar $input->param('notes') // q{},
107 my $success = eval { return $houseboundprofile->store };
108 push @messages, { type => 'error', code => 'error_on_profile_create' }
109 if ( $@ or !$success );
110 $method = undef;
111 } elsif ( $method eq 'visit_update_or_create' ) {
112 # We want to edit, edit a visit, so we must pass its details.
113 $deliverers = Koha::Patrons->search_housebound_deliverers;
114 $choosers = Koha::Patrons->search_housebound_choosers;
115 $houseboundvisit = $visit;
116 } elsif ( $method eq 'visit_delete' and $visit ) {
117 # We want ot delete a specific visit.
118 my $success = eval { return $visit->delete };
119 push @messages, { type => 'error', code => 'error_on_visit_delete' }
120 if ( $@ or !$success );
121 $method = undef;
122 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
123 # We have received input for editing a visit. We must store and return to
124 # simple display.
125 $visit->set({
126 borrowernumber => scalar $input->param('borrowernumber') // q{},
127 appointment_date => dt_from_string($input->param('date') // q{}),
128 day_segment => scalar $input->param('segment') // q{},
129 chooser_brwnumber => scalar $input->param('chooser') // q{},
130 deliverer_brwnumber => scalar $input->param('deliverer') // q{},
132 my $success = eval { return $visit->store };
133 push @messages, { type => 'error', code => 'error_on_visit_store' }
134 if ( $@ or !$success );
135 $method = undef;
136 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
137 # We have received input for creating a visit. We must store and return
138 # to simple display.
139 my $visit = Koha::Patron::HouseboundVisit->new({
140 borrowernumber => scalar $input->param('borrowernumber') // q{},
141 appointment_date => dt_from_string($input->param('date') // q{}),
142 day_segment => scalar $input->param('segment') // q{},
143 chooser_brwnumber => scalar $input->param('chooser') // q{},
144 deliverer_brwnumber => scalar $input->param('deliverer') // q{},
146 my $success = eval { return $visit->store };
147 push @messages, { type => 'error', code => 'error_on_visit_create' }
148 if ( $@ or !$success );
149 $method = undef;
152 # We don't have any profile information, so we must display a creation form.
153 $method = 'update_or_create' if ( !$houseboundprofile );
155 # Ensure template has all patron details.
156 $template->param( patron => $patron );
158 $template->param(
159 housebound_profile => $houseboundprofile,
160 visit => $houseboundvisit,
161 messages => \@messages,
162 method => $method,
163 choosers => $choosers,
164 deliverers => $deliverers,
165 houseboundview => 'on',
168 output_html_with_http_headers $input, $cookie, $template->output;
170 =head1 AUTHOR
172 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
174 =cut