bug 2000 - add total loan limit to alt. issuing rules
[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 $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules
57                                     WHERE branchcode = ?
58                                     AND categorycode = ?");
59     my $categorycode  = $input->param('categorycode');
60     $sth_delete->execute($branch, $categorycode);
61 }
62 # save the values entered
63 elsif ($op eq 'add') {
64     my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
65     my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, issuelength, fine, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?)");
66     my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
67     
68     my $br = $branch; # branch
69     my $bor  = $input->param('categorycode'); # borrower category
70     my $cat  = $input->param('itemtype');     # item type
71     my $fine = $input->param('fine');
72     my $firstremind  = $input->param('firstremind');
73     my $chargeperiod = $input->param('chargeperiod');
74     my $maxissueqty  = $input->param('maxissueqty');
75     my $issuelength  = $input->param('issuelength');
76     $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
77
78     $sth_search->execute($br,$bor,$cat);
79     my $res = $sth_search->fetchrow_hashref();
80     if ($res->{total}) {
81         $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
82     } else {
83         $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
84     }
85
86 elsif ($op eq "add-branch-cat") {
87     my $sth_search = $dbh->prepare("SELECT count(*) AS total
88                                     FROM branch_borrower_circ_rules
89                                     WHERE branchcode = ?
90                                     AND   categorycode = ?");
91     my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules
92                                     (branchcode, categorycode, maxissueqty)
93                                     VALUES (?, ?, ?)");
94     my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules
95                                     SET maxissueqty = ?
96                                     WHERE branchcode = ?
97                                     AND categorycode = ?");
98
99     my $categorycode  = $input->param('categorycode');
100     my $maxissueqty   = $input->param('maxissueqty');
101     $maxissueqty =~ s/\s//g;
102     $maxissueqty = undef if $maxissueqty !~ /^\d+/;
103
104     $sth_search->execute($branch, $categorycode);
105     my $res = $sth_search->fetchrow_hashref();
106     if ($res->{total}) {
107         $sth_update->execute($maxissueqty, $branch, $categorycode);
108     } else {
109         $sth_insert->execute($branch, $categorycode, $maxissueqty);
110     }
111 }
112
113 my $branches = GetBranches();
114 my @branchloop;
115 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
116     my $selected = 1 if $thisbranch eq $branch;
117     my %row =(value => $thisbranch,
118                 selected => $selected,
119                 branchname => $branches->{$thisbranch}->{'branchname'},
120             );
121     push @branchloop, \%row;
122 }
123
124 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
125 $sth->execute;
126 my @category_loop;
127 while (my $data=$sth->fetchrow_hashref){
128     push @category_loop,$data;
129 }
130
131 $sth->finish;
132 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
133 $sth->execute;
134 # $i=0;
135 my $toggle= 1;
136 my @row_loop;
137 my @itemtypes;
138 while (my $row=$sth->fetchrow_hashref){
139     push @itemtypes,$row;
140 }
141
142 my $sth2 = $dbh->prepare("
143     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
144     FROM issuingrules
145     LEFT JOIN itemtypes
146         ON (itemtypes.itemtype = issuingrules.itemtype)
147     LEFT JOIN categories
148         ON (categories.categorycode = issuingrules.categorycode)
149     WHERE issuingrules.branchcode = ?
150 ");
151 $sth2->execute($branch);
152
153 while (my $row = $sth2->fetchrow_hashref) {
154     $row->{'humanitemtype'} ||= $row->{'itemtype'};
155     $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
156     $row->{'humancategorycode'} ||= $row->{'categorycode'};
157     $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
158     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
159     push @row_loop, $row;
160 }
161 $sth->finish;
162
163 my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
164
165 my $sth_branch_cat = $dbh->prepare("
166     SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode
167     FROM branch_borrower_circ_rules
168     JOIN categories USING (categorycode)
169     WHERE branch_borrower_circ_rules.branchcode = ?
170 ");
171 if ($branch ne "*") {
172     $sth_branch_cat->execute($branch);
173     my @branch_cat_rules = ();
174     while (my $row = $sth_branch_cat->fetchrow_hashref) {
175         push @branch_cat_rules, $row;
176     }
177     my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules;
178     $template->param(show_branch_cat_rule_form => 1);
179     $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
180 }
181
182 $template->param(categoryloop => \@category_loop,
183                         itemtypeloop => \@itemtypes,
184                         rules => \@sorted_row_loop,
185                         branchloop => \@branchloop,
186                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
187                         branch => $branch
188                         );
189 output_html_with_http_headers $input, $cookie, $template->output;
190
191 exit 0;
192
193 # sort by patron category, then item type, putting
194 # default entries at the bottom
195 sub by_category_and_itemtype {
196     unless (by_category($a, $b)) {
197         return by_itemtype($a, $b);
198     }
199 }
200
201 sub by_category {
202     my ($a, $b) = @_;
203     if ($a->{'default_humancategorycode'}) {
204         return ($b->{'default_humancategorycode'} ? 0 : 1);
205     } elsif ($b->{'default_humancategorycode'}) {
206         return -1;
207     } else {
208         return $a->{'humancategorycode'} cmp $b->{'humancategorycode'};
209     }
210 }
211
212 sub by_itemtype {
213     my ($a, $b) = @_;
214     if ($a->{'default_humanitemtype'}) {
215         return ($b->{'default_humanitemtype'} ? 0 : 1);
216     } elsif ($b->{'default_humanitemtype'}) {
217         return -1;
218     } else {
219         return $a->{'humanitemtype'} cmp $b->{'humanitemtype'};
220     }
221 }