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

(-)a/Koha/Item.pm (-15 / +30 lines)
Lines 130-137 sub store { Link Here
130
            $self->cn_sort($cn_sort);
130
            $self->cn_sort($cn_sort);
131
        }
131
        }
132
132
133
        if( $self->itemlost ) {
133
        # should be quite rare when adding item
134
            $self->_add_statistic('item_lost'); # should be quite rare when adding item
134
        if ( $self->itemlost ) {
135
            $self->_add_statistic('item_lost');
135
        }
136
        }
136
137
137
    } else { # ModItem
138
    } else { # ModItem
Lines 189-198 sub store { Link Here
189
            $self->permanent_location( $self->location );
190
            $self->permanent_location( $self->location );
190
        }
191
        }
191
192
192
        if( exists $updated_columns{itemlost} && !$updated_columns{itemlost} && $pre_mod_item->itemlost ) { # item found again
193
        if (   exists $updated_columns{itemlost}
193
            $self->_set_found_trigger($pre_mod_item); # reverse any list item charges if necessary
194
            && !$updated_columns{itemlost}
195
            && $pre_mod_item->itemlost )
196
        {
197
            # item found
198
            # reverse any list item charges if necessary
199
            $self->_set_found_trigger($pre_mod_item);
194
            $self->_add_statistic('item_found');
200
            $self->_add_statistic('item_found');
195
        } elsif( exists $updated_columns{itemlost} && $updated_columns{itemlost} && !$pre_mod_item->itemlost ) { # item lost
201
        }
202
        elsif (exists $updated_columns{itemlost}
203
            && $updated_columns{itemlost}
204
            && !$pre_mod_item->itemlost )
205
        {
206
            # item lost
196
            $self->_add_statistic('item_lost');
207
            $self->_add_statistic('item_lost');
197
        }
208
        }
198
    }
209
    }
Lines 219-234 sub store { Link Here
219
230
220
sub _add_statistic {
231
sub _add_statistic {
221
    my ( $self, $type ) = @_;
232
    my ( $self, $type ) = @_;
222
    C4::Stats::UpdateStats({
233
    C4::Stats::UpdateStats(
223
        type           => $type,
234
        {
224
        branch         => C4::Context->userenv ? C4::Context->userenv->{branch} : undef,
235
            type   => $type,
225
        borrowernumber => undef,
236
            branch => C4::Context->userenv
226
        categorycode   => undef,
237
            ? C4::Context->userenv->{branch}
227
        itemnumber     => $self->itemnumber,
238
            : undef,
228
        ccode          => $self->ccode,
239
            borrowernumber => undef,
229
        itemtype       => $self->effective_itemtype,
240
            categorycode   => undef,
230
        location       => $self->location,
241
            itemnumber     => $self->itemnumber,
231
    });
242
            ccode          => $self->ccode,
243
            itemtype       => $self->effective_itemtype,
244
            location       => $self->location,
245
        }
246
    );
232
}
247
}
233
248
234
=head3 delete
249
=head3 delete
(-)a/Koha/Statistic.pm (-26 / +38 lines)
Lines 70-110 Koha::Statistic - Koha Statistic Object class Link Here
70
sub new {
70
sub new {
71
    my ( $class, $params ) = @_;
71
    my ( $class, $params ) = @_;
72
72
73
    Koha::Exceptions::BadParameter->throw( parameter => $params ) if !$params || ref($params) ne 'HASH';
73
    Koha::Exceptions::BadParameter->throw( parameter => $params )
74
    Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} ) if !$params->{type};
74
        if !$params || ref($params) ne 'HASH';
75
    Koha::Exceptions::WrongParameter->throw(
76
        name  => 'type',
77
        value => $params->{type}
78
    ) if !$params->{type};
75
79
76
    if ( $params->{amount} ) {
80
    if ( $params->{amount} ) {
77
        $params->{value} //= delete $params->{amount}; # legacy amount parameter supported
81
        $params->{value} //= delete $params->{amount};    # legacy amount parameter supported
78
    }
82
    }
79
83
80
    my $category;
84
    my $category;
81
    if( grep { $_ eq $params->{type} } @allowed_circulation_types ) {
85
    if ( grep { $_ eq $params->{type} } @allowed_circulation_types ) {
82
        $category = 'circulation';
86
        $category = 'circulation';
83
    } elsif( grep { $_ eq $params->{type} } @allowed_accounts_types ) {
87
    } elsif ( grep { $_ eq $params->{type} } @allowed_accounts_types ) {
84
        $category = 'accounts';
88
        $category = 'accounts';
85
    } else {
89
    } else {
86
        Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} );
90
        Koha::Exceptions::WrongParameter->throw(
91
            name  => 'type',
92
            value => $params->{type}
93
        );
87
    }
