Projekt

Allgemein

Profil

Herunterladen (24,8 KB) Statistiken
| Zweig: | Markierung: | Revision:
79398e65 Geoffrey Richardson
#=====================================================================
# LX-Office ERP
# Copyright (C) 2004
# Based on SQL-Ledger Version 2.1.9
# Web http://www.lx-office.org
#
#=====================================================================
# SQL-Ledger Accounting
# Copyright (c) 2001
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
f7b15d43 Christian Wittmer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1335, USA.
79398e65 Geoffrey Richardson
#======================================================================
#
# Sales report
#
#======================================================================

use POSIX qw(strftime);
use List::Util qw(sum first);

add49a32 Niclas Zimmermann
use SL::AM;
25000c1e Moritz Bunkus
use SL::DB::Employee;
79398e65 Geoffrey Richardson
use SL::VK;
df4c93ae Geoffrey Richardson
use SL::IS;
79398e65 Geoffrey Richardson
use SL::ReportGenerator;
use Data::Dumper;

require "bin/mozilla/common.pl";
require "bin/mozilla/reportgenerator.pl";

use strict;

sub search_invoice {
$main::lxdebug->enter_sub();
983b4461 Bernd Bleßmann
$main::auth->assert('sales_report');
79398e65 Geoffrey Richardson
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

c17d679a Geoffrey Richardson
my ($customer);
79398e65 Geoffrey Richardson
25000c1e Moritz Bunkus
$::request->layout->add_javascripts("autocomplete_project.js");
79398e65 Geoffrey Richardson
$form->{title} = $locale->text('Sales Report');

25000c1e Moritz Bunkus
$form->get_lists("departments" => "ALL_DEPARTMENTS",
a521b29b Geoffrey Richardson
"business_types" => "ALL_BUSINESS_TYPES",
"salesmen" => "ALL_SALESMEN",
25000c1e Moritz Bunkus
'partsgroup' => 'ALL_PARTSGROUPS');

$form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted;

a521b29b Geoffrey Richardson
$form->{CUSTOM_VARIABLES_IC} = CVar->get_configs('module' => 'IC');
($form->{CUSTOM_VARIABLES_FILTER_CODE_IC},
$form->{CUSTOM_VARIABLES_INCLUSION_CODE_IC}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES_IC},
'include_prefix' => 'l_',
'include_value' => 'Y');

$form->{CUSTOM_VARIABLES_CT} = CVar->get_configs('module' => 'CT');
($form->{CUSTOM_VARIABLES_FILTER_CODE_CT},
$form->{CUSTOM_VARIABLES_INCLUSION_CODE_CT}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES_CT},
'include_prefix' => 'l_',
'include_value' => 'Y');
79398e65 Geoffrey Richardson
$form->header;
print $form->parse_html_template('vk/search_invoice', { %myconfig });

$main::lxdebug->leave_sub();
}

sub invoice_transactions {
$main::lxdebug->enter_sub();

983b4461 Bernd Bleßmann
$main::auth->assert('sales_report');
79398e65 Geoffrey Richardson
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;

my ($callback, $href, @columns);

a521b29b Geoffrey Richardson
# can't currently be configured from report, empty line between main sortings
69480f28 Moritz Bunkus
my $addemptylines = 1;
0c404f20 Moritz Bunkus
f44f1216 Geoffrey Richardson
# don't add empty lines between mainsort subtotals when only subtotal_mainsort is selected
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} ) {
0c404f20 Moritz Bunkus
$addemptylines = 0
f44f1216 Geoffrey Richardson
};
a521b29b Geoffrey Richardson
df4c93ae Geoffrey Richardson
if ( $form->{customer} =~ /--/ ) {
aff48c95 Bernd Bleßmann
# Felddaten kommen aus Dropdownbox
my $dummy;
($dummy, $form->{customer_id}) = split(/--/, $form->{customer});
}
c588d5b4 Geoffrey Richardson
# if $form->{customer} is empty nothing further happens here
79398e65 Geoffrey Richardson
c588d5b4 Geoffrey Richardson
# test for decimalplaces or set to default of 2
79398e65 Geoffrey Richardson
$form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;

a521b29b Geoffrey Richardson
my $cvar_configs_ct = CVar->get_configs('module' => 'CT');
my $cvar_configs_ic = CVar->get_configs('module' => 'IC');

