-# Otherwise, it'd need a POD.
-sub TotalPaid {
- my ($time,$time2)=@_;
- $time2=$time unless $time2;
- my $dbh = C4::Context->dbh;
- my $query="Select * from accountlines,borrowers where (accounttype = 'Pay' or accounttype ='W')
- and accountlines.borrowernumber = borrowers.borrowernumber";
- if ($time eq 'today'){
- $query .= " and date = now()";
- } else {
- $query.=" and date>='$time' and date<='$time2'";
- }
- # my $query="Select * from statistics,borrowers
- # where statistics.borrowernumber= borrowers.borrowernumber
- # and (statistics.type='payment' or statistics.type='writeoff') ";
- # if ($time eq 'today'){
- # $query=$query." and datetime = now()";
- # } else {
- # $query.=" and datetime > '$time'";
- # }
- $query.=" order by timestamp";
- # print $query;
- my $sth=$dbh->prepare($query);
- $sth->execute;
- my @results;
- my $i=0;
- while (my $data=$sth->fetchrow_hashref){
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- # print $query;
- return(@results);
-}
-
-# Otherwise, it needs a POD.
-sub getcharges{
- my($borrowerno,$timestamp)=@_;
- my $dbh = C4::Context->dbh;
- my $timestamp2=$timestamp-1;
- my $query="Select * from accountlines where borrowernumber=$borrowerno
- and timestamp = '$timestamp' and accounttype <> 'Pay' and
- accounttype <> 'W'";
- my $sth=$dbh->prepare($query);
- # print $query,"<br>";
- $sth->execute;
- my $i=0;
- my @results;
- while (my $data=$sth->fetchrow_hashref){
- # if ($data->{'timestamp'} == $timestamp){
- $results[$i]=$data;
- $i++;
- # }
- }
- return(@results);
-}
-
-# Otherwise, this needs a POD.
-sub Getpaidbranch{
- my($date,$borrno)=@_;
- my $dbh = C4::Context->dbh;
- my $query="select * from statistics where type='payment' and datetime >'$date' and borrowernumber='$borrno'";
- my $sth=$dbh->prepare($query);
- $sth->execute;
- # print $query;
- my $data=$sth->fetchrow_hashref;
- $sth->finish;
- return($data->{'branch'});
-}
-
-# FIXME - This is only used in reservereport.pl and reservereport.xls,
-# neither of which is used.
-# Otherwise, it needs a POD.
-sub unfilledreserves {
- my $dbh = C4::Context->dbh;
- my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
- is NULL and biblio.biblionumber=reserves.biblionumber and
- reserves.constrainttype='o'
- and (reserves.biblionumber=reserveconstraints.biblionumber
- and reserves.borrowernumber=reserveconstraints.borrowernumber)
- and
- reserves.borrowernumber=borrowers.borrowernumber and
- biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
- biblio.title,reserves.reservedate";
- my $sth=$dbh->prepare($query);
- $sth->execute;
- my $i=0;
- my @results;
- while (my $data=$sth->fetchrow_hashref){
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
- is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
- reserves.borrowernumber=borrowers.borrowernumber
- order by
- biblio.title,reserves.reservedate";
- $sth=$dbh->prepare($query);
- $sth->execute;
- while (my $data=$sth->fetchrow_hashref){
- $results[$i]=$data;
- $i++;
- }
- $sth->finish;
- return($i,\@results);
+sub UpdateStats {
+ my ($params) = @_;
+# make some controls
+ return () if ! defined $params;
+# change these arrays if new types of transaction or new parameters are allowed
+ my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode);
+ my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
+ my @allowed_accounts_types = qw (writeoff payment);
+ my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
+ my @accounts_mandatory_keys = qw (type branch borrowernumber amount);
+
+ my @mandatory_keys = ();
+ if (! exists $params->{type} or ! defined $params->{type}) {
+ croak ("UpdateStats did not received type param");
+ }
+ if (grep ($_ eq $params->{type}, @allowed_circulation_types )) {
+ @mandatory_keys = @circulation_mandatory_keys;
+ } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
+ @mandatory_keys = @accounts_mandatory_keys;
+ } else {
+ croak ("UpdateStats received forbidden type param: ".$params->{type});
+ }
+ my @missing_params = ();
+ for my $mykey (@mandatory_keys ) {
+ push @missing_params, $mykey if !grep (/^$mykey/, keys %$params);
+ }
+ if (scalar @missing_params > 0 ) {
+ croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params ));
+ }
+ my @invalid_params = ();
+ for my $myparam (keys %$params ) {
+ push @invalid_params, $myparam unless grep { $_ eq $myparam } @allowed_keys;
+ }
+ if (scalar @invalid_params > 0 ) {
+ croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params ));
+ }
+# get the parameters
+ my $branch = $params->{branch};
+ my $type = $params->{type};
+ my $borrowernumber = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
+ my $itemnumber = exists $params->{itemnumber} ? $params->{itemnumber} : undef;
+ my $amount = exists $params->{amount} ? $params->{amount} : 0;
+ my $other = exists $params->{other} ? $params->{other} : '';
+ my $itemtype = exists $params->{itemtype} ? $params->{itemtype} : '';
+ my $location = exists $params->{location} ? $params->{location} : undef;
+ my $ccode = exists $params->{ccode} ? $params->{ccode} : '';
+ my $categorycode = exists $params->{categorycode} ? $params->{categorycode} : undef;
+
+ my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+ my $statistic = Koha::Statistic->new(
+ {
+ datetime => $dtf->format_datetime( dt_from_string ),
+ branch => $branch,
+ type => $type,
+ value => $amount,
+ other => $other,
+ itemnumber => $itemnumber,
+ itemtype => $itemtype,
+ location => $location,
+ borrowernumber => $borrowernumber,
+ ccode => $ccode,
+ categorycode => $categorycode,
+ }
+ )->store;
+
+ Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store
+ if C4::Context->preference('Pseudonymization')
+ && $borrowernumber # Not a real transaction if the patron does not exist
+ # For instance can be a transfer, or hold trigger
+ && grep { $_ eq $params->{type} } qw(renew issue return onsite_checkout);