Bug 18459: Add the Koha::Item->biblioitem method
[koha.git] / Koha / Item.pm
1 package Koha::Item;
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::DateUtils qw( dt_from_string );
26
27 use C4::Context;
28 use Koha::IssuingRules;
29 use Koha::Item::Transfer;
30 use Koha::Patrons;
31 use Koha::Libraries;
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Item - Koha Item object class
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 effective_itemtype
46
47 Returns the itemtype for the item based on whether item level itemtypes are set or not.
48
49 =cut
50
51 sub effective_itemtype {
52     my ( $self ) = @_;
53
54     return $self->_result()->effective_itemtype();
55 }
56
57 =head3 home_branch
58
59 =cut
60
61 sub home_branch {
62     my ($self) = @_;
63
64     $self->{_home_branch} ||= Koha::Libraries->find( $self->homebranch() );
65
66     return $self->{_home_branch};
67 }
68
69 =head3 holding_branch
70
71 =cut
72
73 sub holding_branch {
74     my ($self) = @_;
75
76     $self->{_holding_branch} ||= Koha::Libraries->find( $self->holdingbranch() );
77
78     return $self->{_holding_branch};
79 }
80
81 =head3 biblio
82
83 my $biblio = $item->biblio;
84
85 Return the bibliographic record of this item
86
87 =cut
88
89 sub biblio {
90     my ( $self ) = @_;
91     my $biblio_rs = $self->_result->biblio;
92     return Koha::Biblio->_new_from_dbic( $biblio_rs );
93 }
94
95 =head3 biblioitem
96
97 my $biblioitem = $item->biblioitem;
98
99 Return the biblioitem record of this item
100
101 =cut
102
103 sub biblioitem {
104     my ( $self ) = @_;
105     my $biblioitem_rs = $self->_result->biblioitem;
106     return Koha::Biblioitem->_new_from_dbic( $biblioitem_rs );
107 }
108
109 =head3 get_transfer
110
111 my $transfer = $item->get_transfer;
112
113 Return the transfer if the item is in transit or undef
114
115 =cut
116
117 sub get_transfer {
118     my ( $self ) = @_;
119     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
120     return unless $transfer_rs;
121     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
122 }
123
124 =head3 last_returned_by
125
126 Gets and sets the last borrower to return an item.
127
128 Accepts and returns Koha::Patron objects
129
130 $item->last_returned_by( $borrowernumber );
131
132 $last_returned_by = $item->last_returned_by();
133
134 =cut
135
136 sub last_returned_by {
137     my ( $self, $borrower ) = @_;
138
139     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
140
141     if ($borrower) {
142         return $items_last_returned_by_rs->update_or_create(
143             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
144     }
145     else {
146         unless ( $self->{_last_returned_by} ) {
147             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
148             if ($result) {
149                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
150             }
151         }
152
153         return $self->{_last_returned_by};
154     }
155 }
156
157 =head3 can_article_request
158
159 my $bool = $item->can_article_request( $borrower )
160
161 Returns true if item can be specifically requested
162
163 $borrower must be a Koha::Patron object
164
165 =cut
166
167 sub can_article_request {
168     my ( $self, $borrower ) = @_;
169
170     my $rule = $self->article_request_type($borrower);
171
172     return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
173     return q{};
174 }
175
176 =head3 article_request_type
177
178 my $type = $item->article_request_type( $borrower )
179
180 returns 'yes', 'no', 'bib_only', or 'item_only'
181
182 $borrower must be a Koha::Patron object
183
184 =cut
185
186 sub article_request_type {
187     my ( $self, $borrower ) = @_;
188
189     my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
190     my $branchcode =
191         $branch_control eq 'homebranch'    ? $self->homebranch
192       : $branch_control eq 'holdingbranch' ? $self->holdingbranch
193       :                                      undef;
194     my $borrowertype = $borrower->categorycode;
195     my $itemtype = $self->effective_itemtype();
196     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
197
198     return q{} unless $issuing_rule;
199     return $issuing_rule->article_requests || q{}
200 }
201
202 =head3 current_holds
203
204 =cut
205
206 sub current_holds {
207     my ( $self ) = @_;
208     my $attributes = { order_by => 'priority' };
209     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
210     my $params = {
211         itemnumber => $self->itemnumber,
212         suspend => 0,
213         -or => [
214             reservedate => { '<=' => $dtf->format_date(dt_from_string) },
215             waitingdate => { '!=' => undef },
216         ],
217     };
218     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
219     return Koha::Holds->_new_from_dbic($hold_rs);
220 }
221
222 =head3 type
223
224 =cut
225
226 sub _type {
227     return 'Item';
228 }
229
230 =head1 AUTHOR
231
232 Kyle M Hall <kyle@bywatersolutions.com>
233
234 =cut
235
236 1;