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