- #module to insert stats data into stats table
- my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno)=@_;
- my $dbh = C4::Context->dbh;
- if ($branch eq ''){
- $branch=$env->{'branchcode'};
- }
- my $user = $env->{'usercode'};
- print $borrowernumber;
- # 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->finish;
+ 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);
+ my @allowed_circulation_types = qw (renew issue localuse return);
+ 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} :'';
+ my $amount = exists $params->{amount} ? $params->{amount} :'';
+ my $other = exists $params->{other} ? $params->{other} :'';
+ my $itemtype = exists $params->{itemtype} ? $params->{itemtype} :'';
+ 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,
+ borrowernumber, proccode, ccode)
+ VALUES (now(),?,?,?,?,?,?,?,?,?)"
+ );
+ $sth->execute(
+ $branch, $type, $amount,
+ $other, $itemnumber, $itemtype,
+ $borrowernumber, $accountno, $ccode
+ );