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