3 # Copyright 2016 PTFS-Europe Ltd
5 # This file is part of Koha.
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.
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.
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>.
22 Script to handle housebound management for patrons. This single script
23 handles display, creation, deletion and management of profiles and visits.
29 use C4::Auth qw( get_template_and_user );
31 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
35 use Koha::Patron::Categories;
36 use Koha::Patron::HouseboundProfile;
37 use Koha::Patron::HouseboundVisit;
38 use Koha::Patron::HouseboundVisits;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 template_name => 'members/housebound.tt',
47 flagsrequired => { borrowers => 'edit_borrowers' },
51 my @messages; # For error messages.
52 my $method = $input->param('method') // q{};
53 my $visit_id = $input->param('visit_id') // q{};
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 } );
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;
68 return Koha::Patron::HouseboundVisits->find($visit_id);
70 push @messages, { type => 'error', code => 'error_on_visit_load' }
75 my ( $deliverers, $choosers, $houseboundvisit );
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{},
89 my $success = eval { return $houseboundprofile->store };
90 push @messages, { type => 'error', code => 'error_on_profile_store' }
91 if ( $@ or !$success );
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{},
106 my $success = eval { return $houseboundprofile->store };
107 push @messages, { type => 'error', code => 'error_on_profile_create' }
108 if ( $@ or !$success );
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 );
121 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
122 # We have received input for editing a visit. We must store and return to
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{},
131 my $success = eval { return $visit->store };
132 push @messages, { type => 'error', code => 'error_on_visit_store' }
133 if ( $@ or !$success );
135 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
136 # We have received input for creating a visit. We must store and return
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{},
145 my $success = eval { return $visit->store };
146 push @messages, { type => 'error', code => 'error_on_visit_create' }
147 if ( $@ or !$success );
151 # We don't have any profile information, so we must display a creation form.
152 $method = 'update_or_create' if ( !$houseboundprofile );
154 # Ensure template has all patron details.
155 $template->param( patron => $patron );
158 housebound_profile => $houseboundprofile,
159 visit => $houseboundvisit,
160 messages => \@messages,
162 choosers => $choosers,
163 deliverers => $deliverers,
164 houseboundview => 'on',
167 output_html_with_http_headers $input, $cookie, $template->output;
171 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>