Savannah seems out of sync. reloading.Mainly code cleaning and removing of Date:...
authortgarip1957 <tgarip1957>
Wed, 15 Nov 2006 01:36:00 +0000 (01:36 +0000)
committertgarip1957 <tgarip1957>
Wed, 15 Nov 2006 01:36:00 +0000 (01:36 +0000)
C4/Biblio.pm
C4/Circulation/Circ2.pm
C4/Circulation/Fines.pm
C4/Date.pm
C4/Serials.pm
C4/Stats.pm

index 9452aa1..f6ea71d 100644 (file)
@@ -75,6 +75,7 @@ $VERSION = 2.01;
 &XMLkoha2marc
 &XML_separate
 &XML_record_header
+&XMLmodLCindex
 &ZEBRAdelbiblio
 &ZEBRAgetrecord   
 &ZEBRAop 
index 9d86c10..10b08a0 100755 (executable)
@@ -165,10 +165,26 @@ and issues.borrowernumber = borrowers.borrowernumber");
        $data->{'datelastborrowed'} = $data2->{'issue_date'};
             $data->{'card'}     = $data2->{'cardnumber'};
            $data->{'borrower'}     = $data2->{'borrowernumber'};
+       $data->{issues}++;
         } 
 
         $sth2->finish;
+        my $sth2   = $dbh->prepare("select * from reserveissue,borrowers
+where itemnumber = ?
+and rettime is NULL
+and reserveissue.borrowernumber = borrowers.borrowernumber");
+
+        $sth2->execute($itemnumber);
+        if (my $data2 = $sth2->fetchrow_hashref) {
+
+       $data->{'date_due'}=$data2->{'duetime'};
+       $data->{'datelastborrowed'} = $data2->{'restime'};
+            $data->{'card'}     = $data2->{'cardnumber'};
+           $data->{'borrower'}     = $data2->{'borrowernumber'};
+       $data->{issues}++;
+        } 
 
+        $sth2->finish;
         # Find the last 2 people who borrowed this item.
         $sth2 = $dbh->prepare("select * from issues, borrowers
                                                where itemnumber = ?
@@ -176,7 +192,6 @@ and issues.borrowernumber = borrowers.borrowernumber");
                                                                        and returndate is not NULL
                                                                        order by returndate desc,timestamp desc limit 2") ;
         $sth2->execute($itemnumber) ;
-#        for (my $i2 = 0; $i2 < 2; $i2++) { # FIXME : error if there is less than 3 pple borrowing this item
 my $i2=0;
           while (my $data2  = $sth2->fetchrow_hashref) {
                 $data->{"timestamp$i2"} = $data2->{'timestamp'};
@@ -185,7 +200,6 @@ my $i2=0;
 $data->{'datelastborrowed'} = $data2->{'issue_date'} unless $data->{'datelastborrowed'};
        $i2++;
             } # while
-#       } # for
 
         $sth2->finish;
     return($data);
@@ -969,7 +983,7 @@ sub issuebook {
                                } else {
                                        my $tobrcd = ReserveWaiting($res->{'itemnumber'}, $res->{'borrowernumber'});
                                        transferbook($tobrcd,$barcode, 1);
-                                       warn "transferbook";
+#                                      warn "transferbook";
                                }
                        }
                }
