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 (-21 / +33 lines)
Lines 47-69 sub new_from_statistic { Link Here
47
47
48
    my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
48
    my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
49
49
50
    my $statistic_item = $statistic->item;
51
52
    # These fields are treated separately as the column names do not match
50
    if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
53
    if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
51
        $values->{transaction_branchcode} = $statistic->branch;
54
        $values->{transaction_branchcode} = $statistic->branch;
52
    }
55
    }
53
    if ( grep { $_ eq 'holdingbranch' } @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 ) {
56
    if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
60
        $values->{transaction_type} = $statistic->type;
57
        $values->{transaction_type} = $statistic->type;
61
    }
58
    }
62
    if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
63
        $values->{itemcallnumber} = $statistic->item->itemcallnumber;
64
    }
65
59
60
    # These fields are not captured in the statistic so must be pulled from the item
61
    if ($statistic_item) {
62
        if ( grep { $_ eq 'homebranch' } @t_fields_to_copy ) {
63
            $values->{homebranch} = $statistic_item->homebranch;
64
        }
65
        if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
66
            $values->{holdingbranch} = $statistic->item->holdingbranch;
67
        }
68
        if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
69
            $values->{itemcallnumber} = $statistic_item->itemcallnumber;
70
        }
71
    }
66
72
73
    # Remove fields we have already handled from the list
67
    @t_fields_to_copy = grep {
74
    @t_fields_to_copy = grep {
68
             $_ ne 'transaction_branchcode'
75
             $_ ne 'transaction_branchcode'
69
          && $_ ne 'holdingbranch'
76
          && $_ ne 'holdingbranch'
Lines 72-99 sub new_from_statistic { Link Here
72
          && $_ ne 'itemcallnumber'
79
          && $_ ne 'itemcallnumber'
73
    } @t_fields_to_copy;
80
    } @t_fields_to_copy;
74
81
82
    # Populate the remaining columns
75
    $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
83
    $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
76
84
77
    my $patron = Koha::Patrons->find($statistic->borrowernumber);
85
    my $patron = Koha::Patrons->find($statistic->borrowernumber);
78
    my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
86
    if( $patron ){
79
    $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
87
        my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
88
        $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
80
89
81
    $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
90
        $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
82
    $values->{categorycode} = $patron->categorycode;
91
        $values->{categorycode} = $patron->categorycode;
83
92
84
    $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
93
        $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
94
    }
85
95
86
    my $self = $class->SUPER::new($values);
96
    my $self = $class->SUPER::new($values);
87
97
88
    my $extended_attributes = $patron->extended_attributes->unblessed;
98
    if( $patron ){
89
    for my $attribute (@$extended_attributes) {
99
        my $extended_attributes = $patron->extended_attributes->unblessed;
90
        next unless Koha::Patron::Attribute::Types->find(
100
        for my $attribute (@$extended_attributes) {
91
            $attribute->{code} )->keep_for_pseudonymization;
101
            next unless Koha::Patron::Attribute::Types->find(
102
                $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 / +20 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
    my $pseudonmyized_stat = Koha::PseudonymizedTransaction->new_from_statistic($self)->store;
62
63
    return $pseudonmyized_stat;
64
65
}
66
47
=head2 Internal methods
67
=head2 Internal methods
48
68
49
=head3 _type
69
=head3 _type
50
- 

Return to bug 34611