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",
48 flagsrequired
=> { tools
=> "edit_patrons" },
52 my $logged_in_user = Koha
::Patrons
->find( $loggedinuser );
54 my %cookies = parse CGI
::Cookie
($cookie);
55 my $sessionID = $cookies{'CGISESSID'}->value;
56 my $dbh = C4
::Context
->dbh;
58 # Show borrower informations
59 if ( $op eq 'show' ) {
60 my $filefh = $input->upload('uploadfile');
61 my $filecontent = $input->param('filecontent');
62 my $patron_list_id = $input->param('patron_list_id');
65 my @notfoundcardnumbers;
67 # Get cardnumbers from a file or the input area
69 while ( my $content = <$filefh> ) {
70 $content =~ s/[\r\n]*$//g;
71 push @cardnumbers, $content if $content;
73 } elsif ( $patron_list_id ) {
74 my ($list) = GetPatronLists
( { patron_list_id
=> $patron_list_id } );
77 $list->patron_list_patrons()->search_related('borrowernumber')
78 ->get_column('cardnumber')->all();
81 if ( my $list = $input->param('cardnumberlist') ) {
82 push @cardnumbers, split( /\s\n/, $list );
87 for my $cardnumber ( @cardnumbers ) {
88 my $patron = Koha
::Patrons
->find( { cardnumber
=> $cardnumber } );
90 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
91 my $borrower = $patron->unblessed;
92 my $attributes = $patron->extended_attributes;
93 $borrower->{patron_attributes
} = $attributes->as_list;
94 $borrower->{patron_attributes_count
} = $attributes->count;
95 $max_nb_attr = $borrower->{patron_attributes_count
} if $borrower->{patron_attributes_count
} > $max_nb_attr;
96 push @borrowers, $borrower;
98 push @notfoundcardnumbers, $cardnumber;
101 push @notfoundcardnumbers, $cardnumber;
105 # Just for a correct display
106 for my $borrower ( @borrowers ) {
107 my $length = $borrower->{patron_attributes_count
};
108 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
111 # Construct the patron attributes list
112 my @patron_attributes_values;
113 my @patron_attributes_codes;
114 my $library_id = C4
::Context
->userenv ? C4
::Context
->userenv->{'branch'} : undef;
115 my $patron_attribute_types = Koha
::Patron
::Attribute
::Types
->search_with_library_limits({}, {}, $library_id);
116 my @patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['description']});
117 while ( my $attr_type = $patron_attribute_types->next ) {
118 # TODO Repeatable attributes are not correctly managed and can cause data lost.
119 # This should be implemented.
120 next if $attr_type->repeatable;
121 next if $attr_type->unique_id; # Don't display patron attributes that must be unqiue
122 my $options = $attr_type->authorised_value_category
123 ? GetAuthorisedValues
( $attr_type->authorised_value_category )
125 push @patron_attributes_values,
127 attribute_code
=> $attr_type->code,
131 my $category_code = $attr_type->category_code;
132 my ( $category_lib ) = map {
133 ( defined $category_code and $attr_type->category_code eq $category_code ) ?
$attr_type->description : ()
134 } @patron_categories;
135 push @patron_attributes_codes,
137 attribute_code
=> $attr_type->code,
138 attribute_lib
=> $attr_type->description,
139 category_lib
=> $category_lib,
140 type
=> $attr_type->authorised_value_category ?
'select' : 'text',
144 my @attributes_header = ();
145 for ( 1 .. scalar( $max_nb_attr ) ) {
146 push @attributes_header, { attribute
=> "Attributes $_" };
148 $template->param( borrowers
=> \
@borrowers );
149 $template->param( attributes_header
=> \
@attributes_header );
150 @notfoundcardnumbers = map { { cardnumber
=> $_ } } @notfoundcardnumbers;
151 $template->param( notfoundcardnumbers
=> \
@notfoundcardnumbers )
152 if @notfoundcardnumbers;
154 # Construct drop-down list values
155 my $branches = Koha
::Libraries
->search({}, { order_by
=> ['branchname'] })->unblessed;
157 push @branches_option, { value
=> $_->{branchcode
}, lib
=> $_->{branchname
} } for @
$branches;
158 unshift @branches_option, { value
=> "", lib
=> "" };
159 my @categories_option;
160 push @categories_option, { value
=> $_->categorycode, lib
=> $_->description } for @patron_categories;
161 unshift @categories_option, { value
=> "", lib
=> "" };
162 my $bsort1 = GetAuthorisedValues
("Bsort1");
164 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
165 unshift @sort1_option, { value
=> "", lib
=> "" }
167 my $bsort2 = GetAuthorisedValues
("Bsort2");
169 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
170 unshift @sort2_option, { value
=> "", lib
=> "" }
173 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
179 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
185 mandatory
=> ( grep /firstname/, @mandatoryFields ) ?
1 : 0,
189 name
=> "branchcode",
191 option
=> \
@branches_option,
192 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
196 name
=> "categorycode",
198 option
=> \
@categories_option,
199 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
203 name
=> "streetnumber",
205 mandatory
=> ( grep /streetnumber/, @mandatoryFields ) ?
1 : 0,
211 mandatory
=> ( grep /address/, @mandatoryFields ) ?
1 : 0,
217 mandatory
=> ( grep /address2/, @mandatoryFields ) ?
1 : 0,
223 mandatory
=> ( grep /city/, @mandatoryFields ) ?
1 : 0,
229 mandatory
=> ( grep /state/, @mandatoryFields ) ?
1 : 0,
235 mandatory
=> ( grep /zipcode/, @mandatoryFields ) ?
1 : 0,
241 mandatory
=> ( grep /country/, @mandatoryFields ) ?
1 : 0,
247 mandatory
=> ( grep /email/, @mandatoryFields ) ?
1 : 0,
253 mandatory
=> ( grep /phone/, @mandatoryFields ) ?
1 : 0,
259 mandatory
=> ( grep /mobile/, @mandatoryFields ) ?
1 : 0,
264 type
=> @sort1_option ?
"select" : "text",
265 option
=> \
@sort1_option,
266 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
271 type
=> @sort2_option ?
"select" : "text",
272 option
=> \
@sort2_option,
273 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
277 name
=> "dateenrolled",
279 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
283 name
=> "dateexpiry",
285 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
289 name
=> "borrowernotes",
291 mandatory
=> ( grep /borrowernotes/, @mandatoryFields ) ?
1 : 0,
297 mandatory
=> ( grep /opacnote/, @mandatoryFields ) ?
1 : 0,
303 mandatory
=> ( grep /debarred/, @mandatoryFields ) ?
1 : 0,
307 name
=> "debarredcomment",
309 mandatory
=> ( grep /debarredcomment/, @mandatoryFields ) ?
1 : 0,
313 $template->param('patron_attributes_codes', \
@patron_attributes_codes);
314 $template->param('patron_attributes_values', \
@patron_attributes_values);
316 $template->param( fields
=> \
@fields );
319 # Process modifications
322 my @disabled = $input->multi_param('disable_input');
324 for my $field ( qw
/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote/ ) {
325 my $value = $input->param($field);
326 $infos->{$field} = $value if $value;
327 $infos->{$field} = "" if grep { $_ eq $field } @disabled;
330 for my $field ( qw( dateenrolled dateexpiry debarred ) ) {
331 $infos->{$field} = dt_from_string
($infos->{$field}) if $infos->{$field};
334 my @attributes = $input->multi_param('patron_attributes');
335 my @attr_values = $input->multi_param('patron_attributes_value');
338 my @borrowernumbers = $input->multi_param('borrowernumber');
339 # For each borrower selected
340 for my $borrowernumber ( @borrowernumbers ) {
341 # If at least one field are filled, we want to modify the borrower
342 if ( defined $infos ) {
343 # If a debarred date or debarred comment has been submitted make a new debarment
344 if ( $infos->{debarred
} || $infos->{debarredcomment
} ) {
347 borrowernumber
=> $borrowernumber,
349 comment
=> $infos->{debarredcomment
},
350 expiration
=> $infos->{debarred
},
354 # If debarment date or debarment comment are disabled then remove all debarrments
355 if ( grep { /debarred/ } @disabled ) {
357 my $debarrments = GetDebarments
( { borrowernumber
=> $borrowernumber } );
358 foreach my $debarment (@
$debarrments) {
359 DelDebarment
( $debarment->{'borrower_debarment_id'} );
364 $infos->{borrowernumber
} = $borrowernumber;
365 eval { Koha
::Patrons
->find( $borrowernumber )->set($infos)->store; };
366 if ( $@
) { # FIXME We could provide better error handling here
367 my $patron = Koha
::Patrons
->find( $borrowernumber );
368 $infos->{cardnumber
} = $patron ?
$patron->cardnumber || '' : '';
369 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
}, cardnumber
=> $infos->{cardnumber
} };
373 my $patron = Koha
::Patrons
->find( $borrowernumber );
375 for ( @attributes ) {
378 $attribute->{code
} = $_;
379 $attribute->{attribute
} = $attr_values[$i];
380 my $attr_type = Koha
::Patron
::Attribute
::Types
->find($_);
381 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
382 ++$i and next if $attr_type->category_code and $attr_type->category_code ne $patron->categorycode;
383 my $valuename = "attr" . $i . "_value";
384 if ( grep { $_ eq $valuename } @disabled ) {
385 # The attribute is disabled, we remove it for this borrower !
387 $patron->get_extended_attribute($attribute->{code
})->delete;
389 push @errors, { error
=> $@
} if $@
;
393 # We should not need to filter by branch, but stay on the safe side
394 # Repeatable are not supported so we can do that - TODO
395 $patron->extended_attributes->search({'me.code' => $attribute->{code
}})->filter_by_branch_limitations->delete;
396 $patron->add_extended_attribute($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->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;