1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
32 #======================================================================
34 use POSIX qw(strftime);
35 use List::Util qw(sum first);
39 use SL::ReportGenerator;
42 require "bin/mozilla/arap.pl";
43 require "bin/mozilla/common.pl";
44 require "bin/mozilla/drafts.pl";
45 require "bin/mozilla/reportgenerator.pl";
51 $main::lxdebug->enter_sub();
52 $main::auth->assert('general_ledger | invoice_edit');
54 my $form = $main::form;
55 my %myconfig = %main::myconfig;
56 my $locale = $main::locale;
59 my ($customer, $department);
61 # setup customer selection
62 $form->all_vc(\%myconfig, "customer", "AR");
64 $form->{title} = $locale->text('Sales Report');
65 $form->{jsscript} = 1;
67 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
68 "departments" => "ALL_DEPARTMENTS",
69 "customers" => "ALL_VC");
71 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
74 print $form->parse_html_template('vk/search_invoice', { %myconfig });
76 $main::lxdebug->leave_sub();
79 sub invoice_transactions {
80 $main::lxdebug->enter_sub();
82 $main::auth->assert('general_ledger | invoice_edit');
84 my $form = $main::form;
85 my %myconfig = %main::myconfig;
86 my $locale = $main::locale;
88 my ($callback, $href, @columns);
90 if ( $form->{customer} =~ /--/ ) {
91 # Felddaten kommen aus Dropdownbox
92 ($form->{customername}, $form->{customer_id}) = split(/--/, $form->{customer});
93 } elsif ($form->{customer}) {
94 # es wurde ein Wert im Freitextfeld übergeben, auf Eindeutigkeit überprüfen
96 # check_name wird mit no_select => 1 ausgeführt, ist die Abfrage nicht eindeutig kommt ein Fehler
97 # und die Abfrage muß erneut ausgeführt werden
99 # Ohne no_select kommt bei Auswahl des Kunden ein Aufruf von update der ins
100 # Nichts führt, daher diese Zwischenlösung
102 &check_name('customer', no_select => 1);
104 # $form->{customer_id} wurde schon von check_name gesetzt
105 $form->{customername} = $form->{customer};
107 # ist $form->{customer} leer passiert hier nichts weiter
109 # decimalplaces überprüfen oder auf Default 2 setzen
110 $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
112 # report_generator_set_default_sort('transdate', 1);
114 VK->invoice_transactions(\%myconfig, \%$form);
116 # anhand von radio button die Sortierreihenfolge festlegen
117 if ($form->{sortby} eq 'artikelsort') {
118 $form->{'mainsort'} = 'parts_id';
119 $form->{'subsort'} = 'name';
121 $form->{'mainsort'} = 'name';
122 $form->{'subsort'} = 'parts_id';
125 $form->{title} = $locale->text('Sales Report');
128 qw(description invnumber partnumber parts_id transdate qty unit sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent);
130 # hidden variables für pdf/csv export übergeben
131 # einmal mit l_ um zu bestimmen welche Spalten ausgegeben werden sollen
132 # einmal optionen für die Überschrift (z.B. transdatefrom, partnumber, ...)
133 my @hidden_variables = (qw(l_headers l_subtotal l_total transdatefrom transdateto decimalplaces customer customername customer_id department partnumber description project_id), "$form->{db}number", map { "l_$_" } @columns);
134 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
135 # Variablen werden dann als Hidden Variable mitgegeben, z.B.
136 # <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">
138 $href = build_std_url('action=invoice_transactions', grep { $form->{$_} } @hidden_variables);
139 # href = vk.pl?action=invoice_transactions&l_headers=Y&l_subtotal=Y&l_total=Y&transdatefrom=04.03.2010 ...
142 'description' => { 'text' => $locale->text('Description'), },
143 'partnumber' => { 'text' => $locale->text('Part Number'), },
144 'invnumber' => { 'text' => $locale->text('Invoice Number'), },
145 'transdate' => { 'text' => $locale->text('Invoice Date'), },
146 'qty' => { 'text' => $locale->text('Quantity'), },
147 'unit' => { 'text' => $locale->text('Unit'), },
148 'sellprice' => { 'text' => $locale->text('Sales price'), },
149 'sellprice_total' => { 'text' => $locale->text('Sales net amount'), },
150 'lastcost_total' => { 'text' => $locale->text('Purchase net amount'), },
151 'discount' => { 'text' => $locale->text('Discount'), },
152 'lastcost' => { 'text' => $locale->text('Purchase price'), },
153 'marge_total' => { 'text' => $locale->text('Sales margin'), },
154 'marge_percent' => { 'text' => $locale->text('Sales margin %'), },
157 my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total unit discount marge_total marge_percent qty);
159 $form->{"l_type"} = "Y";
160 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
164 if ($form->{description}) {
165 push @options, $locale->text('Description') . " : $form->{description}";
167 if ($form->{customer}) {
168 push @options, $locale->text('Customer') . " : $form->{customername}";
170 if ($form->{department}) {
171 my ($department) = split /--/, $form->{department};
172 push @options, $locale->text('Department') . " : $department";
174 if ($form->{invnumber}) {
175 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
177 if ($form->{invdate}) {
178 push @options, $locale->text('Invoice Date') . " : $form->{invdate}";
180 if ($form->{partnumber}) {
181 push @options, $locale->text('Part Number') . " : $form->{partnumber}";
183 if ($form->{ordnumber}) {
184 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
186 if ($form->{notes}) {
187 push @options, $locale->text('Notes') . " : $form->{notes}";
189 if ($form->{transaction_description}) {
190 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
192 if ($form->{transdatefrom}) {
193 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
195 if ($form->{transdateto}) {
196 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
199 my $report = SL::ReportGenerator->new(\%myconfig, $form);
201 $report->set_options('top_info_text' => join("\n", @options),
202 'output_format' => 'HTML',
203 'title' => $form->{title},
204 'attachment_basename' => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
206 $report->set_options_from_form();
207 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
209 $report->set_columns(%column_defs);
210 $report->set_column_order(@columns);
212 $report->set_export_options('invoice_transactions', @hidden_variables, qw(mainsort sortdir));
214 $report->set_sort_indicator($form->{mainsort}, $form->{sortdir});
216 # add sort and escape callback, this one we use for the add sub
217 $form->{callback} = $href .= "&sort=$form->{mainsort}";
219 # escape callback for href
220 $callback = $form->escape($href);
222 my @subtotal_columns = qw(qty sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
224 # Summe von sellprice_total, lastcost_total und marge_total
225 # Durchschnitt von marge_percent
226 my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
228 my %totals = map { $_ => 0 } @total_columns;
229 my %subtotals1 = map { $_ => 0 } @subtotal_columns;
230 my %subtotals2 = map { $_ => 0 } @subtotal_columns;
234 foreach my $ar (@{ $form->{AR} }) {
236 $ar->{price_factor} = 1 unless $ar->{price_factor};
237 # calculate individual sellprice
238 # discount was already accounted for in db sellprice
239 $ar->{sellprice} = $ar->{sellprice} / $ar->{price_factor};
240 $ar->{lastcost} = $ar->{lastcost} / $ar->{price_factor};
241 $ar->{sellprice_total} = $ar->{qty} * $ar->{sellprice};
242 $ar->{lastcost_total} = $ar->{qty} * $ar->{lastcost};
243 # marge_percent wird neu berechnet, da Wert in invoice leer ist (Bug)
244 $ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total}) : 0;
245 # marge_total neu berechnen
246 $ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total} : 0;
247 $ar->{discount} *= 100; # für Ausgabe formatieren, 10% stored as 0.1 in db
249 # Anfangshauptüberschrift
250 if ( $form->{l_headers} eq "Y" && ( $idx == 0 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
253 if ( $form->{mainsort} eq 'parts_id' ) {
254 $headerrow->{description}->{data} = "$ar->{description}";
256 $headerrow->{description}->{data} = "$ar->{name}";
258 $headerrow->{description}->{class} = "listmainsortheader";
259 my $headerrow_set = [ $headerrow ];
260 $report->add_data($headerrow_set);
262 # add empty row after main header
263 # my $emptyheaderrow->{description}->{data} = "";
264 # $emptyheaderrow->{description}->{class} = "listmainsortheader";
265 # my $emptyheaderrow_set = [ $emptyheaderrow ];
266 # $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
269 # subsort überschriften
271 or $ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
272 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} }
276 if ( $form->{subsort} eq 'parts_id' ) {
277 $name = 'description';
278 $headerrow->{description}->{data} = "$ar->{$name}";
281 $headerrow->{description}->{data} = "$ar->{$name}";
283 $headerrow->{description}->{class} = "listsubsortheader";
284 my $headerrow_set = [ $headerrow ];
285 $report->add_data($headerrow_set) if $form->{l_headers} eq "Y";
288 map { $subtotals1{$_} += $ar->{$_};
289 $subtotals2{$_} += $ar->{$_};
292 map { $totals{$_} += $ar->{$_} } @total_columns;
294 $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
295 $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
296 $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
297 $subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
299 # Ertrag prozentual in den Summen: (summe VK - summe Ertrag) / summe VK
300 $subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) : 0;
301 $subtotals2{marge_percent} = $subtotals2{sellprice_total} ? (($subtotals2{sellprice_total} - $subtotals2{lastcost_total}) / $subtotals2{sellprice_total}) : 0;
303 # Ertrag prozentual: (Summe VK betrag - Summe EK betrag) / Summe VK betrag
304 # wird laufend bei jeder Position neu berechnet
305 $totals{marge_percent} = $totals{sellprice_total} ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total} ) : 0;
307 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_total marge_percent);
308 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total);
312 foreach my $column (@columns) {
314 'data' => $ar->{$column},
315 'align' => $column_alignment{$column},
319 $row->{description}->{class} = 'listsortdescription';
321 $row->{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit')
322 . "&id=" . E($ar->{id}) . "&callback=${callback}";
324 my $row_set = [ $row ];
326 if (($form->{l_subtotal} eq 'Y')
327 && (($idx == (scalar @{ $form->{AR} } - 1)) # last element always has a subtotal
328 || ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'} })
329 || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
330 )) { # if value that is sorted by changes, print subtotal
332 if ( $form->{subsort} eq 'parts_id' ) {
333 $name = 'description';
338 if ($form->{l_subtotal} eq 'Y' ) {
339 push @{ $row_set }, create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, 'listsubsortsubtotal', $ar->{$name}) ;
340 push @{ $row_set }, insert_empty_row();
344 # if mainsort has changed, add mainsort subtotal and empty row
345 if (($form->{l_subtotal} eq 'Y')
346 && (($idx == (scalar @{ $form->{AR} } - 1)) # last element always has a subtotal
347 || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
348 )) { # if value that is sorted by changes, print subtotal
350 if ( $form->{mainsort} eq 'parts_id' ) {
351 $name = 'description';
355 if ($form->{l_subtotal} eq 'Y' ) {
356 push @{ $row_set }, create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listmainsortsubtotal', $ar->{$name});
357 push @{ $row_set }, insert_empty_row();
361 $report->add_data($row_set);
365 if ( $form->{l_total} eq "Y" ) {
366 $report->add_separator();
367 $report->add_data(create_subtotal_row_invoice(\%totals, \@columns, \%column_alignment, \@total_columns, 'listtotal'))
370 $report->generate_with_headers();
371 $main::lxdebug->leave_sub();
375 sub insert_empty_row {
377 $dummyrow->{description}->{data} = "";
378 my $dummyrowset = [ $dummyrow ];
384 sub create_subtotal_row_invoice {
385 $main::lxdebug->enter_sub();
387 my ($totals, $columns, $column_alignment, $subtotal_columns, $class, $name) = @_;
389 my $form = $main::form;
390 my %myconfig = %main::myconfig;
392 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
394 $row->{description}->{data} = "Summe " . $name;
396 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent);
397 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 0) } qw(qty);
398 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);
401 map { $totals->{$_} = 0 } @{ $subtotal_columns };
403 $main::lxdebug->leave_sub();