Bug 12230: Set a maximum suspension days as a new issuing rule
[koha.git] / admin / smart-rules.pl
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # copyright 2010 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 strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Debug;
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
30
31 my $input = CGI->new;
32 my $dbh = C4::Context->dbh;
33
34 # my $flagsrequired;
35 # $flagsrequired->{circulation}=1;
36 my ($template, $loggedinuser, $cookie)
37     = get_template_and_user({template_name => "admin/smart-rules.tmpl",
38                             query => $input,
39                             type => "intranet",
40                             authnotrequired => 0,
41                             flagsrequired => {parameters => 'manage_circ_rules'},
42                             debug => 1,
43                             });
44
45 my $type=$input->param('type');
46 my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' );
47 my $op = $input->param('op') || q{};
48
49 if ($op eq 'delete') {
50     my $itemtype     = $input->param('itemtype');
51     my $categorycode = $input->param('categorycode');
52     $debug and warn "deleting $1 $2 $branch";
53
54     my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
55     $sth_Idelete->execute($branch, $categorycode, $itemtype);
56 }
57 elsif ($op eq 'delete-branch-cat') {
58     my $categorycode  = $input->param('categorycode');
59     if ($branch eq "*") {
60         if ($categorycode eq "*") {
61             my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
62             $sth_delete->execute();
63         } else {
64             my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules
65                                             WHERE categorycode = ?");
66             $sth_delete->execute($categorycode);
67         }
68     } elsif ($categorycode eq "*") {
69         my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
70                                         WHERE branchcode = ?");
71         $sth_delete->execute($branch);
72     } else {
73         my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
74                                         WHERE branchcode = ?
75                                         AND categorycode = ?");
76         $sth_delete->execute($branch, $categorycode);
77     }
78 }
79 elsif ($op eq 'delete-branch-item') {
80     my $itemtype  = $input->param('itemtype');
81     if ($branch eq "*") {
82         if ($itemtype eq "*") {
83             my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules");
84             $sth_delete->execute();
85         } else {
86             my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules
87                                             WHERE itemtype = ?");
88             $sth_delete->execute($itemtype);
89         }
90     } elsif ($itemtype eq "*") {
91         my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules
92                                         WHERE branchcode = ?");
93         $sth_delete->execute($branch);
94     } else {
95         my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules
96                                         WHERE branchcode = ?
97                                         AND itemtype = ?");
98         $sth_delete->execute($branch, $itemtype);
99     }
100 }
101 # save the values entered
102 elsif ($op eq 'add') {
103     my $sth_search = $dbh->prepare('SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?');
104     my $sth_insert = $dbh->prepare('INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, renewalperiod, norenewalbefore, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, maxsuspensiondays, firstremind, chargeperiod,rentaldiscount, overduefinescap) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
105     my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, maxsuspensiondays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, renewalperiod=?, norenewalbefore=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=?, overduefinescap=?  WHERE branchcode=? AND categorycode=? AND itemtype=?");
106     
107     my $br = $branch; # branch
108     my $bor  = $input->param('categorycode'); # borrower category
109     my $cat  = $input->param('itemtype');     # item type
110     my $fine = $input->param('fine');
111     my $finedays     = $input->param('finedays');
112     my $maxsuspensiondays = $input->param('maxsuspensiondays');
113     my $firstremind  = $input->param('firstremind');
114     my $chargeperiod = $input->param('chargeperiod');
115     my $maxissueqty  = $input->param('maxissueqty');
116     my $renewalsallowed  = $input->param('renewalsallowed');
117     my $renewalperiod    = $input->param('renewalperiod');
118     my $norenewalbefore  = $input->param('norenewalbefore');
119     $norenewalbefore = undef if $norenewalbefore eq '0' or $norenewalbefore =~ /^\s*$/;
120     my $reservesallowed  = $input->param('reservesallowed');
121     $maxissueqty =~ s/\s//g;
122     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
123     my $issuelength  = $input->param('issuelength');
124     my $lengthunit  = $input->param('lengthunit');
125     my $hardduedate = $input->param('hardduedate');
126     $hardduedate = format_date_in_iso($hardduedate);
127     my $hardduedatecompare = $input->param('hardduedatecompare');
128     my $rentaldiscount = $input->param('rentaldiscount');
129     my $overduefinescap = $input->param('overduefinescap') || undef;
130     $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
131
132     $sth_search->execute($br,$bor,$cat);
133     my $res = $sth_search->fetchrow_hashref();
134     if ($res->{total}) {
135         $sth_update->execute($fine, $finedays, $maxsuspensiondays, $firstremind, $chargeperiod, $maxissueqty, $renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat);
136     } else {
137         $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed, $renewalperiod, $norenewalbefore, $reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays, $maxsuspensiondays, $firstremind,$chargeperiod,$rentaldiscount,$overduefinescap);
138     }
139
140 elsif ($op eq "set-branch-defaults") {
141     my $categorycode  = $input->param('categorycode');
142     my $maxissueqty   = $input->param('maxissueqty');
143     my $holdallowed   = $input->param('holdallowed');
144     my $returnbranch  = $input->param('returnbranch');
145     $maxissueqty =~ s/\s//g;
146     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
147     $holdallowed =~ s/\s//g;
148     $holdallowed = undef if $holdallowed !~ /^\d+/;
149
150     if ($branch eq "*") {
151         my $sth_search = $dbh->prepare("SELECT count(*) AS total
152                                         FROM default_circ_rules");
153         my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
154                                         (maxissueqty, holdallowed, returnbranch)
155                                         VALUES (?, ?, ?)");
156         my $sth_update = $dbh->prepare("UPDATE default_circ_rules
157                                         SET maxissueqty = ?, holdallowed = ?, returnbranch = ?");
158
159         $sth_search->execute();
160         my $res = $sth_search->fetchrow_hashref();
161         if ($res->{total}) {
162             $sth_update->execute($maxissueqty, $holdallowed, $returnbranch);
163         } else {
164             $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch);
165         }
166     } else {
167         my $sth_search = $dbh->prepare("SELECT count(*) AS total
168                                         FROM default_branch_circ_rules
169                                         WHERE branchcode = ?");
170         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
171                                         (branchcode, maxissueqty, holdallowed, returnbranch)
172                                         VALUES (?, ?, ?, ?)");
173         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
174                                         SET maxissueqty = ?, holdallowed = ?, returnbranch = ?
175                                         WHERE branchcode = ?");
176         $sth_search->execute($branch);
177         my $res = $sth_search->fetchrow_hashref();
178         if ($res->{total}) {
179             $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch);
180         } else {
181             $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch);
182         }
183     }
184 }
185 elsif ($op eq "add-branch-cat") {
186     my $categorycode  = $input->param('categorycode');
187     my $maxissueqty   = $input->param('maxissueqty');
188     $maxissueqty =~ s/\s//g;
189     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
190
191     if ($branch eq "*") {
192         if ($categorycode eq "*") {
193             my $sth_search = $dbh->prepare("SELECT count(*) AS total
194                                             FROM default_circ_rules");
195             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
196                                             (maxissueqty)
197                                             VALUES (?)");
198             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
199                                             SET maxissueqty = ?");
200
201             $sth_search->execute();
202             my $res = $sth_search->fetchrow_hashref();
203             if ($res->{total}) {
204                 $sth_update->execute($maxissueqty);
205             } else {
206                 $sth_insert->execute($maxissueqty);
207             }
208         } else {
209             my $sth_search = $dbh->prepare("SELECT count(*) AS total
210                                             FROM default_borrower_circ_rules
211                                             WHERE categorycode = ?");
212             my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
213                                             (categorycode, maxissueqty)
214                                             VALUES (?, ?)");
215             my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
216                                             SET maxissueqty = ?
217                                             WHERE categorycode = ?");
218             $sth_search->execute($branch);
219             my $res = $sth_search->fetchrow_hashref();
220             if ($res->{total}) {
221                 $sth_update->execute($maxissueqty, $categorycode);
222             } else {
223                 $sth_insert->execute($categorycode, $maxissueqty);
224             }
225         }
226     } elsif ($categorycode eq "*") {
227         my $sth_search = $dbh->prepare("SELECT count(*) AS total
228                                         FROM default_branch_circ_rules
229                                         WHERE branchcode = ?");
230         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
231                                         (branchcode, maxissueqty)
232                                         VALUES (?, ?)");
233         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
234                                         SET maxissueqty = ?
235                                         WHERE branchcode = ?");
236         $sth_search->execute($branch);
237         my $res = $sth_search->fetchrow_hashref();
238         if ($res->{total}) {
239             $sth_update->execute($maxissueqty, $branch);
240         } else {
241             $sth_insert->execute($branch, $maxissueqty);
242         }
243     } else {
244         my $sth_search = $dbh->prepare("SELECT count(*) AS total
245                                         FROM branch_borrower_circ_rules
246                                         WHERE branchcode = ?
247                                         AND   categorycode = ?");
248         my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
249                                         (branchcode, categorycode, maxissueqty)
250                                         VALUES (?, ?, ?)");
251         my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
252                                         SET maxissueqty = ?
253                                         WHERE branchcode = ?
254                                         AND categorycode = ?");
255
256         $sth_search->execute($branch, $categorycode);
257         my $res = $sth_search->fetchrow_hashref();
258         if ($res->{total}) {
259             $sth_update->execute($maxissueqty, $branch, $categorycode);
260         } else {
261             $sth_insert->execute($branch, $categorycode, $maxissueqty);
262         }
263     }
264 }
265 elsif ($op eq "add-branch-item") {
266     my $itemtype  = $input->param('itemtype');
267     my $holdallowed   = $input->param('holdallowed');
268     my $returnbranch  = $input->param('returnbranch');
269     $holdallowed =~ s/\s//g;
270     $holdallowed = undef if $holdallowed !~ /^\d+/;
271
272     if ($branch eq "*") {
273         if ($itemtype eq "*") {
274             my $sth_search = $dbh->prepare("SELECT count(*) AS total
275                                             FROM default_circ_rules");
276             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
277                                             (holdallowed, returnbranch)
278                                             VALUES (?, ?)");
279             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
280                                             SET holdallowed = ?, returnbranch = ?");
281
282             $sth_search->execute();
283             my $res = $sth_search->fetchrow_hashref();
284             if ($res->{total}) {
285                 $sth_update->execute($holdallowed, $returnbranch);
286             } else {
287                 $sth_insert->execute($holdallowed, $returnbranch);
288             }
289         } else {
290             my $sth_search = $dbh->prepare("SELECT count(*) AS total
291                                             FROM default_branch_item_rules
292                                             WHERE itemtype = ?");
293             my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
294                                             (itemtype, holdallowed, returnbranch)
295                                             VALUES (?, ?, ?)");
296             my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
297                                             SET holdallowed = ?, returnbranch = ?
298                                             WHERE itemtype = ?");
299             $sth_search->execute($itemtype);
300             my $res = $sth_search->fetchrow_hashref();
301             if ($res->{total}) {
302                 $sth_update->execute($holdallowed, $returnbranch, $itemtype);
303             } else {
304                 $sth_insert->execute($itemtype, $holdallowed, $returnbranch);
305             }
306         }
307     } elsif ($itemtype eq "*") {
308         my $sth_search = $dbh->prepare("SELECT count(*) AS total
309                                         FROM default_branch_circ_rules
310                                         WHERE branchcode = ?");
311         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
312                                         (branchcode, holdallowed, returnbranch)
313                                         VALUES (?, ?, ?)");
314         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
315                                         SET holdallowed = ?, returnbranch = ?
316                                         WHERE branchcode = ?");
317         $sth_search->execute($branch);
318         my $res = $sth_search->fetchrow_hashref();
319         if ($res->{total}) {
320             $sth_update->execute($holdallowed, $returnbranch, $branch);
321         } else {
322             $sth_insert->execute($branch, $holdallowed, $returnbranch);
323         }
324     } else {
325         my $sth_search = $dbh->prepare("SELECT count(*) AS total
326                                         FROM branch_item_rules
327                                         WHERE branchcode = ?
328                                         AND   itemtype = ?");
329         my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
330                                         (branchcode, itemtype, holdallowed, returnbranch)
331                                         VALUES (?, ?, ?, ?)");
332         my $sth_update = $dbh->prepare("UPDATE branch_item_rules
333                                         SET holdallowed = ?, returnbranch = ?
334                                         WHERE branchcode = ?
335                                         AND itemtype = ?");
336
337         $sth_search->execute($branch, $itemtype);
338         my $res = $sth_search->fetchrow_hashref();
339         if ($res->{total}) {
340             $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype);
341         } else {
342             $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch);
343         }
344     }
345 }
346
347 my $branches = GetBranches();
348 my @branchloop;
349 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
350     push @branchloop, {
351         value      => $thisbranch,
352         selected   => $thisbranch eq $branch,
353         branchname => $branches->{$thisbranch}->{'branchname'},
354     };
355 }
356
357 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
358 $sth->execute;
359 my @category_loop;
360 while (my $data=$sth->fetchrow_hashref){
361     push @category_loop,$data;
362 }
363
364 $sth->finish;
365 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
366 $sth->execute;
367 # $i=0;
368 my @row_loop;
369 my @itemtypes;
370 while (my $row=$sth->fetchrow_hashref){
371     push @itemtypes,$row;
372 }
373
374 my $sth2 = $dbh->prepare("
375     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
376     FROM issuingrules
377     LEFT JOIN itemtypes
378         ON (itemtypes.itemtype = issuingrules.itemtype)
379     LEFT JOIN categories
380         ON (categories.categorycode = issuingrules.categorycode)
381     WHERE issuingrules.branchcode = ?
382 ");
383 $sth2->execute($branch);
384
385 while (my $row = $sth2->fetchrow_hashref) {
386     $row->{'current_branch'} ||= $row->{'branchcode'};
387     $row->{'humanitemtype'} ||= $row->{'itemtype'};
388     $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
389     $row->{'humancategorycode'} ||= $row->{'categorycode'};
390     $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
391     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
392     if ($row->{'hardduedate'} ne '0000-00-00') {
393        $row->{'hardduedate'} = format_date( $row->{'hardduedate'});
394        $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1);
395        $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} ==  0);
396        $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} ==  1);
397     } else {
398        $row->{'hardduedate'} = 0;
399     }
400     push @row_loop, $row;
401 }
402 $sth->finish;
403
404 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
405
406 my $sth_branch_cat;
407 if ($branch eq "*") {
408     $sth_branch_cat = $dbh->prepare("
409         SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
410         FROM default_borrower_circ_rules
411         JOIN categories USING (categorycode)
412         
413     ");
414     $sth_branch_cat->execute();
415 } else {
416     $sth_branch_cat = $dbh->prepare("
417         SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
418         FROM branch_borrower_circ_rules
419         JOIN categories USING (categorycode)
420         WHERE branch_borrower_circ_rules.branchcode = ?
421     ");
422     $sth_branch_cat->execute($branch);
423 }
424
425 my @branch_cat_rules = ();
426 while (my $row = $sth_branch_cat->fetchrow_hashref) {
427     push @branch_cat_rules, $row;
428 }
429 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
430
431 # note undef maxissueqty so that template can deal with them
432 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
433     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
434 }
435
436 @sorted_row_loop = sort by_category_and_itemtype @row_loop;
437
438 my $sth_branch_item;
439 if ($branch eq "*") {
440     $sth_branch_item = $dbh->prepare("
441         SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
442         FROM default_branch_item_rules
443         JOIN itemtypes USING (itemtype)
444     ");
445     $sth_branch_item->execute();
446 } else {
447     $sth_branch_item = $dbh->prepare("
448         SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
449         FROM branch_item_rules
450         JOIN itemtypes USING (itemtype)
451         WHERE branch_item_rules.branchcode = ?
452     ");
453     $sth_branch_item->execute($branch);
454 }
455
456 my @branch_item_rules = ();
457 while (my $row = $sth_branch_item->fetchrow_hashref) {
458     push @branch_item_rules, $row;
459 }
460 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
461
462 # note undef holdallowed so that template can deal with them
463 foreach my $entry (@sorted_branch_item_rules) {
464     $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
465     $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
466 }
467
468 $template->param(show_branch_cat_rule_form => 1);
469 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
470 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
471
472 my $sth_defaults;
473 if ($branch eq "*") {
474     $sth_defaults = $dbh->prepare("
475         SELECT *
476         FROM default_circ_rules
477     ");
478     $sth_defaults->execute();
479 } else {
480     $sth_defaults = $dbh->prepare("
481         SELECT *
482         FROM default_branch_circ_rules
483         WHERE branchcode = ?
484     ");
485     $sth_defaults->execute($branch);
486 }
487
488 my $defaults = $sth_defaults->fetchrow_hashref;
489
490 if ($defaults) {
491     $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
492     $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
493     $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
494     $template->param(default_maxissueqty => $defaults->{maxissueqty});
495     $template->param(default_returnbranch => $defaults->{returnbranch});
496 }
497
498 $template->param(default_rules => ($defaults ? 1 : 0));
499
500 $template->param(categoryloop => \@category_loop,
501                         itemtypeloop => \@itemtypes,
502                         rules => \@sorted_row_loop,
503                         branchloop => \@branchloop,
504                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
505                         current_branch => $branch,
506                         definedbranch => scalar(@sorted_row_loop)>0 
507                         );
508 output_html_with_http_headers $input, $cookie, $template->output;
509
510 exit 0;
511
512 # sort by patron category, then item type, putting
513 # default entries at the bottom
514 sub by_category_and_itemtype {
515     unless (by_category($a, $b)) {
516         return by_itemtype($a, $b);
517     }
518 }
519
520 sub by_category {
521     my ($a, $b) = @_;
522     if ($a->{'default_humancategorycode'}) {
523         return ($b->{'default_humancategorycode'} ? 0 : 1);
524     } elsif ($b->{'default_humancategorycode'}) {
525         return -1;
526     } else {
527         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
528     }
529 }
530
531 sub by_itemtype {
532     my ($a, $b) = @_;
533     if ($a->{'default_humanitemtype'}) {
534         return ($b->{'default_humanitemtype'} ? 0 : 1);
535     } elsif ($b->{'default_humanitemtype'}) {
536         return -1;
537     } else {
538         return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};
539     }
540 }