Bug 3418: Fixes the alpha order of the system preference tags.
[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 # save the values entered
78 elsif ($op eq 'add') {
79     my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
80     my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, issuelength, fine, finedays, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?,?)");
81     my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
82     
83     my $br = $branch; # branch
84     my $bor  = $input->param('categorycode'); # borrower category
85     my $cat  = $input->param('itemtype');     # item type
86     my $fine = $input->param('fine');
87     my $finedays     = $input->param('finedays');
88     my $firstremind  = $input->param('firstremind');
89     my $chargeperiod = $input->param('chargeperiod');
90     my $maxissueqty  = $input->param('maxissueqty');
91     $maxissueqty =~ s/\s//g;
92     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
93     my $issuelength  = $input->param('issuelength');
94     $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
95
96     $sth_search->execute($br,$bor,$cat);
97     my $res = $sth_search->fetchrow_hashref();
98     if ($res->{total}) {
99         $sth_update->execute($fine, $finedays, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
100     } else {
101         $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$finedays,$firstremind,$chargeperiod);
102     }
103
104 elsif ($op eq "add-branch-cat") {
105     my $categorycode  = $input->param('categorycode');
106     my $maxissueqty   = $input->param('maxissueqty');
107     $maxissueqty =~ s/\s//g;
108     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
109
110     if ($branch eq "*") {
111         if ($categorycode eq "*") {
112             my $sth_search = $dbh->prepare("SELECT count(*) AS total
113                                             FROM default_circ_rules");
114             my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
115                                             (maxissueqty)
116                                             VALUES (?)");
117             my $sth_update = $dbh->prepare("UPDATE default_circ_rules
118                                             SET maxissueqty = ?");
119
120             $sth_search->execute();
121             my $res = $sth_search->fetchrow_hashref();
122             if ($res->{total}) {
123                 $sth_update->execute($maxissueqty);
124             } else {
125                 $sth_insert->execute($maxissueqty);
126             }
127         } else {
128             my $sth_search = $dbh->prepare("SELECT count(*) AS total
129                                             FROM default_borrower_circ_rules
130                                             WHERE categorycode = ?");
131             my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
132                                             (categorycode, maxissueqty)
133                                             VALUES (?, ?)");
134             my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
135                                             SET maxissueqty = ?
136                                             WHERE categorycode = ?");
137             $sth_search->execute($branch);
138             my $res = $sth_search->fetchrow_hashref();
139             if ($res->{total}) {
140                 $sth_update->execute($maxissueqty, $categorycode);
141             } else {
142                 $sth_insert->execute($categorycode, $maxissueqty);
143             }
144         }
145     } elsif ($categorycode eq "*") {
146         my $sth_search = $dbh->prepare("SELECT count(*) AS total
147                                         FROM default_branch_circ_rules
148                                         WHERE branchcode = ?");
149         my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
150                                         (branchcode, maxissueqty)
151                                         VALUES (?, ?)");
152         my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
153                                         SET maxissueqty = ?
154                                         WHERE branchcode = ?");
155         $sth_search->execute($branch);
156         my $res = $sth_search->fetchrow_hashref();
157         if ($res->{total}) {
158             $sth_update->execute($maxissueqty, $branch);
159         } else {
160             $sth_insert->execute($branch, $maxissueqty);
161         }
162     } else {
163         my $sth_search = $dbh->prepare("SELECT count(*) AS total
164                                         FROM branch_borrower_circ_rules
165                                         WHERE branchcode = ?
166                                         AND   categorycode = ?");
167         my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
168                                         (branchcode, categorycode, maxissueqty)
169                                         VALUES (?, ?, ?)");
170         my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
171                                         SET maxissueqty = ?
172                                         WHERE branchcode = ?
173                                         AND categorycode = ?");
174
175         $sth_search->execute($branch, $categorycode);
176         my $res = $sth_search->fetchrow_hashref();
177         if ($res->{total}) {
178             $sth_update->execute($maxissueqty, $branch, $categorycode);
179         } else {
180             $sth_insert->execute($branch, $categorycode, $maxissueqty);
181         }
182     }
183 }
184
185 my $branches = GetBranches();
186 my @branchloop;
187 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
188     my $selected = 1 if $thisbranch eq $branch;
189     my %row =(value => $thisbranch,
190                 selected => $selected,
191                 branchname => $branches->{$thisbranch}->{'branchname'},
192             );
193     push @branchloop, \%row;
194 }
195
196 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
197 $sth->execute;
198 my @category_loop;
199 while (my $data=$sth->fetchrow_hashref){
200     push @category_loop,$data;
201 }
202
203 $sth->finish;
204 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
205 $sth->execute;
206 # $i=0;
207 my $toggle= 1;
208 my @row_loop;
209 my @itemtypes;
210 while (my $row=$sth->fetchrow_hashref){
211     push @itemtypes,$row;
212 }
213
214 my $sth2 = $dbh->prepare("
215     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
216     FROM issuingrules
217     LEFT JOIN itemtypes
218         ON (itemtypes.itemtype = issuingrules.itemtype)
219     LEFT JOIN categories
220         ON (categories.categorycode = issuingrules.categorycode)
221     WHERE issuingrules.branchcode = ?
222 ");
223 $sth2->execute($branch);
224
225 while (my $row = $sth2->fetchrow_hashref) {
226     $row->{'humanitemtype'} ||= $row->{'itemtype'};
227     $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
228     $row->{'humancategorycode'} ||= $row->{'categorycode'};
229     $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
230     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
231     push @row_loop, $row;
232 }
233 $sth->finish;
234
235 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
236
237 my $sth_branch_cat;
238 if ($branch eq "*") {
239     $sth_branch_cat = $dbh->prepare("
240         SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
241         FROM default_borrower_circ_rules
242         JOIN categories USING (categorycode)
243         
244     ");
245     $sth_branch_cat->execute();
246 } else {
247     $sth_branch_cat = $dbh->prepare("
248         SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
249         FROM branch_borrower_circ_rules
250         JOIN categories USING (categorycode)
251         WHERE branch_borrower_circ_rules.branchcode = ?
252     ");
253     $sth_branch_cat->execute($branch);
254 }
255
256 my @branch_cat_rules = ();
257 while (my $row = $sth_branch_cat->fetchrow_hashref) {
258     push @branch_cat_rules, $row;
259 }
260 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
261
262 my $sth_branch_default;
263 if ($branch eq "*") {
264     # add global default
265     $sth_branch_default = $dbh->prepare("SELECT maxissueqty 
266                                          FROM default_circ_rules");
267     $sth_branch_default->execute();
268 } else {
269     # add default for branch
270     $sth_branch_default = $dbh->prepare("SELECT maxissueqty 
271                                          FROM default_branch_circ_rules
272                                          WHERE branchcode = ?");
273     $sth_branch_default->execute($branch);
274 }
275
276 if (my ($default_maxissueqty) = $sth_branch_default->fetchrow_array()) {
277     push @sorted_branch_cat_rules, { 
278                                       default_humancategorycode => 1,
279                                       categorycode => '*',
280                                       maxissueqty => $default_maxissueqty,
281                                     };
282 }
283
284 # note undef maxissueqty so that template can deal with them
285 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
286     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
287 }
288
289 $template->param(show_branch_cat_rule_form => 1);
290 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
291
292 $template->param(categoryloop => \@category_loop,
293                         itemtypeloop => \@itemtypes,
294                         rules => \@sorted_row_loop,
295                         branchloop => \@branchloop,
296                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
297                         branch => $branch
298                         );
299 output_html_with_http_headers $input, $cookie, $template->output;
300
301 exit 0;
302
303 # sort by patron category, then item type, putting
304 # default entries at the bottom
305 sub by_category_and_itemtype {
306     unless (by_category($a, $b)) {
307         return by_itemtype($a, $b);
308     }
309 }
310
311 sub by_category {
312     my ($a, $b) = @_;
313     if ($a->{'default_humancategorycode'}) {
314         return ($b->{'default_humancategorycode'} ? 0 : 1);
315     } elsif ($b->{'default_humancategorycode'}) {
316         return -1;
317     } else {
318         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
319     }
320 }
321
322 sub by_itemtype {
323     my ($a, $b) = @_;
324     if ($a->{'default_humanitemtype'}) {
325         return ($b->{'default_humanitemtype'} ? 0 : 1);
326     } elsif ($b->{'default_humanitemtype'}) {
327         return -1;
328     } else {
329         return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};
330     }
331 }