Bug 18403: Add sub output_and_exit_if_error - unknown_patron & cannot_see_patron_infos
[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 ( $branch, $category, $houseboundprofile, $visit, $patron_image );
66 if ( $patron ) {
67     $patron_image = $patron->image;
68     $branch = Koha::Libraries->new->find($patron->branchcode);
69     $category = Koha::Patron::Categories->new->find($patron->categorycode);
70     $houseboundprofile = $patron->housebound_profile;
71 }
72 if ( $visit_id ) {
73     $visit = eval {
74         return Koha::Patron::HouseboundVisits->find($visit_id);
75     };
76     push @messages, { type => 'error', code => 'error_on_visit_load' }
77         if ( $@ or !$visit );
78 }
79
80 # Main processing
81 my ( $deliverers, $choosers, $houseboundvisit );
82
83 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
84     # We have received the input from the profile edit form.  We must save the
85     # changes, and return to simple display.
86     $houseboundprofile->set({
87         day           => scalar $input->param('day')           // q{},
88         frequency     => scalar $input->param('frequency')     // q{},
89         fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
90         fav_subjects  => scalar $input->param('fav_subjects')  // q{},
91         fav_authors   => scalar $input->param('fav_authors')   // q{},
92         referral      => scalar $input->param('referral')      // q{},
93         notes         => scalar $input->param('notes')         // q{},
94     });
95     my $success = eval { return $houseboundprofile->store };
96     push @messages, { type => 'error', code => 'error_on_profile_store' }
97         if ( $@ or !$success );
98     $method = undef;
99 } elsif ( $method eq 'createconfirm' ) {
100     # We have received the input necessary to create a new profile.  We must
101     # save it, and return to simple display.
102     $houseboundprofile = Koha::Patron::HouseboundProfile->new({
103         borrowernumber => $patron->borrowernumber,
104         day            => scalar $input->param('day')           // q{},
105         frequency      => scalar $input->param('frequency')     // q{},
106         fav_itemtypes  => scalar $input->param('fav_itemtypes') // q{},
107         fav_subjects   => scalar $input->param('fav_subjects')  // q{},
108         fav_authors    => scalar $input->param('fav_authors')   // q{},
109         referral       => scalar $input->param('referral')      // q{},
110         notes          => scalar $input->param('notes')         // q{},
111     });
112     my $success = eval { return $houseboundprofile->store };
113     push @messages, { type => 'error', code => 'error_on_profile_create' }
114         if ( $@ or !$success );
115     $method = undef;
116 } elsif ( $method eq 'visit_update_or_create' ) {
117     # We want to edit, edit a visit, so we must pass its details.
118     $deliverers = Koha::Patrons->search_housebound_deliverers;
119     $choosers = Koha::Patrons->search_housebound_choosers;
120     $houseboundvisit = $visit;
121 } elsif ( $method eq 'visit_delete' and $visit ) {
122     # We want ot delete a specific visit.
123     my $success = eval { return $visit->delete };
124     push @messages, { type => 'error', code => 'error_on_visit_delete' }
125         if ( $@ or !$success );
126     $method = undef;
127 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
128     # We have received input for editing a visit.  We must store and return to
129     # simple display.
130     $visit->set({
131         borrowernumber      => scalar $input->param('borrowernumber')      // q{},
132         appointment_date    => dt_from_string($input->param('date') // q{}),
133         day_segment         => scalar $input->param('segment')             // q{},
134         chooser_brwnumber   => scalar $input->param('chooser')             // q{},
135         deliverer_brwnumber => scalar $input->param('deliverer')           // q{},
136     });
137     my $success = eval { return $visit->store };
138     push @messages, { type => 'error', code => 'error_on_visit_store' }
139         if ( $@ or !$success );
140     $method = undef;
141 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
142     # We have received input for creating a visit.  We must store and return
143     # to simple display.
144     my $visit = Koha::Patron::HouseboundVisit->new({
145         borrowernumber      => scalar $input->param('borrowernumber')      // q{},
146         appointment_date    => dt_from_string($input->param('date') // q{}),
147         day_segment         => scalar $input->param('segment')             // q{},
148         chooser_brwnumber   => scalar $input->param('chooser')             // q{},
149         deliverer_brwnumber => scalar $input->param('deliverer')           // q{},
150     });
151     my $success = eval { return $visit->store };
152     push @messages, { type => 'error', code => 'error_on_visit_create' }
153         if ( $@ or !$success );
154     $method = undef;
155 }
156
157 # We don't have any profile information, so we must display a creation form.
158 $method = 'update_or_create' if ( !$houseboundprofile );
159
160 # Ensure template has all patron details.
161 $template->param(%{$patron->unblessed}) if ( $patron );
162
163 # Load extended patron attributes if necessary (taken from members/files.pl).
164 if ( C4::Context->preference('ExtendedPatronAttributes') and $patron ) {
165     my $attributes = GetBorrowerAttributes($patron->borrowernumber);
166     $template->param(
167         ExtendedPatronAttributes => 1,
168         extendedattributes => $attributes
169     );
170 }
171
172 $template->param( adultborrower => 1 ) if ( $category->category_type eq 'A' || $category->category_type eq 'I' );
173 $template->param(
174     picture            => $patron_image,
175     housebound_profile => $houseboundprofile,
176     visit              => $houseboundvisit,
177     branch             => $branch,
178     category           => $category,
179     messages           => \@messages,
180     method             => $method,
181     choosers           => $choosers,
182     deliverers         => $deliverers,
183     houseboundview     => 'on',
184 );
185
186 output_html_with_http_headers $input, $cookie, $template->output;
187
188 =head1 AUTHOR
189
190 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
191
192 =cut