bug 1891 followup - remove dangling /TMPL_IF
[koha.git] / admin / smart-rules.pl
blob558bd42813ead920420d816404386e7e7cd74b54
1 #!/usr/bin/perl
2 # vim: et ts=4 sw=4
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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
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
29 my $input = new CGI;
30 my $dbh = C4::Context->dbh;
32 my $type=$input->param('type');
33 my $branch = $input->param('branch') || '*';
34 my $op = $input->param('op');
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 });
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";
52 my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?");
53 $sth_Idelete->execute($branch, $categorycode, $itemtype);
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);
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);
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, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?)");
81 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
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 $firstremind = $input->param('firstremind');
88 my $chargeperiod = $input->param('chargeperiod');
89 my $maxissueqty = $input->param('maxissueqty');
90 $maxissueqty =~ s/\s//g;
91 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
92 my $issuelength = $input->param('issuelength');
93 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
95 $sth_search->execute($br,$bor,$cat);
96 my $res = $sth_search->fetchrow_hashref();
97 if ($res->{total}) {
98 $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
99 } else {
100 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
103 elsif ($op eq "add-branch-cat") {
104 my $categorycode = $input->param('categorycode');
105 my $maxissueqty = $input->param('maxissueqty');
106 $maxissueqty =~ s/\s//g;
107 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
109 if ($branch eq "*") {
110 if ($categorycode eq "*") {
111 my $sth_search = $dbh->prepare("SELECT count(*) AS total
112 FROM default_circ_rules");
113 my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
114 (maxissueqty)
115 VALUES (?)");
116 my $sth_update = $dbh->prepare("UPDATE default_circ_rules
117 SET maxissueqty = ?");
119 $sth_search->execute();
120 my $res = $sth_search->fetchrow_hashref();
121 if ($res->{total}) {
122 $sth_update->execute($maxissueqty);
123 } else {
124 $sth_insert->execute($maxissueqty);
126 } else {
127 my $sth_search = $dbh->prepare("SELECT count(*) AS total
128 FROM default_borrower_circ_rules
129 WHERE categorycode = ?");
130 my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules
131 (categorycode, maxissueqty)
132 VALUES (?, ?)");
133 my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules
134 SET maxissueqty = ?
135 WHERE categorycode = ?");
136 $sth_search->execute($branch);
137 my $res = $sth_search->fetchrow_hashref();
138 if ($res->{total}) {
139 $sth_update->execute($maxissueqty, $categorycode);
140 } else {
141 $sth_insert->execute($categorycode, $maxissueqty);
144 } elsif ($categorycode eq "*") {
145 my $sth_search = $dbh->prepare("SELECT count(*) AS total
146 FROM default_branch_circ_rules
147 WHERE branchcode = ?");
148 my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
149 (branchcode, maxissueqty)
150 VALUES (?, ?)");
151 my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
152 SET maxissueqty = ?
153 WHERE branchcode = ?");
154 $sth_search->execute($branch);
155 my $res = $sth_search->fetchrow_hashref();
156 if ($res->{total}) {
157 $sth_update->execute($maxissueqty, $branch);
158 } else {
159 $sth_insert->execute($branch, $maxissueqty);
161 } else {
162 my $sth_search = $dbh->prepare("SELECT count(*) AS total
163 FROM branch_borrower_circ_rules
164 WHERE branchcode = ?
165 AND categorycode = ?");
166 my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
167 (branchcode, categorycode, maxissueqty)
168 VALUES (?, ?, ?)");
169 my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
170 SET maxissueqty = ?
171 WHERE branchcode = ?
172 AND categorycode = ?");
174 $sth_search->execute($branch, $categorycode);
175 my $res = $sth_search->fetchrow_hashref();
176 if ($res->{total}) {
177 $sth_update->execute($maxissueqty, $branch, $categorycode);
178 } else {
179 $sth_insert->execute($branch, $categorycode, $maxissueqty);
184 my $branches = GetBranches();
185 my @branchloop;
186 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
187 my $selected = 1 if $thisbranch eq $branch;
188 my %row =(value => $thisbranch,
189 selected => $selected,
190 branchname => $branches->{$thisbranch}->{'branchname'},
192 push @branchloop, \%row;
195 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
196 $sth->execute;
197 my @category_loop;
198 while (my $data=$sth->fetchrow_hashref){
199 push @category_loop,$data;
202 $sth->finish;
203 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
204 $sth->execute;
205 # $i=0;
206 my $toggle= 1;
207 my @row_loop;
208 my @itemtypes;
209 while (my $row=$sth->fetchrow_hashref){
210 push @itemtypes,$row;
213 my $sth2 = $dbh->prepare("
214 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
215 FROM issuingrules
216 LEFT JOIN itemtypes
217 ON (itemtypes.itemtype = issuingrules.itemtype)
218 LEFT JOIN categories
219 ON (categories.categorycode = issuingrules.categorycode)
220 WHERE issuingrules.branchcode = ?
222 $sth2->execute($branch);
224 while (my $row = $sth2->fetchrow_hashref) {
225 $row->{'humanitemtype'} ||= $row->{'itemtype'};
226 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
227 $row->{'humancategorycode'} ||= $row->{'categorycode'};
228 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
229 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
230 push @row_loop, $row;
232 $sth->finish;
234 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
236 my $sth_branch_cat;
237 if ($branch eq "*") {
238 $sth_branch_cat = $dbh->prepare("
239 SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode
240 FROM default_borrower_circ_rules
241 JOIN categories USING (categorycode)
244 $sth_branch_cat->execute();
245 } else {
246 $sth_branch_cat = $dbh->prepare("
247 SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
248 FROM branch_borrower_circ_rules
249 JOIN categories USING (categorycode)
250 WHERE branch_borrower_circ_rules.branchcode = ?
252 $sth_branch_cat->execute($branch);
255 my @branch_cat_rules = ();
256 while (my $row = $sth_branch_cat->fetchrow_hashref) {
257 push @branch_cat_rules, $row;
259 my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
261 my $sth_branch_default;
262 if ($branch eq "*") {
263 # add global default
264 $sth_branch_default = $dbh->prepare("SELECT maxissueqty
265 FROM default_circ_rules");
266 $sth_branch_default->execute();
267 } else {
268 # add default for branch
269 $sth_branch_default = $dbh->prepare("SELECT maxissueqty
270 FROM default_branch_circ_rules
271 WHERE branchcode = ?");
272 $sth_branch_default->execute($branch);
275 if (my ($default_maxissueqty) = $sth_branch_default->fetchrow_array()) {
276 push @sorted_branch_cat_rules, {
277 default_humancategorycode => 1,
278 categorycode => '*',
279 maxissueqty => $default_maxissueqty,
283 # note undef maxissueqty so that template can deal with them
284 foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
285 $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
288 $template->param(show_branch_cat_rule_form => 1);
289 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
291 $template->param(categoryloop => \@category_loop,
292 itemtypeloop => \@itemtypes,
293 rules => \@sorted_row_loop,
294 branchloop => \@branchloop,
295 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
296 branch => $branch
298 output_html_with_http_headers $input, $cookie, $template->output;
300 exit 0;
302 # sort by patron category, then item type, putting
303 # default entries at the bottom
304 sub by_category_and_itemtype {
305 unless (by_category($a, $b)) {
306 return by_itemtype($a, $b);
310 sub by_category {
311 my ($a, $b) = @_;
312 if ($a->{'default_humancategorycode'}) {
313 return ($b->{'default_humancategorycode'} ? 0 : 1);
314 } elsif ($b->{'default_humancategorycode'}) {
315 return -1;
316 } else {
317 return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
321 sub by_itemtype {
322 my ($a, $b) = @_;
323 if ($a->{'default_humanitemtype'}) {
324 return ($b->{'default_humanitemtype'} ? 0 : 1);
325 } elsif ($b->{'default_humanitemtype'}) {
326 return -1;
327 } else {
328 return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};