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 66-131
Koha::Statistic - Koha Statistic Object class
Link Here
|
66 |
|
69 |
|
67 |
sub new { |
70 |
sub new { |
68 |
my ( $class, $params ) = @_; |
71 |
my ( $class, $params ) = @_; |
69 |
# make some controls |
72 |
|
70 |
return () if ! defined $params; |
73 |
Koha::Exceptions::BadParameter->throw( parameter => $params ) if !$params || ref($params) ne 'HASH'; |
71 |
# change these arrays if new types of transaction or new parameters are allowed |
74 |
Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} ) if !$params->{type}; |
72 |
my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode interface); |
75 |
|
73 |
|
76 |
$params->{value} //= delete $params->{amount}; # legacy amount parameter supported |
74 |
my @mandatory_keys = (); |
77 |
|
75 |
if (! exists $params->{type} or ! defined $params->{type}) { |
78 |
my $category; |
76 |
croak ("UpdateStats did not received type param"); |
79 |
if( grep { $_ eq $params->{type} } @allowed_circulation_types ) { |
77 |
} |
80 |
$category = 'circulation'; |
78 |
if (grep ($_ eq $params->{type}, @allowed_circulation_types )) { |
81 |
} elsif( grep { $_ eq $params->{type} } @allowed_accounts_types ) { |
79 |
@mandatory_keys = @mandatory_circulation_keys; |
82 |
$category = 'accounts'; |
80 |
} elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) { |
|
|
81 |
@mandatory_keys = @mandatory_accounts_keys; |
82 |
} else { |
83 |
} else { |
83 |
croak ("UpdateStats received forbidden type param: ".$params->{type}); |
84 |
Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} ); |
84 |
} |
|
|
85 |
my @missing_params = (); |
86 |
for my $mykey (@mandatory_keys ) { |
87 |
push @missing_params, $mykey if !grep (/^$mykey/, keys %$params); |
88 |
} |
89 |
if (scalar @missing_params > 0 ) { |
90 |
croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params )); |
91 |
} |
92 |
my @invalid_params = (); |
93 |
for my $myparam (keys %$params ) { |
94 |
push @invalid_params, $myparam unless grep { $_ eq $myparam } @allowed_keys; |
95 |
} |
96 |
if (scalar @invalid_params > 0 ) { |
97 |
croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params )); |
98 |
} |
85 |
} |
99 |
# get the parameters |
86 |
|
100 |
my $branch = $params->{branch}; |
87 |
my @mandatory_keys = $category eq 'circulation' ? @mandatory_circulation_keys : @mandatory_accounts_keys; |
101 |
my $type = $params->{type}; |
88 |
my $first; |
102 |
my $borrowernumber = exists $params->{borrowernumber} ? $params->{borrowernumber} : ''; |
89 |
Koha::Exceptions::MissingParameter->throw( parameter => $first ) |
103 |
my $itemnumber = exists $params->{itemnumber} ? $params->{itemnumber} : undef; |
90 |
if grep { exists $params->{$_} ? 0 : ( $first ||= $_ ) } @mandatory_keys; |
104 |
my $amount = exists $params->{amount} ? $params->{amount} : 0; |
91 |
|
105 |
my $other = exists $params->{other} ? $params->{other} : ''; |
92 |
return $class->SUPER::new({ |
106 |
my $itemtype = exists $params->{itemtype} ? $params->{itemtype} : ''; |
93 |
datetime => Koha::Database->new->schema->storage->datetime_parser->format_datetime( dt_from_string ), |
107 |
my $location = exists $params->{location} ? $params->{location} : undef; |
94 |
branch => $params->{branch}, |
108 |
my $ccode = exists $params->{ccode} ? $params->{ccode} : ''; |
95 |
type => $params->{type}, |
109 |
my $categorycode = exists $params->{categorycode} ? $params->{categorycode} : undef; |
96 |
value => _key_or_default( $params, 'value', 0 ), |
110 |
my $interface = exists $params->{interface} ? $params->{interface} : undef; |
97 |
other => _key_or_default( $params, 'other', q{} ), |
111 |
|
98 |
itemnumber => $params->{itemnumber}, |
112 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
99 |
itemtype => _key_or_default( $params, 'itemtype', q{} ), |
113 |
return $class->SUPER::new( |
100 |
location => $params->{location}, |
114 |
{ |
101 |
borrowernumber => $params->{borrowernumber}, # no longer sending empty string (changed 2023) |
115 |
datetime => $dtf->format_datetime( dt_from_string ), |
102 |
categorycode => $params->{categorycode}, |
116 |
branch => $branch, |
103 |
ccode => _key_or_default( $params, 'ccode', q{} ), |
117 |
type => $type, |
104 |
interface => $params->{interface}, |
118 |
value => $amount, |
105 |
}); |
119 |
other => $other, |
|
|
120 |
itemnumber => $itemnumber, |
121 |
itemtype => $itemtype, |
122 |
location => $location, |
123 |
borrowernumber => $borrowernumber, |
124 |
categorycode => $categorycode, |
125 |
ccode => $ccode, |
126 |
interface => $interface, |
127 |
} |
128 |
); |
129 |
} |
106 |
} |
130 |
|
107 |
|
131 |
=head3 store |
108 |
=head3 store |
Lines 178-183
sub item {
Link Here
|
178 |
|
155 |
|
179 |
=head2 INTERNAL METHODS |
156 |
=head2 INTERNAL METHODS |
180 |
|
157 |
|
|
|
158 |
=head3 _key_or_default |
159 |
|
160 |
$value = _key_or_default( $hash, $key, $default ); |
161 |
|
162 |
Returns $hash->{$key} if it exists, otherwise $default. |
163 |
Note: this is not the same as $hash->{key} // $default ! |
164 |
|
165 |
=cut |
166 |
|
167 |
sub _key_or_default { |
168 |
my ( $hash, $key, $default ) = @_; |
169 |
return exists $hash->{$key} ? $hash->{$key} : $default; |
170 |
} |
171 |
|
181 |
=head3 _type |
172 |
=head3 _type |
182 |
|
173 |
|
183 |
=cut |
174 |
=cut |