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.
33 use C4::Members::Attributes;
34 use C4::Members::AttributeTypes qw/GetAttributeTypes_hashref/;
36 use List::MoreUtils qw /any uniq/;
37 use Koha::DateUtils qw( dt_from_string );
38 use Koha::List::Patron;
40 use Koha::Patron::Categories;
41 use Koha::Patron::Debarments;
45 my $op = $input->param('op') || 'show_form';
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 { template_name => "tools/modborrowers.tt",
51 flagsrequired => { tools => "edit_patrons" },
55 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
57 my %cookies = parse CGI::Cookie($cookie);
58 my $sessionID = $cookies{'CGISESSID'}->value;
59 my $dbh = C4::Context->dbh;
61 # Show borrower informations
62 if ( $op eq 'show' ) {
63 my $filefh = $input->upload('uploadfile');
64 my $filecontent = $input->param('filecontent');
65 my $patron_list_id = $input->param('patron_list_id');
68 my ( @notfoundcardnumbers, @from_another_group_of_libraries );
70 # Get cardnumbers from a file or the input area
73 while ( my $content = <$filefh> ) {
74 $content =~ s/[\r\n]*$//g;
75 push @cardnumbers, $content if $content;
77 } elsif ( $patron_list_id ) {
78 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
81 $list->patron_list_patrons()->search_related('borrowernumber')
82 ->get_column('cardnumber')->all();
85 if ( my $list = $input->param('cardnumberlist') ) {
86 push @cardnumbers, split( /\s\n/, $list );
91 for my $cardnumber ( @cardnumbers ) {
92 my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
94 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
95 my $borrower = $patron->unblessed;
96 my $attributes = $patron->get_extended_attributes;
97 $borrower->{patron_attributes} = $attributes->as_list;
98 $borrower->{patron_attributes_count} = $attributes->count;
99 $max_nb_attr = $borrower->{patron_attributes_count} if $borrower->{patron_attributes_count} > $max_nb_attr;
100 push @borrowers, $borrower;
102 push @notfoundcardnumbers, $cardnumber;
105 push @notfoundcardnumbers, $cardnumber;
109 # Just for a correct display
110 for my $borrower ( @borrowers ) {
111 my $length = $borrower->{patron_attributes_count};
112 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
115 # Construct the patron attributes list
116 my @patron_attributes_values;
117 my @patron_attributes_codes;
118 my $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
119 my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
120 for ( values %$patron_attribute_types ) {
121 my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
122 # TODO Repeatable attributes are not correctly managed and can cause data lost.
123 # This should be implemented.
124 next if $attr_type->{repeatable};
125 next if $attr_type->{unique_id}; # Don't display patron attributes that must be unqiue
126 my $options = $attr_type->authorised_value_category
127 ? GetAuthorisedValues( $attr_type->authorised_value_category )
129 push @patron_attributes_values,
131 attribute_code => $_->{code},
135 my $category_code = $_->{category_code};
136 my ( $category_lib ) = map {
137 ( defined $category_code and $_->categorycode eq $category_code ) ? $_->description : ()
138 } @patron_categories;
139 push @patron_attributes_codes,
141 attribute_code => $_->{code},
142 attribute_lib => $_->{description},
143 category_lib => $category_lib,
144 type => $attr_type->authorised_value_category ? 'select' : 'text',
148 my @attributes_header = ();
149 for ( 1 .. scalar( $max_nb_attr ) ) {
150 push @attributes_header, { attribute => "Attributes $_" };
152 $template->param( borrowers => \@borrowers );
153 $template->param( attributes_header => \@attributes_header );
154 @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
155 $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
156 if @notfoundcardnumbers;
158 # Construct drop-down list values
159 my $branches = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
161 push @branches_option, { value => $_->{branchcode}, lib => $_->{branchname} } for @$branches;
162 unshift @branches_option, { value => "", lib => "" };
163 my @categories_option;
164 push @categories_option, { value => $_->categorycode, lib => $_->description } for @patron_categories;
165 unshift @categories_option, { value => "", lib => "" };
166 my $bsort1 = GetAuthorisedValues("Bsort1");
168 push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
169 unshift @sort1_option, { value => "", lib => "" }
171 my $bsort2 = GetAuthorisedValues("Bsort2");
173 push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
174 unshift @sort2_option, { value => "", lib => "" }
177 my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
183 mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
189 mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
193 name => "branchcode",
195 option => \@branches_option,
196 mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
200 name => "categorycode",
202 option => \@categories_option,
203 mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
207 name => "streetnumber",
209 mandatory => ( grep /streetnumber/, @mandatoryFields ) ? 1 : 0,
215 mandatory => ( grep /address/, @mandatoryFields ) ? 1 : 0,
221 mandatory => ( grep /address2/, @mandatoryFields ) ? 1 : 0,
227 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
233 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
239 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
245 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
251 mandatory => ( grep /email/, @mandatoryFields ) ? 1 : 0,
257 mandatory => ( grep /phone/, @mandatoryFields ) ? 1 : 0,
263 mandatory => ( grep /mobile/, @mandatoryFields ) ? 1 : 0,
268 type => @sort1_option ? "select" : "text",
269 option => \@sort1_option,
270 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
275 type => @sort2_option ? "select" : "text",
276 option => \@sort2_option,
277 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
281 name => "dateenrolled",
283 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
287 name => "dateexpiry",
289 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
293 name => "borrowernotes",
295 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
301 mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
307 mandatory => ( grep /debarred/, @mandatoryFields ) ? 1 : 0,
311 name => "debarredcomment",
313 mandatory => ( grep /debarredcomment/, @mandatoryFields ) ? 1 : 0,
317 $template->param('patron_attributes_codes', \@patron_attributes_codes);
318 $template->param('patron_attributes_values', \@patron_attributes_values);
320 $template->param( fields => \@fields );
323 # Process modifications
326 my @disabled = $input->multi_param('disable_input');
328 for my $field ( qw/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote/ ) {
329 my $value = $input->param($field);
330 $infos->{$field} = $value if $value;
331 $infos->{$field} = "" if grep { $_ eq $field } @disabled;
334 for my $field ( qw( dateenrolled dateexpiry debarred ) ) {
335 $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
338 my @attributes = $input->multi_param('patron_attributes');
339 my @attr_values = $input->multi_param('patron_attributes_value');
342 my @borrowernumbers = $input->multi_param('borrowernumber');
343 # For each borrower selected
344 for my $borrowernumber ( @borrowernumbers ) {
345 # If at least one field are filled, we want to modify the borrower
346 if ( defined $infos ) {
347 # If a debarred date or debarred comment has been submitted make a new debarment
348 if ( $infos->{debarred} || $infos->{debarredcomment} ) {
351 borrowernumber => $borrowernumber,
353 comment => $infos->{debarredcomment},
354 expiration => $infos->{debarred},
358 # If debarment date or debarment comment are disabled then remove all debarrments
359 if ( grep { /debarred/ } @disabled ) {
361 my $debarrments = GetDebarments( { borrowernumber => $borrowernumber } );
362 foreach my $debarment (@$debarrments) {
363 DelDebarment( $debarment->{'borrower_debarment_id'} );
368 $infos->{borrowernumber} = $borrowernumber;
369 eval { Koha::Patrons->find( $borrowernumber )->set($infos)->store; };
370 if ( $@ ) { # FIXME We could provide better error handling here
371 my $patron = Koha::Patrons->find( $borrowernumber );
372 $infos->{cardnumber} = $patron ? $patron->cardnumber || '' : '';
373 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
377 my $patron = Koha::Patrons->find( $borrowernumber );
379 for ( @attributes ) {
382 $attribute->{code} = $_;
383 $attribute->{attribute} = $attr_values[$i];
384 my $attr_type = C4::Members::AttributeTypes->fetch( $_ );
385 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
386 ++$i and next if $attr_type->{category_code} and $attr_type->{category_code} ne $patron->category_code;
387 my $valuename = "attr" . $i . "_value";
388 if ( grep { $_ eq $valuename } @disabled ) {
389 # The attribute is disabled, we remove it for this borrower !
391 $patron->get_extended_attribute($attribute->{code})->delete;
393 push @errors, { error => $@ } if $@;
396 C4::Members::Attributes::UpdateBorrowerAttribute( $borrowernumber, $attribute );
398 push @errors, { error => $@ } if $@;
403 $op = "show_results"; # We have process modifications, the user want to view its
405 # Construct the results list
408 for my $borrowernumber ( @borrowernumbers ) {
409 my $patron = Koha::Patrons->find( $borrowernumber );
411 my $category_description = $patron->category->description;
412 my $borrower = $patron->unblessed;
413 $borrower->{category_description} = $category_description;
414 my $attributes = $patron->get_extended_attributes;
415 $borrower->{patron_attributes} = $attributes->as_list;
416 $max_nb_attr = $attributes->count if $attributes->count > $max_nb_attr;
417 push @borrowers, $borrower;
420 my @patron_attributes_option;
421 for my $borrower ( @borrowers ) {
422 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
423 my $length = scalar( @{ $borrower->{patron_attributes} } );
424 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
427 my @attributes_header = ();
428 for ( 1 .. scalar( $max_nb_attr ) ) {
429 push @attributes_header, { attribute => "Attributes $_" };
432 $template->param( borrowers => \@borrowers );
433 $template->param( attributes_header => \@attributes_header );
435 $template->param( errors => \@errors );
438 $template->param( patron_lists => [ GetPatronLists() ] );
444 output_html_with_http_headers $input, $cookie, $template->output;