1 package SL::Controller::FinancialControllingReport;
4 use parent qw(SL::Controller::Base);
6 use List::Util qw(max min sum);
9 use SL::DB::ProjectType;
10 use SL::Controller::Helper::GetModels;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::Locale::String;
14 use Rose::Object::MakeMethods::Generic (
15 scalar => [ qw(orders) ],
16 'scalar --get_set_init' => [ qw(project_types models) ],
19 __PACKAGE__->run_before(sub { $::auth->assert('sales_order_edit'); });
22 ordnumber => t8('Order'),
23 customer => t8('Customer'),
24 transaction_description => t8('Transaction description'),
25 globalprojectnumber => t8('Project'),
26 globalproject_type => t8('Project Type'),
32 $self->make_filter_summary;
34 $self->prepare_report;
36 $self->orders($self->models->get);
38 $self->calculate_data;
40 $self->setup_list_action_bar;
49 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
50 $self->{report} = $report;
52 my @columns = qw(customer globalprojectnumber globalproject_type transaction_description ordnumber net_amount delivered_amount delivered_amount_p billed_amount billed_amount_p paid_amount paid_amount_p
53 billable_amount billable_amount_p other_amount);
54 my @sortable = qw(ordnumber transdate customer globalprojectnumber globalproject_type transaction_description );
55 $self->{number_columns} = [ qw(net_amount billed_amount billed_amount_p delivered_amount delivered_amount_p paid_amount paid_amount_p other_amount billable_amount billable_amount_p) ];
58 net_amount => { text => $::locale->text('Order amount') },
59 billed_amount => { text => $::locale->text('Billed amount') },
60 billed_amount_p => { text => $::locale->text('%') },
61 delivered_amount => { text => $::locale->text('Delivered amount') },
62 delivered_amount_p => { text => $::locale->text('%') },
63 paid_amount => { text => $::locale->text('Paid amount') },
64 paid_amount_p => { text => $::locale->text('%') },
65 billable_amount => { text => $::locale->text('Billable amount') },
66 billable_amount_p => { text => $::locale->text('%') },
67 other_amount => { text => $::locale->text('Billed extra expenses') },
68 transaction_description => { text => $::locale->text('Transaction description') },
69 ordnumber => { obj_link => sub { $self->link_to($_[0]) } },
70 customer => { sub => sub { $_[0]->customer->name },
71 obj_link => sub { $self->link_to($_[0]->customer) } },
72 globalprojectnumber => { sub => sub { $_[0]->globalproject_id ? $_[0]->globalproject->projectnumber : '' },
73 obj_link => sub { $self->link_to($_[0]->globalproject) } },
74 globalproject_type => { text => $::locale->text('Project type'),
75 sub => sub { $_[0]->globalproject_id ? $_[0]->globalproject->project_type->description : '' } },
78 $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) for keys %column_defs;
79 $column_defs{$_}->{align} = 'right' for @{ $self->{number_columns} };
82 std_column_visibility => 1,
83 controller_class => 'FinancialControllingReport',
84 output_format => 'HTML',
85 top_info_text => $::locale->text('Financial controlling report for open sales orders'),
86 title => $::locale->text('Financial Controlling Report'),
87 allow_pdf_export => 1,
88 allow_csv_export => 1,
90 $report->set_columns(%column_defs);
91 $report->set_column_order(@columns);
92 $report->set_export_options(qw(list filter));
93 $report->set_options_from_form;
94 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
95 $self->models->finalize;
96 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
98 raw_top_info_text => $self->render('financial_controlling_report/report_top', { output => 0 }),
99 raw_bottom_info_text => $self->render('financial_controlling_report/report_bottom', { output => 0 }, models => $self->models),
106 foreach my $order (@{ $self->orders }) {
107 my @delivery_orders = @{ $order->linked_records(direction => 'to', to => 'DeliveryOrder', via => 'Order', query => [ '!customer_id' => undef ]) };
108 my @invoices = @{ $order->linked_records(direction => 'to', to => 'Invoice', via => [ 'Order', 'DeliveryOrder' ]) };
111 grep({ !$_->storno_id } @invoices),
112 map({ @{ $_->storno_invoices } } grep { $_->storno && !$_->storno_id } @invoices),
115 $order->{delivered_amount} = sum(map { $self->sum_relevant_items(order => $order, other => $_, by_order => 1) } @delivery_orders) // 0;
116 $order->{billed_amount} = sum(map { $self->sum_relevant_items(order => $order, other => $_) } @invoices) // 0;
117 $order->{paid_amount} = sum(map { $_->paid * $_->netamount / (($_->amount * 1) || ($_->netamount * 1) || 1) } @invoices) // 0;
118 my $billed_amount = sum(map { $_->netamount } @invoices) // 0;
119 $order->{other_amount} = $billed_amount - $order->{billed_amount};
120 $order->{billable_amount} = $order->{delivered_amount} - $order->{billed_amount};
122 if ($order->periodic_invoices_config) {
123 $order->{net_amount} = $self->calculate_periodic_invoices_order_netamount($order);
126 $order->{net_amount} = $order->netamount;
129 foreach (qw(delivered billed paid billable)) {
130 $order->{"${_}_amount_p"} = $order->{net_amount} * 1 ? $order->{"${_}_amount"} * 100 / $order->{net_amount} : undef;
135 sub calculate_periodic_invoices_order_netamount {
136 my ($self, $order) = @_;
138 my $year = DateTime->today_local->year;
139 my $year_start = DateTime->new_local(day => 1, month => 1, year => $year);
140 my $year_end = DateTime->new_local(day => 31, month => 12, year => $year);
142 my $cfg = $order->periodic_invoices_config;
143 my $period_len = $cfg->get_billing_period_length;
145 my $cur_date = $cfg->start_date->clone;
146 my $end_date = $cfg->terminated ? $cfg->end_date : undef;
147 $end_date //= $year_end;
148 $end_date = min $end_date, $year_end;
150 while ($cur_date <= $end_date) {
151 $num_months += $period_len if $cur_date >= $year_start;
152 $cur_date->add(months => $period_len);
155 return $num_months * $order->netamount / $order->periodic_invoices_config->get_order_value_period_length;
159 my ($self, %params) = @_;
163 foreach my $item (@{ $params{obj}->items }) {
164 my $key = $item->parts_id;
165 $key .= ':' . $item->serialnumber if $item->serialnumber;
167 $vals{$key} ||= { parts_id => $item->parts_id, serialnumber => $item->serialnumber, amount => 0, base_qty => 0 };
168 $vals{$key}->{amount} += $item->qty * $item->sellprice * (1 - $item->discount) / (($item->price_factor * 1) || 1);
169 $vals{$key}->{base_qty} += $item->qty * $item->unit_obj->base_factor;
175 sub sum_relevant_items {
176 my ($self, %params) = @_;
178 $params{order}->{amounts_by_parts_id} ||= $self->sum_items(obj => $params{order});
179 my $sums = $self->sum_items(obj => $params{other});
182 foreach my $item (grep { $params{order}->{amounts_by_parts_id}->{ $_->{parts_id} } } values %{ $sums }) {
183 my $key = $item->{parts_id};
185 if ($params{by_order}) {
186 $key .= ':' . $item->{serialnumber};
187 my $order_item = $params{order}->{amounts_by_parts_id}->{ $key } || $params{order}->{amounts_by_parts_id}->{ $item->{parts_id} };
189 if ($order_item && $order_item->{base_qty}) {
190 $total += $order_item->{amount} * $item->{base_qty} / $order_item->{base_qty};
195 $total += $item->{amount};
203 my $modify_data = sub {
205 $data->{$_}->{data} = defined $data->{$_}->{data} ? int($data->{$_}->{data}) : '' for grep { m/_p$/ } @{ $self->{number_columns} };
206 $data->{$_}->{data} = $::form->format_amount(\%::myconfig, $data->{$_}->{data}, 2) for grep { !m/_p$/ } @{ $self->{number_columns} };
209 return $self->report_generator_list_objects(report => $self->{report}, objects => $self->orders, data_callback => $modify_data);
212 sub make_filter_summary {
215 my $filter = $::form->{filter} || {};
219 [ $filter->{"ordnumber:substr::ilike"}, $::locale->text('Number') ],
220 [ $filter->{"transdate:date::ge"}, $::locale->text('Order Date') . " " . $::locale->text('From Date') ],
221 [ $filter->{"transdate:date::le"}, $::locale->text('Order Date') . " " . $::locale->text('To Date') ],
222 [ $filter->{customer}{"name:substr::ilike"}, $::locale->text('Customer') ],
223 [ $filter->{customer}{"customernumber:substr::ilike"}, $::locale->text('Customer Number') ],
226 $self->{filter_summary} = join ', ', @filter_strings;
229 sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
234 SL::Controller::Helper::GetModels->new(
239 by => 'globalprojectnumber',
245 SL::DB::Manager::Order->type_filter('sales_order'),
247 (salesman_id => SL::DB::Manager::Employee->current->id) x !$::auth->assert('sales_all_edit', 1),
249 globalproject_id => undef,
251 'globalproject.active' => 1,
252 'globalproject.valid' => 1,
255 # oder aber (WR aktiv und (kein enddatum oder enddatum noch nicht überschritten))
257 'periodic_invoices_config.id' => undef,
259 'periodic_invoices_config.active' => 1,
261 # 'periodic_invoices_config.end_date' => undef,
262 # 'periodic_invoices_config.end_date' => { le => DateTime->today_local },
267 with_objects => [ 'customer', 'globalproject', 'globalproject.project_type', 'periodic_invoices_config' ],
272 my ($self, $object, %params) = @_;
274 return unless $object;
275 my $action = $params{action} || 'edit';
277 if ($object->isa('SL::DB::Order')) {
278 my $type = $object->type;
279 my $id = $object->id;
281 if ($::instance_conf->get_feature_experimental_order) {
282 return "controller.pl?action=Order/$action&type=$type&id=$id";
284 return "oe.pl?action=$action&type=$type&vc=customer&id=$id";
287 if ($object->isa('SL::DB::Customer')) {
288 my $id = $object->id;
289 return "controller.pl?action=CustomerVendor/$action&id=$id";
291 if ($object->isa('SL::DB::Project')) {
292 my $id = $object->id;
293 return "controller.pl?action=Project/$action&id=$id";
297 sub setup_list_action_bar {
298 my ($self, %params) = @_;
300 for my $bar ($::request->layout->get('actionbar')) {
304 submit => [ '#filter_form', { action => 'FinancialControllingReport/list' } ],
305 accesskey => 'enter',