add a kinda config file for Makefile.PL
[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 =~ /delete-(.+)-(.+)/) {
48     my $itemtype = $1;
49     my $categorycode = $2;
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 # 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=?");
60     
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";
70
71     $sth_search->execute($br,$bor,$cat);
72     my $res = $sth_search->fetchrow_hashref();
73     if ($res->{total}) {
74         $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat);
75     } else {
76         $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
77     }
78 }
79 my $branches = GetBranches();
80 my @branchloop;
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'},
86             );
87     push @branchloop, \%row;
88 }
89
90 my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description");
91 $sth->execute;
92 my @category_loop;
93 while (my $data=$sth->fetchrow_hashref){
94     push @category_loop,$data;
95 }
96
97 my %row = (categorycode => "*", description => 'Any');
98 push @category_loop, \%row;
99
100 $sth->finish;
101 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
102 $sth->execute;
103 # $i=0;
104 my $toggle= 1;
105 my @row_loop;
106 my @itemtypes;
107 while (my $row=$sth->fetchrow_hashref){
108     push @itemtypes,$row;
109 }
110 my %row = (itemtype => '*', description => 'Any');
111 push @itemtypes,\%row;
112
113 my $sth2 = $dbh->prepare("
114     SELECT issuingrules.*, itemtypes.description AS humanitemtype, categories.description AS humancategorycode
115     FROM issuingrules
116     LEFT JOIN itemtypes
117         ON (itemtypes.itemtype = issuingrules.itemtype)
118     LEFT JOIN categories
119         ON (categories.categorycode = issuingrules.categorycode)
120     WHERE issuingrules.branchcode = ?
121 ");
122 $sth2->execute($branch);
123
124 while (my $row = $sth2->fetchrow_hashref) {
125     $row->{'humanitemtype'} ||= $row->{'itemtype'};
126     $row->{'humanitemtype'} = 'Any' if $row->{'humanitemtype'} eq '*';
127     $row->{'humancategorycode'} ||= $row->{'categorycode'};
128     $row->{'humancategorycode'} = 'Any' if $row->{'humancategorycode'} eq '*';
129     $row->{'fine'} = sprintf('%.2f', $row->{'fine'});
130     push @row_loop, $row;
131 }
132 $sth->finish;
133 $template->param(categoryloop => \@category_loop,
134                         itemtypeloop => \@itemtypes,
135                         rules => \@row_loop,
136                         branchloop => \@branchloop,
137                         humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''),
138                         branch => $branch
139                         );
140 output_html_with_http_headers $input, $cookie, $template->output;