94
    }
88
95
89
    my @mandatory_keys = $category eq 'circulation' ? @mandatory_circulation_keys : @mandatory_accounts_keys;
96
    my @mandatory_keys =
97
          $category eq 'circulation'
98
        ? @mandatory_circulation_keys
99
        : @mandatory_accounts_keys;
90
    my $first;
100
    my $first;
91
    Koha::Exceptions::MissingParameter->throw( parameter => $first )
101
    Koha::Exceptions::MissingParameter->throw( parameter => $first )
92
        if grep { exists $params->{$_} ? 0 : ( $first ||= $_ ) } @mandatory_keys;
102
        if grep { exists $params->{$_} ? 0 : ( $first ||= $_ ) } @mandatory_keys;
93
103
94
    return $class->SUPER::new({
104
    return $class->SUPER::new(
95
        datetime       => Koha::Database->new->schema->storage->datetime_parser->format_datetime( dt_from_string ),
105
        {
96
        branch         => $params->{branch},
106
            datetime       => Koha::Database->new->schema->storage->datetime_parser->format_datetime(dt_from_string),
97
        type           => $params->{type},
107
            branch         => $params->{branch},
98
        value          => _key_or_default( $params, 'value', 0 ),
108
            type           => $params->{type},
99
        other          => _key_or_default( $params, 'other', q{} ),
109
            value          => _key_or_default( $params, 'value', 0 ),
100
        itemnumber     => $params->{itemnumber},
110
            other          => _key_or_default( $params, 'other', q{} ),
101
        itemtype       => _key_or_default( $params, 'itemtype', q{} ),
111
            itemnumber     => $params->{itemnumber},
102
        location       => $params->{location},
112
            itemtype       => _key_or_default( $params, 'itemtype', q{} ),
103
        borrowernumber => $params->{borrowernumber}, # no longer sending empty string (changed 2023)
113
            location       => $params->{location},
104
        categorycode   => $params->{categorycode},
114
            borrowernumber => $params->{borrowernumber},                 # no longer sending empty string (changed 2023)
105
        ccode          => _key_or_default( $params, 'ccode', q{} ),
115
            categorycode   => $params->{categorycode},
106
        interface      => $params->{interface} // C4::Context->interface,
116
            ccode          => _key_or_default( $params, 'ccode', q{} ),
107
    });
117
            interface      => $params->{interface} // C4::Context->interface,
118
        }
119
    );
