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