Bug 15380: Move Koha::Authority to Koha::MetadataRecord::Authority
[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;
31 use C4::Branch;
32 use C4::Koha;
33 use C4::Members;
34 use C4::Members::Attributes;
35 use C4::Members::AttributeTypes qw/GetAttributeTypes_hashref/;
36 use C4::Output;
37 use List::MoreUtils qw /any uniq/;
38 use Koha::DateUtils qw( dt_from_string );
39 use Koha::List::Patron;
40
41 my $input = new CGI;
42 my $op = $input->param('op') || 'show_form';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {   template_name   => "tools/modborrowers.tt",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { tools => "edit_patrons" },
49     }
50 );
51
52 my %cookies   = parse CGI::Cookie($cookie);
53 my $sessionID = $cookies{'CGISESSID'}->value;
54 my $dbh       = C4::Context->dbh;
55
56 # Show borrower informations
57 if ( $op eq 'show' ) {
58     my $filefh         = $input->upload('uploadfile');
59     my $filecontent    = $input->param('filecontent');
60     my $patron_list_id = $input->param('patron_list_id');
61     my @borrowers;
62     my @cardnumbers;
63     my @notfoundcardnumbers;
64
65     # Get cardnumbers from a file or the input area
66     my @contentlist;
67     if ($filefh) {
68         while ( my $content = <$filefh> ) {
69             $content =~ s/[\r\n]*$//g;
70             push @cardnumbers, $content if $content;
71         }
72     } elsif ( $patron_list_id ) {
73         my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
74
75         @cardnumbers =
76           $list->patron_list_patrons()->search_related('borrowernumber')
77           ->get_column('cardnumber')->all();
78
79     } else {
80         if ( my $list = $input->param('cardnumberlist') ) {
81             push @cardnumbers, split( /\s\n/, $list );
82         }
83     }
84
85     my $max_nb_attr = 0;
86     for my $cardnumber ( @cardnumbers ) {
87         my $borrower = GetBorrowerInfos( cardnumber => $cardnumber );
88         if ( $borrower ) {
89             $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
90                 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
91             push @borrowers, $borrower;
92         } else {
93             push @notfoundcardnumbers, $cardnumber;
94         }
95     }
96
97     # Just for a correct display
98     for my $borrower ( @borrowers ) {
99         my $length = scalar( @{ $borrower->{patron_attributes} } );
100         push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
101     }
102
103     # Construct the patron attributes list
104     my @patron_attributes_values;
105     my @patron_attributes_codes;
106     my $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
107     my $patron_categories = C4::Members::GetBorrowercategoryList;
108     for ( values %$patron_attribute_types ) {
109         my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
110         my $options = $attr_type->authorised_value_category
111             ? GetAuthorisedValues( $attr_type->authorised_value_category )
112             : undef;
113         push @patron_attributes_values,
114             {
115                 attribute_code => $_->{code},
116                 options        => $options,
117             };
118
119         my $category_code = $_->{category_code};
120         my ( $category_lib ) = map {
121             ( defined $category_code and $_->{categorycode} eq $category_code ) ? $_->{description} : ()
122         } @$patron_categories;
123         push @patron_attributes_codes,
124             {
125                 attribute_code => $_->{code},
126                 attribute_lib  => $_->{description},
127                 category_lib   => $category_lib,
128                 type           => $attr_type->authorised_value_category ? 'select' : 'text',
129             };
130     }
131
132     my @attributes_header = ();
133     for ( 1 .. scalar( $max_nb_attr ) ) {
134         push @attributes_header, { attribute => "Attributes $_" };
135     }
136     $template->param( borrowers => \@borrowers );
137     $template->param( attributes_header => \@attributes_header );
138     @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
139     $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
140         if @notfoundcardnumbers;
141
142     # Construct drop-down list values
143     my $branches = GetBranchesLoop;
144     my @branches_option;
145     push @branches_option, { value => $_->{value}, lib => $_->{branchname} } for @$branches;
146     unshift @branches_option, { value => "", lib => "" };
147     my $categories = GetBorrowercategoryList;
148     my @categories_option;
149     push @categories_option, { value => $_->{categorycode}, lib => $_->{description} } for @$categories;
150     unshift @categories_option, { value => "", lib => "" };
151     my $bsort1 = GetAuthorisedValues("Bsort1");
152     my @sort1_option;
153     push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
154     unshift @sort1_option, { value => "", lib => "" }
155         if @sort1_option;
156     my $bsort2 = GetAuthorisedValues("Bsort2");
157     my @sort2_option;
158     push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
159     unshift @sort2_option, { value => "", lib => "" }
160         if @sort2_option;
161
162     my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
163
164     my @fields = (
165         {
166             name => "surname",
167             type => "text",
168             mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
169         }
170         ,
171         {
172             name => "firstname",
173             type => "text",
174             mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
175         }
176         ,
177         {
178             name => "branchcode",
179             type => "select",
180             option => \@branches_option,
181             mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
182         }
183         ,
184         {
185             name => "categorycode",
186             type => "select",
187             option => \@categories_option,
188             mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
189         }
190         ,
191         {
192             name => "city",
193             type => "text",
194             mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
195         }
196         ,
197         {
198             name => "state",
199             type => "text",
200             mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
201         }
202         ,
203         {
204             name => "zipcode",
205             type => "text",
206             mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
207         }
208         ,
209         {
210             name => "country",
211             type => "text",
212             mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
213         }
214         ,
215         {
216             name => "sort1",
217             type => @sort1_option ? "select" : "text",
218             option => \@sort1_option,
219             mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
220         }
221         ,
222         {
223             name => "sort2",
224             type => @sort2_option ? "select" : "text",
225             option => \@sort2_option,
226             mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
227         }
228         ,
229         {
230             name => "dateenrolled",
231             type => "date",
232             mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
233         }
234         ,
235         {
236             name => "dateexpiry",
237             type => "date",
238             mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
239         }
240         ,
241         {
242             name => "borrowernotes",
243             type => "text",
244             mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
245         }
246     );
247
248     $template->param('patron_attributes_codes', \@patron_attributes_codes);
249     $template->param('patron_attributes_values', \@patron_attributes_values);
250
251     $template->param( fields => \@fields );
252 }
253
254 # Process modifications
255 if ( $op eq 'do' ) {
256
257     my @disabled = $input->param('disable_input');
258     my $infos;
259     for my $field ( qw/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes/ ) {
260         my $value = $input->param($field);
261         $infos->{$field} = $value if $value;
262         $infos->{$field} = "" if grep { /^$field$/ } @disabled;
263     }
264
265     for my $field ( qw( dateenrolled dateexpiry ) ) {
266         $infos->{$field} = dt_from_string($infos->{$field});
267     }
268
269     my @attributes = $input->param('patron_attributes');
270     my @attr_values = $input->param('patron_attributes_value');
271
272     my @errors;
273     my @borrowernumbers = $input->param('borrowernumber');
274     # For each borrower selected
275     for my $borrowernumber ( @borrowernumbers ) {
276         # If at least one field are filled, we want to modify the borrower
277         if ( defined $infos ) {
278             $infos->{borrowernumber} = $borrowernumber;
279             my $success = ModMember(%$infos);
280             if (!$success) {
281                 my $borrowerinfo = GetBorrowerInfos( borrowernumber => $borrowernumber );
282                 $infos->{cardnumber} = $borrowerinfo->{cardnumber} || '';
283                 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
284             }
285         }
286
287         #
288         my $borrower_categorycode = GetBorrowerCategorycode $borrowernumber;
289         my $i=0;
290         for ( @attributes ) {
291             my $attribute;
292             $attribute->{code} = $_;
293             $attribute->{attribute} = $attr_values[$i];
294             my $attr_type = C4::Members::AttributeTypes->fetch( $_ );
295             # If this borrower is not in the category of this attribute, we don't want to modify this attribute
296             ++$i and next if $attr_type->{category_code} and $attr_type->{category_code} ne $borrower_categorycode;
297             my $valuename = "attr" . $i . "_value";
298             if ( grep { /^$valuename$/ } @disabled ) {
299                 # The attribute is disabled, we remove it for this borrower !
300                 eval {
301                     C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
302                 };
303                 push @errors, { error => $@ } if $@;
304             } else {
305                 # Attribute's value is empty, we don't want to modify it
306                 ++$i and next if not $attribute->{attribute};
307
308                 eval {
309                     C4::Members::Attributes::UpdateBorrowerAttribute( $borrowernumber, $attribute );
310                 };
311                 push @errors, { error => $@ } if $@;
312             }
313             $i++;
314         }
315     }
316     $op = "show_results"; # We have process modifications, the user want to view its
317
318     # Construct the results list
319     my @borrowers;
320     my $max_nb_attr = 0;
321     for my $borrowernumber ( @borrowernumbers ) {
322         my $borrower = GetBorrowerInfos( borrowernumber => $borrowernumber );
323         if ( $borrower ) {
324             $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
325                 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
326             push @borrowers, $borrower;
327         }
328     }
329     my @patron_attributes_option;
330     for my $borrower ( @borrowers ) {
331         push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
332         my $length = scalar( @{ $borrower->{patron_attributes} } );
333         push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
334     }
335
336     my @attributes_header = ();
337     for ( 1 .. scalar( $max_nb_attr ) ) {
338         push @attributes_header, { attribute => "Attributes $_" };
339     }
340
341     $template->param( borrowers => \@borrowers );
342     $template->param( attributes_header => \@attributes_header );
343
344     $template->param( borrowers => \@borrowers );
345     $template->param( errors => \@errors );
346 } else {
347
348     $template->param( patron_lists => [ GetPatronLists() ] );
349 }
350
351 $template->param(
352     op => $op,
353 );
354 output_html_with_http_headers $input, $cookie, $template->output;
355 exit;
356
357 sub GetBorrowerInfos {
358     my ( %info ) = @_;
359     my $borrower = GetMember( %info );
360     if ( $borrower ) {
361         $borrower->{branchname} = GetBranchName( $borrower->{branchcode} );
362         for ( qw(dateenrolled dateexpiry) ) {
363             my $userdate = $borrower->{$_};
364             unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
365                 $borrower->{$_} = '';
366                 next;
367             }
368             $borrower->{$_} = $userdate || '';
369         }
370         $borrower->{category_description} = GetBorrowercategory( $borrower->{categorycode} )->{description};
371         my $attr_loop = C4::Members::Attributes::GetBorrowerAttributes( $borrower->{borrowernumber} );
372         $borrower->{patron_attributes} = $attr_loop;
373     }
374     return $borrower;
375 }