X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FARAP.pm;h=121ea065ac40d4acb51a65711a0f6fc31afe017d;hb=dd2ee66b1d04c3644c48c32b33673463af223077;hp=299c32f16aa2af5c4a6dd9f0fb9836a045326fde;hpb=247a26dc4b5b0d73c03fc6f05fb17daace0835d9;p=kivitendo-erp.git diff --git a/SL/ARAP.pm b/SL/ARAP.pm index 299c32f16..121ea065a 100644 --- a/SL/ARAP.pm +++ b/SL/ARAP.pm @@ -4,8 +4,11 @@ use SL::AM; use SL::Common; use SL::DBUtils; use SL::MoreCommon; +use SL::DB; use Data::Dumper; +use strict; + sub close_orders_if_billed { $main::lxdebug->enter_sub(); @@ -17,7 +20,7 @@ sub close_orders_if_billed { my $myconfig = \%main::myconfig; my $form = $main::form; - my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + my $dbh = $params{dbh} || SL::DB->client->dbh; # First, find all order IDs from which this invoice has been # created. Either directly by a conversion from an order to this invoice @@ -49,18 +52,16 @@ sub close_orders_if_billed { my @oe_ids = keys %oe_id_map; -# $main::lxdebug->dump(0, "oe_ids", \@oe_ids); - # No orders found? Nothing to do then, so let's return. - return $main::lxdebug->leave_sub() if (!scalar @oe_ids); + return $main::lxdebug->leave_sub unless @oe_ids; - my $all_units = AM->retrieve_all_units(); + my $all_units = AM->retrieve_all_units; my $qtyfactor = $params{table} eq 'ap' ? '* -1' : ''; my $q_billed = qq|SELECT i.parts_id, i.qty ${qtyfactor} AS qty, i.unit, p.unit AS partunit FROM invoice i LEFT JOIN parts p ON (i.parts_id = p.id) - WHERE i.trans_id = ?|; + WHERE i.trans_id = ? AND i.assemblyitem is false|; my $h_billed = prepare_query($form, $dbh, $q_billed); my $q_ordered = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit @@ -99,8 +100,6 @@ sub close_orders_if_billed { my @arap_ids = keys %arap_id_map; -# $main::lxdebug->dump(0, "for $oe_id arap_ids", \@arap_ids); - next if (!scalar @arap_ids); # Retrieve all positions for this order. Calculate the ordered quantity for each position. @@ -109,7 +108,7 @@ sub close_orders_if_billed { do_statement($form, $h_ordered, $q_ordered, $oe_id); while (my $ref = $h_ordered->fetchrow_hashref()) { - $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor}; + $ref->{baseqty} = $ref->{qty} * AM->convert_unit($ref->{unit}, $ref->{partunit}, $all_units); if ($ordered{$ref->{parts_id}}) { $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty}; @@ -125,7 +124,7 @@ sub close_orders_if_billed { do_statement($form, $h_billed, $q_billed, $arap_id); while (my $ref = $h_billed->fetchrow_hashref()) { - $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor}; + $ref->{baseqty} = $ref->{qty} * AM->convert_unit($ref->{unit}, $ref->{partunit}, $all_units); if ($billed{$ref->{parts_id}}) { $billed{$ref->{parts_id}}->{baseqty} += $ref->{baseqty}; @@ -144,22 +143,19 @@ sub close_orders_if_billed { } } -# $main::lxdebug->message(0, "all_billed $all_billed"); -# $main::lxdebug->dump(0, "ordered", \%ordered); -# $main::lxdebug->dump(0, "billed", \%billed); - push @close_oe_ids, $oe_id if ($all_billed); } - $h_billed->finish(); - $h_ordered->finish(); + $h_billed->finish; + $h_ordered->finish; # Close orders that have been billed fully. if (scalar @close_oe_ids) { - my $query = qq|UPDATE oe SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar @close_oe_ids) . qq|)|; - do_query($form, $dbh, $query, @close_oe_ids); - - $dbh->commit() unless ($params{dbh}); + SL::DB->client->with_transaction(sub { + my $query = qq|UPDATE oe SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar @close_oe_ids) . qq|)|; + do_query($form, $dbh, $query, @close_oe_ids); + 1; + }) or do { die SL::DB->client->error }; } $main::lxdebug->leave_sub();