Bug 12395: Save order line's creator
[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::Acquisition::Baskets;
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string output_pref );
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::Acquisition::Order Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =head3 new
37
38 Overloaded I<new> method for backwards compatibility.
39
40 =cut
41
42 sub new {
43     my ( $self, $params ) = @_;
44
45     my $schema  = Koha::Database->new->schema;
46     my @columns = $schema->source('Aqorder')->columns;
47
48     my $values =
49       { map { exists $params->{$_} ? ( $_ => $params->{$_} ) : () } @columns };
50     return $self->SUPER::new($values);
51 }
52
53 =head3 store
54
55 Overloaded I<store> method for backwards compatibility.
56
57 =cut
58
59 sub store {
60     my ($self) = @_;
61
62     my $schema  = Koha::Database->new->schema;
63     # Override quantity for standing orders
64     $self->quantity(1) if ( $self->basketno && $schema->resultset('Aqbasket')->find( $self->basketno )->is_standing );
65
66     # if these parameters are missing, we can't continue
67     for my $key (qw( basketno quantity biblionumber budget_id )) {
68         croak "Cannot insert order: Mandatory parameter $key is missing"
69           unless $self->$key;
70     }
71
72     if (not defined $self->{created_by}) {
73         my $userenv = C4::Context->userenv;
74         if ($userenv) {
75             $self->created_by($userenv->{number});
76         }
77     }
78
79     $self->quantityreceived(0) unless $self->quantityreceived;
80     $self->entrydate(dt_from_string) unless $self->entrydate;
81
82     $self->ordernumber(undef) unless $self->ordernumber;
83     $self = $self->SUPER::store( $self );
84
85     unless ( $self->parent_ordernumber ) {
86         $self->set( { parent_ordernumber => $self->ordernumber } );
87         $self = $self->SUPER::store( $self );
88     }
89
90     return $self;
91 }
92
93 =head3 add_item
94
95   $order->add_item( $itemnumber );
96
97 Link an item to this order.
98
99 =cut
100
101 sub add_item {
102     my ( $self, $itemnumber )  = @_;
103
104     my $schema = Koha::Database->new->schema;
105     my $rs = $schema->resultset('AqordersItem');
106     $rs->create({ ordernumber => $self->ordernumber, itemnumber => $itemnumber });
107 }
108
109 =head3 basket
110
111     my $basket = Koha::Acquisition::Orders->find( $id )->basket;
112
113 Returns the basket associated to the order.
114
115 =cut
116
117 sub basket {
118     my ( $self )  = @_;
119     my $basket_rs = $self->_result->basketno;
120     return Koha::Acquisition::Basket->_new_from_dbic( $basket_rs );
121 }
122
123 =head2 Internal methods
124
125 =head3 _type
126
127 =cut
128
129 sub _type {
130     return 'Aqorder';
131 }
132
133 1;