79398e65 Geoffrey Richardson
# report_generator_set_default_sort('transdate', 1);

VK->invoice_transactions(\%myconfig, \%$form);

8516ea3a Moritz Bunkus
a521b29b Geoffrey Richardson
if ( $form->{mainsort} eq 'month' or $form->{subsort} eq 'month' ) {

# Data already comes out of SELECT statement in correct month order, but
# remove whitespaces (month names are padded) and translate them as early
# as possible

foreach (@{ $form->{AR} }) {
$_->{month} =~ s/\s//g;
$_->{month} = $locale->text($_->{month});
};
79398e65 Geoffrey Richardson
};

$form->{title} = $locale->text('Sales Report');

@columns =
eefbbed3 Jan Büren
qw(description invnumber transdate shipvia customernumber customername partnumber partsgroup country business transdate qty parts_unit weight sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent employee salesman);
a521b29b Geoffrey Richardson
my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;

push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;

79398e65 Geoffrey Richardson
14a2c687 Geoffrey Richardson
# pass hidden variables for pdf/csv export
# first with l_ to determine which columns to show
# then with the options for headings (such as transdatefrom, partnumber, ...)
c17d679a Geoffrey Richardson
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),
8516ea3a Moritz Bunkus
"$form->{db}number",
a521b29b Geoffrey Richardson
map({ "cvar_$_->{name}" } @searchable_custom_variables),
409d5d5f Geoffrey Richardson
map { "l_$_" } @columns
a521b29b Geoffrey Richardson
);
79398e65 Geoffrey Richardson
my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
14a2c687 Geoffrey Richardson
# variables are passed as hiddens, e.g.
79398e65 Geoffrey Richardson
# <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">

$href = build_std_url('action=invoice_transactions', grep { $form->{$_} } @hidden_variables);
# href = vk.pl?action=invoice_transactions&l_headers=Y&l_subtotal=Y&l_total=Y&transdatefrom=04.03.2010 ...

my %column_defs = (
'description' => { 'text' => $locale->text('Description'), },
'partnumber' => { 'text' => $locale->text('Part Number'), },
df41380a Geoffrey Richardson
'partsgroup' => { 'text' => $locale->text('Partsgroup'), },
a521b29b Geoffrey Richardson
'country' => { 'text' => $locale->text('Country'), },
'business' => { 'text' => $locale->text('Customer type'), },
'employee' => { 'text' => $locale->text('Employee'), },
'salesman' => { 'text' => $locale->text('Salesperson'), },
79398e65 Geoffrey Richardson
'invnumber' => { 'text' => $locale->text('Invoice Number'), },
'transdate' => { 'text' => $locale->text('Invoice Date'), },
eefbbed3 Jan Büren
'shipvia' => { 'text' => $locale->text('Ship via'), },
79398e65 Geoffrey Richardson
'qty' => { 'text' => $locale->text('Quantity'), },
add49a32 Niclas Zimmermann
'parts_unit' => { 'text' => $locale->text('Base unit'), },
c0318621 Niclas Zimmermann
'weight' => { 'text' => $locale->text('Weight'), },
79398e65 Geoffrey Richardson
'sellprice' => { 'text' => $locale->text('Sales price'), },
'sellprice_total' => { 'text' => $locale->text('Sales net amount'), },
'lastcost_total' => { 'text' => $locale->text('Purchase net amount'), },
'discount' => { 'text' => $locale->text('Discount'), },
'lastcost' => { 'text' => $locale->text('Purchase price'), },
'marge_total' => { 'text' => $locale->text('Sales margin'), },
'marge_percent' => { 'text' => $locale->text('Sales margin %'), },
7e4b86b1 Geoffrey Richardson
'customernumber' => { 'text' => $locale->text('Customer Number'), },
a521b29b Geoffrey Richardson
'customername' => { 'text' => $locale->text('Customer Name'), },
# add 3 more column_defs so we have a translation for top_info_text
'customer' => { 'text' => $locale->text('Customer'), },
'part' => { 'text' => $locale->text('Part'), },
'month' => { 'text' => $locale->text('Month'), },
%column_defs_cvars,
79398e65 Geoffrey Richardson
);
a873249c Moritz Bunkus
5ef8b310 Geoffrey Richardson
map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
79398e65 Geoffrey Richardson
409d5d5f Geoffrey Richardson
my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total parts_unit discount marge_total marge_percent qty weight);

