Bug 24183: Add before_send_messages hook
[koha.git] / tools / modborrowers.pl
blob6c53a23912d4eebd03bfa11b972b31d1794ac828
1 #!/usr/bin/perl
3 # Copyright 2012 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 # modborrowers.pl
22 # Batch Edit Patrons
23 # Modification for patron's fields:
24 # surname firstname branchcode categorycode city state zipcode country sort1
25 # sort2 dateenrolled dateexpiry borrowernotes
26 # And for patron attributes.
28 use Modern::Perl;
29 use CGI qw ( -utf8 );
30 use C4::Auth;
31 use C4::Koha;
32 use C4::Members;
33 use C4::Output;
34 use List::MoreUtils qw /any uniq/;
35 use Koha::DateUtils qw( dt_from_string );
36 use Koha::List::Patron;
37 use Koha::Libraries;
38 use Koha::Patron::Categories;
39 use Koha::Patron::Debarments;
40 use Koha::Patrons;
42 my $input = new CGI;
43 my $op = $input->param('op') || 'show_form';
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 { template_name => "tools/modborrowers.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { tools => "edit_patrons" },
53 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
55 my %cookies = parse CGI::Cookie($cookie);
56 my $sessionID = $cookies{'CGISESSID'}->value;
57 my $dbh = C4::Context->dbh;
59 # Show borrower informations
60 if ( $op eq 'show' ) {
61 my $filefh = $input->upload('uploadfile');
62 my $filecontent = $input->param('filecontent');
63 my $patron_list_id = $input->param('patron_list_id');
64 my @borrowers;
65 my @cardnumbers;
66 my ( @notfoundcardnumbers, @from_another_group_of_libraries );
68 # Get cardnumbers from a file or the input area
69 my @contentlist;
70 if ($filefh) {
71 while ( my $content = <$filefh> ) {
72 $content =~ s/[\r\n]*$//g;
73 push @cardnumbers, $content if $content;
75 } elsif ( $patron_list_id ) {
76 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
78 @cardnumbers =
79 $list->patron_list_patrons()->search_related('borrowernumber')
80 ->get_column('cardnumber')->all();
82 } else {
83 if ( my $list = $input->param('cardnumberlist') ) {
84 push @cardnumbers, split( /\s\n/, $list );
88 my $max_nb_attr = 0;
89 for my $cardnumber ( @cardnumbers ) {
90 my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
91 if ( $patron ) {
92 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
93 my $borrower = $patron->unblessed;
94 my $attributes = $patron->extended_attributes;
95 $borrower->{patron_attributes} = $attributes->as_list;
96 $borrower->{patron_attributes_count} = $attributes->count;
97 $max_nb_attr = $borrower->{patron_attributes_count} if $borrower->{patron_attributes_count} > $max_nb_attr;
98 push @borrowers, $borrower;
99 } else {
100 push @notfoundcardnumbers, $cardnumber;
102 } else {
103 push @notfoundcardnumbers, $cardnumber;
107 # Just for a correct display
108 for my $borrower ( @borrowers ) {
109 my $length = $borrower->{patron_attributes_count};
110 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
113 # Construct the patron attributes list
114 my @patron_attributes_values;
115 my @patron_attributes_codes;
116 my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
117 my $patron_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
118 my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
119 while ( my $attr_type = $patron_attribute_types->next ) {
120 # TODO Repeatable attributes are not correctly managed and can cause data lost.
121 # This should be implemented.
122 next if $attr_type->repeatable;
123 next if $attr_type->unique_id; # Don't display patron attributes that must be unqiue
124 my $options = $attr_type->authorised_value_category
125 ? GetAuthorisedValues( $attr_type->authorised_value_category )
126 : undef;
127 push @patron_attributes_values,
129 attribute_code => $attr_type->code,
130 options => $options,
133 my $category_code = $attr_type->category_code;
134 my ( $category_lib ) = map {
135 ( defined $category_code and $attr_type->categorycode eq $category_code ) ? $attr_type->description : ()
136 } @patron_categories;
137 push @patron_attributes_codes,
139 attribute_code => $attr_type->code,
140 attribute_lib => $attr_type->description,
141 category_lib => $category_lib,
142 type => $attr_type->authorised_value_category ? 'select' : 'text',
146 my @attributes_header = ();
147 for ( 1 .. scalar( $max_nb_attr ) ) {
148 push @attributes_header, { attribute => "Attributes $_" };
150 $template->param( borrowers => \@borrowers );
151 $template->param( attributes_header => \@attributes_header );
152 @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
153 $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
154 if @notfoundcardnumbers;
156 # Construct drop-down list values
157 my $branches = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
158 my @branches_option;
159 push @branches_option, { value => $_->{branchcode}, lib => $_->{branchname} } for @$branches;
160 unshift @branches_option, { value => "", lib => "" };
161 my @categories_option;
162 push @categories_option, { value => $_->categorycode, lib => $_->description } for @patron_categories;
163 unshift @categories_option, { value => "", lib => "" };
164 my $bsort1 = GetAuthorisedValues("Bsort1");
165 my @sort1_option;
166 push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
167 unshift @sort1_option, { value => "", lib => "" }
168 if @sort1_option;
169 my $bsort2 = GetAuthorisedValues("Bsort2");
170 my @sort2_option;
171 push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
172 unshift @sort2_option, { value => "", lib => "" }
173 if @sort2_option;
175 my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
177 my @fields = (
179 name => "surname",
180 type => "text",
181 mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
185 name => "firstname",
186 type => "text",
187 mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
191 name => "branchcode",
192 type => "select",
193 option => \@branches_option,
194 mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
198 name => "categorycode",
199 type => "select",
200 option => \@categories_option,
201 mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
205 name => "streetnumber",
206 type => "text",
207 mandatory => ( grep /streetnumber/, @mandatoryFields ) ? 1 : 0,
211 name => "address",
212 type => "text",
213 mandatory => ( grep /address/, @mandatoryFields ) ? 1 : 0,
217 name => "address2",
218 type => "text",
219 mandatory => ( grep /address2/, @mandatoryFields ) ? 1 : 0,
223 name => "city",
224 type => "text",
225 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
229 name => "state",
230 type => "text",
231 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
235 name => "zipcode",
236 type => "text",
237 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
241 name => "country",
242 type => "text",
243 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
247 name => "email",
248 type => "text",
249 mandatory => ( grep /email/, @mandatoryFields ) ? 1 : 0,
253 name => "phone",
254 type => "text",
255 mandatory => ( grep /phone/, @mandatoryFields ) ? 1 : 0,
259 name => "mobile",
260 type => "text",
261 mandatory => ( grep /mobile/, @mandatoryFields ) ? 1 : 0,
265 name => "sort1",
266 type => @sort1_option ? "select" : "text",
267 option => \@sort1_option,
268 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
272 name => "sort2",
273 type => @sort2_option ? "select" : "text",
274 option => \@sort2_option,
275 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
279 name => "dateenrolled",
280 type => "date",
281 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
285 name => "dateexpiry",
286 type => "date",
287 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
291 name => "borrowernotes",
292 type => "text",
293 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
297 name => "opacnote",
298 type => "text",
299 mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
303 name => "debarred",
304 type => "date",
305 mandatory => ( grep /debarred/, @mandatoryFields ) ? 1 : 0,
309 name => "debarredcomment",
310 type => "text",
311 mandatory => ( grep /debarredcomment/, @mandatoryFields ) ? 1 : 0,
315 $template->param('patron_attributes_codes', \@patron_attributes_codes);
316 $template->param('patron_attributes_values', \@patron_attributes_values);
318 $template->param( fields => \@fields );
321 # Process modifications
322 if ( $op eq 'do' ) {
324 my @disabled = $input->multi_param('disable_input');
325 my $infos;
326 for my $field ( qw/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote/ ) {
327 my $value = $input->param($field);
328 $infos->{$field} = $value if $value;
329 $infos->{$field} = "" if grep { $_ eq $field } @disabled;
332 for my $field ( qw( dateenrolled dateexpiry debarred ) ) {
333 $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
336 my @attributes = $input->multi_param('patron_attributes');
337 my @attr_values = $input->multi_param('patron_attributes_value');
339 my @errors;
340 my @borrowernumbers = $input->multi_param('borrowernumber');
341 # For each borrower selected
342 for my $borrowernumber ( @borrowernumbers ) {
343 # If at least one field are filled, we want to modify the borrower
344 if ( defined $infos ) {
345 # If a debarred date or debarred comment has been submitted make a new debarment
346 if ( $infos->{debarred} || $infos->{debarredcomment} ) {
347 AddDebarment(
349 borrowernumber => $borrowernumber,
350 type => 'MANUAL',
351 comment => $infos->{debarredcomment},
352 expiration => $infos->{debarred},
356 # If debarment date or debarment comment are disabled then remove all debarrments
357 if ( grep { /debarred/ } @disabled ) {
358 eval {
359 my $debarrments = GetDebarments( { borrowernumber => $borrowernumber } );
360 foreach my $debarment (@$debarrments) {
361 DelDebarment( $debarment->{'borrower_debarment_id'} );
366 $infos->{borrowernumber} = $borrowernumber;
367 eval { Koha::Patrons->find( $borrowernumber )->set($infos)->store; };
368 if ( $@ ) { # FIXME We could provide better error handling here
369 my $patron = Koha::Patrons->find( $borrowernumber );
370 $infos->{cardnumber} = $patron ? $patron->cardnumber || '' : '';
371 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
375 my $patron = Koha::Patrons->find( $borrowernumber );
376 my $i=0;
377 for ( @attributes ) {
378 next unless $_;
379 my $attribute;
380 $attribute->{code} = $_;
381 $attribute->{attribute} = $attr_values[$i];
382 my $attr_type = Koha::Patron::Attribute::Types->find($_);
383 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
384 ++$i and next if $attr_type->category_code and $attr_type->category_code ne $patron->category_code;
385 my $valuename = "attr" . $i . "_value";
386 if ( grep { $_ eq $valuename } @disabled ) {
387 # The attribute is disabled, we remove it for this borrower !
388 eval {
389 $patron->get_extended_attribute($attribute->{code})->delete;
391 push @errors, { error => $@ } if $@;
392 } else {
393 eval {
394 # Note:
395 # We should not need to filter by branch, but stay on the safe side
396 # Repeatable are not supported so we can do that - TODO
397 $patron->extended_attributes->search({'me.code' => $attribute->{code}})->filter_by_branch_limitations->delete;
398 $patron->add_extended_attribute($attribute);
400 push @errors, { error => $@ } if $@;
402 $i++;
405 $op = "show_results"; # We have process modifications, the user want to view its
407 # Construct the results list
408 my @borrowers;
409 my $max_nb_attr = 0;
410 for my $borrowernumber ( @borrowernumbers ) {
411 my $patron = Koha::Patrons->find( $borrowernumber );
412 if ( $patron ) {
413 my $category_description = $patron->category->description;
414 my $borrower = $patron->unblessed;
415 $borrower->{category_description} = $category_description;
416 my $attributes = $patron->extended_attributes;
417 $borrower->{patron_attributes} = $attributes->as_list;
418 $max_nb_attr = $attributes->count if $attributes->count > $max_nb_attr;
419 push @borrowers, $borrower;
422 my @patron_attributes_option;
423 for my $borrower ( @borrowers ) {
424 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
425 my $length = scalar( @{ $borrower->{patron_attributes} } );
426 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
429 my @attributes_header = ();
430 for ( 1 .. scalar( $max_nb_attr ) ) {
431 push @attributes_header, { attribute => "Attributes $_" };
434 $template->param( borrowers => \@borrowers );
435 $template->param( attributes_header => \@attributes_header );
437 $template->param( errors => \@errors );
438 } else {
440 $template->param( patron_lists => [ GetPatronLists() ] );
443 $template->param(
444 op => $op,
446 output_html_with_http_headers $input, $cookie, $template->output;
447 exit;