1 package SL::Controller::DeliveryPlan;
4 use parent qw(SL::Controller::Base);
8 use SL::Controller::Helper::GetModels;
9 use SL::Controller::Helper::ReportGenerator;
10 use SL::Locale::String;
12 use Rose::Object::MakeMethods::Generic (
13 scalar => [ qw(db_args flat_filter) ],
14 'scalar --get_set_init' => [ qw(models all_edit_right) ],
17 __PACKAGE__->run_before(sub { $::auth->assert('delivery_plan'); });
20 reqdate => t8('Reqdate'),
21 description => t8('Description'),
22 partnumber => t8('Part Number'),
24 shipped_qty => t8('shipped'),
25 not_shipped_qty => t8('not shipped'),
26 ordnumber => t8('Order'),
27 customer => t8('Customer'),
33 $self->make_filter_summary;
34 $self->prepare_report;
36 my $orderitems = $self->models->get;
38 $self->report_generator_list_objects(report => $self->{report}, objects => $orderitems);
46 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
47 $self->{report} = $report;
49 my @columns = qw(reqdate customer ordnumber partnumber description qty shipped_qty not_shipped_qty);
50 my @sortable = qw(reqdate customer ordnumber partnumber description);
53 reqdate => { sub => sub { $_[0]->reqdate_as_date || $_[0]->order->reqdate_as_date } },
54 description => { sub => sub { $_[0]->description },
55 obj_link => sub { $self->link_to($_[0]->part) } },
56 partnumber => { sub => sub { $_[0]->part->partnumber },
57 obj_link => sub { $self->link_to($_[0]->part) } },
58 qty => { sub => sub { $_[0]->qty_as_number . ' ' . $_[0]->unit } },
59 shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]->shipped_qty, 2) . ' ' . $_[0]->unit } },
60 not_shipped_qty => { sub => sub { $::form->format_amount(\%::myconfig, $_[0]->qty - $_[0]->shipped_qty, 2) . ' ' . $_[0]->unit } },
61 ordnumber => { sub => sub { $_[0]->order->ordnumber },
62 obj_link => sub { $self->link_to($_[0]->order) } },
63 customer => { sub => sub { $_[0]->order->customer->name },
64 obj_link => sub { $self->link_to($_[0]->order->customer) } },
67 $column_defs{$_}->{text} = $sort_columns{$_} for keys %column_defs;
70 std_column_visibility => 1,
71 controller_class => 'DeliveryPlan',
72 output_format => 'HTML',
73 top_info_text => $::locale->text('Delivery Plan for currently outstanding sales orders'),
74 title => $::locale->text('Delivery Plan'),
75 allow_pdf_export => 1,
76 allow_csv_export => 1,
78 $report->set_columns(%column_defs);
79 $report->set_column_order(@columns);
80 $report->set_export_options(qw(list filter));
81 $report->set_options_from_form;
82 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
83 $self->models->finalize; # for filter laundering
84 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
86 raw_top_info_text => $self->render('delivery_plan/report_top', { output => 0 }),
87 raw_bottom_info_text => $self->render('delivery_plan/report_bottom', { output => 0 }, models => $self->models),
91 sub make_filter_summary {
94 my $filter = $::form->{filter} || {};
98 [ $filter->{order}{"ordnumber:substr::ilike"}, $::locale->text('Number') ],
99 [ $filter->{part}{"partnumber:substr::ilike"}, $::locale->text('Part Number') ],
100 [ $filter->{"description:substr::ilike"}, $::locale->text('Part Description') ],
101 [ $filter->{"reqdate:date::ge"}, $::locale->text('Delivery Date') . " " . $::locale->text('From Date') ],
102 [ $filter->{"reqdate:date::le"}, $::locale->text('Delivery Date') . " " . $::locale->text('To Date') ],
103 [ $filter->{"qty:number"}, $::locale->text('Quantity') ],
104 [ $filter->{order}{customer}{"name:substr::ilike"}, $::locale->text('Customer') ],
105 [ $filter->{order}{customer}{"customernumber:substr::ilike"}, $::locale->text('Customer Number') ],
109 part => $::locale->text('Parts'),
110 service => $::locale->text('Services'),
111 assembly => $::locale->text('Assemblies'),
113 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
116 push @filter_strings, $_ if $_;
119 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
122 $self->{filter_summary} = join ', ', @filter_strings;
125 sub delivery_plan_query {
126 my $employee_id = SL::DB::Manager::Employee->current->id;
127 my $oe_owner = $_[0]->all_edit_right ? '' : " oe.eployee_id = $employee_id AND";
129 'order.customer_id' => { gt => 0 },
131 or => [ 'order.quotation' => 0, 'order.quotation' => undef ],
133 # filter by shipped_qty < qty, read from innermost to outermost
135 -- 3. resolve the desired information about those
137 -- 2. slice only part, orderitem and both quantities from it
138 SELECT parts_id, trans_id, qty, SUM(doi_qty) AS doi_qty FROM (
139 -- 1. join orderitems and deliverorder items via record_links.
140 -- also add customer data to filter for sales_orders
141 SELECT oi.parts_id, oi.trans_id, oi.id, oi.qty, doi.qty AS doi_qty
142 FROM orderitems oi, oe, record_links rl, delivery_order_items doi
144 oe.id = oi.trans_id AND
145 oe.customer_id IS NOT NULL AND
146 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
149 rl.from_id = oe.id AND
150 rl.from_id = oi.trans_id AND
151 oe.id = oi.trans_id AND
152 rl.from_table = 'oe' AND
153 rl.to_table = 'delivery_orders' AND
154 rl.to_id = doi.delivery_order_id AND
155 oi.parts_id = doi.parts_id
156 ) tuples GROUP BY parts_id, trans_id, qty
158 LEFT JOIN orderitems oi ON partials.parts_id = oi.parts_id AND partials.trans_id = oi.trans_id
159 WHERE oi.qty > doi_qty
163 -- 4. since the join over record_links fails for sales_orders wihtout any delivery order
164 -- retrieve those without record_links at all
165 SELECT oi.id FROM orderitems oi, oe
167 oe.id = oi.trans_id AND
168 oe.customer_id IS NOT NULL AND
169 (oe.quotation = 'f' OR oe.quotation IS NULL) AND
176 rl.from_table ='oe' AND
177 rl.to_table = 'delivery_orders'
182 -- 5. now for the really nasty cases.
183 -- If someone partially delivered an order in several delivery orders,
184 -- there will be lots of record_links (4 doesn't catch those) but those
185 -- won't have matching part_ids in delivery_order_items, so 1-3 can't
187 -- In this case aggreg record_links - delivery_order - delivery_order_items
188 -- slice only oe.id, parts_id and sum of of qty
189 -- left join that onto orderitems to get matching qtys in doi while retaining
190 -- entrys without matches and then throw out those without record_links
191 -- TODO: join this and 1-3 into a general case
192 -- need debug info? uncomment these:
193 SELECT oi.id -- ,oi.trans_id, oi.parts_id, coalesce(sum, 0), agg.parts_id
194 FROM orderitems oi LEFT JOIN (
195 SELECT rl.from_id as oid, doi.parts_id, sum(doi.qty) FROM (
196 SELECT from_id, to_id
198 LEFT JOIN oe ON oe.id = from_id
200 rl.from_table = 'oe' AND
201 rl.to_table = 'delivery_orders' AND
203 oe.customer_id IS NOT NULL AND
205 (oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed
207 LEFT JOIN delivery_order_items doi ON (rl.to_id = doi.delivery_order_id)
208 GROUP BY rl.from_id, doi.parts_id
209 ) agg ON (agg.oid = oi.trans_id AND agg.parts_id = oi.parts_id)
210 LEFT JOIN oe ON oe.id = oi.trans_id
215 WHERE oi.trans_id = rl.from_id AND rl.from_table = 'oe' AND rl.to_table = 'delivery_orders'
217 coalesce(sum, 0) < oi.qty AND
218 oe.customer_id IS NOT NULL AND
220 (oe.quotation = 'f' OR oe.quotation IS NULL) AND NOT oe.closed
228 SL::Controller::Helper::GetModels->new(
230 model => 'OrderItem',
238 query => $self->delivery_plan_query,
239 with_objects => [ 'order', 'order.customer', 'part' ],
243 sub init_all_edit_right {
244 $::auth->assert('sales_all_edit', 1)
248 my ($self, $object, %params) = @_;
250 return unless $object;
251 my $action = $params{action} || 'edit';
253 if ($object->isa('SL::DB::Order')) {
254 my $type = $object->type;
255 my $vc = $object->is_sales ? 'customer' : 'vendor';
256 my $id = $object->id;
258 return "oe.pl?action=$action&type=$type&vc=$vc&id=$id";
260 if ($object->isa('SL::DB::Part')) {
261 my $id = $object->id;
262 return "ic.pl?action=$action&id=$id";
264 if ($object->isa('SL::DB::Customer')) {
265 my $id = $object->id;
266 return "controller.pl?action=CustomerVendor/$action&id=$id&db=customer";