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 );
28 use Koha::Subscriptions;
30 use base qw(Koha::Object);
34 Koha::Acquisition::Order Object class
42 Overloaded I<new> method for backwards compatibility.
47 my ( $self, $params ) = @_;
49 my $schema = Koha::Database->new->schema;
50 my @columns = $schema->source('Aqorder')->columns;
53 { map { exists $params->{$_} ? ( $_ => $params->{$_} ) : () } @columns };
54 return $self->SUPER::new($values);
59 Overloaded I<store> method for backwards compatibility.
66 my $schema = Koha::Database->new->schema;
67 # Override quantity for standing orders
68 $self->quantity(1) if ( $self->basketno && $schema->resultset('Aqbasket')->find( $self->basketno )->is_standing );
70 # if these parameters are missing, we can't continue
71 for my $key (qw( basketno quantity biblionumber budget_id )) {
72 croak "Cannot insert order: Mandatory parameter $key is missing"
76 if (not defined $self->{created_by}) {
77 my $userenv = C4::Context->userenv;
79 $self->created_by($userenv->{number});
83 $self->quantityreceived(0) unless $self->quantityreceived;
84 $self->entrydate(dt_from_string) unless $self->entrydate;
86 $self->ordernumber(undef) unless $self->ordernumber;
87 $self = $self->SUPER::store( $self );
89 unless ( $self->parent_ordernumber ) {
90 $self->set( { parent_ordernumber => $self->ordernumber } );
91 $self = $self->SUPER::store( $self );
99 $order->add_item( $itemnumber );
101 Link an item to this order.
106 my ( $self, $itemnumber ) = @_;
108 my $schema = Koha::Database->new->schema;
109 my $rs = $schema->resultset('AqordersItem');
110 $rs->create({ ordernumber => $self->ordernumber, itemnumber => $itemnumber });
115 my $basket = Koha::Acquisition::Orders->find( $id )->basket;
117 Returns the basket associated to the order.
123 my $basket_rs = $self->_result->basketno;
124 return Koha::Acquisition::Basket->_new_from_dbic( $basket_rs );
129 my $fund = $order->fund
131 Returns the fund (aqbudgets) associated to the order.
137 my $fund_rs = $self->_result->budget;
138 return Koha::Acquisition::Fund->_new_from_dbic( $fund_rs );
143 my $invoice = $order->invoice
145 Returns the invoice associated to the order.
151 my $invoice_rs = $self->_result->invoiceid;
152 return unless $invoice_rs;
153 return Koha::Acquisition::Invoice->_new_from_dbic( $invoice_rs );
158 my $subscription = $order->subscription
160 Returns the subscription associated to the order.
166 my $subscription_rs = $self->_result->subscriptionid;
167 return unless $subscription_rs;
168 return Koha::Subscription->_new_from_dbic( $subscription_rs );
173 my $items = $order->items
175 Returns the items associated to the order.
181 # aqorders_items is not a join table
182 # There is no FK on items (may have been deleted)
183 my $items_rs = $self->_result->aqorders_items;
184 my @itemnumbers = $items_rs->get_column( 'itemnumber' )->all;
185 return Koha::Items->search({ itemnumber => \@itemnumbers });
190 my $duplicated_order = $order->duplicate_to($basket, [$default_values]);
192 Duplicate an existing order and attach it to a basket. $default_values can be specified as a hashref
193 that contain default values for the different order's attributes.
194 Items will be duplicated as well but barcodes will be set to null.
199 my ( $self, $basket, $default_values ) = @_;
201 $default_values //= {};
202 Koha::Database->schema->txn_do(
204 my $order_info = $self->unblessed;
205 undef $order_info->{ordernumber};
211 datecancellationprinted
220 undef $order_info->{$field};
222 $order_info->{placed_on} = dt_from_string;
223 $order_info->{entrydate} = dt_from_string;
224 $order_info->{orderstatus} = 'new';
225 $order_info->{quantityreceived} = 0;
226 while ( my ( $field, $value ) = each %$default_values ) {
227 $order_info->{$field} = $value;
230 # FIXME $order_info->{created_by} = logged_in_user?
231 $order_info->{basketno} = $basket->basketno;
233 $new_order = Koha::Acquisition::Order->new($order_info)->store;
234 my $items = $self->items;
235 while ( my ($item) = $items->next ) {
236 my $item_info = $item->unblessed;
237 undef $item_info->{itemnumber};
238 undef $item_info->{barcode};
239 my $new_item = Koha::Item->new($item_info)->store;
240 $new_order->add_item( $new_item->itemnumber );
248 =head2 Internal methods