1 package Koha::Acquisition::Basket;
3 # Copyright 2017 Aleisha Amohia <aleisha@catalyst.net.nz>
5 # This file is part of Koha.
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.
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.
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>.
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::Acquisition::BasketGroups;
25 use Koha::Acquisition::Orders;
26 use Koha::Exceptions::Acquisition::Basket;
28 use C4::Log qw(logaction);
30 use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields );
34 Koha::Acquisition::Basket - Koha Basket Object class
50 my $bookseller_rs = $self->_result->booksellerid;
51 return Koha::Acquisition::Bookseller->_new_from_dbic( $bookseller_rs );
56 my $creator = $basket->creator;
58 Returns the I<Koha::Patron> for the basket creator.
64 my $borrowernumber = $self->authorisedby; # FIXME missing FK here
65 return unless $borrowernumber;
66 return Koha::Patrons->find( $borrowernumber );
71 Returns the basket group associated to this basket
78 my $basket_group_rs = $self->_result->basket_group;
79 return unless $basket_group_rs;
80 return Koha::Acquisition::BasketGroup->_new_from_dbic( $basket_group_rs );
85 my $orders = $basket->orders;
87 Returns a Koha::Acquisition::Orders resultset, with the orders linked
95 my $orders_rs = $self->_result->orders;
96 return Koha::Acquisition::Orders->_new_from_dbic( $orders_rs );
99 =head3 effective_create_items
101 Returns C<create_items> for this basket, falling back to C<AcqCreateItem> if unset.
105 sub effective_create_items {
108 return $self->create_items || C4::Context->preference('AcqCreateItem');
111 =head3 estimated_delivery_date
113 my $estimated_delivery_date = $basket->estimated_delivery_date;
115 Return the estimated delivery date for this basket.
117 It is calculated adding the delivery time of the vendor to the close date of this basket.
119 Return implicit undef if the basket is not closed, or the vendor does not have a delivery time.
123 sub estimated_delivery_date {
125 return unless $self->closedate and $self->bookseller->deliverytime;
126 return dt_from_string($self->closedate)->add( days => $self->bookseller->deliverytime);
129 =head3 late_since_days
131 my $number_of_days_late = $basket->late_since_days;
133 Return the number of days the basket is late.
135 Return implicit undef if the basket is not closed.
139 sub late_since_days {
141 return unless $self->closedate;
142 return dt_from_string->delta_days(dt_from_string($self->closedate))->delta_days();
147 my $authorizer = $basket->authorizer;
149 Returns the patron who authorized/created this basket.
155 # FIXME We should use a DBIC rs, but the FK is missing
156 return unless $self->authorisedby;
157 return scalar Koha::Patrons->find($self->authorisedby);
162 if ( $basket->is_closed ) { ... }
164 Returns a boolean value representing if the basket is closed.
171 return ($self->closedate) ? 1 : 0;
178 Close the basket and mark all open orders as ordered.
180 A I<Koha::Exceptions::Acquisition::Basket::AlreadyClosed> exception is thrown
181 if the basket is already closed.
188 Koha::Exceptions::Acquisition::Basket::AlreadyClosed->throw
191 $self->_result->result_source->schema->txn_do(
193 my $open_orders = $self->orders->search(
195 orderstatus => { not_in => [ 'complete', 'cancelled' ] }
198 # Mark open orders as ordered
199 $open_orders->update({ orderstatus => 'ordered' }, { no_triggers => 1 });
201 $self->set({ closedate => \'NOW()' })->store;
206 if (C4::Context->preference("AcquisitionLog")) {
219 my $json = $basket->to_api;
221 Overloaded method that returns a JSON representation of the Koha::Acquisition::Basket object,
222 suitable for API output.
227 my ( $self, $params ) = @_;
229 my $json = $self->SUPER::to_api( $params );
231 $json->{closed} = ( $self->closedate )
238 =head3 to_api_mapping
240 This method returns the mapping for representing a Koha::Acquisition::Basket object
247 basketno => 'basket_id',
248 basketname => 'name',
249 booksellernote => 'vendor_note',
250 contractnumber => 'contract_id',
251 creationdate => 'creation_date',
252 closedate => 'close_date',
253 booksellerid => 'vendor_id',
254 authorisedby => 'creator_id',
255 booksellerinvoicenumber => undef,
256 basketgroupid => 'basket_group_id',
257 deliveryplace => 'delivery_library_id',
258 billingplace => 'billing_library_id',
259 branch => 'library_id',
260 is_standing => 'standing'
264 =head2 Internal methods
276 Aleisha Amohia <aleisha@catalyst.net.nz>