Lines 1-5
Link Here
|
1 |
package Koha::Statistic; |
1 |
package Koha::Statistic; |
2 |
|
2 |
|
|
|
3 |
# Copyright 2019, 2023 Koha development team |
4 |
# |
3 |
# This file is part of Koha. |
5 |
# This file is part of Koha. |
4 |
# |
6 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
7 |
# Koha is free software; you can redistribute it and/or modify it |
Lines 27-33
use base qw(Koha::Object);
Link Here
|
27 |
|
29 |
|
28 |
our @allowed_accounts_types = qw( writeoff payment ); |
30 |
our @allowed_accounts_types = qw( writeoff payment ); |
29 |
our @allowed_circulation_types = qw( renew issue localuse return onsite_checkout recall item_found item_lost ); |
31 |
our @allowed_circulation_types = qw( renew issue localuse return onsite_checkout recall item_found item_lost ); |
30 |
our @mandatory_accounts_keys = qw( type branch borrowernumber amount ); |
32 |
our @mandatory_accounts_keys = qw( type branch borrowernumber value ); # note that amount is mapped to value |
31 |
our @mandatory_circulation_keys = qw( type branch borrowernumber itemnumber ccode itemtype ); |
33 |
our @mandatory_circulation_keys = qw( type branch borrowernumber itemnumber ccode itemtype ); |
32 |
|
34 |
|
33 |
=head1 NAME |
35 |
=head1 NAME |
Lines 48-54
Koha::Statistic - Koha Statistic Object class
Link Here
|
48 |
itemnumber : itemnumber |
50 |
itemnumber : itemnumber |
49 |
borrowernumber : borrowernumber |
51 |
borrowernumber : borrowernumber |
50 |
categorycode : patron category |
52 |
categorycode : patron category |
51 |
amount : transaction amount |
53 |
amount : transaction amount (legacy parameter name) |
|
|
54 |
value : transaction amount |
52 |
other : sipmode |
55 |
other : sipmode |
53 |
itemtype : itemtype |
56 |
itemtype : itemtype |
54 |
ccode : collection code |
57 |
ccode : collection code |
Lines 67-133
Koha::Statistic - Koha Statistic Object class
Link Here
|
67 |
sub new { |
70 |
sub new { |
68 |
my ( $class, $params ) = @_; |
71 |
my ( $class, $params ) = @_; |
69 |
|
72 |
|
70 |
# make some controls |
73 |
Koha::Exceptions::BadParameter->throw( parameter => $params ) if !$params || ref($params) ne 'HASH'; |
71 |
return () if !defined $params; |
74 |
Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} ) if !$params->{type}; |
72 |
|
|
|
73 |
# change these arrays if new types of transaction or new parameters are allowed |
74 |
my @allowed_keys = |
75 |
qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode interface); |
76 |
|
75 |
|
77 |
my @mandatory_keys = (); |
76 |
if ( exists $params->{amount} ) { # legacy amount parameter still supported |
78 |
if ( !exists $params->{type} or !defined $params->{type} ) { |
77 |
$params->{value} //= delete $params->{amount}; |
79 |
croak("UpdateStats did not received type param"); |
|
|
80 |
} |
78 |
} |
81 |
if ( grep ( $_ eq $params->{type}, @allowed_circulation_types ) ) { |
79 |
|
82 |
@mandatory_keys = @mandatory_circulation_keys; |
80 |
my $category; |
83 |
} elsif ( grep ( $_ eq $params->{type}, @allowed_accounts_types ) ) { |
81 |
if ( grep { $_ eq $params->{type} } @allowed_circulation_types ) { |
84 |
@mandatory_keys = @mandatory_accounts_keys; |
82 |
$category = 'circulation'; |
|
|
83 |
} elsif ( grep { $_ eq $params->{type} } @allowed_accounts_types ) { |
84 |
$category = 'accounts'; |
85 |
} else { |
85 |
} else { |
86 |
croak( "UpdateStats received forbidden type param: " . $params->{type} ); |
86 |
Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} ); |
87 |
} |
|
|
88 |
my @missing_params = (); |
89 |
for my $mykey (@mandatory_keys) { |
90 |
push @missing_params, $mykey if !grep ( /^$mykey/, keys %$params ); |
91 |
} |
92 |
if ( scalar @missing_params > 0 ) { |
93 |
croak( "UpdateStats did not received mandatory param(s): " . join( ", ", @missing_params ) ); |
94 |
} |
95 |
my @invalid_params = (); |
96 |
for my $myparam ( keys %$params ) { |
97 |
push @invalid_params, $myparam unless grep { $_ eq $myparam } @allowed_keys; |
98 |
} |
99 |
if ( scalar @invalid_params > 0 ) { |
100 |
croak( "UpdateStats received invalid param(s): " . join( ", ", @invalid_params ) ); |
101 |
} |
87 |
} |
102 |
|
88 |
|
103 |
# get the parameters |
89 |
my @mandatory_keys = $category eq 'circulation' ? @mandatory_circulation_keys : @mandatory_accounts_keys; |
104 |
my $branch = $params->{branch}; |
90 |
my @missing = map { exists $params->{$_} ? () : $_ } @mandatory_keys; |
105 |
my $type = $params->{type}; |
91 |
Koha::Exceptions::MissingParameter->throw( parameter => join( ',', @missing ) ) if @missing; |
106 |
my $borrowernumber = exists $params->{borrowernumber} ? $params->{borrowernumber} : ''; |
92 |
|
107 |
my $itemnumber = exists $params->{itemnumber} ? $params->{itemnumber} : undef; |
93 |
my $datetime = Koha::Database->new->schema->storage->datetime_parser->format_datetime( dt_from_string() ); |
108 |
my $amount = exists $params->{amount} ? $params->{amount} : 0; |
|
|
109 |
my $other = exists $params->{other} ? $params->{other} : ''; |
110 |
my $itemtype = exists $params->{itemtype} ? $params->{itemtype} : ''; |
111 |
my $location = exists $params->{location} ? $params->{location} : undef; |
112 |
my $ccode = exists $params->{ccode} ? $params->{ccode} : ''; |
113 |
my $categorycode = exists $params->{categorycode} ? $params->{categorycode} : undef; |
114 |
my $interface = exists $params->{interface} ? $params->{interface} : undef; |
115 |
|
116 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
117 |
return $class->SUPER::new( |
94 |
return $class->SUPER::new( |
118 |
{ |
95 |
{ |
119 |
datetime => $dtf->format_datetime( dt_from_string() ), |
96 |
borrowernumber => $params->{borrowernumber}, # no longer sending empty string (changed 2023) |
120 |
branch => $branch, |
97 |
branch => $params->{branch}, |
121 |
type => $type, |
98 |
categorycode => $params->{categorycode}, |
122 |
value => $amount, |
99 |
ccode => exists $params->{ccode} ? $params->{ccode} : q{}, |
123 |
other => $other, |
100 |
datetime => $datetime, |
124 |
itemnumber => $itemnumber, |
101 |
interface => $params->{interface}, |
125 |
itemtype => $itemtype, |
102 |
itemnumber => $params->{itemnumber}, |
126 |
location => $location, |
103 |
itemtype => exists $params->{itemtype} ? $params->{itemtype} : q{}, |
127 |
borrowernumber => $borrowernumber, |
104 |
location => $params->{location}, |
128 |
categorycode => $categorycode, |
105 |
other => exists $params->{other} ? $params->{other} : q{}, |
129 |
ccode => $ccode, |
106 |
type => $params->{type}, |
130 |
interface => $interface, |
107 |
value => exists $params->{value} ? $params->{value} : 0, |
|
|
108 |
|
131 |
} |
109 |
} |
132 |
); |
110 |
); |
133 |
} |
111 |
} |