|
#=====================================================================
|
|
# 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.
|
|
#======================================================================
|
|
#
|
|
# General ledger backend code
|
|
#
|
|
# CHANGE LOG:
|
|
# DS. 2000-07-04 Created
|
|
# DS. 2001-06-12 Changed relations from accno to chart_id
|
|
#
|
|
#======================================================================
|
|
|
|
package GL;
|
|
|
|
use List::Util qw(first);
|
|
|
|
use Data::Dumper;
|
|
use SL::DATEV qw(:CONSTANTS);
|
|
use SL::DBUtils;
|
|
use SL::DB::Chart;
|
|
use SL::DB::Draft;
|
|
use SL::Util qw(trim);
|
|
use SL::DB;
|
|
|
|
use strict;
|
|
|
|
sub delete_transaction {
|
|
my ($self, $myconfig, $form) = @_;
|
|
$main::lxdebug->enter_sub();
|
|
|
|
SL::DB->client->with_transaction(sub {
|
|
do_query($form, SL::DB->client->dbh, qq|DELETE FROM gl WHERE id = ?|, conv_i($form->{id}));
|
|
1;
|
|
}) or do { die SL::DB->client->error };
|
|
|
|
$main::lxdebug->leave_sub();
|
|
}
|
|
|
|
sub post_transaction {
|
|
my ($self, $myconfig, $form) = @_;
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form);
|
|
|
|
$::lxdebug->leave_sub;
|
|
return $rc;
|
|
}
|
|
|
|
sub _post_transaction {
|
|
my ($self, $myconfig, $form) = @_;
|
|
$main::lxdebug->enter_sub();
|
|
|
|
my ($debit, $credit) = (0, 0);
|
|
my $project_id;
|
|
|
|
my $i;
|
|
|
|
my $dbh = SL::DB->client->dbh;
|
|
|
|
# post the transaction
|
|
# make up a unique handle and store in reference field
|
|
# then retrieve the record based on the unique handle to get the id
|
|
# replace the reference field with the actual variable
|
|
# add records to acc_trans
|
|
|
|
# if there is a $form->{id} replace the old transaction
|
|
# delete all acc_trans entries and add the new ones
|
|
|
|
if (!$form->{taxincluded}) {
|
|
$form->{taxincluded} = 0;
|
|
}
|
|
|
|
my ($query, $sth, @values, $taxkey, $rate, $posted);
|
|
|
|
if ($form->{id}) {
|
|
|
|
# delete individual transactions
|
|
$query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
|
|
@values = (conv_i($form->{id}));
|
|
do_query($form, $dbh, $query, @values);
|
|
|
|
} else {
|
|
$query = qq|SELECT nextval('glid')|;
|
|
($form->{id}) = selectrow_query($form, $dbh, $query);
|
|
|
|
$query =
|
|
qq|INSERT INTO gl (id, employee_id) | .
|
|
qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
|
|
@values = ($form->{id}, $::myconfig{login});
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
|
|
$form->{ob_transaction} *= 1;
|
|
$form->{cb_transaction} *= 1;
|
|
|
|
$query =
|
|
qq|UPDATE gl SET
|
|
reference = ?, description = ?, notes = ?,
|
|
transdate = ?, deliverydate = ?, tax_point = ?, department_id = ?, taxincluded = ?,
|
|
storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
|
|
WHERE id = ?|;
|
|
|
|
@values = ($form->{reference}, $form->{description}, $form->{notes},
|
|
conv_date($form->{transdate}), conv_date($form->{deliverydate}), conv_date($form->{tax_point}), conv_i($form->{department_id}), $form->{taxincluded} ? 't' : 'f',
|
|
$form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
|
|
conv_i($form->{id}));
|
|
do_query($form, $dbh, $query, @values);
|
|
|
|
# insert acc_trans transactions
|
|
for $i (1 .. $form->{rowcount}) {
|
|
($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
|
|
if ($form->{"tax_id_$i"} ne "") {
|
|
$query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
|
|
($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
|
|
}
|
|
|
|
my $amount = 0;
|
|
my $debit = $form->{"debit_$i"};
|
|
my $credit = $form->{"credit_$i"};
|
|
my $tax = $form->{"tax_$i"};
|
|
|
|
if ($credit) {
|
|
$amount = $credit;
|
|
$posted = 0;
|
|
}
|
|
if ($debit) {
|
|
$amount = $debit * -1;
|
|
$tax = $tax * -1;
|
|
$posted = 0;
|
|
}
|
|
|
|
$project_id = conv_i($form->{"project_id_$i"});
|
|
|
|
# if there is an amount, add the record
|
|
if ($amount != 0) {
|
|
$query =
|
|
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
|
|
source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id, chart_link)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE id = ?))|;
|
|
@values = (conv_i($form->{id}), $form->{"accno_id_$i"}, $amount, conv_date($form->{transdate}),
|
|
$form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f', conv_i($form->{"tax_id_$i"}), $form->{"accno_id_$i"});
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
|
|
if ($tax != 0) {
|
|
# add taxentry
|
|
$query =
|
|
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
|
|
source, memo, project_id, taxkey, tax_id, chart_link)
|
|
VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
|
|
?, ?, ?, ?, ?, ?, ?, (SELECT link
|
|
FROM chart
|
|
WHERE id = (SELECT chart_id
|
|
FROM tax
|
|
WHERE id = ?)))|;
|
|
@values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
|
|
$tax, conv_date($form->{transdate}), $form->{"source_$i"},
|
|
$form->{"memo_$i"}, $project_id, $taxkey, conv_i($form->{"tax_id_$i"}), conv_i($form->{"tax_id_$i"}));
|
|
do_query($form, $dbh, $query, @values);
|
|
}
|
|
}
|
|
|
|
if ($form->{storno} && $form->{storno_id}) {
|
|
do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?| |