Bug 5670: [Followup] Refactor .pl; error messages.
[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::Output;
31 use Koha::Libraries;
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::Patron::HouseboundProfile;
35 use Koha::Patron::HouseboundVisit;
36 use Koha::Patron::HouseboundVisits;
37
38 my $input = CGI->new;
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => 'members/housebound.tt',
43         query           => $input,
44         type            => 'intranet',
45         authnotrequired => 0,
46         flagsrequired   => { borrowers => 1 },
47     }
48 );
49
50 my @messages;                   # For error messages.
51 my $method = $input->param('method') // q{};
52 my $visit_id = $input->param('visit_id') // q{};
53
54 # Get patron
55 my $patron = eval {
56     return Koha::Patrons->new->find($input->param('borrowernumber'));
57 };
58 push @messages, { type => 'error', code => 'error_on_patron_load' }
59     if ( $@ or !$patron );
60
61 # Get supporting cast
62 my ( $branch, $category, $houseboundprofile, $visit );
63 if ( $patron ) {
64     $branch = Koha::Libraries->new->find($patron->branchcode);
65     $category = Koha::Patron::Categories->new->find($patron->categorycode);
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 ( $houseboundvisits, $deliverers, $choosers );
78 my ( $houseboundvisit, $deliverer, $chooser );
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           => $input->param('day')           // q{},
85         frequency     => $input->param('frequency')     // q{},
86         fav_itemtypes => $input->param('fav_itemtypes') // q{},
87         fav_subjects  => $input->param('fav_subjects')  // q{},
88         fav_authors   => $input->param('fav_authors')   // q{},
89         referral      => $input->param('referral')      // q{},
90         notes         => $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            => $input->param('day')           // q{},
102         frequency      => $input->param('frequency')     // q{},
103         fav_itemtypes  => $input->param('fav_itemtypes') // q{},
104         fav_subjects   => $input->param('fav_subjects')  // q{},
105         fav_authors    => $input->param('fav_authors')   // q{},
106         referral       => $input->param('referral')      // q{},
107         notes          => $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->new->housebound_deliverers;
116     $choosers = Koha::Patrons->new->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      => $input->param('borrowernumber') // q{},
129         appointment_date    => $input->param('date')           // q{},
130         day_segment         => $input->param('segment')        // q{},
131         chooser_brwnumber   => $input->param('chooser')        // q{},
132         deliverer_brwnumber => $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      => $input->param('borrowernumber') // q{},
143         appointment_date    => $input->param('date')           // q{},
144         day_segment         => $input->param('segment')        // q{},
145         chooser_brwnumber   => $input->param('chooser')        // q{},
146         deliverer_brwnumber => $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 $template->param(
158     patron             => $patron,
159     housebound_profile => $houseboundprofile,
160     visit              => $houseboundvisit,
161     branch             => $branch,
162     category           => $category,
163     messages           => \@messages,
164     method             => $method,
165     choosers           => $choosers,
166     deliverers         => $deliverers,
167     houseboundview     => 'on',
168 );
169
170 output_html_with_http_headers $input, $cookie, $template->output;
171
172 =head1 AUTHOR
173
174 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
175
176 =cut