8516ea3a Moritz Bunkus
a521b29b Geoffrey Richardson
# so now the check-box "Description" is only used as switch for part description in invoice-mode
# always fill the column "Description" if we are in Zwischensummenmode
409d5d5f Geoffrey Richardson
$form->{"l_description"} = "Y" if not defined $form->{"l_parts"};;
79398e65 Geoffrey Richardson
map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;

my @options;
a521b29b Geoffrey Richardson
3ffb8503 Moritz Bunkus
push @options, $locale->text('Description') . " : $form->{description}" if $form->{description};
aff48c95 Bernd Bleßmann
push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
3ffb8503 Moritz Bunkus
push @options, $locale->text('Customer Number') . " : $form->{customernumber}" if $form->{customernumber};
14a2c687 Geoffrey Richardson
# TODO: only customer id is passed
c17d679a Geoffrey Richardson
push @options, $locale->text('Department') . " : " . SL::DB::Department->new(id => $form->{department_id})->load->description if $form->{department_id};
3ffb8503 Moritz Bunkus
push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if $form->{invnumber};
push @options, $locale->text('Invoice Date') . " : $form->{invdate}" if $form->{invdate};
eefbbed3 Jan Büren
push @options, $locale->text('Ship via') . " : $form->{shipvia}" if $form->{shipvia};
3ffb8503 Moritz Bunkus
push @options, $locale->text('Part Number') . " : $form->{partnumber}" if $form->{partnumber};
df41380a Geoffrey Richardson
push @options, $locale->text('Partsgroup') . " : " . SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load->partsgroup if $form->{partsgroup_id};
3ffb8503 Moritz Bunkus
push @options, $locale->text('Country') . " : $form->{country}" if $form->{country};
push @options, $locale->text('Employee') . ' : ' . SL::DB::Employee->new(id => $form->{employee_id})->load->name if $form->{employee_id};
push @options, $locale->text('Salesman') . ' : ' . SL::DB::Employee->new(id => $form->{salesman_id})->load->name if $form->{salesman_id};
push @options, $locale->text('Customer type') . ' : ' . SL::DB::Business->new(id => $form->{business_id})->load->description if $form->{business_id};
push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
79398e65 Geoffrey Richardson
my $report = SL::ReportGenerator->new(\%myconfig, $form);

a521b29b Geoffrey Richardson
$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),
79398e65 Geoffrey Richardson
'output_format' => 'HTML',
'title' => $form->{title},
54ddc03b Sven Donath
'attachment_basename' => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
79398e65 Geoffrey Richardson
);
$report->set_options_from_form();
a873249c Moritz Bunkus
$locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
79398e65 Geoffrey Richardson
$report->set_columns(%column_defs);
$report->set_column_order(@columns);

$report->set_export_options('invoice_transactions', @hidden_variables, qw(mainsort sortdir));

409d5d5f Geoffrey Richardson
$report->set_sort_indicator($form->{mainsort}, $form->{sortdir});
79398e65 Geoffrey Richardson
a521b29b Geoffrey Richardson
CVar->add_custom_variables_to_report('module' => 'CT',
'trans_id_field' => 'customerid',
'configs' => $cvar_configs_ct,
'column_defs' => \%column_defs,
'data' => $form->{AR}
);

CVar->add_custom_variables_to_report('module' => 'IC',
'trans_id_field' => 'parts_id',
'configs' => $cvar_configs_ic,
'column_defs' => \%column_defs,
'data' => $form->{AR}
);

69480f28 Moritz Bunkus
my $num_visible_columns = scalar $report->get_visible_columns;
my %empty_row = (
description => {
data => '',
class => 'listrowempty',
colspan => $num_visible_columns,
},
);

79398e65 Geoffrey Richardson
# add sort and escape callback, this one we use for the add sub
$form->{callback} = $href .= "&sort=$form->{mainsort}";

# escape callback for href
$callback = $form->escape($href);

