Bug 5670: [QA Followup] Don't force ISO dates.
[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 DateTime;
32 use Koha::DateUtils;
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         authnotrequired => 0,
48         flagsrequired   => { borrowers => 1 },
49     }
50 );
51
52 my @messages;                   # For error messages.
53 my $method = $input->param('method') // q{};
54 my $visit_id = $input->param('visit_id') // q{};
55
56 # Get patron
57 my $patron = eval {
58     return Koha::Patrons->new->find($input->param('borrowernumber'));
59 };
60 push @messages, { type => 'error', code => 'error_on_patron_load' }
61     if ( $@ or !$patron );
62
63 # Get supporting cast
64 my ( $branch, $category, $houseboundprofile, $visit );
65 if ( $patron ) {
66     $branch = Koha::Libraries->new->find($patron->branchcode);
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 ( $houseboundvisits, $deliverers, $choosers );
80 my ( $houseboundvisit, $deliverer, $chooser );
81
82 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
83     # We have received the input from the profile edit form.  We must save the
84     # changes, and return to simple display.
85     $houseboundprofile->set({
86         day           => $input->param('day')           // q{},
87         frequency     => $input->param('frequency')     // q{},
88         fav_itemtypes => $input->param('fav_itemtypes') // q{},
89         fav_subjects  => $input->param('fav_subjects')  // q{},
90         fav_authors   => $input->param('fav_authors')   // q{},
91         referral      => $input->param('referral')      // q{},
92         notes         => $input->param('notes')         // q{},
93     });
94     my $success = eval { return $houseboundprofile->store };
95     push @messages, { type => 'error', code => 'error_on_profile_store' }
96         if ( $@ or !$success );
97     $method = undef;
98 } elsif ( $method eq 'createconfirm' ) {
99     # We have received the input necessary to create a new profile.  We must
100     # save it, and return to simple display.
101     $houseboundprofile = Koha::Patron::HouseboundProfile->new({
102         borrowernumber => $patron->borrowernumber,
103         day            => $input->param('day')           // q{},
104         frequency      => $input->param('frequency')     // q{},
105         fav_itemtypes  => $input->param('fav_itemtypes') // q{},
106         fav_subjects   => $input->param('fav_subjects')  // q{},
107         fav_authors    => $input->param('fav_authors')   // q{},
108         referral       => $input->param('referral')      // q{},
109         notes          => $input->param('notes')         // q{},
110     });
111     my $success = eval { return $houseboundprofile->store };
112     push @messages, { type => 'error', code => 'error_on_profile_create' }
113         if ( $@ or !$success );
114     $method = undef;
115 } elsif ( $method eq 'visit_update_or_create' ) {
116     # We want to edit, edit a visit, so we must pass its details.
117     $deliverers = Koha::Patrons->new->housebound_deliverers;
118     $choosers = Koha::Patrons->new->housebound_choosers;
119     $houseboundvisit = $visit;
120 } elsif ( $method eq 'visit_delete' and $visit ) {
121     # We want ot delete a specific visit.
122     my $success = eval { return $visit->delete };
123     push @messages, { type => 'error', code => 'error_on_visit_delete' }
124         if ( $@ or !$success );
125     $method = undef;
126 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
127     # We have received input for editing a visit.  We must store and return to
128     # simple display.
129     $visit->set({
130         borrowernumber      => $input->param('borrowernumber')      // q{},
131         appointment_date    => dt_from_string($input->param('date') // q{}),
132         day_segment         => $input->param('segment')             // q{},
133         chooser_brwnumber   => $input->param('chooser')             // q{},
134         deliverer_brwnumber => $input->param('deliverer')           // q{},
135     });
136     my $success = eval { return $visit->store };
137     push @messages, { type => 'error', code => 'error_on_visit_store' }
138         if ( $@ or !$success );
139     $method = undef;
140 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
141     # We have received input for creating a visit.  We must store and return
142     # to simple display.
143     my $visit = Koha::Patron::HouseboundVisit->new({
144         borrowernumber      => $input->param('borrowernumber')      // q{},
145         appointment_date    => dt_from_string($input->param('date') // q{}),
146         day_segment         => $input->param('segment')             // q{},
147         chooser_brwnumber   => $input->param('chooser')             // q{},
148         deliverer_brwnumber => $input->param('deliverer')           // q{},
149     });
150     my $success = eval { return $visit->store };
151     push @messages, { type => 'error', code => 'error_on_visit_create' }
152         if ( $@ or !$success );
153     $method = undef;
154 }
155
156 # We don't have any profile information, so we must display a creation form.
157 $method = 'update_or_create' if ( !$houseboundprofile );
158
159 $template->param(
160     patron             => $patron,
161     housebound_profile => $houseboundprofile,
162     visit              => $houseboundvisit,
163     branch             => $branch,
164     category           => $category,
165     messages           => \@messages,
166     method             => $method,
167     choosers           => $choosers,
168     deliverers         => $deliverers,
169     houseboundview     => 'on',
170 );
171
172 output_html_with_http_headers $input, $cookie, $template->output;
173
174 =head1 AUTHOR
175
176 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
177
178 =cut