# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
#======================================================================
#
# Accounts Payables database backend routines
use SL::MoreCommon;
use SL::DB::Default;
use SL::Util qw(trim);
+use SL::DB;
use Data::Dumper;
use strict;
sub post_transaction {
+ my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
$main::lxdebug->enter_sub();
+ my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form, $provided_dbh, $payments_only);
+
+ $::lxdebug->leave_sub;
+ return $rc;
+}
+
+sub _post_transaction {
my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
- my $rc = 0; # return code auf false setzen
- # connect to database
- my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+ my $dbh = $provided_dbh || SL::DB->client->dbh;
my ($null, $taxrate, $amount);
my $exchangerate = 0;
$form->{defaultcurrency} = $form->get_default_currency($myconfig);
$form->{taxincluded} = 0 unless $form->{taxincluded};
- ($null, $form->{department_id}) = split(/--/, $form->{department});
-
if ($form->{currency} eq $form->{defaultcurrency}) {
$form->{exchangerate} = 1;
} else {
$form->{"exchangerate_$i"}), 2);
if ($amount != 0) {
+ # fetch fxgain and fxloss chart info from defaults if charts aren't already filled in form
+ if ( !$form->{fxgain_accno} && $::instance_conf->get_fxgain_accno_id ) {
+ $form->{fxgain_accno} = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_fxgain_accno_id)->accno;
+ };
+ if ( !$form->{fxloss_accno} && $::instance_conf->get_fxloss_accno_id ) {
+ $form->{fxloss_accno} = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_fxloss_accno_id)->accno;
+ };
+ die "fxloss_accno missing" if $amount < 0 and not $form->{fxloss_accno};
+ die "fxgain_accno missing" if $amount > 0 and not $form->{fxgain_accno};
$query =
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
$datev->export;
if ($datev->errors) {
- $dbh->rollback;
die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
}
}
- if (!$provided_dbh) {
- $dbh->commit();
- $dbh->disconnect();
- }
-
- $rc = 1; # Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
-
- $main::lxdebug->leave_sub();
-
- return $rc;
+ return 1;
}
sub delete_transaction {
my ($self, $myconfig, $form) = @_;
- # connect to database
- my $dbh = $form->dbconnect_noauto($myconfig);
-
- # acc_trans entries are deleted by database triggers.
- my $query = qq|DELETE FROM ap WHERE id = ?|;
- do_query($form, $dbh, $query, $form->{id});
-
- my $rc = $dbh->commit;
- $dbh->disconnect;
+ SL::DB->client->with_transaction(sub {
+ my $query = qq|DELETE FROM ap WHERE id = ?|;
+ do_query($form, SL::DB->client->dbh, $query, $form->{id});
+ 1;
+ }) or do { die SL::DB->client->error };
$main::lxdebug->leave_sub();
- return $rc;
+ return 1;
}
sub ap_transactions {
$where .= " AND (cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)";
push(@values, (like($form->{"cp_name"}))x2);
}
- if ($form->{department}) {
- # ähnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
- # wird so nicht mehr als zeichenkette zusammengebaut
- # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
- #my ($null, $department_id) = split /--/, $form->{department};
+ if ($form->{department_id}) {
$where .= " AND a.department_id = ?";
- push(@values, $form->{department});
+ push(@values, $form->{department_id});
}
if ($form->{invnumber}) {
$where .= " AND a.invnumber ILIKE ?";
}
if ($form->{project_id}) {
$where .=
- qq|AND ((a.globalproject_id = ?) OR EXISTS | .
+ qq| AND ((a.globalproject_id = ?) OR EXISTS | .
qq| (SELECT * FROM invoice i | .
qq| WHERE i.project_id = ? AND i.trans_id = a.id) | .
qq| OR EXISTS | .
if ($form->{parts_partnumber}) {
$where .= <<SQL;
- AND EXISTS (
+ AND EXISTS (
SELECT invoice.trans_id
FROM invoice
LEFT JOIN parts ON (invoice.parts_id = parts.id)
if ($form->{parts_description}) {
$where .= <<SQL;
- AND EXISTS (
+ AND EXISTS (
SELECT invoice.trans_id
FROM invoice
WHERE (invoice.trans_id = a.id)
my ($self, $myconfig, $form) = @_;
# connect to database
- my $dbh = $form->dbconnect($myconfig);
+ my $dbh = SL::DB->client->dbh;
my $query =
"SELECT COALESCE(" .
" current_date)";
($form->{transdate}) = $dbh->selectrow_array($query);
- $dbh->disconnect;
-
$main::lxdebug->leave_sub();
}
}
sub post_payment {
+ my ($self, $myconfig, $form, $locale) = @_;
$main::lxdebug->enter_sub();
+ my $rc = SL::DB->client->with_transaction(\&_post_payment, $self, $myconfig, $form, $locale);
+
+ $::lxdebug->leave_sub;
+ return $rc;
+}
+
+sub _post_payment {
my ($self, $myconfig, $form, $locale) = @_;
- # connect to database, turn off autocommit
- my $dbh = $form->dbconnect_noauto($myconfig);
+ my $dbh = SL::DB->client->dbh;
my (%payments, $old_form, $row, $item, $query, %keep_vars);
restore_form($old_form);
- my $rc = $dbh->commit();
- $dbh->disconnect();
-
- $main::lxdebug->leave_sub();
-
- return $rc;
+ return 1;
}
sub setup_form {
}
$index = $form->{acc_trans}{$key}->[$i - 1]->{index};
- $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
+ $form->{"tax_$index"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} * -1 / $exchangerate, 2);
$totaltax += $form->{"tax_$index"};
} else {
}
sub storno {
+ my ($self, $form, $myconfig, $id) = @_;
$main::lxdebug->enter_sub();
+ my $rc = SL::DB->client->with_transaction(\&_storno, $self, $form, $myconfig, $id);
+
+ $::lxdebug->leave_sub;
+ return $rc;
+}
+
+sub _storno {
my ($self, $form, $myconfig, $id) = @_;
my ($query, $new_id, $storno_row, $acc_trans_rows);
- my $dbh = $form->get_standard_dbh($myconfig);
+ my $dbh = SL::DB->client->dbh;
$query = qq|SELECT nextval('glid')|;
($new_id) = selectrow_query($form, $dbh, $query);
map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
- $dbh->commit;
-
- $main::lxdebug->leave_sub();
+ return 1;
}
1;