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
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
27 use C4::Branch; # GetBranches
30 my $dbh = C4::Context->dbh;
32 my $type=$input->param('type');
33 my $branch = $input->param('branch') || '*';
34 my $op = $input->param('op');
37 # $flagsrequired->{circulation}=1;
38 my ($template, $loggedinuser, $cookie)
39 = get_template_and_user({template_name => "admin/smart-rules.tmpl",
43 flagsrequired => {parameters => 1},
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 # save the values entered
56 elsif ($op eq 'add') {
57 my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?");
58 my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, issuelength, fine, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?)");
59 my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?");
61 my $br = $branch; # branch
62 my $bor = $input->param('categorycode'); # borrower category
63 my $cat = $input->param('itemtype'); # item type
64 my $fine = $input->param('fine');
65 my $firstremind = $input->param('firstremind');
66 my $chargeperiod = $input->param('chargeperiod');
67 my $maxissueqty = $input->param('maxissueqty');
68 my $issuelength = $input->param('issuelength');
69 $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty";
71 $sth_search->execute($br,$bor,$cat);
72 my $res = $sth_search->fetchrow_hashref();
74 $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
76 $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
79 my $branches = GetBranches();
81 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
82 my $selected = 1 if $thisbranch eq $branch;
83 my %row =(value => $thisbranch,
84 selected => $selected,
85 branchname => $branches->{$thisbranch}->{'branchname'},
87 push @branchloop, \%row;
90 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
93 while (my $data=$sth->fetchrow_hashref){
94 push @category_loop,$data;
98 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
104 while (my $row=$sth->fetchrow_hashref){
105 push @itemtypes,$row;
108 my $sth2 = $dbh->prepare("
109 SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
112 ON (itemtypes.itemtype = issuingrules.itemtype)
114 ON (categories.categorycode = issuingrules.categorycode)
115 WHERE issuingrules.branchcode = ?
117 $sth2->execute($branch);
119 while (my $row = $sth2->fetchrow_hashref) {
120 $row->{'humanitemtype'} ||= $row->{'itemtype'};
121 $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*';
122 $row->{'humancategorycode'} ||= $row->{'categorycode'};
123 $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*';
124 $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
125 push @row_loop, $row;
128 $template->param(categoryloop => \@category_loop,
129 itemtypeloop => \@itemtypes,
131 branchloop => \@branchloop,
132 humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
135 output_html_with_http_headers $input, $cookie, $template->output;