Bug 29486: _koha_marc_update_bib_ids no longer needed for GetMarcBiblio
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output qw( output_html_with_http_headers );
24 use C4::Auth qw( get_template_and_user );
25 use Koha::Exceptions::Exception;
26 use Koha::DateUtils qw( dt_from_string output_pref );
27 use Koha::Database;
28 use Koha::Logger;
29 use Koha::Libraries;
30 use Koha::CirculationRules;
31 use Koha::Patron::Categories;
32 use Koha::Caches;
33 use Koha::Patrons;
34
35 my $input = CGI->new;
36 my $dbh = C4::Context->dbh;
37
38 # my $flagsrequired;
39 # $flagsrequired->{circulation}=1;
40 my ($template, $loggedinuser, $cookie)
41     = get_template_and_user({template_name => "admin/smart-rules.tt",
42                             query => $input,
43                             type => "intranet",
44                             flagsrequired => {parameters => 'manage_circ_rules'},
45                             });
46
47 my $type=$input->param('type');
48
49 my $branch = $input->param('branch');
50 unless ( $branch ) {
51     if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
52         $branch = Koha::Libraries->search->count() == 1 ? undef : C4::Context::mybranch();
53     }
54     else {
55         $branch = C4::Context::only_my_library() ? ( C4::Context::mybranch() || '*' ) : '*';
56     }
57 }
58
59 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
60
61 my $can_edit_from_any_library = $logged_in_patron->has_permission( {parameters => 'manage_circ_rules_from_any_libraries' } );
62 $template->param( restricted_to_own_library => not $can_edit_from_any_library );
63 $branch = C4::Context::mybranch() unless $can_edit_from_any_library;
64
65 my $op = $input->param('op') || q{};
66 my $language = C4::Languages::getlanguage();
67
68 my $cache = Koha::Caches->get_instance;
69 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
70
71 if ($op eq 'delete') {
72     my $itemtype     = $input->param('itemtype');
73     my $categorycode = $input->param('categorycode');
74
75     Koha::CirculationRules->set_rules(
76         {
77             categorycode => $categorycode eq '*' ? undef : $categorycode,
78             branchcode   => $branch eq '*' ? undef : $branch,
79             itemtype     => $itemtype eq '*' ? undef : $itemtype,
80             rules        => {
81                 maxissueqty                      => undef,
82                 maxonsiteissueqty                => undef,
83                 rentaldiscount                   => undef,
84                 fine                             => undef,
85                 finedays                         => undef,
86                 maxsuspensiondays                => undef,
87                 suspension_chargeperiod          => undef,
88                 firstremind                      => undef,
89                 chargeperiod                     => undef,
90                 chargeperiod_charge_at           => undef,
91                 issuelength                      => undef,
92                 daysmode                         => undef,
93                 lengthunit                       => undef,
94                 hardduedate                      => undef,
95                 hardduedatecompare               => undef,
96                 renewalsallowed                  => undef,
97                 unseen_renewals_allowed          => undef,
98                 renewalperiod                    => undef,
99                 norenewalbefore                  => undef,
100                 auto_renew                       => undef,
101                 no_auto_renewal_after            => undef,
102                 no_auto_renewal_after_hard_limit => undef,
103                 reservesallowed                  => undef,
104                 holds_per_record                 => undef,
105                 holds_per_day                    => undef,
106                 onshelfholds                     => undef,
107                 opacitemholds                    => undef,
108                 overduefinescap                  => undef,
109                 cap_fine_to_replacement_price    => undef,
110                 article_requests                 => undef,
111                 note                             => undef,
112                 decreaseloanholds                => undef,
113             }
114         }
115     );
116 }
117 elsif ($op eq 'delete-branch-cat') {
118     my $categorycode  = $input->param('categorycode');
119     if ($branch eq "*") {
120         if ($categorycode eq "*") {
121             Koha::CirculationRules->set_rules(
122                 {
123                     branchcode   => undef,
124                     categorycode => undef,
125                     rules        => {
126                         max_holds                      => undef,
127                         patron_maxissueqty             => undef,
128                         patron_maxonsiteissueqty       => undef,
129                     }
130                 }
131             );
132             Koha::CirculationRules->set_rules(
133                 {
134                     branchcode   => undef,
135                     itemtype     => undef,
136                     rules        => {
137                         holdallowed             => undef,
138                         hold_fulfillment_policy => undef,
139                         returnbranch            => undef,
140                     }
141                 }
142             );
143         } else {
144             Koha::CirculationRules->set_rules(
145                 {
146                     categorycode => $categorycode,
147                     branchcode   => undef,
148                     rules        => {
149                         max_holds                => undef,
150                         patron_maxissueqty       => undef,
151                         patron_maxonsiteissueqty => undef,
152                     }
153                 }
154             );
155         }
156     } elsif ($categorycode eq "*") {
157         Koha::CirculationRules->set_rules(
158             {
159                 branchcode   => $branch,
160                 categorycode => undef,
161                 rules        => {
162                     max_holds                => undef,
163                     patron_maxissueqty       => undef,
164                     patron_maxonsiteissueqty => undef,
165                 }
166             }
167         );
168         Koha::CirculationRules->set_rules(
169             {
170                 branchcode   => $branch,
171                 itemtype     => undef,
172                 rules        => {
173                     holdallowed             => undef,
174                     hold_fulfillment_policy => undef,
175                     returnbranch            => undef,
176                 }
177             }
178         );
179     } else {
180         Koha::CirculationRules->set_rules(
181             {
182                 categorycode => $categorycode,
183                 branchcode   => $branch,
184                 rules        => {
185                     max_holds         => undef,
186                     patron_maxissueqty       => undef,
187                     patron_maxonsiteissueqty => undef,
188                 }
189             }
190         );
191     }
192 }
193 elsif ($op eq 'delete-branch-item') {
194     my $itemtype  = $input->param('itemtype');
195     if ($branch eq "*") {
196         if ($itemtype eq "*") {
197             Koha::CirculationRules->set_rules(
198                 {
199                     branchcode   => undef,
200                     itemtype     => undef,
201                     rules        => {
202                         holdallowed             => undef,
203                         hold_fulfillment_policy => undef,
204                         returnbranch            => undef,
205                     }
206                 }
207             );
208         } else {
209             Koha::CirculationRules->set_rules(
210                 {
211                     branchcode   => undef,
212                     itemtype     => $itemtype,
213                     rules        => {
214                         holdallowed             => undef,
215                         hold_fulfillment_policy => undef,
216                         returnbranch            => undef,
217                     }
218                 }
219             );
220         }
221     } elsif ($itemtype eq "*") {
222         Koha::CirculationRules->set_rules(
223             {
224                 branchcode   => $branch,
225                 itemtype     => undef,
226                 rules        => {
227                     holdallowed             => undef,
228                     hold_fulfillment_policy => undef,
229                     returnbranch            => undef,
230                 }
231             }
232         );
233     } else {
234         Koha::CirculationRules->set_rules(
235             {
236                 branchcode   => $branch,
237                 itemtype     => $itemtype,
238                 rules        => {
239                     holdallowed             => undef,
240                     hold_fulfillment_policy => undef,
241                     returnbranch            => undef,
242                 }
243             }
244         );
245     }
246 }
247 # save the values entered
248 elsif ($op eq 'add') {
249     my $br = $branch; # branch
250     my $bor  = $input->param('categorycode'); # borrower category
251     my $itemtype  = $input->param('itemtype');     # item type
252     my $fine = $input->param('fine');
253     my $finedays     = $input->param('finedays');
254     my $maxsuspensiondays = $input->param('maxsuspensiondays') || '';
255     my $suspension_chargeperiod = $input->param('suspension_chargeperiod') || 1;
256     my $firstremind  = $input->param('firstremind');
257     my $chargeperiod = $input->param('chargeperiod');
258     my $chargeperiod_charge_at = $input->param('chargeperiod_charge_at');
259     my $maxissueqty = strip_non_numeric( scalar $input->param('maxissueqty') );
260     my $maxonsiteissueqty = strip_non_numeric( scalar $input->param('maxonsiteissueqty') );
261     my $renewalsallowed  = $input->param('renewalsallowed');
262     my $unseen_renewals_allowed  = $input->param('unseen_renewals_allowed');
263     my $renewalperiod    = $input->param('renewalperiod');
264     my $norenewalbefore  = $input->param('norenewalbefore');
265     $norenewalbefore = '' if $norenewalbefore =~ /^\s*$/;
266     my $auto_renew = $input->param('auto_renew') eq 'yes' ? 1 : 0;
267     my $no_auto_renewal_after = $input->param('no_auto_renewal_after');
268     $no_auto_renewal_after = '' if $no_auto_renewal_after =~ /^\s*$/;
269     my $no_auto_renewal_after_hard_limit = $input->param('no_auto_renewal_after_hard_limit') || '';
270     $no_auto_renewal_after_hard_limit = eval { dt_from_string( scalar $no_auto_renewal_after_hard_limit ) } if ( $no_auto_renewal_after_hard_limit );
271     $no_auto_renewal_after_hard_limit = output_pref( { dt => $no_auto_renewal_after_hard_limit, dateonly => 1, dateformat => 'iso' } ) if ( $no_auto_renewal_after_hard_limit );
272     my $reservesallowed  = strip_non_numeric( scalar $input->param('reservesallowed') );
273     my $holds_per_record = strip_non_numeric( scalar $input->param('holds_per_record') );
274     my $holds_per_day    = strip_non_numeric( scalar $input->param('holds_per_day') );
275     my $onshelfholds     = $input->param('onshelfholds') || 0;
276     my $issuelength  = $input->param('issuelength');
277     $issuelength = $issuelength eq q{} ? undef : $issuelength;
278     my $daysmode = $input->param('daysmode');
279     my $lengthunit  = $input->param('lengthunit');
280     my $hardduedate = $input->param('hardduedate') || undef;
281     $hardduedate = eval { dt_from_string( scalar $hardduedate ) } if ( $hardduedate );
282     $hardduedate = output_pref( { dt => $hardduedate, dateonly => 1, dateformat => 'iso' } ) if ( $hardduedate );
283     my $hardduedatecompare = $input->param('hardduedatecompare');
284     my $rentaldiscount = $input->param('rentaldiscount');
285     my $opacitemholds = $input->param('opacitemholds') || 0;
286     my $article_requests = $input->param('article_requests') || 'no';
287     my $overduefinescap = $input->param('overduefinescap') || '';
288     my $cap_fine_to_replacement_price = ($input->param('cap_fine_to_replacement_price') || '') eq 'on';
289     my $note = $input->param('note');
290     my $decreaseloanholds = $input->param('decreaseloanholds') || undef;
291
292     my $rules = {
293         maxissueqty                   => $maxissueqty,
294         maxonsiteissueqty             => $maxonsiteissueqty,
295         rentaldiscount                => $rentaldiscount,
296         fine                          => $fine,
297         finedays                      => $finedays,
298         maxsuspensiondays             => $maxsuspensiondays,
299         suspension_chargeperiod       => $suspension_chargeperiod,
300         firstremind                   => $firstremind,
301         chargeperiod                  => $chargeperiod,
302         chargeperiod_charge_at        => $chargeperiod_charge_at,
303         issuelength                   => $issuelength,
304         daysmode                      => $daysmode,
305         lengthunit                    => $lengthunit,
306         hardduedate                   => $hardduedate,
307         hardduedatecompare            => $hardduedatecompare,
308         renewalsallowed               => $renewalsallowed,
309         unseen_renewals_allowed       => $unseen_renewals_allowed,
310         renewalperiod                 => $renewalperiod,
311         norenewalbefore               => $norenewalbefore,
312         auto_renew                    => $auto_renew,
313         no_auto_renewal_after         => $no_auto_renewal_after,
314         no_auto_renewal_after_hard_limit => $no_auto_renewal_after_hard_limit,
315         reservesallowed               => $reservesallowed,
316         holds_per_record              => $holds_per_record,
317         holds_per_day                 => $holds_per_day,
318         onshelfholds                  => $onshelfholds,
319         opacitemholds                 => $opacitemholds,
320         overduefinescap               => $overduefinescap,
321         cap_fine_to_replacement_price => $cap_fine_to_replacement_price,
322         article_requests              => $article_requests,
323         note                          => $note,
324         decreaseloanholds             => $decreaseloanholds,
325     };
326
327     Koha::CirculationRules->set_rules(
328         {
329             categorycode => $bor eq '*' ? undef : $bor,
330             itemtype     => $itemtype eq '*' ? undef : $itemtype,
331             branchcode   => $br eq '*' ? undef : $br,
332             rules        => $rules,
333         }
334     );
335
336 }
337 elsif ($op eq "set-branch-defaults") {
338     my $categorycode  = $input->param('categorycode');
339     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
340     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
341     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
342     my $holdallowed   = $input->param('holdallowed');
343     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
344     my $returnbranch  = $input->param('returnbranch');
345     my $max_holds = strip_non_numeric( scalar $input->param('max_holds') );
346
347     if ($branch eq "*") {
348         Koha::CirculationRules->set_rules(
349             {
350                 itemtype     => undef,
351                 branchcode   => undef,
352                 rules        => {
353                     holdallowed             => $holdallowed,
354                     hold_fulfillment_policy => $hold_fulfillment_policy,
355                     returnbranch            => $returnbranch,
356                 }
357             }
358         );
359         Koha::CirculationRules->set_rules(
360             {
361                 categorycode => undef,
362                 branchcode   => undef,
363                 rules        => {
364                     patron_maxissueqty             => $patron_maxissueqty,
365                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
366                 }
367             }
368         );
369     } else {
370         Koha::CirculationRules->set_rules(
371             {
372                 itemtype     => undef,
373                 branchcode   => $branch,
374                 rules        => {
375                     holdallowed             => $holdallowed,
376                     hold_fulfillment_policy => $hold_fulfillment_policy,
377                     returnbranch            => $returnbranch,
378                 }
379             }
380         );
381         Koha::CirculationRules->set_rules(
382             {
383                 categorycode => undef,
384                 branchcode   => $branch,
385                 rules        => {
386                     patron_maxissueqty             => $patron_maxissueqty,
387                     patron_maxonsiteissueqty       => $patron_maxonsiteissueqty,
388                 }
389             }
390         );
391     }
392     Koha::CirculationRules->set_rule(
393         {
394             branchcode   => $branch,
395             categorycode => undef,
396             rule_name    => 'max_holds',
397             rule_value   => $max_holds,
398         }
399     );
400 }
401 elsif ($op eq "add-branch-cat") {
402     my $categorycode  = $input->param('categorycode');
403     my $patron_maxissueqty = strip_non_numeric( scalar $input->param('patron_maxissueqty') );
404     my $patron_maxonsiteissueqty = $input->param('patron_maxonsiteissueqty');
405     $patron_maxonsiteissueqty = strip_non_numeric($patron_maxonsiteissueqty);
406     my $max_holds = $input->param('max_holds');
407     $max_holds =~ s/\s//g;
408     $max_holds = undef if $max_holds !~ /^\d+/;
409
410     if ($branch eq "*") {
411         if ($categorycode eq "*") {
412             Koha::CirculationRules->set_rules(
413                 {
414                     categorycode => undef,
415                     branchcode   => undef,
416                     rules        => {
417                         max_holds         => $max_holds,
418                         patron_maxissueqty       => $patron_maxissueqty,
419                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
420                     }
421                 }
422             );
423         } else {
424             Koha::CirculationRules->set_rules(
425                 {
426                     categorycode => $categorycode,
427                     branchcode   => undef,
428                     rules        => {
429                         max_holds         => $max_holds,
430                         patron_maxissueqty       => $patron_maxissueqty,
431                         patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
432                     }
433                 }
434             );
435         }
436     } elsif ($categorycode eq "*") {
437         Koha::CirculationRules->set_rules(
438             {
439                 categorycode => undef,
440                 branchcode   => $branch,
441                 rules        => {
442                     max_holds         => $max_holds,
443                     patron_maxissueqty       => $patron_maxissueqty,
444                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
445                 }
446             }
447         );
448     } else {
449         Koha::CirculationRules->set_rules(
450             {
451                 categorycode => $categorycode,
452                 branchcode   => $branch,
453                 rules        => {
454                     max_holds         => $max_holds,
455                     patron_maxissueqty       => $patron_maxissueqty,
456                     patron_maxonsiteissueqty => $patron_maxonsiteissueqty,
457                 }
458             }
459         );
460     }
461 }
462 elsif ( $op eq "add-open-article-requests-limit" ) {
463     my $categorycode                = $input->param('categorycode');
464     my $open_article_requests_limit = strip_non_numeric( scalar $input->param('open_article_requests_limit') );
465
466     Koha::Exceptions::Exception->throw("No value passed for article request limit")
467       if not defined $open_article_requests_limit # There is a JS check for that
468       || $open_article_requests_limit eq '';
469
470     if ( $branch eq "*" ) {
471         if ( $categorycode eq "*" ) {
472             Koha::CirculationRules->set_rules(
473                 {   categorycode => undef,
474                     branchcode   => undef,
475                     rules        => { open_article_requests_limit => $open_article_requests_limit, }
476                 }
477             );
478         } else {
479             Koha::CirculationRules->set_rules(
480                 {   categorycode => $categorycode,
481                     branchcode   => undef,
482                     rules        => { open_article_requests_limit => $open_article_requests_limit, }
483                 }
484             );
485         }
486     } elsif ( $categorycode eq "*" ) {
487         Koha::CirculationRules->set_rules(
488             {   categorycode => undef,
489                 branchcode   => $branch,
490                 rules        => { open_article_requests_limit => $open_article_requests_limit, }
491             }
492         );
493     } else {
494         Koha::CirculationRules->set_rules(
495             {   categorycode => $categorycode,
496                 branchcode   => $branch,
497                 rules        => { open_article_requests_limit => $open_article_requests_limit, }
498             }
499         );
500     }
501 } elsif ( $op eq 'del-open-article-requests-limit' ) {
502     my $categorycode = $input->param('categorycode');
503     if ( $branch eq "*" ) {
504         if ( $categorycode eq "*" ) {
505             Koha::CirculationRules->set_rules(
506                 {   branchcode   => undef,
507                     categorycode => undef,
508                     rules        => { open_article_requests_limit => undef, }
509                 }
510             );
511         } else {
512             Koha::CirculationRules->set_rules(
513                 {   categorycode => $categorycode,
514                     branchcode   => undef,
515                     rules        => { open_article_requests_limit => undef, }
516                 }
517             );
518         }
519     } elsif ( $categorycode eq "*" ) {
520         Koha::CirculationRules->set_rules(
521             {   branchcode   => $branch,
522                 categorycode => undef,
523                 rules        => { open_article_requests_limit => undef, }
524             }
525         );
526     } else {
527         Koha::CirculationRules->set_rules(
528             {   categorycode => $categorycode,
529                 branchcode   => $branch,
530                 rules        => { open_article_requests_limit => undef, }
531             }
532         );
533     }
534 }
535 elsif ($op eq "add-branch-item") {
536     my $itemtype                = $input->param('itemtype');
537     my $holdallowed             = $input->param('holdallowed');
538     my $hold_fulfillment_policy = $input->param('hold_fulfillment_policy');
539     my $returnbranch            = $input->param('returnbranch');
540
541     if ($branch eq "*") {
542         if ($itemtype eq "*") {
543             Koha::CirculationRules->set_rules(
544                 {
545                     itemtype     => undef,
546                     branchcode   => undef,
547                     rules        => {
548                         holdallowed             => $holdallowed,
549                         hold_fulfillment_policy => $hold_fulfillment_policy,
550                         returnbranch            => $returnbranch,
551                     }
552                 }
553             );
554         } else {
555             Koha::CirculationRules->set_rules(
556                 {
557                     itemtype     => $itemtype,
558                     branchcode   => undef,
559                     rules        => {
560                         holdallowed             => $holdallowed,
561                         hold_fulfillment_policy => $hold_fulfillment_policy,
562                         returnbranch            => $returnbranch,
563                     }
564                 }
565             );
566         }
567     } elsif ($itemtype eq "*") {
568             Koha::CirculationRules->set_rules(
569                 {
570                     itemtype     => undef,
571                     branchcode   => $branch,
572                     rules        => {
573                         holdallowed             => $holdallowed,
574                         hold_fulfillment_policy => $hold_fulfillment_policy,
575                         returnbranch            => $returnbranch,
576                     }
577                 }
578             );
579     } else {
580         Koha::CirculationRules->set_rules(
581             {
582                 itemtype     => $itemtype,
583                 branchcode   => $branch,
584                 rules        => {
585                     holdallowed             => $holdallowed,
586                     hold_fulfillment_policy => $hold_fulfillment_policy,
587                     returnbranch            => $returnbranch,
588                 }
589             }
590         );
591     }
592 }
593 elsif ( $op eq 'mod-refund-lost-item-fee-rule' ) {
594
595     my $lostreturn = $input->param('lostreturn');
596
597     if ( $lostreturn eq '*' ) {
598         if ( $branch ne '*' ) {
599             # only do something for $lostreturn eq '*' if branch-specific
600             Koha::CirculationRules->set_rules(
601                 {
602                     branchcode   => $branch,
603                     rules        => {
604                         lostreturn => undef
605                     }
606                 }
607             );
608         }
609     } else {
610         Koha::CirculationRules->set_rules(
611             {
612                 branchcode   => $branch,
613                 rules        => {
614                     lostreturn => $lostreturn
615                 }
616             }
617         );
618     }
619 }
620
621 my $refundLostItemFeeRule = Koha::CirculationRules->find({ branchcode => ($branch eq '*') ? undef : $branch, rule_name => 'lostreturn' });
622 my $defaultLostItemFeeRule = Koha::CirculationRules->find({ branchcode => undef, rule_name => 'lostreturn' });
623 $template->param(
624     refundLostItemFeeRule => $refundLostItemFeeRule,
625     defaultRefundRule     => $defaultLostItemFeeRule ? $defaultLostItemFeeRule->rule_value : 'refund'
626 );
627
628 my $patron_categories = Koha::Patron::Categories->search({}, { order_by => ['description'] });
629
630 my $itemtypes = Koha::ItemTypes->search_with_localization;
631
632 my $humanbranch = ( $branch ne '*' ? $branch : undef );
633
634 my $all_rules = Koha::CirculationRules->search({ branchcode => $humanbranch });
635 my $definedbranch = $all_rules->count ? 1 : 0;
636
637 my $rules = {};
638 while ( my $r = $all_rules->next ) {
639     $r = $r->unblessed;
640     $rules->{ $r->{categorycode} // '' }->{ $r->{itemtype} // '' }->{ $r->{rule_name} } = $r->{rule_value};
641 }
642
643 $template->param(show_branch_cat_rule_form => 1);
644
645 $template->param(
646     patron_categories => $patron_categories,
647     itemtypeloop      => $itemtypes,
648     humanbranch       => $humanbranch,
649     current_branch    => $branch,
650     definedbranch     => $definedbranch,
651     all_rules         => $rules,
652 );
653 output_html_with_http_headers $input, $cookie, $template->output;
654
655 exit 0;
656
657 # sort by patron category, then item type, putting
658 # default entries at the bottom
659 sub by_category_and_itemtype {
660     unless (by_category($a, $b)) {
661         return by_itemtype($a, $b);
662     }
663 }
664
665 sub by_category {
666     my ($a, $b) = @_;
667     if ($a->{'default_humancategorycode'}) {
668         return ($b->{'default_humancategorycode'} ? 0 : 1);
669     } elsif ($b->{'default_humancategorycode'}) {
670         return -1;
671     } else {
672         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
673     }
674 }
675
676 sub by_itemtype {
677     my ($a, $b) = @_;
678     if ($a->{default_translated_description}) {
679         return ($b->{'default_translated_description'} ? 0 : 1);
680     } elsif ($b->{'default_translated_description'}) {
681         return -1;
682     } else {
683         return lc $a->{'translated_description'} cmp lc $b->{'translated_description'};
684     }
685 }
686
687 sub strip_non_numeric {
688     my $string = shift;
689     $string =~ s/\s//g;
690     $string = '' if $string !~ /^\d+/;
691     return $string;
692 }