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>.
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.
33 use C4
::Members
::Attributes
;
34 use C4
::Members
::AttributeTypes qw
/GetAttributeTypes_hashref/;
36 use List
::MoreUtils qw
/any uniq/;
37 use Koha
::DateUtils
qw( dt_from_string );
38 use Koha
::List
::Patron
;
40 use Koha
::Patron
::Categories
;
41 use Koha
::Patron
::Debarments
;
45 my $op = $input->param('op') || 'show_form';
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
47 { template_name
=> "tools/modborrowers.tt",
51 flagsrequired
=> { tools
=> "edit_patrons" },
55 my $logged_in_user = Koha
::Patrons
->find( $loggedinuser ) or die "Not logged in";
57 my %cookies = parse CGI
::Cookie
($cookie);
58 my $sessionID = $cookies{'CGISESSID'}->value;
59 my $dbh = C4
::Context
->dbh;
61 # Show borrower informations
62 if ( $op eq 'show' ) {
63 my $filefh = $input->upload('uploadfile');
64 my $filecontent = $input->param('filecontent');
65 my $patron_list_id = $input->param('patron_list_id');
68 my ( @notfoundcardnumbers, @from_another_group_of_libraries );
70 # Get cardnumbers from a file or the input area
73 while ( my $content = <$filefh> ) {
74 $content =~ s/[\r\n]*$//g;
75 push @cardnumbers, $content if $content;
77 } elsif ( $patron_list_id ) {
78 my ($list) = GetPatronLists
( { patron_list_id
=> $patron_list_id } );
81 $list->patron_list_patrons()->search_related('borrowernumber')
82 ->get_column('cardnumber')->all();
85 if ( my $list = $input->param('cardnumberlist') ) {
86 push @cardnumbers, split( /\s\n/, $list );
91 for my $cardnumber ( @cardnumbers ) {
92 my $patron = Koha
::Patrons
->find( { cardnumber
=> $cardnumber } );
94 if ( $logged_in_user->can_see_patron_infos( $patron ) ) {
95 $patron = $patron->unblessed;
96 $patron->{patron_attributes
} = C4
::Members
::Attributes
::GetBorrowerAttributes
( $patron->{borrowernumber
} );
97 $max_nb_attr = scalar( @
{ $patron->{patron_attributes
} } )
98 if scalar( @
{ $patron->{patron_attributes
} } ) > $max_nb_attr;
99 push @borrowers, $patron;
101 push @notfoundcardnumbers, $cardnumber;
104 push @notfoundcardnumbers, $cardnumber;
108 # Just for a correct display
109 for my $borrower ( @borrowers ) {
110 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
111 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
114 # Construct the patron attributes list
115 my @patron_attributes_values;
116 my @patron_attributes_codes;
117 my $patron_attribute_types = C4
::Members
::AttributeTypes
::GetAttributeTypes_hashref
('all');
118 my @patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['description']});
119 for ( values %$patron_attribute_types ) {
120 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_->{code
} );
121 # TODO Repeatable attributes are not correctly managed and can cause data lost.
122 # This should be implemented.
123 next if $attr_type->{repeatable
};
124 next if $attr_type->{unique_id
}; # Don't display patron attributes that must be unqiue
125 my $options = $attr_type->authorised_value_category
126 ? GetAuthorisedValues
( $attr_type->authorised_value_category )
128 push @patron_attributes_values,
130 attribute_code
=> $_->{code
},
134 my $category_code = $_->{category_code
};
135 my ( $category_lib ) = map {
136 ( defined $category_code and $_->categorycode eq $category_code ) ?
$_->description : ()
137 } @patron_categories;
138 push @patron_attributes_codes,
140 attribute_code
=> $_->{code
},
141 attribute_lib
=> $_->{description
},
142 category_lib
=> $category_lib,
143 type
=> $attr_type->authorised_value_category ?
'select' : 'text',
147 my @attributes_header = ();
148 for ( 1 .. scalar( $max_nb_attr ) ) {
149 push @attributes_header, { attribute
=> "Attributes $_" };
151 $template->param( borrowers
=> \
@borrowers );
152 $template->param( attributes_header
=> \
@attributes_header );
153 @notfoundcardnumbers = map { { cardnumber
=> $_ } } @notfoundcardnumbers;
154 $template->param( notfoundcardnumbers
=> \
@notfoundcardnumbers )
155 if @notfoundcardnumbers;
157 # Construct drop-down list values
158 my $branches = Koha
::Libraries
->search({}, { order_by
=> ['branchname'] })->unblessed;
160 push @branches_option, { value
=> $_->{branchcode
}, lib
=> $_->{branchname
} } for @
$branches;
161 unshift @branches_option, { value
=> "", lib
=> "" };
162 my @categories_option;
163 push @categories_option, { value
=> $_->categorycode, lib
=> $_->description } for @patron_categories;
164 unshift @categories_option, { value
=> "", lib
=> "" };
165 my $bsort1 = GetAuthorisedValues
("Bsort1");
167 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
168 unshift @sort1_option, { value
=> "", lib
=> "" }
170 my $bsort2 = GetAuthorisedValues
("Bsort2");
172 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
173 unshift @sort2_option, { value
=> "", lib
=> "" }
176 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
182 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
188 mandatory
=> ( grep /firstname/, @mandatoryFields ) ?
1 : 0,
192 name
=> "branchcode",
194 option
=> \
@branches_option,
195 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
199 name
=> "categorycode",
201 option
=> \
@categories_option,
202 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
208 mandatory
=> ( grep /city/, @mandatoryFields ) ?
1 : 0,
214 mandatory
=> ( grep /state/, @mandatoryFields ) ?
1 : 0,
220 mandatory
=> ( grep /zipcode/, @mandatoryFields ) ?
1 : 0,
226 mandatory
=> ( grep /country/, @mandatoryFields ) ?
1 : 0,
231 type
=> @sort1_option ?
"select" : "text",
232 option
=> \
@sort1_option,
233 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
238 type
=> @sort2_option ?
"select" : "text",
239 option
=> \
@sort2_option,
240 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
244 name
=> "dateenrolled",
246 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
250 name
=> "dateexpiry",
252 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
256 name
=> "borrowernotes",
258 mandatory
=> ( grep /borrowernotes/, @mandatoryFields ) ?
1 : 0,
264 mandatory
=> ( grep /opacnote/, @mandatoryFields ) ?
1 : 0,
270 mandatory
=> ( grep /debarred/, @mandatoryFields ) ?
1 : 0,
274 name
=> "debarredcomment",
276 mandatory
=> ( grep /debarredcomment/, @mandatoryFields ) ?
1 : 0,
280 $template->param('patron_attributes_codes', \
@patron_attributes_codes);
281 $template->param('patron_attributes_values', \
@patron_attributes_values);
283 $template->param( fields
=> \
@fields );
286 # Process modifications
289 my @disabled = $input->multi_param('disable_input');
291 for my $field ( qw
/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote debarred debarredcomment/ ) {
292 my $value = $input->param($field);
293 $infos->{$field} = $value if $value;
294 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
297 for my $field ( qw( dateenrolled dateexpiry debarred ) ) {
298 $infos->{$field} = dt_from_string
($infos->{$field}) if $infos->{$field};
301 my @attributes = $input->multi_param('patron_attributes');
302 my @attr_values = $input->multi_param('patron_attributes_value');
305 my @borrowernumbers = $input->multi_param('borrowernumber');
306 # For each borrower selected
307 for my $borrowernumber ( @borrowernumbers ) {
308 # If at least one field are filled, we want to modify the borrower
309 if ( defined $infos ) {
310 # If a debarred date or debarred comment has been submitted make a new debarment
311 if ( $infos->{debarred
} || $infos->{debarredcomment
} ) {
314 borrowernumber
=> $borrowernumber,
316 comment
=> $infos->{debarredcomment
},
317 expiration
=> $infos->{debarred
},
321 # If debarment date or debarment comment are disabled then remove all debarrments
322 if ( grep { /debarred/ } @disabled ) {
324 my $debarrments = GetDebarments
( { borrowernumber
=> $borrowernumber } );
325 foreach my $debarment (@
$debarrments) {
326 DelDebarment
( $debarment->{'borrower_debarment_id'} );
331 $infos->{borrowernumber
} = $borrowernumber;
332 eval { Koha
::Patrons
->find( $borrowernumber )->set($infos)->store; };
333 if ( $@
) { # FIXME We could provide better error handling here
334 my $patron = Koha
::Patrons
->find( $borrowernumber );
335 $infos->{cardnumber
} = $patron ?
$patron->cardnumber || '' : '';
336 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
}, cardnumber
=> $infos->{cardnumber
} };
340 my $borrower_categorycode = Koha
::Patrons
->find( $borrowernumber )->categorycode;
342 for ( @attributes ) {
345 $attribute->{code
} = $_;
346 $attribute->{attribute
} = $attr_values[$i];
347 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_ );
348 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
349 ++$i and next if $attr_type->{category_code
} and $attr_type->{category_code
} ne $borrower_categorycode;
350 my $valuename = "attr" . $i . "_value";
351 if ( grep { /^$valuename$/ } @disabled ) {
352 # The attribute is disabled, we remove it for this borrower !
354 C4
::Members
::Attributes
::DeleteBorrowerAttribute
( $borrowernumber, $attribute );
356 push @errors, { error
=> $@
} if $@
;
359 C4
::Members
::Attributes
::UpdateBorrowerAttribute
( $borrowernumber, $attribute );
361 push @errors, { error
=> $@
} if $@
;
366 $op = "show_results"; # We have process modifications, the user want to view its
368 # Construct the results list
371 for my $borrowernumber ( @borrowernumbers ) {
372 my $patron = Koha
::Patrons
->find( $borrowernumber );
374 $patron = $patron->unblessed;
375 $patron->{patron_attributes
} = C4
::Members
::Attributes
::GetBorrowerAttributes
( $patron->{borrowernumber
} );
376 $max_nb_attr = scalar( @
{ $patron->{patron_attributes
} } )
377 if scalar( @
{ $patron->{patron_attributes
} } ) > $max_nb_attr;
378 push @borrowers, $patron;
381 my @patron_attributes_option;
382 for my $borrower ( @borrowers ) {
383 push @patron_attributes_option, { value
=> "$_->{code}", lib
=> $_->{code
} } for @
{ $borrower->{patron_attributes
} };
384 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
385 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
388 my @attributes_header = ();
389 for ( 1 .. scalar( $max_nb_attr ) ) {
390 push @attributes_header, { attribute
=> "Attributes $_" };
393 $template->param( borrowers
=> \
@borrowers );
394 $template->param( attributes_header
=> \
@attributes_header );
396 $template->param( errors
=> \
@errors );
399 $template->param( patron_lists
=> [ GetPatronLists
() ] );
405 output_html_with_http_headers
$input, $cookie, $template->output;