Bug 30708: Add Koha Objects
[koha.git] / Koha / Preservation / Train.pm
1 package Koha::Preservation::Train;
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 JSON qw( to_json );
21 use Try::Tiny;
22
23 use Koha::Database;
24
25 use base qw(Koha::Object);
26
27 use Koha::Preservation::Processings;
28 use Koha::Preservation::Train::Items;
29
30 use Koha::Exceptions::Preservation;
31
32 =head1 NAME
33
34 Koha::Preservation::Train - Koha Train Object class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =cut
41
42 =head3 default_processing
43
44 Return the default processing object for this train
45
46 =cut
47
48 sub default_processing {
49     my ( $self ) = @_;
50     my $rs = $self->_result->default_processing;
51     return unless $rs;
52     return Koha::Preservation::Processing->_new_from_dbic($rs);
53 }
54
55 =head3 add_item
56
57 Add item to this train
58
59 my $train_item = $train->add_item({item_id => $itemnumber, processing_id => $processing_id});
60 my $train_item = $train->add_item({barcode => $barcode, processing_id => $processing_id});
61
62 =cut
63
64 sub add_item {
65     my ( $self, $train_item ) = @_;
66
67     my $not_for_loan = C4::Context->preference('PreservationNotForLoanWaitingListIn');
68
69     my $key  = exists $train_item->{item_id} ? 'itemnumber' : 'barcode';
70     my $item = Koha::Items->find( { $key => $train_item->{item_id} || $train_item->{barcode} } );
71     Koha::Exceptions::Preservation::ItemNotFound->throw unless $item;
72     Koha::Exceptions::Preservation::ItemNotInWaitingList->throw if $item->notforloan != $not_for_loan;
73
74     my $train_item_rs = $self->_result->add_to_preservation_trains_items(
75         {
76             item_id       => $item->itemnumber,
77             processing_id => $train_item->{processing_id} || $self->default_processing_id,
78             added_on      => \'NOW()',
79         }
80     );
81     $item->notforloan( $self->not_for_loan )->store;
82     return Koha::Preservation::Train::Item->_new_from_dbic($train_item_rs);
83 }
84
85 =head3 add_items
86
87 my $train_items = $train->add_items([$item_1, $item_2]);
88
89 Add items in batch.
90
91 =cut
92
93 sub add_items {
94     my ( $self, $train_items ) = @_;
95     my @added_items;
96     for my $train_item (@$train_items) {
97         try {
98             push @added_items, $self->add_item($train_item);
99         } catch {
100
101             # FIXME Do we rollback and raise an error or just skip it?
102             # FIXME See status code 207 partial success
103             warn "Item not added to train: " . $_;
104         };
105     }
106     return Koha::Preservation::Train::Items->search( { train_item_id => [ map { $_->train_item_id } @added_items ] } );
107 }
108
109 =head3 items
110
111 my $items = $train->items;
112
113 Return the items in this train.
114
115 =cut
116
117 sub items {
118     my ( $self ) = @_;
119     my $items_rs = $self->_result->preservation_trains_items;
120     return Koha::Preservation::Train::Items->_new_from_dbic($items_rs)
121 }
122
123 =head2 Internal methods
124
125 =head3 _type
126
127 =cut
128
129 sub _type {
130     return 'PreservationTrain';
131 }
132
133 1;