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.
33 use Koha::Patron::Categories;
34 use Koha::Patron::HouseboundProfile;
35 use Koha::Patron::HouseboundVisit;
36 use Koha::Patron::HouseboundVisits;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 template_name => 'members/housebound.tt',
46 flagsrequired => { borrowers => 1 },
50 my @messages; # For error messages.
51 my $method = $input->param('method') // q{};
52 my $visit_id = $input->param('visit_id') // q{};
56 return Koha::Patrons->new->find($input->param('borrowernumber'));
58 push @messages, { type => 'error', code => 'error_on_patron_load' }
59 if ( $@ or !$patron );
62 my ( $branch, $category, $houseboundprofile, $visit );
64 $branch = Koha::Libraries->new->find($patron->branchcode);
65 $category = Koha::Patron::Categories->new->find($patron->categorycode);
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 ( $houseboundvisits, $deliverers, $choosers );
78 my ( $houseboundvisit, $deliverer, $chooser );
80 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
81 # We have received the input from the profile edit form. We must save the
82 # changes, and return to simple display.
83 $houseboundprofile->set({
84 day => $input->param('day') // q{},
85 frequency => $input->param('frequency') // q{},
86 fav_itemtypes => $input->param('fav_itemtypes') // q{},
87 fav_subjects => $input->param('fav_subjects') // q{},
88 fav_authors => $input->param('fav_authors') // q{},
89 referral => $input->param('referral') // q{},
90 notes => $input->param('notes') // q{},
92 my $success = eval { return $houseboundprofile->store };
93 push @messages, { type => 'error', code => 'error_on_profile_store' }
94 if ( $@ or !$success );
96 } elsif ( $method eq 'createconfirm' ) {
97 # We have received the input necessary to create a new profile. We must
98 # save it, and return to simple display.
99 $houseboundprofile = Koha::Patron::HouseboundProfile->new({
100 borrowernumber => $patron->borrowernumber,
101 day => $input->param('day') // q{},
102 frequency => $input->param('frequency') // q{},
103 fav_itemtypes => $input->param('fav_itemtypes') // q{},
104 fav_subjects => $input->param('fav_subjects') // q{},
105 fav_authors => $input->param('fav_authors') // q{},
106 referral => $input->param('referral') // q{},
107 notes => $input->param('notes') // q{},
109 my $success = eval { return $houseboundprofile->store };
110 push @messages, { type => 'error', code => 'error_on_profile_create' }
111 if ( $@ or !$success );
113 } elsif ( $method eq 'visit_update_or_create' ) {
114 # We want to edit, edit a visit, so we must pass its details.
115 $deliverers = Koha::Patrons->new->housebound_deliverers;
116 $choosers = Koha::Patrons->new->housebound_choosers;
117 $houseboundvisit = $visit;
118 } elsif ( $method eq 'visit_delete' and $visit ) {
119 # We want ot delete a specific visit.
120 my $success = eval { return $visit->delete };
121 push @messages, { type => 'error', code => 'error_on_visit_delete' }
122 if ( $@ or !$success );
124 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
125 # We have received input for editing a visit. We must store and return to
128 borrowernumber => $input->param('borrowernumber') // q{},
129 appointment_date => $input->param('date') // q{},
130 day_segment => $input->param('segment') // q{},
131 chooser_brwnumber => $input->param('chooser') // q{},
132 deliverer_brwnumber => $input->param('deliverer') // q{},
134 my $success = eval { return $visit->store };
135 push @messages, { type => 'error', code => 'error_on_visit_store' }
136 if ( $@ or !$success );
138 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
139 # We have received input for creating a visit. We must store and return
141 my $visit = Koha::Patron::HouseboundVisit->new({
142 borrowernumber => $input->param('borrowernumber') // q{},
143 appointment_date => $input->param('date') // q{},
144 day_segment => $input->param('segment') // q{},
145 chooser_brwnumber => $input->param('chooser') // q{},
146 deliverer_brwnumber => $input->param('deliverer') // q{},
148 my $success = eval { return $visit->store };
149 push @messages, { type => 'error', code => 'error_on_visit_create' }
150 if ( $@ or !$success );
154 # We don't have any profile information, so we must display a creation form.
155 $method = 'update_or_create' if ( !$houseboundprofile );
159 housebound_profile => $houseboundprofile,
160 visit => $houseboundvisit,
162 category => $category,
163 messages => \@messages,
165 choosers => $choosers,
166 deliverers => $deliverers,
167 houseboundview => 'on',
170 output_html_with_http_headers $input, $cookie, $template->output;
174 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>