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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Koha::Acquisition::Baskets;
23 use Koha::Acquisition::Funds;
24 use Koha::Acquisition::Invoices;
26 use Koha::DateUtils qw( dt_from_string output_pref );
29 use Koha::Subscriptions;
31 use base qw(Koha::Object);
35 Koha::Acquisition::Order Object class
43 Overloaded I<new> method for backwards compatibility.
48 my ( $self, $params ) = @_;
50 my $schema = Koha::Database->new->schema;
51 my @columns = $schema->source('Aqorder')->columns;
54 { map { exists $params->{$_} ? ( $_ => $params->{$_} ) : () } @columns };
55 return $self->SUPER::new($values);
60 Overloaded I<store> method for backwards compatibility.
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"
77 if (not defined $self->{created_by}) {
78 my $userenv = C4::Context->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 );
100 $order->add_item( $itemnumber );
102 Link an item to this order.
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 });
116 my $basket = $order->basket;
118 Returns the I<Koha::Acquisition::Basket> object for the basket associated
125 my $basket_rs = $self->_result->basket;
126 return Koha::Acquisition::Basket->_new_from_dbic( $basket_rs );
131 my $fund = $order->fund;
133 Returns the I<Koha::Acquisition::Fund> object for the fund (aqbudgets)
134 associated to the order.
140 my $fund_rs = $self->_result->fund;
141 return Koha::Acquisition::Fund->_new_from_dbic( $fund_rs );
146 my $invoice = $order->invoice;
148 Returns the I<Koha::Acquisition::Invoice> object for the invoice associated
151 It returns B<undef> if no linked invoice is found.
157 my $invoice_rs = $self->_result->invoice;
158 return unless $invoice_rs;
159 return Koha::Acquisition::Invoice->_new_from_dbic( $invoice_rs );
164 my $subscription = $order->subscription
166 Returns the I<Koha::Subscription> object for the subscription associated
169 It returns B<undef> if no linked subscription is found.
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.
191 sub current_item_level_holds {
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(
205 -in => \@item_numbers
213 my $items = $order->items
215 Returns the items associated to the order.
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 });
230 my $biblio = $order->biblio
232 Returns the bibliographic record associated to the order
238 my $biblio_rs= $self->_result->biblio;
239 return unless $biblio_rs;
240 return Koha::Biblio->_new_from_dbic( $biblio_rs );
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.
254 my ( $self, $basket, $default_values ) = @_;
256 $default_values //= {};
257 Koha::Database->schema->txn_do(
259 my $order_info = $self->unblessed;
260 undef $order_info->{ordernumber};
267 datecancellationprinted
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 );
307 =head3 to_api_mapping
309 This method returns the mapping for representing a Koha::Acquisition::Order object
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