+ my @links = RecordLinks->get_links('dbh' => $dbh,
+ 'from_table' => 'oe',
+ 'to_table' => 'delivery_orders',
+ 'to_id' => $params{do_id});
+
+ my $oe_id = @links ? $links[0]->{from_id} : undef;
+
+ return $main::lxdebug->leave_sub() if (!$oe_id);
+
+ my $all_units = AM->retrieve_all_units();
+
+ my $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);
+
+ my %shipped = $self->get_shipped_qty('type' => $params{type},
+ 'oe_id' => $oe_id,);
+ my %ordered = ();
+
+ 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
+ WHERE id = ?|;
+ do_query($form, $dbh, $query, $oe_id);
+ $dbh->commit() if (!$params{dbh});
+ }
+
+ $main::lxdebug->leave_sub();
+}
+
+sub close_orders {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ Common::check_params(\%params, qw(ids));
+
+ if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
+ $main::lxdebug->leave_sub();
+ return;
+ }
+
+ my $myconfig = \%main::myconfig;
+ my $form = $main::form;
+
+ my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+ my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
+
+ do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
+
+ $dbh->commit() unless ($params{dbh});