X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=tools%2Foverduerules.pl;h=cc90d0bf71883141318f54ba1aac2a0019f40dd4;hb=4b36244c7398c3af9a83293b848b71bbce5cc499;hp=668a69ef03c1ddc786d5965e6bf169250e363c90;hpb=fc6ccb1a616c96e8cddf57efd2195a0bec7bc976;p=koha.git diff --git a/tools/overduerules.pl b/tools/overduerules.pl index 668a69ef03..cc90d0bf71 100755 --- a/tools/overduerules.pl +++ b/tools/overduerules.pl @@ -13,41 +13,70 @@ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use strict; +use warnings; use CGI; use C4::Context; use C4::Output; use C4::Auth; use C4::Koha; -use C4::Branch; # GetBranches +use C4::Branch; use C4::Letters; +use C4::Members; -my $input = new CGI; +our $input = new CGI; my $dbh = C4::Context->dbh; +my @categories = @{$dbh->selectall_arrayref( + 'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0', + { Slice => {} } +)}; +my @category_codes = map { $_->{categorycode} } @categories; +our @rule_params = qw(delay letter debarred); + +# blank_row($category_code) - return true if the entire row is blank. +sub blank_row { + my ($category_code) = @_; + for my $rp (@rule_params) { + for my $n (1 .. 3) { + my $key = "${rp}${n}-$category_code"; + + if (utf8::is_utf8($key)) { + utf8::encode($key); + } + + my $value = $input->param($key); + if ($value) { + return 0; + } + } + } + return 1; +} + my $type=$input->param('type'); my $branch = $input->param('branch'); -$branch="" unless $branch; +$branch ||= q{}; my $op = $input->param('op'); +$op ||= q{}; -# my $flagsrequired; -# $flagsrequired->{circulation}=1; my ($template, $loggedinuser, $cookie) = get_template_and_user({template_name => "tools/overduerules.tmpl", query => $input, type => "intranet", authnotrequired => 0, - flagsrequired => {parameters => 1, tools => 'edit_notice_status_triggers'}, + flagsrequired => { tools => 'edit_notice_status_triggers'}, debug => 1, }); my $err=0; # save the values entered into tables my %temphash; +my $input_saved = 0; if ($op eq 'save') { my @names=$input->param(); my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?"); @@ -57,29 +86,52 @@ if ($op eq 'save') { my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?"); foreach my $key (@names){ # ISSUES - if ($key =~ /(.*)([1-3])-(.*)/) { + if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) { my $type = $1; # data type my $num = $2; # From 1 to 3 my $bor = $3; # borrower category - $temphash{$bor}->{"$type$num"}=$input->param("$key") if (($input->param("$key") ne "") or ($input->param("$key")>0)); + my $value = $input->param($key); + if ($type eq 'delay') { + $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : ''; + } else { + # type is letter + $temphash{$bor}->{"$type$num"} = $value if $value ne ''; + } } } + + # figure out which rows need to be deleted + my @rows_to_delete = grep { blank_row($_) } @category_codes; + foreach my $bor (keys %temphash){ + # get category name if we need it for an error message + my $bor_category = GetBorrowercategory($bor); + my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor; + # Do some Checking here : delay1 < delay2 {delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){ - $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor); + $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name); $err=1; } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){ - $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor); + $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name); $err=1; } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){ - $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor); + $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name); + $err=1; + } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) { + $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name); + $err=1; + } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) { + $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name); + $err=1; + } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) { + $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name); $err=1; }elsif ($temphash{$bor}->{delay3} and ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1}) or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){ - $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor); + $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name); $err=1; } unless ($err){ @@ -90,13 +142,13 @@ if ($op eq 'save') { my $res = $sth_search->fetchrow_hashref(); if ($res->{'total'}>0) { $sth_update->execute( - ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0), + ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef), ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""), ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0), - ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0), + ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef), ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""), ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0), - ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0), + ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef), ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""), ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0), $branch ,$bor @@ -117,46 +169,43 @@ if ($op eq 'save') { } } } - unless ($err) {$template->param(datasaved=>1);} -} -my $branches = GetBranches(); -my @branchloop; -foreach my $thisbranch (keys %$branches) { - my $selected = 1 if $thisbranch eq $branch; - my %row =(value => $thisbranch, - selected => $selected, - branchname => $branches->{$thisbranch}->{'branchname'}, - ); - push @branchloop, \%row; + unless ($err) { + for my $category_code (@rows_to_delete) { + $sth_delete->execute($branch, $category_code); + } + $template->param(datasaved => 1); + $input_saved = 1; + } } +my $branchloop = GetBranchesLoop($branch); my $letters = GetLetters("circulation"); -my $countletters = scalar $letters; +my $countletters = keys %{$letters}; -my $sth=$dbh->prepare("SELECT description,categorycode FROM categories WHERE overduenoticerequired>0 ORDER BY description"); -$sth->execute; my @line_loop; -my $toggle= 1; -# my $i=0; -while (my $data=$sth->fetchrow_hashref){ - if ( $toggle eq 1 ) { - $toggle = 0; - } else { - $toggle = 1; - } - my %row = ( overduename => $data->{'categorycode'}, - toggle => $toggle, - line => $data->{'description'} - ); - if (%temphash){ + +for my $data (@categories) { + my %row = ( + overduename => $data->{'categorycode'}, + line => $data->{'description'} + ); + if (%temphash and not $input_saved){ + # if we managed to save the form submission, don't + # reuse %temphash, but take the values from the + # database - this makes it easier to identify + # bugs where the form submission was not correctly saved for (my $i=1;$i<=3;$i++){ $row{"delay$i"}=$temphash{$data->{'categorycode'}}->{"delay$i"}; $row{"debarred$i"}=$temphash{$data->{'categorycode'}}->{"debarred$i"}; if ($countletters){ my @letterloop; - foreach my $thisletter (keys %$letters) { - my $selected = 1 if $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"}; + foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) { + my $selected; + if ( $temphash{$data->{categorycode}}->{"letter$i"} && + $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"}) { + $selected = 1; + } my %letterrow =(value => $thisletter, selected => $selected, lettername => $letters->{$thisletter}, @@ -177,8 +226,11 @@ while (my $data=$sth->fetchrow_hashref){ for (my $i=1;$i<=3;$i++){ if ($countletters){ my @letterloop; - foreach my $thisletter (keys %$letters) { - my $selected = 1 if $thisletter eq $dat->{"letter$i"}; + foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) { + my $selected; + if ($dat->{"letter$i"} && $thisletter eq $dat->{"letter$i"}) { + $selected = 1; + } my %letterrow =(value => $thisletter, selected => $selected, lettername => $letters->{$thisletter}, @@ -193,13 +245,11 @@ while (my $data=$sth->fetchrow_hashref){ if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};} if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};} } - $sth2->finish; } push @line_loop,\%row; } -$sth->finish; $template->param(table=> \@line_loop, - branchloop => \@branchloop, + branchloop => $branchloop, branch => $branch); output_html_with_http_headers $input, $cookie, $template->output;