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