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