a3cdff9815ccc5211d147c47d85adb3054157b5c
[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, issuelength, fine, finedays, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?)");
103     my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, 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     $maxissueqty =~ s/\s//g;
114     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
115     my $issuelength  = $input->param('issuelength');
116     $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
117
118     $sth_search->execute($br,$bor,$cat);
119     my $res = $sth_search->fetchrow_hashref();
120     if ($res->{total}) {
121         $sth_update->execute($fine, $finedays, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
122     } else {
123         $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$finedays,$firstremind,$chargeperiod);
124     }
125
126 elsif ($op eq "set-branch-defaults") {
127     my $categorycode  = $input->param('categorycode');
128     my $maxissueqty   = $input->param('maxissueqty');
129     my $holdallowed   = $input->param('holdallowed');
130     $maxissueqty =~ s/\s//g;
131     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
132     $holdallowed =~ s/\s//g;
133     $holdallowed = undef if $holdallowed !~ /^\d+/;
134
135     if ($branch eq "*") {
136         my $sth_search = $dbh->prepare("SELECT count(*) AS total
137                                         FROM default_circ_rules");
138         my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
139                                         (maxissueqty, holdallowed)
140                                         VALUES (?, ?)");
141         my $sth_update = $dbh->prepare("UPDATE default_circ_rules
142                                         SET maxissueqty = ?, holdallowed = ?");
143
144         $sth_search->execute();
145         my $res = $sth_search->fetchrow_hashref();
146         if ($res->{total}) {
147             $sth_update->execute($maxissueqty, $holdallowed);
148         } else {
149             $sth_insert->execute($maxissueqty, $holdallowed);
150         }
151     } else {
152         my $sth_search = $dbh->prepare("SELECT count(*) AS total
153                                         FROM default_branch_circ_rules
154                                         WHERE branchcode = ?");
155         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
156                                         (branchcode, maxissueqty, holdallowed)
157                                         VALUES (?, ?, ?)");
158         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
159                                         SET maxissueqty = ?, holdallowed = ?
160                                         WHERE branchcode = ?");
161         $sth_search->execute($branch);
162         my $res = $sth_search->fetchrow_hashref();
163         if ($res->{total}) {
164             $sth_update->execute($maxissueqty, $holdallowed, $branch);
165         } else {
166             $sth_insert->execute($branch, $maxissueqty, $holdallowed);
167         }
168     }
169 }
170 elsif ($op eq "add-branch-cat") {
171     my $categorycode  = $input->param('categorycode');
172     my $maxissueqty   = $input->param('maxissueqty');
173     $maxissueqty =~ s/\s//g;
174     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
175
176     if ($branch eq "*") {
177         if ($categorycode eq "*") {
178             my $sth_search = $dbh->prepare("SELECT count(*) AS total
179                                             FROM default_circ_rules");
180             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
181                                             (maxissueqty)
182                                             VALUES (?)");
183             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
184                                             SET maxissueqty = ?");
185
186             $sth_search->execute();
187             my $res = $sth_search->fetchrow_hashref();
188             if ($res->{total}) {
189                 $sth_update->execute($maxissueqty);
190             } else {
191                 $sth_insert->execute($maxissueqty);
192             }
193         } else {
194             my $sth_search = $dbh->prepare("SELECT count(*) AS total
195                                             FROM default_borrower_circ_rules
196                                             WHERE categorycode = ?");
197             my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
198                                             (categorycode, maxissueqty)
199                                             VALUES (?, ?)");
200             my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
201                                             SET maxissueqty = ?
202                                             WHERE categorycode = ?");
203             $sth_search->execute($branch);
204             my $res = $sth_search->fetchrow_hashref();
205             if ($res->{total}) {
206                 $sth_update->execute($maxissueqty, $categorycode);
207             } else {
208                 $sth_insert->execute($categorycode, $maxissueqty);
209             }
210         }
211     } elsif ($categorycode eq "*") {
212         my $sth_search = $dbh->prepare("SELECT count(*) AS total
213                                         FROM default_branch_circ_rules
214                                         WHERE branchcode = ?");
215         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
216                                         (branchcode, maxissueqty)
217                                         VALUES (?, ?)");
218         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
219                                         SET maxissueqty = ?
220                                         WHERE branchcode = ?");
221         $sth_search->execute($branch);
222         my $res = $sth_search->fetchrow_hashref();
223         if ($res->{total}) {
224             $sth_update->execute($maxissueqty, $branch);
225         } else {
226             $sth_insert->execute($branch, $maxissueqty);
227         }
228     } else {
229         my $sth_search = $dbh->prepare("SELECT count(*) AS total
230                                         FROM branch_borrower_circ_rules
231                                         WHERE branchcode = ?
232                                         AND   categorycode = ?");
233         my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
234                                         (branchcode, categorycode, maxissueqty)
235                                         VALUES (?, ?, ?)");
236         my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
237                                         SET maxissueqty = ?
238                                         WHERE branchcode = ?
239                                         AND categorycode = ?");
240
241         $sth_search->execute($branch, $categorycode);
242         my $res = $sth_search->fetchrow_hashref();
243         if ($res->{total}) {
244             $sth_update->execute($maxissueqty, $branch, $categorycode);
245         } else {
246             $sth_insert->execute($branch, $categorycode, $maxissueqty);
247         }
248     }
249 }
250 elsif ($op eq "add-branch-item") {
251     my $itemtype  = $input->param('itemtype');
252     my $holdallowed   = $input->param('holdallowed');
253     $holdallowed =~ s/\s//g;
254     $holdallowed = undef if $holdallowed !~ /^\d+/;
255
256     if ($branch eq "*") {
257         if ($itemtype eq "*") {
258             my $sth_search = $dbh->prepare("SELECT count(*) AS total
259                                             FROM default_circ_rules");
260             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
261                                             (holdallowed)
262                                             VALUES (?)");
263             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
264                                             SET holdallowed = ?");
265
266             $sth_search->execute();
267             my $res = $sth_search->fetchrow_hashref();
268             if ($res->{total}) {
269                 $sth_update->execute($holdallowed);
270             } else {
271                 $sth_insert->execute($holdallowed);
272             }
273         } else {
274             my $sth_search = $dbh->prepare("SELECT count(*) AS total
275                                             FROM default_branch_item_rules
276                                             WHERE itemtype = ?");
277             my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
278                                             (itemtype, holdallowed)
279                                             VALUES (?, ?)");
280             my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
281                                             SET holdallowed = ?
282                                             WHERE itemtype = ?");
283             $sth_search->execute($itemtype);
284             my $res = $sth_search->fetchrow_hashref();
285             if ($res->{total}) {
286                 $sth_update->execute($holdallowed, $itemtype);
287             } else {
288                 $sth_insert->execute($itemtype, $holdallowed);
289             }
290         }
291     } elsif ($itemtype eq "*") {
292         my $sth_search = $dbh->prepare("SELECT count(*) AS total
293                                         FROM default_branch_circ_rules
294                                         WHERE branchcode = ?");
295         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
296                                         (branchcode, holdallowed)
297                                         VALUES (?, ?)");
298         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
299                                         SET holdallowed = ?
300                                         WHERE branchcode = ?");
301         $sth_search->execute($branch);
302         my $res = $sth_search->fetchrow_hashref();
303         if ($res->{total}) {
304             $sth_update->execute($holdallowed, $branch);
305         } else {
306             $sth_insert->execute($branch, $holdallowed);
307         }
308     } else {
309         my $sth_search = $dbh->prepare("SELECT count(*) AS total
310                                         FROM branch_item_rules
311                                         WHERE branchcode = ?
312                                         AND   itemtype = ?");
313         my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
314                                         (branchcode, itemtype, holdallowed)
315                                         VALUES (?, ?, ?)");
316         my $sth_update = $dbh->prepare("UPDATE branch_item_rules
317                                         SET holdallowed = ?
318                                         WHERE branchcode = ?
319                                         AND itemtype = ?");
320
321         $sth_search->execute($branch, $itemtype);
322         my $res = $sth_search->fetchrow_hashref();
323         if ($res->{total}) {
324             $sth_update->execute($holdallowed, $branch, $itemtype);
325         } else {
326             $sth_insert->execute($branch, $itemtype, $holdallowed);
327         }
328     }
329 }
330
331 my $branches = GetBranches();
332 my @branchloop;
333 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
334     my $selected = 1 if $thisbranch eq $branch;
335     my %row =(value => $thisbranch,
336                 selected => $selected,
337                 branchname => $branches->{$thisbranch}->{'branchname'},
338             );
339     push @branchloop, \%row;
340 }
341
342 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
343 $sth->execute;
344 my @category_loop;
345 while (my $data=$sth->fetchrow_hashref){
346     push @category_loop,$data;
347 }
348
349 $sth->finish;
350 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
351 $sth->execute;
352 # $i=0;
353 my @row_loop;
354 my @itemtypes;
355 while (my $row=$sth->fetchrow_hashref){
356     push @itemtypes,$row;
357 }
358
359 my $sth2 = $dbh->prepare("
360     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
361     FROM issuingrules
362     LEFT JOIN itemtypes
363         ON (itemtypes.itemtype = issuingrules.itemtype)
364     LEFT JOIN categories
365         ON (categories.categorycode = issuingrules.categorycode)
366     WHERE issuingrules.branchcode = ?
367 ");
368 $sth2->execute($branch);
369
370 while (my $row = $sth2->fetchrow_hashref) {
371     $row->{'humanitemtype'} ||= $row->{'itemtype'};
372     $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
373     $row->{'humancategorycode'} ||= $row->{'categorycode'};
374     $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
375     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
376     push @row_loop, $row;
377 }
378 $sth->finish;
379
380 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
381
382 my $sth_branch_cat;
383 if ($branch eq "*") {
384     $sth_branch_cat = $dbh->prepare("
385         SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
386         FROM default_borrower_circ_rules
387         JOIN categories USING (categorycode)
388         
389     ");
390     $sth_branch_cat->execute();
391 } else {
392     $sth_branch_cat = $dbh->prepare("
393         SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
394         FROM branch_borrower_circ_rules
395         JOIN categories USING (categorycode)
396         WHERE branch_borrower_circ_rules.branchcode = ?
397     ");
398     $sth_branch_cat->execute($branch);
399 }
400
401 my @branch_cat_rules = ();
402 while (my $row = $sth_branch_cat->fetchrow_hashref) {
403     push @branch_cat_rules, $row;
404 }
405 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
406
407 # note undef maxissueqty so that template can deal with them
408 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
409     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
410 }
411
412 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
413
414 my $sth_branch_item;
415 if ($branch eq "*") {
416     $sth_branch_item = $dbh->prepare("
417         SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
418         FROM default_branch_item_rules
419         JOIN itemtypes USING (itemtype)
420     ");
421     $sth_branch_item->execute();
422 } else {
423     $sth_branch_item = $dbh->prepare("
424         SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
425         FROM branch_item_rules
426         JOIN itemtypes USING (itemtype)
427         WHERE branch_item_rules.branchcode = ?
428     ");
429     $sth_branch_item->execute($branch);
430 }
431
432 my @branch_item_rules = ();
433 while (my $row = $sth_branch_item->fetchrow_hashref) {
434     push @branch_item_rules, $row;
435 }
436 my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
437
438 # note undef holdallowed so that template can deal with them
439 foreach my $entry (@sorted_branch_item_rules) {
440     $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
441     $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
442 }
443
444 $template->param(show_branch_cat_rule_form => 1);
445 $template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
446 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
447
448 my $sth_defaults;
449 if ($branch eq "*") {
450     $sth_defaults = $dbh->prepare("
451         SELECT *
452         FROM default_circ_rules
453     ");
454     $sth_defaults->execute();
455 } else {
456     $sth_defaults = $dbh->prepare("
457         SELECT *
458         FROM default_branch_circ_rules
459         WHERE branchcode = ?
460     ");
461     $sth_defaults->execute($branch);
462 }
463
464 my $defaults = $sth_defaults->fetchrow_hashref;
465
466 if ($defaults) {
467     $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0);
468     $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1);
469     $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2);
470     $template->param(default_maxissueqty => $defaults->{maxissueqty});
471 }
472
473 $template->param(default_rules => ($defaults ? 1 : 0));
474
475 $template->param(categoryloop => \@category_loop,
476                         itemtypeloop => \@itemtypes,
477                         rules => \@sorted_row_loop,
478                         branchloop => \@branchloop,
479                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
480                         branch => $branch
481                         );
482 output_html_with_http_headers $input, $cookie, $template->output;
483
484 exit 0;
485
486 # sort by patron category, then item type, putting
487 # default entries at the bottom
488 sub by_category_and_itemtype {
489     unless (by_category($a, $b)) {
490         return by_itemtype($a, $b);
491     }
492 }
493
494 sub by_category {
495     my ($a, $b) = @_;
496     if ($a->{'default_humancategorycode'}) {
497         return ($b->{'default_humancategorycode'} ? 0 : 1);
498     } elsif ($b->{'default_humancategorycode'}) {
499         return -1;
500     } else {
501         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
502     }
503 }
504
505 sub by_itemtype {
506     my ($a, $b) = @_;
507     if ($a->{'default_humanitemtype'}) {
508         return ($b->{'default_humanitemtype'} ? 0 : 1);
509     } elsif ($b->{'default_humanitemtype'}) {
510         return -1;
511     } else {
512         return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};
513     }
514 }