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

(-)a/C4/Stats.pm (-100 / +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-153 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
70
type key is mandatory.
71
For types used in C4::Circulation (renew,issue,localuse,return), the following other keys are mandatory:
72
branch, borrowernumber, itemnumber, ccode, itemtype
73
For types used in C4::Accounts (writeoff, payment), the following other keys are mandatory:
74
branch, borrowernumber, itemnumber, ccode, itemtype
75
If an optional key is not provided, the value '' is used for this key.
76
77
Returns undef if no C<$param> is given
78
53
79
=cut
54
=cut
80
55
81
sub UpdateStats {
56
sub UpdateStats {
82
    my ($params) = @_;
57
    my $params = shift;
83
# make some controls
58
    Koha::Statistic->insert($params);
84
    return () if ! defined $params;
85
# change these arrays if new types of transaction or new parameters are allowed
86
    my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode);
87
    my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall item_found item_lost);
88
    my @allowed_accounts_types = qw (writeoff payment);
89
    my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
90
    my @accounts_mandatory_keys = qw (type branch borrowernumber amount);
91
92
    my @mandatory_keys = ();
93
    if (! exists $params->{type} or ! defined $params->{type}) {
94
        croak ("UpdateStats did not received type param");
95
    }
96
    if (grep ($_ eq $params->{type}, @allowed_circulation_types  )) {
97
        @mandatory_keys = @circulation_mandatory_keys;
98
    } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
99
        @mandatory_keys = @accounts_mandatory_keys;
100
    } else {
101
        croak ("UpdateStats received forbidden type param: ".$params->{type});
102
    }
103
    my @missing_params = ();
104
    for my $mykey (@mandatory_keys ) {
105
        push @missing_params, $mykey if !grep (/^$mykey/, keys %$params);
106
    }
107
    if (scalar @missing_params > 0 ) {
108
        croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params ));
109
    }
110
    my @invalid_params = ();
111
    for my $myparam (keys %$params ) {
112
        push @invalid_params, $myparam unless grep { $_ eq $myparam } @allowed_keys;
113
    }
114
    if (scalar @invalid_params > 0 ) {
115
        croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params ));
116
    }
117
# get the parameters
118
    my $branch            = $params->{branch};
119
    my $type              = $params->{type};
120
    my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
121
    my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber}     : undef;
122
    my $amount            = exists $params->{amount}         ? $params->{amount}         : 0;
123
    my $other             = exists $params->{other}          ? $params->{other}          : '';
124
    my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype}       : '';
125
    my $location          = exists $params->{location}       ? $params->{location}       : undef;
126
    my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
127
    my $categorycode      = exists $params->{categorycode}   ? $params->{categorycode}   : undef;
128
129
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
130
    my $statistic = Koha::Statistic->new(
131
        {
132
            datetime       => $dtf->format_datetime( dt_from_string ),
133
            branch         => $branch,
134
            type           => $type,
135
            value          => $amount,
136
            other          => $other,
137
            itemnumber     => $itemnumber,
138
            itemtype       => $itemtype,
139
            location       => $location,
140
            borrowernumber => $borrowernumber,
141
            ccode          => $ccode,
142
            categorycode   => $categorycode,
143
        }
144
    )->store;
145
146
    Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store
147
      if C4::Context->preference('Pseudonymization')
148
        && $borrowernumber # Not a real transaction if the patron does not exist
149
                           # For instance can be a transfer, or hold trigger
150
        && grep { $_ eq $params->{type} } qw(renew issue return onsite_checkout);
151
}
59
}
152
60
153
1;
61
1;
Lines 158-161 __END__ Link Here
158
Koha Development Team <http://koha-community.org/>
66
Koha Development Team <http://koha-community.org/>
159
67
160
=cut
68
=cut
161
(-)a/Koha/Statistic.pm (-5 / +134 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
56
    The type key is mandatory, see @Koha::Statistic::allowed_account_types or
57
    @Koha::Statistic::allowed_circulation_types.
58
    Some keys are mandatory, depending on type. See @mandatory_accounts_keys
59
    or @mandatory_circulation_keys.
60
61
    The method throws an exception when given bad data, otherwise returns a
62
    Koha::Statistic object, which is not yet stored.
63
64
=cut
65
66
sub new {
67
    my ( $class, $params ) = @_;
68
# make some controls
69
    return () if ! defined $params;
70
# change these arrays if new types of transaction or new parameters are allowed
71
    my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode);
72
73
    my @mandatory_keys = ();
74
    if (! exists $params->{type} or ! defined $params->{type}) {
75
        croak ("UpdateStats did not received type param");
76
    }
77
    if (grep ($_ eq $params->{type}, @allowed_circulation_types  )) {
78
        @mandatory_keys = @mandatory_circulation_keys;
79
    } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
80
        @mandatory_keys = @mandatory_accounts_keys;
81
    } else {
82
        croak ("UpdateStats received forbidden type param: ".$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
    }
98
# get the parameters
99
    my $branch            = $params->{branch};
100
    my $type              = $params->{type};
101
    my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
102
    my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber}     : undef;
103
    my $amount            = exists $params->{amount}         ? $params->{amount}         : 0;
104
    my $other             = exists $params->{other}          ? $params->{other}          : '';
105
    my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype}       : '';
106
    my $location          = exists $params->{location}       ? $params->{location}       : undef;
107
    my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
108
    my $categorycode      = exists $params->{categorycode}   ? $params->{categorycode}   : undef;
109
110
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
111
    return $class->SUPER::new(
112
        {
113
            datetime       => $dtf->format_datetime( dt_from_string ),
114
            branch         => $branch,
115
            type           => $type,
116
            value          => $amount,
117
            other          => $other,
118
            itemnumber     => $itemnumber,
119
            itemtype       => $itemtype,
120
            location       => $location,
121
            borrowernumber => $borrowernumber,
122
            categorycode   => $categorycode,
123
            ccode          => $ccode,
124
        }
125
    );
126
}
127
128
=head3 store
129
130
    $statistic->store;
131
132
    This call includes pseudonymization if enabled.
133
134
=cut
135
136
sub store {
137
    my ( $self ) = @_;
138
    $self->SUPER::store;
139
    Koha::PseudonymizedTransaction->new_from_statistic($self)->store
140
      if C4::Context->preference('Pseudonymization')
141
        && $self->borrowernumber # Not a real transaction if the patron does not exist
142
                           # For instance can be a transfer, or hold trigger
143
        && grep { $_ eq $self->type } qw(renew issue return onsite_checkout);
144
    return $self;
145
}
146
147
=head3 insert
148
149
    Koha::Statistic->insert( $params );
150
151
    This is a shorthand for ->new($params)->store.
152
    It is the new equivalent for the legacy C4::Stats::UpdateStats call.
153
154
=cut
155
156
sub insert {
157
    my ( $class, $params ) = @_;
158
    my $statistic = $class->new($params) or return;
159
    $statistic->store;
160
    return $statistic;
161
}
33
162
34
=head3 item
163
=head3 item
35
164
36
my $item = $statistic->item;
165
    my $item = $statistic->item;
37
166
38
Return the item associated to this statistic.
167
    Return the item associated to this statistic.
39
168
40
=cut
169
=cut
41
170
Lines 44-50 sub item { Link Here
44
    return Koha::Items->find( $self->itemnumber );
173
    return Koha::Items->find( $self->itemnumber );
45
}
174
}
46
175
47
=head2 Internal methods
176
=head2 INTERNAL METHODS
48
177
49
=head3 _type
178
=head3 _type
50
179
(-)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