108
}
120
}
109
121
110
=head3 store
122
=head3 store
Lines 116-127 sub new { Link Here
116
=cut
128
=cut
117
129
118
sub store {
130
sub store {
119
    my ( $self ) = @_;
131
    my ($self) = @_;
120
    $self->SUPER::store;
132
    $self->SUPER::store;
121
    Koha::PseudonymizedTransaction->new_from_statistic($self)->store
133
    Koha::PseudonymizedTransaction->new_from_statistic($self)->store
122
      if C4::Context->preference('Pseudonymization')
134
        if C4::Context->preference('Pseudonymization')
123
        && $self->borrowernumber # Not a real transaction if the patron does not exist
135
        && $self->borrowernumber    # Not a real transaction if the patron does not exist
124
                           # For instance can be a transfer, or hold trigger
136
                                    # For instance can be a transfer, or hold trigger
125
        && grep { $_ eq $self->type } qw(renew issue return onsite_checkout);
137
        && grep { $_ eq $self->type } qw(renew issue return onsite_checkout);
126
    return $self;
138
    return $self;
127
}
139
}
Lines 151-157 sub insert { Link Here
151
=cut
163
=cut
152
164
153
sub item {
165
sub item {
154
    my ( $self ) = @_;
166
    my ($self) = @_;
155
    return Koha::Items->find( $self->itemnumber );
167
    return Koha::Items->find( $self->itemnumber );
156
}
168
}
157
169
(-)a/t/db_dependent/Koha/Statistics.t (-46 / +51 lines)
Lines 28-47 use Koha::Statistics; Link Here
28
28
29
use t::lib::TestBuilder;
29
use t::lib::TestBuilder;
30
30
31
our $schema = Koha::Database->new->schema;
31
our $schema  = Koha::Database->new->schema;
32
our $builder = t::lib::TestBuilder->new;
32
our $builder = t::lib::TestBuilder->new;
33
33
34
our $test_params = { # No FK checks here
34
our $test_params = {    # No FK checks here
35
    branch => "BRA",
35
    branch         => "BRA",
36
    itemnumber => 31,
36
    itemnumber     => 31,
37
    borrowernumber => 5,
37
    borrowernumber => 5,
38
    categorycode => 'S',
38
    categorycode   => 'S',
39
    amount => 5.1,
39
    amount         => 5.1,
40
    other => "bla",
40
    other          => "bla",
41
    itemtype => "BK",
41
    itemtype       => "BK",
42
    location => "LOC",
42
    location       => "LOC",
43
    ccode => "CODE",
43
    ccode          => "CODE",
44
    interface => 'INTERFACE',
44
    interface      => 'INTERFACE',
45
};
45
};
46
46
47
subtest 'Basic Koha object tests' => sub {
47
subtest 'Basic Koha object tests' => sub {
Lines 52-73 subtest 'Basic Koha object tests' => sub { Link Here
52
    my $item    = $builder->build_sample_item;
52
    my $item    = $builder->build_sample_item;
53
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
53
    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
54
54
55
    Koha::Statistic->insert({
55
    Koha::Statistic->insert(
56
        type           => 'issue',
56
        {
57
        branch         => $library->branchcode,
57
            type           => 'issue',
58
        itemnumber     => $item->itemnumber,
58
            branch         => $library->branchcode,
59
        borrowernumber => $patron->borrowernumber,
59
            itemnumber     => $item->itemnumber,
60
        itemtype       => $item->effective_itemtype,
60
            borrowernumber => $patron->borrowernumber,
61
        location       => $item->location,
61
            itemtype       => $item->effective_itemtype,
62
        ccode          => $item->ccode,
62
            location       => $item->location,
63
        interface      => C4::Context->interface,
63
            ccode          => $item->ccode,
64
    });
64
            interface      => C4::Context->interface,
65
        }
66
    );
65
67
66
    my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
68
    my $stat = Koha::Statistics->search( { itemnumber => $item->itemnumber } )->next;
67
    is( $stat->borrowernumber, $patron->borrowernumber, 'Patron is there' );
69
    is( $stat->borrowernumber,   $patron->borrowernumber, 'Patron is there' );
68
    is( $stat->branch,         $library->branchcode,    'Library is there' );
70
    is( $stat->branch,           $library->branchcode,    'Library is there' );
69
    is( ref( $stat->item ), 'Koha::Item', '->item returns a Koha::Item object' );
71
    is( ref( $stat->item ),      'Koha::Item',            '->item returns a Koha::Item object' );
70
    is( $stat->item->itemnumber, $item->itemnumber, '->item works great' );
72
    is( $stat->item->itemnumber, $item->itemnumber,       '->item works great' );
71
73
72
    $schema->storage->txn_rollback;
74
    $schema->storage->txn_rollback;
73
};
75
};
Lines 77-101 subtest 'Test exceptions in ->new' => sub { Link Here
77
    $schema->storage->txn_begin;
79
    $schema->storage->txn_begin;
78
80
79
    throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params';
81
    throws_ok { Koha::Statistic->new } 'Koha::Exceptions::BadParameter', '->new called without params';
80
        #FIXME Should we remove this for sake of consistency?
82
83
    #FIXME Should we remove this for sake of consistency?
81
84
82
    # Type is missing
85
    # Type is missing
83
    my $params = { %$test_params };
86
    my $params = {%$test_params};
84
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type';
87
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called without type';
85
88
86
    # Type is not allowed
89
    # Type is not allowed
87
    $params = { %$test_params };
90
    $params = {%$test_params};
88
    $params ->{type} = "bla";
91
    $params->{type} = "bla";
89
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type';
92
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::WrongParameter', '->new called with wrong type';
90
93
91
    # Test mandatory accounts/circulation keys
94
    # Test mandatory accounts/circulation keys
92
    $params = { %$test_params };
95
    $params = {%$test_params};
93
    $params->{type} = 'payment';
96
    $params->{type} = 'payment';
94
    delete $params->{amount};
97
    delete $params->{amount};
95
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for accounts without amount';
98
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter',
99
        '->new called for accounts without amount';
96
    $params->{type} = 'issue';
100
    $params->{type} = 'issue';
97
    delete $params->{itemnumber};
101
    delete $params->{itemnumber};
98
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter', '->new called for circulation without itemnumber';
102
    throws_ok { Koha::Statistic->new($params) } 'Koha::Exceptions::MissingParameter',
103
        '->new called for circulation without itemnumber';
99
104
100
    $schema->storage->txn_rollback;
105
    $schema->storage->txn_rollback;
101
};
106
};
Lines 105-120 subtest 'Test ->insert (fka UpdateStats)' => sub { Link Here
105
    $schema->storage->txn_begin;
110
    $schema->storage->txn_begin;
106
111
107
    # save the params in the right database fields
112
    # save the params in the right database fields
108
    my $statistic = insert_and_fetch({ %$test_params, type => 'return' });
113
    my $statistic = insert_and_fetch( { %$test_params, type => 'return' } );
109
    is( $statistic->branch, $test_params->{branch}, "Check branch" );
114
    is( $statistic->branch,         $test_params->{branch},         "Check branch" );
110
    is( $statistic->type, 'return', "Check type" );
115
    is( $statistic->type,           'return',                       "Check type" );
111
    is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" );
116
    is( $statistic->borrowernumber, $test_params->{borrowernumber}, "Check borrowernumber" );
112
    is( $statistic->value, $test_params->{amount}, "Check value" );
117
    is( $statistic->value,          $test_params->{amount},         "Check value" );
113
    is(  $statistic->other, $test_params->{other}, "Check other" );
118
    is( $statistic->other,          $test_params->{other},          "Check other" );
114
    is( $statistic->itemtype, $test_params->{itemtype}, "Check itemtype" );
119
    is( $statistic->itemtype,       $test_params->{itemtype},       "Check itemtype" );
115
    is( $statistic->location, $test_params->{location}, "Check location" );
120
    is( $statistic->location,       $test_params->{location},       "Check location" );
116
    is( $statistic->ccode, $test_params->{ccode}, "Check ccode" );
121
    is( $statistic->ccode,          $test_params->{ccode},          "Check ccode" );
117
    is( $statistic->interface, $test_params->{interface}, "Check interface" );
122
    is( $statistic->interface,      $test_params->{interface},      "Check interface" );
118
123
119
    # Test location with undef and empty string
124
    # Test location with undef and empty string
120
    my $params = { %$test_params, type => 'return' };
125
    my $params = { %$test_params, type => 'return' };
Lines 135-141 subtest 'Test ->insert (fka UpdateStats)' => sub { Link Here
135
    is( $statistic->other, undef, "Other is NULL if passed undef" );
140
    is( $statistic->other, undef, "Other is NULL if passed undef" );
136
141
137
    # Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?)
142
    # Test amount versus value; value is the db column, amount is the legacy name (to be deprecated?)
138
    $params = { %$test_params, type => 'return', value => 0 };
143
    $params    = { %$test_params, type => 'return', value => 0 };
139
    $statistic = insert_and_fetch($params);
144
    $statistic = insert_and_fetch($params);
140
    is( $statistic->value, 0, "Value is zero, overriding non-zero amount" );
145
    is( $statistic->value, 0, "Value is zero, overriding non-zero amount" );
141
    delete $params->{value};
146
    delete $params->{value};
Lines 146-153 subtest 'Test ->insert (fka UpdateStats)' => sub { Link Here
146
};
151
};
147
152
148
sub insert_and_fetch {
153
sub insert_and_fetch {
149
    my $params = shift;
154
    my $params    = shift;
150
    my $statistic = Koha::Statistic->insert($params);
155
    my $statistic = Koha::Statistic->insert($params);
151
    return Koha::Statistics->search({ borrowernumber => $test_params->{borrowernumber} })->last;
156
    return Koha::Statistics->search( { borrowernumber => $test_params->{borrowernumber} } )->last;
152
        # FIXME discard_changes would be nicer, but we dont have a PK (yet)
157
158
    # FIXME discard_changes would be nicer, but we dont have a PK (yet)
153
}
159
}
154
- 

Return to bug 33608