Bug 21156: Add plural translation capabilities to JS files
[koha.git] / Koha / Acquisition / Order.pm
blobe553386652a39f62a683463d0be28178d6aec4df
1 package Koha::Acquisition::Order;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Modern::Perl;
20 use Carp qw( croak );
22 use Koha::Acquisition::Baskets;
23 use Koha::Acquisition::Funds;
24 use Koha::Acquisition::Invoices;
25 use Koha::Database;
26 use Koha::DateUtils qw( dt_from_string output_pref );
27 use Koha::Biblios;
28 use Koha::Items;
29 use Koha::Subscriptions;
31 use base qw(Koha::Object);
33 =head1 NAME
35 Koha::Acquisition::Order Object class
37 =head1 API
39 =head2 Class methods
41 =head3 new
43 Overloaded I<new> method for backwards compatibility.
45 =cut
47 sub new {
48 my ( $self, $params ) = @_;
50 my $schema = Koha::Database->new->schema;
51 my @columns = $schema->source('Aqorder')->columns;
53 my $values =
54 { map { exists $params->{$_} ? ( $_ => $params->{$_} ) : () } @columns };
55 return $self->SUPER::new($values);
58 =head3 store
60 Overloaded I<store> method for backwards compatibility.
62 =cut
64 sub store {
65 my ($self) = @_;
67 my $schema = Koha::Database->new->schema;
68 # Override quantity for standing orders
69 $self->quantity(1) if ( $self->basketno && $schema->resultset('Aqbasket')->find( $self->basketno )->is_standing );
71 # if these parameters are missing, we can't continue
72 for my $key (qw( basketno quantity biblionumber budget_id )) {
73 croak "Cannot insert order: Mandatory parameter $key is missing"
74 unless $self->$key;
77 if (not defined $self->{created_by}) {
78 my $userenv = C4::Context->userenv;
79 if ($userenv) {
80 $self->created_by($userenv->{number});
84 $self->quantityreceived(0) unless $self->quantityreceived;
85 $self->entrydate(dt_from_string) unless $self->entrydate;
87 $self->ordernumber(undef) unless $self->ordernumber;
88 $self = $self->SUPER::store( $self );
90 unless ( $self->parent_ordernumber ) {
91 $self->set( { parent_ordernumber => $self->ordernumber } );
92 $self = $self->SUPER::store( $self );
95 return $self;
98 =head3 add_item
100 $order->add_item( $itemnumber );
102 Link an item to this order.
104 =cut
106 sub add_item {
107 my ( $self, $itemnumber ) = @_;
109 my $schema = Koha::Database->new->schema;
110 my $rs = $schema->resultset('AqordersItem');
111 $rs->create({ ordernumber => $self->ordernumber, itemnumber => $itemnumber });
114 =head3 basket
116 my $basket = $order->basket;
118 Returns the I<Koha::Acquisition::Basket> object for the basket associated
119 to the order.
121 =cut
123 sub basket {
124 my ( $self ) = @_;
125 my $basket_rs = $self->_result->basket;
126 return Koha::Acquisition::Basket->_new_from_dbic( $basket_rs );
129 =head3 fund
131 my $fund = $order->fund;
133 Returns the I<Koha::Acquisition::Fund> object for the fund (aqbudgets)
134 associated to the order.
136 =cut
138 sub fund {
139 my ( $self ) = @_;
140 my $fund_rs = $self->_result->fund;
141 return Koha::Acquisition::Fund->_new_from_dbic( $fund_rs );
144 =head3 invoice
146 my $invoice = $order->invoice;
148 Returns the I<Koha::Acquisition::Invoice> object for the invoice associated
149 to the order.
151 It returns B<undef> if no linked invoice is found.
153 =cut
155 sub invoice {
156 my ( $self ) = @_;
157 my $invoice_rs = $self->_result->invoice;
158 return unless $invoice_rs;
159 return Koha::Acquisition::Invoice->_new_from_dbic( $invoice_rs );
162 =head3 subscription
164 my $subscription = $order->subscription
166 Returns the I<Koha::Subscription> object for the subscription associated
167 to the order.
169 It returns B<undef> if no linked subscription is found.
171 =cut
173 sub subscription {
174 my ( $self ) = @_;
175 my $subscription_rs = $self->_result->subscription;
176 return unless $subscription_rs;
177 return Koha::Subscription->_new_from_dbic( $subscription_rs );
180 =head3 current_item_level_holds
182 my $holds = $order->current_item_level_holds;
184 Returns the current item-level holds associated to the order. It returns a I<Koha::Holds>
185 resultset in scalar context or a list of I<Koha::Hold> objects in list context.
187 It returns B<undef> if no I<biblio> or no I<items> are linked to the order.
189 =cut
191 sub current_item_level_holds {
192 my ($self) = @_;
194 my $items_rs = $self->_result->aqorders_items;
195 my @item_numbers = $items_rs->get_column('itemnumber')->all;
197 return unless @item_numbers;
199 my $biblio = $self->biblio;
200 return unless $biblio;
202 return $biblio->current_holds->search(
204 itemnumber => {
205 -in => \@item_numbers
211 =head3 items
213 my $items = $order->items
215 Returns the items associated to the order.
217 =cut
219 sub items {
220 my ( $self ) = @_;
221 # aqorders_items is not a join table
222 # There is no FK on items (may have been deleted)
223 my $items_rs = $self->_result->aqorders_items;
224 my @itemnumbers = $items_rs->get_column( 'itemnumber' )->all;
225 return Koha::Items->search({ itemnumber => \@itemnumbers });
228 =head3 biblio
230 my $biblio = $order->biblio
232 Returns the bibliographic record associated to the order
234 =cut
236 sub biblio {
237 my ( $self ) = @_;
238 my $biblio_rs= $self->_result->biblio;
239 return unless $biblio_rs;
240 return Koha::Biblio->_new_from_dbic( $biblio_rs );
243 =head3 duplicate_to
245 my $duplicated_order = $order->duplicate_to($basket, [$default_values]);
247 Duplicate an existing order and attach it to a basket. $default_values can be specified as a hashref
248 that contain default values for the different order's attributes.
249 Items will be duplicated as well but barcodes will be set to null.
251 =cut
253 sub duplicate_to {
254 my ( $self, $basket, $default_values ) = @_;
255 my $new_order;
256 $default_values //= {};
257 Koha::Database->schema->txn_do(
258 sub {
259 my $order_info = $self->unblessed;
260 undef $order_info->{ordernumber};
261 for my $field (
263 ordernumber
264 received_on
265 datereceived
266 invoiceid
267 datecancellationprinted
268 cancellationreason
269 purchaseordernumber
270 claims_count
271 claimed_date
272 parent_ordernumber
276 undef $order_info->{$field};
278 $order_info->{placed_on} = dt_from_string;
279 $order_info->{entrydate} = dt_from_string;
280 $order_info->{orderstatus} = 'new';
281 $order_info->{quantityreceived} = 0;
282 while ( my ( $field, $value ) = each %$default_values ) {
283 $order_info->{$field} = $value;
286 my $userenv = C4::Context->userenv;
287 $order_info->{created_by} = $userenv->{number};
288 $order_info->{basketno} = $basket->basketno;
290 $new_order = Koha::Acquisition::Order->new($order_info)->store;
292 if ( ! $self->subscriptionid && $self->basket->effective_create_items eq 'ordering') { # Do copy items if not a subscription order AND if items are created on ordering
293 my $items = $self->items;
294 while ( my ($item) = $items->next ) {
295 my $item_info = $item->unblessed;
296 undef $item_info->{itemnumber};
297 undef $item_info->{barcode};
298 my $new_item = Koha::Item->new($item_info)->store;
299 $new_order->add_item( $new_item->itemnumber );
304 return $new_order;
307 =head3 to_api_mapping
309 This method returns the mapping for representing a Koha::Acquisition::Order object
310 on the API.
312 =cut
314 sub to_api_mapping {
315 return {
316 basketno => 'basket_id',
317 biblionumber => 'biblio_id',
318 budget_id => 'fund_id',
319 budgetdate => undef, # unused
320 cancellationreason => 'cancellation_reason',
321 claimed_date => 'last_claim_date',
322 datecancellationprinted => 'cancellation_date',
323 datereceived => 'date_received',
324 discount => 'discount_rate',
325 entrydate => 'entry_date',
326 freight => 'shipping_cost',
327 invoiceid => 'invoice_id',
328 line_item_id => undef, # EDIFACT related
329 listprice => 'list_price',
330 order_internalnote => 'internal_note',
331 order_vendornote => 'vendor_note',
332 ordernumber => 'order_id',
333 orderstatus => 'status',
334 parent_ordernumber => 'parent_order_id',
335 purchaseordernumber => undef, # obsolete
336 quantityreceived => 'quantity_received',
337 replacementprice => 'replacement_price',
338 sort1 => 'statistics_1',
339 sort1_authcat => 'statistics_1_authcat',
340 sort2 => 'statistics_2',
341 sort2_authcat => 'statistics_2_authcat',
342 subscriptionid => 'subscription_id',
343 suppliers_reference_number => undef, # EDIFACT related
344 suppliers_reference_qualifier => undef, # EDIFACT related
345 suppliers_report => undef, # EDIFACT related
346 tax_rate_bak => undef, # unused
347 tax_value_bak => undef, # unused
348 uncertainprice => 'uncertain_price',
349 unitprice => 'unit_price',
350 unitprice_tax_excluded => 'unit_price_tax_excluded',
351 unitprice_tax_included => 'unit_price_tax_included'
355 =head2 Internal methods
357 =head3 _type
359 =cut
361 sub _type {
362 return 'Aqorder';