Bug 24161: Fix license statements
[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::Patrons;
26
27 use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields );
28
29 =head1 NAME
30
31 Koha::Acquisition::Basket - Koha Basket Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =cut
38
39 =head3 bookseller
40
41 Returns the vendor
42
43 =cut
44
45 sub bookseller {
46     my ($self) = @_;
47     my $bookseller_rs = $self->_result->booksellerid;
48     return Koha::Acquisition::Bookseller->_new_from_dbic( $bookseller_rs );
49 }
50
51 =head3 creator
52
53     my $creator = $basket->creator;
54
55 Returns the I<Koha::Patron> for the basket creator.
56
57 =cut
58
59 sub creator {
60     my ($self) = @_;
61     my $borrowernumber = $self->authorisedby; # FIXME missing FK here
62     return unless $borrowernumber;
63     return Koha::Patrons->find( $borrowernumber );
64 }
65
66 =head3 basket_group
67
68 Returns the basket group associated to this basket
69
70 =cut
71
72 sub basket_group {
73     my ($self) = @_;
74
75     my $basket_group_rs = $self->_result->basket_group;
76     return unless $basket_group_rs;
77     return Koha::Acquisition::BasketGroup->_new_from_dbic( $basket_group_rs );
78 }
79
80 =head3 effective_create_items
81
82 Returns C<create_items> for this basket, falling back to C<AcqCreateItem> if unset.
83
84 =cut
85
86 sub effective_create_items {
87     my ( $self ) = @_;
88
89     return $self->create_items || C4::Context->preference('AcqCreateItem');
90 }
91
92 =head3 estimated_delivery_date
93
94 my $estimated_delivery_date = $basket->estimated_delivery_date;
95
96 Return the estimated delivery date for this basket.
97
98 It is calculated adding the delivery time of the vendor to the close date of this basket.
99
100 Return implicit undef if the basket is not closed, or the vendor does not have a delivery time.
101
102 =cut
103
104 sub estimated_delivery_date {
105     my ( $self ) = @_;
106     return unless $self->closedate and $self->bookseller->deliverytime;
107     return dt_from_string($self->closedate)->add( days => $self->bookseller->deliverytime);
108 }
109
110 =head3 late_since_days
111
112 my $number_of_days_late = $basket->late_since_days;
113
114 Return the number of days the basket is late.
115
116 Return implicit undef if the basket is not closed.
117
118 =cut
119
120 sub late_since_days {
121     my ( $self ) = @_;
122     return unless $self->closedate;
123     return dt_from_string->delta_days(dt_from_string($self->closedate))->delta_days();
124 }
125
126 =head3 authorizer
127
128 my $authorizer = $basket->authorizer;
129
130 Returns the patron who authorized/created this basket.
131
132 =cut
133
134 sub authorizer {
135     my ($self) = @_;
136     # FIXME We should use a DBIC rs, but the FK is missing
137     return unless $self->authorisedby;
138     return scalar Koha::Patrons->find($self->authorisedby);
139 }
140
141
142 =head3 to_api
143
144     my $json = $basket->to_api;
145
146 Overloaded method that returns a JSON representation of the Koha::Acquisition::Basket object,
147 suitable for API output.
148
149 =cut
150
151 sub to_api {
152     my ( $self, $params ) = @_;
153
154     my $json = $self->SUPER::to_api( $params );
155
156     $json->{closed} = ( $self->closedate )
157                                     ? Mojo::JSON->true
158                                     : Mojo::JSON->false;
159
160     return $json;
161 }
162
163 =head3 to_api_mapping
164
165 This method returns the mapping for representing a Koha::Acquisition::Basket object
166 on the API.
167
168 =cut
169
170 sub to_api_mapping {
171     return {
172         basketno                => 'basket_id',
173         basketname              => 'name',
174         booksellernote          => 'vendor_note',
175         contractnumber          => 'contract_id',
176         creationdate            => 'creation_date',
177         closedate               => 'close_date',
178         booksellerid            => 'vendor_id',
179         authorisedby            => 'creator_id',
180         booksellerinvoicenumber => undef,
181         basketgroupid           => 'basket_group_id',
182         deliveryplace           => 'delivery_library_id',
183         billingplace            => 'billing_library_id',
184         branch                  => 'library_id',
185         is_standing             => 'standing'
186     };
187 }
188
189 =head2 Internal methods
190
191 =head3 _type
192
193 =cut
194
195 sub _type {
196     return 'Aqbasket';
197 }
198
199 =head1 AUTHOR
200
201 Aleisha Amohia <aleisha@catalyst.net.nz>
202 Catalyst IT
203
204 =cut
205
206 1;