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