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