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