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>.
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.
30 use C4::Auth qw( get_template_and_user );
31 use C4::Koha qw( GetAuthorisedValues );
33 use C4::Output qw( output_html_with_http_headers );
34 use Koha::DateUtils qw( dt_from_string );
35 use Koha::List::Patron qw( GetPatronLists );
37 use Koha::Patron::Categories;
38 use Koha::Patron::Debarments qw( AddDebarment DelDebarment );
40 use List::MoreUtils qw(uniq);
43 my $op = $input->param('op') || 'show_form';
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 { template_name => "tools/modborrowers.tt",
48 flagsrequired => { tools => "edit_patrons" },
52 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
54 $template->param( CanUpdatePasswordExpiration => 1 ) if $logged_in_user->is_superlibrarian;
56 my $dbh = C4::Context->dbh;
58 # Show borrower informations
59 if ( $op eq 'show' ) {
62 my @notfoundcardnumbers;
63 my $useborrowernumbers = 0;
65 # Get cardnumbers from a file or the input area
66 if( my $cardnumberlist = $input->param('cardnumberlist') ){
67 # User submitted a list of card numbers
68 push @patronidnumbers, split( /\s\n/, $cardnumberlist );
69 } elsif ( my $cardnumberuploadfile = $input->param('cardnumberuploadfile') ){
70 # User uploaded a file of card numbers
71 binmode $cardnumberuploadfile, ':encoding(UTF-8)';
72 while ( my $content = <$cardnumberuploadfile> ) {
74 $content =~ s/[\r\n]*$//;
75 push @patronidnumbers, $content if $content;
77 } elsif ( my $borrowernumberlist = $input->param('borrowernumberlist') ){
78 # User submitted a list of borrowernumbers
79 $useborrowernumbers = 1;
80 push @patronidnumbers, split( /\s\n/, $borrowernumberlist );
81 } elsif ( my $borrowernumberuploadfile = $input->param('borrowernumberuploadfile') ){
82 # User uploaded a file of borrowernumbers
83 $useborrowernumbers = 1;
84 binmode $borrowernumberuploadfile, ':encoding(UTF-8)';
85 while ( my $content = <$borrowernumberuploadfile> ) {
87 $content =~ s/[\r\n]*$//;
88 push @patronidnumbers, $content if $content;
90 } elsif ( my $patron_list_id = $input->param('patron_list_id') ){
91 # User selected a patron list
92 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
94 $list->patron_list_patrons()->search_related('borrowernumber')
95 ->get_column('cardnumber')->all();
100 # Make sure there is only one of each patron id number
101 @patronidnumbers = uniq( @patronidnumbers );
103 for my $patronidnumber ( @patronidnumbers ) {
105 if( $useborrowernumbers == 1 ){
106 $patron = Koha::Patrons->find( { borrowernumber => $patronidnumber } );
108 $patron = Koha::Patrons->find( { cardnumber => $patronidnumber } );
111 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
112 my $borrower = $patron->unblessed;
113 my $attributes = $patron->extended_attributes;
114 $borrower->{patron_attributes} = $attributes->as_list;
115 $borrower->{patron_attributes_count} = $attributes->count;
116 $max_nb_attr = $borrower->{patron_attributes_count} if $borrower->{patron_attributes_count} > $max_nb_attr;
117 push @borrowers, $borrower;
119 push @notfoundcardnumbers, $patronidnumber;
122 push @notfoundcardnumbers, $patronidnumber;
126 # Just for a correct display
127 for my $borrower ( @borrowers ) {
128 my $length = $borrower->{patron_attributes_count};
129 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
132 # Construct the patron attributes list
133 my @patron_attributes_values;
134 my @patron_attributes_codes;
135 my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
136 my $patron_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
137 my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']})->as_list;
138 while ( my $attr_type = $patron_attribute_types->next ) {
139 next if $attr_type->unique_id; # Don't display patron attributes that must be unqiue
140 my $options = $attr_type->authorised_value_category
141 ? GetAuthorisedValues( $attr_type->authorised_value_category )
143 push @patron_attributes_values,
145 attribute_code => $attr_type->code,
149 my $category_code = $attr_type->category_code;
150 my ( $category_lib ) = map {
151 ( defined $category_code and $attr_type->category_code eq $category_code ) ? $attr_type->description : ()
152 } @patron_categories;
153 push @patron_attributes_codes,
155 attribute_code => $attr_type->code,
156 attribute_lib => $attr_type->description,
157 category_lib => $category_lib,
158 type => $attr_type->authorised_value_category ? 'select' : 'text',
162 my @attributes_header = ();
163 for ( 1 .. scalar( $max_nb_attr ) ) {
164 push @attributes_header, { attribute => "Attributes $_" };
166 $template->param( borrowers => \@borrowers );
167 $template->param( attributes_header => \@attributes_header );
168 @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
169 $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
170 if @notfoundcardnumbers;
171 $template->param( useborrowernumbers => $useborrowernumbers );
173 # Construct drop-down list values
174 my $branches = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
176 push @branches_option, { value => $_->{branchcode}, lib => $_->{branchname} } for @$branches;
177 unshift @branches_option, { value => "", lib => "" };
178 my @categories_option;
179 push @categories_option, { value => $_->categorycode, lib => $_->description } for @patron_categories;
180 unshift @categories_option, { value => "", lib => "" };
181 my $bsort1 = GetAuthorisedValues("Bsort1");
183 push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
184 unshift @sort1_option, { value => "", lib => "" }
186 my $bsort2 = GetAuthorisedValues("Bsort2");
188 push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
189 unshift @sort2_option, { value => "", lib => "" }
192 my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
198 mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
204 mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
208 name => "branchcode",
210 option => \@branches_option,
211 mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
215 name => "categorycode",
217 option => \@categories_option,
218 mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
222 name => "streetnumber",
224 mandatory => ( grep /streetnumber/, @mandatoryFields ) ? 1 : 0,
230 mandatory => ( grep /address/, @mandatoryFields ) ? 1 : 0,
236 mandatory => ( grep /address2/, @mandatoryFields ) ? 1 : 0,
242 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
248 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
254 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
260 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
266 mandatory => ( grep /email/, @mandatoryFields ) ? 1 : 0,
272 mandatory => ( grep /phone/, @mandatoryFields ) ? 1 : 0,
278 mandatory => ( grep /mobile/, @mandatoryFields ) ? 1 : 0,
283 type => @sort1_option ? "select" : "text",
284 option => \@sort1_option,
285 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
290 type => @sort2_option ? "select" : "text",
291 option => \@sort2_option,
292 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
296 name => "dateenrolled",
298 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
302 name => "dateexpiry",
304 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
308 name => "borrowernotes",
310 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
316 mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
322 mandatory => ( grep /debarred/, @mandatoryFields ) ? 1 : 0,
326 name => "debarredcomment",
328 mandatory => ( grep /debarredcomment/, @mandatoryFields ) ? 1 : 0,
332 push @fields, { name => "password_expiration_date", type => "date" } if $logged_in_user->is_superlibrarian;
334 $template->param('patron_attributes_codes', \@patron_attributes_codes);
335 $template->param('patron_attributes_values', \@patron_attributes_values);
337 $template->param( fields => \@fields );
340 # Process modifications
343 my @disabled = $input->multi_param('disable_input');
345 for my $field ( qw/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry password_expiration_date borrowernotes opacnote debarred debarredcomment/ ) {
346 my $value = $input->param($field);
347 $infos->{$field} = $value if $value;
348 $infos->{$field} = "" if grep { $_ eq $field } @disabled;
351 for my $field ( qw( dateenrolled dateexpiry debarred password_expiration_date ) ) {
352 $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
355 delete $infos->{password_expiration_date} unless $logged_in_user->is_superlibrarian;
358 my @borrowernumbers = $input->multi_param('borrowernumber');
359 # For each borrower selected
360 for my $borrowernumber ( @borrowernumbers ) {
362 # If at least one field are filled, we want to modify the borrower
363 if ( defined $infos ) {
364 # If a debarred date or debarred comment has been submitted make a new debarment
365 if ( $infos->{debarred} || $infos->{debarredcomment} ) {
368 borrowernumber => $borrowernumber,
370 comment => $infos->{debarredcomment},
371 expiration => $infos->{debarred},
375 # If debarment date or debarment comment are disabled then remove all debarrments
376 my $patron = Koha::Patrons->find( $borrowernumber );
377 if ( grep { /debarred/ } @disabled ) {
379 my $debarrments = $patron->restrictions;
380 while( my $debarment = $debarrments->next ) {
381 DelDebarment( $debarment->borrower_debarment_id );
386 $infos->{borrowernumber} = $borrowernumber;
387 eval { $patron->set($infos)->store; };
388 if ( $@ ) { # FIXME We could provide better error handling here
389 $infos->{cardnumber} = $patron ? $patron->cardnumber || '' : '';
390 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
394 my $patron = Koha::Patrons->find( $borrowernumber );
395 my @attributes = $input->multi_param('patron_attributes');
396 my @attr_values = $input->multi_param('patron_attributes_value');
399 for my $code ( @attributes ) {
400 push @{ $attributes->{$code}->{values} }, shift @attr_values; # Handling repeatables
401 $attributes->{$code}->{disabled} = grep { $_ eq sprintf("attr%s_value", ++$i) } @disabled;
404 for my $code ( keys %$attributes ) {
405 my $attr_type = Koha::Patron::Attribute::Types->find($code);
406 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
407 next if $attr_type->category_code and $attr_type->category_code ne $patron->categorycode;
409 if ( $attributes->{$code}->{disabled} ) {
410 # The attribute is disabled, we remove it for this borrower !
412 $patron->get_extended_attribute($code)->delete;
414 push @errors, { error => $@ } if $@;
417 $patron->extended_attributes->search({'me.code' => $code})->filter_by_branch_limitations->delete;
418 $patron->add_extended_attribute({ code => $code, attribute => $_ }) for @{$attributes->{$code}->{values}};
420 push @errors, { error => $@ } if $@;
424 $op = "show_results"; # We have process modifications, the user want to view its
426 # Construct the results list
429 for my $borrowernumber ( @borrowernumbers ) {
430 my $patron = Koha::Patrons->find( $borrowernumber );
432 my $category_description = $patron->category->description;
433 my $borrower = $patron->unblessed;
434 $borrower->{category_description} = $category_description;
435 my $attributes = $patron->extended_attributes;
436 $borrower->{patron_attributes} = $attributes->as_list;
437 $max_nb_attr = $attributes->count if $attributes->count > $max_nb_attr;
438 push @borrowers, $borrower;
441 my @patron_attributes_option;
442 for my $borrower ( @borrowers ) {
443 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
444 my $length = scalar( @{ $borrower->{patron_attributes} } );
445 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
448 my @attributes_header = ();
449 for ( 1 .. scalar( $max_nb_attr ) ) {
450 push @attributes_header, { attribute => "Attributes $_" };
453 $template->param( borrowers => \@borrowers );
454 $template->param( attributes_header => \@attributes_header );
456 $template->param( errors => \@errors );
459 $template->param( patron_lists => [ GetPatronLists() ] );
465 output_html_with_http_headers $input, $cookie, $template->output;