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 my $borrowernumber = $input->param('borrowernumber') // q{};
59 return Koha::Patrons->new->find($borrowernumber);
61 push @messages, { type => 'error', code => 'error_on_patron_load' }
62 if ( $@ or !$patron );
65 my ( $branch, $category, $houseboundprofile, $visit );
67 $branch = Koha::Libraries->new->find($patron->branchcode);
68 $category = Koha::Patron::Categories->new->find($patron->categorycode);
69 $houseboundprofile = $patron->housebound_profile;
73 return Koha::Patron::HouseboundVisits->find($visit_id);
75 push @messages, { type => 'error', code => 'error_on_visit_load' }
80 my ( $houseboundvisits, $deliverers, $choosers );
81 my ( $houseboundvisit, $deliverer, $chooser );
83 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
84 # We have received the input from the profile edit form. We must save the
85 # changes, and return to simple display.
86 $houseboundprofile->set({
87 day => $input->param('day') // q{},
88 frequency => $input->param('frequency') // q{},
89 fav_itemtypes => $input->param('fav_itemtypes') // q{},
90 fav_subjects => $input->param('fav_subjects') // q{},
91 fav_authors => $input->param('fav_authors') // q{},
92 referral => $input->param('referral') // q{},
93 notes => $input->param('notes') // q{},
95 my $success = eval { return $houseboundprofile->store };
96 push @messages, { type => 'error', code => 'error_on_profile_store' }
97 if ( $@ or !$success );
99 } elsif ( $method eq 'createconfirm' ) {
100 # We have received the input necessary to create a new profile. We must
101 # save it, and return to simple display.
102 $houseboundprofile = Koha::Patron::HouseboundProfile->new({
103 borrowernumber => $patron->borrowernumber,
104 day => $input->param('day') // q{},
105 frequency => $input->param('frequency') // q{},
106 fav_itemtypes => $input->param('fav_itemtypes') // q{},
107 fav_subjects => $input->param('fav_subjects') // q{},
108 fav_authors => $input->param('fav_authors') // q{},
109 referral => $input->param('referral') // q{},
110 notes => $input->param('notes') // q{},
112 my $success = eval { return $houseboundprofile->store };
113 push @messages, { type => 'error', code => 'error_on_profile_create' }
114 if ( $@ or !$success );
116 } elsif ( $method eq 'visit_update_or_create' ) {
117 # We want to edit, edit a visit, so we must pass its details.
118 $deliverers = Koha::Patrons->new->housebound_deliverers;
119 $choosers = Koha::Patrons->new->housebound_choosers;
120 $houseboundvisit = $visit;
121 } elsif ( $method eq 'visit_delete' and $visit ) {
122 # We want ot delete a specific visit.
123 my $success = eval { return $visit->delete };
124 push @messages, { type => 'error', code => 'error_on_visit_delete' }
125 if ( $@ or !$success );
127 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
128 # We have received input for editing a visit. We must store and return to
131 borrowernumber => $input->param('borrowernumber') // q{},
132 appointment_date => dt_from_string($input->param('date') // q{}),
133 day_segment => $input->param('segment') // q{},
134 chooser_brwnumber => $input->param('chooser') // q{},
135 deliverer_brwnumber => $input->param('deliverer') // q{},
137 my $success = eval { return $visit->store };
138 push @messages, { type => 'error', code => 'error_on_visit_store' }
139 if ( $@ or !$success );
141 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
142 # We have received input for creating a visit. We must store and return
144 my $visit = Koha::Patron::HouseboundVisit->new({
145 borrowernumber => $input->param('borrowernumber') // q{},
146 appointment_date => dt_from_string($input->param('date') // q{}),
147 day_segment => $input->param('segment') // q{},
148 chooser_brwnumber => $input->param('chooser') // q{},
149 deliverer_brwnumber => $input->param('deliverer') // q{},
151 my $success = eval { return $visit->store };
152 push @messages, { type => 'error', code => 'error_on_visit_create' }
153 if ( $@ or !$success );
157 # We don't have any profile information, so we must display a creation form.
158 $method = 'update_or_create' if ( !$houseboundprofile );
162 housebound_profile => $houseboundprofile,
163 visit => $houseboundvisit,
165 category => $category,
166 messages => \@messages,
168 choosers => $choosers,
169 deliverers => $deliverers,
170 houseboundview => 'on',
173 output_html_with_http_headers $input, $cookie, $template->output;
177 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>