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;
48 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
49 $self->{report} = $report;
51 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
52 billable_amount billable_amount_p other_amount);
53 my @sortable = qw(ordnumber transdate customer globalprojectnumber globalproject_type transaction_description );
54 $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) ];
57 net_amount => { text => $::locale->text('Order amount') },
58 billed_amount => { text => $::locale->text('Billed amount') },
59 billed_amount_p => { text => $::locale->text('%') },
60 delivered_amount => { text => $::locale->text('Delivered amount') },
61 delivered_amount_p => { text => $::locale->text('%') },
62 paid_amount => { text => $::locale->text('Paid amount') },
63 paid_amount_p => { text => $::locale->text('%') },
64 billable_amount => { text => $::locale->text('Billable amount') },
65 billable_amount_p => { text => $::locale->text('%') },
66 other_amount => { text => $::locale->text('Billed extra expenses') },
67 transaction_description => { text => $::locale->text('Transaction description') },
68 ordnumber => { obj_link => sub { $self->link_to($_[0]) } },
69 customer => { sub => sub { $_[0]->customer->name },
70 obj_link => sub { $self->link_to($_[0]->customer) } },
71 globalprojectnumber => { sub => sub { $_[0]->globalproject_id ? $_[0]->globalproject->projectnumber : '' },
72 obj_link => sub { $self->link_to($_[0]->globalproject) } },
73 globalproject_type => { text => $::locale->text('Project type'),
74 sub => sub { $_[0]->globalproject_id ? $_[0]->globalproject->project_type->description : '' } },
77 $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) for keys %column_defs;
78 $column_defs{$_}->{align} = 'right' for @{ $self->{number_columns} };
81 std_column_visibility => 1,
82 controller_class => 'FinancialControllingReport',
83 output_format => 'HTML',
84 top_info_text => $::locale->text('Financial controlling report for open sales orders'),
85 title => $::locale->text('Financial Controlling Report'),
86 allow_pdf_export => 1,
87 allow_csv_export => 1,
89 $report->set_columns(%column_defs);
90 $report->set_column_order(@columns);
91 $report->set_export_options(qw(list filter));
92 $report->set_options_from_form;
93 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
94 $self->models->finalize;
95 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
97 raw_top_info_text => $self->render('financial_controlling_report/report_top', { output => 0 }),
98 raw_bottom_info_text => $self->render('financial_controlling_report/report_bottom', { output => 0 }, models => $self->models),
105 foreach my $order (@{ $self->orders }) {
106 my @delivery_orders = @{ $order->linked_records(direction => 'to', to => 'DeliveryOrder', via => 'Order', query => [ '!customer_id' => undef ]) };
107 my @invoices = @{ $order->linked_records(direction => 'to', to => 'Invoice', via => [ 'Order', 'DeliveryOrder' ]) };
110 grep({ !$_->storno_id } @invoices),
111 map({ @{ $_->storno_invoices } } grep { $_->storno && !$_->storno_id } @invoices),
114 $order->{delivered_amount} = sum(map { $self->sum_relevant_items(order => $order, other => $_, by_order => 1) } @delivery_orders) // 0;
115 $order->{billed_amount} = sum(map { $self->sum_relevant_items(order => $order, other => $_) } @invoices) // 0;
116 $order->{paid_amount} = sum(map { $_->paid * $_->netamount / (($_->amount * 1) || ($_->netamount * 1) || 1) } @invoices) // 0;
117 my $billed_amount = sum(map { $_->netamount } @invoices) // 0;
118 $order->{other_amount} = $billed_amount - $order->{billed_amount};
119 $order->{billable_amount} = $order->{delivered_amount} - $order->{billed_amount};
121 if ($order->periodic_invoices_config) {
122 $order->{net_amount} = $self->calculate_periodic_invoices_order_netamount($order);
125 $order->{net_amount} = $order->netamount;
128 foreach (qw(delivered billed paid billable)) {
129 $order->{"${_}_amount_p"} = $order->{net_amount} * 1 ? $order->{"${_}_amount"} * 100 / $order->{net_amount} : undef;
134 sub calculate_periodic_invoices_order_netamount {
135 my ($self, $order) = @_;
137 my $year = DateTime->today_local->year;
138 my $year_start = DateTime->new_local(day => 1, month => 1, year => $year);
139 my $year_end = DateTime->new_local(day => 31, month => 12, year => $year);
141 my $cfg = $order->periodic_invoices_config;
142 my $period_len = $cfg->get_billing_period_length;
144 my $cur_date = $cfg->start_date->clone;
145 my $end_date = $cfg->terminated ? $cfg->end_date : undef;
146 $end_date //= $year_end;
147 $end_date = min $end_date, $year_end;
149 while ($cur_date <= $end_date) {
150 $num_months += $period_len if $cur_date >= $year_start;
151 $cur_date->add(months => $period_len);
154 return $num_months * $order->netamount / $order->periodic_invoices_config->get_order_value_period_length;
158 my ($self, %params) = @_;
162 foreach my $item (@{ $params{obj}->items }) {
163 my $key = $item->parts_id;
164 $key .= ':' . $item->serialnumber if $item->serialnumber;
166 $vals{$key} ||= { parts_id => $item->parts_id, serialnumber => $item->serialnumber, amount => 0, base_qty => 0 };
167 $vals{$key}->{amount} += $item->qty * $item->sellprice * (1 - $item->discount) / (($item->price_factor * 1) || 1);
168 $vals{$key}->{base_qty} += $item->qty * $item->unit_obj->base_factor;
174 sub sum_relevant_items {
175 my ($self, %params) = @_;
177 $params{order}->{amounts_by_parts_id} ||= $self->sum_items(obj => $params{order});
178 my $sums = $self->sum_items(obj => $params{other});
181 foreach my $item (grep { $params{order}->{amounts_by_parts_id}->{ $_->{parts_id} } } values %{ $sums }) {
182 my $key = $item->{parts_id};
184 if ($params{by_order}) {
185 $key .= ':' . $item->{serialnumber};
186 my $order_item = $params{order}->{amounts_by_parts_id}->{ $key } || $params{order}->{amounts_by_parts_id}->{ $item->{parts_id} };
188 if ($order_item && $order_item->{base_qty}) {
189 $total += $order_item->{amount} * $item->{base_qty} / $order_item->{base_qty};
194 $total += $item->{amount};
202 my $modify_data = sub {
204 $data->{$_}->{data} = defined $data->{$_}->{data} ? int($data->{$_}->{data}) : '' for grep { m/_p$/ } @{ $self->{number_columns} };
205 $data->{$_}->{data} = $::form->format_amount(\%::myconfig, $data->{$_}->{data}, 2) for grep { !m/_p$/ } @{ $self->{number_columns} };
208 return $self->report_generator_list_objects(report => $self->{report}, objects => $self->orders, data_callback => $modify_data);
211 sub make_filter_summary {
214 my $filter = $::form->{filter} || {};
218 [ $filter->{"ordnumber:substr::ilike"}, $::locale->text('Number') ],
219 [ $filter->{"transdate:date::ge"}, $::locale->text('Order Date') . " " . $::locale->text('From Date') ],
220 [ $filter->{"transdate:date::le"}, $::locale->text('Order Date') . " " . $::locale->text('To Date') ],
221 [ $filter->{customer}{"name:substr::ilike"}, $::locale->text('Customer') ],
222 [ $filter->{customer}{"customernumber:substr::ilike"}, $::locale->text('Customer Number') ],
225 $self->{filter_summary} = join ', ', @filter_strings;
228 sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
233 SL::Controller::Helper::GetModels->new(
238 by => 'globalprojectnumber',
244 SL::DB::Manager::Order->type_filter('sales_order'),
247 globalproject_id => undef,
249 'globalproject.active' => 1,
250 'globalproject.valid' => 1,
253 # oder aber (WR aktiv und (kein enddatum oder enddatum noch nicht überschritten))
255 'periodic_invoices_config.id' => undef,
257 'periodic_invoices_config.active' => 1,
259 # 'periodic_invoices_config.end_date' => undef,
260 # 'periodic_invoices_config.end_date' => { le => DateTime->today_local },
265 with_objects => [ 'customer', 'globalproject', 'globalproject.project_type', 'periodic_invoices_config' ],
270 my ($self, $object, %params) = @_;
272 return unless $object;
273 my $action = $params{action} || 'edit';
275 if ($object->isa('SL::DB::Order')) {
276 my $type = $object->type;
277 my $id = $object->id;
279 return "oe.pl?action=$action&type=$type&vc=customer&id=$id";
281 if ($object->isa('SL::DB::Customer')) {
282 my $id = $object->id;
283 return "controller.pl?action=CustomerVendor/$action&id=$id";
285 if ($object->isa('SL::DB::Project')) {
286 my $id = $object->id;
287 return "controller.pl?action=Project/$action&id=$id";