Adding getletters to Koha.pm :
[koha.git] / tools / overduerules.pl
1 #!/usr/bin/perl
2
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::Database;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Interface::CGI::Output;
29 use HTML::Template;
30
31 my $input = new CGI;
32 my $dbh = C4::Context->dbh;
33
34 my $type=$input->param('type');
35 my $branch = $input->param('branch');
36 $branch="" unless $branch;
37 my $op = $input->param('op');
38
39 # my $flagsrequired;
40 # $flagsrequired->{circulation}=1;
41 my ($template, $loggedinuser, $cookie)
42     = get_template_and_user({template_name => "tools/overduerules.tmpl",
43                              query => $input,
44                              type => "intranet",
45                              authnotrequired => 0,
46                              flagsrequired => {parameters => 1, management => 1},
47                               debug => 1,
48                              });
49 # save the values entered
50 if ($op eq 'save') {
51         my @names=$input->param();
52         my $sth_search = $dbh->prepare("select count(*) as total from overduerules where branchcode=? and categorycode=?");
53
54         my $sth_insert = $dbh->prepare("insert into overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
55         my $sth_update=$dbh->prepare("Update overduerules set delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? where branchcode=? and categorycode=?");
56         my $sth_delete=$dbh->prepare("delete from overduerules where branchcode=? and categorycode=?");
57         my %temphash;
58         foreach my $key (@names){
59                 # ISSUES
60                 if ($key =~ /(.*)([1-3])-(.*)/) {
61                         my $type = $1; # data type
62                         my $num = $2; # From 1 to 3
63                         my $bor = $3; # borrower category
64                         $temphash{$bor}->{"$type$num"}=$input->param("$key");
65                 }
66         }
67         foreach my $bor (keys %temphash){
68                 $sth_search->execute($branch,$bor);
69                 my $res = $sth_search->fetchrow_hashref();
70                 if ($res->{'total'}>0) {
71                                         $sth_update->execute(
72                                                 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
73                                                 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
74                                                 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
75                                                 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
76                                                 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
77                                                 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
78                                                 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
79                                                 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
80                                                 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
81                                                 $branch ,$bor
82                                                 );
83                                 } else {
84 #                                       warn "insert  overduenotice1: $data[0],delay1: $data[1], letter1 : $data[2],debarred1 : $data[3], notice2 : $data[4], delay2 : $data[5], letter2 : $data[6],debarred2 : $data[7], 3 : $data[8],3 : $data[9],3 : $data[10],3 : $data[11], $br ,$bor ";
85                                         $sth_insert->execute($branch,$bor,
86                                                 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
87                                                 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
88                                                 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
89                                                 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
90                                                 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
91                                                 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
92                                                 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
93                                                 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
94                                                 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
95                                                 );
96                                 }
97         }
98
99 }
100 my $branches = getbranches;
101 my @branchloop;
102 foreach my $thisbranch (keys %$branches) {
103         my $selected = 1 if $thisbranch eq $branch;
104         my %row =(value => $thisbranch,
105                                 selected => $selected,
106                                 branchname => $branches->{$thisbranch}->{'branchname'},
107                         );
108         push @branchloop, \%row;
109 }
110
111 my ($countletters,$letters) = getletters("circulation");
112
113 my $sth=$dbh->prepare("Select description,categorycode from categories where overduenoticerequired>0 order by description");
114 $sth->execute;
115 my @line_loop;
116 my $toggle= 1;
117 # my $i=0;
118 while (my $data=$sth->fetchrow_hashref){
119         if ( $toggle eq 1 ) {
120                 $toggle = 0;
121         } else {
122                 $toggle = 1;
123         }
124         my %row = ( overduename => $data->{'categorycode'},
125                                 toggle => $toggle,
126                                 line => $data->{'description'}
127                                 );
128         my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? and categorycode=?");
129         $sth2->execute($branch,$data->{'categorycode'});
130         my $dat=$sth2->fetchrow_hashref;
131 #       foreach my $test (keys %$dat){
132 #               warn "$test : ".$dat->{$test};
133 #       }
134         for (my $i=1;$i<=3;$i++){
135                 if ($countletters){
136                         my @letterloop;
137                         foreach my $thisletter (keys %$letters) {
138                                 my $selected = 1 if $thisletter eq $dat->{"letter$i"};
139                                 my %letterrow =(value => $thisletter,
140                                                         selected => $selected,
141                                                         lettername => $letters->{$thisletter},
142                                                 );
143                                 push @letterloop, \%letterrow;
144                         }
145                         $row{"letterloop$i"}=\@letterloop;
146                 } else {
147                         $row{"noletter"}=1;
148                         if ($dat->{"letter$i"}){$row{"letter$i"}=$dat->{"letter$i"};}
149                 }
150                 if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};}
151                 if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};}
152         }
153         $sth2->finish;
154         push @line_loop,\%row;
155 }
156 $sth->finish;
157
158 $template->param(table=> \@line_loop,
159                                                 branchloop => \@branchloop,
160                                                 branch => $branch);
161 output_html_with_http_headers $input, $cookie, $template->output;