X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=admin%2Fsmart-rules.pl;h=f45ea43a2f7ed8c49be724842b9d37ca46f2e96e;hb=b20c00ee2d678c2cdf49115ebb10f02802ada907;hp=2b6a1bec06cf2f2a1c21dcf473c1323c350dd77d;hpb=3e9a94c546bb1907ab38351afddc53f3d8d85b12;p=koha_fer diff --git a/admin/smart-rules.pl b/admin/smart-rules.pl index 2b6a1bec06..f45ea43a2f 100755 --- a/admin/smart-rules.pl +++ b/admin/smart-rules.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -# vim: et ts=4 sw=4 # Copyright 2000-2002 Katipo Communications +# copyright 2010 BibLibre # # This file is part of Koha. # @@ -13,11 +13,12 @@ # 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; @@ -25,14 +26,11 @@ use C4::Auth; use C4::Koha; use C4::Debug; use C4::Branch; # GetBranches +use C4::Dates qw/format_date format_date_in_iso/; -my $input = new CGI; +my $input = CGI->new; my $dbh = C4::Context->dbh; -my $type=$input->param('type'); -my $branch = $input->param('branch') || '*'; -my $op = $input->param('op'); - # my $flagsrequired; # $flagsrequired->{circulation}=1; my ($template, $loggedinuser, $cookie) @@ -40,10 +38,14 @@ my ($template, $loggedinuser, $cookie) query => $input, type => "intranet", authnotrequired => 0, - flagsrequired => {parameters => 1}, + flagsrequired => {parameters => 'manage_circ_rules'}, debug => 1, }); +my $type=$input->param('type'); +my $branch = $input->param('branch') || ( C4::Branch::onlymine() ? ( C4::Branch::mybranch() || '*' ) : '*' ); +my $op = $input->param('op'); + if ($op eq 'delete') { my $itemtype = $input->param('itemtype'); my $categorycode = $input->param('categorycode'); @@ -52,39 +54,300 @@ if ($op eq 'delete') { my $sth_Idelete = $dbh->prepare("delete from issuingrules where branchcode=? and categorycode=? and itemtype=?"); $sth_Idelete->execute($branch, $categorycode, $itemtype); } +elsif ($op eq 'delete-branch-cat') { + my $categorycode = $input->param('categorycode'); + if ($branch eq "*") { + if ($categorycode eq "*") { + my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules"); + $sth_delete->execute(); + } else { + my $sth_delete = $dbh->prepare("DELETE FROM default_borrower_circ_rules + WHERE categorycode = ?"); + $sth_delete->execute($categorycode); + } + } elsif ($categorycode eq "*") { + my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules + WHERE branchcode = ?"); + $sth_delete->execute($branch); + } else { + my $sth_delete = $dbh->prepare("DELETE FROM branch_borrower_circ_rules + WHERE branchcode = ? + AND categorycode = ?"); + $sth_delete->execute($branch, $categorycode); + } +} +elsif ($op eq 'delete-branch-item') { + my $itemtype = $input->param('itemtype'); + if ($branch eq "*") { + if ($itemtype eq "*") { + my $sth_delete = $dbh->prepare("DELETE FROM default_circ_rules"); + $sth_delete->execute(); + } else { + my $sth_delete = $dbh->prepare("DELETE FROM default_branch_item_rules + WHERE itemtype = ?"); + $sth_delete->execute($itemtype); + } + } elsif ($itemtype eq "*") { + my $sth_delete = $dbh->prepare("DELETE FROM default_branch_circ_rules + WHERE branchcode = ?"); + $sth_delete->execute($branch); + } else { + my $sth_delete = $dbh->prepare("DELETE FROM branch_item_rules + WHERE branchcode = ? + AND itemtype = ?"); + $sth_delete->execute($branch, $itemtype); + } +} # save the values entered elsif ($op eq 'add') { - my $sth_search = $dbh->prepare("SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?"); - my $sth_insert = $dbh->prepare("INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, issuelength, fine, firstremind, chargeperiod) VALUES(?,?,?,?,?,?,?,?)"); - my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, firstremind=?, chargeperiod=?, maxissueqty=?, issuelength=? WHERE branchcode=? AND categorycode=? AND itemtype=?"); + my $sth_search = $dbh->prepare('SELECT COUNT(*) AS total FROM issuingrules WHERE branchcode=? AND categorycode=? AND itemtype=?'); + my $sth_insert = $dbh->prepare('INSERT INTO issuingrules (branchcode, categorycode, itemtype, maxissueqty, renewalsallowed, reservesallowed, issuelength, lengthunit, hardduedate, hardduedatecompare, fine, finedays, firstremind, chargeperiod,rentaldiscount, overduefinescap) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); + my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=?, overduefinescap=? WHERE branchcode=? AND categorycode=? AND itemtype=?"); my $br = $branch; # branch my $bor = $input->param('categorycode'); # borrower category my $cat = $input->param('itemtype'); # item type my $fine = $input->param('fine'); + my $finedays = $input->param('finedays'); my $firstremind = $input->param('firstremind'); my $chargeperiod = $input->param('chargeperiod'); my $maxissueqty = $input->param('maxissueqty'); + my $renewalsallowed = $input->param('renewalsallowed'); + my $reservesallowed = $input->param('reservesallowed'); + $maxissueqty =~ s/\s//g; + $maxissueqty = undef if $maxissueqty !~ /^\d+/; my $issuelength = $input->param('issuelength'); + my $lengthunit = $input->param('lengthunit'); + my $hardduedate = $input->param('hardduedate'); + $hardduedate = format_date_in_iso($hardduedate); + my $hardduedatecompare = $input->param('hardduedatecompare'); + my $rentaldiscount = $input->param('rentaldiscount'); + my $overduefinescap = $input->param('overduefinescap') || undef; $debug and warn "Adding $br, $bor, $cat, $fine, $maxissueqty"; $sth_search->execute($br,$bor,$cat); my $res = $sth_search->fetchrow_hashref(); if ($res->{total}) { - $sth_update->execute($fine, $firstremind, $chargeperiod, $maxissueqty,$issuelength,$br,$bor,$cat); + $sth_update->execute($fine, $finedays,$firstremind, $chargeperiod, $maxissueqty, $renewalsallowed,$reservesallowed, $issuelength,$lengthunit, $hardduedate,$hardduedatecompare,$rentaldiscount,$overduefinescap, $br,$bor,$cat); + } else { + $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount,$overduefinescap); + } +} +elsif ($op eq "set-branch-defaults") { + my $categorycode = $input->param('categorycode'); + my $maxissueqty = $input->param('maxissueqty'); + my $holdallowed = $input->param('holdallowed'); + my $returnbranch = $input->param('returnbranch'); + $maxissueqty =~ s/\s//g; + $maxissueqty = undef if $maxissueqty !~ /^\d+/; + $holdallowed =~ s/\s//g; + $holdallowed = undef if $holdallowed !~ /^\d+/; + + if ($branch eq "*") { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_circ_rules"); + my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules + (maxissueqty, holdallowed, returnbranch) + VALUES (?, ?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_circ_rules + SET maxissueqty = ?, holdallowed = ?, returnbranch = ?"); + + $sth_search->execute(); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($maxissueqty, $holdallowed, $returnbranch); + } else { + $sth_insert->execute($maxissueqty, $holdallowed, $returnbranch); + } } else { - $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod); + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_branch_circ_rules + WHERE branchcode = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules + (branchcode, maxissueqty, holdallowed, returnbranch) + VALUES (?, ?, ?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules + SET maxissueqty = ?, holdallowed = ?, returnbranch = ? + WHERE branchcode = ?"); + $sth_search->execute($branch); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($maxissueqty, $holdallowed, $returnbranch, $branch); + } else { + $sth_insert->execute($branch, $maxissueqty, $holdallowed, $returnbranch); + } } } +elsif ($op eq "add-branch-cat") { + my $categorycode = $input->param('categorycode'); + my $maxissueqty = $input->param('maxissueqty'); + $maxissueqty =~ s/\s//g; + $maxissueqty = undef if $maxissueqty !~ /^\d+/; + + if ($branch eq "*") { + if ($categorycode eq "*") { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_circ_rules"); + my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules + (maxissueqty) + VALUES (?)"); + my $sth_update = $dbh->prepare("UPDATE default_circ_rules + SET maxissueqty = ?"); + + $sth_search->execute(); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($maxissueqty); + } else { + $sth_insert->execute($maxissueqty); + } + } else { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_borrower_circ_rules + WHERE categorycode = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO default_borrower_circ_rules + (categorycode, maxissueqty) + VALUES (?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_borrower_circ_rules + SET maxissueqty = ? + WHERE categorycode = ?"); + $sth_search->execute($branch); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($maxissueqty, $categorycode); + } else { + $sth_insert->execute($categorycode, $maxissueqty); + } + } + } elsif ($categorycode eq "*") { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_branch_circ_rules + WHERE branchcode = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules + (branchcode, maxissueqty) + VALUES (?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules + SET maxissueqty = ? + WHERE branchcode = ?"); + $sth_search->execute($branch); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($maxissueqty, $branch); + } else { + $sth_insert->execute($branch, $maxissueqty); + } + } else { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM branch_borrower_circ_rules + WHERE branchcode = ? + AND categorycode = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO branch_borrower_circ_rules + (branchcode, categorycode, maxissueqty) + VALUES (?, ?, ?)"); + my $sth_update = $dbh->prepare("UPDATE branch_borrower_circ_rules + SET maxissueqty = ? + WHERE branchcode = ? + AND categorycode = ?"); + + $sth_search->execute($branch, $categorycode); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($maxissueqty, $branch, $categorycode); + } else { + $sth_insert->execute($branch, $categorycode, $maxissueqty); + } + } +} +elsif ($op eq "add-branch-item") { + my $itemtype = $input->param('itemtype'); + my $holdallowed = $input->param('holdallowed'); + my $returnbranch = $input->param('returnbranch'); + $holdallowed =~ s/\s//g; + $holdallowed = undef if $holdallowed !~ /^\d+/; + + if ($branch eq "*") { + if ($itemtype eq "*") { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_circ_rules"); + my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules + (holdallowed, returnbranch) + VALUES (?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_circ_rules + SET holdallowed = ?, returnbranch = ?"); + + $sth_search->execute(); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($holdallowed, $returnbranch); + } else { + $sth_insert->execute($holdallowed, $returnbranch); + } + } else { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_branch_item_rules + WHERE itemtype = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules + (itemtype, holdallowed, returnbranch) + VALUES (?, ?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules + SET holdallowed = ?, returnbranch = ? + WHERE itemtype = ?"); + $sth_search->execute($itemtype); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($holdallowed, $returnbranch, $itemtype); + } else { + $sth_insert->execute($itemtype, $holdallowed, $returnbranch); + } + } + } elsif ($itemtype eq "*") { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM default_branch_circ_rules + WHERE branchcode = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules + (branchcode, holdallowed, returnbranch) + VALUES (?, ?, ?)"); + my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules + SET holdallowed = ?, returnbranch = ? + WHERE branchcode = ?"); + $sth_search->execute($branch); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($holdallowed, $returnbranch, $branch); + } else { + $sth_insert->execute($branch, $holdallowed, $returnbranch); + } + } else { + my $sth_search = $dbh->prepare("SELECT count(*) AS total + FROM branch_item_rules + WHERE branchcode = ? + AND itemtype = ?"); + my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules + (branchcode, itemtype, holdallowed, returnbranch) + VALUES (?, ?, ?, ?)"); + my $sth_update = $dbh->prepare("UPDATE branch_item_rules + SET holdallowed = ?, returnbranch = ? + WHERE branchcode = ? + AND itemtype = ?"); + + $sth_search->execute($branch, $itemtype); + my $res = $sth_search->fetchrow_hashref(); + if ($res->{total}) { + $sth_update->execute($holdallowed, $returnbranch, $branch, $itemtype); + } else { + $sth_insert->execute($branch, $itemtype, $holdallowed, $returnbranch); + } + } +} + my $branches = GetBranches(); my @branchloop; for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) { - my $selected = 1 if $thisbranch eq $branch; - my %row =(value => $thisbranch, - selected => $selected, - branchname => $branches->{$thisbranch}->{'branchname'}, - ); - push @branchloop, \%row; + push @branchloop, { + value => $thisbranch, + selected => $thisbranch eq $branch, + branchname => $branches->{$thisbranch}->{'branchname'}, + }; } my $sth=$dbh->prepare("SELECT description,categorycode FROM categories ORDER BY description"); @@ -98,7 +361,6 @@ $sth->finish; $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description"); $sth->execute; # $i=0; -my $toggle= 1; my @row_loop; my @itemtypes; while (my $row=$sth->fetchrow_hashref){ @@ -117,23 +379,127 @@ my $sth2 = $dbh->prepare(" $sth2->execute($branch); while (my $row = $sth2->fetchrow_hashref) { + $row->{'current_branch'} ||= $row->{'branchcode'}; $row->{'humanitemtype'} ||= $row->{'itemtype'}; $row->{'default_humanitemtype'} = 1 if $row->{'humanitemtype'} eq '*'; $row->{'humancategorycode'} ||= $row->{'categorycode'}; $row->{'default_humancategorycode'} = 1 if $row->{'humancategorycode'} eq '*'; $row->{'fine'} = sprintf('%.2f', $row->{'fine'}); + if ($row->{'hardduedate'} ne '0000-00-00') { + $row->{'hardduedate'} = format_date( $row->{'hardduedate'}); + $row->{'hardduedatebefore'} = 1 if ($row->{'hardduedatecompare'} == -1); + $row->{'hardduedateexact'} = 1 if ($row->{'hardduedatecompare'} == 0); + $row->{'hardduedateafter'} = 1 if ($row->{'hardduedatecompare'} == 1); + } else { + $row->{'hardduedate'} = 0; + } push @row_loop, $row; } $sth->finish; my @sorted_row_loop = sort by_category_and_itemtype @row_loop; +my $sth_branch_cat; +if ($branch eq "*") { + $sth_branch_cat = $dbh->prepare(" + SELECT default_borrower_circ_rules.*, categories.description AS humancategorycode + FROM default_borrower_circ_rules + JOIN categories USING (categorycode) + + "); + $sth_branch_cat->execute(); +} else { + $sth_branch_cat = $dbh->prepare(" + SELECT branch_borrower_circ_rules.*, categories.description AS humancategorycode + FROM branch_borrower_circ_rules + JOIN categories USING (categorycode) + WHERE branch_borrower_circ_rules.branchcode = ? + "); + $sth_branch_cat->execute($branch); +} + +my @branch_cat_rules = (); +while (my $row = $sth_branch_cat->fetchrow_hashref) { + push @branch_cat_rules, $row; +} +my @sorted_branch_cat_rules = sort { $a->{'humancategorycode'} cmp $b->{'humancategorycode'} } @branch_cat_rules; + +# note undef maxissueqty so that template can deal with them +foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) { + $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty}); +} + +@sorted_row_loop = sort by_category_and_itemtype @row_loop; + +my $sth_branch_item; +if ($branch eq "*") { + $sth_branch_item = $dbh->prepare(" + SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype + FROM default_branch_item_rules + JOIN itemtypes USING (itemtype) + "); + $sth_branch_item->execute(); +} else { + $sth_branch_item = $dbh->prepare(" + SELECT branch_item_rules.*, itemtypes.description AS humanitemtype + FROM branch_item_rules + JOIN itemtypes USING (itemtype) + WHERE branch_item_rules.branchcode = ? + "); + $sth_branch_item->execute($branch); +} + +my @branch_item_rules = (); +while (my $row = $sth_branch_item->fetchrow_hashref) { + push @branch_item_rules, $row; +} +my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules; + +# note undef holdallowed so that template can deal with them +foreach my $entry (@sorted_branch_item_rules) { + $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2); + $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1); +} + +$template->param(show_branch_cat_rule_form => 1); +$template->param(branch_item_rule_loop => \@sorted_branch_item_rules); +$template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules); + +my $sth_defaults; +if ($branch eq "*") { + $sth_defaults = $dbh->prepare(" + SELECT * + FROM default_circ_rules + "); + $sth_defaults->execute(); +} else { + $sth_defaults = $dbh->prepare(" + SELECT * + FROM default_branch_circ_rules + WHERE branchcode = ? + "); + $sth_defaults->execute($branch); +} + +my $defaults = $sth_defaults->fetchrow_hashref; + +if ($defaults) { + $template->param(default_holdallowed_none => 1) if($defaults->{holdallowed} == 0); + $template->param(default_holdallowed_same => 1) if($defaults->{holdallowed} == 1); + $template->param(default_holdallowed_any => 1) if($defaults->{holdallowed} == 2); + $template->param(default_maxissueqty => $defaults->{maxissueqty}); + $template->param(default_returnbranch => $defaults->{returnbranch}); +} + +$template->param(default_rules => ($defaults ? 1 : 0)); + $template->param(categoryloop => \@category_loop, itemtypeloop => \@itemtypes, rules => \@sorted_row_loop, branchloop => \@branchloop, humanbranch => ($branch ne '*' ? $branches->{$branch}->{branchname} : ''), - branch => $branch + current_branch => $branch, + definedbranch => scalar(@sorted_row_loop)>0 ); output_html_with_http_headers $input, $cookie, $template->output;