Projekt

Allgemein

Profil

Herunterladen (67,5 KB) Statistiken
| Zweig: | Markierung: | Revision:
#=====================================================================
# 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) 1998-2002
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
# Contributors: Benjamin Lee <benjaminlee@consultant.com>
#
# 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
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1335, USA.
#======================================================================
#
# backend code for reports
#
#======================================================================

package RP;

use SL::DBUtils;
use Data::Dumper;
use SL::DB::Helper::AccountingPeriod qw(get_balance_starting_date);
use List::Util qw(sum);
use List::UtilsBy qw(partition_by sort_by);
use SL::DB;

# use warnings;
use strict;

# new implementation of balance sheet
# readme!
#
# stuff missing from the original implementation:
# - bold stuff
# - subdescription
# - proper testing for heading charts
# - transmission from $form to TMPL realm is not as clear as i'd like

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

my ($self) = @_;

my $myconfig = \%main::myconfig;
my $form = $main::form;
my $dbh = $::form->get_standard_dbh;

my $last_period = 0;
my @categories = qw(A C L Q);

# if there are any dates construct a where
if ($form->{asofdate}) {
$form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
}

# get starting date for calculating balance
$form->{this_startdate} = $self->get_balance_starting_date($form->{asofdate});

get_accounts($dbh, $last_period, $form->{this_startdate}, $form->{asofdate}, $form, \@categories);

# if there are any compare dates
if ($form->{compareasofdate}) {
$last_period = 1;

$form->{last_startdate} = $self->get_balance_starting_date($form->{compareasofdate});

get_accounts($dbh, $last_period, $form->{last_startdate} , $form->{compareasofdate}, $form, \@categories);
$form->{last_period} = conv_dateq($form->{compareasofdate});
}

# now we got $form->{A}{accno}{ } assets
# and $form->{L}{accno}{ } liabilities
# and $form->{Q}{accno}{ } equity
# build asset accounts

my %account = ('A' => { 'ml' => -1 },
'L' => { 'ml' => 1 },
'Q' => { 'ml' => 1 });

my $TMPL_DATA = {};

foreach my $category (grep { !/C/ } @categories) {

$TMPL_DATA->{$category} = [];
my $ml = $account{$category}{ml};

foreach my $key (sort keys %{ $form->{$category} }) {

my $row = { %{ $form->{$category}{$key} } };

# if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
# header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
if ($row->{charttype} eq "H") {
if ($account{$category}{subtotal} && $form->{l_subtotal}) {
$row->{subdescription} = $account{$category}{subdescription};
$row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
$row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
}

$row->{subheader} = 1;
$account{$category}{subthis} = $row->{this};
$account{$category}{sublast} = $row->{last};
$account{$category}{subdescription} = $row->{description};
$account{$category}{subtotal} = 1;

$row->{this} = 0;
$row->{last} = 0;

next unless $form->{l_heading};
}

for my $period (qw(this last)) {
next if ($period eq 'last' && !$last_period);
# only add assets
$row->{$period} *= $ml;
}

push @{ $TMPL_DATA->{$category} }, $row;
} # foreach

# resolve heading/subtotal
if ($account{$category}{subtotal} && $form->{l_subtotal}) {
$TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
$TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
$TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
}

$TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
$TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
}

for my $period (qw(this last)) {
next if ($period eq 'last' && !$last_period);

$form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
$TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period};
$TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
}
$form->{E}{description}='nicht verbuchter Gewinn/Verlust';
push @{ $TMPL_DATA->{Q} }, $form->{E};

$main::lxdebug->leave_sub();

return $TMPL_DATA;
}

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

my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;

my ($null, $department_id) = split /--/, $form->{department};

my $query;
my $dpt_where = '';
my $dpt_where_without_arapgl = '';
my $project = '';
my $where = "1 = 1";
my $glwhere = "";
my $subwhere = "";
my $item;
my $sth;
my $dec = $form->{decimalplaces};

my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;

# get headings
$query =
qq|SELECT c.accno, c.description, c.category
FROM chart c
WHERE (c.charttype = 'H')
$category
ORDER by c.accno|;

$sth = prepare_execute_query($form, $dbh, $query);

my @headingaccounts = ();
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
$form->{ $ref->{category} }{ $ref->{accno} }{description} =
"$ref->{description}";
$form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
$form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};

push @headingaccounts, $ref->{accno};
}

$sth->finish;

# filter for opening and closing bookings
# if l_ob is selected l_cb is always ignored
if ( $last_period ) {
# ob/cb-settings for "compared to" balance
if ( $form->{l_ob_compared} ) {
$where .= ' AND ac.ob_transaction is true '
} elsif ( not $form->{l_cb_compared} ) {
$where .= ' AND ac.cb_transaction is false ';
};
} else {
# ob/cb-settings for "as of" balance
if ( $form->{l_ob} ) {
$where .= ' AND ac.ob_transaction is true '
} elsif ( not $form->{l_cb} ) {
$where .= ' AND ac.cb_transaction is false ';
};
};


if ($fromdate