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