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

(-)a/Koha/Statistic.pm (-56 / +34 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 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
}
(-)a/t/db_dependent/Stats.t (-17 / +12 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-42 $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",
37
                  itemnumber => 31,
39
                  itemnumber => 31,
38
                  borrowernumber => 5,
40
                  borrowernumber => 5,
39
                  amount =>5.1,
41
                  amount => 5.1,
40
                  other => "bla",
42
                  other => "bla",
41
                  itemtype => "BK",
43
                  itemtype => "BK",
42
                  location => "LOC",
44
                  location => "LOC",
Lines 64-76 subtest 'UpdateStats' => sub { Link Here
64
    isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
66
    isnt ($return_error,'',"UpdateStats returns undef and croaks if type is undef");
65
67
66
    # returns undef and croaks if mandatory params are missing
68
    # returns undef and croaks if mandatory params are missing
67
    my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
69
    my @allowed_circulation_types  = @Koha::Statistic::allowed_circulation_types;
68
    my @allowed_accounts_types = qw (writeoff payment);
70
    my @allowed_accounts_types     = @Koha::Statistic::allowed_accounts_types;
69
    my @circulation_mandatory_keys = qw (branch borrowernumber itemnumber ccode itemtype); #don't check type here
71
    my @circulation_mandatory_keys = @Koha::Statistic::mandatory_circulation_keys;
70
    my @accounts_mandatory_keys = qw (branch borrowernumber amount); #don't check type here
72
    my @accounts_mandatory_keys    = @Koha::Statistic::mandatory_accounts_keys;
71
73
72
    my @missing_errors = ();
74
    my @missing_errors = ();
73
    foreach my $key (@circulation_mandatory_keys) {
75
    foreach my $key (@circulation_mandatory_keys) {
76
        next if $key eq 'type';
74
        my $value = $params->{$key};
77
        my $value = $params->{$key};
75
        delete $params->{$key};
78
        delete $params->{$key};
76
        foreach my $type (@allowed_circulation_types) {
79
        foreach my $type (@allowed_circulation_types) {
Lines 82-87 subtest 'UpdateStats' => sub { Link Here
82
        $params->{$key} = $value;
85
        $params->{$key} = $value;
83
    }
86
    }
84
    foreach my $key (@accounts_mandatory_keys) {
87
    foreach my $key (@accounts_mandatory_keys) {
88
        next if $key eq 'type';
85
        my $value = $params->{$key};
89
        my $value = $params->{$key};
86
        delete $params->{$key};
90
        delete $params->{$key};
87
        foreach my $type (@allowed_accounts_types) {
91
        foreach my $type (@allowed_accounts_types) {
Lines 95-108 subtest 'UpdateStats' => sub { Link Here
95
    }
99
    }
96
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
100
    is (join (", ", @missing_errors),'',"UpdateStats returns undef and croaks if mandatory params are missing");
97
101
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
102
    # save the params in the right database fields
107
    $dbh->do(q|DELETE FROM statistics|);
103
    $dbh->do(q|DELETE FROM statistics|);
108
    $params = {
104
    $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");
120
    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");
121
    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");
122
    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");
123
    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");
124
    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");
125
    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");
126
    is ($params->{location},       $line->{location},       "UpdateStats save location param in location field of statistics table");
131
- 

Return to bug 33608