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