Projekt

Allgemein

Profil

Herunterladen (38,9 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) 2001
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
# Contributors:
#
# 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.
#======================================================================
#
# Accounts Receivable module backend routines
#
#======================================================================

package AR;

use Data::Dumper;
use SL::DATEV qw(:CONSTANTS);
use SL::DBUtils;
use SL::DB::Draft;
use SL::IO;
use SL::MoreCommon;
use SL::DB::Default;
use SL::DB::Invoice;
use SL::DB::EmailJournal;
use SL::DB::ValidityToken;
use SL::TransNumber;
use SL::Util qw(trim);
use SL::DB;

use strict;

sub post_transaction {
my ($self, $myconfig, $form, $provided_dbh, %params) = @_;
$main::lxdebug->enter_sub();

my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form, $provided_dbh, %params);

$::lxdebug->leave_sub;
return $rc;
}

sub _post_transaction {
my ($self, $myconfig, $form, $provided_dbh, %params) = @_;

my $validity_token;
if (!$form->{id}) {
$validity_token = SL::DB::Manager::ValidityToken->fetch_valid_token(
scope => SL::DB::ValidityToken::SCOPE_SALES_INVOICE_POST(),
token => $form->{form_validity_token},
);

die $::locale->text('The form is not valid anymore.') if !$validity_token;
}

my $payments_only = $params{payments_only};

my ($query, $sth, $null, $taxrate, $amount, $tax);
my $exchangerate = 0;
my $i;

my @values;

my $dbh = $provided_dbh || SL::DB->client->dbh;

# if we have an id delete old records else make one
if (!$payments_only) {
if ($form->{id}) {
# delete detail records
$query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
do_query($form, $dbh, $query, $form->{id});

} else {
$query = qq|SELECT nextval('glid')|;
($form->{id}) = selectrow_query($form, $dbh, $query);
$query = qq|INSERT INTO ar (id, invnumber, employee_id, currency_id, taxzone_id) VALUES (?, 'dummy', ?, (SELECT id FROM currencies WHERE name=?), (SELECT taxzone_id FROM customer WHERE id = ?))|;
do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}, $form->{currency}, $form->{customer_id});
if (!$form->{invnumber}) {
my $trans_number = SL::TransNumber->new(type => 'invoice', dbh => $dbh, number => $form->{partnumber}, id => $form->{id});
$form->{invnumber} = $trans_number->create_unique;
}
}
}

$form->{defaultcurrency} = $form->get_default_currency($myconfig);
# check default or record exchangerate
if ($form->{currency} eq $form->{defaultcurrency}) {
$form->{exchangerate} = 1;
} else {
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
$form->{exchangerate} = $form->parse_amount($myconfig, $form->{exchangerate}, 5);

# if default exchangerate is not defined, define one
unless ($exchangerate) {
$form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
# delete records exchangerate -> if user sets new invdate for record
$query = qq|UPDATE ar set exchangerate = NULL where id = ?|;
do_query($form, $dbh, $query, $form->{"id"});
}
# update record exchangerate, if the default is set and differs from current
if ($exchangerate && ($form->{exchangerate} != $exchangerate)) {
$form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
$form->{exchangerate}, 0, $form->{id}, 'ar');
}
}

# get the charts selected
$form->{AR_amounts}{"amount_$_"} = $form->{"AR_amount_chart_id_$_"} for (1 .. $form->{rowcount});

$form->{tax} = 0; # is this still needed?

# main calculation of rowcount loop inside Form method, amount_$i and tax_$i get formatted
$form->{taxincluded} = 0 unless $form->{taxincluded};
(