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.
34 use List
::MoreUtils qw
/any uniq/;
35 use Koha
::DateUtils
qw( dt_from_string );
36 use Koha
::List
::Patron
;
38 use Koha
::Patron
::Categories
;
39 use Koha
::Patron
::Debarments
;
43 my $op = $input->param('op') || 'show_form';
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
45 { template_name
=> "tools/modborrowers.tt",
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');
66 my @notfoundcardnumbers;
68 # Get cardnumbers from a file or the input area
70 while ( my $content = <$filefh> ) {
71 $content =~ s/[\r\n]*$//g;
72 push @cardnumbers, $content if $content;
74 } elsif ( $patron_list_id ) {
75 my ($list) = GetPatronLists
( { patron_list_id
=> $patron_list_id } );
78 $list->patron_list_patrons()->search_related('borrowernumber')
79 ->get_column('cardnumber')->all();
82 if ( my $list = $input->param('cardnumberlist') ) {
83 push @cardnumbers, split( /\s\n/, $list );
88 for my $cardnumber ( @cardnumbers ) {
89 my $patron = Koha
::Patrons
->find( { cardnumber
=> $cardnumber } );
91 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
92 my $borrower = $patron->unblessed;
93 my $attributes = $patron->extended_attributes;
94 $borrower->{patron_attributes
} = $attributes->as_list;
95 $borrower->{patron_attributes_count
} = $attributes->count;
96 $max_nb_attr = $borrower->{patron_attributes_count
} if $borrower->{patron_attributes_count
} > $max_nb_attr;
97 push @borrowers, $borrower;
99 push @notfoundcardnumbers, $cardnumber;
102 push @notfoundcardnumbers, $cardnumber;
106 # Just for a correct display
107 for my $borrower ( @borrowers ) {
108 my $length = $borrower->{patron_attributes_count
};
109 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
112 # Construct the patron attributes list
113 my @patron_attributes_values;
114 my @patron_attributes_codes;
115 my $library_id = C4
::Context
->userenv ? C4
::Context
->userenv->{'branch'} : undef;
116 my $patron_attribute_types = Koha
::Patron
::Attribute
::Types
->search_with_library_limits({}, {}, $library_id);
117 my @patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['description']});
118 while ( my $attr_type = $patron_attribute_types->next ) {
119 # TODO Repeatable attributes are not correctly managed and can cause data lost.
120 # This should be implemented.
121 next if $attr_type->repeatable;
122 next if $attr_type->unique_id; # Don't display patron attributes that must be unqiue
123 my $options = $attr_type->authorised_value_category
124 ? GetAuthorisedValues
( $attr_type->authorised_value_category )
126 push @patron_attributes_values,
128 attribute_code
=> $attr_type->code,
132 my $category_code = $attr_type->category_code;
133 my ( $category_lib ) = map {
134 ( defined $category_code and $attr_type->category_code eq $category_code ) ?
$attr_type->description : ()
135 } @patron_categories;
136 push @patron_attributes_codes,
138 attribute_code
=> $attr_type->code,
139 attribute_lib
=> $attr_type->description,
140 category_lib
=> $category_lib,
141 type
=> $attr_type->authorised_value_category ?
'select' : 'text',
145 my @attributes_header = ();
146 for ( 1 .. scalar( $max_nb_attr ) ) {
147 push @attributes_header, { attribute
=> "Attributes $_" };
149 $template->param( borrowers
=> \
@borrowers );
150 $template->param( attributes_header
=> \
@attributes_header );
151 @notfoundcardnumbers = map { { cardnumber
=> $_ } } @notfoundcardnumbers;
152 $template->param( notfoundcardnumbers
=> \
@notfoundcardnumbers )
153 if @notfoundcardnumbers;
155 # Construct drop-down list values
156 my $branches = Koha
::Libraries
->search({}, { order_by
=> ['branchname'] })->unblessed;
158 push @branches_option, { value
=> $_->{branchcode
}, lib
=> $_->{branchname
} } for @
$branches;
159 unshift @branches_option, { value
=> "", lib
=> "" };
160 my @categories_option;
161 push @categories_option, { value
=> $_->categorycode, lib
=> $_->description } for @patron_categories;
162 unshift @categories_option, { value
=> "", lib
=> "" };
163 my $bsort1 = GetAuthorisedValues
("Bsort1");
165 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
166 unshift @sort1_option, { value
=> "", lib
=> "" }
168 my $bsort2 = GetAuthorisedValues
("Bsort2");
170 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
171 unshift @sort2_option, { value
=> "", lib
=> "" }
174 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
180 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
186 mandatory
=> ( grep /firstname/, @mandatoryFields ) ?
1 : 0,
190 name
=> "branchcode",
192 option
=> \
@branches_option,
193 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
197 name
=> "categorycode",
199 option
=> \
@categories_option,
200 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
204 name
=> "streetnumber",
206 mandatory
=> ( grep /streetnumber/, @mandatoryFields ) ?
1 : 0,
212 mandatory
=> ( grep /address/, @mandatoryFields ) ?
1 : 0,
218 mandatory
=> ( grep /address2/, @mandatoryFields ) ?
1 : 0,
224 mandatory
=> ( grep /city/, @mandatoryFields ) ?
1 : 0,
230 mandatory
=> ( grep /state/, @mandatoryFields ) ?
1 : 0,
236 mandatory
=> ( grep /zipcode/, @mandatoryFields ) ?
1 : 0,
242 mandatory
=> ( grep /country/, @mandatoryFields ) ?
1 : 0,
248 mandatory
=> ( grep /email/, @mandatoryFields ) ?
1 : 0,
254 mandatory
=> ( grep /phone/, @mandatoryFields ) ?
1 : 0,
260 mandatory
=> ( grep /mobile/, @mandatoryFields ) ?
1 : 0,
265 type
=> @sort1_option ?
"select" : "text",
266 option
=> \
@sort1_option,
267 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
272 type
=> @sort2_option ?
"select" : "text",
273 option
=> \
@sort2_option,
274 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
278 name
=> "dateenrolled",
280 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
284 name
=> "dateexpiry",
286 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
290 name
=> "borrowernotes",
292 mandatory
=> ( grep /borrowernotes/, @mandatoryFields ) ?
1 : 0,
298 mandatory
=> ( grep /opacnote/, @mandatoryFields ) ?
1 : 0,
304 mandatory
=> ( grep /debarred/, @mandatoryFields ) ?
1 : 0,
308 name
=> "debarredcomment",
310 mandatory
=> ( grep /debarredcomment/, @mandatoryFields ) ?
1 : 0,
314 $template->param('patron_attributes_codes', \
@patron_attributes_codes);
315 $template->param('patron_attributes_values', \
@patron_attributes_values);
317 $template->param( fields
=> \
@fields );
320 # Process modifications
323 my @disabled = $input->multi_param('disable_input');
325 for my $field ( qw
/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote/ ) {
326 my $value = $input->param($field);
327 $infos->{$field} = $value if $value;
328 $infos->{$field} = "" if grep { $_ eq $field } @disabled;
331 for my $field ( qw( dateenrolled dateexpiry debarred ) ) {
332 $infos->{$field} = dt_from_string
($infos->{$field}) if $infos->{$field};
335 my @attributes = $input->multi_param('patron_attributes');
336 my @attr_values = $input->multi_param('patron_attributes_value');
339 my @borrowernumbers = $input->multi_param('borrowernumber');
340 # For each borrower selected
341 for my $borrowernumber ( @borrowernumbers ) {
342 # If at least one field are filled, we want to modify the borrower
343 if ( defined $infos ) {
344 # If a debarred date or debarred comment has been submitted make a new debarment
345 if ( $infos->{debarred
} || $infos->{debarredcomment
} ) {
348 borrowernumber
=> $borrowernumber,
350 comment
=> $infos->{debarredcomment
},
351 expiration
=> $infos->{debarred
},
355 # If debarment date or debarment comment are disabled then remove all debarrments
356 if ( grep { /debarred/ } @disabled ) {
358 my $debarrments = GetDebarments
( { borrowernumber
=> $borrowernumber } );
359 foreach my $debarment (@
$debarrments) {
360 DelDebarment
( $debarment->{'borrower_debarment_id'} );
365 $infos->{borrowernumber
} = $borrowernumber;
366 eval { Koha
::Patrons
->find( $borrowernumber )->set($infos)->store; };
367 if ( $@
) { # FIXME We could provide better error handling here
368 my $patron = Koha
::Patrons
->find( $borrowernumber );
369 $infos->{cardnumber
} = $patron ?
$patron->cardnumber || '' : '';
370 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
}, cardnumber
=> $infos->{cardnumber
} };
374 my $patron = Koha
::Patrons
->find( $borrowernumber );
376 for ( @attributes ) {
379 $attribute->{code
} = $_;
380 $attribute->{attribute
} = $attr_values[$i];
381 my $attr_type = Koha
::Patron
::Attribute
::Types
->find($_);
382 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
383 ++$i and next if $attr_type->category_code and $attr_type->category_code ne $patron->categorycode;
384 my $valuename = "attr" . $i . "_value";
385 if ( grep { $_ eq $valuename } @disabled ) {
386 # The attribute is disabled, we remove it for this borrower !
388 $patron->get_extended_attribute($attribute->{code
})->delete;
390 push @errors, { error
=> $@
} if $@
;
394 # We should not need to filter by branch, but stay on the safe side
395 # Repeatable are not supported so we can do that - TODO
396 $patron->extended_attributes->search({'me.code' => $attribute->{code
}})->filter_by_branch_limitations->delete;
397 $patron->add_extended_attribute($attribute);
399 push @errors, { error
=> $@
} if $@
;
404 $op = "show_results"; # We have process modifications, the user want to view its
406 # Construct the results list
409 for my $borrowernumber ( @borrowernumbers ) {
410 my $patron = Koha
::Patrons
->find( $borrowernumber );
412 my $category_description = $patron->category->description;
413 my $borrower = $patron->unblessed;
414 $borrower->{category_description
} = $category_description;
415 my $attributes = $patron->extended_attributes;
416 $borrower->{patron_attributes
} = $attributes->as_list;
417 $max_nb_attr = $attributes->count if $attributes->count > $max_nb_attr;
418 push @borrowers, $borrower;
421 my @patron_attributes_option;
422 for my $borrower ( @borrowers ) {
423 push @patron_attributes_option, { value
=> "$_->{code}", lib
=> $_->{code
} } for @
{ $borrower->{patron_attributes
} };
424 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
425 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
428 my @attributes_header = ();
429 for ( 1 .. scalar( $max_nb_attr ) ) {
430 push @attributes_header, { attribute
=> "Attributes $_" };
433 $template->param( borrowers
=> \
@borrowers );
434 $template->param( attributes_header
=> \
@attributes_header );
436 $template->param( errors
=> \
@errors );
439 $template->param( patron_lists
=> [ GetPatronLists
() ] );
445 output_html_with_http_headers
$input, $cookie, $template->output;