Circulation.pm - BEGIN block VERSION and vars related to export.
[koha.git] / C4 / Circulation.pm
index 15eb7d0..172b246 100644 (file)
@@ -25,6 +25,7 @@ use C4::Stats;
 use C4::Reserves;
 use C4::Koha;
 use C4::Biblio;
+use C4::Items;
 use C4::Members;
 use C4::Dates;
 use Date::Calc qw(
@@ -42,10 +43,48 @@ use C4::Log; # logaction
 
 use Data::Dumper;
 
-our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,%EXPORT_TAGS);
-
-# set the version for version checking
-$VERSION = 3.00;
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+
+BEGIN {
+       # set the version for version checking
+       $VERSION = 3.01;
+       @ISA    = qw(Exporter);
+
+       # FIXME subs that should probably be elsewhere
+       push @EXPORT, qw(
+               &FixOverduesOnReturn
+               &cuecatbarcodedecode
+       );
+
+       # subs to deal with issuing a book
+       push @EXPORT, qw(
+               &CanBookBeIssued
+               &CanBookBeRenewed
+               &AddIssue
+               &AddRenewal
+               &GetRenewCount
+               &GetItemIssue
+               &GetItemIssues
+               &GetBorrowerIssues
+               &GetIssuingCharges
+               &GetBiblioIssues
+               &AnonymiseIssueHistory
+       );
+
+       # subs to deal with returns
+       push @EXPORT, qw(
+               &AddReturn
+       );
+
+       # subs to deal with transfers
+       push @EXPORT, qw(
+               &transferbook
+               &GetTransfers
+               &GetTransfersFromTo
+               &updateWrongTransfer
+               &DeleteTransfer
+       );
+}
 
 =head1 NAME
 
@@ -63,47 +102,6 @@ Also deals with stocktaking.
 
 =head1 FUNCTIONS
 
-=cut
-
-@ISA    = qw(Exporter);
-
-# FIXME subs that should probably be elsewhere
-push @EXPORT, qw(
-  &FixOverduesOnReturn
-  &cuecatbarcodedecode
-);
-
-# subs to deal with issuing a book
-push @EXPORT, qw(
-  &CanBookBeIssued
-  &CanBookBeRenewed
-  &AddIssue
-  &AddRenewal
-  &GetRenewCount
-  &GetItemIssue
-  &GetItemIssues
-  &GetBorrowerIssues
-  &GetIssuingCharges
-  &GetBiblioIssues
-  &AnonymiseIssueHistory
-);
-# subs to deal with returns
-push @EXPORT, qw(
-  &AddReturn
-);
-
-# subs to deal with transfers
-push @EXPORT, qw(
-  &transferbook
-  &GetTransfers
-  &GetTransfersFromTo
-  &updateWrongTransfer
-  &DeleteTransfer
-);
-
-# FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
-# FIXME From Paul : i don't understand what this sub does & why it has to be called on every circ. Speak of this with chris maybe ?
-
 =head2 decode
 
 =head3 $str = &decode($chunk);
@@ -117,6 +115,9 @@ returns it.
 
 =cut
 
