25e351d0d7acfebdc4961a01c56c3126cb78b58c
[kivitendo-erp.git] / bin / mozilla / vk.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # Sales report
32 #
33 #======================================================================
34
35 use POSIX qw(strftime);
36 use List::Util qw(sum first);
37
38 use SL::AM;
39 use SL::DB::Employee;
40 use SL::VK;
41 use SL::IS;
42 use SL::ReportGenerator;
43 use Data::Dumper;
44
45 require "bin/mozilla/arap.pl";
46 require "bin/mozilla/common.pl";
47 require "bin/mozilla/reportgenerator.pl";
48
49 use strict;
50
51 sub search_invoice {
52   $main::lxdebug->enter_sub();
53   $main::auth->assert('ar_transactions | ap_transactions | invoice_edit');
54
55   my $form     = $main::form;
56   my %myconfig = %main::myconfig;
57   my $locale   = $main::locale;
58
59   my ($customer);
60
61   $::request->layout->add_javascripts("autocomplete_project.js");
62
63   $form->{title}    = $locale->text('Sales Report');
64
65   $form->get_lists("departments"     => "ALL_DEPARTMENTS",
66                    "business_types"  => "ALL_BUSINESS_TYPES",
67                    "salesmen"        => "ALL_SALESMEN",
68                    'partsgroup'      => 'ALL_PARTSGROUPS');
69
70   $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted;
71
72   $form->{CUSTOM_VARIABLES_IC}                  = CVar->get_configs('module' => 'IC');
73   ($form->{CUSTOM_VARIABLES_FILTER_CODE_IC},
74    $form->{CUSTOM_VARIABLES_INCLUSION_CODE_IC}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES_IC},
75                                                                            'include_prefix' => 'l_',
76                                                                            'include_value'  => 'Y');
77
78   $form->{CUSTOM_VARIABLES_CT}                  = CVar->get_configs('module' => 'CT');
79   ($form->{CUSTOM_VARIABLES_FILTER_CODE_CT},
80    $form->{CUSTOM_VARIABLES_INCLUSION_CODE_CT}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES_CT},
81                                                                            'include_prefix' => 'l_',
82                                                                            'include_value'  => 'Y');
83   $form->header;
84   print $form->parse_html_template('vk/search_invoice', { %myconfig });
85
86   $main::lxdebug->leave_sub();
87 }
88
89 sub invoice_transactions {
90   $main::lxdebug->enter_sub();
91
92   $main::auth->assert('ar_transactions | ap_transactions | invoice_edit');
93
94   my $form     = $main::form;
95   my %myconfig = %main::myconfig;
96   my $locale   = $main::locale;
97
98   my ($callback, $href, @columns);
99
100   # can't currently be configured from report, empty line between main sortings
101   my $addemptylines = 1;
102
103   # don't add empty lines between mainsort subtotals when only subtotal_mainsort is selected
104   if ($form->{l_subtotal_mainsort} eq "Y" and not defined $form->{l_headers_mainsort} and not defined $form->{l_headers_subsort} and not defined $form->{l_subtotal_subsort} ) {
105     $addemptylines = 0
106   };
107
108   if ( $form->{customer} =~ /--/ ) {
109     # Felddaten kommen aus Dropdownbox
110     my $dummy;
111     ($dummy, $form->{customer_id}) = split(/--/, $form->{customer});
112   }
113   # if $form->{customer} is empty nothing further happens here
114
115   # test for decimalplaces or set to default of 2
116   $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
117
118   my $cvar_configs_ct = CVar->get_configs('module' => 'CT');
119   my $cvar_configs_ic = CVar->get_configs('module' => 'IC');
120
121 #  report_generator_set_default_sort('transdate', 1);
122
123   VK->invoice_transactions(\%myconfig, \%$form);
124
125
126   if ( $form->{mainsort} eq 'month' or $form->{subsort} eq 'month' ) {
127
128     # Data already comes out of SELECT statement in correct month order, but
129     # remove whitespaces (month names are padded) and translate them as early
130     # as possible
131
132     foreach (@{ $form->{AR} }) {
133       $_->{month} =~ s/\s//g;
134       $_->{month} = $locale->text($_->{month});
135     };
136   };
137
138   $form->{title} = $locale->text('Sales Report');
139
140   @columns =
141     qw(description invnumber transdate customernumber customername partnumber partsgroup country business transdate qty parts_unit weight sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent employee salesman);
142
143   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
144   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
145   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
146
147   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
148
149
150   # pass hidden variables for pdf/csv export
151   # first with l_ to determine which columns to show
152   # then with the options for headings (such as transdatefrom, partnumber, ...)
153   my @hidden_variables  = (qw(l_headers_mainsort l_headers_subsort l_subtotal_mainsort l_subtotal_subsort l_total l_parts l_customername l_customernumber transdatefrom transdateto decimalplaces customer customer_id department_id partnumber partsgroup country business description project_id customernumber salesman employee salesman_id employee_id business_id partsgroup_id mainsort subsort),
154       "$form->{db}number",
155       map({ "cvar_$_->{name}" } @searchable_custom_variables),
156       map { "l_$_" } @columns
157       );
158   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
159   # variables are passed as hiddens, e.g.
160   # <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">
161
162   $href = build_std_url('action=invoice_transactions', grep { $form->{$_} } @hidden_variables);
163   # href = vk.pl?action=invoice_transactions&l_headers=Y&l_subtotal=Y&l_total=Y&transdatefrom=04.03.2010 ...
164
165   my %column_defs = (
166     'description'             => { 'text' => $locale->text('Description'), },
167     'partnumber'              => { 'text' => $locale->text('Part Number'), },
168     'partsgroup'              => { 'text' => $locale->text('Partsgroup'), },
169     'country'                 => { 'text' => $locale->text('Country'), },
170     'business'                => { 'text' => $locale->text('Customer type'), },
171     'employee'                => { 'text' => $locale->text('Employee'), },
172     'salesman'                => { 'text' => $locale->text('Salesperson'), },
173     'invnumber'               => { 'text' => $locale->text('Invoice Number'), },
174     'transdate'               => { 'text' => $locale->text('Invoice Date'), },
175     'qty'                     => { 'text' => $locale->text('Quantity'), },
176     'parts_unit'              => { 'text' => $locale->text('Base unit'), },
177     'weight'                  => { 'text' => $locale->text('Weight'), },
178     'sellprice'               => { 'text' => $locale->text('Sales price'), },
179     'sellprice_total'         => { 'text' => $locale->text('Sales net amount'), },
180     'lastcost_total'          => { 'text' => $locale->text('Purchase net amount'), },
181     'discount'                => { 'text' => $locale->text('Discount'), },
182     'lastcost'                => { 'text' => $locale->text('Purchase price'), },
183     'marge_total'             => { 'text' => $locale->text('Sales margin'), },
184     'marge_percent'           => { 'text' => $locale->text('Sales margin %'), },
185     'customernumber'          => { 'text' => $locale->text('Customer Number'), },
186     'customername'            => { 'text' => $locale->text('Customer Name'), },
187 # add 3 more column_defs so we have a translation for top_info_text
188     'customer'                => { 'text' => $locale->text('Customer'), },
189     'part'                    => { 'text' => $locale->text('Part'), },
190     'month'                   => { 'text' => $locale->text('Month'), },
191     %column_defs_cvars,
192   );
193
194   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
195
196   my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total parts_unit discount marge_total marge_percent qty weight);
197
198
199   # so now the check-box "Description" is only used as switch for part description in invoice-mode
200   # always fill the column "Description" if we are in Zwischensummenmode
201   $form->{"l_description"} = "Y" if not defined $form->{"l_parts"};;
202   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
203
204   my @options;
205
206   push @options, $locale->text('Description')             . " : $form->{description}"                                                       if $form->{description};
207   push @options, $locale->text('Customer')                . " : $form->{customer}"                                                          if $form->{customer};
208   push @options, $locale->text('Customer Number')         . " : $form->{customernumber}"                                                    if $form->{customernumber};
209   # TODO: only customer id is passed
210   push @options, $locale->text('Department')              . " : " . SL::DB::Department->new(id => $form->{department_id})->load->description if $form->{department_id};
211   push @options, $locale->text('Invoice Number')          . " : $form->{invnumber}"                                                         if $form->{invnumber};
212   push @options, $locale->text('Invoice Date')            . " : $form->{invdate}"                                                           if $form->{invdate};
213   push @options, $locale->text('Part Number')             . " : $form->{partnumber}"                                                        if $form->{partnumber};
214   push @options, $locale->text('Partsgroup')              . " : " . SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load->partsgroup if $form->{partsgroup_id};
215   push @options, $locale->text('Country')                 . " : $form->{country}"                                                           if $form->{country};
216   push @options, $locale->text('Employee')                . ' : ' . SL::DB::Employee->new(id => $form->{employee_id})->load->name           if $form->{employee_id};
217   push @options, $locale->text('Salesman')                . ' : ' . SL::DB::Employee->new(id => $form->{salesman_id})->load->name           if $form->{salesman_id};
218   push @options, $locale->text('Customer type')           . ' : ' . SL::DB::Business->new(id => $form->{business_id})->load->description    if $form->{business_id};
219   push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                                                         if $form->{ordnumber};
220   push @options, $locale->text('Notes')                   . " : $form->{notes}"                                                             if $form->{notes};
221   push @options, $locale->text('Transaction description') . " : $form->{transaction_description}"                                           if $form->{transaction_description};
222   push @options, $locale->text('From')                    . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1)                      if $form->{transdatefrom};
223   push @options, $locale->text('Bis')                     . " " . $locale->date(\%myconfig, $form->{transdateto}, 1)                        if $form->{transdateto};
224
225   my $report = SL::ReportGenerator->new(\%myconfig, $form);
226
227   $report->set_options('top_info_text'        => join("\n", $locale->text('Main sorting') . ' : ' . $column_defs{$form->{mainsort}}->{text} , $locale->text('Secondary sorting') . ' : ' . $column_defs{$form->{'subsort'}}->{text}, @options),
228                        'output_format'        => 'HTML',
229                        'title'                => $form->{title},
230                        'attachment_basename'  => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
231     );
232   $report->set_options_from_form();
233   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
234
235   $report->set_columns(%column_defs);
236   $report->set_column_order(@columns);
237
238   $report->set_export_options('invoice_transactions', @hidden_variables, qw(mainsort sortdir));
239
240   $report->set_sort_indicator($form->{mainsort}, $form->{sortdir});
241
242   CVar->add_custom_variables_to_report('module'         => 'CT',
243       'trans_id_field' => 'customerid',
244       'configs'        => $cvar_configs_ct,
245       'column_defs'    => \%column_defs,
246       'data'           => $form->{AR}
247   );
248
249   CVar->add_custom_variables_to_report('module'         => 'IC',
250       'trans_id_field' => 'parts_id',
251       'configs'        => $cvar_configs_ic,
252       'column_defs'    => \%column_defs,
253       'data'           => $form->{AR}
254   );
255
256   my $num_visible_columns = scalar $report->get_visible_columns;
257   my %empty_row           = (
258     description           => {
259       data                => '',
260       class               => 'listrowempty',
261       colspan             => $num_visible_columns,
262     },
263   );
264
265   # add sort and escape callback, this one we use for the add sub
266   $form->{callback} = $href .= "&sort=$form->{mainsort}";
267
268   # escape callback for href
269   $callback = $form->escape($href);
270
271   my @subtotal_columns = qw(qty weight sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
272   # Total sum:
273   # sum of sellprice_total, lastcost_total and marge_total
274   # average of marge_percent
275   my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
276
277   my %totals     = map { $_ => 0 } @total_columns;
278   my %subtotals1 = map { $_ => 0 } @subtotal_columns;
279   my %subtotals2 = map { $_ => 0 } @subtotal_columns;
280
281   my $idx = 0;
282
283   my $basefactor;
284   my $all_units = AM->retrieve_all_units();
285
286   foreach my $ar (@{ $form->{AR} }) {
287     $basefactor = $all_units->{$ar->{unit}}->{factor} / $all_units->{$ar->{parts_unit}}->{factor};
288     $basefactor = 1 unless $basefactor;
289
290     $ar->{price_factor} = 1 unless $ar->{price_factor};
291
292     # calculate individual sellprice, discount is already accounted for in column sellprice in db
293
294     # The sellprice total can be calculated from sellprice or fxsellprice (the
295     # value that was actually entered in the sellprice field and is always
296     # stored separately).  However, for fxsellprice this method only works when
297     # the tax is not included, because otherwise fxsellprice includes the tax
298     # and there is no simple way to extract the tax rate of the article from
299     # the big query.
300     #
301     # Using fxsellprice is potentially more accurate (certainly for tax
302     # included), because we can use the same method as is used while the
303     # invoice is generated.
304     #
305     # sellprice however has already been converted to the net value (but
306     # rounded in the process, which leads to rounding errors when calculating
307     # the linetotal from the rounded sellprice in the report.  These rounding
308     # errors can quickly amount to several cents when qty is large)
309     #
310     # For calculating sellprice_total from fxsellprice, you would use:
311     # sellprice_total_including_tax = qty * fxsellprice * (1-discount) /  price_factor * exchangerate
312     # $ar->{sellprice_total_including_tax} =  $form->round_amount( $ar->{qty} * ( $ar->{fxsellprice} * ( 1 - $ar->{discount} ) ) / $ar->{price_factor}, $form->{"decimalplaces"});
313
314     my $sellprice_orig     = $ar->{sellprice};
315     my $qty_orig           = $ar->{qty};
316     # adjust sellprice so it reflects the unit sellprice according to price_factor and basefactor
317     $ar->{sellprice}       = $ar->{sellprice}  / $ar->{price_factor} / $basefactor;
318     # for sellprice_total use the original amounts
319     $ar->{sellprice_total} = $form->round_amount( $qty_orig * $sellprice_orig / $ar->{price_factor}, $form->{"decimalplaces"});
320
321     my $lastcost_orig      = $ar->{lastcost};
322     $ar->{lastcost}        = $ar->{lastcost}   / $ar->{price_factor} / $basefactor;
323     $ar->{lastcost_total}  = $form->round_amount( $qty_orig * $lastcost_orig / $ar->{price_factor}, $form->{"decimalplaces"});
324
325     # marge_percent is recalculated, because the value in invoice used to be empty
326     $ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total} * 100) : 0;
327     # also recalculate marge_total
328     $ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total}  : 0;
329     $ar->{discount} *= 100;  # format discount value for output, 10% is stored as 0.1 in db
330
331     # adjust qty to reflect the qty of the base unit
332     $ar->{qty} *= $basefactor;
333
334     # now $ar->{sellprice} * $ar->{qty} should be the same as $qty_orig * $ar->{qty}
335
336     # weight is the still the weight per part, but here we want the total weight
337     $ar->{weight} *= $ar->{qty};
338
339     # Main header
340     if ( $form->{l_headers_mainsort} eq "Y" && ( $idx == 1 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
341       my $headerrow = {
342         # use $emptyname for mainsort header if mainsort is empty
343         data    => $ar->{$form->{'mainsort'}} || $locale->text('empty'),
344         class   => "listmainsortheader",
345         colspan => $num_visible_columns,
346       };
347       $report->add_data([ { description => $headerrow } ]);
348
349       # add empty row after main header
350 #      my $emptyheaderrow->{description}->{data} = "";
351 #      $emptyheaderrow->{description}->{class} = "listmainsortheader";
352 #      my $emptyheaderrow_set = [ $emptyheaderrow ];
353 #      $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
354     };
355
356     # subsort headers
357     # special case: subsort headers only makes (aesthetical) sense if we show individual parts
358     if ((   $idx == 0
359          or $ar->{ $form->{'subsort'} }  ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
360          or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} })
361         && ($form->{l_headers_subsort} eq "Y")
362         && $form->{l_parts}
363     ) {
364       my $headerrow = {
365         # if subsort name is defined, use that name in header, otherwise use $emptyname
366         data    => $ar->{$form->{'subsort'}} || $locale->text('empty'),
367         class   => "listsubsortheader",
368         colspan => $num_visible_columns,
369       };
370       $report->add_data([ { description => $headerrow } ]);
371     };
372
373     map { $subtotals1{$_} += $ar->{$_};
374           $subtotals2{$_} += $ar->{$_};
375         } @subtotal_columns;
376
377     map { $totals{$_}    += $ar->{$_} } @total_columns;
378
379     if ( $subtotals1{qty} != 0 ) {
380       # calculate averages for subtotals1 and subtotals2
381       # credited positions reduce both total and qty and thus don't influence average prices
382       $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty};
383       $subtotals1{lastcost}  = $subtotals1{lastcost_total}  / $subtotals1{qty};
384     } else {
385       # qty is zero, so we have a special case where each position in subtotal
386       # group has a corresponding credit note so that the total qty is zero in
387       # this case we also want the total amounts to be zero, so overwrite them,
388       # rather than leaving the last value in sellprice/lastcost
389
390       $subtotals1{sellprice} = 0;
391       $subtotals1{lastcost}  = 0;
392     };
393
394     if ( $subtotals2{qty} != 0 ) {
395       $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty};
396       $subtotals2{lastcost}  = $subtotals2{lastcost_total}  / $subtotals2{qty};
397     } else {
398       $subtotals2{sellprice} = 0;
399       $subtotals2{lastcost}  = 0;
400     };
401
402     # marge percent as sums: (sum VK - sum Ertrag) / sum VK
403     $subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) * 100 : 0;
404     $subtotals2{marge_percent} = $subtotals2{sellprice_total} ? (($subtotals2{sellprice_total} - $subtotals2{lastcost_total}) / $subtotals2{sellprice_total}) *100 : 0;
405
406     # total marge percent:  (sum VK betrag - sum EK betrag) / sum VK betrag
407     # is recalculated after each position
408     $totals{marge_percent}    = $totals{sellprice_total}    ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total}   ) * 100 : 0;
409
410     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_percent qty);
411     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 3) } qw(weight);
412     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total marge_total);
413
414     # only show individual lines when l_parts is set, this is useful, if you
415     # only want to see subtotals and totals
416
417     if ($form->{l_parts}) {
418       my %row = (
419         map { ($_ => { data => $ar->{$_}, align => $column_alignment{$_} }) } @columns
420       );
421
422       $row{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit') . "&id=" . E($ar->{id}) . "&callback=${callback}";
423
424       # use partdescription according to invoice in article mode
425       $row{description}->{data} = $ar->{invoice_description};
426
427       $report->add_data(\%row);
428     }
429
430     # choosing l_subtotal doesn't make a distinction between mainsort and subsort
431     # if l_subtotal_mainsort is not selected l_subtotal_subsort isn't run either
432     if (   ($form->{l_subtotal_mainsort} eq 'Y')
433         && ($form->{l_subtotal_subsort}  eq 'Y')
434         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
435           || ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'}   })
436           || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
437           )) {   # if value that is sorted by changes, print subtotal
438
439       $report->add_data(create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, $form->{l_parts} ? 'listsubtotal' : undef, $ar->{ $form->{'subsort'} }));
440       $report->add_data({ %empty_row }) if $form->{l_parts} and $addemptylines;
441     }
442
443     # if last mainsort is reached or mainsort has changed, add mainsort subtotal and empty row
444     if (   ($form->{l_subtotal_mainsort} eq 'Y')
445         && ($form->{l_subtotal_mainsort} eq 'Y')
446         && ($form->{mainsort}            ne $form->{subsort})
447         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
448             || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
449             )) {   # if value that is sorted by changes, print subtotal
450         # subtotal is overridden if mainsort and subsort are equal, don't print
451         # subtotal line even if it is selected
452       $report->add_data(create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal', $ar->{$form->{mainsort}}));
453       $report->add_data({ %empty_row }) if $addemptylines; # insert empty row after mainsort
454     }
455
456     $idx++;
457   }
458   if ( $form->{l_total} eq "Y" ) {
459     $report->add_separator();
460     $report->add_data(create_subtotal_row_invoice(\%totals, \@columns, \%column_alignment, \@total_columns, 'listtotal', 'l_total'))
461   };
462
463   $report->generate_with_headers();
464   $main::lxdebug->leave_sub();
465 }
466
467 sub create_subtotal_row_invoice {
468   $main::lxdebug->enter_sub();
469
470   my ($totals, $columns, $column_alignment, $subtotal_columns, $class, $name) = @_;
471
472   my $form     = $main::form;
473   my %myconfig = %main::myconfig;
474   my $locale   = $main::locale;
475
476   my $row = { map { $_ => { data => '', class => $class, align => $column_alignment->{$_}, } } @{ $columns } };
477
478   # set name as "empty" if no value is given, except if we are dealing with the
479   # absolute total, then just write "Total sum"
480   # here we assume that no name will be called 'l_total'
481   $name = $locale->text('empty') unless $name;
482   if ( $name eq 'l_total' ) {
483     $row->{description}->{data} = $locale->text('Total sum');
484   } else {
485     $row->{description}->{data} = $locale->text('Total') . ' ' . $name;
486   };
487
488   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent qty);
489   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 3) } qw(weight);
490   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);
491
492
493   map { $totals->{$_} = 0 } @{ $subtotal_columns };
494
495   $main::lxdebug->leave_sub();
496
497   return $row;
498 }
499
500 1;