Bug 28786: Fix Patrons/Import.t
[koha.git] / Koha / Checkouts / ReturnClaim.pm
1 package Koha::Checkouts::ReturnClaim;
2
3 # Copyright ByWater Solutions 2019
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use base qw(Koha::Object);
23
24 use Koha::Checkouts;
25 use Koha::Exceptions;
26 use Koha::Exceptions::Checkouts::ReturnClaims;
27 use Koha::Old::Checkouts;
28 use Koha::Patrons;
29
30 =head1 NAME
31
32 Koha::Checkouts::ReturnClaim - Koha ReturnClaim object class
33
34 =head1 API
35
36 =head2 Class methods
37
38 =cut
39
40 =head3 store
41
42     my $return_claim = Koha::Checkout::ReturnClaim->new($args)->store;
43
44 Overloaded I<store> method that validates the attributes and raises relevant
45 exceptions as needed.
46
47 =cut
48
49 sub store {
50     my ( $self ) = @_;
51
52     unless ( $self->in_storage || $self->created_by ) {
53         Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy->throw();
54     }
55
56     unless ( !$self->issue_id
57         || Koha::Checkouts->find( $self->issue_id )
58         || Koha::Old::Checkouts->find( $self->issue_id ) )
59     {
60         Koha::Exceptions::Object::FKConstraint->throw(
61             error     => 'Broken FK constraint',
62             broken_fk => 'issue_id'
63         );
64     }
65
66     return $self->SUPER::store;
67 }
68
69 =head3 checkout
70
71 =cut
72
73 sub checkout {
74     my ($self) = @_;
75
76     my $checkout_rs = $self->_result->checkout;
77     return unless $checkout_rs;
78     return Koha::Checkout->_new_from_dbic($checkout_rs);
79 }
80
81 =head3 old_checkout
82
83 =cut
84
85 sub old_checkout {
86     my ($self) = @_;
87
88     my $old_checkout_rs = $self->_result->old_checkout;
89     return unless $old_checkout_rs;
90     return Koha::Old::Checkout->_new_from_dbic($old_checkout_rs);
91 }
92
93 =head3 patron
94
95 =cut
96
97 sub patron {
98     my ( $self ) = @_;
99
100     my $borrower = $self->_result->borrowernumber;
101     return Koha::Patron->_new_from_dbic( $borrower ) if $borrower;
102 }
103
104 =head3 item
105
106   my $item = $claim->item;
107
108 Return the return claim item
109
110 =cut
111
112 sub item {
113     my ( $self ) = @_;
114     my $item_rs = $self->_result->item;
115     return Koha::Item->_new_from_dbic( $item_rs );
116 }
117
118 =head3 resolve
119
120     $claim->resolve(
121         {
122             resolution      => $resolution,
123             resolved_by     => $patron_id,
124           [ resolved_on     => $dt
125             new_lost_status => $status, ]
126         }
127     );
128
129 Resolve the claim.
130
131 =cut
132
133 sub resolve {
134     my ( $self, $params ) = @_;
135
136     my @mandatory = ( 'resolution', 'resolved_by' );
137     for my $param (@mandatory) {
138         unless ( defined( $params->{$param} ) ) {
139             Koha::Exceptions::MissingParameter->throw( error => "The $param parameter is mandatory" );
140         }
141     }
142
143     $self->_result->result_source->schema->txn_do(
144         sub {
145             $self->set(
146                 {   resolution  => $params->{resolution},
147                     resolved_by => $params->{resolved_by},
148                     resolved_on => $params->{resolved_on} // \'NOW()',
149                     updated_by  => $params->{resolved_by}
150                 }
151             )->store;
152
153             if ( defined $params->{new_lost_status} ) {
154                 $self->item->itemlost( $params->{new_lost_status} )->store;
155             }
156         }
157     );
158
159     return $self;
160 }
161
162 =head3 to_api_mapping
163
164 This method returns the mapping for representing a Koha::Checkouts::ReturnClaim object
165 on the API.
166
167 =cut
168
169 sub to_api_mapping {
170     return {
171         id             => 'claim_id',
172         itemnumber     => 'item_id',
173         borrowernumber => 'patron_id',
174     };
175 }
176
177 =head2 Internal methods
178
179 =head3 _type
180
181 =cut
182
183 sub _type {
184     return 'ReturnClaim';
185 }
186
187 =head1 AUTHOR
188
189 Kyle M Hall <kyle@bywatersolutions.com>
190
191 =cut
192
193 1;