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.
34 use C4
::Members
::Attributes
;
35 use C4
::Members
::AttributeTypes qw
/GetAttributeTypes_hashref/;
37 use List
::MoreUtils qw
/any uniq/;
38 use Koha
::DateUtils
qw( dt_from_string );
39 use Koha
::List
::Patron
;
42 my $op = $input->param('op') || 'show_form';
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
44 { template_name
=> "tools/modborrowers.tt",
48 flagsrequired
=> { tools
=> "edit_patrons" },
52 my %cookies = parse CGI
::Cookie
($cookie);
53 my $sessionID = $cookies{'CGISESSID'}->value;
54 my $dbh = C4
::Context
->dbh;
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');
63 my @notfoundcardnumbers;
65 # Get cardnumbers from a file or the input area
68 while ( my $content = <$filefh> ) {
69 $content =~ s/[\r\n]*$//g;
70 push @cardnumbers, $content if $content;
72 } elsif ( $patron_list_id ) {
73 my ($list) = GetPatronLists
( { patron_list_id
=> $patron_list_id } );
76 $list->patron_list_patrons()->search_related('borrowernumber')
77 ->get_column('cardnumber')->all();
80 if ( my $list = $input->param('cardnumberlist') ) {
81 push @cardnumbers, split( /\s\n/, $list );
86 for my $cardnumber ( @cardnumbers ) {
87 my $borrower = GetBorrowerInfos
( cardnumber
=> $cardnumber );
89 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
90 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
91 push @borrowers, $borrower;
93 push @notfoundcardnumbers, $cardnumber;
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);
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 # TODO Repeatable attributes are not correctly managed and can cause data lost.
111 # This should be implemented.
112 next if $attr_type->{repeatable
};
113 next if $attr_type->{unique_id
}; # Don't display patron attributes that must be unqiue
114 my $options = $attr_type->authorised_value_category
115 ? GetAuthorisedValues
( $attr_type->authorised_value_category )
117 push @patron_attributes_values,
119 attribute_code
=> $_->{code
},
123 my $category_code = $_->{category_code
};
124 my ( $category_lib ) = map {
125 ( defined $category_code and $_->{categorycode
} eq $category_code ) ?
$_->{description
} : ()
126 } @
$patron_categories;
127 push @patron_attributes_codes,
129 attribute_code
=> $_->{code
},
130 attribute_lib
=> $_->{description
},
131 category_lib
=> $category_lib,
132 type
=> $attr_type->authorised_value_category ?
'select' : 'text',
136 my @attributes_header = ();
137 for ( 1 .. scalar( $max_nb_attr ) ) {
138 push @attributes_header, { attribute
=> "Attributes $_" };
140 $template->param( borrowers
=> \
@borrowers );
141 $template->param( attributes_header
=> \
@attributes_header );
142 @notfoundcardnumbers = map { { cardnumber
=> $_ } } @notfoundcardnumbers;
143 $template->param( notfoundcardnumbers
=> \
@notfoundcardnumbers )
144 if @notfoundcardnumbers;
146 # Construct drop-down list values
147 my $branches = GetBranchesLoop
;
149 push @branches_option, { value
=> $_->{value
}, lib
=> $_->{branchname
} } for @
$branches;
150 unshift @branches_option, { value
=> "", lib
=> "" };
151 my $categories = GetBorrowercategoryList
;
152 my @categories_option;
153 push @categories_option, { value
=> $_->{categorycode
}, lib
=> $_->{description
} } for @
$categories;
154 unshift @categories_option, { value
=> "", lib
=> "" };
155 my $bsort1 = GetAuthorisedValues
("Bsort1");
157 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
158 unshift @sort1_option, { value
=> "", lib
=> "" }
160 my $bsort2 = GetAuthorisedValues
("Bsort2");
162 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
163 unshift @sort2_option, { value
=> "", lib
=> "" }
166 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
172 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
178 mandatory
=> ( grep /firstname/, @mandatoryFields ) ?
1 : 0,
182 name
=> "branchcode",
184 option
=> \
@branches_option,
185 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
189 name
=> "categorycode",
191 option
=> \
@categories_option,
192 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
198 mandatory
=> ( grep /city/, @mandatoryFields ) ?
1 : 0,
204 mandatory
=> ( grep /state/, @mandatoryFields ) ?
1 : 0,
210 mandatory
=> ( grep /zipcode/, @mandatoryFields ) ?
1 : 0,
216 mandatory
=> ( grep /country/, @mandatoryFields ) ?
1 : 0,
221 type
=> @sort1_option ?
"select" : "text",
222 option
=> \
@sort1_option,
223 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
228 type
=> @sort2_option ?
"select" : "text",
229 option
=> \
@sort2_option,
230 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
234 name
=> "dateenrolled",
236 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
240 name
=> "dateexpiry",
242 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
246 name
=> "borrowernotes",
248 mandatory
=> ( grep /borrowernotes/, @mandatoryFields ) ?
1 : 0,
252 $template->param('patron_attributes_codes', \
@patron_attributes_codes);
253 $template->param('patron_attributes_values', \
@patron_attributes_values);
255 $template->param( fields
=> \
@fields );
258 # Process modifications
261 my @disabled = $input->param('disable_input');
263 for my $field ( qw
/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes/ ) {
264 my $value = $input->param($field);
265 $infos->{$field} = $value if $value;
266 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
269 for my $field ( qw( dateenrolled dateexpiry ) ) {
270 $infos->{$field} = dt_from_string
($infos->{$field}) if $infos->{$field};
273 my @attributes = $input->param('patron_attributes');
274 my @attr_values = $input->param('patron_attributes_value');
277 my @borrowernumbers = $input->param('borrowernumber');
278 # For each borrower selected
279 for my $borrowernumber ( @borrowernumbers ) {
280 # If at least one field are filled, we want to modify the borrower
281 if ( defined $infos ) {
282 $infos->{borrowernumber
} = $borrowernumber;
283 my $success = ModMember
(%$infos);
285 my $borrowerinfo = GetBorrowerInfos
( borrowernumber
=> $borrowernumber );
286 $infos->{cardnumber
} = $borrowerinfo->{cardnumber
} || '';
287 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
}, cardnumber
=> $infos->{cardnumber
} };
292 my $borrower_categorycode = GetBorrowerCategorycode
$borrowernumber;
294 for ( @attributes ) {
296 $attribute->{code
} = $_;
297 $attribute->{attribute
} = $attr_values[$i];
298 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_ );
299 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
300 ++$i and next if $attr_type->{category_code
} and $attr_type->{category_code
} ne $borrower_categorycode;
301 my $valuename = "attr" . $i . "_value";
302 if ( grep { /^$valuename$/ } @disabled ) {
303 # The attribute is disabled, we remove it for this borrower !
305 C4
::Members
::Attributes
::DeleteBorrowerAttribute
( $borrowernumber, $attribute );
307 push @errors, { error
=> $@
} if $@
;
309 # Attribute's value is empty, we don't want to modify it
310 ++$i and next if not $attribute->{attribute
};
313 C4
::Members
::Attributes
::UpdateBorrowerAttribute
( $borrowernumber, $attribute );
315 push @errors, { error
=> $@
} if $@
;
320 $op = "show_results"; # We have process modifications, the user want to view its
322 # Construct the results list
325 for my $borrowernumber ( @borrowernumbers ) {
326 my $borrower = GetBorrowerInfos
( borrowernumber
=> $borrowernumber );
328 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
329 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
330 push @borrowers, $borrower;
333 my @patron_attributes_option;
334 for my $borrower ( @borrowers ) {
335 push @patron_attributes_option, { value
=> "$_->{code}", lib
=> $_->{code
} } for @
{ $borrower->{patron_attributes
} };
336 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
337 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
340 my @attributes_header = ();
341 for ( 1 .. scalar( $max_nb_attr ) ) {
342 push @attributes_header, { attribute
=> "Attributes $_" };
345 $template->param( borrowers
=> \
@borrowers );
346 $template->param( attributes_header
=> \
@attributes_header );
348 $template->param( borrowers
=> \
@borrowers );
349 $template->param( errors
=> \
@errors );
352 $template->param( patron_lists
=> [ GetPatronLists
() ] );
358 output_html_with_http_headers
$input, $cookie, $template->output;
361 sub GetBorrowerInfos
{
363 my $borrower = GetMember
( %info );
365 $borrower->{branchname
} = GetBranchName
( $borrower->{branchcode
} );
366 for ( qw(dateenrolled dateexpiry) ) {
367 my $userdate = $borrower->{$_};
368 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
369 $borrower->{$_} = '';
372 $borrower->{$_} = $userdate || '';
374 $borrower->{category_description
} = GetBorrowercategory
( $borrower->{categorycode
} )->{description
};
375 my $attr_loop = C4
::Members
::Attributes
::GetBorrowerAttributes
( $borrower->{borrowernumber
} );
376 $borrower->{patron_attributes
} = $attr_loop;