409d5d5f Geoffrey Richardson
my @subtotal_columns = qw(qty weight sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
c588d5b4 Geoffrey Richardson
# Total sum:
# sum of sellprice_total, lastcost_total and marge_total
# average of marge_percent
409d5d5f Geoffrey Richardson
my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
79398e65 Geoffrey Richardson
3ffb8503 Moritz Bunkus
my %totals = map { $_ => 0 } @total_columns;
79398e65 Geoffrey Richardson
my %subtotals1 = map { $_ => 0 } @subtotal_columns;
my %subtotals2 = map { $_ => 0 } @subtotal_columns;

my $idx = 0;

add49a32 Niclas Zimmermann
my $basefactor;
my $all_units = AM->retrieve_all_units();

79398e65 Geoffrey Richardson
foreach my $ar (@{ $form->{AR} }) {
add49a32 Niclas Zimmermann
$basefactor = $all_units->{$ar->{unit}}->{factor} / $all_units->{$ar->{parts_unit}}->{factor};
$basefactor = 1 unless $basefactor;
79398e65 Geoffrey Richardson
$ar->{price_factor} = 1 unless $ar->{price_factor};
14a2c687 Geoffrey Richardson
# calculate individual sellprice, discount is already accounted for in column sellprice in db

# The sellprice total can be calculated from sellprice or fxsellprice (the
# value that was actually entered in the sellprice field and is always
32eacf36 Geoffrey Richardson
# stored separately). However, for fxsellprice this method only works when
14a2c687 Geoffrey Richardson
# the tax is not included, because otherwise fxsellprice includes the tax
# and there is no simple way to extract the tax rate of the article from
2f6a11fb Bernd Bleßmann
# the big query.
14a2c687 Geoffrey Richardson
#
# Using fxsellprice is potentially more accurate (certainly for tax
# included), because we can use the same method as is used while the
# invoice is generated.
#
# sellprice however has already been converted to the net value (but
# rounded in the process, which leads to rounding errors when calculating
# the linetotal from the rounded sellprice in the report. These rounding
# errors can quickly amount to several cents when qty is large)
#
# For calculating sellprice_total from fxsellprice, you would use:
# sellprice_total_including_tax = qty * fxsellprice * (1-discount) / price_factor * exchangerate
# $ar->{sellprice_total_including_tax} = $form->round_amount( $ar->{qty} * ( $ar->{fxsellprice} * ( 1 - $ar->{discount} ) ) / $ar->{price_factor}, $form->{"decimalplaces"});

ba003bf8 Geoffrey Richardson
my $sellprice_orig = $ar->{sellprice};
my $qty_orig = $ar->{qty};
# adjust sellprice so it reflects the unit sellprice according to price_factor and basefactor
2f6a11fb Bernd Bleßmann
$ar->{sellprice} = $ar->{sellprice} / $ar->{price_factor} / $basefactor;
ba003bf8 Geoffrey Richardson
# for sellprice_total use the original amounts
$ar->{sellprice_total} = $form->round_amount( $qty_orig * $sellprice_orig / $ar->{price_factor}, $form->{"decimalplaces"});
14a2c687 Geoffrey Richardson
ba003bf8 Geoffrey Richardson
my $lastcost_orig = $ar->{lastcost};
e7f0039c Niclas Zimmermann
$ar->{lastcost} = $ar->{lastcost} / $ar->{price_factor} / $basefactor;
ba003bf8 Geoffrey Richardson
$ar->{lastcost_total} = $form->round_amount( $qty_orig * $lastcost_orig / $ar->{price_factor}, $form->{"decimalplaces"});
14a2c687 Geoffrey Richardson
# marge_percent is recalculated, because the value in invoice used to be empty
a521b29b Geoffrey Richardson
$ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total} * 100) : 0;
14a2c687 Geoffrey Richardson
# also recalculate marge_total
79398e65 Geoffrey Richardson
$ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total} : 0;
14a2c687 Geoffrey Richardson
$ar->{discount} *= 100; # format discount value for output, 10% is stored as 0.1 in db
79398e65 Geoffrey Richardson
ba003bf8 Geoffrey Richardson
# adjust qty to reflect the qty of the base unit
4c438c0a Niclas Zimmermann
$ar->{qty} *= $basefactor;

ba003bf8 Geoffrey Richardson
# now $ar->{sellprice} * $ar->{qty} should be the same as $qty_orig * $ar->{qty}

14a2c687 Geoffrey Richardson
# weight is the still the weight per part, but here we want the total weight
4c438c0a Niclas Zimmermann
$ar->{weight} *= $ar->{qty};

