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

(-)a/C4/Stats.pm (-5 / +2 lines)
Lines 146-156 sub UpdateStats { Link Here
146
        }
146
        }
147
    )->store;
147
    )->store;
148
148
149
    Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store
149
    $statistic->pseudonymize() if C4::Context->preference('Pseudonymization');
150
      if C4::Context->preference('Pseudonymization')
150
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
}
151
}
155
152
156
1;
153
1;
(-)a/Koha/PseudonymizedTransaction.pm (-24 / +36 lines)
Lines 17-22 package Koha::PseudonymizedTransaction; Link Here
17
use Modern::Perl;
17
use Modern::Perl;
18
18
19
use Crypt::Eksblowfish::Bcrypt qw( bcrypt );
19
use Crypt::Eksblowfish::Bcrypt qw( bcrypt );
20
use List::MoreUtils            qw(any);
20
21
21
use Koha::Database;
22
use Koha::Database;
22
use Koha::Exceptions::Config;
23
use Koha::Exceptions::Config;
Lines 47-69 sub new_from_statistic { Link Here
47
48
48
    my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
49
    my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
49
50
50
    if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
51
    my $statistic_item = $statistic->item;
52
53
    # These fields are treated separately as the column names do not match
54
    if ( any { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
51
        $values->{transaction_branchcode} = $statistic->branch;
55
        $values->{transaction_branchcode} = $statistic->branch;
52
    }
56
    }
53
    if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
57
    if ( any { $_ eq 'transaction_type' } @t_fields_to_copy ) {
54
        $values->{holdingbranch} = $statistic->item->holdingbranch;
55
    }
56
    if ( grep { $_ eq 'homebranch' } @t_fields_to_copy ) {
57
        $values->{homebranch} = $statistic->item->homebranch;
58
    }
59
    if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
60
        $values->{transaction_type} = $statistic->type;
58
        $values->{transaction_type} = $statistic->type;
61
    }
59
    }
62
    if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
63
        $values->{itemcallnumber} = $statistic->item->itemcallnumber;
64
    }
65
60
61
    # These fields are not captured in the statistic so must be pulled from the item
62
    if ($statistic_item) {
63
        if ( any { $_ eq 'homebranch' } @t_fields_to_copy ) {
64
            $values->{homebranch} = $statistic_item->homebranch;
65
        }
66
        if ( any { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
67
            $values->{holdingbranch} = $statistic_item->holdingbranch;
68
        }
69
        if ( any { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
70
            $values->{itemcallnumber} = $statistic_item->itemcallnumber;
71
        }
72
    }
66
73
74
    # Remove fields we have already handled from the list
67
    @t_fields_to_copy = grep {
75
    @t_fields_to_copy = grep {
68
             $_ ne 'transaction_branchcode'
76
             $_ ne 'transaction_branchcode'
69
          && $_ ne 'holdingbranch'
77
          && $_ ne 'holdingbranch'
Lines 72-99 sub new_from_statistic { Link Here
72
          && $_ ne 'itemcallnumber'
80
          && $_ ne 'itemcallnumber'
73
    } @t_fields_to_copy;
81
    } @t_fields_to_copy;
74
82
83
    # Populate the remaining columns
75
    $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
84
    $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
76
85
77
    my $patron = Koha::Patrons->find($statistic->borrowernumber);
86
    my $patron = Koha::Patrons->find( $statistic->borrowernumber );
78
    my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
87
    if ($patron) {
79
    $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
88
        my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
89
        $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
80
90
81
    $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
91
        $values->{branchcode}   = $patron->branchcode;  # FIXME Must be removed from the pref options, or FK removed (?)
82
    $values->{categorycode} = $patron->categorycode;
92
        $values->{categorycode} = $patron->categorycode;
83
93
84
    $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
94
        $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
95
    }
85
96
86
    my $self = $class->SUPER::new($values);
97
    my $self = $class->SUPER::new($values);
87
98
88
    my $extended_attributes = $patron->extended_attributes->unblessed;
99
    if ($patron) {
89
    for my $attribute (@$extended_attributes) {
100
        my $extended_attributes = $patron->extended_attributes->unblessed;
90
        next unless Koha::Patron::Attribute::Types->find(
101
        for my $attribute (@$extended_attributes) {
91
            $attribute->{code} )->keep_for_pseudonymization;
102
            next unless Koha::Patron::Attribute::Types->find( $attribute->{code} )->keep_for_pseudonymization;
92
103
93
        delete $attribute->{id};
104
            delete $attribute->{id};
94
        delete $attribute->{borrowernumber};
105
            delete $attribute->{borrowernumber};
95
106
96
        $self->_result->create_related('pseudonymized_borrower_attributes', $attribute);
107
            $self->_result->create_related( 'pseudonymized_borrower_attributes', $attribute );
108
        }
97
    }
109
    }
98
110
99
    return $self;
111
    return $self;
(-)a/Koha/Statistic.pm (-1 / +17 lines)
Lines 20-25 use Modern::Perl; Link Here
20
20
21
use Koha::Database;
21
use Koha::Database;
22
use Koha::Items;
22
use Koha::Items;
23
use Koha::PseudonymizedTransaction;
23
24
24
use base qw(Koha::Object);
25
use base qw(Koha::Object);
25
26
Lines 44-49 sub item { Link Here
44
    return Koha::Items->find( $self->itemnumber );
45
    return Koha::Items->find( $self->itemnumber );
45
}
46
}
46
47
48
=head3 pseudonymize
49
50
my $pseudonymized_stat = $statistic->pseudonymize;
51
52
Generate a pesudonymized version of the statistic.
53
54
=cut
55
56
sub pseudonymize {
57
    my ($self) = @_;
58
59
    return unless ( $self->borrowernumber && grep { $_ eq $self->type } qw(renew issue return onsite_checkout) );
60
61
    return Koha::PseudonymizedTransaction->new_from_statistic($self)->store;
62
}
63
47
=head2 Internal methods
64
=head2 Internal methods
48
65
49
=head3 _type
66
=head3 _type
50
- 

Return to bug 34611