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
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.
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 = Koha::Acquisition::Orders->find( $id )->basket;
118 Returns the basket associated to the order.
124 my $basket_rs = $self->_result->basketno;
125 return Koha
::Acquisition
::Basket
->_new_from_dbic( $basket_rs );
130 my $fund = $order->fund
132 Returns the fund (aqbudgets) associated to the order.
138 my $fund_rs = $self->_result->budget;
139 return Koha
::Acquisition
::Fund
->_new_from_dbic( $fund_rs );
144 my $invoice = $order->invoice
146 Returns the invoice associated to the order.
152 my $invoice_rs = $self->_result->invoiceid;
153 return unless $invoice_rs;
154 return Koha
::Acquisition
::Invoice
->_new_from_dbic( $invoice_rs );
159 my $subscription = $order->subscription
161 Returns the subscription associated to the order.
167 my $subscription_rs = $self->_result->subscriptionid;
168 return unless $subscription_rs;
169 return Koha
::Subscription
->_new_from_dbic( $subscription_rs );
174 my $items = $order->items
176 Returns the items associated to the order.
182 # aqorders_items is not a join table
183 # There is no FK on items (may have been deleted)
184 my $items_rs = $self->_result->aqorders_items;
185 my @itemnumbers = $items_rs->get_column( 'itemnumber' )->all;
186 return Koha
::Items
->search({ itemnumber
=> \
@itemnumbers });
191 my $biblio = $order->biblio
193 Returns the bibliographic record associated to the order
199 my $biblio_rs= $self->_result->biblionumber;
200 return Koha
::Biblio
->_new_from_dbic( $biblio_rs );
205 my $duplicated_order = $order->duplicate_to($basket, [$default_values]);
207 Duplicate an existing order and attach it to a basket. $default_values can be specified as a hashref
208 that contain default values for the different order's attributes.
209 Items will be duplicated as well but barcodes will be set to null.
214 my ( $self, $basket, $default_values ) = @_;
216 $default_values //= {};
217 Koha
::Database
->schema->txn_do(
219 my $order_info = $self->unblessed;
220 undef $order_info->{ordernumber
};
226 datecancellationprinted
235 undef $order_info->{$field};
237 $order_info->{placed_on
} = dt_from_string
;
238 $order_info->{entrydate
} = dt_from_string
;
239 $order_info->{orderstatus
} = 'new';
240 $order_info->{quantityreceived
} = 0;
241 while ( my ( $field, $value ) = each %$default_values ) {
242 $order_info->{$field} = $value;
245 my $userenv = C4
::Context
->userenv;
246 $order_info->{created_by
} = $userenv->{number
};
247 $order_info->{basketno
} = $basket->basketno;
249 $new_order = Koha
::Acquisition
::Order
->new($order_info)->store;
251 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
252 my $items = $self->items;
253 while ( my ($item) = $items->next ) {
254 my $item_info = $item->unblessed;
255 undef $item_info->{itemnumber
};
256 undef $item_info->{barcode
};
257 my $new_item = Koha
::Item
->new($item_info)->store;
258 $new_order->add_item( $new_item->itemnumber );
267 =head2 Internal methods