14a2c687 Geoffrey Richardson
# Main header
if ( $form->{l_headers_mainsort} eq "Y" && ( $idx == 1 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
3ffb8503 Moritz Bunkus
my $headerrow = {
# use $emptyname for mainsort header if mainsort is empty
69480f28 Moritz Bunkus
data => $ar->{$form->{'mainsort'}} || $locale->text('empty'),
class => "listmainsortheader",
colspan => $num_visible_columns,
79398e65 Geoffrey Richardson
};
3ffb8503 Moritz Bunkus
$report->add_data([ { description => $headerrow } ]);
79398e65 Geoffrey Richardson
# add empty row after main header
a873249c Moritz Bunkus
# my $emptyheaderrow->{description}->{data} = "";
79398e65 Geoffrey Richardson
# $emptyheaderrow->{description}->{class} = "listmainsortheader";
# my $emptyheaderrow_set = [ $emptyheaderrow ];
a873249c Moritz Bunkus
# $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
79398e65 Geoffrey Richardson
};

14a2c687 Geoffrey Richardson
# subsort headers
3ffb8503 Moritz Bunkus
# special case: subsort headers only makes (aesthetical) sense if we show individual parts
if (( $idx == 0
or $ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} })
&& ($form->{l_headers_subsort} eq "Y")
&& $form->{l_parts}
79398e65 Geoffrey Richardson
) {
3ffb8503 Moritz Bunkus
my $headerrow = {
# if subsort name is defined, use that name in header, otherwise use $emptyname
69480f28 Moritz Bunkus
data => $ar->{$form->{'subsort'}} || $locale->text('empty'),
class => "listsubsortheader",
colspan => $num_visible_columns,
79398e65 Geoffrey Richardson
};
3ffb8503 Moritz Bunkus
$report->add_data([ { description => $headerrow } ]);
79398e65 Geoffrey Richardson
};

map { $subtotals1{$_} += $ar->{$_};
$subtotals2{$_} += $ar->{$_};
} @subtotal_columns;
a873249c Moritz Bunkus
map { $totals{$_} += $ar->{$_} } @total_columns;
79398e65 Geoffrey Richardson
7b9e6d68 Geoffrey Richardson
if ( $subtotals1{qty} != 0 ) {
# calculate averages for subtotals1 and subtotals2
# credited positions reduce both total and qty and thus don't influence average prices
$subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty};
3ffb8503 Moritz Bunkus
$subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty};
7b9e6d68 Geoffrey Richardson
} else {
# qty is zero, so we have a special case where each position in subtotal
# group has a corresponding credit note so that the total qty is zero in
# this case we also want the total amounts to be zero, so overwrite them,
# rather than leaving the last value in sellprice/lastcost

$subtotals1{sellprice} = 0;
3ffb8503 Moritz Bunkus
$subtotals1{lastcost} = 0;
7b9e6d68 Geoffrey Richardson
};

if ( $subtotals2{qty} != 0 ) {
$subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty};
3ffb8503 Moritz Bunkus
$subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty};
7b9e6d68 Geoffrey Richardson
} else {
$subtotals2{sellprice} = 0;
3ffb8503 Moritz Bunkus
$subtotals2{lastcost} = 0;
7b9e6d68 Geoffrey Richardson
};
79398e65 Geoffrey Richardson
14a2c687 Geoffrey Richardson
# marge percent as sums: (sum VK - sum Ertrag) / sum VK
a521b29b Geoffrey Richardson
$subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) * 100 : 0;
$subtotals2{marge_percent} = $subtotals2{sellprice_total} ? (($subtotals2{sellprice_total} - $subtotals2{lastcost_total}) / $subtotals2{sellprice_total}) *100 : 0;
79398e65 Geoffrey Richardson
14a2c687 Geoffrey Richardson
# total marge percent: (sum VK betrag - sum EK betrag) / sum VK betrag
# is recalculated after each position
a521b29b Geoffrey Richardson
$totals{marge_percent} = $totals{sellprice_total} ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total} ) * 100 : 0;
79398e65 Geoffrey Richardson
6ad46272 Niclas Zimmermann
map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_percent qty);
4c438c0a Niclas Zimmermann
map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 3) } qw(weight);
6ad46272 Niclas Zimmermann
map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total marge_total);
79398e65 Geoffrey Richardson
14a2c687 Geoffrey Richardson
# only show individual lines when l_parts is set, this is useful, if you
# only want to see subtotals and totals

