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