b5d5aded706efc18656836b1fa07ffed3f3a80cc
[koha.git] / Koha / Patron / Modification.pm
1 package Koha::Patron::Modification;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use Koha::Exceptions::Patron::Modification;
26 use Koha::Patron::Attribute;
27 use Koha::Patron::Attributes;
28 use Koha::Patron::Modifications;
29
30 use JSON;
31 use List::MoreUtils qw( uniq );
32 use Try::Tiny;
33
34 use base qw(Koha::Object);
35
36 =head1 NAME
37
38 Koha::Patron::Modification - Class represents a request to modify or create a patron
39
40 =head2 Class Methods
41
42 =cut
43
44 =head2 store
45
46 =cut
47
48 sub store {
49     my ($self) = @_;
50
51     if ( $self->verification_token ) {
52         if (Koha::Patron::Modifications->search(
53                 { verification_token => $self->verification_token }
54             )->count()
55             )
56         {
57             Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
58                 "Duplicate verification token " . $self->verification_token );
59         }
60     }
61
62     if ( $self->extended_attributes ) {
63         try {
64             my $json_parser = JSON->new;
65             $json_parser->decode( $self->extended_attributes );
66         }
67         catch {
68             Koha::Exceptions::Patron::Modification::InvalidData->throw(
69                 'The passed extended_attributes is not valid JSON');
70         };
71     }
72
73     return $self->SUPER::store();
74 }
75
76 =head2 approve
77
78 $m->approve();
79
80 Commits the pending modifications to the borrower record and removes
81 them from the modifications table.
82
83 =cut
84
85 sub approve {
86     my ($self) = @_;
87
88     my $data = $self->unblessed();
89     my $extended_attributes;
90
91     delete $data->{timestamp};
92     delete $data->{verification_token};
93     delete $data->{extended_attributes};
94
95     my $patron = Koha::Patrons->find( $self->borrowernumber );
96     return unless $patron;
97
98     foreach my $key ( keys %$data ) {
99         next # Unset it!
100           if $key eq 'dateofbirth'
101           && $patron->dateofbirth
102           && not defined $data->{$key};
103
104         delete $data->{$key} unless defined $data->{$key};
105     }
106
107     $patron->set($data);
108
109     # Take care of extended attributes
110     if ( $self->extended_attributes ) {
111         $extended_attributes = try { from_json( $self->extended_attributes ) }
112         catch {
113             Koha::Exceptions::Patron::Modification::InvalidData->throw(
114                 'The passed extended_attributes is not valid JSON');
115         };
116     }
117
118     $self->_result->result_source->schema->txn_do(
119         sub {
120             try {
121                 $patron->store();
122
123                 # Deal with attributes
124                 my @codes
125                     = uniq( map { $_->{code} } @{$extended_attributes} );
126                 foreach my $code (@codes) {
127                     map { $_->delete } Koha::Patron::Attributes->search(
128                         {   borrowernumber => $patron->borrowernumber,
129                             code           => $code
130                         }
131                     );
132                 }
133                 foreach my $attr ( @{$extended_attributes} ) {
134                     Koha::Patron::Attribute->new(
135                         {   borrowernumber => $patron->borrowernumber,
136                             code           => $attr->{code},
137                             attribute      => $attr->{value}
138                         }
139                     )->store
140                         if $attr->{value} # there's a value
141                            or
142                           (    defined $attr->{value} # there's a value that is 0, and not
143                             && $attr->{value} ne ""   # the empty string which means delete
144                             && $attr->{value} == 0
145                           );
146                 }
147             }
148             catch {
149                 if ( $_->isa('DBIx::Class::Exception') ) {
150                     Koha::Exceptions::Patron::Modification->throw( $_->{msg} );
151                 }
152                 else {
153                     Koha::Exceptions::Patron::Modification->throw($_);
154                 }
155             };
156         }
157     );
158
159     return $self->delete();
160 }
161
162 =head3 type
163
164 =cut
165
166 sub _type {
167     return 'BorrowerModification';
168 }
169
170 =head1 AUTHOR
171
172 Kyle M Hall <kyle@bywatersolutions.com>
173
174 =cut
175
176 1;