Bug 5549 : DateUtils add subroutine format_sqldatetime
[koha.git] / admin / smart-rules.pl
index 40e9084..5b19313 100755 (executable)
@@ -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.
 #
 # 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; FIXME - Bug 2505
 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 $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)
@@ -44,6 +42,10 @@ my ($template, $loggedinuser, $cookie)
                             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');
@@ -98,28 +100,36 @@ elsif ($op eq 'delete-branch-item') {
 }
 # 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) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
+    my $sth_update=$dbh->prepare("UPDATE issuingrules SET fine=?, finedays=?, firstremind=?, chargeperiod=?, maxissueqty=?, renewalsallowed=?, reservesallowed=?, issuelength=?, lengthunit = ?, hardduedate=?, hardduedatecompare=?, rentaldiscount=?  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');
     $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, $br,$bor,$cat);
     } else {
-        $sth_insert->execute($br,$bor,$cat,$maxissueqty,$issuelength,$fine,$firstremind,$chargeperiod);
+        $sth_insert->execute($br,$bor,$cat,$maxissueqty,$renewalsallowed,$reservesallowed,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$fine,$finedays,$firstremind,$chargeperiod,$rentaldiscount);
     }
 } 
 elsif ($op eq "set-branch-defaults") {
@@ -330,12 +340,11 @@ elsif ($op eq "add-branch-item") {
 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");
@@ -367,11 +376,20 @@ 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;
@@ -408,7 +426,7 @@ foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
 }
 
-my @sorted_row_loop = sort by_category_and_itemtype @row_loop;
+@sorted_row_loop = sort by_category_and_itemtype @row_loop;
 
 my $sth_branch_item;
 if ($branch eq "*") {
@@ -476,7 +494,8 @@ $template->param(categoryloop => \@category_loop,
                         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;