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