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