use SL::MoreCommon;
use SL::DB::Default;
use SL::DB::TaxZone;
+use SL::DB;
use List::Util qw(min);
use strict;
sub post_invoice {
+ my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
$main::lxdebug->enter_sub();
+ my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, $payments_only);
+
+ $::lxdebug->leave_sub;
+ return $rc;
+}
+
+sub _post_invoice {
my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
- # connect to database, turn off autocommit
- my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+ my $dbh = $provided_dbh || SL::DB->client->dbh;
my $restricter = SL::HTML::Restrict->create;
$form->{defaultcurrency} = $form->get_default_currency($myconfig);
dbh => $dbh);
# link previous items with invoice items See IS.pm (no credit note -> no invoice item)
- foreach (qw(delivery_order_items orderitems)) {
+ foreach (qw(delivery_order_items orderitems invoice)) {
if (!$form->{useasnew} && $form->{"converted_from_${_}_id_$i"}) {
RecordLinks->create_links('dbh' => $dbh,
'mode' => 'ids',
if ($payments_only) {
$query = qq|UPDATE ap SET paid = ? WHERE id = ?|;
do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
+ $form->new_lastmtime('ap');
- if (!$provided_dbh) {
- $dbh->commit();
- $dbh->disconnect();
- }
-
- $main::lxdebug->leave_sub();
return;
}
invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?,
intnotes = ?, storno_id = ?, storno = ?,
cp_id = ?, employee_id = ?, department_id = ?, delivery_term_id = ?,
+ currency_id = (SELECT id FROM currencies WHERE name = ?),
globalproject_id = ?, direct_debit = ?
WHERE id = ?|;
@values = (
'1', $taxzone_id, $restricter->process($form->{notes}), $form->{taxincluded} ? 't' : 'f',
$form->{intnotes}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f',
conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}), conv_i($form->{delivery_term_id}),
+ $form->{"currency"},
conv_i($form->{globalproject_id}),
$form->{direct_debit} ? 't' : 'f',
conv_i($form->{id})
do_query($form, $dbh, $query, conv_i($form->{id}));
}
+ $form->new_lastmtime('ap');
$form->{name} = $form->{vendor};
$form->{name} =~ s/--\Q$form->{vendor_id}\E//;
Common::webdav_folder($form);
- # Link this record to the records it was created from.
- if ($form->{convert_from_oe_ids}) {
- RecordLinks->create_links('dbh' => $dbh,
- 'mode' => 'ids',
- 'from_table' => 'oe',
- 'from_ids' => $form->{convert_from_oe_ids},
- 'to_table' => 'ap',
- 'to_id' => $form->{id},
+ # Link this record to the records it was created from order or invoice (storno)
+ foreach (qw(oe ap)) {
+ if ($form->{"convert_from_${_}_ids"}) {
+ RecordLinks->create_links('dbh' => $dbh,
+ 'mode' => 'ids',
+ 'from_table' => $_,
+ 'from_ids' => $form->{"convert_from_${_}_ids"},
+ 'to_table' => 'ap',
+ 'to_id' => $form->{id},
);
- delete $form->{convert_from_oe_ids};
+ delete $form->{"convert_from_${_}_ids"};
+ }
}
my @convert_from_do_ids = map { $_ * 1 } grep { $_ } split m/\s+/, $form->{convert_from_do_ids};
# safety check datev export
if ($::instance_conf->get_datev_check_on_purchase_invoice) {
+ # if we need department for kostenstelle in DATEV check
+ $form->{department} = SL::DB::Manager::Department->find_by(id => $form->{department_id})->description if $form->{department_id};
my $transdate = $::form->{invdate} ? DateTime->from_lxoffice($::form->{invdate}) : undef;
$transdate ||= DateTime->today;
$datev->export;
if ($datev->errors) {
- $dbh->rollback;
die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
}
}
- my $rc = 1;
- if (!$provided_dbh) {
- $rc = $dbh->commit();
- $dbh->disconnect();
- }
-
- $main::lxdebug->leave_sub();
-
- return $rc;
+ return 1;
}
sub reverse_invoice {
my ($self, $myconfig, $form) = @_;
my $query;
# connect to database
- my $dbh = $form->dbconnect_noauto($myconfig);
+ my $dbh = SL::DB->client->dbh;
- &reverse_invoice($dbh, $form);
+ SL::DB->client->with_transaction(sub{
- my @values = (conv_i($form->{id}));
+ &reverse_invoice($dbh, $form);
- # delete zero entries
- # wtf? use case for this?
- $query = qq|DELETE FROM acc_trans WHERE amount = 0|;
- do_query($form, $dbh, $query);
+ my @values = (conv_i($form->{id}));
+ # delete zero entries
+ # wtf? use case for this?
+ $query = qq|DELETE FROM acc_trans WHERE amount = 0|;
+ do_query($form, $dbh, $query);
- my @queries = (
- qq|DELETE FROM invoice WHERE trans_id = ?|,
- qq|DELETE FROM ap WHERE id = ?|,
- );
-
- map { do_query($form, $dbh, $_, @values) } @queries;
- my $rc = $dbh->commit;
- $dbh->disconnect;
+ my @queries = (
+ qq|DELETE FROM invoice WHERE trans_id = ?|,
+ qq|DELETE FROM ap WHERE id = ?|,
+ );
- $main::lxdebug->leave_sub();
+ map { do_query($form, $dbh, $_, @values) } @queries;
+ 1;
+ }) or do { die SL::DB->client->error };
- return $rc;
+ return 1;
}
sub retrieve_invoice {
my ($self, $myconfig, $form) = @_;
# connect to database
- my $dbh = $form->dbconnect($myconfig);
+ my $dbh = SL::DB->client->dbh;
my ($query, $sth, $ref, $q_invdate);
map { $form->{$_} = $ref->{$_} } keys %$ref;
if (!$form->{id}) {
- $dbh->disconnect();
$main::lxdebug->leave_sub();
return;
Common::webdav_folder($form);
- $dbh->disconnect();
-
$main::lxdebug->leave_sub();
}
$params = $form unless defined $params && ref $params eq "HASH";
# connect to database
- my $dbh = $form->dbconnect($myconfig);
+ my $dbh = SL::DB->client->dbh;
my $dateformat = $myconfig->{dateformat};
$dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
$params->{rowcount} = $i if ($i && !$params->{type});
}
- $dbh->disconnect();
-
$main::lxdebug->leave_sub();
}
my ($self, $myconfig, $form) = @_;
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
+ my $dbh = SL::DB->client->dbh;
my $i = $form->{rowcount};
my $field = (split m{\.}, $table_column)[1];
next unless $form->{"${field}_${i}"};
$where .= " AND lower(${table_column}) LIKE lower(?)";
- push @values, '%' . $form->{"${field}_${i}"} . '%';
+ push @values, like($form->{"${field}_${i}"});
}
my (%mm_by_id);
LEFT JOIN parts ON parts.id = parts_id
WHERE NOT parts.obsolete AND model ILIKE ? AND (make IS NULL OR make = ?);
|;
- my $mm_results = selectall_hashref_query($::form, $dbh, $mm_query, '%' . $form->{"partnumber_$i"} . '%', $::form->{vendor_id});
+ my $mm_results = selectall_hashref_query($::form, $dbh, $mm_query, like($form->{"partnumber_$i"}), $::form->{vendor_id});
my @mm_ids = map { $_->{parts_id} } @$mm_results;
push @{$mm_by_id{ $_->{parts_id} } ||= []}, $_ for @$mm_results;
push @{ $ref->{matches} ||= [] }, $::locale->text('Model') . ': ' . join ', ', map { $_->{model} } @{ $mm_by_id{$ref->{id}} };
}
- if ($ref->{ean} eq $::form->{"partnumber_$i"}) {
+ if (($::form->{"partnumber_$i"} ne '') && ($ref->{ean} eq $::form->{"partnumber_$i"})) {
push @{ $ref->{matches} ||= [] }, $::locale->text('EAN') . ': ' . $ref->{ean};
}
map { $item->{"ic_cvar_" . $_->{name} } = $_->{value} } @{ $custom_variables };
}
- $dbh->disconnect();
-
$main::lxdebug->leave_sub();
}
my ($self, $myconfig, $form, @wanted_vars) = @_;
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
+ my $dbh = SL::DB->client->dbh;
my @values;
LIMIT 1|;
my $ref = selectfirst_hashref_query($form, $dbh, $query, $form->{vendor_id}, @values);
- # remove id and taxincluded before copy back
- delete @$ref{qw(id taxincluded)};
+ # remove id,notes (double of vendornotes) and taxincluded before copy back
+ delete @$ref{qw(id taxincluded notes)};
@wanted_vars = grep({ $_ } @wanted_vars);
if (scalar(@wanted_vars) > 0) {
'translation_type' => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
'allow_fallback' => 1);
- $dbh->disconnect();
-
$main::lxdebug->leave_sub();
}
my ($self, $myconfig, $form) = @_;
- # connect to database
- my $dbh = $form->dbconnect($myconfig);
+ my $dbh = SL::DB->client->dbh;
my $query =
qq|SELECT accno, description, link
}
$sth->finish();
- $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 get_duedate {