Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[koha.git] / Koha / Acquisition / Basket.pm
1 package Koha::Acquisition::Basket;
2
3 # Copyright 2017 Aleisha Amohia <aleisha@catalyst.net.nz>
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 Koha::Database;
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::Acquisition::BasketGroups;
25 use Koha::Acquisition::Orders;
26 use Koha::Exceptions::Acquisition::Basket;
27 use Koha::Patrons;
28
29 use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields );
30
31 =head1 NAME
32
33 Koha::Acquisition::Basket - Koha Basket Object class
34
35 =head1 API
36
37 =head2 Class methods
38
39 =cut
40
41 =head3 bookseller
42
43 Returns the vendor
44
45 =cut
46
47 sub bookseller {
48     my ($self) = @_;
49     my $bookseller_rs = $self->_result->booksellerid;
50     return Koha::Acquisition::Bookseller->_new_from_dbic( $bookseller_rs );
51 }
52
53 =head3 creator
54
55     my $creator = $basket->creator;
56
57 Returns the I<Koha::Patron> for the basket creator.
58
59 =cut
60
61 sub creator {
62     my ($self) = @_;
63     my $borrowernumber = $self->authorisedby; # FIXME missing FK here
64     return unless $borrowernumber;
65     return Koha::Patrons->find( $borrowernumber );
66 }
67
68 =head3 basket_group
69
70 Returns the basket group associated to this basket
71
72 =cut
73
74 sub basket_group {
75     my ($self) = @_;
76
77     my $basket_group_rs = $self->_result->basket_group;
78     return unless $basket_group_rs;
79     return Koha::Acquisition::BasketGroup->_new_from_dbic( $basket_group_rs );
80 }
81
82 =head3 orders
83
84     my $orders = $basket->orders;
85
86 Returns a Koha::Acquisition::Orders resultset, with the orders linked
87 to this basket.
88
89 =cut
90
91 sub orders {
92     my ($self) = @_;
93
94     my $orders_rs = $self->_result->orders;
95     return Koha::Acquisition::Orders->_new_from_dbic( $orders_rs );
96 }
97
98 =head3 effective_create_items
99
100 Returns C<create_items> for this basket, falling back to C<AcqCreateItem> if unset.
101
102 =cut
103
104 sub effective_create_items {
105     my ( $self ) = @_;
106
107     return $self->create_items || C4::Context->preference('AcqCreateItem');
108 }
109
110 =head3 estimated_delivery_date
111
112 my $estimated_delivery_date = $basket->estimated_delivery_date;
113
114 Return the estimated delivery date for this basket.
115
116 It is calculated adding the delivery time of the vendor to the close date of this basket.
117
118 Return implicit undef if the basket is not closed, or the vendor does not have a delivery time.
119
120 =cut
121
122 sub estimated_delivery_date {
123     my ( $self ) = @_;
124     return unless $self->closedate and $self->bookseller->deliverytime;
125     return dt_from_string($self->closedate)->add( days => $self->bookseller->deliverytime);
126 }
127
128 =head3 late_since_days
129
130 my $number_of_days_late = $basket->late_since_days;
131
132 Return the number of days the basket is late.
133
134 Return implicit undef if the basket is not closed.
135
136 =cut
137
138 sub late_since_days {
139     my ( $self ) = @_;
140     return unless $self->closedate;
141     return dt_from_string->delta_days(dt_from_string($self->closedate))->delta_days();
142 }
143
144 =head3 authorizer
145
146 my $authorizer = $basket->authorizer;
147
148 Returns the patron who authorized/created this basket.
149
150 =cut
151
152 sub authorizer {
153     my ($self) = @_;
154     # FIXME We should use a DBIC rs, but the FK is missing
155     return unless $self->authorisedby;
156     return scalar Koha::Patrons->find($self->authorisedby);
157 }
158
159 =head3 is_closed
160
161     if ( $basket->is_closed ) { ... }
162
163 Returns a boolean value representing if the basket is closed.
164
165 =cut
166
167 sub is_closed {
168     my ($self) = @_;
169
170     return ($self->closedate) ? 1 : 0;
171 }
172
173 =head3 close
174
175     $basket->close;
176
177 Close the basket and mark all open orders as ordered.
178
179 A I<Koha::Exceptions::Acquisition::Basket::AlreadyClosed> exception is thrown
180 if the basket is already closed.
181
182 =cut
183
184 sub close {
185     my ($self) = @_;
186
187     Koha::Exceptions::Acquisition::Basket::AlreadyClosed->throw
188         if $self->is_closed;
189
190     $self->_result->result_source->schema->txn_do(
191         sub {
192             my $open_orders = $self->orders->search(
193                 {
194                     orderstatus => { not_in => [ 'complete', 'cancelled' ] }
195                 }
196             );
197             # Mark open orders as ordered
198             $open_orders->update({ orderstatus => 'ordered' }, { no_triggers => 1 });
199             # set as closed
200             $self->set({ closedate => \'NOW()' })->store;
201         }
202     );
203
204     return $self;
205 }
206
207 =head3 to_api
208
209     my $json = $basket->to_api;
210
211 Overloaded method that returns a JSON representation of the Koha::Acquisition::Basket object,
212 suitable for API output.
213
214 =cut
215
216 sub to_api {
217     my ( $self, $params ) = @_;
218
219     my $json = $self->SUPER::to_api( $params );
220
221     $json->{closed} = ( $self->closedate )
222                                     ? Mojo::JSON->true
223                                     : Mojo::JSON->false;
224
225     return $json;
226 }
227
228 =head3 to_api_mapping
229
230 This method returns the mapping for representing a Koha::Acquisition::Basket object
231 on the API.
232
233 =cut
234
235 sub to_api_mapping {
236     return {
237         basketno                => 'basket_id',
238         basketname              => 'name',
239         booksellernote          => 'vendor_note',
240         contractnumber          => 'contract_id',
241         creationdate            => 'creation_date',
242         closedate               => 'close_date',
243         booksellerid            => 'vendor_id',
244         authorisedby            => 'creator_id',
245         booksellerinvoicenumber => undef,
246         basketgroupid           => 'basket_group_id',
247         deliveryplace           => 'delivery_library_id',
248         billingplace            => 'billing_library_id',
249         branch                  => 'library_id',
250         is_standing             => 'standing'
251     };
252 }
253
254 =head2 Internal methods
255
256 =head3 _type
257
258 =cut
259
260 sub _type {
261     return 'Aqbasket';
262 }
263
264 =head1 AUTHOR
265
266 Aleisha Amohia <aleisha@catalyst.net.nz>
267 Catalyst IT
268
269 =cut
270
271 1;