@@ -1148,7 +1162,7 @@ sub returnbook {
        if (not $itemrecord) {
                $messages->{'BadBarcode'} = $barcode;
                $doreturn = 0;
-       return ($doreturn, $messages, "", "");
+       return ($doreturn, $messages, undef, undef);
        }
        my $iteminformation=XMLmarc2koha_onerecord($dbh,$itemrecord,"holdings");
               $iteminformation->{'itemtype'}=MARCfind_itemtype($dbh,$iteminformation->{biblionumber});
@@ -1168,7 +1182,7 @@ sub returnbook {
        # check that the book has been cancelled
        if ($iteminformation->{'wthdrawn'}) {
                $messages->{'wthdrawn'} = 1;
-               $doreturn = 0;
+       #       $doreturn = 0;
        }
        # update issues, thereby returning book (should push this out into another subroutine
        my ($borrower) = getpatroninformation(\%env, $currentborrower, 0);
@@ -1178,10 +1192,11 @@ sub returnbook {
                $messages->{'WasReturned'} = 1; # FIXME is the "= 1" right?
        
                $sth->finish;
+       }
        $itemrecord=XML_writeline($itemrecord, "date_due", "","holdings");
        $itemrecord=XML_writeline($itemrecord, "onloan", "0","holdings");
        $itemrecord=XML_writeline($itemrecord, "borrowernumber", "","holdings");
-       }
+       
        my ($transfered, $mess, $item) = transferbook($branch, $barcode, 1);
        my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
                $year += 1900;
@@ -1715,7 +1730,7 @@ sub renewstatus {
        my $renews = 1;
        my $resfound;
        my $resrec;
-       my $renewokay; ##
+       my $renewokay=0; ##
        # Look in the issues table for this item, lent to this borrower,
        # and not yet returned.
 my $borrower=C4::Members::getpatroninformation($dbh,$bornum,undef);
@@ -1728,15 +1743,16 @@ my $borrower=C4::Members::getpatroninformation($dbh,$bornum,undef);
                                                                and returndate is null
                                                                and items.itemnumber=issues.itemnumber");
        $sth1->execute($bornum,$itemnumber);
-       if (my $data1 = $sth1->fetchrow_hashref) {
+my $data1 = $sth1->fetchrow_hashref;
+       if ($data1 ) {
                # Found a matching item
-       if (C4::Context->preference("LibraryName") eq "NEU Grand Library"){
-               ##privileged get renewal whatever the case may be
-               if ($borrower->{'categorycode'} eq 'P'){
-               $renewokay = 1;
-               return $renewokay;
+               if (C4::Context->preference("LibraryName") eq "NEU Grand Library"){
+                       ##privileged get renewal whatever the case may be
+                       if ($borrower->{'categorycode'} eq 'P'){
+                       $renewokay = 1;
+                       return $renewokay;
+                       }
                }
-       }
                # See if this item may be renewed. 
                my $sth2 = $dbh->prepare("select renewalsallowed from itemtypes where itemtypes.itemtype=?");
                $sth2->execute($data1->{itemtype});
@@ -1759,18 +1775,15 @@ my $borrower=C4::Members::getpatroninformation($dbh,$bornum,undef);
                               $renewokay = 0;
                                 }
                }
-       }## item found
                 ($resfound, $resrec) = CheckReserves($itemnumber);
                                 if ($resfound) {
                                 if (C4::Context->preference("strictrenewals")){
                                                $renewokay=4;
-                                               }else{
+                               }else{
                                                 $renewokay = 0;
                                          }
-                                       }       
-#      }
-       $sth1->finish;
-if (C4::Context->preference("strictrenewals")){
+                       }
+     if (C4::Context->preference("strictrenewals")){
        ### A new system pref "allowRenewalsBefore" prevents the renewal before a set amount of days left before expiry
        ## Try to find whether book can be renewed at this date
        my $loanlength;
@@ -1788,7 +1801,10 @@ if (C4::Context->preference("strictrenewals")){
        if  ($difference < 0) {
        $renewokay=2 ;
        }
-}##strictrenewals
+     }##strictrenewals 
+       }##item found
+       $sth1->finish;
+
        return($renewokay);
 }
 
@@ -1850,7 +1866,7 @@ if ($datedue eq "" ) {
        &XMLmoditemonefield($dbh,$iteminformation->{'biblionumber'},$iteminformation->{'itemnumber'},'date_due',$datedue);
                
        # Log the renewal
-       UpdateStats($env,$env->{'branchcode'},'renew','','',$itemnumber,'',$bornum);
+       UpdateStats($env,$env->{'branchcode'},'renew','','',$itemnumber,$iteminformation->{'itemtype'},$bornum);
 
        # Charge a new rental fee, if applicable?
        my ($charge,$type)=calc_charges($env, $itemnumber, $bornum);
index 0aee466..7296dee 100644 (file)
@@ -93,20 +93,7 @@ standard fine for books might be $0.50, but $1.50 for DVDs, or staff
 members might get a longer grace period between the first and second
 reminders that a book is overdue).
 
