X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAR.pm;h=5f7faddf30facfd97a4d79de9840293c1d8324a7;hb=dd2ee66b1d04c3644c48c32b33673463af223077;hp=d64ddb2194b02c3383b65612675e4376babe6efb;hpb=85a6e1a3857c43bffeec46e08a2bda9c16419b03;p=kivitendo-erp.git diff --git a/SL/AR.pm b/SL/AR.pm index d64ddb219..5f7faddf3 100644 --- a/SL/AR.pm +++ b/SL/AR.pm @@ -25,7 +25,8 @@ # 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 Receivable module backend routines @@ -42,12 +43,21 @@ use SL::MoreCommon; use SL::DB::Default; use SL::TransNumber; use SL::Util qw(trim); +use SL::DB; 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 ($query, $sth, $null, $taxrate, $amount, $tax); @@ -56,7 +66,7 @@ sub post_transaction { my @values; - my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig); + my $dbh = $provided_dbh || SL::DB->client->dbh; $form->{defaultcurrency} = $form->get_default_currency($myconfig); # set exchangerate @@ -115,9 +125,6 @@ sub post_transaction { } } - # update department - ($null, $form->{department_id}) = split(/--/, $form->{department}); - # amount for AR account $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1; @@ -275,6 +282,15 @@ sub post_transaction { $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1, 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}; my $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno}; $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?), @@ -314,18 +330,11 @@ sub post_transaction { $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() and return $rc; + return 1; } sub _delete_payments { @@ -368,12 +377,19 @@ sub _delete_payments { } 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); @@ -422,12 +438,7 @@ sub post_payment { restore_form($old_form); - my $rc = $dbh->commit(); - $dbh->disconnect(); - - $main::lxdebug->leave_sub(); - - return $rc; + return 1; } sub delete_transaction { @@ -435,20 +446,16 @@ sub delete_transaction { my ($self, $myconfig, $form) = @_; - # connect to database, turn AutoCommit off - my $dbh = $form->dbconnect_noauto($myconfig); - - # acc_trans entries are deleted by database triggers. - my $query = qq|DELETE FROM ar WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - # commit - my $rc = $dbh->commit; - $dbh->disconnect; + SL::DB->client->with_transaction(sub { + # acc_trans entries are deleted by database triggers. + my $query = qq|DELETE FROM ar 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 ar_transactions { @@ -523,14 +530,8 @@ sub ar_transactions { push(@values, $business_id); } if ($form->{department_id}) { - my $department_id = $form->{department_id}; $where .= " AND a.department_id = ?"; - push(@values, $department_id); - } - if ($form->{department}) { - my $department = like($form->{department}); - $where .= " AND d.description ILIKE ?"; - push(@values, $department); + push(@values, $form->{department_id}); } foreach my $column (qw(invnumber ordnumber cusordnumber notes transaction_description)) { if ($form->{$column}) { @@ -648,7 +649,7 @@ sub get_transdate { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $query = "SELECT COALESCE(" . @@ -657,8 +658,6 @@ sub get_transdate { " current_date)"; ($form->{transdate}) = $dbh->selectrow_array($query); - $dbh->disconnect; - $main::lxdebug->leave_sub(); } @@ -674,7 +673,7 @@ sub setup_form { $form->{forex} = $form->{exchangerate}; $exchangerate = $form->{exchangerate} ? $form->{exchangerate} : 1; - # expected keys: AR, AR_paid, AR_tax, AR_amount + # expected keys: AR, AR_paid, AR_tax, AR_amount foreach my $key (keys %{ $form->{AR_links} }) { $j = 0; $k = 0; @@ -779,12 +778,21 @@ sub setup_form { } 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); @@ -829,9 +837,7 @@ sub storno { map { IO->set_datepaid(table => 'ar', id => $_, dbh => $dbh) } ($id, $new_id); - $dbh->commit; - - $main::lxdebug->leave_sub(); + return 1; }