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

(-)a/C4/Stats.pm (-103 / +7 lines)
Lines 1-6 Link Here
1
package C4::Stats;
1
package C4::Stats;
2
2
3
4
# Copyright 2000-2002 Katipo Communications
3
# Copyright 2000-2002 Katipo Communications
5
#
4
#
6
# This file is part of Koha.
5
# This file is part of Koha.
Lines 19-30 package C4::Stats; Link Here
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
19
21
use Modern::Perl;
20
use Modern::Perl;
22
use Carp qw( croak );
23
use C4::Context;
24
21
25
use Koha::DateUtils qw( dt_from_string );
22
use Koha::Statistic;
26
use Koha::Statistics;
27
use Koha::PseudonymizedTransactions;
28
23
29
our (@ISA, @EXPORT_OK);
24
our (@ISA, @EXPORT_OK);
30
BEGIN {
25
BEGIN {
Lines 35-48 BEGIN { Link Here
35
    );
30
    );
36
}
31
}
37
32
38
39
=head1 NAME
33
=head1 NAME
40
34
41
C4::Stats - Update Koha statistics (log)
35
C4::Stats - Update Koha statistics (log)
42
36
43
=head1 SYNOPSIS
37
=head1 SYNOPSIS
44
38
45
  use C4::Stats;
39
    use C4::Stats;
46
40
47
=head1 DESCRIPTION
41
=head1 DESCRIPTION
48
42
Lines 52-156 The functions of this module deals with statistics table of Koha database. Link Here
52
46
53
=head2 UpdateStats
47
=head2 UpdateStats
54
48
55
  &UpdateStats($params);
49
    C4::Stats::UpdateStats($params);
56
57
Adds an entry to the statistics table in the Koha database, which acts as an activity log.
58
50
59
C<$params> is an hashref whose expected keys are:
51
    This is a (legacy) alias for Koha::Statistic->insert($params).
60
    branch             : the branch where the transaction occurred
52
    Please see Koha::Statistic module.
61
    type               : the type of transaction (renew, issue, localuse, return, writeoff, payment
62
    itemnumber         : the itemnumber of the item
63
    borrowernumber     : the borrowernumber of the patron
64
    amount             : the amount of the transaction
65
    other              : sipmode
66
    itemtype           : the type of the item
67
    ccode              : the collection code of the item
68
    categorycode       : the categorycode of the patron
69
    interface          : the context this action was taken in
70
71
type key is mandatory.
72
For types used in C4::Circulation (renew,issue,localuse,return), the following other keys are mandatory:
73
branch, borrowernumber, itemnumber, ccode, itemtype
74
For types used in C4::Accounts (writeoff, payment), the following other keys are mandatory:
75
branch, borrowernumber, itemnumber, ccode, itemtype
76
If an optional key is not provided, the value '' is used for this key.
77
78
Returns undef if no C<$param> is given
79
53
80
=cut
54
=cut
81
55
82
sub UpdateStats {
56
sub UpdateStats {
83
    my ($params) = @_;
57
    my $params = shift;
84
# make some controls
58
    Koha::Statistic->insert($params);
85
    return () if ! defined $params;
86
# change these arrays if new types of transaction or new parameters are allowed
87
    my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode interface);
88
    my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall item_found item_lost);
89
    my @allowed_accounts_types = qw (writeoff payment);
90
    my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
91
    my @accounts_mandatory_keys = qw (type branch borrowernumber amount);
92
93
    my @mandatory_keys = ();
94
    if (! exists $params->{type} or ! defined $params->{type}) {
95
        croak ("UpdateStats did not received type param");
96
    }
97
    if (grep ($_ eq $params->{type}, @allowed_circulation_types  )) {
98
        @mandatory_keys = @circulation_mandatory_keys;
99
    } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
100
        @mandatory_keys = @accounts_mandatory_keys;
101
    } else {
102
        croak ("UpdateStats received forbidden type param: ".$params->{type});
103
    }
104
    my @missing_params = ();
