-# XXX - POD. Doesn't appear to be used
-sub Overdues{
- my $dbh=C4Connect;
- my $query="Select count(*) from issues where date_due >= now()";
- my $sth=$dbh->prepare($query);
- $sth->execute;
- my $count=$sth->fetchrow_hashref;
- $sth->finish;
- $dbh->disconnect;
- return($count->{'count(*)'});
-}
-
-# XXX - POD. Never used
-sub TotalOwing{
- my ($type)=@_;
- my $dbh=C4Connect;
- my $query="Select sum(amountoutstanding) from accountlines";
- if ($type eq 'fine'){
- $query=$query." where accounttype='F' or accounttype='FN'";
- }
- my $sth=$dbh->prepare($query);
-# print $query;
- $sth->execute;
- my $total=$sth->fetchrow_hashref;
- $sth->finish;
- $dbh->disconnect;
- return($total->{'sum(amountoutstanding)'});
-}
-
-# XXX - POD. Never used
-sub TotalPaid {
- my ($time)=@_;
- my $dbh=C4Connect;
- my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
-or accounttype ='W')
- and accountlines.borrowernumber = borrowers.borrowernumber";
- if ($time eq 'today'){
- $query=$query." and date = now()";
- } else {
- $query.=" and date='$time'";
- }
-# 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;
- $dbh->disconnect;
-# print $query;
- return(@results);
-}
-
-# XXX - POD. Only used in stats.pl, which in turn is never used.
-sub getcharges{
- my($borrowerno,$timestamp)=@_;
- my $dbh=C4Connect;
- 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++;
-# }
- }
- $dbh->disconnect;
- return(@results);
-}
-
-# XXX - POD. This is only used in stats.pl and stats2.pl, neither of
-# which is used.
-sub Getpaidbranch{
- my($date,$borrno)=@_;
- my $dbh=C4Connect;
- 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;
- $dbh->disconnect;
- return($data->{'branch'});
-}
-
-# XXX - POD. This is only used in reservereport.pl and
-# reservereport.xls, neither of which is used.
-sub unfilledreserves {
- my $dbh=C4Connect;
- 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;
- $dbh->disconnect;
- 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 accountno ccode location);
+ my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout);
+ 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 (/^$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 $accountno = exists $params->{accountno} ? $params->{accountno} : '';
+ my $ccode = exists $params->{ccode} ? $params->{ccode} : '';
+
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare(
+ "INSERT INTO statistics
+ (datetime,
+ branch, type, value,
+ other, itemnumber, itemtype, location,
+ borrowernumber, proccode, ccode)
+ VALUES (now(),?,?,?,?,?,?,?,?,?,?)"
+ );
+ $sth->execute(
+ $branch, $type, $amount, $other,
+ $itemnumber, $itemtype, $location, $borrowernumber,
+ $accountno, $ccode
+ );