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;
12 use SL::Helper::ShippedQty;
17 use Rose::Object::MakeMethods::Generic (
18 scalar => [ qw(db_args flat_filter) ],
19 'scalar --get_set_init' => [ qw(models all_edit_right vc use_linked_items all_employees all_businesses) ],
22 __PACKAGE__->run_before(sub { $::auth->assert('delivery_plan'); });
25 reqdate => t8('Reqdate'),
26 description => t8('Description'),
27 partnumber => t8('Part Number'),
29 shipped_qty => t8('shipped'),
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->setup_list_action_bar;
45 $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems);
54 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
55 $self->{report} = $report;
57 my @columns = qw(reqdate customer vendor ordnumber partnumber description qty shipped_qty not_shipped_qty);
59 my @sortable = qw(reqdate customer vendor ordnumber partnumber description);
62 reqdate => { sub => sub { $_[0]->reqdate_as_date || $_[0]->order->reqdate_as_date } },
63 description => { sub => sub { $_[0]->description },
64 obj_link => sub { $self->link_to($_[0]->part) } },
65 partnumber => { sub => sub { $_[0]->part->partnumber },
66 obj_link => sub { $self->link_to($_[0]->part) } },
67 qty => { sub => sub { $_[0]->qty_as_number . ' ' . $_[0]->unit } },
68 shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]{shipped_qty}, 2) . ' ' . $_[0]->unit } },
69 not_shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]->qty - $_[0]{shipped_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) = @_;
108 return unless scalar @$orderitems;
110 SL::Helper::ShippedQty
111 ->new(fill_up => !$self->use_linked_items)
112 ->calculate($orderitems)
116 sub make_filter_summary {
119 my ($business, $employee);
121 my $filter = $::form->{filter} || {};
124 $business = SL::DB::Business->new(id => $filter->{order}{customer}{"business_id"})->load->description if $filter->{order}{customer}{"business_id"};
125 $employee = SL::DB::Employee->new(id => $filter->{order}{employee_id})->load->name if $filter->{order}{employee_id};
128 [ $filter->{order}{"ordnumber:substr::ilike"}, $::locale->text('Number') ],
129 [ $filter->{order}{globalproject}{"projectnumber:substr::ilike"}, $::locale->text('Document Project Number') ],
130 [ $filter->{part}{"partnumber:substr::ilike"}, $::locale->text('Part Number') ],
131 [ $filter->{"description:substr::ilike"}, $::locale->text('Part Description') ],
132 [ $filter->{"reqdate:date::ge"}, $::locale->text('Delivery Date') . " " . $::locale->text('From Date') ],
133 [ $filter->{"reqdate:date::le"}, $::locale->text('Delivery Date') . " " . $::locale->text('To Date') ],
134 [ $filter->{"qty:number"}, $::locale->text('Quantity') ],
135 [ $filter->{order}{vendor}{"name:substr::ilike"}, $::locale->text('Vendor') ],
136 [ $filter->{order}{vendor}{"vendornumber:substr::ilike"}, $::locale->text('Vendor Number') ],
137 [ $filter->{order}{customer}{"name:substr::ilike"}, $::locale->text('Customer') ],
138 [ $filter->{order}{customer}{"customernumber:substr::ilike"}, $::locale->text('Customer Number') ],
139 [ $business, $::locale->text('Customer type') ],
140 [ $employee, $::locale->text('Employee') ],
144 part => $::locale->text('Parts'),
145 service => $::locale->text('Services'),
146 assembly => $::locale->text('Assemblies'),
148 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
151 push @filter_strings, $_ if $_;
154 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
157 $self->{filter_summary} = join ', ', @filter_strings;
160 sub delivery_plan_query {
163 my $employee_id = SL::DB::Manager::Employee->current->id;
164 my $oe_owner = $_[0]->all_edit_right ? '' : " oe.employee_id = $employee_id AND";
167 "order.${vc}_id" => { gt => 0 },
169 or => [ 'order.quotation' => 0, 'order.quotation' => undef ],
171 # filter by shipped_qty < qty, read from innermost to outermost
173 -- 3. resolve the desired information about those
175 -- 2. slice only part, orderitem and both quantities from it
176 SELECT parts_id, trans_id, qty, SUM(doi_qty) AS doi_qty FROM (
177 -- 1. join orderitems and deliverorder items via record_links.
178 -- also add customer data to filter for sales_orders
179 SELECT oi.parts_id, oi.trans_id, oi.id, oi.qty, doi.qty AS doi_qty
180 FROM orderitems oi, oe, record_links rl, delivery_order_items doi
182 oe.id = oi.trans_id AND
183 oe.${vc}_id IS NOT NULL AND
184 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
187 rl.from_id = oe.id AND
188 rl.from_id = oi.trans_id AND
189 oe.id = oi.trans_id AND
190 rl.from_table = 'oe' AND
191 rl.to_table = 'delivery_orders' AND
192 rl.to_id = doi.delivery_order_id AND
193 oi.parts_id = doi.parts_id
194 ) tuples GROUP BY parts_id, trans_id, qty
196 LEFT JOIN orderitems oi ON partials.parts_id = oi.parts_id AND partials.trans_id = oi.trans_id
197 WHERE oi.qty > doi_qty
201 -- 4. since the join over record_links fails for sales_orders without any delivery order
202 -- retrieve those without record_links at all
203 SELECT oi.id FROM orderitems oi, oe
205 oe.id = oi.trans_id AND
206 oe.${vc}_id IS NOT NULL AND
207 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
214 rl.from_table ='oe' AND
215 rl.to_table = 'delivery_orders'
220 -- 5. now for the really nasty cases.
221 -- If someone partially delivered an order in several delivery orders,
222 -- there will be lots of record_links (4 doesn't catch those) but those
223 -- won't have matching part_ids in delivery_order_items, so 1-3 can't
225 -- In this case aggreg record_links - delivery_order - delivery_order_items
226 -- slice only oe.id, parts_id and sum of of qty
227 -- left join that onto orderitems to get matching qtys in doi while retaining
228 -- entrys without matches and then throw out those without record_links
229 -- TODO: join this and 1-3 into a general case
230 -- need debug info? uncomment these:
231 SELECT oi.id -- ,oi.trans_id, oi.parts_id, coalesce(sum, 0), agg.parts_id
232 FROM orderitems oi LEFT JOIN (
233 SELECT rl.from_id as oid, doi.parts_id, sum(doi.qty) FROM (
234 SELECT from_id, to_id
236 LEFT JOIN oe ON oe.id = from_id
238 rl.from_table = 'oe' AND
239 rl.to_table = 'delivery_orders' AND
241 oe.${vc}_id IS NOT NULL AND
243 (oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed
245 LEFT JOIN delivery_order_items doi ON (rl.to_id = doi.delivery_order_id)
246 GROUP BY rl.from_id, doi.parts_id
247 ) agg ON (agg.oid = oi.trans_id AND agg.parts_id = oi.parts_id)
248 LEFT JOIN oe ON oe.id = oi.trans_id
253 WHERE oi.trans_id = rl.from_id AND rl.from_table = 'oe' AND rl.to_table = 'delivery_orders'
255 coalesce(sum, 0) < oi.qty AND
256 oe.${vc}_id IS NOT NULL AND
258 (oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed
259 " ], # make emacs happy again: '
263 sub delivery_plan_query_linked_items {
266 my $employee_id = SL::DB::Manager::Employee->current->id;
267 my $oe_owner = $_[0]->all_edit_right ? '' : " oe.employee_id = $employee_id AND";
270 "order.${vc}_id" => { gt => 0 },
272 or => [ 'order.quotation' => 0, 'order.quotation' => undef ],
274 # filter by shipped_qty < qty, read from innermost to outermost
277 SELECT oi.qty, oi.id, SUM(doi.qty) AS doi_qty
278 FROM orderitems oi, oe, record_links rl, delivery_order_items doi
280 oe.id = oi.trans_id AND
281 oe.${vc}_id IS NOT NULL AND
282 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
285 doi.id = rl.to_id AND
286 rl.from_table = 'orderitems'AND
287 rl.to_table = 'delivery_order_items' AND
295 -- 2. since the join over record_links fails for items not in any delivery order
296 -- retrieve those without record_links at all
297 SELECT oi.id FROM orderitems oi, oe
299 oe.id = oi.trans_id AND
300 oe.${vc}_id IS NOT NULL AND
301 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
308 rl.from_table ='orderitems' AND
309 rl.to_table = 'delivery_order_items'
312 " ], # make emacs happy again: " ]
320 my $query = $self->use_linked_items ? $self->delivery_plan_query_linked_items
321 : $self->delivery_plan_query;
323 SL::Controller::Helper::GetModels->new(
325 model => 'OrderItem',
334 with_objects => [ 'order', "order.$vc", 'part' ],
335 additional_url_params => { vc => $vc, use_linked_items => $self->use_linked_items },
339 sub init_all_edit_right {
340 $::auth->assert('sales_all_edit', 1)
343 return $::form->{vc} if ($::form->{vc} eq 'customer' || $::form->{vc} eq 'vendor') || croak "self (DeliveryPlan) has no vc defined";
346 sub init_use_linked_items {
347 !!$::form->{use_linked_items};
350 sub init_all_employees {
351 return SL::DB::Manager::Employee->get_all_sorted;
353 sub init_all_businesses {
354 return SL::DB::Manager::Business->get_all_sorted;
357 my ($self, $object, %params) = @_;
359 return unless $object;
360 my $action = $params{action} || 'edit';
362 if ($object->isa('SL::DB::Order')) {
363 my $type = $object->type;
364 my $vc = $object->is_sales ? 'customer' : 'vendor';
365 my $id = $object->id;
367 return "oe.pl?action=$action&type=$type&vc=$vc&id=$id";
369 if ($object->isa('SL::DB::Part')) {
370 my $id = $object->id;
371 return "controller.pl?action=Part/$action&part.id=$id";
373 if ($object->isa('SL::DB::Customer')) {
374 my $id = $object->id;
375 return "controller.pl?action=CustomerVendor/$action&id=$id&db=customer";
379 sub setup_list_action_bar {
380 my ($self, %params) = @_;
382 for my $bar ($::request->layout->get('actionbar')) {
386 submit => [ '#filter_form', { action => 'DeliveryPlan/list' } ],
387 accesskey => 'enter',