X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FIR.pm;h=57cfb5a5f4ee04d9e1aad757b1648ff3b093f38a;hb=072caebf05b59b8b74f7f5699d99129834bab616;hp=620fa184748dcc480488648fc35ee2a7c45c0f3b;hpb=96ab68a31bb2e33769aeccea9b7e51b23b65f59a;p=kivitendo-erp.git diff --git a/SL/IR.pm b/SL/IR.pm index 620fa1847..57cfb5a5f 100644 --- a/SL/IR.pm +++ b/SL/IR.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. #====================================================================== # # Inventory received module @@ -47,17 +48,25 @@ use SL::IO; 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); @@ -691,12 +700,7 @@ SQL $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; } @@ -716,6 +720,7 @@ SQL 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 = ( @@ -725,6 +730,7 @@ SQL '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}) @@ -818,20 +824,11 @@ SQL $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 { @@ -841,7 +838,7 @@ sub reverse_invoice { # reverse inventory items my $query = - qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id, i.qty, i.allocated, i.sellprice + qq|SELECT i.parts_id, p.part_type, i.qty, i.allocated, i.sellprice FROM invoice i, parts p WHERE (i.parts_id = p.id) AND (i.trans_id = ?)|; @@ -852,7 +849,7 @@ sub reverse_invoice { while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2); - next unless $ref->{inventory_accno_id}; + next unless $ref->{part_type} eq 'part'; # if $ref->{allocated} > 0 than we sold that many items next if ($ref->{allocated} <= 0); @@ -917,31 +914,30 @@ sub delete_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})); - - # delete zero entries - # wtf? use case for this? - $query = qq|DELETE FROM acc_trans WHERE amount = 0|; - do_query($form, $dbh, $query); + &reverse_invoice($dbh, $form); + my @values = (conv_i($form->{id})); - my @queries = ( - qq|DELETE FROM invoice WHERE trans_id = ?|, - qq|DELETE FROM ap WHERE id = ?|, - ); + # delete zero entries + # wtf? use case for this? + $query = qq|DELETE FROM acc_trans WHERE amount = 0|; + do_query($form, $dbh, $query); - 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 { @@ -950,7 +946,7 @@ 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); @@ -982,7 +978,6 @@ sub retrieve_invoice { map { $form->{$_} = $ref->{$_} } keys %$ref; if (!$form->{id}) { - $dbh->disconnect(); $main::lxdebug->leave_sub(); return; @@ -1025,7 +1020,7 @@ sub retrieve_invoice { i.id AS invoice_id, i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber, i.price_factor_id, i.price_factor, i.marge_price_factor, i.discount, i.active_price_source, i.active_discount_source, - p.partnumber, p.inventory_accno_id AS part_inventory_accno_id, pr.projectnumber, pg.partsgroup + p.partnumber, p.part_type, pr.projectnumber, pg.partsgroup FROM invoice i JOIN parts p ON (i.parts_id = p.id) @@ -1049,8 +1044,7 @@ sub retrieve_invoice { ); map { $ref->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars }; - map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"}; - delete($ref->{"part_inventory_accno_id"}); + map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_type"} eq 'part'; foreach my $type (qw(inventory income expense)) { while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) { @@ -1100,8 +1094,6 @@ sub retrieve_invoice { Common::webdav_folder($form); - $dbh->disconnect(); - $main::lxdebug->leave_sub(); } @@ -1113,7 +1105,7 @@ sub get_vendor { $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/; @@ -1213,8 +1205,6 @@ sub get_vendor { $params->{rowcount} = $i if ($i && !$params->{type}); } - $dbh->disconnect(); - $main::lxdebug->leave_sub(); } @@ -1223,13 +1213,12 @@ sub retrieve_item { my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $i = $form->{rowcount}; # don't include assemblies or obsolete parts - my $where = "NOT p.assembly = '1' AND NOT p.obsolete = '1'"; + my $where = "NOT p.part_type = 'assembly' AND NOT p.obsolete = '1'"; my @values; foreach my $table_column (qw(p.partnumber p.description pg.partsgroup)) { @@ -1287,9 +1276,9 @@ sub retrieve_item { my $query = qq|SELECT p.id, p.partnumber, p.description, p.lastcost AS sellprice, p.listprice, - p.unit, p.assembly, p.onhand, p.formel, + p.unit, p.part_type, p.onhand, p.formel, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable, - p.inventory_accno_id, p.price_factor_id, + p.price_factor_id, p.ean, pfac.factor AS price_factor, @@ -1429,8 +1418,6 @@ sub retrieve_item { map { $item->{"ic_cvar_" . $_->{name} } = $_->{value} } @{ $custom_variables }; } - $dbh->disconnect(); - $main::lxdebug->leave_sub(); } @@ -1439,8 +1426,7 @@ sub vendor_details { my ($self, $myconfig, $form, @wanted_vars) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my @values; @@ -1465,8 +1451,8 @@ sub vendor_details { 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) { @@ -1486,8 +1472,6 @@ sub vendor_details { 'translation_type' => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'), 'allow_fallback' => 1); - $dbh->disconnect(); - $main::lxdebug->leave_sub(); } @@ -1496,8 +1480,7 @@ sub item_links { 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 @@ -1517,8 +1500,6 @@ sub item_links { } $sth->finish(); - $dbh->disconnect(); - $main::lxdebug->leave_sub(); } @@ -1562,12 +1543,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); @@ -1623,12 +1611,7 @@ sub post_payment { restore_form($old_form); - my $rc = $dbh->commit(); - $dbh->disconnect(); - - $main::lxdebug->leave_sub(); - - return $rc; + return 1; } sub get_duedate {