View | Details | Raw Unified | Return to bug 33608
Collapse All | Expand All

(-)a/Koha/Statistic.pm (-59 / +50 lines)
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 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
(-)a/t/db_dependent/Stats.t (-12 / +5 lines)
Lines 19-24 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 1;
21
use Test::More tests => 1;
22
use Test::Exception;
22
23
23
use C4::Context;
24
use C4::Context;
24
use C4::Stats qw( UpdateStats );
25
use C4::Stats qw( UpdateStats );
Lines 29-36 $schema->storage->txn_begin; Link Here
29
my $dbh = C4::Context->dbh;
30
my $dbh = C4::Context->dbh;
30
31
31
subtest 'UpdateStats' => sub {
32
subtest 'UpdateStats' => sub {
32
    plan tests => 17;
33
    plan tests => 16;
33
    is (UpdateStats () ,undef, "UpdateStats returns undef if no params");
34
35
    throws_ok { UpdateStats() } 'Koha::Exceptions::BadParameter', 'UpdateStats called without params';
34
36
35
    my $params = {
37
    my $params = {
36
                  branch => "BRA",
38
                  branch => "BRA",
Lines 95-108 subtest 'UpdateStats' => sub { Link Here
95
    }
97
    }
96
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
98
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
97
99
98
    # returns undef and croaks if forbidden params are given
99
    $params -> {type} = "return";
100
    $params -> {newparam} = "true";
101
    eval {UpdateStats($params)};
102
    $return_error = $@;
103
    isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
104
    delete $params->{newparam};
105
106
    # save the params in the right database fields
100
    # save the params in the right database fields
107
    $dbh->do(q|DELETE FROM statistics|);
101
    $dbh->do(q|DELETE FROM statistics|);
108
    $params = {
102
    $params = {
Lines 124-130 subtest 'UpdateStats' => sub { Link Here
124
    is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
118
    is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
125
    is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
119
    is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
126
    is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
120
    is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
127
    cmp_ok($params->{amount},'==', $line->{value},          "UpdateStats save amount param in value field of statistics table");
121
    is ($params->{value},          $line->{value},          "UpdateStats save amount param in value field of statistics table");
128
    is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
122
    is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
129
    is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
123
    is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
130
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
124
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
131
- 

Return to bug 33608