Bug 13071: format_date should not be called in currency.pl
[koha.git] / tools / modborrowers.pl
bloba3f8b3f1733f8c01c623750240fb178187f26962
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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::List::Patron;
40 my $input = new CGI;
41 my $op = $input->param('op') || 'show_form';
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 { template_name => "tools/modborrowers.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { tools => "edit_patrons" },
51 my %cookies = parse CGI::Cookie($cookie);
52 my $sessionID = $cookies{'CGISESSID'}->value;
53 my $dbh = C4::Context->dbh;
55 # Show borrower informations
56 if ( $op eq 'show' ) {
57 my $filefh = $input->upload('uploadfile');
58 my $filecontent = $input->param('filecontent');
59 my $patron_list_id = $input->param('patron_list_id');
60 my @borrowers;
61 my @cardnumbers;
62 my @notfoundcardnumbers;
64 # Get cardnumbers from a file or the input area
65 my @contentlist;
66 if ($filefh) {
67 while ( my $content = <$filefh> ) {
68 $content =~ s/[\r\n]*$//g;
69 push @cardnumbers, $content if $content;
71 } elsif ( $patron_list_id ) {
72 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
74 @cardnumbers =
75 $list->patron_list_patrons()->search_related('borrowernumber')
76 ->get_column('cardnumber')->all();
78 } else {
79 if ( my $list = $input->param('cardnumberlist') ) {
80 push @cardnumbers, split( /\s\n/, $list );
84 my $max_nb_attr = 0;
85 for my $cardnumber ( @cardnumbers ) {
86 my $borrower = GetBorrowerInfos( cardnumber => $cardnumber );
87 if ( $borrower ) {
88 $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
89 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
90 push @borrowers, $borrower;
91 } else {
92 push @notfoundcardnumbers, $cardnumber;
96 # Just for a correct display
97 for my $borrower ( @borrowers ) {
98 my $length = scalar( @{ $borrower->{patron_attributes} } );
99 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
102 # Construct the patron attributes list
103 my @patron_attributes_values;
104 my @patron_attributes_codes;
105 my $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
106 my $patron_categories = C4::Members::GetBorrowercategoryList;
107 for ( values %$patron_attribute_types ) {
108 my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
109 my $options = $attr_type->authorised_value_category
110 ? GetAuthorisedValues( $attr_type->authorised_value_category )
111 : undef;
112 push @patron_attributes_values,
114 attribute_code => $_->{code},
115 options => $options,
118 my $category_code = $_->{category_code};
119 my ( $category_lib ) = map {
120 ( defined $category_code and $_->{categorycode} eq $category_code ) ? $_->{description} : ()
121 } @$patron_categories;
122 push @patron_attributes_codes,
124 attribute_code => $_->{code},
125 attribute_lib => $_->{description},
126 category_lib => $category_lib,
127 type => $attr_type->authorised_value_category ? 'select' : 'text',
131 my @attributes_header = ();
132 for ( 1 .. scalar( $max_nb_attr ) ) {
133 push @attributes_header, { attribute => "Attributes $_" };
135 $template->param( borrowers => \@borrowers );
136 $template->param( attributes_header => \@attributes_header );
137 @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
138 $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
139 if @notfoundcardnumbers;
141 # Construct drop-down list values
142 my $branches = GetBranchesLoop;
143 my @branches_option;
144 push @branches_option, { value => $_->{value}, lib => $_->{branchname} } for @$branches;
145 unshift @branches_option, { value => "", lib => "" };
146 my $categories = GetBorrowercategoryList;
147 my @categories_option;
148 push @categories_option, { value => $_->{categorycode}, lib => $_->{description} } for @$categories;
149 unshift @categories_option, { value => "", lib => "" };
150 my $bsort1 = GetAuthorisedValues("Bsort1");
151 my @sort1_option;
152 push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
153 unshift @sort1_option, { value => "", lib => "" }
154 if @sort1_option;
155 my $bsort2 = GetAuthorisedValues("Bsort2");
156 my @sort2_option;
157 push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
158 unshift @sort2_option, { value => "", lib => "" }
159 if @sort2_option;
161 my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
163 my @fields = (
165 name => "surname",
166 type => "text",
167 mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
171 name => "firstname",
172 type => "text",
173 mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
177 name => "branchcode",
178 type => "select",
179 option => \@branches_option,
180 mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
184 name => "categorycode",
185 type => "select",
186 option => \@categories_option,
187 mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
191 name => "city",
192 type => "text",
193 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
197 name => "state",
198 type => "text",
199 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
203 name => "zipcode",
204 type => "text",
205 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
209 name => "country",
210 type => "text",
211 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
215 name => "sort1",
216 type => @sort1_option ? "select" : "text",
217 option => \@sort1_option,
218 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
222 name => "sort2",
223 type => @sort2_option ? "select" : "text",
224 option => \@sort2_option,
225 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
229 name => "dateenrolled",
230 type => "date",
231 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
235 name => "dateexpiry",
236 type => "date",
237 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
241 name => "borrowernotes",
242 type => "text",
243 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
247 $template->param('patron_attributes_codes', \@patron_attributes_codes);
248 $template->param('patron_attributes_values', \@patron_attributes_values);
250 $template->param( fields => \@fields );
253 # Process modifications
254 if ( $op eq 'do' ) {
256 my @disabled = $input->param('disable_input');
257 my $infos;
258 for my $field ( qw/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes/ ) {
259 my $value = $input->param($field);
260 $infos->{$field} = $value if $value;
261 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
264 my @attributes = $input->param('patron_attributes');
265 my @attr_values = $input->param('patron_attributes_value');
267 my @errors;
268 my @borrowernumbers = $input->param('borrowernumber');
269 # For each borrower selected
270 for my $borrowernumber ( @borrowernumbers ) {
271 # If at least one field are filled, we want to modify the borrower
272 if ( defined $infos ) {
273 $infos->{borrowernumber} = $borrowernumber;
274 my $success = ModMember(%$infos);
275 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber} } if not $success;
279 my $borrower_categorycode = GetBorrowerCategorycode $borrowernumber;
280 my $i=0;
281 for ( @attributes ) {
282 my $attribute;
283 $attribute->{code} = $_;
284 $attribute->{attribute} = $attr_values[$i];
285 my $attr_type = C4::Members::AttributeTypes->fetch( $_ );
286 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
287 ++$i and next if $attr_type->{category_code} and $attr_type->{category_code} ne $borrower_categorycode;
288 my $valuename = "attr" . $i . "_value";
289 if ( grep { /^$valuename$/ } @disabled ) {
290 # The attribute is disabled, we remove it for this borrower !
291 eval {
292 C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
294 push @errors, { error => $@ } if $@;
295 } else {
296 # Attribute's value is empty, we don't want to modify it
297 ++$i and next if not $attribute->{attribute};
299 eval {
300 C4::Members::Attributes::UpdateBorrowerAttribute( $borrowernumber, $attribute );
302 push @errors, { error => $@ } if $@;
304 $i++;
307 $op = "show_results"; # We have process modifications, the user want to view its
309 # Construct the results list
310 my @borrowers;
311 my $max_nb_attr = 0;
312 for my $borrowernumber ( @borrowernumbers ) {
313 my $borrower = GetBorrowerInfos( borrowernumber => $borrowernumber );
314 if ( $borrower ) {
315 $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
316 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
317 push @borrowers, $borrower;
320 my @patron_attributes_option;
321 for my $borrower ( @borrowers ) {
322 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
323 my $length = scalar( @{ $borrower->{patron_attributes} } );
324 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
327 my @attributes_header = ();
328 for ( 1 .. scalar( $max_nb_attr ) ) {
329 push @attributes_header, { attribute => "Attributes $_" };
332 $template->param( borrowers => \@borrowers );
333 $template->param( attributes_header => \@attributes_header );
335 $template->param( borrowers => \@borrowers );
336 $template->param( errors => \@errors );
337 } else {
339 $template->param( patron_lists => [ GetPatronLists() ] );
342 $template->param(
343 op => $op,
345 output_html_with_http_headers $input, $cookie, $template->output;
346 exit;
348 sub GetBorrowerInfos {
349 my ( %info ) = @_;
350 my $borrower = GetMember( %info );
351 if ( $borrower ) {
352 $borrower->{branchname} = GetBranchName( $borrower->{branchcode} );
353 for ( qw(dateenrolled dateexpiry) ) {
354 my $userdate = $borrower->{$_};
355 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
356 $borrower->{$_} = '';
357 next;
359 $borrower->{$_} = $userdate || '';
361 $borrower->{category_description} = GetBorrowercategory( $borrower->{categorycode} )->{description};
362 my $attr_loop = C4::Members::Attributes::GetBorrowerAttributes( $borrower->{borrowernumber} );
363 $borrower->{patron_attributes} = $attr_loop;
365 return $borrower;