From e73b17d69ae188821a87c133edb315284d74dfb5 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 18 Jun 2008 06:42:28 +0000 Subject: [PATCH] =?utf8?q?Beim=20Speichern=20von=20Lieferscheinen=20wird?= =?utf8?q?=20=C3=BCberpr=C3=BCft,=20ob=20der=20dazugeh=C3=B6rige=20Auftrag?= =?utf8?q?=20nun=20komplett=20geliefert=20wurde.=20Wenn=20ja,=20so=20wird?= =?utf8?q?=20der=20Auftrag=20sowohl=20als=20geliefert=20als=20auch=20als?= =?utf8?q?=20geschlossen=20markiert.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DO.pm | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/SL/DO.pm b/SL/DO.pm index 7f547f0b9..18fba890a 100644 --- a/SL/DO.pm +++ b/SL/DO.pm @@ -342,6 +342,10 @@ sub save { # save printed, emailed, queued $form->save_status($dbh); + $self->close_order_if_delivered('do_id' => $form->{id}, + 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase', + 'dbh' => $dbh,); + my $rc = $dbh->commit(); $form->{saved_donumber} = $form->{donumber}; @@ -353,19 +357,79 @@ sub save { return $rc; } -sub close_order { +sub close_order_if_delivered { $main::lxdebug->enter_sub(); - my ($self) = @_; + my $self = shift; + my %params = @_; - my $myconfig = \%main::myconfig; - my $form = $main::form; + Common::check_params(\%params, qw(do_id type)); - return $main::lxdebug->leave_sub() unless ($form->{id}); + my $myconfig = \%main::myconfig; + my $form = $main::form; - my $dbh = $form->get_standard_dbh($myconfig); - do_query($form, $dbh, qq|UPDATE do SET closed = TRUE where id = ?|, conv_i($form->{id})); - $dbh->commit(); + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + my $query = qq|SELECT ordnumber FROM delivery_orders WHERE id = ?|; + my ($ordnumber) = selectfirst_array_query($form, $dbh, $query, conv_i($params{do_id})); + + return $main::lxdebug->leave_sub() if (!$ordnumber); + + my $vc = $params{type} eq 'purchase' ? 'vendor' : 'customer'; + $query = qq|SELECT id + FROM oe + WHERE NOT COALESCE(quotation, FALSE) + AND (COALESCE(${vc}_id, 0) > 0) + AND (ordnumber = ?) + ORDER BY id + LIMIT 1|; + + my ($oe_id) = selectfirst_array_query($form, $dbh, $query, $ordnumber); + + return $main::lxdebug->leave_sub() if (!$oe_id); + + my $all_units = AM->retrieve_all_units(); + + my %shipped = $self->get_shipped_qty('type' => $params{type}, + 'ordnumber' => $ordnumber,); + my %ordered = (); + + $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit + FROM orderitems oi + LEFT JOIN parts p ON (oi.parts_id = p.id) + WHERE (oi.trans_id = ?)|; + my $sth = prepare_execute_query($form, $dbh, $query, $oe_id); + + while (my $ref = $sth->fetchrow_hashref()) { + $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor}; + + if ($ordered{$ref->{parts_id}}) { + $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty}; + } else { + $ordered{$ref->{parts_id}} = $ref; + } + } + + $sth->finish(); + + map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped; + + my $delivered = 1; + foreach my $part (values %ordered) { + if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) { + $delivered = 0; + last; + } + } + + if ($delivered) { + $query = qq|UPDATE oe + SET delivered = TRUE, closed = TRUE + WHERE id = ?|; + do_query($form, $dbh, $query, $oe_id); + + $dbh->commit() if (!$params{dbh}); + } $main::lxdebug->leave_sub(); } -- 2.20.1