more logs for MARC::Charset problems
[koha.git] / C4 / Overdues.pm
index a22483a..812c5f3 100644 (file)
@@ -1,6 +1,5 @@
 package C4::Overdues;
 
-# $Id$
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -20,40 +19,21 @@ package C4::Overdues;
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
-require Exporter;
-use C4::Context;
 use Date::Calc qw/Today/;
-use vars qw($VERSION @ISA @EXPORT);
-use C4::Accounts;
 use Date::Manip qw/UnixDate/;
+use C4::Context;
+use C4::Accounts;
 use C4::Log; # logaction
 
-# set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g; 
-shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
-
-=head1 NAME
-
-C4::Circulation::Fines - Koha module dealing with fines
-
-=head1 SYNOPSIS
-
-  use C4::Overdues;
-
-=head1 DESCRIPTION
-
-This module contains several functions for dealing with fines for
-overdue items. It is primarily used by the 'misc/fines2.pl' script.
-
-=head1 FUNCTIONS
-
-=over 2
-
-=cut
+use vars qw($VERSION @ISA @EXPORT);
 
-@ISA    = qw(Exporter);
-# subs to rename (and maybe merge some...)
-push @EXPORT, qw(
+BEGIN {
+       # set the version for version checking
+       $VERSION = 3.01;
+       require Exporter;
+       @ISA    = qw(Exporter);
+       # subs to rename (and maybe merge some...)
+       push @EXPORT, qw(
         &CalcFine
         &Getoverdues
         &checkoverdues
@@ -76,39 +56,53 @@ push @EXPORT, qw(
         &GetOverduesForBranch
         &RemoveNotifyLine
         &AddNotifyLine
-);
-# subs to remove
-push @EXPORT, qw(
+       );
+       # subs to remove
+       push @EXPORT, qw(
         &BorType
-);
+       );
 
-#
-# All subs to move : check that an equivalent don't exist already before moving
-#
+       # check that an equivalent don't exist already before moving
 
-# subs to move to Circulation.pm
-push @EXPORT, qw(
+       # subs to move to Circulation.pm
+       push @EXPORT, qw(
         &GetIssuingRules
         &GetIssuesIteminfo
-);
-# subs to move to Members.pm
-push @EXPORT, qw(
+       );
+       # subs to move to Members.pm
+       push @EXPORT, qw(
         &CheckBorrowerDebarred
         &UpdateBorrowerDebarred
-);
-# subs to move to Biblio.pm
-push @EXPORT, qw(
+       );
+       # subs to move to Biblio.pm
+       push @EXPORT, qw(
         &GetItems
         &ReplacementCost
-);
+       );
+}
 
-=item Getoverdues
+=head1 NAME
+
+C4::Circulation::Fines - Koha module dealing with fines
+
+=head1 SYNOPSIS
+
+  use C4::Overdues;
 
-  ($count, $overdues) = &Getoverdues();
+=head1 DESCRIPTION
 
-Returns the list of all overdue books.
+This module contains several functions for dealing with fines for
+overdue items. It is primarily used by the 'misc/fines2.pl' script.
 
-C<$count> is the number of elements in C<@{$overdues}>.
+=head1 FUNCTIONS
+
+=over 2
+
+=item Getoverdues
+
+  ($overdues) = &Getoverdues();
+
+Returns the list of all overdue books, with their itemtype.
 
 C<$overdues> is a reference-to-array. Each element is a
 reference-to-hash whose keys are the fields of the issues table in the
@@ -119,10 +113,20 @@ Koha database.
 #'
 sub Getoverdues {
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare(
-        "Select * from issues where date_due < now() and returndate is
-  NULL order by borrowernumber "
-    );
+    my $sth =  (C4::context->preference('item-level_itypes')) ? 
+                               $dbh->prepare(
+                               "SELECT issues.*,items.itype as itemtype FROM issues 
+                       LEFT JOIN items USING (itemnumber)
+                       WHERE date_due < now() 
+                           AND returndate IS NULL ORDER BY borrowernumber " )
+                               :
+                               $dbh->prepare(
+                    "SELECT issues.*,biblioitems.itemtype,items.itype FROM issues 
+                    LEFT JOIN items USING (itemnumber)
+                    LEFT JOIN biblioitems USING (biblioitemnumber)
+                    WHERE date_due < now() 
+                        AND returndate IS 
+                        NULL ORDER BY borrowernumber " );
     $sth->execute;
 
     my @results;
@@ -220,20 +224,36 @@ or "Final Notice".
 
 #'
 sub CalcFine {
-    my ( $itemnumber, $bortype, $difference , $dues  ) = @_;
+    my ( $item, $bortype, $difference , $dues  ) = @_;
     my $dbh = C4::Context->dbh;
-    my $data = GetIssuingRules($itemnumber,$bortype);
     my $amount = 0;
     my $printout;
-    my $countspecialday=&GetSpecialHolidays($dues,$itemnumber);
-    my $countrepeatableday=&GetRepeatableHolidays($dues,$itemnumber,$difference);    
+    # calculate how many days the patron is late
+    my $countspecialday=&GetSpecialHolidays($dues,$item->{itemnumber});
+    my $countrepeatableday=&GetRepeatableHolidays($dues,$item->{itemnumber},$difference);    
     my $countalldayclosed = $countspecialday + $countrepeatableday;
-    my $daycount = $difference - $countalldayclosed;    
+    my $daycount = $difference - $countalldayclosed;
+    # get issuingrules (fines part will be used)
+    my $data = GetIssuingRules($item->{'itemtype'},$bortype);
     my $daycounttotal = $daycount - $data->{'firstremind'};
+    if ($data->{'chargeperiod'} >0) { # if there is a rule for this bortype
         if ($data->{'firstremind'} < $daycount)
-    {
-    $amount   = $daycounttotal*$data->{'fine'};
+            {
+            $amount   = int($daycounttotal/$data->{'chargeperiod'})*$data->{'fine'};
+        }
+    } else {
+        # get fines default rules
+        my $data = GetIssuingRules($item->{'itemtype'},'*');
+        $daycounttotal = $daycount - $data->{'firstremind'};
+        if ($data->{'firstremind'} < $daycount)
+            {
+                if ($data->{'chargeperiod'} >0) { # if there is a rule for this bortype
+                    $amount   = int($daycounttotal/$data->{'chargeperiod'})*$data->{'fine'};
+                }
+            }
     }
+    
+    warn "Calc Fine for $item->{'itemnumber'}, $bortype, $difference , $dues = $amount / $daycount";
  return ( $amount, $data->{'chargename'}, $printout ,$daycounttotal ,$daycount );
 }
 
@@ -441,10 +461,10 @@ sub UpdateFine {
             my $diff = $amount - $data->{'amount'};
             my $out  = $data->{'amountoutstanding'} + $diff;
             my $sth2 = $dbh->prepare(
-                "update accountlines set date=now(), amount=?,
-      amountoutstanding=?,accounttype='FU' where
-      borrowernumber=? and itemnumber=?
-      and (accounttype='FU' or accounttype='O') and description like ?"
+                "UPDATE accountlines SET date=now(), amount=?,
+      amountoutstanding=?,accounttype='FU' WHERE
+      borrowernumber=? AND itemnumber=?
+      AND (accounttype='FU' OR accounttype='O') AND description LIKE ?"
             );
             $sth2->execute( $amount, $out, $data->{'borrowernumber'},
                 $data->{'itemnumber'}, "%$due%" );
@@ -460,7 +480,7 @@ sub UpdateFine {
         # I think this else-clause deals with the case where we're adding
         # a new fine.
         my $sth4 = $dbh->prepare(
-            "select title from biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber where items.itemnumber=?"
+            "SELECT title FROM biblio LEFT JOIN items ON biblio.biblionumber=items.biblionumber WHERE items.itemnumber=?"
         );
         $sth4->execute($itemnum);
         my $title = $sth4->fetchrow_hashref;
@@ -475,11 +495,11 @@ sub UpdateFine {
 #         $sth3->finish;
 #         $accountno[0]++;
 # begin transaction
-  my $nextaccntno = getnextacctno($borrowernumber);
+  my $nextaccntno = C4::Accounts::getnextacctno($borrowernumber);
     my $sth2 = $dbh->prepare(
-            "Insert into accountlines
+            "INSERT INTO accountlines
     (borrowernumber,itemnumber,date,amount,
-    description,accounttype,amountoutstanding,accountno) values
+    description,accounttype,amountoutstanding,accountno) VALUES
     (?,?,now(),?,?,'FU',?,?)"
         );
         $sth2->execute( $borrowernumber, $itemnum, $amount,
@@ -581,7 +601,7 @@ sub GetFine {
 
 =item GetIssuingRules
 
-$data = &GetIssuingRules($itemnumber,$categorycode);
+$data = &GetIssuingRules($itemtype,$categorycode);
 
 Looks up for all issuingrules an item info 
 
@@ -595,20 +615,16 @@ category he or she belongs to.
 =cut 
 
 sub GetIssuingRules {
-   my ($itemnumber,$categorycode)=@_;
+   my ($itemtype,$categorycode)=@_;
    my $dbh   = C4::Context->dbh();    
    my $query=qq|SELECT * 
-        FROM items,biblioitems,itemtypes,issuingrules
-        WHERE items.itemnumber=?
-        AND items.biblioitemnumber=biblioitems.biblioitemnumber
-        AND biblioitems.itemtype=itemtypes.itemtype
-        AND issuingrules.itemtype=itemtypes.itemtype
-        AND issuingrules.categorycode=?
-        AND  (items.itemlost <> 1
-        OR items.itemlost is NULL)|;
+        FROM issuingrules
+        WHERE issuingrules.itemtype=?
+            AND issuingrules.categorycode=?
+        |;
     my $sth = $dbh->prepare($query);
     #  print $query;
-    $sth->execute($itemnumber,$categorycode);
+    $sth->execute($itemtype,$categorycode);
     my ($data) = $sth->fetchrow_hashref;
    $sth->finish;
 return ($data);
@@ -677,9 +693,9 @@ return $result;
 }
 
 
-=item AmountNotify
+=item NumberNotifyId
 
-(@notify) = &AmountNotify($borrowernumber);
+(@notify) = &NumberNotifyId($borrowernumber);
 
 Returns amount for all file per borrowers
 C<@notify> array contains all file per borrowers
@@ -697,8 +713,8 @@ sub NumberNotifyId{
     my @notify;
     my $sth=$dbh->prepare($query);
         $sth->execute($borrowernumber);
-          while ( my $numberofotify=$sth->fetchrow_array){
-    push (@notify,$numberofotify);
+          while ( my ($numberofnotify)=$sth->fetchrow){
+    push (@notify,$numberofnotify);
     }
     $sth->finish;
 
@@ -715,19 +731,19 @@ C<$notifyid> is the file number
 
 C<$totalnotify> contains amount of a file
 
-C<$notify_id> contains the file number for the borrower number nad item number
+C<$notify_id> contains the file number for the borrower number and item number
 
 =cut
 
 sub AmountNotify{
-    my ($notifyid)=@_;
+    my ($notifyid,$borrowernumber)=@_;
     my $dbh = C4::Context->dbh;
     my $query=qq|    SELECT sum(amountoutstanding)
             FROM accountlines
-            WHERE notify_id=?|;
+            WHERE notify_id=? AND borrowernumber = ?|;
     my $sth=$dbh->prepare($query);
-        $sth->execute($notifyid);
-          my $totalnotify=$sth->fetchrow;
+       $sth->execute($notifyid,$borrowernumber);
+       my $totalnotify=$sth->fetchrow;
     $sth->finish;
     return ($totalnotify);
 }
@@ -1131,6 +1147,7 @@ display is filtered by branch
 
 sub GetOverduesForBranch {
     my ( $branch, $location) = @_;
+       my $itype_link =  (C4::Context->preference('item-level_itypes')) ?  " items.itype " :  " biblioitems.itemtype ";
     if ( not $location ) {
         my $dbh = C4::Context->dbh;
         my $sth = $dbh->prepare("
@@ -1160,7 +1177,7 @@ sub GetOverduesForBranch {
             LEFT JOIN items ON items.itemnumber = issues.itemnumber
             LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
             LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
-            LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
+            LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
             LEFT JOIN branches ON branches.branchcode = issues.branchcode
             WHERE ( issues.returndate  is null)
               AND ( accountlines.amountoutstanding  != '0.000000')
@@ -1211,7 +1228,7 @@ sub GetOverduesForBranch {
             LEFT JOIN items ON items.itemnumber = issues.itemnumber
             LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
             LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
-            LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
+            LEFT JOIN itemtypes ON itemtypes.itemtype = $itype_link
             LEFT JOIN branches ON branches.branchcode = issues.branchcode
            WHERE ( issues.returndate  is null )
              AND ( accountlines.amountoutstanding  != '0.000000')