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