+# FIXME - At least, I'm pretty sure this is for decoding CueCat stuff.
+# FIXME From Paul : i don't understand what this sub does & why it has to be called on every circ. Speak of this with chris maybe ?
+
 sub cuecatbarcodedecode {
     my ($barcode) = @_;
     chomp($barcode);
@@ -445,7 +446,7 @@ sub TooMany {
 
     # check the 2 parameters (branch / itemtype / default categorycode
     $sth->execute( '*', $type, $branch );
-    my $result = $sth->fetchrow_hashref;
+    $result = $sth->fetchrow_hashref;
 #     warn "*, $type, $branch = ".Data::Dumper::Dumper($result);
 
     if ( $result->{maxissueqty} ne '' ) {
@@ -469,7 +470,7 @@ sub TooMany {
     
     # check the 1 parameters (default branch / itemtype / categorycode
     $sth->execute( $cat_borrower, $type, '*' );
-    my $result = $sth->fetchrow_hashref;
+    $result = $sth->fetchrow_hashref;
 #     warn "$cat_borrower, $type, * = ".Data::Dumper::Dumper($result);
     
     if ( $result->{maxissueqty} ne '' ) {
@@ -493,7 +494,7 @@ sub TooMany {
 
     # check the 0 parameters (default branch / itemtype / default categorycode
     $sth->execute( '*', $type, '*' );
-    my $result = $sth->fetchrow_hashref;
+    $result = $sth->fetchrow_hashref;
 #     warn "*, $type, * = ".Data::Dumper::Dumper($result);
 
     if ( $result->{maxissueqty} ne '' ) {
@@ -506,7 +507,7 @@ sub TooMany {
        }
     # now checking for total
     $sth->execute( '*', '*', '*' );
-    my $result = $sth->fetchrow_hashref;
+    $result = $sth->fetchrow_hashref;
     if ( $result->{maxissueqty} ne '' ) {
                warn "checking total";
                $sth2->execute( $borrower->{'borrowernumber'}, $type );
@@ -1012,20 +1013,14 @@ sub AddIssue {
         );
         $sth->finish;
         $item->{'issues'}++;
-        $sth =
-          $dbh->prepare(
-            "UPDATE items SET issues=?, holdingbranch=?, itemlost=0, datelastborrowed  = now(), onloan = ? WHERE itemnumber=?");
-        $sth->execute(
-            $item->{'issues'},
-            C4::Context->userenv->{'branch'},
-                       $dateduef->output('iso'),
-            $item->{'itemnumber'}
-        );
-        $sth->finish;
-        &ModDateLastSeen( $item->{'itemnumber'} );
-        my $record = GetMarcItem( $item->{'biblionumber'}, $item->{'itemnumber'} );
-        my $frameworkcode = GetFrameworkCode( $item->{'biblionumber'} );                                                                                         
-        ModItemInMarc( $record, $item->{'biblionumber'}, $item->{'itemnumber'}, $frameworkcode );
+        ModItem({ issues           => $item->{'issues'},
+                  holdingbranch    => C4::Context->userenv->{'branch'},
+                  itemlost         => 0,
+                  datelastborrowed => C4::Dates->new()->output('iso'),
+                  onloan           => $dateduef->output('iso'),
+                }, $item->{'biblionumber'}, $item->{'itemnumber'});
+        ModDateLastSeen( $item->{'itemnumber'} );
+        
         # If it costs to borrow this book, charge it to the patron's account.
         my ( $charge, $itemtype ) = GetIssuingCharges(
             $item->{'itemnumber'},
@@ -1234,12 +1229,7 @@ sub AddReturn {
                $iteminformation->{'holdingbranch'} = C4::Context->userenv->{'branch'};
        }
         ModDateLastSeen( $iteminformation->{'itemnumber'} );
-               my $sth = $dbh->prepare("UPDATE items SET onloan = NULL where itemnumber = ?");
-               $sth->execute($iteminformation->{'itemnumber'});
-               $sth->finish();
-               my $record = GetMarcItem( $biblio->{'biblionumber'}, $iteminformation->{'itemnumber'} );
-               my $frameworkcode = GetFrameworkCode( $biblio->{'biblionumber'} );
-               ModItemInMarc( $record, $biblio->{'biblionumber'}, $iteminformation->{'itemnumber'}, $frameworkcode );
+        ModItem({ onloan => undef }, $biblio->{'biblionumber'}, $iteminformation->{'itemnumber'});
                
                if ($iteminformation->{borrowernumber}){
                        ($borrower) = C4::Members::GetMemberDetails( $iteminformation->{borrowernumber}, 0 );
@@ -1446,9 +1436,7 @@ sub FixAccountForLostAndReturned {
                        VALUES (?,?,?,?)");
                $usth->execute($borrower->{'borrowernumber'},$data->{'accountno'},$nextaccntno,$offset);
                $usth->finish;
-               $usth = $dbh->prepare("UPDATE items SET paidfor='' WHERE itemnumber=?");
-               $usth->execute($itm);
-               $usth->finish;
+        ModItem({ paidfor => '' }, undef, $itm);
        }
        $sth->finish;
        return;
@@ -1463,6 +1451,7 @@ Returns patrons currently having a book. nothing if item is not issued atm
 C<$itemnumber> is the itemnumber
 
 Returns an array of hashes
+
 =cut
 
 sub GetItemIssue {
@@ -1501,6 +1490,7 @@ C<$itemnumber> is the itemnumber
 C<$history> is 0 if you want actuel "issuer" (if it exist) and 1 if you want issues history
 
 Returns an array of hashes
+
 =cut
 
 sub GetItemIssues {
@@ -1711,12 +1701,7 @@ sub AddRenewal {
 
     # Update the renewal count on the item, and tell zebra to reindex
     $renews = $biblio->{'renewals'} + 1;
-    $sth = $dbh->prepare("UPDATE items SET renewals = ? WHERE itemnumber = ?");
-    $sth->execute($renews,$itemnumber);
-    $sth->finish();
-    my $record = GetMarcItem( $biblio->{'biblionumber'}, $itemnumber );
-    my $frameworkcode = GetFrameworkCode( $biblio->{'biblionumber'} );
-    ModItemInMarc( $record, $biblio->{'biblionumber'}, $itemnumber, $frameworkcode );
+    ModItem({ renewals => $renews }, $biblio->{'biblionumber'}, $itemnumber);
 
     # Charge a new rental fee, if applicable?
     my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
@@ -1980,21 +1965,14 @@ sub updateWrongTransfer {
 
 $items = UpdateHoldingbranch($branch,$itmenumber);
 Simple methode for updating hodlingbranch in items BDD line
+
 =cut
 
 sub UpdateHoldingbranch {
-       my ( $branch,$itmenumber ) = @_;
-       my $dbh = C4::Context->dbh;     
-# first step validate the actual line of transfert .
-       my $sth =
-               $dbh->prepare(
-                       "update items set holdingbranch = ? where itemnumber= ?"
-               );
-               $sth->execute($branch,$itmenumber);
-               $sth->finish;
-        
-       
+       my ( $branch,$itemnumber ) = @_;
+    ModItem({ holdingbranch => $branch }, undef, $itemnumber);
 }
+
 =head2 CheckValidDatedue
 
 $newdatedue = CheckValidDatedue($date_due,$itemnumber,$branchcode);
@@ -2002,7 +1980,9 @@ this function return a new date due after checked if it's a repeatable or specia
 C<$date_due>   = returndate calculate with no day check
 C<$itemnumber>  = itemnumber
 C<$branchcode>  = localisation of issue 
+
 =cut
+
 # Why not create calendar object?  - 
 # TODO add 'duedate' option to useDaysMode .
 sub CheckValidDatedue { 
@@ -2027,6 +2007,7 @@ for (my $i=0;$i<2;$i++){
        my $newdatedue=C4::Dates->new(sprintf("%04d-%02d-%02d",$years,$month,$day),'iso');
 return $newdatedue;
 }
+
 =head2 CheckRepeatableHolidays
 
 $countrepeatable = CheckRepeatableHoliday($itemnumber,$week_day,$branchcode);
@@ -2061,7 +2042,9 @@ C<$month>   = the month of datedue
 C<$day>     = the day of datedue
 C<$itemnumber>  = itemnumber
 C<$branchcode>  = localisation of issue 
+
 =cut
+
 sub CheckSpecialHolidays{
 my ($years,$month,$day,$itemnumber,$branchcode) = @_;
 my $dbh = C4::Context->dbh;
@@ -2087,7 +2070,9 @@ C<$month>   = the month of datedue
 C<$day>     = the day of datedue
 C<$itemnumber>  = itemnumber
 C<$branchcode>  = localisation of issue 
+
 =cut
+
 sub CheckRepeatableSpecialHolidays{
 my ($month,$day,$itemnumber,$branchcode) = @_;
 my $dbh = C4::Context->dbh;