1 package SL::Controller::DeliveryPlan;
4 use parent qw(SL::Controller::Base);
8 use SL::Controller::Helper::GetModels;
9 use SL::Controller::Helper::Paginated;
10 use SL::Controller::Helper::Sorted;
11 use SL::Controller::Helper::Filtered;
12 use SL::Controller::Helper::ReportGenerator;
13 use SL::Locale::String;
15 use Rose::Object::MakeMethods::Generic (
16 scalar => [ qw(db_args flat_filter) ],
19 __PACKAGE__->run_before(sub { $::auth->assert('sales_order_edit'); });
21 __PACKAGE__->make_filtered(
23 LAUNDER_TO => 'filter'
25 __PACKAGE__->make_paginated(
30 __PACKAGE__->make_sorted(
34 DEFAULT_BY => 'reqdate',
37 reqdate => t8('Reqdate'),
38 description => t8('Description'),
39 partnumber => t8('Part Number'),
41 shipped_qty => t8('shipped'),
42 not_shipped_qty => t8('not shipped'),
43 ordnumber => t8('Order'),
44 customer => t8('Customer'),
47 my $delivery_plan_query = [
48 'order.customer_id' => { gt => 0 },
50 or => [ 'order.quotation' => 0, 'order.quotation' => undef ],
52 # filter by shipped_qty < qty, read from innermost to outermost
54 -- 3. resolve the desired information about those
56 -- 2. slice only part, orderitem and both quantities from it
57 SELECT parts_id, trans_id, qty, SUM(doi_qty) AS doi_qty FROM (
58 -- 1. join orderitems and deliverorder items via record_links.
59 -- also add customer data to filter for sales_orders
60 SELECT oi.parts_id, oi.trans_id, oi.id, oi.qty, doi.qty AS doi_qty
61 FROM orderitems oi, oe, record_links rl, delivery_order_items doi
63 oe.id = oi.trans_id AND
64 oe.customer_id IS NOT NULL AND
65 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
67 rl.from_id = oe.id AND
68 rl.from_id = oi.trans_id AND
69 oe.id = oi.trans_id AND
70 rl.from_table = 'oe' AND
71 rl.to_table = 'delivery_orders' AND
72 rl.to_id = doi.delivery_order_id AND
73 oi.parts_id = doi.parts_id
74 ) tuples GROUP BY parts_id, trans_id, qty
76 LEFT JOIN orderitems oi ON partials.parts_id = oi.parts_id AND partials.trans_id = oi.trans_id
77 WHERE oi.qty > doi_qty
81 -- 4. since the join over record_links fails for sales_orders wihtout any delivery order
82 -- retrieve those without record_links at all
83 SELECT oi.id FROM orderitems oi, oe
85 oe.id = oi.trans_id AND
86 oe.customer_id IS NOT NULL AND
87 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
93 rl.from_table ='oe' AND
94 rl.to_table = 'delivery_orders'
99 -- 5. In case someone deleted a line of the delivery_order there will be a record_link (4 fails)
100 -- but there won't be a delivery_order_items to find (3 fails too). Search for orphaned orderitems this way
101 SELECT oi.id FROM orderitems AS oi, oe, record_links AS rl
103 rl.from_table = 'oe' AND
104 rl.to_table = 'delivery_orders' AND
106 oi.trans_id = rl.from_id AND
108 SELECT doi.parts_id FROM delivery_order_items AS doi WHERE doi.delivery_order_id = rl.to_id
111 oe.id = oi.trans_id AND
113 oe.customer_id IS NOT NULL AND
114 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
122 $self->make_filter_summary;
124 my $orderitems = $self->get_models(query => $delivery_plan_query, with_objects => [ 'order', 'order.customer', 'part' ]);
126 $self->prepare_report;
127 $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems);
135 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
136 $self->{report} = $report;
138 my @columns = qw(reqdate customer ordnumber partnumber description qty shipped_qty not_shipped_qty);
139 my @sortable = qw(reqdate customer ordnumber partnumber description);
142 reqdate => { sub => sub { $_[0]->reqdate_as_date || $_[0]->order->reqdate_as_date } },
143 description => { sub => sub { $_[0]->description },
144 obj_link => sub { $self->link_to($_[0]->part) } },
145 partnumber => { sub => sub { $_[0]->part->partnumber },
146 obj_link => sub { $self->link_to($_[0]->part) } },
147 qty => { sub => sub { $_[0]->qty_as_number . ' ' . $_[0]->unit } },
148 shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]->shipped_qty, 2) . ' ' . $_[0]->unit } },
149 not_shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]->qty - $_[0]->shipped_qty, 2) . ' ' . $_[0]->unit } },
150 ordnumber => { sub => sub { $_[0]->order->ordnumber },
151 obj_link => sub { $self->link_to($_[0]->order) } },
152 customer => { sub => sub { $_[0]->order->customer->name },
153 obj_link => sub { $self->link_to($_[0]->order->customer) } },
156 map { $column_defs{$_}->{text} = $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
158 $report->set_options(
159 std_column_visibility => 1,
160 controller_class => 'DeliveryPlan',
161 output_format => 'HTML',
162 top_info_text => $::locale->text('Delivery Plan for currently outstanding sales orders'),
163 raw_top_info_text => $self->render('delivery_plan/report_top', { output => 0 }),
164 raw_bottom_info_text => $self->render('delivery_plan/report_bottom', { output => 0 }),
165 title => $::locale->text('Delivery Plan'),
166 allow_pdf_export => 1,
167 allow_csv_export => 1,
169 $report->set_columns(%column_defs);
170 $report->set_column_order(@columns);
171 $report->set_export_options(qw(list filter));
172 $report->set_options_from_form;
173 $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
175 $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
178 sub make_filter_summary {
181 my $filter = $::form->{filter} || {};
185 [ $filter->{order}{"ordnumber:substr::ilike"}, $::locale->text('Number') ],
186 [ $filter->{part}{"partnumber:substr::ilike"}, $::locale->text('Part Number') ],
187 [ $filter->{"description:substr::ilike"}, $::locale->text('Part Description') ],
188 [ $filter->{"reqdate:date::ge"}, $::locale->text('Delivery Date') . " " . $::locale->text('From Date') ],
189 [ $filter->{"reqdate:date::le"}, $::locale->text('Delivery Date') . " " . $::locale->text('To Date') ],
190 [ $filter->{"qty:number"}, $::locale->text('Quantity') ],
191 [ $filter->{order}{customer}{"name:substr::ilike"}, $::locale->text('Customer') ],
192 [ $filter->{order}{customer}{"customernumber:substr::ilike"}, $::locale->text('Customer Number') ],
196 part => $::locale->text('Parts'),
197 service => $::locale->text('Services'),
198 assembly => $::locale->text('Assemblies'),
200 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
203 push @filter_strings, $_ if $_;
206 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
209 $self->{filter_summary} = join ', ', @filter_strings;
213 my ($self, $object, %params) = @_;
215 return unless $object;
216 my $action = $params{action} || 'edit';
218 if ($object->isa('SL::DB::Order')) {
219 my $type = $object->type;
220 my $vc = $object->is_sales ? 'customer' : 'vendor';
221 my $id = $object->id;
223 return "oe.pl?action=$action&type=$type&vc=$vc&id=$id";
225 if ($object->isa('SL::DB::Part')) {
226 my $id = $object->id;
227 return "ic.pl?action=$action&id=$id";
229 if ($object->isa('SL::DB::Customer')) {
230 my $id = $object->id;
231 return "ct.pl?action=$action&id=$id&db=customer";