1 package SL::Controller::DeliveryPlan;
4 use parent qw(SL::Controller::Base);
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::ReportGenerator;
11 use SL::Locale::String;
16 use Rose::Object::MakeMethods::Generic (
17 scalar => [ qw(db_args flat_filter) ],
18 'scalar --get_set_init' => [ qw(models all_edit_right vc use_linked_items all_employees all_businesses) ],
21 __PACKAGE__->run_before(sub { $::auth->assert('delivery_plan'); });
24 reqdate => t8('Reqdate'),
25 description => t8('Description'),
26 partnumber => t8('Part Number'),
28 shipped_qty => t8('shipped'),
29 delivered_qty => t8('transferred in / out'),
30 not_shipped_qty => t8('not shipped'),
31 ordnumber => t8('Order'),
32 customer => t8('Customer'),
33 vendor => t8('Vendor'),
39 $self->make_filter_summary;
40 $self->prepare_report;
42 my $orderitems = $self->models->get;
43 $self->calc_qtys($orderitems);
44 $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems);
53 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
54 $self->{report} = $report;
56 my @columns = qw(reqdate customer vendor ordnumber partnumber description qty shipped_qty not_shipped_qty delivered_qty);
58 my @sortable = qw(reqdate customer vendor ordnumber partnumber description);
61 reqdate => { sub => sub { $_[0]->reqdate_as_date || $_[0]->order->reqdate_as_date } },
62 description => { sub => sub { $_[0]->description },
63 obj_link => sub { $self->link_to($_[0]->part) } },
64 partnumber => { sub => sub { $_[0]->part->partnumber },
65 obj_link => sub { $self->link_to($_[0]->part) } },
66 qty => { sub => sub { $_[0]->qty_as_number . ' ' . $_[0]->unit } },
67 shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]{shipped_qty}, 2) . ' ' . $_[0]->unit } },
68 not_shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]->qty - $_[0]{shipped_qty}, 2) . ' ' . $_[0]->unit } },
69 delivered_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]{delivered_qty}, 2) .' ' . $_[0]->unit } },
70 ordnumber => { sub => sub { $_[0]->order->ordnumber },
71 obj_link => sub { $self->link_to($_[0]->order) } },
72 vendor => { sub => sub { $_[0]->order->vendor->name },
73 visible => $vc eq 'vendor',
74 obj_link => sub { $self->link_to($_[0]->order->vendor) } },
75 customer => { sub => sub { $_[0]->order->customer->name },
76 visible => $vc eq 'customer',
77 obj_link => sub { $self->link_to($_[0]->order->customer) } },
80 $column_defs{$_}->{text} = $sort_columns{$_} for keys %column_defs;
83 std_column_visibility => 1,
84 controller_class => 'DeliveryPlan',
85 output_format => 'HTML',
86 top_info_text => ($vc eq 'customer') ? $::locale->text('Delivery Plan for currently outstanding sales orders') :
87 $::locale->text('Delivery Plan for currently outstanding purchase orders'),
88 title => $::locale->text('Delivery Plan'),
89 allow_pdf_export => 1,
90 allow_csv_export => 1,
92 $report->set_columns(%column_defs);
93 $report->set_column_order(@columns);
94 $report->set_export_options(qw(list filter vc use_linked_items));
95 $report->set_options_from_form;
96 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
97 $self->models->finalize; # for filter laundering
98 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
100 raw_top_info_text => $self->render('delivery_plan/report_top', { output => 0 }),
101 raw_bottom_info_text => $self->render('delivery_plan/report_bottom', { output => 0 }, models => $self->models),
106 my ($self, $orderitems) = @_;
107 # using $orderitem->shipped_qty 40 times is far too slow. need to do it manually
110 return unless scalar @$orderitems;
112 my %orderitems_by_id = map { $_->id => $_ } @$orderitems;
114 my $query = $self->use_linked_items ? _calc_qtys_query_linked_items(scalar @$orderitems)
115 : _calc_qtys_query_match_parts(scalar @$orderitems);
117 my $result = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $query, map { $_->id } @$orderitems);
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};
128 sub _calc_qtys_query_match_parts {
129 my ($num_items) = @_;
132 SELECT oi.id, doi.qty, doi.unit, doe.delivered
134 INNER JOIN delivery_order_items doi ON (doi.delivery_order_id = rl.to_id)
135 INNER JOIN delivery_orders doe ON (doe.id = rl.to_id)
136 INNER JOIN orderitems oi ON (oi.trans_id = rl.from_id)
137 WHERE rl.from_table = 'oe'
138 AND rl.to_table = 'delivery_orders'
139 AND oi.parts_id = doi.parts_id
140 AND oi.id IN (@{[ join ', ', ("?")x $num_items ]})
146 sub _calc_qtys_query_linked_items {
147 my ($num_items) = @_;
150 SELECT rl.from_id as id, doi.qty, doi.unit, doe.delivered
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 ]})
162 sub make_filter_summary {
165 my ($business, $employee);
167 my $filter = $::form->{filter} || {};
170 $business = SL::DB::Business->new(id => $filter->{order}{customer}{"business_id"})->load->description if $filter->{order}{customer}{"business_id"};
171 $employee = SL::DB::Employee->new(id => $filter->{order}{employee_id})->load->name if $filter->{order}{employee_id};
174 [ $filter->{order}{"ordnumber:substr::ilike"}, $::locale->text('Number') ],
175 [ $filter->{order}{globalproject}{"projectnumber:substr::ilike"}, $::locale->text('Document Project Number') ],
176 [ $filter->{part}{"partnumber:substr::ilike"}, $::locale->text('Part Number') ],
177 [ $filter->{"description:substr::ilike"}, $::locale->text('Part Description') ],
178 [ $filter->{"reqdate:date::ge"}, $::locale->text('Delivery Date') . " " . $::locale->text('From Date') ],
179 [ $filter->{"reqdate:date::le"}, $::locale->text('Delivery Date') . " " . $::locale->text('To Date') ],
180 [ $filter->{"qty:number"}, $::locale->text('Quantity') ],
181 [ $filter->{order}{vendor}{"name:substr::ilike"}, $::locale->text('Vendor') ],
182 [ $filter->{order}{vendor}{"vendornumber:substr::ilike"}, $::locale->text('Vendor Number') ],
183 [ $filter->{order}{customer}{"name:substr::ilike"}, $::locale->text('Customer') ],
184 [ $filter->{order}{customer}{"customernumber:substr::ilike"}, $::locale->text('Customer Number') ],
185 [ $business, $::locale->text('Customer type') ],
186 [ $employee, $::locale->text('Employee') ],
190 part => $::locale->text('Parts'),
191 service => $::locale->text('Services'),
192 assembly => $::locale->text('Assemblies'),
194 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
197 push @filter_strings, $_ if $_;
200 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
203 $self->{filter_summary} = join ', ', @filter_strings;
206 sub delivery_plan_query {
209 my $employee_id = SL::DB::Manager::Employee->current->id;
210 my $oe_owner = $_[0]->all_edit_right ? '' : " oe.employee_id = $employee_id AND";
213 "order.${vc}_id" => { gt => 0 },
215 or => [ 'order.quotation' => 0, 'order.quotation' => undef ],
217 # filter by shipped_qty < qty, read from innermost to outermost
219 -- 3. resolve the desired information about those
221 -- 2. slice only part, orderitem and both quantities from it
222 SELECT parts_id, trans_id, qty, SUM(doi_qty) AS doi_qty FROM (
223 -- 1. join orderitems and deliverorder items via record_links.
224 -- also add customer data to filter for sales_orders
225 SELECT oi.parts_id, oi.trans_id, oi.id, oi.qty, doi.qty AS doi_qty
226 FROM orderitems oi, oe, record_links rl, delivery_order_items doi
228 oe.id = oi.trans_id AND
229 oe.${vc}_id IS NOT NULL AND
230 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
233 rl.from_id = oe.id AND
234 rl.from_id = oi.trans_id AND
235 oe.id = oi.trans_id AND
236 rl.from_table = 'oe' AND
237 rl.to_table = 'delivery_orders' AND
238 rl.to_id = doi.delivery_order_id AND
239 oi.parts_id = doi.parts_id
240 ) tuples GROUP BY parts_id, trans_id, qty
242 LEFT JOIN orderitems oi ON partials.parts_id = oi.parts_id AND partials.trans_id = oi.trans_id
243 WHERE oi.qty > doi_qty
247 -- 4. since the join over record_links fails for sales_orders without any delivery order
248 -- retrieve those without record_links at all
249 SELECT oi.id FROM orderitems oi, oe
251 oe.id = oi.trans_id AND
252 oe.${vc}_id IS NOT NULL AND
253 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
260 rl.from_table ='oe' AND
261 rl.to_table = 'delivery_orders'
266 -- 5. now for the really nasty cases.
267 -- If someone partially delivered an order in several delivery orders,
268 -- there will be lots of record_links (4 doesn't catch those) but those
269 -- won't have matching part_ids in delivery_order_items, so 1-3 can't
271 -- In this case aggreg record_links - delivery_order - delivery_order_items
272 -- slice only oe.id, parts_id and sum of of qty
273 -- left join that onto orderitems to get matching qtys in doi while retaining
274 -- entrys without matches and then throw out those without record_links
275 -- TODO: join this and 1-3 into a general case
276 -- need debug info? uncomment these:
277 SELECT oi.id -- ,oi.trans_id, oi.parts_id, coalesce(sum, 0), agg.parts_id
278 FROM orderitems oi LEFT JOIN (
279 SELECT rl.from_id as oid, doi.parts_id, sum(doi.qty) FROM (
280 SELECT from_id, to_id
282 LEFT JOIN oe ON oe.id = from_id
284 rl.from_table = 'oe' AND
285 rl.to_table = 'delivery_orders' AND
287 oe.${vc}_id IS NOT NULL AND
289 (oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed
291 LEFT JOIN delivery_order_items doi ON (rl.to_id = doi.delivery_order_id)
292 GROUP BY rl.from_id, doi.parts_id
293 ) agg ON (agg.oid = oi.trans_id AND agg.parts_id = oi.parts_id)
294 LEFT JOIN oe ON oe.id = oi.trans_id
299 WHERE oi.trans_id = rl.from_id AND rl.from_table = 'oe' AND rl.to_table = 'delivery_orders'
301 coalesce(sum, 0) < oi.qty AND
302 oe.${vc}_id IS NOT NULL AND
304 (oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed
305 " ], # make emacs happy again: '
309 sub delivery_plan_query_linked_items {
312 my $employee_id = SL::DB::Manager::Employee->current->id;
313 my $oe_owner = $_[0]->all_edit_right ? '' : " oe.employee_id = $employee_id AND";
316 "order.${vc}_id" => { gt => 0 },
318 or => [ 'order.quotation' => 0, 'order.quotation' => undef ],
320 # filter by shipped_qty < qty, read from innermost to outermost
323 SELECT oi.qty, oi.id, SUM(doi.qty) AS doi_qty
324 FROM orderitems oi, oe, record_links rl, delivery_order_items doi
326 oe.id = oi.trans_id AND
327 oe.${vc}_id IS NOT NULL AND
328 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
331 doi.id = rl.to_id AND
332 rl.from_table = 'orderitems'AND
333 rl.to_table = 'delivery_order_items' AND
341 -- 2. since the join over record_links fails for items not in any delivery order
342 -- retrieve those without record_links at all
343 SELECT oi.id FROM orderitems oi, oe
345 oe.id = oi.trans_id AND
346 oe.${vc}_id IS NOT NULL AND
347 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
354 rl.from_table ='orderitems' AND
355 rl.to_table = 'delivery_order_items'
358 " ], # make emacs happy again: " ]
366 my $query = $self->use_linked_items ? $self->delivery_plan_query_linked_items
367 : $self->delivery_plan_query;
369 SL::Controller::Helper::GetModels->new(
371 model => 'OrderItem',
380 with_objects => [ 'order', "order.$vc", 'part' ],
381 additional_url_params => { vc => $vc, use_linked_items => $self->use_linked_items },
385 sub init_all_edit_right {
386 $::auth->assert('sales_all_edit', 1)
389 return $::form->{vc} if ($::form->{vc} eq 'customer' || $::form->{vc} eq 'vendor') || croak "self (DeliveryPlan) has no vc defined";
392 sub init_use_linked_items {
393 !!$::form->{use_linked_items};
396 sub init_all_employees {
397 return SL::DB::Manager::Employee->get_all_sorted;
399 sub init_all_businesses {
400 return SL::DB::Manager::Business->get_all_sorted;
403 my ($self, $object, %params) = @_;
405 return unless $object;
406 my $action = $params{action} || 'edit';
408 if ($object->isa('SL::DB::Order')) {
409 my $type = $object->type;
410 my $vc = $object->is_sales ? 'customer' : 'vendor';
411 my $id = $object->id;
413 return "oe.pl?action=$action&type=$type&vc=$vc&id=$id";
415 if ($object->isa('SL::DB::Part')) {
416 my $id = $object->id;
417 return "ic.pl?action=$action&id=$id";
419 if ($object->isa('SL::DB::Customer')) {
420 my $id = $object->id;
421 return "controller.pl?action=CustomerVendor/$action&id=$id&db=customer";