Bug 5670: Housebound Readers Module
[koha.git] / members / housebound.pl
1 #!/usr/bin/perl
2
3 # Copyright 2016 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 =head1 housebound.pl
21
22  Script to handle housebound management for patrons.  This single script
23  handles display, creation, deletion and management of profiles and visits.
24
25 =cut
26
27 use Modern::Perl;
28 use CGI;
29 use C4::Auth;
30 use C4::Output;
31 use Koha::Libraries;
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::Patron::HouseboundProfile;
35 use Koha::Patron::HouseboundVisit;
36 use Koha::Patron::HouseboundVisits;
37
38 my $input = CGI->new;
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => 'members/housebound.tt',
43         query           => $input,
44         type            => 'intranet',
45         authnotrequired => 0,
46         flagsrequired   => { borrowers => 1 },
47     }
48 );
49
50 my $patron = Koha::Patrons->new->find($input->param('borrowernumber'));
51 my $method = $input->param('method') // q{};
52 my $visit_id = $input->param('visit_id') // q{};
53 my $branch = Koha::Libraries->new->find($patron->branchcode);
54 my $category = Koha::Patron::Categories->new->find($patron->categorycode);
55 my $houseboundprofile = $patron->housebound_profile;
56
57 my ( $houseboundvisits, $deliverers, $choosers );
58 my ( $houseboundvisit, $deliverer, $chooser );
59
60 if ( $method eq 'updateconfirm' ) {
61     # We have received the input from the profile edit form.  We must save the
62     # changes, and return to simple display.
63     $houseboundprofile->set({
64         day           => $input->param('day')           // q{},
65         frequency     => $input->param('frequency')     // q{},
66         fav_itemtypes => $input->param('fav_itemtypes') // q{},
67         fav_subjects  => $input->param('fav_subjects')  // q{},
68         fav_authors   => $input->param('fav_authors')   // q{},
69         referral      => $input->param('referral')      // q{},
70         notes         => $input->param('notes')         // q{},
71     });
72     die("Unable to store edited profile")
73         unless ( $houseboundprofile->store );
74     $method = undef;
75 } elsif ( $method eq 'createconfirm' ) {
76     # We have received the input necessary to create a new profile.  We must
77     # save it, and return to simple display.
78     $houseboundprofile = Koha::Patron::HouseboundProfile->new({
79         borrowernumber => $patron->borrowernumber,
80         day            => $input->param('day')           // q{},
81         frequency      => $input->param('frequency')     // q{},
82         fav_itemtypes  => $input->param('fav_itemtypes') // q{},
83         fav_subjects   => $input->param('fav_subjects')  // q{},
84         fav_authors    => $input->param('fav_authors')   // q{},
85         referral       => $input->param('referral')      // q{},
86         notes          => $input->param('notes')         // q{},
87     });
88     die("Unable to store new profile")
89         unless ( $houseboundprofile->store );
90     $method = undef;
91 } elsif ( $method eq 'visit_update_or_create' ) {
92     # We want to edit, edit a visit, so we must pass its details.
93     $deliverers = Koha::Patrons->new->housebound_deliverers;
94     $choosers = Koha::Patrons->new->housebound_choosers;
95     $houseboundvisit = Koha::Patron::HouseboundVisits->find($visit_id)
96         if ( $visit_id );
97 } elsif ( $method eq 'visit_delete' ) {
98     # We want ot delete a specific visit.
99     my $visit = Koha::Patron::HouseboundVisits->find($visit_id);
100     die("Unable to delete visit") unless ( $visit->delete );
101     $method = undef;
102 } elsif ( $method eq 'editvisitconfirm' ) {
103     # We have received input for editing a visit.  We must store and return to
104     # simple display.
105     my $visit = Koha::Patron::HouseboundVisits->find($visit_id);
106     $visit->set({
107         borrowernumber      => $input->param('borrowernumber') // q{},
108         appointment_date    => $input->param('date')           // q{},
109         day_segment         => $input->param('segment')        // q{},
110         chooser_brwnumber   => $input->param('chooser')        // q{},
111         deliverer_brwnumber => $input->param('deliverer')      // q{},
112     });
113     die("Unable to store edited visit") unless ( $visit->store );
114     $method = undef;
115 } elsif ( $method eq 'addvisitconfirm' ) {
116     # We have received input for creating a visit.  We must store and return
117     # to simple display.
118     my $visit = Koha::Patron::HouseboundVisit->new({
119         borrowernumber      => $input->param('borrowernumber') // q{},
120         appointment_date    => $input->param('date')           // q{},
121         day_segment         => $input->param('segment')        // q{},
122         chooser_brwnumber   => $input->param('chooser')        // q{},
123         deliverer_brwnumber => $input->param('deliverer')      // q{},
124     });
125     die("Unable to store new visit") unless ( $visit->store );
126     $method = undef;
127 }
128
129 # We don't have any profile information, so we must display a creation form.
130 $method = 'update_or_create' if ( !$houseboundprofile );
131
132 $template->param(
133     patron             => $patron,
134     housebound_profile => $houseboundprofile,
135     visit              => $houseboundvisit,
136     branch             => $branch,
137     category           => $category,
138     method             => $method,
139     choosers           => $choosers,
140     deliverers         => $deliverers,
141     houseboundview     => 'on',
142 );
143
144 output_html_with_http_headers $input, $cookie, $template->output;
145
146 =head1 AUTHOR
147
148 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
149
150 =cut