2b06b81d1e50499ea8de6e99cfd4fc7f4967ce8d
[srvgit] / Koha / Acquisition / Orders.pm
1 package Koha::Acquisition::Orders;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Carp;
21
22 use Koha::Database;
23
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Acquisition::Order;
26 use Koha::Exceptions::Exception;
27
28 use base qw(Koha::Objects);
29
30 =head1 NAME
31
32 Koha::Acquisition::Orders object set class
33
34 =head1 API
35
36 =head2 Class methods
37
38 =head3 filter_by_lates
39
40 my $late_orders = $orders->filter_by_lates($params);
41
42 Filter an order set given different parameters.
43
44 This is the equivalent method of the former GetLateOrders C4 subroutine
45
46 $params can be:
47
48 =over
49
50 =item C<delay> the number of days the basket has been closed
51
52 =item C<bookseller_id> the bookseller id
53
54 =item C<estimated_from> Beginning of the estimated delivery date
55
56 =item C<estimated_to> End of the estimated delivery date
57
58 =back
59
60 =cut
61
62 sub filter_by_lates {
63     my ( $self, $params ) = @_;
64     my $delay = $params->{delay};
65     my $bookseller_id = $params->{bookseller_id};
66     # my $branchcode = $params->{branchcode}; # FIXME do we really need this
67     my $estimated_from = $params->{estimated_from};
68     my $estimated_to = $params->{estimated_to};
69     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
70
71     my @delivery_time_conditions;
72     my $date_add = "DATE_ADD(basketno.closedate, INTERVAL COALESCE(booksellerid.deliverytime, booksellerid.deliverytime, 0) day)";
73     if ( defined $estimated_from or defined $estimated_to ) {
74         push @delivery_time_conditions, \[ "$date_add IS NOT NULL" ];
75     }
76     if ( defined $estimated_from ) {
77         push @delivery_time_conditions, \[ "$date_add >= ?", $dtf->format_date($estimated_from) ];
78     }
79     if ( defined $estimated_to ) {
80         push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date($estimated_to) ];
81     }
82     if ( defined $estimated_from and not defined $estimated_to ) {
83         push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date(dt_from_string) ];
84     }
85
86     $self->search(
87         {
88             -or => [
89                 { datereceived => undef },
90                 quantityreceived => { '<' => \'quantity' }
91             ],
92             'basketno.closedate' => [
93                 -and =>
94                 { '!=' => undef },
95                 {
96                     defined $delay
97                     ? (
98                         '<=' => $dtf->format_date(
99                             dt_from_string->subtract( days => $delay )
100                         )
101                       )
102                     : ()
103                 }
104               ],
105             'datecancellationprinted' => undef,
106             (
107                 $bookseller_id
108                 ? ( 'basketno.booksellerid' => $bookseller_id )
109                 : ()
110             ),
111
112             # ( $branchcode ? ('borrower.branchcode')) # FIXME branch is not a filter we may not need to implement this
113
114             ( @delivery_time_conditions ? ( -and => \@delivery_time_conditions ) : ()),
115             (
116                 C4::Context->preference('IndependentBranches')
117                   && !C4::Context->IsSuperLibrarian
118                 ? ( 'borrower.branchcode' => C4::Context->userenv->{branch} )
119                 : ()
120             ),
121
122             ( orderstatus => { '!=' => 'cancelled' } ),
123
124         },
125         {
126             '+select' => [\"DATE_ADD(basketno.closedate, INTERVAL COALESCE(booksellerid.deliverytime, booksellerid.deliverytime, 0) day)"],
127             '+as' => ['estimated_delivery_date'],
128             join => { 'basketno' => 'booksellerid' },
129             prefetch => {'basketno' => 'booksellerid'},
130         }
131     );
132 }
133
134 =head3 filter_by_active
135
136     my $new_rs = $orders->filter_by_active;
137
138 Returns a new resultset filtering orders that are not active.
139
140 =cut
141
142 sub filter_by_active {
143     my ($self) = @_;
144     return $self->search(
145         {
146             '-or' => [
147                 { 'basket.is_standing' => 1,
148                   'orderstatus' => [ 'new', 'ordered', 'partial' ] },
149                 { 'orderstatus' => [ 'ordered', 'partial' ] }
150             ]
151         },
152         { join => 'basket' }
153     );
154 }
155
156 =head3 filter_by_current
157
158     $orders->filter_by_current
159
160 Return the orders of the set that have not been cancelled.
161
162 =cut
163
164 sub filter_by_current {
165     my ($self) = @_;
166     return $self->search(
167         {
168             datecancellationprinted => undef,
169         }
170     );
171 }
172
173 =head3 filter_by_cancelled
174
175     $orders->filter_by_cancelled
176
177 Return the orders of the set that have been cancelled.
178
179 =cut
180
181 sub filter_by_cancelled {
182     my ($self) = @_;
183     return $self->search(
184         {
185             datecancellationprinted => { '!=' => undef }
186         }
187     );
188 }
189
190 =head3 filter_by_id_including_transfers
191
192     my $orders = $orders->filter_by_id_including_transfers(
193         {
194             ordernumber => $ordernumber
195         }
196     );
197
198 When searching for orders by I<ordernumber>, include the aqorders_transfers table
199 so we can find orders that have changed their ordernumber as the result of a transfer
200
201 =cut
202
203 sub filter_by_id_including_transfers {
204     my ( $self, $params ) = @_;
205
206     Koha::Exceptions::MissingParameter->throw( "The ordernumber param is mandatory" )
207         unless $params->{ordernumber};
208
209     return $self->search(
210         {
211             -or => [
212                 { 'me.ordernumber' => $params->{ordernumber} },
213                 { 'aqorders_transfers_ordernumber_to.ordernumber_from' => $params->{ordernumber} }
214             ]
215         },
216         { join => 'aqorders_transfers_ordernumber_to' }
217     );
218 }
219
220 =head2 Internal methods
221
222 =head3 _type
223
224 =cut
225
226 sub _type {
227     return 'Aqorder';
228 }
229
230 =head3 object_class
231
232 =cut
233
234 sub object_class {
235     return 'Koha::Acquisition::Order';
236 }
237
238 1;