fe28838f873a5bbdcb3fa338babc882fc4c173e8
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Sales report
31 #
32 #======================================================================
33
34 use POSIX qw(strftime);
35 use List::Util qw(sum first);
36
37 use SL::AM;
38 use SL::VK;
39 use SL::IS;
40 use SL::ReportGenerator;
41 use Data::Dumper;
42
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/common.pl";
45 require "bin/mozilla/drafts.pl";
46 require "bin/mozilla/reportgenerator.pl";
47
48 use strict;
49
50 sub search_invoice {
51   $main::lxdebug->enter_sub();
52   $main::auth->assert('general_ledger | invoice_edit');
53
54   my $form     = $main::form;
55   my %myconfig = %main::myconfig;
56   my $locale   = $main::locale;
57
58   my ($customer, $department);
59
60   # setup customer selection
61   $form->all_vc(\%myconfig, "customer", "AR");
62
63   $form->{title}    = $locale->text('Sales Report');
64   $form->{jsscript} = 1;
65
66   $form->get_lists("projects"        => { "key" => "ALL_PROJECTS", "all" => 1 },
67                    "departments"     => "ALL_DEPARTMENTS",
68                    "business_types"  => "ALL_BUSINESS_TYPES",
69                    "salesmen"        => "ALL_SALESMEN",
70                    'employees'       => 'ALL_EMPLOYEES',
71                    'partsgroup'      => 'ALL_PARTSGROUPS',
72                    "customers"       => "ALL_VC");
73   $form->{CUSTOM_VARIABLES_IC}                  = CVar->get_configs('module' => 'IC');
74   ($form->{CUSTOM_VARIABLES_FILTER_CODE_IC},
75    $form->{CUSTOM_VARIABLES_INCLUSION_CODE_IC}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES_IC},
76                                                                            'include_prefix' => 'l_',
77                                                                            'include_value'  => 'Y');
78
79   $form->{CUSTOM_VARIABLES_CT}                  = CVar->get_configs('module' => 'CT');
80   ($form->{CUSTOM_VARIABLES_FILTER_CODE_CT},
81    $form->{CUSTOM_VARIABLES_INCLUSION_CODE_CT}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES_CT},
82                                                                            'include_prefix' => 'l_',
83                                                                            'include_value'  => 'Y');
84   $form->{vc_keys}   = sub { "$_[0]->{name}--$_[0]->{id}" };
85   $form->{employee_labels} = sub { $_[0]->{"name"} || $_[0]->{"login"} };
86   $form->{salesman_labels} = $form->{employee_labels};
87
88   $form->header;
89   print $form->parse_html_template('vk/search_invoice', { %myconfig });
90
91   $main::lxdebug->leave_sub();
92 }
93
94 sub invoice_transactions {
95   $main::lxdebug->enter_sub();
96
97   $main::auth->assert('general_ledger | invoice_edit');
98
99   my $form     = $main::form;
100   my %myconfig = %main::myconfig;
101   my $locale   = $main::locale;
102
103   my ($callback, $href, @columns);
104
105   # can't currently be configured from report, empty line between main sortings
106   my $addemptylines = '1';
107
108   if ( $form->{customer} =~ /--/ ) {
109     # Felddaten kommen aus Dropdownbox
110     ($form->{customername}, $form->{customer_id}) = split(/--/, $form->{customer});
111   } elsif ($form->{customer}) {
112     # es wurde ein Wert im Freitextfeld übergeben, auf Eindeutigkeit überprüfen
113
114     # check_name wird mit no_select => 1 ausgeführt, ist die Abfrage nicht eindeutig kommt ein Fehler
115     # und die Abfrage muß erneut ausgeführt werden
116
117     # Ohne no_select kommt bei Auswahl des Kunden ein Aufruf von update der ins
118     # Nichts führt, daher diese Zwischenlösung
119
120     &check_name('customer', no_select => 1);
121
122     # $form->{customer_id} wurde schon von check_name gesetzt
123     $form->{customername} = $form->{customer};
124   };
125   # ist $form->{customer} leer passiert hier nichts weiter
126
127   # decimalplaces überprüfen oder auf Default 2 setzen
128   $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
129
130   my $cvar_configs_ct = CVar->get_configs('module' => 'CT');
131   my $cvar_configs_ic = CVar->get_configs('module' => 'IC');
132
133 #  report_generator_set_default_sort('transdate', 1);
134
135   VK->invoice_transactions(\%myconfig, \%$form);
136
137   
138   if ( $form->{mainsort} eq 'month' or $form->{subsort} eq 'month' ) {
139
140     # Data already comes out of SELECT statement in correct month order, but
141     # remove whitespaces (month names are padded) and translate them as early
142     # as possible
143
144     foreach (@{ $form->{AR} }) {
145       $_->{month} =~ s/\s//g;
146       $_->{month} = $locale->text($_->{month});
147     };
148   };
149
150   $form->{title} = $locale->text('Sales Report');
151
152   @columns =
153     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);
154
155   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
156   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
157   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
158
159   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
160
161
162   # hidden variables für pdf/csv export übergeben
163   # einmal mit l_ um zu bestimmen welche Spalten ausgegeben werden sollen
164   # einmal optionen für die Überschrift (z.B. transdatefrom, partnumber, ...)
165   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 customername customer_id department partnumber partsgroup country business description project_id customernumber salesman employee salesman_id employee_id business_id partsgroup_id mainsort subsort), 
166       "$form->{db}number", 
167       map({ "cvar_$_->{name}" } @searchable_custom_variables),
168       map { "l_$_" } @columns
169       );
170   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
171   # Variablen werden dann als Hidden Variable mitgegeben, z.B.
172   # <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">
173
174   $href = build_std_url('action=invoice_transactions', grep { $form->{$_} } @hidden_variables);
175   # href = vk.pl?action=invoice_transactions&l_headers=Y&l_subtotal=Y&l_total=Y&transdatefrom=04.03.2010 ...
176
177   my %column_defs = (
178     'description'             => { 'text' => $locale->text('Description'), },
179     'partnumber'              => { 'text' => $locale->text('Part Number'), },
180     'partsgroup'              => { 'text' => $locale->text('Group'), },
181     'country'                 => { 'text' => $locale->text('Country'), },
182     'business'                => { 'text' => $locale->text('Customer type'), },
183     'employee'                => { 'text' => $locale->text('Employee'), },
184     'salesman'                => { 'text' => $locale->text('Salesperson'), },
185     'invnumber'               => { 'text' => $locale->text('Invoice Number'), },
186     'transdate'               => { 'text' => $locale->text('Invoice Date'), },
187     'qty'                     => { 'text' => $locale->text('Quantity'), },
188     'parts_unit'              => { 'text' => $locale->text('Base unit'), },
189     'weight'                  => { 'text' => $locale->text('Weight'), },
190     'sellprice'               => { 'text' => $locale->text('Sales price'), },
191     'sellprice_total'         => { 'text' => $locale->text('Sales net amount'), },
192     'lastcost_total'          => { 'text' => $locale->text('Purchase net amount'), },
193     'discount'                => { 'text' => $locale->text('Discount'), },
194     'lastcost'                => { 'text' => $locale->text('Purchase price'), },
195     'marge_total'             => { 'text' => $locale->text('Sales margin'), },
196     'marge_percent'           => { 'text' => $locale->text('Sales margin %'), },
197     'customernumber'          => { 'text' => $locale->text('Customer Number'), },
198     'customername'            => { 'text' => $locale->text('Customer Name'), },
199 # add 3 more column_defs so we have a translation for top_info_text
200     'customer'                => { 'text' => $locale->text('Customer'), },
201     'part'                    => { 'text' => $locale->text('Part'), },
202     'month'                   => { 'text' => $locale->text('Month'), },
203     %column_defs_cvars,
204   );
205
206   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
207
208   my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total parts_unit discount marge_total marge_percent qty weight);
209
210   
211   # so now the check-box "Description" is only used as switch for part description in invoice-mode
212   # always fill the column "Description" if we are in Zwischensummenmode
213   if (not defined $form->{"l_parts"}) {
214     $form->{"l_description"} = "Y";
215   };
216   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
217
218   my @options;
219
220   if ($form->{description}) {
221     push @options, $locale->text('Description') . " : $form->{description}";
222   }
223   if ($form->{customer}) {
224     push @options, $locale->text('Customer') . " : $form->{customername}";
225   }
226   if ($form->{customernumber}) {
227     push @options, $locale->text('Customer Number') . " : $form->{customernumber}";
228   }
229 # TODO: es wird nur id übergeben
230   if ($form->{department}) {
231     my ($department) = split /--/, $form->{department};
232     push @options, $locale->text('Department') . " : $department";
233   }
234   if ($form->{invnumber}) {
235     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
236   }
237   if ($form->{invdate}) {
238     push @options, $locale->text('Invoice Date') . " : $form->{invdate}";
239   }
240   if ($form->{partnumber}) {
241     push @options, $locale->text('Part Number') . " : $form->{partnumber}";
242   }
243   if ($form->{partsgroup_id}) {
244     my $partsgroup = SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load;
245     push @options, $locale->text('Group') . " : $partsgroup->{partsgroup}";
246   }
247   if ($form->{country}) {
248     push @options, $locale->text('Country') . " : $form->{country}";
249   }
250   if ($form->{employee_id}) {
251     my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
252     push @options, $locale->text('Employee') . ' : ' . $employee->name;
253   }
254   if ($form->{salesman_id}) {
255     my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
256     push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
257   }
258   if ($form->{business_id}) {
259     my $business = SL::DB::Business->new(id => $form->{business_id})->load;
260     push @options, $locale->text('Customer type') . ' : ' . $business->description;
261   }
262   if ($form->{ordnumber}) {
263     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
264   }
265   if ($form->{notes}) {
266     push @options, $locale->text('Notes') . " : $form->{notes}";
267   }
268   if ($form->{transaction_description}) {
269     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
270   }
271   if ($form->{transdatefrom}) {
272     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
273   }
274   if ($form->{transdateto}) {
275     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
276   }
277
278   my $report = SL::ReportGenerator->new(\%myconfig, $form);
279
280   $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),
281                        'output_format'        => 'HTML',
282                        'title'                => $form->{title},
283                        'attachment_basename'  => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
284     );
285   $report->set_options_from_form();
286   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
287
288   $report->set_columns(%column_defs);
289   $report->set_column_order(@columns);
290
291   $report->set_export_options('invoice_transactions', @hidden_variables, qw(mainsort sortdir));
292
293   $report->set_sort_indicator($form->{mainsort}, $form->{sortdir});
294
295   CVar->add_custom_variables_to_report('module'         => 'CT',
296       'trans_id_field' => 'customerid',
297       'configs'        => $cvar_configs_ct,
298       'column_defs'    => \%column_defs,
299       'data'           => $form->{AR}
300   );
301
302   CVar->add_custom_variables_to_report('module'         => 'IC',
303       'trans_id_field' => 'parts_id',
304       'configs'        => $cvar_configs_ic,
305       'column_defs'    => \%column_defs,
306       'data'           => $form->{AR}
307   );
308
309   # add sort and escape callback, this one we use for the add sub
310   $form->{callback} = $href .= "&sort=$form->{mainsort}";
311
312   # escape callback for href
313   $callback = $form->escape($href);
314
315   my @subtotal_columns = qw(qty weight sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
316   # Gesamtsumme:
317   # Summe von sellprice_total, lastcost_total und marge_total
318   # Durchschnitt von marge_percent
319   my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
320
321   my %totals    = map { $_ => 0 } @total_columns;
322   my %subtotals1 = map { $_ => 0 } @subtotal_columns;
323   my %subtotals2 = map { $_ => 0 } @subtotal_columns;
324
325   my $idx = 0;
326
327   my $basefactor;
328   my $all_units = AM->retrieve_all_units();
329
330   foreach my $ar (@{ $form->{AR} }) {
331     $basefactor = $all_units->{$ar->{unit}}->{factor} / $all_units->{$ar->{parts_unit}}->{factor};
332     $basefactor = 1 unless $basefactor;
333
334     $ar->{price_factor} = 1 unless $ar->{price_factor};
335     # calculate individual sellprice
336     # discount was already accounted for in db sellprice
337     $ar->{sellprice}       = $ar->{sellprice}  / $ar->{price_factor} / $basefactor;
338     $ar->{lastcost}        = $ar->{lastcost}   / $ar->{price_factor} / $basefactor;
339     $ar->{sellprice_total} = $ar->{qty} * ( $ar->{fxsellprice} * ( 1 - $ar->{discount} ) ) / $ar->{price_factor};
340     $ar->{lastcost_total}  = $ar->{qty} * $ar->{lastcost} * $basefactor;
341     # marge_percent wird neu berechnet, da Wert in invoice leer ist (Bug)
342     $ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total} * 100) : 0;
343     # marge_total neu berechnen
344     $ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total}  : 0;
345     $ar->{discount} *= 100;  # für Ausgabe formatieren, 10% stored as 0.1 in db
346
347     #adapt qty to the chosen unit
348     $ar->{qty} *= $basefactor;
349
350     #weight is the still the weight per part, but here we want the total weight
351     $ar->{weight} *= $ar->{qty};
352
353     # Anfangshauptüberschrift
354     if ( $form->{l_headers_mainsort} eq "Y" && ( $idx == 0 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
355       my $headerrow;
356
357       # use $emptyname for mainsort header if mainsort is empty
358       if ( $ar->{$form->{'mainsort'}} ) {
359         $headerrow->{description}->{data} = $ar->{$form->{'mainsort'}};
360       } else {
361         $headerrow->{description}->{data} = $locale->text('empty');
362       };
363
364       $headerrow->{description}->{class} = "listmainsortheader";
365       my $headerrow_set = [ $headerrow ];
366       $report->add_data($headerrow_set);
367
368       # add empty row after main header
369 #      my $emptyheaderrow->{description}->{data} = "";
370 #      $emptyheaderrow->{description}->{class} = "listmainsortheader";
371 #      my $emptyheaderrow_set = [ $emptyheaderrow ];
372 #      $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
373     };
374
375     # subsort überschriften
376     if ( $idx == 0
377       or $ar->{ $form->{'subsort'} }  ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
378       or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} }
379     ) {
380       my $headerrow;
381
382       # if subsort name is defined, use that name in header, otherwise use $emptyname
383       if ( $ar->{$form->{'subsort'}} ) {
384         $headerrow->{description}->{data} = $ar->{$form->{'subsort'}};
385       } else {
386         $headerrow->{description}->{data} = $locale->text('empty');
387       };
388       $headerrow->{description}->{class} = "listsubsortheader";
389       my $headerrow_set = [ $headerrow ];
390       # special case: subsort headers only makes (aesthetical) sense if we show individual parts
391       $report->add_data($headerrow_set) if $form->{l_headers_subsort} eq "Y" and $form->{l_parts};
392     };
393
394     map { $subtotals1{$_} += $ar->{$_};
395           $subtotals2{$_} += $ar->{$_};
396         } @subtotal_columns;
397
398     map { $totals{$_}    += $ar->{$_} } @total_columns;
399
400     if ( $subtotals1{qty} != 0 ) {
401       # calculate averages for subtotals1 and subtotals2
402       # credited positions reduce both total and qty and thus don't influence average prices
403       $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty};
404       $subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty};
405     } else {
406       # qty is zero, so we have a special case where each position in subtotal
407       # group has a corresponding credit note so that the total qty is zero in
408       # this case we also want the total amounts to be zero, so overwrite them,
409       # rather than leaving the last value in sellprice/lastcost
410
411       $subtotals1{sellprice} = 0;
412       $subtotals1{lastcost} = 0;
413     };
414
415     if ( $subtotals2{qty} != 0 ) {
416       $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty};
417       $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty};
418     } else {
419       $subtotals2{sellprice} = 0;
420       $subtotals2{lastcost} = 0;
421     };
422
423     # Ertrag prozentual in den Summen: (summe VK - summe Ertrag) / summe VK
424     $subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) * 100 : 0;
425     $subtotals2{marge_percent} = $subtotals2{sellprice_total} ? (($subtotals2{sellprice_total} - $subtotals2{lastcost_total}) / $subtotals2{sellprice_total}) *100 : 0;
426
427     # Ertrag prozentual:  (Summe VK betrag - Summe EK betrag) / Summe VK betrag
428     # wird laufend bei jeder Position neu berechnet
429     $totals{marge_percent}    = $totals{sellprice_total}    ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total}   ) * 100 : 0;
430
431     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_total marge_percent qty);
432     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 3) } qw(weight);
433     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total);
434
435     my $row = { };
436
437     foreach my $column (@columns) {
438       $row->{$column} = {
439         'data'  => $ar->{$column},
440         'align' => $column_alignment{$column},
441       };
442     }
443
444    $row->{description}->{class} = 'listsortdescription';
445
446     $row->{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit')
447       . "&id=" . E($ar->{id}) . "&callback=${callback}";
448
449     # Einzelzeilen nur zeigen wenn l_parts gesetzt ist, nützlich, wenn man nur
450     # Subtotals und Totals sehen möchte
451     my $row_set = $form->{l_parts} ? [ $row ] : [ ];
452
453     # hier wird bei l_subtotal nicht differenziert zwischen mainsort und subsort
454     # macht man l_subtotal_mainsort aus wird l_subtotal_subsort auch nicht ausgeführt
455     if (($form->{l_subtotal_mainsort} eq 'Y')
456         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
457           || ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'}   })
458           || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
459           )) {   # if value that is sorted by changes, print subtotal
460
461       if ($form->{l_subtotal_subsort} eq 'Y') {
462         push @{ $row_set }, create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, 'listsubsortsubtotal', $ar->{ $form->{'subsort'} }) ;
463         push @{ $row_set }, insert_empty_row() if $form->{l_parts} and $addemptylines;
464       };
465     }
466
467     # if last mainsort is reached or mainsort has changed, add mainsort subtotal and empty row
468     if (($form->{l_subtotal_mainsort} eq 'Y')
469         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
470             || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
471             )) {   # if value that is sorted by changes, print subtotal
472       if ($form->{l_subtotal_mainsort} eq 'Y' and $form->{mainsort} ne $form->{subsort} ) {
473         # subtotal is overriden if mainsort and subsort are equal, don't print
474         # subtotal line even if it is selected
475         push @{ $row_set }, create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listmainsortsubtotal', $ar->{$form->{mainsort}});
476         push @{ $row_set }, insert_empty_row() if $addemptylines; # insert empty row after mainsort
477       };
478     }
479
480     $report->add_data($row_set);
481
482     $idx++;
483   }
484   if ( $form->{l_total} eq "Y" ) {
485     $report->add_separator();
486     $report->add_data(create_subtotal_row_invoice(\%totals, \@columns, \%column_alignment, \@total_columns, 'listtotal', 'l_total'))
487   };
488
489   $report->generate_with_headers();
490   $main::lxdebug->leave_sub();
491 }
492
493
494 sub insert_empty_row {
495     my $dummyrow;
496     $dummyrow->{description}->{data} = "";
497     my $dummyrowset = [ $dummyrow ];
498     return $dummyrow;
499 };
500
501
502
503 sub create_subtotal_row_invoice {
504   $main::lxdebug->enter_sub();
505
506   my ($totals, $columns, $column_alignment, $subtotal_columns, $class, $name) = @_;
507
508   my $form     = $main::form;
509   my %myconfig = %main::myconfig;
510   my $locale   = $main::locale;
511
512   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
513
514   # set name as "empty" if no value is given, except if we are dealing with the
515   # absolute total, then just write "Total sum"
516   # here we assume that no name will be called 'l_total'
517   $name = $locale->text('empty') unless $name;
518   if ( $name eq 'l_total' ) {
519     $row->{description}->{data} = $locale->text('Total sum');
520   } else {
521     $row->{description}->{data} = $locale->text('Total') . ' ' . $name;
522   };
523
524   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent qty);
525   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 3) } qw(weight);
526   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);
527
528
529   map { $totals->{$_} = 0 } @{ $subtotal_columns };
530
531   $main::lxdebug->leave_sub();
532
533   return $row;
534 }
535
536 1;
537