Bug 19256: Make Koha::Acq::Order using Koha::Object
[koha.git] / Koha / Acquisition / Order.pm
1 package Koha::Acquisition::Order;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Carp qw( croak );
21
22 use Koha::Database;
23 use Koha::DateUtils qw( dt_from_string output_pref );
24
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::Acquisition::Order Object class
30
31 =head1 API
32
33 =head2 Class Methods
34
35 =cut
36
37 sub new {
38     my ( $self, $params ) = @_;
39
40     my $schema  = Koha::Database->new->schema;
41     my @columns = $schema->source('Aqorder')->columns;
42
43     my $values =
44       { map { exists $params->{$_} ? ( $_ => $params->{$_} ) : () } @columns };
45     return $self->SUPER::new($values);
46 }
47
48 sub store {
49     my ($self) = @_;
50
51     my $schema  = Koha::Database->new->schema;
52     # Override quantity for standing orders
53     $self->quantity(1) if ( $self->basketno && $schema->resultset('Aqbasket')->find( $self->basketno )->is_standing );
54
55     # if these parameters are missing, we can't continue
56     for my $key (qw( basketno quantity biblionumber budget_id )) {
57         croak "Cannot insert order: Mandatory parameter $key is missing"
58           unless $self->$key;
59     }
60
61     $self->quantityreceived(0) unless $self->quantityreceived;
62     $self->entrydate(output_pref( { dt => dt_from_string, dateformat => 'iso' } )) unless $self->entrydate;
63
64     $self->ordernumber(undef) unless $self->ordernumber;
65     $self = $self->SUPER::store( $self );
66
67     unless ( $self->parent_ordernumber ) {
68         $self->set( { parent_ordernumber => $self->ordernumber } );
69         $self = $self->SUPER::store( $self );
70     }
71
72     return $self;
73 }
74
75 =head3 add_item
76
77   $order->add_item( $itemnumber );
78
79 Link an item to this order.
80
81 =cut
82
83 sub add_item {
84     my ( $self, $itemnumber )  = @_;
85
86     my $schema = Koha::Database->new->schema;
87     my $rs = $schema->resultset('AqordersItem');
88     $rs->create({ ordernumber => $self->ordernumber, itemnumber => $itemnumber });
89 }
90
91 =head3 _type
92
93 =cut
94
95 sub _type {
96     return 'Aqorder';
97 }
98
99 1;