69480f28 Moritz Bunkus
if ($form->{l_parts}) {
my %row = (
map { ($_ => { data => $ar->{$_}, align => $column_alignment{$_} }) } @columns
);

$row{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit') . "&id=" . E($ar->{id}) . "&callback=${callback}";

17cac1f6 Geoffrey Richardson
# use partdescription according to invoice in article mode
$row{description}->{data} = $ar->{invoice_description};

69480f28 Moritz Bunkus
$report->add_data(\%row);
}
79398e65 Geoffrey Richardson
14a2c687 Geoffrey Richardson
# choosing l_subtotal doesn't make a distinction between mainsort and subsort
# if l_subtotal_mainsort is not selected l_subtotal_subsort isn't run either
69480f28 Moritz Bunkus
if ( ($form->{l_subtotal_mainsort} eq 'Y')
&& ($form->{l_subtotal_subsort} eq 'Y')
79398e65 Geoffrey Richardson
&& (($idx == (scalar @{ $form->{AR} } - 1)) # last element always has a subtotal
|| ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'} })
|| ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
)) { # if value that is sorted by changes, print subtotal
a873249c Moritz Bunkus
409d5d5f Geoffrey Richardson
$report->add_data(create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, $form->{l_parts} ? 'listsubtotal' : undef, $ar->{ $form->{'subsort'} }));
$report->add_data({ %empty_row }) if $form->{l_parts} and $addemptylines;
}
79398e65 Geoffrey Richardson
a521b29b Geoffrey Richardson
# if last mainsort is reached or mainsort has changed, add mainsort subtotal and empty row
69480f28 Moritz Bunkus
if ( ($form->{l_subtotal_mainsort} eq 'Y')
&& ($form->{l_subtotal_mainsort} eq 'Y')
&& ($form->{mainsort} ne $form->{subsort})
79398e65 Geoffrey Richardson
&& (($idx == (scalar @{ $form->{AR} } - 1)) # last element always has a subtotal
|| ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
)) { # if value that is sorted by changes, print subtotal
14a2c687 Geoffrey Richardson
# subtotal is overridden if mainsort and subsort are equal, don't print
a521b29b Geoffrey Richardson
# subtotal line even if it is selected
69480f28 Moritz Bunkus
$report->add_data(create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal', $ar->{$form->{mainsort}}));
$report->add_data({ %empty_row }) if $addemptylines; # insert empty row after mainsort
79398e65 Geoffrey Richardson
}
a873249c Moritz Bunkus
79398e65 Geoffrey Richardson
$idx++;
}
if ( $form->{l_total} eq "Y" ) {
$report->add_separator();
a521b29b Geoffrey Richardson
$report->add_data(create_subtotal_row_invoice(\%totals, \@columns, \%column_alignment, \@total_columns, 'listtotal', 'l_total'))
79398e65 Geoffrey Richardson
};
5ef8b310 Geoffrey Richardson
409d5d5f Geoffrey Richardson
$report->generate_with_headers();
79398e65 Geoffrey Richardson
$main::lxdebug->leave_sub();
}

sub create_subtotal_row_invoice {
$main::lxdebug->enter_sub();

my ($totals, $columns, $column_alignment, $subtotal_columns, $class, $name) = @_;

my $form = $main::form;
my %myconfig = %main::myconfig;
a521b29b Geoffrey Richardson
my $locale = $main::locale;
79398e65 Geoffrey Richardson
69480f28 Moritz Bunkus
my $row = { map { $_ => { data => '', class => $class, align => $column_alignment->{$_}, } } @{ $columns } };
a873249c Moritz Bunkus
a521b29b Geoffrey Richardson
# set name as "empty" if no value is given, except if we are dealing with the
# absolute total, then just write "Total sum"
# here we assume that no name will be called 'l_total'
$name = $locale->text('empty') unless $name;
if ( $name eq 'l_total' ) {
$row->{description}->{data} = $locale->text('Total sum');
} else {
$row->{description}->{data} = $locale->text('Total') . ' ' . $name;
};
79398e65 Geoffrey Richardson
4c438c0a Niclas Zimmermann
map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent qty);
map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 3) } qw(weight);
409d5d5f Geoffrey Richardson
map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);

79398e65 Geoffrey Richardson
map { $totals->{$_} = 0 } @{ $subtotal_columns };

$main::lxdebug->leave_sub();

return $row;
}

1;