Bug 23376: Move AcqCreateItem logic to template
[koha.git] / Koha / Acquisition / Basket.pm
blob4b89fd08f4fad2f9930d9699104826c591dcd5a8
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>.
20 use Modern::Perl;
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::Patrons;
28 use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields );
30 =head1 NAME
32 Koha::Acquisition::Basket - Koha Basket Object class
34 =head1 API
36 =head2 Class methods
38 =cut
40 =head3 bookseller
42 Returns the vendor
44 =cut
46 sub bookseller {
47 my ($self) = @_;
48 my $bookseller_rs = $self->_result->booksellerid;
49 return Koha::Acquisition::Bookseller->_new_from_dbic( $bookseller_rs );
52 =head3 creator
54 my $creator = $basket->creator;
56 Returns the I<Koha::Patron> for the basket creator.
58 =cut
60 sub creator {
61 my ($self) = @_;
62 my $borrowernumber = $self->authorisedby; # FIXME missing FK here
63 return unless $borrowernumber;
64 return Koha::Patrons->find( $borrowernumber );
67 =head3 basket_group
69 Returns the basket group associated to this basket
71 =cut
73 sub basket_group {
74 my ($self) = @_;
76 my $basket_group_rs = $self->_result->basket_group;
77 return unless $basket_group_rs;
78 return Koha::Acquisition::BasketGroup->_new_from_dbic( $basket_group_rs );
81 =head3 orders
83 my $orders = $basket->orders;
85 Returns a Koha::Acquisition::Orders resultset, with the orders linked
86 to this basket.
88 =cut
90 sub orders {
91 my ($self) = @_;
93 my $orders_rs = $self->_result->orders;
94 return Koha::Acquisition::Orders->_new_from_dbic( $orders_rs );
97 =head3 effective_create_items
99 Returns C<create_items> for this basket, falling back to C<AcqCreateItem> if unset.
101 =cut
103 sub effective_create_items {
104 my ( $self ) = @_;
106 return $self->create_items || C4::Context->preference('AcqCreateItem');
109 =head3 estimated_delivery_date
111 my $estimated_delivery_date = $basket->estimated_delivery_date;
113 Return the estimated delivery date for this basket.
115 It is calculated adding the delivery time of the vendor to the close date of this basket.
117 Return implicit undef if the basket is not closed, or the vendor does not have a delivery time.
119 =cut
121 sub estimated_delivery_date {
122 my ( $self ) = @_;
123 return unless $self->closedate and $self->bookseller->deliverytime;
124 return dt_from_string($self->closedate)->add( days => $self->bookseller->deliverytime);
127 =head3 late_since_days
129 my $number_of_days_late = $basket->late_since_days;
131 Return the number of days the basket is late.
133 Return implicit undef if the basket is not closed.
135 =cut
137 sub late_since_days {
138 my ( $self ) = @_;
139 return unless $self->closedate;
140 return dt_from_string->delta_days(dt_from_string($self->closedate))->delta_days();
143 =head3 authorizer
145 my $authorizer = $basket->authorizer;
147 Returns the patron who authorized/created this basket.
149 =cut
151 sub authorizer {
152 my ($self) = @_;
153 # FIXME We should use a DBIC rs, but the FK is missing
154 return unless $self->authorisedby;
155 return scalar Koha::Patrons->find($self->authorisedby);
159 =head3 to_api
161 my $json = $basket->to_api;
163 Overloaded method that returns a JSON representation of the Koha::Acquisition::Basket object,
164 suitable for API output.
166 =cut
168 sub to_api {
169 my ( $self, $params ) = @_;
171 my $json = $self->SUPER::to_api( $params );
173 $json->{closed} = ( $self->closedate )
174 ? Mojo::JSON->true
175 : Mojo::JSON->false;
177 return $json;
180 =head3 to_api_mapping
182 This method returns the mapping for representing a Koha::Acquisition::Basket object
183 on the API.
185 =cut
187 sub to_api_mapping {
188 return {
189 basketno => 'basket_id',
190 basketname => 'name',
191 booksellernote => 'vendor_note',
192 contractnumber => 'contract_id',
193 creationdate => 'creation_date',
194 closedate => 'close_date',
195 booksellerid => 'vendor_id',
196 authorisedby => 'creator_id',
197 booksellerinvoicenumber => undef,
198 basketgroupid => 'basket_group_id',
199 deliveryplace => 'delivery_library_id',
200 billingplace => 'billing_library_id',
201 branch => 'library_id',
202 is_standing => 'standing'
206 =head2 Internal methods
208 =head3 _type
210 =cut
212 sub _type {
213 return 'Aqbasket';
216 =head1 AUTHOR
218 Aleisha Amohia <aleisha@catalyst.net.nz>
219 Catalyst IT
221 =cut