-The fine is calculated as follows: if it is time for the first
-reminder, the fine is the value listed for the given (branch, item type,
-borrower code) combination. If it is time for the second reminder, the
-fine is doubled. Finally, if it is time to send the account to a
-collection agency, the fine is set to 5 local monetary units (a really
-good deal for the patron if the library is in Italy). Otherwise, the
-fine is 0.
-
-Note that the way this function is currently implemented, it only
-returns a nonzero value on the notable days listed above. That is, if
-the issuingruless entry says to send a first reminder 7 days after the
-book is due, then if you call C<&CalcFine> 7 days after the book is
-due, it will give a nonzero fine. If you call C<&CalcFine> the next
-day, however, it will say that the fine is 0.
+
 
 C<$itemnumber> is the book's item number.
 
@@ -133,16 +120,7 @@ sub CalcFine {
   my $dbh = C4::Context->dbh;
   # Look up the issuingrules record for this book's item type and the
   # given borrwer type.
-  # The reason this query is so messy is that it's a messy question:
-  # given the barcode, we can find the book's items record. This gives
-  # us the biblio record, which gives us a set of issuingrules
-  # records. Then we select the one that corresponds to the desired
-  # borrower type.
-
-  # FIXME - Is it really necessary to get absolutely everything from
-  # all four tables? It looks as if this code only wants
-  # firstremind, chargeperiod, accountsent, and chargename from the
-  # issuingrules table.
 
   my $sth=$dbh->prepare("Select * from items,biblio,itemtypes,issuingrules where items.itemnumber=?
   and items.biblionumber=biblio.biblionumber and
@@ -159,38 +137,26 @@ sub CalcFine {
   my $amount=0;
   my $printout;
 
-  # Is it time to send out the first reminder?
-  # FIXME - I'm not sure the "=="s are correct here. Let's say that
-  # $data->{firstremind} is today, but 'fines2.pl' doesn't run for
-  # some reason (the cron daemon died, the server crashed, the
-  # sysadmin had the machine down for maintenance, or whatever).
-  #
-  # Then the next day, the book is $data->{firstremind}+1 days
-  # overdue. But this function returns $amount == 0, $printout ==
-  # undef, on the assumption that 'fines2.pl' ran the previous day. So
-  # the first thing the patron gets is a second notice, but that's a
-  # week after the server crash, so people may not connect the two
-  # events.
   if ($difference > $data->{'firstremind'}){
     # Yes. Set the fine as listed.
-    $amount=$data->{'fine'}* $difference;
+$amount=$data->{'fine'}* $difference;
+
     $printout="First Notice";
   }
 
   # Is it time to send out a second reminder?
-  my $second=$data->{'firstremind'}+$data->{'chargeperiod'};
+  my $second=$data->{'firstremind'}+$data->{chargeperiod};
   if ($difference == $second){
-#    # Yes. The fine is double.
-#    $amount=$data->{'fine'}*2;
+$amount=$data->{'fine'}* $difference;
+
     $printout="Second Notice";
   }
 
   # Is it time to send the account to a collection agency?
-  # FIXME - At least, I *think* that's what this code is doing.
-  if ($difference == $data->{'accountsent'} && $data->{'fine'} > 0){
-    # Yes. Set the fine at 5 local monetary units.
-    # FIXME - This '5' shouldn't be hard-wired.
-    $amount=$data->{'fine'}* $difference;
+  # FIXME -This $data->{'accountsent'} is not seemed to be set in the DB
+  if ($difference == $data->{'accountsent'}){
+     $amount=$data->{'fine'}* $difference;
+
     $printout="Final Notice";
   }
   return($amount,$data->{'chargename'},$printout);
index 513a573..f5b6f89 100644 (file)
@@ -27,7 +27,7 @@ use DateTime;
 use DateTime::Format::ISO8601;
 use DateTime::Format::Strptime;
 use DateTime::Format::Duration;
-
+use POSIX qw(ceil floor);
 require Exporter;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
@@ -187,18 +187,21 @@ return $newdate;
 }
 sub get_duration{
 my $period=shift;
+
 my $parse;
-if ($period=~/day/){
+if ($period=~/ays/){
 $parse="\%e days";
 }elsif ($period=~/week/){
 $parse="\%W weeks";
 }elsif ($period=~/year/){
 $parse="\%Y years";
-}elsif ($period=~/month/){
+}elsif ($period=~/onth/){
 $parse="\%m months";
 }
+
 my $parser=DateTime::Format::Duration->new(pattern => $parse  );
        my $duration=$parser->parse_duration($period);
+
 return $duration;
 
 }
@@ -208,6 +211,6 @@ my $dt1=DateTime::Format::ISO8601->parse_datetime($date1);
 my $dt2=DateTime::Format::ISO8601->parse_datetime($date2);
 my $dur=$dt2->subtract_datetime_absolute($dt1);## in seconds
 my $days=$dur->seconds/(60*60*24);
-return int($days);
+return floor($days);
 }
 1;
index 6634141..2919efa 100644 (file)
@@ -115,7 +115,7 @@ name,title,planneddate,serialseq,serial.subscriptionid from tables : subscriptio
 
 =cut
 sub GetLateIssues {
-    my ($supplierid) = @_;
+    my ($supplierid) = shift;
     my $dbh = C4::Context->dbh;
     my $sth;
     if ($supplierid) {
@@ -390,8 +390,8 @@ a table of hashref. Each hash containt the subscription.
 
 =cut
 sub GetSubscriptions {
-    my ($title,$ISSN,$biblionumber) = @_;
-    return unless $title or $ISSN or $biblionumber;
+    my ($title,$ISSN,$biblionumber,$supplierid) = @_;
+    return unless $title or $ISSN or $biblionumber or $supplierid;
     my $dbh = C4::Context->dbh;
     my $sth;
     if ($biblionumber) {
@@ -404,10 +404,9 @@ sub GetSubscriptions {
         );
     $sth = $dbh->prepare($query);
     $sth->execute($biblionumber);
-    } else {
-        if ($ISSN and $title){
+    } elsif ($ISSN and $title){
             my $query = qq|
-                SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber
+                SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber,aqbooksellerid
                 FROM   subscription,biblio
                 WHERE biblio.biblionumber= subscription.biblionumber
                     AND (biblio.title LIKE ? or biblio.issn = ?)
@@ -415,22 +414,29 @@ sub GetSubscriptions {
             |;
             $sth = $dbh->prepare($query);
             $sth->execute("%$title%",$ISSN);
-        }
-        else{
-            if ($ISSN){
+        } elsif ($ISSN){
                 my $query = qq(
-                    SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber
+                    SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber,aqbooksellerid
                     FROM   subscription,biblio
-                    WHERE  biblio.biblionumber = biblioitems.biblionumber
-                        AND biblio.biblionumber=subscription.biblionumber
-                        AND biblioitems.issn = ?
+                       WHERE biblio.biblionumber=subscription.biblionumber
+                        AND biblio.issn = ?
                     ORDER BY title
                 );
                 $sth = $dbh->prepare($query);
                 $sth->execute($ISSN);
+       }elsif ($supplierid){
+                my $query = qq(
+                    SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber,aqbooksellerid
+                    FROM   subscription,biblio
+                   WHERE biblio.biblionumber=subscription.biblionumber
+                        AND subscription.aqbooksellerid = ?
+                    ORDER BY title
+                );
+                $sth = $dbh->prepare($query);
+                $sth->execute($supplierid);
             } else {
                 my $query = qq(
-                    SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber
+                    SELECT subscription.subscriptionid,biblio.title,biblio.issn,subscription.notes,biblio.biblionumber,aqbooksellerid
                     FROM   subscription,biblio
                     WHERE biblio.biblionumber=subscription.biblionumber
                         AND biblio.title LIKE ?
@@ -438,9 +444,9 @@ sub GetSubscriptions {
                 );
                 $sth = $dbh->prepare($query);
                 $sth->execute("%$title%");
-            }
         }
-    }
+        
+   
     my @results;
     my $previoustitle="";
     my $odd=1;
@@ -599,7 +605,7 @@ all the input params updated.
 =back
 
 =cut
-sub GetNextSeq {
+sub Get_Next_Seq {
     my ($val) =@_;
     my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
     $calculated = $val->{numberingmethod};
@@ -631,7 +637,7 @@ sub GetNextSeq {
 }
 
 
-sub New_Get_Next_Seq {
+sub GetNextSeq {
     my ($val) =@_;
     my ($calculated,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3);
     my $pattern = $val->{numberpattern};
@@ -708,44 +714,6 @@ the date on ISO format.
 =back
 
 =cut
-sub GetNextDate(@) {
-    my ($planneddate,$subscription) = @_;
-    my $resultdate;
-   my $duration;
-    if ($subscription->{periodicity} == 1) {
-       $duration=get_duration("1 days");    
-    }
-    if ($subscription->{periodicity} == 2) {
-       $duration=get_duration("1 weeks");    
-    }
-    if ($subscription->{periodicity} == 3) {
-      $duration=get_duration("2 weeks");    
-    }
-    if ($subscription->{periodicity} == 4) {
-       $duration=get_duration("3 weeks");    
-    }
-    if ($subscription->{periodicity} == 5) {
-     $duration=get_duration("1 months");    
-    }
-    if ($subscription->{periodicity} == 6) {
-       $duration=get_duration("2 months");    
-    }
-    if ($subscription->{periodicity} == 7 || $subscription->{periodicity} == 8) {
-        $duration=get_duration("3 months");    
-    }
-    
-    if ($subscription->{periodicity} == 9) {
-         $duration=get_duration("6 months");    
-    }
-    if ($subscription->{periodicity} == 10) {
-          $duration=get_duration("1 years");    
-    }
-    if ($subscription->{periodicity} == 11) {
-        $duration=get_duration("2 years");    
-    }
- $resultdate=DATE_Add_Duration($planneddate,$duration);
-    return $resultdate;
-}
 
 =head2 GetSeq
 
@@ -799,9 +767,9 @@ sub GetSubscriptionExpirationDate {
         }
     }
     else {
-       my $duration=get_duration($subscription->{monthlength}." months") if ($subscription->{monthlength});
-       my $duration=get_duration($subscription->{weeklength}." weeks") if ($subscription->{weeklength});
-
+       my $duration;
+        $duration=get_duration($subscription->{monthlength}." months") if ($subscription->{monthlength});
+        $duration=get_duration($subscription->{weeklength}." weeks") if ($subscription->{weeklength});
         $enddate = DATE_Add_Duration($subscription->{startdate},$duration) ;
     }
     return $enddate;
@@ -930,8 +898,8 @@ sub ModSerialStatus {
         # next issue number
         my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3,$newinnerloop1,$newinnerloop2,$newinnerloop3) = GetNextSeq($val);
         # next date (calculated from actual date & frequency parameters)
-          my $nextplanneddate = Get_Next_Date($planneddate,$val);
-          my $nextpublisheddate = Get_Next_Date($publisheddate,$val);
+          my $nextplanneddate = GetNextDate($planneddate,$val);
+          my $nextpublisheddate = GetNextDate($publisheddate,$val);
         NewIssue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextpublisheddate,$nextplanneddate,0);
         my $query = qq|
             UPDATE subscription
@@ -1193,7 +1161,7 @@ sub serialchangestatus {
         my $val = $sth->fetchrow_hashref;
         # next issue number
         my ($newserialseq,$newlastvalue1,$newlastvalue2,$newlastvalue3) = New_Get_Next_Seq($val);
-        my $nextplanneddate = Get_Next_Date($planneddate,$val);
+        my $nextplanneddate = GetNextDate($planneddate,$val);
         NewIssue($newserialseq, $subscriptionid, $val->{'biblionumber'}, 1, $nextplanneddate);
         $sth = $dbh->prepare("update subscription set lastvalue1=?, lastvalue2=?,lastvalue3=? where subscriptionid = ?");
         $sth->execute($newlastvalue1,$newlastvalue2,$newlastvalue3,$subscriptionid);
@@ -1254,11 +1222,12 @@ sub HasSubscriptionExpired {
         $sth->execute($subscriptionid);
         my $res = $sth->fetchrow;
         my $endofsubscriptiondate;
-       my $duration=get_duration($subscription->{monthlength}." months") if ($subscription->{monthlength});
-       my $duration=get_duration($subscription->{weeklength}." weeks") if ($subscription->{weeklength});
+       my $duration;
+        $duration=get_duration($subscription->{monthlength}." months") if ($subscription->{monthlength});
+       $duration=get_duration($subscription->{weeklength}." weeks") if ($subscription->{weeklength});
 
         $endofsubscriptiondate = DATE_Add_Duration($subscription->{startdate},$duration) ;
-        return 1 if ($res >= $endofsubscriptiondate);
+        return 1 if ($res ge $endofsubscriptiondate);
         return 0;
     }
 }
@@ -1674,8 +1643,9 @@ sub abouttoexpire {
        $sth->execute($subscriptionid);
        my $res = $sth->fetchrow;
        my $endofsubscriptiondate;
-       my $duration=get_duration($subscription->{monthlength}." months") if ($subscription->{monthlength});
-       my $duration=get_duration($subscription->{weeklength}." weeks") if ($subscription->{weeklength});
+my $duration;
+        $duration=get_duration($subscription->{monthlength}." months") if ($subscription->{monthlength});
+        $duration=get_duration($subscription->{weeklength}." weeks") if ($subscription->{weeklength});
 
         $endofsubscriptiondate = DATE_Add_Duration($subscription->{startdate},$duration) ;
        my $per = $subscription->{'periodicity'};
@@ -1700,15 +1670,13 @@ sub abouttoexpire {
 
 
 
-=head2 Get_Next_Date
+=head2 GetNextDate
 
 =over 4
 
-($resultdate) = &Get_Next_Date($planneddate,$subscription)
-
-this function is an extension of GetNextDate which allows for checking for irregularity
+($resultdate) = &GetNextDate($planneddate,$subscription)
 
-it takes the planneddate and will return the next issue's date and will skip dates if there
+this function  takes the planneddate and will return the next issue's date and will skip dates if there
 exists an irregularity
 - eg if periodicity is monthly and $planneddate is 2007-02-10 but if March and April is to be 
 skipped then the returned date will be 2007-05-10
@@ -1719,21 +1687,24 @@ $resultdate - then next date in the sequence
 =back
 
 =cut
-sub Get_Next_Date(@) {
+sub GetNextDate {
     my ($planneddate,$subscription) = @_;
     my @irreg = split(/\|/,$subscription->{irregularity});
  my $dateobj=DATE_obj($planneddate);
     my $dayofweek = $dateobj->day_of_week;
   my $month=$dateobj->month;
     my $resultdate;
-    #       warn "DOW $dayofweek";
 
     if ($subscription->{periodicity} == 1) {
+       my %irreghash;
+       for(my $i=0;$i<@irreg;$i++){
+       $irreghash{$irreg[$i]}=1;
+       }
 my $duration=get_duration("1 days");
        for(my $i=0;$i<@irreg;$i++){
            if($dayofweek == 7){ $dayofweek = 0; }
 
-           if(in_array(($dayofweek+1), @irreg)){
+           if($irreghash{$dayofweek+1}){
                $planneddate = DATE_Add_Duration($planneddate,$duration);
                $dayofweek++;
            }
@@ -1788,7 +1759,6 @@ my $duration=get_duration("1 months");
            }
        }
        $resultdate=DATE_Add_Duration($planneddate,$duration);
-       # warn "Planneddate2: $planneddate";
     }
     if ($subscription->{periodicity} == 6) {
 my $duration=get_duration("2 months");
index f9b7b2d..4540e29 100644 (file)
@@ -74,15 +74,17 @@ sub UpdateStats {
         #module to insert stats data into stats table
         my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno)=@_;
         my $dbh = C4::Context->dbh;
+       $env=C4::Context->userenv unless $env;
         if ($branch eq ''){
                 $branch=$env->{'branchcode'};
         }
-        my $user = $env->{'usercode'};
-        print $borrowernumber;
+        my $user = C4::Context->userenv;
+#        print $borrowernumber;
+       my $userid=$user->{'cardnumber'} if $user;
         # FIXME - Use $dbh->do() instead
         my $sth=$dbh->prepare("Insert into statistics (datetime,branch,type,usercode,value,
                                         other,itemnumber,itemtype,borrowernumber,proccode) values (now(),?,?,?,?,?,?,?,?,?)");
-        $sth->execute($branch,$type,$user,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno);
+        $sth->execute($branch,$type,$userid,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno);
         $sth->finish;
 }