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