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   $form->{customer} = $form->unescape($form->{customer});
 
  92   if ( $form->{customer} =~ /--/ ) {
 
  93     # Felddaten kommen aus Dropdownbox
 
  94     ($form->{customername}, $form->{customer_id}) = split(/--/, $form->{customer});
 
  96     # Felddaten kommen aus Freitextfeld
 
  98     # check_name wird mit no_select => 1 ausgeführt, ist die Abfrage nicht eindeutig kommt ein Fehler
 
  99     # und die Abfrage muß erneut ausgeführt werden
 
 101     # Ohne no_select kommt bei Auswahl des Kunden ein Aufruf von update der ins
 
 102     # Nichts führt, daher diese Zwischenlösung
 
 104     &check_name('customer', no_select => 1);
 
 106     # $form->{customer_id} wurde schon von check_name gesetzt
 
 107     $form->{customername} = $form->{customer};
 
 110   # decimalplaces überprüfen oder auf Default 2 setzen
 
 111   $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
 
 113 #  report_generator_set_default_sort('transdate', 1);
 
 115   VK->invoice_transactions(\%myconfig, \%$form);
 
 117   # anhand von radio button die Sortierreihenfolge festlegen
 
 118   if ($form->{sortby} eq 'artikelsort') {
 
 119     $form->{'mainsort'} = 'parts_id';
 
 120     $form->{'subsort'}  = 'name';
 
 122     $form->{'mainsort'} = 'name';
 
 123     $form->{'subsort'}  = 'parts_id';
 
 126   $form->{title} = $locale->text('Sales Report');
 
 129     qw(description invnumber partnumber parts_id transdate qty unit sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent);
 
 131   # hidden variables für pdf/csv export übergeben
 
 132   # einmal mit l_ um zu bestimmen welche Spalten ausgegeben werden sollen
 
 133   # einmal optionen für die Überschrift (z.B. transdatefrom, partnumber, ...)
 
 134   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);
 
 135   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
 
 136   # Variablen werden dann als Hidden Variable mitgegeben, z.B.
 
 137   # <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">
 
 139   $href = build_std_url('action=invoice_transactions', grep { $form->{$_} } @hidden_variables);
 
 140   # href = vk.pl?action=invoice_transactions&l_headers=Y&l_subtotal=Y&l_total=Y&transdatefrom=04.03.2010 ...
 
 143     'description'             => { 'text' => $locale->text('Description'), },
 
 144     'partnumber'              => { 'text' => $locale->text('Part Number'), },
 
 145     'invnumber'               => { 'text' => $locale->text('Invoice Number'), },
 
 146     'transdate'               => { 'text' => $locale->text('Invoice Date'), },
 
 147     'qty'                     => { 'text' => $locale->text('Quantity'), },
 
 148     'unit'                    => { 'text' => $locale->text('Unit'), },
 
 149     'sellprice'               => { 'text' => $locale->text('Sales price'), },
 
 150     'sellprice_total'         => { 'text' => $locale->text('Sales net amount'), },
 
 151     'lastcost_total'          => { 'text' => $locale->text('Purchase net amount'), },
 
 152     'discount'                => { 'text' => $locale->text('Discount'), },
 
 153     'lastcost'                => { 'text' => $locale->text('Purchase price'), },
 
 154     'marge_total'             => { 'text' => $locale->text('Sales margin'), },
 
 155     'marge_percent'           => { 'text' => $locale->text('Sales margin %'), },
 
 158   my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total unit discount marge_total marge_percent qty);
 
 160   $form->{"l_type"} = "Y";
 
 161   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 165   if ($form->{description}) {
 
 166     push @options, $locale->text('Description') . " : $form->{description}";
 
 168   if ($form->{customer}) {
 
 169     push @options, $locale->text('Customer') . " : $form->{customername}";
 
 171   if ($form->{department}) {
 
 172     my ($department) = split /--/, $form->{department};
 
 173     push @options, $locale->text('Department') . " : $department";
 
 175   if ($form->{invnumber}) {
 
 176     push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
 
 178   if ($form->{invdate}) {
 
 179     push @options, $locale->text('Invoice Date') . " : $form->{invdate}";
 
 181   if ($form->{partnumber}) {
 
 182     push @options, $locale->text('Part Number') . " : $form->{partnumber}";
 
 184   if ($form->{ordnumber}) {
 
 185     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
 
 187   if ($form->{notes}) {
 
 188     push @options, $locale->text('Notes') . " : $form->{notes}";
 
 190   if ($form->{transaction_description}) {
 
 191     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
 
 193   if ($form->{transdatefrom}) {
 
 194     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
 
 196   if ($form->{transdateto}) {
 
 197     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
 
 200   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 202   $report->set_options('top_info_text'        => join("\n", @options),
 
 203                        'output_format'        => 'HTML',
 
 204                        'title'                => $form->{title},
 
 205                        'attachment_basename'  => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
 
 207   $report->set_options_from_form();
 
 208   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 210   $report->set_columns(%column_defs);
 
 211   $report->set_column_order(@columns);
 
 213   $report->set_export_options('invoice_transactions', @hidden_variables, qw(mainsort sortdir));
 
 215   $report->set_sort_indicator($form->{mainsort}, $form->{sortdir});
 
 217   # add sort and escape callback, this one we use for the add sub
 
 218   $form->{callback} = $href .= "&sort=$form->{mainsort}";
 
 220   # escape callback for href
 
 221   $callback = $form->escape($href);
 
 223   my @subtotal_columns = qw(qty sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
 
 225   # Summe von sellprice_total, lastcost_total und marge_total
 
 226   # Durchschnitt von marge_percent
 
 227   my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
 
 229   my %totals    = map { $_ => 0 } @total_columns;
 
 230   my %subtotals1 = map { $_ => 0 } @subtotal_columns;
 
 231   my %subtotals2 = map { $_ => 0 } @subtotal_columns;
 
 235   foreach my $ar (@{ $form->{AR} }) {
 
 237     $ar->{price_factor} = 1 unless $ar->{price_factor};
 
 238     # calculate individual sellprice
 
 239     # discount was already accounted for in db sellprice
 
 240     $ar->{sellprice} = $ar->{sellprice} / $ar->{price_factor};
 
 241     $ar->{lastcost} = $ar->{lastcost} / $ar->{price_factor};
 
 242     $ar->{sellprice_total} = $ar->{qty} * $ar->{sellprice};
 
 243     $ar->{lastcost_total}  = $ar->{qty} * $ar->{lastcost};
 
 244     # marge_percent wird neu berechnet, da Wert in invoice leer ist (Bug)
 
 245     $ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total}) : 0;
 
 246     # marge_total neu berechnen
 
 247     $ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total}  : 0;
 
 248     $ar->{discount} *= 100;  # für Ausgabe formatieren, 10% stored as 0.1 in db
 
 250     # Anfangshauptüberschrift
 
 251     if ( $form->{l_headers} eq "Y" && ( $idx == 0 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
 
 254       if ( $form->{mainsort} eq 'parts_id' ) {
 
 255         $headerrow->{description}->{data} = "$ar->{description}";
 
 257         $headerrow->{description}->{data} = "$ar->{name}";
 
 259       $headerrow->{description}->{class} = "listmainsortheader";
 
 260       my $headerrow_set = [ $headerrow ];
 
 261       $report->add_data($headerrow_set);
 
 263       # add empty row after main header
 
 264 #      my $emptyheaderrow->{description}->{data} = "";
 
 265 #      $emptyheaderrow->{description}->{class} = "listmainsortheader";
 
 266 #      my $emptyheaderrow_set = [ $emptyheaderrow ];
 
 267 #      $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
 
 270     # subsort überschriften
 
 272       or $ar->{ $form->{'subsort'} }  ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
 
 273       or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} }
 
 277       if ( $form->{subsort} eq 'parts_id' ) {
 
 278         $name = 'description';
 
 279         $headerrow->{description}->{data} = "$ar->{$name}";
 
 282         $headerrow->{description}->{data} = "$ar->{$name}";
 
 284       $headerrow->{description}->{class} = "listsubsortheader";
 
 285       my $headerrow_set = [ $headerrow ];
 
 286       $report->add_data($headerrow_set) if $form->{l_headers} eq "Y";
 
 289     map { $subtotals1{$_} += $ar->{$_};
 
 290           $subtotals2{$_} += $ar->{$_};
 
 293     map { $totals{$_}    += $ar->{$_} } @total_columns;
 
 295     $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
 
 296     $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
 
 297     $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
 
 298     $subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
 
 300     # Ertrag prozentual in den Summen: (summe VK - summe Ertrag) / summe VK
 
 301     $subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) : 0;
 
 302     $subtotals2{marge_percent} = $subtotals2{sellprice_total} ? (($subtotals2{sellprice_total} - $subtotals2{lastcost_total}) / $subtotals2{sellprice_total}) : 0;
 
 304     # Ertrag prozentual:  (Summe VK betrag - Summe EK betrag) / Summe VK betrag
 
 305     # wird laufend bei jeder Position neu berechnet
 
 306     $totals{marge_percent}    = $totals{sellprice_total}    ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total}   ) : 0;
 
 308     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_total marge_percent);
 
 309     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total);
 
 313     foreach my $column (@columns) {
 
 315         'data'  => $ar->{$column},
 
 316         'align' => $column_alignment{$column},
 
 320    $row->{description}->{class} = 'listsortdescription';
 
 322     $row->{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit')
 
 323       . "&id=" . E($ar->{id}) . "&callback=${callback}";
 
 325     my $row_set = [ $row ];
 
 327     if (($form->{l_subtotal} eq 'Y')
 
 328         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
 
 329           || ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'}   })
 
 330           || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
 
 331           )) {   # if value that is sorted by changes, print subtotal
 
 333       if ( $form->{subsort} eq 'parts_id' ) {
 
 334         $name = 'description';
 
 339       if ($form->{l_subtotal} eq 'Y' ) {
 
 340         push @{ $row_set }, create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, 'listsubsortsubtotal', $ar->{$name}) ;
 
 341         push @{ $row_set }, insert_empty_row();
 
 345     # if mainsort has changed, add mainsort subtotal and empty row
 
 346     if (($form->{l_subtotal} eq 'Y')
 
 347         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
 
 348             || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
 
 349             )) {   # if value that is sorted by changes, print subtotal
 
 351       if ( $form->{mainsort} eq 'parts_id' ) {
 
 352         $name = 'description';
 
 356       if ($form->{l_subtotal} eq 'Y' ) {
 
 357         push @{ $row_set }, create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listmainsortsubtotal', $ar->{$name});
 
 358         push @{ $row_set }, insert_empty_row();
 
 362     $report->add_data($row_set);
 
 366   if ( $form->{l_total} eq "Y" ) {
 
 367     $report->add_separator();
 
 368     $report->add_data(create_subtotal_row_invoice(\%totals, \@columns, \%column_alignment, \@total_columns, 'listtotal'))
 
 371   $report->generate_with_headers();
 
 372   $main::lxdebug->leave_sub();
 
 376 sub insert_empty_row {
 
 378     $dummyrow->{description}->{data} = "";
 
 379     my $dummyrowset = [ $dummyrow ];
 
 385 sub create_subtotal_row_invoice {
 
 386   $main::lxdebug->enter_sub();
 
 388   my ($totals, $columns, $column_alignment, $subtotal_columns, $class, $name) = @_;
 
 390   my $form     = $main::form;
 
 391   my %myconfig = %main::myconfig;
 
 393   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 395   $row->{description}->{data} = "Summe " . $name;
 
 397   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent);
 
 398   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 0) } qw(qty);
 
 399   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);
 
 402   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 404   $main::lxdebug->leave_sub();