Bug 29503: Make GET /patrons use Koha::Patrons->search_limited
[koha.git] / 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
21 use Koha::Database;
22
23 use Koha::DateUtils qw( dt_from_string );
24 use Koha::Acquisition::Order;
25 use Koha::Exceptions::Exception;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Acquisition::Orders object set class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 filter_by_lates
38
39 my $late_orders = $orders->filter_by_lates($params);
40
41 Filter an order set given different parameters.
42
43 This is the equivalent method of the former GetLateOrders C4 subroutine
44
45 $params can be:
46
47 =over
48
49 =item C<delay> the number of days the basket has been closed
50
51 =item C<bookseller_id> the bookseller id
52
53 =item C<estimated_from> Beginning of the estimated delivery date
54
55 =item C<estimated_to> End of the estimated delivery date
56
57 =back
58
59 =cut
60
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;
69
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" ];
74     }
75     if ( defined $estimated_from ) {
76         push @delivery_time_conditions, \[ "$date_add >= ?", $dtf->format_date($estimated_from) ];
77     }
78     if ( defined $estimated_to ) {
79         push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date($estimated_to) ];
80     }
81     if ( defined $estimated_from and not defined $estimated_to ) {
82         push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date(dt_from_string) ];
83     }
84
85     $self->search(
86         {
87             -or => [
88                 { datereceived => undef },
89                 quantityreceived => { '<' => \'quantity' }
90             ],
91             'basketno.closedate' => [
92                 -and =>
93                 { '!=' => undef },
94                 {
95                     defined $delay
96                     ? (
97                         '<=' => $dtf->format_date(
98                             dt_from_string->subtract( days => $delay )
99                         )
100                       )
101                     : ()
102                 }
103               ],
104             'datecancellationprinted' => undef,
105             (
106                 $bookseller_id
107                 ? ( 'basketno.booksellerid' => $bookseller_id )
108                 : ()
109             ),
110
111             # ( $branchcode ? ('borrower.branchcode')) # FIXME branch is not a filter we may not need to implement this
112
113             ( @delivery_time_conditions ? ( -and => \@delivery_time_conditions ) : ()),
114             (
115                 C4::Context->preference('IndependentBranches')
116                   && !C4::Context->IsSuperLibrarian
117                 ? ( 'borrower.branchcode' => C4::Context->userenv->{branch} )
118                 : ()
119             ),
120
121             ( orderstatus => { '!=' => 'cancelled' } ),
122
123         },
124         {
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'},
129         }
130     );
131 }
132
133 =head3 filter_by_active
134
135     my $new_rs = $orders->filter_by_active;
136
137 Returns a new resultset filtering orders that are not active.
138
139 =cut
140
141 sub filter_by_active {
142     my ($self) = @_;
143     return $self->search(
144         {
145             '-or' => [
146                 { 'basket.is_standing' => 1,
147                   'orderstatus' => [ 'new', 'ordered', 'partial' ] },
148                 { 'orderstatus' => [ 'ordered', 'partial' ] }
149             ]
150         },
151         { join => 'basket' }
152     );
153 }
154
155 =head3 filter_by_current
156
157     $orders->filter_by_current
158
159 Return the orders of the set that have not been cancelled.
160
161 =cut
162
163 sub filter_by_current {
164     my ($self) = @_;
165     return $self->search(
166         {
167             datecancellationprinted => undef,
168         }
169     );
170 }
171
172 =head3 filter_by_cancelled
173
174     $orders->filter_by_cancelled
175
176 Return the orders of the set that have been cancelled.
177
178 =cut
179
180 sub filter_by_cancelled {
181     my ($self) = @_;
182     return $self->search(
183         {
184             datecancellationprinted => { '!=' => undef }
185         }
186     );
187 }
188
189 =head3 filter_by_id_including_transfers
190
191     my $orders = $orders->filter_by_id_including_transfers(
192         {
193             ordernumber => $ordernumber
194         }
195     );
196
197 When searching for orders by I<ordernumber>, include the aqorders_transfers table
198 so we can find orders that have changed their ordernumber as the result of a transfer
199
200 =cut
201
202 sub filter_by_id_including_transfers {
203     my ( $self, $params ) = @_;
204
205     Koha::Exceptions::MissingParameter->throw( "The ordernumber param is mandatory" )
206         unless $params->{ordernumber};
207
208     return $self->search(
209         {
210             -or => [
211                 { 'me.ordernumber' => $params->{ordernumber} },
212                 { 'aqorders_transfers_ordernumber_to.ordernumber_from' => $params->{ordernumber} }
213             ]
214         },
215         { join => 'aqorders_transfers_ordernumber_to' }
216     );
217 }
218
219 =head2 Internal methods
220
221 =head3 _type
222
223 =cut
224
225 sub _type {
226     return 'Aqorder';
227 }
228
229 =head3 object_class
230
231 =cut
232
233 sub object_class {
234     return 'Koha::Acquisition::Order';
235 }
236
237 1;