105
    for my $mykey (@mandatory_keys ) {
106
        push @missing_params, $mykey if !grep (/^$mykey/, keys %$params);
107
    }
108
    if (scalar @missing_params > 0 ) {
109
        croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params ));
110
    }
111
    my @invalid_params = ();
112
    for my $myparam (keys %$params ) {
113
        push @invalid_params, $myparam unless grep { $_ eq $myparam } @allowed_keys;
114
    }
115
    if (scalar @invalid_params > 0 ) {
116
        croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params ));
117
    }
118
# get the parameters
119
    my $branch            = $params->{branch};
120
    my $type              = $params->{type};
121
    my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
122
    my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber}     : undef;
123
    my $amount            = exists $params->{amount}         ? $params->{amount}         : 0;
124
    my $other             = exists $params->{other}          ? $params->{other}          : '';
125
    my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype}       : '';
126
    my $location          = exists $params->{location}       ? $params->{location}       : undef;
127
    my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
128
    my $categorycode      = exists $params->{categorycode}   ? $params->{categorycode}   : undef;
129
    my $interface         = exists $params->{interface}      ? $params->{interface}      : undef;
130
131
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
132
    my $statistic = Koha::Statistic->new(
133
        {
134
            datetime       => $dtf->format_datetime( dt_from_string ),
135
            branch         => $branch,
136
            type           => $type,
137
            value          => $amount,
138
            other          => $other,
139
            itemnumber     => $itemnumber,
140
            itemtype       => $itemtype,
141
            location       => $location,
142
            borrowernumber => $borrowernumber,
143
            ccode          => $ccode,
144
            categorycode   => $categorycode,
145
            interface      => $interface,
146
        }
147
    )->store;
148
149
    Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store
150
      if C4::Context->preference('Pseudonymization')
151
        && $borrowernumber # Not a real transaction if the patron does not exist
152
                           # For instance can be a transfer, or hold trigger
153
        && grep { $_ eq $params->{type} } qw(renew issue return onsite_checkout);
154
}
59
}
155
60
156
1;
61
1;
Lines 161-164 __END__ Link Here
161
Koha Development Team <http://koha-community.org/>
66
Koha Development Team <http://koha-community.org/>
162
67
163
=cut
68
=cut
164
(-)a/Koha/Statistic.pm (-5 / +137 lines)
Lines 17-41 package Koha::Statistic; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
20
use C4::Context;
21
use Koha::Database;
21
use Koha::Database;
22
use Koha::DateUtils qw/dt_from_string/;
22
use Koha::Items;
23
use Koha::Items;
24
use Koha::PseudonymizedTransaction;
23
25
24
use base qw(Koha::Object);
26
use base qw(Koha::Object);
25
27
28
our @allowed_accounts_types     = qw( writeoff payment );
29
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 );
31
our @mandatory_circulation_keys = qw( type branch borrowernumber itemnumber ccode itemtype );
32
26
=head1 NAME
33
=head1 NAME
27
34
28
Koha::Statistic - Koha Statistic Object class
35
Koha::Statistic - Koha Statistic Object class
29
36
30
=head1 API
37
=head1 API
31
38
32
=head2 Class methods
39
=head2 METHODS
40
41
=head3 new
42
43
    my $record = Koha::Statistic->new( $params );
44
45
    C<$params> is an hashref whose expected keys are:
46
    branch             : transaction branch
47
    type               : transaction type
48
    itemnumber         : itemnumber
49
    borrowernumber     : borrowernumber
50
    categorycode       : patron category
51
    amount             : transaction amount
52
    other              : sipmode
53
    itemtype           : itemtype
54
    ccode              : collection code
55
    interface          : the context this action was taken in
56
57
    The type key is mandatory, see @Koha::Statistic::allowed_account_types or
58
    @Koha::Statistic::allowed_circulation_types.
59
    Some keys are mandatory, depending on type. See @mandatory_accounts_keys
60
    or @mandatory_circulation_keys.
