Experimental rewrite of Stop Words administration screen. Feedback welcomed.
[koha.git] / admin / issuingrules.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 => "admin/issuingrules.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 issuingrules where branchcode=? and categorycode=? and itemtype=?");
53
54         my $sth_Finsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,fine,firstremind,chargeperiod) values (?,?,?,?,?,?)");
55         my $sth_Fupdate=$dbh->prepare("Update issuingrules set fine=?,firstremind=?,chargeperiod=? where branchcode=? and categorycode=? and itemtype=?");
56         my $sth_Fdelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and issuelength=0");
57
58         my $sth_Iinsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,maxissueqty,issuelength) values (?,?,?,?,?)");
59         my $sth_Iupdate=$dbh->prepare("Update issuingrules set maxissueqty=?, issuelength=? where branchcode=? and categorycode=? and itemtype=?");
60         my $sth_Idelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and fine=0");
61         foreach my $key (@names){
62                 # ISSUES
63                 if ($key =~ /I-(.*)-(.*)\.(.*)/) {
64                         my $br = $1; # branch
65                         my $bor = $2; # borrower category
66                         my $cat = $3; # item type
67                         my $data=$input->param($key);
68                         my ($issuelength,$maxissueqty)=split(',',$data);
69 #                       if ($maxissueqty >0) {
70                                 $sth_search->execute($br,$bor,$cat);
71                                 my $res = $sth_search->fetchrow_hashref();
72                                 if ($res->{total}) {
73                                         $sth_Iupdate->execute($maxissueqty,$issuelength,$br,$bor,$cat);
74                                 } else {
75                                         $sth_Iinsert->execute($br,$bor,$cat,$maxissueqty,$issuelength);
76                                 }
77 #                       } else {
78 #                               $sth_Idelete->execute($br,$bor,$cat);
79 #                       }
80                 }
81                 # FINES
82                 if ($key =~ /F-(.*)-(.*)\.(.*)/) {
83                         my $br = $1; # branch
84                         my $bor = $2; # borrower category
85                         my $cat = $3; # item type
86                         my $data=$input->param($key);
87                         my ($fine,$firstremind,$chargeperiod)=split(',',$data);
88 #                       if ($fine >0) {
89                                 $sth_search->execute($br,$bor,$cat);
90                                 my $res = $sth_search->fetchrow_hashref();
91                                 if ($res->{total}) {
92                                         $sth_Fupdate->execute($fine,$firstremind,$chargeperiod,$br,$bor,$cat);
93                                 } else {
94                                         $sth_Finsert->execute($br,$bor,$cat,$fine,$firstremind,$chargeperiod);
95                                 }
96 #                       } else {
97 #                               $sth_Fdelete->execute($br,$bor,$cat);
98 #                       }
99                 }
100         }
101
102 }
103 my $branches = getbranches;
104 my @branchloop;
105 foreach my $thisbranch (keys %$branches) {
106         my $selected = 1 if $thisbranch eq $branch;
107         my %row =(value => $thisbranch,
108                                 selected => $selected,
109                                 branchname => $branches->{$thisbranch}->{'branchname'},
110                         );
111         push @branchloop, \%row;
112 }
113
114 my $sth=$dbh->prepare("Select description,categorycode from categories order by description");
115 $sth->execute;
116  my @trow3;
117 my @title_loop;
118 # my $i=0;
119 while (my $data=$sth->fetchrow_hashref){
120         my %row = (in_title => $data->{'description'});
121         push @title_loop,\%row;
122         push @trow3,$data->{'categorycode'};
123 #       $i++;
124 }
125
126 my %row = (in_title => "*");
127 push @title_loop, \%row;
128 push @trow3,'*';
129
130 $sth->finish;
131 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
132 $sth->execute;
133 # $i=0;
134 my $toggle= 1;
135 my @row_loop;
136 my @itemtypes;
137 while (my $row=$sth->fetchrow_hashref){
138         push @itemtypes,\$row;
139 }
140 my $line;
141 $line->{itemtype} = "*";
142 $line->{description} = "*";
143 push @itemtypes,\$line;
144
145 foreach my $data (@itemtypes) {
146         my @trow2;
147         my @cell_loop;
148         if ( $toggle eq 1 ) {
149                 $toggle = 0;
150         } else {
151                 $toggle = 1;
152         }
153         for (my $i=0;$i<=$#trow3;$i++){
154                 my $sth2=$dbh->prepare("select * from issuingrules where branchcode=? and categorycode=? and itemtype=?");
155                 $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
156                 my $dat=$sth2->fetchrow_hashref;
157                 $sth2->finish;
158                 my $fine=$dat->{'fine'}+0;
159                 my $maxissueqty = $dat->{'maxissueqty'}+0;
160                 my $issuelength = $dat->{'issuelength'}+0;
161                 my $finesvalue;
162                 $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine+$dat->{'firstremind'}+$dat->{'chargeperiod'}>0;
163                 my $issuingvalue;
164 #               if ($maxissueqty>0) {
165                     $issuingvalue = "$issuelength,$maxissueqty" if $issuelength+$maxissueqty>0;
166 #               }
167 #               else {          
168 #                   $issuingvalue = "$issuelength, 5";
169 #                   $maxissueqty = 5;
170 #               }
171                 my %row = (finesname=> "F-$branch-$trow3[$i].$$data->{'itemtype'}",
172                                         finesvalue => $finesvalue,
173                                         issuingname => "I-$branch-$trow3[$i].$$data->{itemtype}",
174                                         issuingvalue => $issuingvalue,
175                                         toggle => $toggle,
176                                         );
177                 push @cell_loop,\%row;
178         }
179         my %row = (categorycode => $$data->{description},
180                                         cell =>\@cell_loop);
181         push @row_loop, \%row;
182 }
183
184 $sth->finish;
185 $template->param(title => \@title_loop,
186                                                 row => \@row_loop,
187                                                 branchloop => \@branchloop,
188                                                 branch => $branch);
189 output_html_with_http_headers $input, $cookie, $template->output;