Bug 16612 - Cannot set "Until date" for "Enrollment period" for Patron Categories
[koha.git] / admin / categories.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Auth;
26 use C4::Branch;
27 use C4::Output;
28 use C4::Form::MessagingPreferences;
29 use Koha::Patrons;
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Patron::Categories;
33
34 my $input         = new CGI;
35 my $searchfield   = $input->param('description') // q||;
36 my $categorycode  = $input->param('categorycode');
37 my $op            = $input->param('op') // 'list';
38 my @messages;
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "admin/categories.tt",
43         query           => $input,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
47         debug           => 1,
48     }
49 );
50
51 if ( $op eq 'add_form' ) {
52     my ( $category, $selected_branches );
53     if ($categorycode) {
54         $category          = Koha::Patron::Categories->find($categorycode);
55         $selected_branches = $category->branch_limitations;
56     }
57
58     my $branches = GetBranches;
59     my @branches_loop;
60     foreach my $branchcode ( sort { uc( $branches->{$a}->{branchname} ) cmp uc( $branches->{$b}->{branchname} ) } keys %$branches ) {
61         my $selected = ( grep { $_ eq $branchcode } @$selected_branches ) ? 1 : 0;
62         push @branches_loop,
63           { branchcode => $branchcode,
64             branchname => $branches->{$branchcode}->{branchname},
65             selected   => $selected,
66           };
67     }
68
69     $template->param(
70         category => $category,
71         branches_loop       => \@branches_loop,
72     );
73
74     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
75         C4::Form::MessagingPreferences::set_form_values(
76             { categorycode => $categorycode }, $template );
77     }
78 }
79 elsif ( $op eq 'add_validate' ) {
80
81     my $categorycode = $input->param('categorycode');
82     my $description = $input->param('description');
83     my $enrolmentperiod = $input->param('enrolmentperiod');
84     my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
85     my $upperagelimit = $input->param('upperagelimit');
86     my $dateofbirthrequired = $input->param('dateofbirthrequired');
87     my $enrolmentfee = $input->param('enrolmentfee');
88     my $reservefee = $input->param('reservefee');
89     my $hidelostitems = $input->param('hidelostitems');
90     my $overduenoticerequired = $input->param('overduenoticerequired');
91     my $category_type = $input->param('category_type');
92     my $BlockExpiredPatronOpacActions = $input->param('BlockExpiredPatronOpacActions');
93     my $default_privacy = $input->param('default_privacy');
94     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
95
96     my $is_a_modif = $input->param("is_a_modif");
97
98     if ($enrolmentperioddate) {
99         $enrolmentperioddate = output_pref(
100             {
101                 dt         => dt_from_string($enrolmentperioddate),
102                 dateformat => 'iso',
103                 dateonly   => 1,
104             }
105         );
106     }
107
108     if ($is_a_modif) {
109         my $category = Koha::Patron::Categories->find( $categorycode );
110         $category->categorycode($categorycode);
111         $category->description($description);
112         $category->enrolmentperiod($enrolmentperiod);
113         $category->enrolmentperioddate($enrolmentperioddate);
114         $category->upperagelimit($upperagelimit);
115         $category->dateofbirthrequired($dateofbirthrequired);
116         $category->enrolmentfee($enrolmentfee);
117         $category->reservefee($reservefee);
118         $category->hidelostitems($hidelostitems);
119         $category->overduenoticerequired($overduenoticerequired);
120         $category->category_type($category_type);
121         $category->BlockExpiredPatronOpacActions($BlockExpiredPatronOpacActions);
122         $category->default_privacy($default_privacy);
123         eval {
124             $category->store;
125             $category->replace_branch_limitations( \@branches );
126         };
127         if ( $@ ) {
128             push @messages, {type => 'error', code => 'error_on_update' };
129         } else {
130             push @messages, { type => 'message', code => 'success_on_update' };
131         }
132     }
133     else {
134         my $category = Koha::Patron::Category->new({
135             categorycode => $categorycode,
136             description => $description,
137             enrolmentperiod => $enrolmentperiod,
138             enrolmentperioddate => $enrolmentperioddate,
139             upperagelimit => $upperagelimit,
140             dateofbirthrequired => $dateofbirthrequired,
141             enrolmentfee => $enrolmentfee,
142             reservefee => $reservefee,
143             hidelostitems => $hidelostitems,
144             overduenoticerequired => $overduenoticerequired,
145             category_type => $category_type,
146             BlockExpiredPatronOpacActions => $BlockExpiredPatronOpacActions,
147             default_privacy => $default_privacy,
148         });
149         eval {
150             $category->store;
151             $category->replace_branch_limitations( \@branches );
152         };
153
154         if ( $@ ) {
155             push @messages, { type => 'error', code => 'error_on_insert' };
156         } else {
157             push @messages, { type => 'message', code => 'success_on_insert' };
158         }
159     }
160
161     if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
162         C4::Form::MessagingPreferences::handle_form_action( $input,
163             { categorycode => scalar $input->param('categorycode') }, $template );
164     }
165
166     $searchfield = q||;
167     $op = 'list';
168 }
169 elsif ( $op eq 'delete_confirm' ) {
170
171     my $count = Koha::Patrons->search({
172         categorycode => $categorycode
173     })->count;
174
175     my $category = Koha::Patron::Categories->find($categorycode);
176
177     $template->param(
178         category => $category,
179         patrons_in_category => $count,
180     );
181
182 }
183 elsif ( $op eq 'delete_confirmed' ) {
184     my $categorycode = uc( $input->param('categorycode') );
185
186     my $category = Koha::Patron::Categories->find( $categorycode );
187     my $deleted = eval { $category->delete; };
188
189     if ( $@ or not $deleted ) {
190         push @messages, {type => 'error', code => 'error_on_delete' };
191     } else {
192         push @messages, { type => 'message', code => 'success_on_delete' };
193     }
194
195     $op = 'list';
196 }
197
198 if ( $op eq 'list' ) {
199     my $categories = Koha::Patron::Categories->search(
200         {
201             description => { -like => "$searchfield%" }
202         },
203         {
204             order_by => ['category_type', 'description', 'categorycode' ]
205         }
206     );
207
208     $template->param(
209         categories => $categories,
210     )
211 }
212
213 $template->param(
214     categorycode => $categorycode,
215     searchfield  => $searchfield,
216     messages     => \@messages,
217     op => $op,
218 );
219
220 output_html_with_http_headers $input, $cookie, $template->output;
221
222 exit 0;