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