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 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
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.
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.
22 Script to handle housebound management for patrons. This single script
23 handles display, creation, deletion and management of profiles and visits.
36 use Koha
::Patron
::Categories
;
37 use Koha
::Patron
::HouseboundProfile
;
38 use Koha
::Patron
::HouseboundVisit
;
39 use Koha
::Patron
::HouseboundVisits
;
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
45 template_name
=> 'members/housebound.tt',
49 flagsrequired
=> { borrowers
=> 'edit_borrowers' },
53 my @messages; # For error messages.
54 my $method = $input->param('method') // q{};
55 my $visit_id = $input->param('visit_id') // q{};
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 } );
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;
70 return Koha
::Patron
::HouseboundVisits
->find($visit_id);
72 push @messages, { type
=> 'error', code
=> 'error_on_visit_load' }
77 my ( $deliverers, $choosers, $houseboundvisit );
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{},
91 my $success = eval { return $houseboundprofile->store };
92 push @messages, { type
=> 'error', code
=> 'error_on_profile_store' }
93 if ( $@
or !$success );
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{},
108 my $success = eval { return $houseboundprofile->store };
109 push @messages, { type
=> 'error', code
=> 'error_on_profile_create' }
110 if ( $@
or !$success );
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 );
123 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
124 # We have received input for editing a visit. We must store and return to
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{},
133 my $success = eval { return $visit->store };
134 push @messages, { type
=> 'error', code
=> 'error_on_visit_store' }
135 if ( $@
or !$success );
137 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
138 # We have received input for creating a visit. We must store and return
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{},
147 my $success = eval { return $visit->store };
148 push @messages, { type
=> 'error', code
=> 'error_on_visit_create' }
149 if ( $@
or !$success );
153 # We don't have any profile information, so we must display a creation form.
154 $method = 'update_or_create' if ( !$houseboundprofile );
156 # Ensure template has all patron details.
157 $template->param( patron
=> $patron );
160 housebound_profile
=> $houseboundprofile,
161 visit
=> $houseboundvisit,
162 messages
=> \
@messages,
164 choosers
=> $choosers,
165 deliverers
=> $deliverers,
166 houseboundview
=> 'on',
169 output_html_with_http_headers
$input, $cookie, $template->output;
173 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>