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.
31 use C4::Members::Attributes qw(GetBorrowerAttributes);
37 use Koha::Patron::Categories;
38 use Koha::Patron::HouseboundProfile;
39 use Koha::Patron::HouseboundVisit;
40 use Koha::Patron::HouseboundVisits;
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46 template_name => 'members/housebound.tt',
50 flagsrequired => { borrowers => 'edit_borrowers' },
54 my @messages; # For error messages.
55 my $method = $input->param('method') // q{};
56 my $visit_id = $input->param('visit_id') // q{};
59 my $borrowernumber = $input->param('borrowernumber');
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
61 my $patron = Koha::Patrons->find($borrowernumber);
62 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
65 my ( $branch, $category, $houseboundprofile, $visit, $patron_image );
67 $patron_image = $patron->image;
68 $branch = Koha::Libraries->new->find($patron->branchcode);
69 $category = Koha::Patron::Categories->new->find($patron->categorycode);
70 $houseboundprofile = $patron->housebound_profile;
74 return Koha::Patron::HouseboundVisits->find($visit_id);
76 push @messages, { type => 'error', code => 'error_on_visit_load' }
81 my ( $deliverers, $choosers, $houseboundvisit );
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 => scalar $input->param('day') // q{},
88 frequency => scalar $input->param('frequency') // q{},
89 fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
90 fav_subjects => scalar $input->param('fav_subjects') // q{},
91 fav_authors => scalar $input->param('fav_authors') // q{},
92 referral => scalar $input->param('referral') // q{},
93 notes => scalar $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 => scalar $input->param('day') // q{},
105 frequency => scalar $input->param('frequency') // q{},
106 fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
107 fav_subjects => scalar $input->param('fav_subjects') // q{},
108 fav_authors => scalar $input->param('fav_authors') // q{},
109 referral => scalar $input->param('referral') // q{},
110 notes => scalar $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->search_housebound_deliverers;
119 $choosers = Koha::Patrons->search_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 => scalar $input->param('borrowernumber') // q{},
132 appointment_date => dt_from_string($input->param('date') // q{}),
133 day_segment => scalar $input->param('segment') // q{},
134 chooser_brwnumber => scalar $input->param('chooser') // q{},
135 deliverer_brwnumber => scalar $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 => scalar $input->param('borrowernumber') // q{},
146 appointment_date => dt_from_string($input->param('date') // q{}),
147 day_segment => scalar $input->param('segment') // q{},
148 chooser_brwnumber => scalar $input->param('chooser') // q{},
149 deliverer_brwnumber => scalar $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 );
160 # Ensure template has all patron details.
161 $template->param(%{$patron->unblessed}) if ( $patron );
163 # Load extended patron attributes if necessary (taken from members/files.pl).
164 if ( C4::Context->preference('ExtendedPatronAttributes') and $patron ) {
165 my $attributes = GetBorrowerAttributes($patron->borrowernumber);
167 ExtendedPatronAttributes => 1,
168 extendedattributes => $attributes
172 $template->param( adultborrower => 1 ) if ( $category->category_type eq 'A' || $category->category_type eq 'I' );
174 picture => $patron_image,
175 housebound_profile => $houseboundprofile,
176 visit => $houseboundvisit,
178 category => $category,
179 messages => \@messages,
181 choosers => $choosers,
182 deliverers => $deliverers,
183 houseboundview => 'on',
186 output_html_with_http_headers $input, $cookie, $template->output;
190 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>