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>.
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Acquisition::Order;
26 use Koha::Exceptions::Exception;
28 use base qw(Koha::Objects);
32 Koha::Acquisition::Orders object set class
38 =head3 filter_by_lates
40 my $late_orders = $orders->filter_by_lates($params);
42 Filter an order set given different parameters.
44 This is the equivalent method of the former GetLateOrders C4 subroutine
50 =item C<delay> the number of days the basket has been closed
52 =item C<bookseller_id> the bookseller id
54 =item C<estimated_from> Beginning of the estimated delivery date
56 =item C<estimated_to> End of the estimated delivery date
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;
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" ];
76 if ( defined $estimated_from ) {
77 push @delivery_time_conditions, \[ "$date_add >= ?", $dtf->format_date($estimated_from) ];
79 if ( defined $estimated_to ) {
80 push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date($estimated_to) ];
82 if ( defined $estimated_from and not defined $estimated_to ) {
83 push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date(dt_from_string) ];
89 { datereceived => undef },
90 quantityreceived => { '<' => \'quantity' }
92 'basketno.closedate' => [
98 '<=' => $dtf->format_date(
99 dt_from_string->subtract( days => $delay )
105 'datecancellationprinted' => undef,
108 ? ( 'basketno.booksellerid' => $bookseller_id )
112 # ( $branchcode ? ('borrower.branchcode')) # FIXME branch is not a filter we may not need to implement this
114 ( @delivery_time_conditions ? ( -and => \@delivery_time_conditions ) : ()),
116 C4::Context->preference('IndependentBranches')
117 && !C4::Context->IsSuperLibrarian
118 ? ( 'borrower.branchcode' => C4::Context->userenv->{branch} )
122 ( orderstatus => { '!=' => 'cancelled' } ),
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'},
134 =head3 filter_by_active
136 my $new_rs = $orders->filter_by_active;
138 Returns a new resultset filtering orders that are not active.
142 sub filter_by_active {
144 return $self->search(
147 { 'basket.is_standing' => 1,
148 'orderstatus' => [ 'new', 'ordered', 'partial' ] },
149 { 'orderstatus' => [ 'ordered', 'partial' ] }
156 =head3 filter_by_current
158 $orders->filter_by_current
160 Return the orders of the set that have not been cancelled.
164 sub filter_by_current {
166 return $self->search(
168 datecancellationprinted => undef,
173 =head3 filter_by_cancelled
175 $orders->filter_by_cancelled
177 Return the orders of the set that have been cancelled.
181 sub filter_by_cancelled {
183 return $self->search(
185 datecancellationprinted => { '!=' => undef }
190 =head3 filter_by_id_including_transfers
192 my $orders = $orders->filter_by_id_including_transfers(
194 ordernumber => $ordernumber
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
203 sub filter_by_id_including_transfers {
204 my ( $self, $params ) = @_;
206 Koha::Exceptions::MissingParameter->throw( "The ordernumber param is mandatory" )
207 unless $params->{ordernumber};
209 return $self->search(
212 { 'me.ordernumber' => $params->{ordernumber} },
213 { 'aqorders_transfers_ordernumber_to.ordernumber_from' => $params->{ordernumber} }
216 { join => 'aqorders_transfers_ordernumber_to' }
220 =head2 Internal methods
235 return 'Koha::Acquisition::Order';