Bug 17737: Rename holds_placed_before_today with current_holds
[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 get_transfer
96
97 my $transfer = $item->get_transfer;
98
99 Return the transfer if the item is in transit or undef
100
101 =cut
102
103 sub get_transfer {
104     my ( $self ) = @_;
105     my $transfer_rs = $self->_result->branchtransfers->search({ datearrived => undef })->first;
106     return unless $transfer_rs;
107     return Koha::Item::Transfer->_new_from_dbic( $transfer_rs );
108 }
109
110 =head3 last_returned_by
111
112 Gets and sets the last borrower to return an item.
113
114 Accepts and returns Koha::Patron objects
115
116 $item->last_returned_by( $borrowernumber );
117
118 $last_returned_by = $item->last_returned_by();
119
120 =cut
121
122 sub last_returned_by {
123     my ( $self, $borrower ) = @_;
124
125     my $items_last_returned_by_rs = Koha::Database->new()->schema()->resultset('ItemsLastBorrower');
126
127     if ($borrower) {
128         return $items_last_returned_by_rs->update_or_create(
129             { borrowernumber => $borrower->borrowernumber, itemnumber => $self->id } );
130     }
131     else {
132         unless ( $self->{_last_returned_by} ) {
133             my $result = $items_last_returned_by_rs->single( { itemnumber => $self->id } );
134             if ($result) {
135                 $self->{_last_returned_by} = Koha::Patrons->find( $result->get_column('borrowernumber') );
136             }
137         }
138
139         return $self->{_last_returned_by};
140     }
141 }
142
143 =head3 can_article_request
144
145 my $bool = $item->can_article_request( $borrower )
146
147 Returns true if item can be specifically requested
148
149 $borrower must be a Koha::Patron object
150
151 =cut
152
153 sub can_article_request {
154     my ( $self, $borrower ) = @_;
155
156     my $rule = $self->article_request_type($borrower);
157
158     return 1 if $rule && $rule ne 'no' && $rule ne 'bib_only';
159     return q{};
160 }
161
162 =head3 article_request_type
163
164 my $type = $item->article_request_type( $borrower )
165
166 returns 'yes', 'no', 'bib_only', or 'item_only'
167
168 $borrower must be a Koha::Patron object
169
170 =cut
171
172 sub article_request_type {
173     my ( $self, $borrower ) = @_;
174
175     my $branch_control = C4::Context->preference('HomeOrHoldingBranch');
176     my $branchcode =
177         $branch_control eq 'homebranch'    ? $self->homebranch
178       : $branch_control eq 'holdingbranch' ? $self->holdingbranch
179       :                                      undef;
180     my $borrowertype = $borrower->categorycode;
181     my $itemtype = $self->effective_itemtype();
182     my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule({ categorycode => $borrowertype, itemtype => $itemtype, branchcode => $branchcode });
183
184     return q{} unless $issuing_rule;
185     return $issuing_rule->article_requests || q{}
186 }
187
188 =head3 current_holds
189
190 =cut
191
192 sub current_holds {
193     my ( $self ) = @_;
194     my $attributes = { order_by => 'priority' };
195     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
196     my $params = {
197         itemnumber => $self->itemnumber,
198         suspend => 0,
199         -or => [
200             reservedate => { '<=' => $dtf->format_date(dt_from_string) },
201             waitingdate => { '!=' => undef },
202         ],
203     };
204     my $hold_rs = $self->_result->reserves->search( $params, $attributes );
205     return Koha::Holds->_new_from_dbic($hold_rs);
206 }
207
208 =head3 type
209
210 =cut
211
212 sub _type {
213     return 'Item';
214 }
215
216 =head1 AUTHOR
217
218 Kyle M Hall <kyle@bywatersolutions.com>
219
220 =cut
221
222 1;