Revision 9e7d8938
Von Bernd Bleßmann vor fast 9 Jahren hinzugefügt
SL/Controller/DeliveryPlan.pm | ||
---|---|---|
15 | 15 |
|
16 | 16 |
use Rose::Object::MakeMethods::Generic ( |
17 | 17 |
scalar => [ qw(db_args flat_filter) ], |
18 |
'scalar --get_set_init' => [ qw(models all_edit_right vc all_employees all_businesses) ], |
|
18 |
'scalar --get_set_init' => [ qw(models all_edit_right vc use_linked_items all_employees all_businesses) ],
|
|
19 | 19 |
); |
20 | 20 |
|
21 | 21 |
__PACKAGE__->run_before(sub { $::auth->assert('delivery_plan'); }); |
... | ... | |
91 | 91 |
); |
92 | 92 |
$report->set_columns(%column_defs); |
93 | 93 |
$report->set_column_order(@columns); |
94 |
$report->set_export_options(qw(list filter vc)); |
|
94 |
$report->set_export_options(qw(list filter vc use_linked_items));
|
|
95 | 95 |
$report->set_options_from_form; |
96 | 96 |
$self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i; |
97 | 97 |
$self->models->finalize; # for filter laundering |
... | ... | |
111 | 111 |
|
112 | 112 |
my %orderitems_by_id = map { $_->id => $_ } @$orderitems; |
113 | 113 |
|
114 |
my $query = $self->use_linked_items ? _calc_qtys_query_linked_items(scalar @$orderitems) |
|
115 |
: _calc_qtys_query_match_parts(scalar @$orderitems); |
|
116 |
|
|
117 |
my $result = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $query, map { $_->id } @$orderitems); |
|
118 |
|
|
119 |
for my $row (@$result) { |
|
120 |
my $item = $orderitems_by_id{ $row->{id} }; |
|
121 |
$item->{shipped_qty} ||= 0; |
|
122 |
$item->{delivered_qty} ||= 0; |
|
123 |
$item->{shipped_qty} += AM->convert_unit($row->{unit} => $item->unit) * $row->{qty}; |
|
124 |
$item->{delivered_qty} += AM->convert_unit($row->{unit} => $item->unit) * $row->{qty} if $row->{delivered}; |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
sub _calc_qtys_query_match_parts { |
|
129 |
my ($num_items) = @_; |
|
130 |
|
|
114 | 131 |
my $query = <<SQL; |
115 | 132 |
SELECT oi.id, doi.qty, doi.unit, doe.delivered |
116 | 133 |
FROM record_links rl |
... | ... | |
120 | 137 |
WHERE rl.from_table = 'oe' |
121 | 138 |
AND rl.to_table = 'delivery_orders' |
122 | 139 |
AND oi.parts_id = doi.parts_id |
123 |
AND oi.id IN (@{[ join ', ', ("?")x @$orderitems ]})
|
|
140 |
AND oi.id IN (@{[ join ', ', ("?")x $num_items ]})
|
|
124 | 141 |
SQL |
125 | 142 |
|
126 |
my $result = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $query, map { $_->id } @$orderitems); |
|
143 |
return $query; |
|
144 |
} |
|
127 | 145 |
|
128 |
for my $row (@$result) { |
|
129 |
my $item = $orderitems_by_id{ $row->{id} }; |
|
130 |
$item->{shipped_qty} ||= 0; |
|
131 |
$item->{delivered_qty} ||= 0; |
|
132 |
$item->{shipped_qty} += AM->convert_unit($row->{unit} => $item->unit) * $row->{qty}; |
|
133 |
$item->{delivered_qty} += AM->convert_unit($row->{unit} => $item->unit) * $row->{qty} if $row->{delivered}; |
|
134 |
} |
|
146 |
sub _calc_qtys_query_linked_items { |
|
147 |
my ($num_items) = @_; |
|
148 |
|
|
149 |
my $query = <<SQL; |
|
150 |
SELECT rl.from_id as id, doi.qty, doi.unit, doe.delivered |
|
151 |
FROM record_links rl |
|
152 |
INNER JOIN delivery_order_items doi ON (doi.id = rl.to_id) |
|
153 |
INNER JOIN delivery_orders doe ON (doe.id = doi.delivery_order_id) |
|
154 |
WHERE rl.from_table LIKE 'orderitems' |
|
155 |
AND rl.to_table LIKE 'delivery_order_items' |
|
156 |
AND rl.from_id IN (@{[ join ', ', ("?")x $num_items ]}) |
|
157 |
SQL |
|
158 |
|
|
159 |
return $query; |
|
135 | 160 |
} |
136 | 161 |
|
137 | 162 |
sub make_filter_summary { |
... | ... | |
282 | 307 |
oe.${vc}_id IS NOT NULL AND |
283 | 308 |
$oe_owner |
284 | 309 |
(oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed |
285 |
" ], |
|
310 |
" ], # make emacs happy again: ' |
|
311 |
] |
|
312 |
} |
|
313 |
|
|
314 |
sub delivery_plan_query_linked_items { |
|
315 |
my ($self) = @_; |
|
316 |
my $vc = $self->vc; |
|
317 |
my $employee_id = SL::DB::Manager::Employee->current->id; |
|
318 |
my $oe_owner = $_[0]->all_edit_right ? '' : " oe.employee_id = $employee_id AND"; |
|
319 |
# check delivered state for delivery_orders (transferred out) if enabled |
|
320 |
my $filter_delivered = ($::instance_conf->get_delivery_plan_calculate_transferred_do) ? |
|
321 |
"AND (SELECT delivered from delivery_orders where id = doi.delivery_order_id)" : ''; |
|
322 |
|
|
323 |
[ |
|
324 |
"order.${vc}_id" => { gt => 0 }, |
|
325 |
'order.closed' => 0, |
|
326 |
or => [ 'order.quotation' => 0, 'order.quotation' => undef ], |
|
327 |
|
|
328 |
# filter by shipped_qty < qty, read from innermost to outermost |
|
329 |
'id' => [ \" |
|
330 |
SELECT id FROM ( |
|
331 |
SELECT oi.qty, oi.id, SUM(doi.qty) AS doi_qty |
|
332 |
FROM orderitems oi, oe, record_links rl, delivery_order_items doi |
|
333 |
WHERE |
|
334 |
oe.id = oi.trans_id AND |
|
335 |
oe.${vc}_id IS NOT NULL AND |
|
336 |
(oe.quotation = 'f' OR oe.quotation IS NULL) AND |
|
337 |
NOT oe.closed AND |
|
338 |
$oe_owner |
|
339 |
doi.id = rl.to_id AND |
|
340 |
rl.from_table = 'orderitems'AND |
|
341 |
rl.to_table = 'delivery_order_items' AND |
|
342 |
rl.from_id = oi.id |
|
343 |
$filter_delivered |
|
344 |
GROUP BY oi.id |
|
345 |
) linked |
|
346 |
WHERE qty > doi_qty |
|
347 |
|
|
348 |
UNION ALL |
|
349 |
|
|
350 |
-- 2. since the join over record_links fails for items not in any delivery order |
|
351 |
-- retrieve those without record_links at all |
|
352 |
SELECT oi.id FROM orderitems oi, oe |
|
353 |
WHERE |
|
354 |
oe.id = oi.trans_id AND |
|
355 |
oe.${vc}_id IS NOT NULL AND |
|
356 |
(oe.quotation = 'f' OR oe.quotation IS NULL) AND |
|
357 |
NOT oe.closed AND |
|
358 |
$oe_owner |
|
359 |
oi.id NOT IN ( |
|
360 |
SELECT from_id |
|
361 |
FROM record_links rl |
|
362 |
WHERE |
|
363 |
rl.from_table ='orderitems' AND |
|
364 |
rl.to_table = 'delivery_order_items' |
|
365 |
) |
|
366 |
|
|
367 |
" ], # make emacs happy again: " ] |
|
286 | 368 |
] |
287 | 369 |
} |
288 | 370 |
|
... | ... | |
290 | 372 |
my ($self) = @_; |
291 | 373 |
my $vc = $self->vc; |
292 | 374 |
|
375 |
my $query = $self->use_linked_items ? $self->delivery_plan_query_linked_items |
|
376 |
: $self->delivery_plan_query; |
|
377 |
|
|
293 | 378 |
SL::Controller::Helper::GetModels->new( |
294 | 379 |
controller => $self, |
295 | 380 |
model => 'OrderItem', |
... | ... | |
300 | 385 |
}, |
301 | 386 |
%sort_columns, |
302 | 387 |
}, |
303 |
query => $self->delivery_plan_query,
|
|
388 |
query => $query, |
|
304 | 389 |
with_objects => [ 'order', "order.$vc", 'part' ], |
305 |
additional_url_params => { vc => $vc}, |
|
390 |
additional_url_params => { vc => $vc, use_linked_items => $self->use_linked_items },
|
|
306 | 391 |
); |
307 | 392 |
} |
308 | 393 |
|
... | ... | |
313 | 398 |
return $::form->{vc} if ($::form->{vc} eq 'customer' || $::form->{vc} eq 'vendor') || croak "self (DeliveryPlan) has no vc defined"; |
314 | 399 |
} |
315 | 400 |
|
401 |
sub init_use_linked_items { |
|
402 |
!!$::form->{use_linked_items}; |
|
403 |
} |
|
404 |
|
|
316 | 405 |
sub init_all_employees { |
317 | 406 |
return SL::DB::Manager::Employee->get_all_sorted; |
318 | 407 |
} |
Auch abrufbar als: Unified diff
DeliveryPlan: optional auch mit verlinkten Positionen suchen.