Bug 36908: (QA follow-up) Proposed improvement to prefernce description
[koha.git] / tools / modborrowers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 BibLibre
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 # modborrowers.pl
21 #
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.
27
28 use Modern::Perl;
29 use CGI qw ( -utf8 );
30 use C4::Auth qw( get_template_and_user );
31 use C4::Koha qw( GetAuthorisedValues );
32 use C4::Members;
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 );
36 use Koha::Libraries;
37 use Koha::Patron::Categories;
38 use Koha::Patron::Debarments qw( AddDebarment DelDebarment );
39 use Koha::Patrons;
40 use List::MoreUtils qw(uniq);
41
42 my $input = CGI->new;
43 my $op = $input->param('op') || 'show_form';
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {   template_name   => "tools/modborrowers.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { tools => "edit_patrons" },
49     }
50 );
51
52 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
53
54 $template->param( CanUpdatePasswordExpiration => 1 ) if $logged_in_user->is_superlibrarian;
55
56 my $dbh       = C4::Context->dbh;
57
58 # Show borrower informations
59 if ( $op eq 'cud-show' ) {
60     my @borrowers;
61     my @patronidnumbers;
62     my @notfoundcardnumbers;
63     my $useborrowernumbers = 0;
64
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 @cardnumbers = $input->multi_param('cardnumber') ) {
70         @patronidnumbers = @cardnumbers;
71     } elsif ( my $cardnumberuploadfile = $input->param('cardnumberuploadfile') ){
72         # User uploaded a file of card numbers
73         binmode $cardnumberuploadfile, ':encoding(UTF-8)';
74         while ( my $content = <$cardnumberuploadfile> ) {
75             next unless $content;
76             $content =~ s/[\r\n]*$//;
77             push @patronidnumbers, $content if $content;
78         }
79     } elsif ( my $borrowernumberlist = $input->param('borrowernumberlist') ){
80         # User submitted a list of borrowernumbers
81         $useborrowernumbers = 1;
82         push @patronidnumbers, split( /\s\n/, $borrowernumberlist );
83     } elsif ( my $borrowernumberuploadfile = $input->param('borrowernumberuploadfile') ){
84         # User uploaded a file of borrowernumbers
85         $useborrowernumbers = 1;
86         binmode $borrowernumberuploadfile, ':encoding(UTF-8)';
87         while ( my $content = <$borrowernumberuploadfile> ) {
88             next unless $content;
89             $content =~ s/[\r\n]*$//;
90             push @patronidnumbers, $content if $content;
91         }
92     } elsif ( my $patron_list_id = $input->param('patron_list_id') ){
93         # User selected a patron list
94         my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
95         @patronidnumbers =
96           $list->patron_list_patrons()->search_related('borrowernumber')
97           ->get_column('cardnumber')->all();
98     }
99
100     my $max_nb_attr = 0;
101
102     # Make sure there is only one of each patron id number
103     @patronidnumbers = uniq( @patronidnumbers );
104
105     for my $patronidnumber ( @patronidnumbers ) {
106         my $patron;
107         if( $useborrowernumbers == 1 ){
108             $patron = Koha::Patrons->find( { borrowernumber => $patronidnumber } );
109         } else {
110             $patron = Koha::Patrons->find( { cardnumber => $patronidnumber } );
111         }
112         if ( $patron ) {
113             if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
114                 my $borrower = $patron->unblessed;
115                 my $attributes = $patron->extended_attributes;
116                 $borrower->{patron_attributes} = $attributes->as_list;
117                 $borrower->{patron_attributes_count} = $attributes->count;
118                 $max_nb_attr = $borrower->{patron_attributes_count} if $borrower->{patron_attributes_count} > $max_nb_attr;
119                 push @borrowers, $borrower;
120             } else {
121                 push @notfoundcardnumbers, $patronidnumber;
122             }
123         } else {
124             push @notfoundcardnumbers, $patronidnumber;
125         }
126     }
127
128     # Just for a correct display
129     for my $borrower ( @borrowers ) {
130         my $length = $borrower->{patron_attributes_count};
131         push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
132     }
133
134     # Construct the patron attributes list
135     my @patron_attributes_values;
136     my @patron_attributes_codes;
137     my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
138     my $patron_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
139     my @patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']})->as_list;
140     while ( my $attr_type = $patron_attribute_types->next ) {
141         next if $attr_type->unique_id; # Don't display patron attributes that must be unqiue
142         my $options = $attr_type->authorised_value_category
143             ? GetAuthorisedValues( $attr_type->authorised_value_category )
144             : undef;
145         push @patron_attributes_values,
146             {
147                 attribute_code => $attr_type->code,
148                 options        => $options,
149             };
150
151         my $category_code = $attr_type->category_code;
152         my ($category_lib) =
153             map { ( defined $category_code and $attr_type->category_code eq $_->categorycode ) ? $_->description : () }
154             @patron_categories;
155         push @patron_attributes_codes,
156             {
157                 attribute_code => $attr_type->code,
158                 attribute_lib  => $attr_type->description,
159                 category_lib   => $category_lib,
160                 type           => $attr_type->authorised_value_category ? 'cud-select' : 'text',
161             };
162     }
163
164     my @attributes_header = ();
165     for ( 1 .. scalar( $max_nb_attr ) ) {
166         push @attributes_header, { attribute => "Attributes $_" };
167     }
168     $template->param( borrowers => \@borrowers );
169     $template->param( attributes_header => \@attributes_header );
170     @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
171     $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
172         if @notfoundcardnumbers;
173     $template->param( useborrowernumbers => $useborrowernumbers );
174
175     # Construct drop-down list values
176     my $branches = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
177     my @branches_option;
178     push @branches_option, { value => $_->{branchcode}, lib => $_->{branchname} } for @$branches;
179     unshift @branches_option, { value => "", lib => "" };
180     my @categories_option;
181     push @categories_option, { value => $_->categorycode, lib => $_->description } for @patron_categories;
182     unshift @categories_option, { value => "", lib => "" };
183     my $bsort1 = GetAuthorisedValues("Bsort1");
184     my @sort1_option;
185     push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
186     unshift @sort1_option, { value => "", lib => "" }
187         if @sort1_option;
188     my $bsort2 = GetAuthorisedValues("Bsort2");
189     my @sort2_option;
190     push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
191     unshift @sort2_option, { value => "", lib => "" }
192         if @sort2_option;
193
194     my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
195
196     my @fields = (
197         {
198             name => "surname",
199             type => "text",
200             mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
201         }
202         ,
203         {
204             name => "firstname",
205             type => "text",
206             mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
207         }
208         ,
209         {
210             name => "branchcode",
211             type => "select",
212             option => \@branches_option,
213             mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
214         }
215         ,
216         {
217             name => "categorycode",
218             type => "select",
219             option => \@categories_option,
220             mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
221         }
222         ,
223         {
224             name => "streetnumber",
225             type => "text",
226             mandatory => ( grep /streetnumber/, @mandatoryFields ) ? 1 : 0,
227         }
228         ,
229         {
230             name => "address",
231             type => "text",
232             mandatory => ( grep /address/, @mandatoryFields ) ? 1 : 0,
233         }
234         ,
235         {
236             name => "address2",
237             type => "text",
238             mandatory => ( grep /address2/, @mandatoryFields ) ? 1 : 0,
239         }
240         ,
241         {
242             name => "city",
243             type => "text",
244             mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
245         }
246         ,
247         {
248             name => "state",
249             type => "text",
250             mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
251         }
252         ,
253         {
254             name => "zipcode",
255             type => "text",
256             mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
257         }
258         ,
259         {
260             name => "country",
261             type => "text",
262             mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
263         }
264         ,
265         {
266             name => "email",
267             type => "text",
268             mandatory => ( grep /email/, @mandatoryFields ) ? 1 : 0,
269         }
270         ,
271         {
272             name => "phone",
273             type => "text",
274             mandatory => ( grep /phone/, @mandatoryFields ) ? 1 : 0,
275         }
276         ,
277         {
278             name => "mobile",
279             type => "text",
280             mandatory => ( grep /mobile/, @mandatoryFields ) ? 1 : 0,
281         }
282         ,
283         {
284             name      => "fax",
285             type      => "text",
286             mandatory => ( grep /fax/, @mandatoryFields ) ? 1 : 0,
287         }
288         ,
289         {
290             name => "sort1",
291             type => @sort1_option ? "select" : "text",
292             option => \@sort1_option,
293             mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
294         }
295         ,
296         {
297             name => "sort2",
298             type => @sort2_option ? "select" : "text",
299             option => \@sort2_option,
300             mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
301         }
302         ,
303         {
304             name => "dateenrolled",
305             type => "date",
306             mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
307         }
308         ,
309         {
310             name => "dateexpiry",
311             type => "date",
312             mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
313         }
314         ,
315         {
316             name => "borrowernotes",
317             type => "text",
318             mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
319         }
320         ,
321         {
322             name => "opacnote",
323             type => "text",
324             mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
325         }
326         ,
327         {
328             name => "debarred",
329             type => "date",
330             mandatory => ( grep /debarred/, @mandatoryFields ) ? 1 : 0,
331         }
332         ,
333         {
334             name => "debarredcomment",
335             type => "text",
336             mandatory => ( grep /debarredcomment/, @mandatoryFields ) ? 1 : 0,
337         },
338     );
339
340     push @fields, { name => "password_expiration_date", type => "date" } if $logged_in_user->is_superlibrarian;
341
342     $template->param('patron_attributes_codes', \@patron_attributes_codes);
343     $template->param('patron_attributes_values', \@patron_attributes_values);
344
345     $template->param( fields => \@fields );
346     $op = 'show';
347 }
348
349 # Process modifications
350 if ( $op eq 'cud-do' ) {
351
352     my @disabled = $input->multi_param('disable_input');
353     my $infos;
354     for my $field (
355         qw/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile fax sort1 sort2 dateenrolled dateexpiry password_expiration_date borrowernotes opacnote debarred debarredcomment/
356         )
357     {
358         my $value = $input->param($field);
359         $infos->{$field} = $value if $value;
360         $infos->{$field} = ""     if grep { $_ eq $field } @disabled;
361     }
362
363     for my $field ( qw( dateenrolled dateexpiry debarred password_expiration_date ) ) {
364         $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
365     }
366
367     delete $infos->{password_expiration_date} unless $logged_in_user->is_superlibrarian;
368
369     my @errors;
370     my @borrowernumbers = $input->multi_param('borrowernumber');
371     # For each borrower selected
372     for my $borrowernumber ( @borrowernumbers ) {
373
374         # If at least one field are filled, we want to modify the borrower
375         if ( defined $infos ) {
376             # If a debarred date or debarred comment has been submitted make a new debarment
377             if ( $infos->{debarred} || $infos->{debarredcomment} ) {
378                 AddDebarment(
379                     {
380                         borrowernumber => $borrowernumber,
381                         type           => 'MANUAL',
382                         comment        => $infos->{debarredcomment},
383                         expiration     => $infos->{debarred},
384                     });
385             }
386
387             # If debarment date or debarment comment are disabled then remove all debarrments
388             my $patron = Koha::Patrons->find( $borrowernumber );
389             if ( grep { /debarred/ } @disabled ) {
390                 eval {
391                    my $debarrments = $patron->restrictions;
392                    while( my $debarment = $debarrments->next ) {
393                       DelDebarment( $debarment->borrower_debarment_id );
394                    }
395                 };
396             }
397
398             $infos->{borrowernumber} = $borrowernumber;
399             eval { $patron->set($infos)->store; };
400             if ( $@ ) { # FIXME We could provide better error handling here
401                 $infos->{cardnumber} = $patron ? $patron->cardnumber || '' : '';
402                 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
403             }
404         }
405
406         my $patron = Koha::Patrons->find( $borrowernumber );
407         my @attributes = $input->multi_param('patron_attributes');
408         my @attr_values = $input->multi_param('patron_attributes_value');
409         my $attributes;
410         my $i = 0;
411         for my $code ( @attributes ) {
412             push @{ $attributes->{$code}->{values} }, shift @attr_values; # Handling repeatables
413             $attributes->{$code}->{disabled} = grep { $_ eq sprintf("attr%s_value", ++$i) } @disabled;
414         }
415
416         for my $code ( keys %$attributes ) {
417             my $attr_type = Koha::Patron::Attribute::Types->find($code);
418
419             next unless $attr_type;
420
421             # If this borrower is not in the category of this attribute, we don't want to modify this attribute
422             next if $attr_type->category_code and $attr_type->category_code ne $patron->categorycode;
423
424             if ( $attributes->{$code}->{disabled} ) {
425                 # The attribute is disabled, we remove it for this borrower !
426                 eval {
427                     $patron->extended_attributes->search({'me.code' => $code})->filter_by_branch_limitations->delete;
428                 };
429                 push @errors, { error => $@ } if $@;
430             } else {
431                 eval {
432                     $patron->extended_attributes->search({'me.code' => $code})->filter_by_branch_limitations->delete;
433                     $patron->add_extended_attribute({ code => $code, attribute => $_ }) for @{$attributes->{$code}->{values}};
434                 };
435                 push @errors, { error => $@ } if $@;
436             }
437         }
438     }
439     $op = "show_results"; # We have process modifications, the user want to view its
440
441     # Construct the results list
442     my @borrowers;
443     my $max_nb_attr = 0;
444     for my $borrowernumber ( @borrowernumbers ) {
445         my $patron = Koha::Patrons->find( $borrowernumber );
446         if ( $patron ) {
447             my $category_description = $patron->category->description;
448             my $borrower = $patron->unblessed;
449             $borrower->{category_description} = $category_description;
450             my $attributes = $patron->extended_attributes;
451             $borrower->{patron_attributes} = $attributes->as_list;
452             $max_nb_attr = $attributes->count if $attributes->count > $max_nb_attr;
453             push @borrowers, $borrower;
454         }
455     }
456     my @patron_attributes_option;
457     for my $borrower ( @borrowers ) {
458         push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
459         my $length = scalar( @{ $borrower->{patron_attributes} } );
460         push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
461     }
462
463     my @attributes_header = ();
464     for ( 1 .. scalar( $max_nb_attr ) ) {
465         push @attributes_header, { attribute => "Attributes $_" };
466     }
467
468     $template->param( borrowers => \@borrowers );
469     $template->param( attributes_header => \@attributes_header );
470
471     $template->param( errors => \@errors );
472 } else {
473
474     $template->param( patron_lists => [ GetPatronLists() ] );
475 }
476
477 $template->param(
478     op => $op,
479 );
480 output_html_with_http_headers $input, $cookie, $template->output;
481 exit;