New framework handling. New koha_attr table for mapping MARC to koha
[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::Auth;
25 use C4::Output;
26 use C4::Koha;
27 use C4::Interface::CGI::Output;
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 $branch="" unless $branch;
35 my $op = $input->param('op');
36
37 # my $flagsrequired;
38 # $flagsrequired->{circulation}=1;
39 my ($template, $loggedinuser, $cookie)
40     = get_template_and_user({template_name => "admin/issuingrules.tmpl",
41                              query => $input,
42                              type => "intranet",
43                              authnotrequired => 0,
44                              flagsrequired => {parameters => 1, management => 1},
45                               debug => 1,
46                              });
47 # save the values entered
48 if ($op eq 'save') {
49         my @names=$input->param();
50         my $sth_search = $dbh->prepare("select count(*) as total from issuingrules where branchcode=? and categorycode=? and itemtype=?");
51
52         my $sth_Finsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,fine,firstremind,chargeperiod) values (?,?,?,?,?,?)");
53         my $sth_Fupdate=$dbh->prepare("Update issuingrules set fine=?,firstremind=?,chargeperiod=? where branchcode=? and categorycode=? and itemtype=?");
54         my $sth_Fdelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and issuelength=0");
55
56         my $sth_Iinsert = $dbh->prepare("insert into issuingrules (branchcode,categorycode,itemtype,maxissueqty,issuelength) values (?,?,?,?,?)");
57         my $sth_Iupdate=$dbh->prepare("Update issuingrules set maxissueqty=?, issuelength=? where branchcode=? and categorycode=? and itemtype=?");
58         my $sth_Idelete=$dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=? and fine=0");
59         foreach my $key (@names){
60                 # ISSUES
61                 if ($key =~ /I-(.*)-(.*)\.(.*)/) {
62                         my $br = $1; # branch
63                         my $bor = $2; # borrower category
64                         my $cat = $3; # item type
65                         my $data=$input->param($key);
66                         my ($issuelength,$maxissueqty)=split(',',$data);
67 #                       if ($maxissueqty >0) {
68                                 $sth_search->execute($br,$bor,$cat);
69                                 my $res = $sth_search->fetchrow_hashref();
70                                 if ($res->{total}) {
71                                         $sth_Iupdate->execute($maxissueqty,$issuelength,$br,$bor,$cat);
72                                 } else {
73                                         $sth_Iinsert->execute($br,$bor,$cat,$maxissueqty,$issuelength);
74                                 }
75 #                       } else {
76 #                               $sth_Idelete->execute($br,$bor,$cat);
77 #                       }
78                 }
79                 # FINES
80                 if ($key =~ /F-(.*)-(.*)\.(.*)/) {
81                         my $br = $1; # branch
82                         my $bor = $2; # borrower category
83                         my $cat = $3; # item type
84                         my $data=$input->param($key);
85                         my ($fine,$firstremind,$chargeperiod)=split(',',$data);
86 #                       if ($fine >0) {
87                                 $sth_search->execute($br,$bor,$cat);
88                                 my $res = $sth_search->fetchrow_hashref();
89                                 if ($res->{total}) {
90                                         $sth_Fupdate->execute($fine,$firstremind,$chargeperiod,$br,$bor,$cat);
91                                 } else {
92                                         $sth_Finsert->execute($br,$bor,$cat,$fine,$firstremind,$chargeperiod);
93                                 }
94 #                       } else {
95 #                               $sth_Fdelete->execute($br,$bor,$cat);
96 #                       }
97                 }
98         }
99
100 }
101 my $branches = getbranches;
102 my @branchloop;
103 foreach my $thisbranch (keys %$branches) {
104         my $selected = 1 if $thisbranch eq $branch;
105         my %row =(value => $thisbranch,
106                                 selected => $selected,
107                                 branchname => $branches->{$thisbranch}->{'branchname'},
108                         );
109         push @branchloop, \%row;
110 }
111
112 my $sth=$dbh->prepare("Select description,categorycode from categories order by description");
113 $sth->execute;
114  my @trow3;
115 my @title_loop;
116 # my $i=0;
117 while (my $data=$sth->fetchrow_hashref){
118         my %row = (in_title => $data->{'description'});
119         push @title_loop,\%row;
120         push @trow3,$data->{'categorycode'};
121 #       $i++;
122 }
123
124 my %row = (in_title => "*");
125 push @title_loop, \%row;
126 push @trow3,'*';
127
128 $sth->finish;
129 $sth=$dbh->prepare("Select description,itemtype from itemtypes order by description");
130 $sth->execute;
131 # $i=0;
132 my $toggle= 1;
133 my @row_loop;
134 my @itemtypes;
135 while (my $row=$sth->fetchrow_hashref){
136         push @itemtypes,\$row;
137 }
138 my $line;
139 $line->{itemtype} = "*";
140 $line->{description} = "*";
141 push @itemtypes,\$line;
142
143 foreach my $data (@itemtypes) {
144         my @trow2;
145         my @cell_loop;
146         if ( $toggle eq 1 ) {
147                 $toggle = 0;
148         } else {
149                 $toggle = 1;
150         }
151         for (my $i=0;$i<=$#trow3;$i++){
152                 my $sth2=$dbh->prepare("select * from issuingrules where branchcode=? and categorycode=? and itemtype=?");
153                 $sth2->execute($branch,$trow3[$i],$$data->{'itemtype'});
154                 my $dat=$sth2->fetchrow_hashref;
155                 $sth2->finish;
156                 my $fine=$dat->{'fine'}+0;
157                 my $maxissueqty = $dat->{'maxissueqty'}+0;
158                 my $issuelength = $dat->{'issuelength'}+0;
159                 my $finesvalue;
160                 $finesvalue= "$fine,$dat->{'firstremind'},$dat->{'chargeperiod'}" if $fine+$dat->{'firstremind'}+$dat->{'chargeperiod'}>0;
161                 my $issuingvalue;
162 #               if ($maxissueqty>0) {
163                     $issuingvalue = "$issuelength,$maxissueqty" if $issuelength+$maxissueqty>0;
164 #               }
165 #               else {          
166 #                   $issuingvalue = "$issuelength, 5";
167 #                   $maxissueqty = 5;
168 #               }
169                 my %row = (finesname=> "F-$branch-$trow3[$i].$$data->{'itemtype'}",
170                                         finesvalue => $finesvalue,
171                                         issuingname => "I-$branch-$trow3[$i].$$data->{itemtype}",
172                                         issuingvalue => $issuingvalue,
173                                         toggle => $toggle,
174                                         );
175                 push @cell_loop,\%row;
176         }
177         my %row = (categorycode => $$data->{description},
178                                         cell =>\@cell_loop);
179         push @row_loop, \%row;
180 }
181
182 $sth->finish;
183 $template->param(title => \@title_loop,
184                                                 row => \@row_loop,
185                                                 branchloop => \@branchloop,
186                                                 branch => $branch);
187 output_html_with_http_headers $input, $cookie, $template->output;