61
62
    The method throws an exception when given bad data, otherwise returns a
63
    Koha::Statistic object, which is not yet stored.
64
65
=cut
66
67
sub new {
68
    my ( $class, $params ) = @_;
69
# make some controls
70
    return () if ! defined $params;
71
# change these arrays if new types of transaction or new parameters are allowed
72
    my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode interface);
73
74
    my @mandatory_keys = ();
75
    if (! exists $params->{type} or ! defined $params->{type}) {
76
        croak ("UpdateStats did not received type param");
77
    }
78
    if (grep ($_ eq $params->{type}, @allowed_circulation_types  )) {
79
        @mandatory_keys = @mandatory_circulation_keys;
80
    } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
81
        @mandatory_keys = @mandatory_accounts_keys;
82
    } else {
83
        croak ("UpdateStats received forbidden type param: ".$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
    }
99
# get the parameters
100
    my $branch            = $params->{branch};
101
    my $type              = $params->{type};
102
    my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
103
    my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber}     : undef;
104
    my $amount            = exists $params->{amount}         ? $params->{amount}         : 0;
105
    my $other             = exists $params->{other}          ? $params->{other}          : '';
106
    my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype}       : '';
107
    my $location          = exists $params->{location}       ? $params->{location}       : undef;
108
    my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
109
    my $categorycode      = exists $params->{categorycode}   ? $params->{categorycode}   : undef;
110
    my $interface         = exists $params->{interface}      ? $params->{interface}      : undef;
111
112
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
113
    return $class->SUPER::new(
114
        {
115
            datetime       => $dtf->format_datetime( dt_from_string ),
116
            branch         => $branch,
117
            type           => $type,
118
            value          => $amount,
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
}
130
131
=head3 store
132
133
    $statistic->store;
134
135
    This call includes pseudonymization if enabled.
136
137
=cut
138
139
sub store {
140
    my ( $self ) = @_;
141
    $self->SUPER::store;
142
    Koha::PseudonymizedTransaction->new_from_statistic($self)->store
143
      if C4::Context->preference('Pseudonymization')
144
        && $self->borrowernumber # Not a real transaction if the patron does not exist
145
                           # For instance can be a transfer, or hold trigger
146
        && grep { $_ eq $self->type } qw(renew issue return onsite_checkout);
147
    return $self;
148
}
149
150
=head3 insert
151
152
    Koha::Statistic->insert( $params );
153
154
    This is a shorthand for ->new($params)->store.
155
    It is the new equivalent for the legacy C4::Stats::UpdateStats call.
156
157
=cut
158
159
sub insert {
160
    my ( $class, $params ) = @_;
161
    my $statistic = $class->new($params) or return;
162
    $statistic->store;
163
    return $statistic;
164
}
33
165
34
=head3 item
166
=head3 item
35
167
36
my $item = $statistic->item;
168
    my $item = $statistic->item;
37
169
38
Return the item associated to this statistic.
170
    Return the item associated to this statistic.
39
171
40
=cut
172
=cut
41
173
Lines 44-50 sub item { Link Here
44
    return Koha::Items->find( $self->itemnumber );
176
    return Koha::Items->find( $self->itemnumber );
45
}
177
}
46
178
47
=head2 Internal methods
179
=head2 INTERNAL METHODS
48
180
49
=head3 _type
181
=head3 _type
50
182
(-)a/t/db_dependent/Koha/Pseudonymization.t (-1 / +1 lines)
Lines 29-34 use Koha::Database; Link Here
29
use Koha::DateUtils qw( dt_from_string );
29
use Koha::DateUtils qw( dt_from_string );
30
use Koha::Patrons;
30
use Koha::Patrons;
31
use Koha::PseudonymizedTransactions;
31
use Koha::PseudonymizedTransactions;
32
use Koha::Statistics;
32
33
33
use t::lib::TestBuilder;
34
use t::lib::TestBuilder;
34
use t::lib::Mocks;
35
use t::lib::Mocks;
35
- 

Return to bug 33608