Bug 20581: (follow-up) Fix fallback status_alias
[koha.git] / tools / modborrowers.pl
blobfb672a631de8a3f46a5c0a47cd6865865bfdf7d8
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::Members::Attributes;
34 use C4::Members::AttributeTypes qw/GetAttributeTypes_hashref/;
35 use C4::Output;
36 use List::MoreUtils qw /any uniq/;
37 use Koha::DateUtils qw( dt_from_string );
38 use Koha::List::Patron;
39 use Koha::Libraries;
40 use Koha::Patron::Categories;
41 use Koha::Patrons;
43 my $input = new CGI;
44 my $op = $input->param('op') || 'show_form';
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46 { template_name => "tools/modborrowers.tt",
47 query => $input,
48 type => "intranet",
49 authnotrequired => 0,
50 flagsrequired => { tools => "edit_patrons" },
54 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
56 my %cookies = parse CGI::Cookie($cookie);
57 my $sessionID = $cookies{'CGISESSID'}->value;
58 my $dbh = C4::Context->dbh;
60 # Show borrower informations
61 if ( $op eq 'show' ) {
62 my $filefh = $input->upload('uploadfile');
63 my $filecontent = $input->param('filecontent');
64 my $patron_list_id = $input->param('patron_list_id');
65 my @borrowers;
66 my @cardnumbers;
67 my ( @notfoundcardnumbers, @from_another_group_of_libraries );
69 # Get cardnumbers from a file or the input area
70 my @contentlist;
71 if ($filefh) {
72 while ( my $content = <$filefh> ) {
73 $content =~ s/[\r\n]*$//g;
74 push @cardnumbers, $content if $content;
76 } elsif ( $patron_list_id ) {
77 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
79 @cardnumbers =
80 $list->patron_list_patrons()->search_related('borrowernumber')
81 ->get_column('cardnumber')->all();
83 } else {
84 if ( my $list = $input->param('cardnumberlist') ) {
85 push @cardnumbers, split( /\s\n/, $list );
89 my $max_nb_attr = 0;
90 for my $cardnumber ( @cardnumbers ) {
91 my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
92 if ( $patron ) {
93 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
94 $patron = $patron->unblessed;
95 $patron->{patron_attributes} = C4::Members::Attributes::GetBorrowerAttributes( $patron->{borrowernumber} );
96 $max_nb_attr = scalar( @{ $patron->{patron_attributes} } )
97 if scalar( @{ $patron->{patron_attributes} } ) > $max_nb_attr;
98 push @borrowers, $patron;
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 = scalar( @{ $borrower->{patron_attributes} } );
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 $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
117 my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
118 for ( values %$patron_attribute_types ) {
119 my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
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 => $_->{code},
130 options => $options,
133 my $category_code = $_->{category_code};
134 my ( $category_lib ) = map {
135 ( defined $category_code and $_->categorycode eq $category_code ) ? $_->description : ()
136 } @patron_categories;
137 push @patron_attributes_codes,
139 attribute_code => $_->{code},
140 attribute_lib => $_->{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 => "city",
206 type => "text",
207 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
211 name => "state",
212 type => "text",
213 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
217 name => "zipcode",
218 type => "text",
219 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
223 name => "country",
224 type => "text",
225 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
229 name => "sort1",
230 type => @sort1_option ? "select" : "text",
231 option => \@sort1_option,
232 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
236 name => "sort2",
237 type => @sort2_option ? "select" : "text",
238 option => \@sort2_option,
239 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
243 name => "dateenrolled",
244 type => "date",
245 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
249 name => "dateexpiry",
250 type => "date",
251 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
255 name => "borrowernotes",
256 type => "text",
257 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
261 name => "opacnote",
262 type => "text",
263 mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
267 $template->param('patron_attributes_codes', \@patron_attributes_codes);
268 $template->param('patron_attributes_values', \@patron_attributes_values);
270 $template->param( fields => \@fields );
273 # Process modifications
274 if ( $op eq 'do' ) {
276 my @disabled = $input->multi_param('disable_input');
277 my $infos;
278 for my $field ( qw/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote/ ) {
279 my $value = $input->param($field);
280 $infos->{$field} = $value if $value;
281 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
284 for my $field ( qw( dateenrolled dateexpiry ) ) {
285 $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
288 my @attributes = $input->multi_param('patron_attributes');
289 my @attr_values = $input->multi_param('patron_attributes_value');
291 my @errors;
292 my @borrowernumbers = $input->multi_param('borrowernumber');
293 # For each borrower selected
294 for my $borrowernumber ( @borrowernumbers ) {
295 # If at least one field are filled, we want to modify the borrower
296 if ( defined $infos ) {
297 $infos->{borrowernumber} = $borrowernumber;
298 eval { Koha::Patrons->find( $borrowernumber )->set($infos)->store; };
299 if ( $@ ) { # FIXME We could provide better error handling here
300 my $patron = Koha::Patrons->find( $borrowernumber );
301 $infos->{cardnumber} = $patron ? $patron->cardnumber || '' : '';
302 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
306 my $borrower_categorycode = Koha::Patrons->find( $borrowernumber )->categorycode;
307 my $i=0;
308 for ( @attributes ) {
309 my $attribute;
310 $attribute->{code} = $_;
311 $attribute->{attribute} = $attr_values[$i];
312 my $attr_type = C4::Members::AttributeTypes->fetch( $_ );
313 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
314 ++$i and next if $attr_type->{category_code} and $attr_type->{category_code} ne $borrower_categorycode;
315 my $valuename = "attr" . $i . "_value";
316 if ( grep { /^$valuename$/ } @disabled ) {
317 # The attribute is disabled, we remove it for this borrower !
318 eval {
319 C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
321 push @errors, { error => $@ } if $@;
322 } else {
323 eval {
324 C4::Members::Attributes::UpdateBorrowerAttribute( $borrowernumber, $attribute );
326 push @errors, { error => $@ } if $@;
328 $i++;
331 $op = "show_results"; # We have process modifications, the user want to view its
333 # Construct the results list
334 my @borrowers;
335 my $max_nb_attr = 0;
336 for my $borrowernumber ( @borrowernumbers ) {
337 my $patron = Koha::Patrons->find( $borrowernumber );
338 if ( $patron ) {
339 $patron = $patron->unblessed;
340 $patron->{patron_attributes} = C4::Members::Attributes::GetBorrowerAttributes( $patron->{borrowernumber} );
341 $max_nb_attr = scalar( @{ $patron->{patron_attributes} } )
342 if scalar( @{ $patron->{patron_attributes} } ) > $max_nb_attr;
343 push @borrowers, $patron;
346 my @patron_attributes_option;
347 for my $borrower ( @borrowers ) {
348 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
349 my $length = scalar( @{ $borrower->{patron_attributes} } );
350 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
353 my @attributes_header = ();
354 for ( 1 .. scalar( $max_nb_attr ) ) {
355 push @attributes_header, { attribute => "Attributes $_" };
358 $template->param( borrowers => \@borrowers );
359 $template->param( attributes_header => \@attributes_header );
361 $template->param( errors => \@errors );
362 } else {
364 $template->param( patron_lists => [ GetPatronLists() ] );
367 $template->param(
368 op => $op,
370 output_html_with_http_headers $input, $cookie, $template->output;
371 exit;