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