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