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.
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',
48 flagsrequired => { borrowers => 1 },
52 my @messages; # For error messages.
53 my $method = $input->param('method') // q{};
54 my $visit_id = $input->param('visit_id') // q{};
58 return Koha::Patrons->new->find($input->param('borrowernumber'));
60 push @messages, { type => 'error', code => 'error_on_patron_load' }
61 if ( $@ or !$patron );
64 my ( $branch, $category, $houseboundprofile, $visit );
66 $branch = Koha::Libraries->new->find($patron->branchcode);
67 $category = Koha::Patron::Categories->new->find($patron->categorycode);
68 $houseboundprofile = $patron->housebound_profile;
72 return Koha::Patron::HouseboundVisits->find($visit_id);
74 push @messages, { type => 'error', code => 'error_on_visit_load' }
79 my ( $houseboundvisits, $deliverers, $choosers );
80 my ( $houseboundvisit, $deliverer, $chooser );
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{},
94 my $success = eval { return $houseboundprofile->store };
95 push @messages, { type => 'error', code => 'error_on_profile_store' }
96 if ( $@ or !$success );
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{},
111 my $success = eval { return $houseboundprofile->store };
112 push @messages, { type => 'error', code => 'error_on_profile_create' }
113 if ( $@ or !$success );
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 );
126 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
127 # We have received input for editing a visit. We must store and return to
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{},
136 my $success = eval { return $visit->store };
137 push @messages, { type => 'error', code => 'error_on_visit_store' }
138 if ( $@ or !$success );
140 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
141 # We have received input for creating a visit. We must store and return
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{},
150 my $success = eval { return $visit->store };
151 push @messages, { type => 'error', code => 'error_on_visit_create' }
152 if ( $@ or !$success );
156 # We don't have any profile information, so we must display a creation form.
157 $method = 'update_or_create' if ( !$houseboundprofile );
161 housebound_profile => $houseboundprofile,
162 visit => $houseboundvisit,
164 category => $category,
165 messages => \@messages,
167 choosers => $choosers,
168 deliverers => $deliverers,
169 houseboundview => 'on',
172 output_html_with_http_headers $input, $cookie, $template->output;
176 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>