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