Bug 26922: Regression tests
[koha.git] / Koha / Acquisition / Orders.pm
blobb8ee2f7d65570d66a4c7592122786bbe9a423489
1 package Koha::Acquisition::Orders;
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Carp;
22 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Acquisition::Order;
27 use base qw(Koha::Objects);
29 =head1 NAME
31 Koha::Acquisition::Orders object set class
33 =head1 API
35 =head2 Class Methods
37 =head3 filter_by_lates
39 my $late_orders = $orders->filter_by_lates($params);
41 Filter an order set given different parameters.
43 This is the equivalent method of the former GetLateOrders C4 subroutine
45 $params can be:
47 =over
49 =item C<delay> the number of days the basket has been closed
51 =item C<bookseller_id> the bookseller id
53 =item C<estimated_from> Beginning of the estimated delivery date
55 =item C<estimated_to> End of the estimated delivery date
57 =back
59 =cut
61 sub filter_by_lates {
62 my ( $self, $params ) = @_;
63 my $delay = $params->{delay};
64 my $bookseller_id = $params->{bookseller_id};
65 # my $branchcode = $params->{branchcode}; # FIXME do we really need this
66 my $estimated_from = $params->{estimated_from};
67 my $estimated_to = $params->{estimated_to};
68 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
70 my @delivery_time_conditions;
71 my $date_add = "DATE_ADD(basketno.closedate, INTERVAL COALESCE(booksellerid.deliverytime, booksellerid.deliverytime, 0) day)";
72 if ( defined $estimated_from or defined $estimated_to ) {
73 push @delivery_time_conditions, \[ "$date_add IS NOT NULL" ];
75 if ( defined $estimated_from ) {
76 push @delivery_time_conditions, \[ "$date_add >= ?", $dtf->format_date($estimated_from) ];
78 if ( defined $estimated_to ) {
79 push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date($estimated_to) ];
81 if ( defined $estimated_from and not defined $estimated_to ) {
82 push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date(dt_from_string) ];
85 $self->search(
87 -or => [
88 { datereceived => undef },
89 quantityreceived => { '<' => \'quantity' }
91 'basketno.closedate' => [
92 -and =>
93 { '!=' => undef },
95 defined $delay
96 ? (
97 '<=' => $dtf->format_date(
98 dt_from_string->subtract( days => $delay )
101 : ()
104 'datecancellationprinted' => undef,
106 $bookseller_id
107 ? ( 'basketno.booksellerid' => $bookseller_id )
108 : ()
111 # ( $branchcode ? ('borrower.branchcode')) # FIXME branch is not a filter we may not need to implement this
113 ( @delivery_time_conditions ? ( -and => \@delivery_time_conditions ) : ()),
115 C4::Context->preference('IndependentBranches')
116 && !C4::Context->IsSuperLibrarian
117 ? ( 'borrower.branchcode' => C4::Context->userenv->{branch} )
118 : ()
121 ( orderstatus => { '!=' => 'cancelled' } ),
125 '+select' => [\"DATE_ADD(basketno.closedate, INTERVAL COALESCE(booksellerid.deliverytime, booksellerid.deliverytime, 0) day)"],
126 '+as' => ['estimated_delivery_date'],
127 join => { 'basketno' => 'booksellerid' },
128 prefetch => {'basketno' => 'booksellerid'},
133 =head3 filter_by_current
135 $orders->filter_by_current
137 Return the orders of the set that have not been cancelled.
139 =cut
141 sub filter_by_current {
142 my ($self) = @_;
143 return $self->search(
145 datecancellationprinted => undef,
150 =head3 filter_by_cancelled
152 $orders->filter_by_cancelled
154 Return the orders of the set that have been cancelled.
156 =cut
158 sub filter_by_cancelled {
159 my ($self) = @_;
160 return $self->search(
162 datecancellationprinted => { '!=' => undef }
167 =head2 Internal methods
169 =head3 _type (internal)
171 =cut
173 sub _type {
174 return 'Aqorder';
177 =head3 object_class (internal)
179 =cut
181 sub object_class {
182 return 'Koha::Acquisition::Order';