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

(-)a/Koha/Checkouts/ReturnClaim.pm (+113 lines)
Line 0 Link Here
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 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 base qw(Koha::Object);
23
24
use Koha::Checkouts;
25
use Koha::Exceptions::Checkouts::ReturnClaims;
26
use Koha::Old::Checkouts;
27
use Koha::Patrons;
28
29
=head1 NAME
30
31
Koha::Checkouts::ReturnClaim - Koha ReturnClaim object class
32
33
=head1 API
34
35
=head2 Class methods
36
37
=cut
38
39
=head3 store
40
41
    my $return_claim = Koha::Checkout::ReturnClaim->new($args)->store;
42
43
Overloaded I<store> method that validates the attributes and raises relevant
44
exceptions as needed.
45
46
=cut
47
48
sub store {
49
    my ( $self ) = @_;
50
51
    unless ( $self->created_by ) {
52
        Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy->throw();
53
    }
54
55
    return $self->SUPER::store;
56
}
57
58
=head3 checkout
59
60
=cut
61
62
sub checkout {
63
    my ($self) = @_;
64
65
    my $checkout = Koha::Checkouts->find( $self->issue_id )
66
      || Koha::Old::Checkouts->find( $self->issue_id );
67
68
    return $checkout;
69
}
70
71
=head3 patron
72
73
=cut
74
75
sub patron {
76
    my ( $self ) = @_;
77
78
    my $borrower = $self->_result->borrowernumber;
79
    return Koha::Patron->_new_from_dbic( $borrower ) if $borrower;
80
}
81
82
=head3 to_api_mapping
83
84
This method returns the mapping for representing a Koha::Checkouts::ReturnClaim object
85
on the API.
86
87
=cut
88
89
sub to_api_mapping {
90
    return {
91
        id             => 'claim_id',
92
        itemnumber     => 'item_id',
93
        borrowernumber => 'patron_id',
94
    };
95
}
96
97
=head2 Internal methods
98
99
=head3 _type
100
101
=cut
102
103
sub _type {
104
    return 'ReturnClaim';
105
}
106
107
=head1 AUTHOR
108
109
Kyle M Hall <kyle@bywatersolutions.com>
110
111
=cut
112
113
1;
(-)a/Koha/Checkouts/ReturnClaims.pm (+86 lines)
Line 0 Link Here
1
package Koha::Checkouts::ReturnClaims;
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 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
26
use Koha::Checkouts::ReturnClaim;
27
28
use base qw(Koha::Objects);
29
30
=head1 NAME
31
32
Koha::Checkouts::ReturnClaims - Koha ReturnClaim object set class
33
34
=head1 API
35
36
=head2 Class Methods
37
38
=cut
39
40
=head3 unresolved
41
42
=cut
43
44
sub unresolved {
45
    my ($self) = @_;
46
47
    my $results = $self->_resultset()->search_rs( { resolved_on => undef } );
48
49
    return Koha::Checkouts::ReturnClaims->_new_from_dbic( $results );
50
}
51
52
=head3 resolved
53
54
=cut
55
56
sub resolved {
57
    my ($self) = @_;
58
59
    my $results = $self->_resultset()->search_rs( { resolved_on => { '!=' => undef } } );
60
61
    return Koha::Checkouts::ReturnClaims->_new_from_dbic( $results );
62
}
63
64
=head3 type
65
66
=cut
67
68
sub _type {
69
    return 'ReturnClaim';
70
}
71
72
=head3 object_class
73
74
=cut
75
76
sub object_class {
77
    return 'Koha::Checkouts::ReturnClaim';
78
}
79
80
=head1 AUTHOR
81
82
Kyle M Hall <kyle@bywatersolutions.com>
83
84
=cut
85
86
1;
(-)a/Koha/Exceptions/Checkouts/ReturnClaims.pm (+50 lines)
Line 0 Link Here
1
package Koha::Exceptions::Checkouts::ReturnClaims;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Koha::Exceptions::Exception;
21
22
use Exception::Class (
23
    'Koha::Exceptions::Checkouts::ReturnClaims' => {
24
        isa         => 'Koha::Exceptions::Exception',
25
        description => 'Something went wrong!',
26
    },
27
    'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy' => {
28
        isa         => 'Koha::Exceptions::Checkouts::ReturnClaims',
29
        description => 'created_by is mandatory'
30
    },
31
);
32
33
=head1 NAME
34
35
Koha::Exceptions::Checkouts - Base class for Checkouts exceptions
36
37
=head1 Exceptions
38
39
=head2 Koha::Exceptions::Checkouts::ReturnClaims
40
41
Generic return claim exception
42
43
=head2 Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy
44
45
Exception to be used when a return claim is requested to be store but
46
the 'created_by' param is not passed.
47
48
=cut
49
50
1;
(-)a/t/db_dependent/Koha/Checkouts/ReturnClaim.t (-1 / +105 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Exception;
22
23
use Koha::Database;
24
use Koha::Checkouts::ReturnClaims;
25
26
use t::lib::TestBuilder;
27
28
my $schema  = Koha::Database->new->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
subtest "store() tests" => sub {
32
33
    plan tests => 6;
34
35
    $schema->storage->txn_begin;
36
37
    my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
38
    my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
39
    my $item      = $builder->build_sample_item;
40
41
    my $checkout = $builder->build_object(
42
        {
43
            class => 'Koha::Checkouts',
44
            value => {
45
                borrowernumber => $patron->borrowernumber,
46
                itemnumber     => $item->itemnumber,
47
                branchcode     => $patron->branchcode
48
            }
49
        }
50
    );
51
52
    throws_ok
53
        { Koha::Checkouts::ReturnClaim->new(
54
            {
55
                issue_id       => $checkout->id,
56
                itemnumber     => $checkout->itemnumber,
57
                borrowernumber => $checkout->borrowernumber,
58
                notes          => 'Some notes'
59
            }
60
          )->store }
61
        'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy',
62
        'Exception thrown correctly';
63
64
    is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
65
66
    my $claim = Koha::Checkouts::ReturnClaim->new(
67
        {
68
            issue_id       => $checkout->id,
69
            itemnumber     => $checkout->itemnumber,
70
            borrowernumber => $checkout->borrowernumber,
71
            notes          => 'Some notes',
72
            created_by     => $librarian->borrowernumber
73
        }
74
    )->store;
75
76
    is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
77
    is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
78
79
    {   # hide useless warnings
80
        local *STDERR;
81
        open STDERR, '>', '/dev/null';
82
        throws_ok {
83
            Koha::Checkouts::ReturnClaim->new(
84
                {
85
                    issue_id       => $checkout->id + 1000,
86
                    itemnumber     => $checkout->itemnumber,
87
                    borrowernumber => $checkout->borrowernumber,
88
                    notes          => 'Some notes',
89
                    created_by     => $librarian->borrowernumber
90
                }
91
            )->store;
92
        }
93
        'Koha::Exceptions::Object::FKConstraint',
94
            'An exception is thrown on invalid issue_id';
95
        close STDERR;
96
97
        is(
98
            $@->broken_fk,
99
            'issue_id',
100
            'Exception field is correct'
101
        );
102
    }
103
104
    $schema->storage->txn_rollback;
105
};

Return to bug 14697