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

(-)a/Koha/Statistic.pm (-57 / +49 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 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
(-)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 => 16;
33
    plan tests => 15;
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 94-107 subtest 'UpdateStats' => sub { Link Here
94
    }
96
    }
95
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
97
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
96
98
97
    # returns undef and croaks if forbidden params are given
98
    $params -> {type} = "return";
99
    $params -> {newparam} = "true";
100
    eval {UpdateStats($params)};
101
    $return_error = $@;
102
    isnt ($return_error,'',"UpdateStats returns undef and croaks if a forbidden param is given");
103
    delete $params->{newparam};
104
105
    # save the params in the right database fields
99
    # save the params in the right database fields
106
    $dbh->do(q|DELETE FROM statistics|);
100
    $dbh->do(q|DELETE FROM statistics|);
107
    $params = {
101
    $params = {
Lines 122-128 subtest 'UpdateStats' => sub { Link Here
122
    is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
116
    is ($params->{branch},         $line->{branch},         "UpdateStats save branch param in branch field of statistics table");
123
    is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
117
    is ($params->{type},           $line->{type},           "UpdateStats save type param in type field of statistics table");
124
    is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
118
    is ($params->{borrowernumber}, $line->{borrowernumber}, "UpdateStats save borrowernumber param in borrowernumber field of statistics table");
125
    cmp_ok($params->{amount},'==', $line->{value},          "UpdateStats save amount param in value field of statistics table");
119
    is ($params->{value},          $line->{value},          "UpdateStats save amount param in value field of statistics table");
126
    is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
120
    is ($params->{other},          $line->{other},          "UpdateStats save other param in other field of statistics table");
127
    is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
121
    is ($params->{itemtype},       $line->{itemtype},       "UpdateStats save itemtype param in itemtype field of statistics table");
128
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
122
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
129
- 

Return to bug 33608