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);
38 use SL::ReportGenerator;
41 require "bin/mozilla/arap.pl";
42 require "bin/mozilla/common.pl";
43 require "bin/mozilla/drafts.pl";
44 require "bin/mozilla/reportgenerator.pl";
50 $main::lxdebug->enter_sub();
51 $main::auth->assert('general_ledger | invoice_edit');
53 my $form = $main::form;
54 my %myconfig = %main::myconfig;
55 my $locale = $main::locale;
58 my ($customer, $department);
60 # setup customer selection
61 $form->all_vc(\%myconfig, "customer", "AR");
63 $form->{title} = $locale->text('Sales Report');
64 $form->{jsscript} = 1;
66 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
67 "departments" => "ALL_DEPARTMENTS",
68 "customers" => "ALL_VC");
70 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
73 print $form->parse_html_template('vk/search_invoice', { %myconfig });
75 $main::lxdebug->leave_sub();
78 sub invoice_transactions {
79 $main::lxdebug->enter_sub();
81 $main::auth->assert('general_ledger | invoice_edit');
83 my $form = $main::form;
84 my %myconfig = %main::myconfig;
85 my $locale = $main::locale;
87 my ($callback, $href, @columns);
89 $form->{customer} = $form->unescape($form->{customer});
91 ($form->{customername}, $form->{customer_id}) = split(/--/, $form->{customer});
93 # decimalplaces überprüfen oder auf Default 2 setzen
94 $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
96 # report_generator_set_default_sort('transdate', 1);
98 VK->invoice_transactions(\%myconfig, \%$form);
100 # anhand von radio button die Sortierreihenfolge festlegen
101 if ($form->{sortby} eq 'artikelsort') {
102 $form->{'mainsort'} = 'parts_id';
103 $form->{'subsort'} = 'name';
105 $form->{'mainsort'} = 'name';
106 $form->{'subsort'} = 'parts_id';
109 $form->{title} = $locale->text('Sales Report');
112 qw(description invnumber partnumber parts_id transdate qty unit sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent);
114 # hidden variables für pdf/csv export übergeben
115 # einmal mit l_ um zu bestimmen welche Spalten ausgegeben werden sollen
116 # einmal optionen für die Überschrift (z.B. transdatefrom, partnumber, ...)
117 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);
118 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
119 # Variablen werden dann als Hidden Variable mitgegeben, z.B.
120 # <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">
122 $href = build_std_url('action=invoice_transactions', grep { $form->{$_} } @hidden_variables);
123 # href = vk.pl?action=invoice_transactions&l_headers=Y&l_subtotal=Y&l_total=Y&transdatefrom=04.03.2010 ...
126 'description' => { 'text' => $locale->text('Description'), },
127 'partnumber' => { 'text' => $locale->text('Part Number'), },
128 'invnumber' => { 'text' => $locale->text('Invoice Number'), },
129 'transdate' => { 'text' => $locale->text('Invoice Date'), },
130 'qty' => { 'text' => $locale->text('Quantity'), },
131 'unit' => { 'text' => $locale->text('Unit'), },
132 'sellprice' => { 'text' => $locale->text('Sales price'), },
133 'sellprice_total' => { 'text' => $locale->text('Sales net amount'), },
134 'lastcost_total' => { 'text' => $locale->text('Purchase net amount'), },
135 'discount' => { 'text' => $locale->text('Discount'), },
136 'lastcost' => { 'text' => $locale->text('Purchase price'), },
137 'marge_total' => { 'text' => $locale->text('Sales margin'), },
138 'marge_percent' => { 'text' => $locale->text('Sales margin %'), },
141 my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total unit discount marge_total marge_percent qty);
143 $form->{"l_type"} = "Y";
144 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
148 if ($form->{description}) {
149 push @options, $locale->text('Description') . " : $form->{description}";
151 if ($form->{customer}) {
152 push @options, $locale->text('Customer') . " : $form->{customername}";
154 if ($form->{department}) {
155 my ($department) = split /--/, $form->{department};
156 push @options, $locale->text('Department') . " : $department";
158 if ($form->{invnumber}) {
159 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
161 if ($form->{invdate}) {
162 push @options, $locale->text('Invoice Date') . " : $form->{invdate}";
164 if ($form->{partnumber}) {
165 push @options, $locale->text('Part Number') . " : $form->{partnumber}";
167 if ($form->{ordnumber}) {
168 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
170 if ($form->{notes}) {
171 push @options, $locale->text('Notes') . " : $form->{notes}";
173 if ($form->{transaction_description}) {
174 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
176 if ($form->{transdatefrom}) {
177 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
179 if ($form->{transdateto}) {
180 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
183 my $report = SL::ReportGenerator->new(\%myconfig, $form);
185 $report->set_options('top_info_text' => join("\n", @options),
186 'output_format' => 'HTML',
187 'title' => $form->{title},
188 'attachment_basename' => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
190 $report->set_options_from_form();
191 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
193 $report->set_columns(%column_defs);
194 $report->set_column_order(@columns);
196 $report->set_export_options('invoice_transactions', @hidden_variables, qw(mainsort sortdir));
198 $report->set_sort_indicator($form->{mainsort}, $form->{sortdir});
200 # add sort and escape callback, this one we use for the add sub
201 $form->{callback} = $href .= "&sort=$form->{mainsort}";
203 # escape callback for href
204 $callback = $form->escape($href);
206 my @subtotal_columns = qw(qty sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
208 # Summe von sellprice_total, lastcost_total und marge_total
209 # Durchschnitt von marge_percent
210 my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
212 my %totals = map { $_ => 0 } @total_columns;
213 my %subtotals1 = map { $_ => 0 } @subtotal_columns;
214 my %subtotals2 = map { $_ => 0 } @subtotal_columns;
218 foreach my $ar (@{ $form->{AR} }) {
220 $ar->{price_factor} = 1 unless $ar->{price_factor};
221 # calculate individual sellprice
222 # discount was already accounted for in db sellprice
223 $ar->{sellprice} = $ar->{sellprice} / $ar->{price_factor};
224 $ar->{lastcost} = $ar->{lastcost} / $ar->{price_factor};
225 $ar->{sellprice_total} = $ar->{qty} * $ar->{sellprice};
226 $ar->{lastcost_total} = $ar->{qty} * $ar->{lastcost};
227 # marge_percent wird neu berechnet, da Wert in invoice leer ist (Bug)
228 $ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total}) : 0;
229 # marge_total neu berechnen
230 $ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total} : 0;
231 $ar->{discount} *= 100; # für Ausgabe formatieren, 10% stored as 0.1 in db
233 # Anfangshauptüberschrift
234 if ( $form->{l_headers} eq "Y" && ( $idx == 0 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
237 if ( $form->{mainsort} eq 'parts_id' ) {
238 $headerrow->{description}->{data} = "$ar->{description}";
240 $headerrow->{description}->{data} = "$ar->{name}";
242 $headerrow->{description}->{class} = "listmainsortheader";
243 my $headerrow_set = [ $headerrow ];
244 $report->add_data($headerrow_set);
246 # add empty row after main header
247 # my $emptyheaderrow->{description}->{data} = "";
248 # $emptyheaderrow->{description}->{class} = "listmainsortheader";
249 # my $emptyheaderrow_set = [ $emptyheaderrow ];
250 # $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
253 # subsort überschriften
255 or $ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
256 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} }
260 if ( $form->{subsort} eq 'parts_id' ) {
261 $name = 'description';
262 $headerrow->{description}->{data} = "$ar->{$name}";
265 $headerrow->{description}->{data} = "$ar->{$name}";
267 $headerrow->{description}->{class} = "listsubsortheader";
268 my $headerrow_set = [ $headerrow ];
269 $report->add_data($headerrow_set) if $form->{l_headers} eq "Y";
272 map { $subtotals1{$_} += $ar->{$_};
273 $subtotals2{$_} += $ar->{$_};
276 map { $totals{$_} += $ar->{$_} } @total_columns;
278 $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
279 $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
280 $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
281 $subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
283 # Ertrag prozentual in den Summen: (summe VK - summe Ertrag) / summe VK
284 $subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) : 0;
285 $subtotals2{marge_percent} = $subtotals2{sellprice_total} ? (($subtotals2{sellprice_total} - $subtotals2{lastcost_total}) / $subtotals2{sellprice_total}) : 0;
287 # Ertrag prozentual: (Summe VK betrag - Summe EK betrag) / Summe VK betrag
288 # wird laufend bei jeder Position neu berechnet
289 $totals{marge_percent} = $totals{sellprice_total} ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total} ) : 0;
291 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_total marge_percent);
292 map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total);
296 foreach my $column (@columns) {
298 'data' => $ar->{$column},
299 'align' => $column_alignment{$column},
303 $row->{description}->{class} = 'listsortdescription';
305 $row->{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit')
306 . "&id=" . E($ar->{id}) . "&callback=${callback}";
308 my $row_set = [ $row ];
310 if (($form->{l_subtotal} eq 'Y')
311 && (($idx == (scalar @{ $form->{AR} } - 1)) # last element always has a subtotal
312 || ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'} })
313 || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
314 )) { # if value that is sorted by changes, print subtotal
316 if ( $form->{subsort} eq 'parts_id' ) {
317 $name = 'description';
322 if ($form->{l_subtotal} eq 'Y' ) {
323 push @{ $row_set }, create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, 'listsubsortsubtotal', $ar->{$name}) ;
324 push @{ $row_set }, insert_empty_row();
328 # if mainsort has changed, add mainsort subtotal and empty row
329 if (($form->{l_subtotal} eq 'Y')
330 && (($idx == (scalar @{ $form->{AR} } - 1)) # last element always has a subtotal
331 || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
332 )) { # if value that is sorted by changes, print subtotal
334 if ( $form->{mainsort} eq 'parts_id' ) {
335 $name = 'description';
339 if ($form->{l_subtotal} eq 'Y' ) {
340 push @{ $row_set }, create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listmainsortsubtotal', $ar->{$name});
341 push @{ $row_set }, insert_empty_row();
345 $report->add_data($row_set);
349 if ( $form->{l_total} eq "Y" ) {
350 $report->add_separator();
351 $report->add_data(create_subtotal_row_invoice(\%totals, \@columns, \%column_alignment, \@total_columns, 'listtotal'))
354 $report->generate_with_headers();
355 $main::lxdebug->leave_sub();
359 sub insert_empty_row {
361 $dummyrow->{description}->{data} = "";
362 my $dummyrowset = [ $dummyrow ];
368 sub create_subtotal_row_invoice {
369 $main::lxdebug->enter_sub();
371 my ($totals, $columns, $column_alignment, $subtotal_columns, $class, $name) = @_;
373 my $form = $main::form;
374 my %myconfig = %main::myconfig;
376 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
378 $row->{description}->{data} = "Summe " . $name;
380 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent);
381 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 0) } qw(qty);
382 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);
385 map { $totals->{$_} = 0 } @{ $subtotal_columns };
387 $main::lxdebug->leave_sub();