use SL::AM;
use SL::Common;
use SL::DBUtils;
+use SL::RecordLinks;
sub transactions {
$main::lxdebug->enter_sub();
# save printed, emailed, queued
$form->save_status($dbh);
- $self->mark_order_if_delivered('do_id' => $form->{id},
- 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
- 'dbh' => $dbh,);
+ # Link this delivery order to the quotations it was created from.
+ my @oe_ids = grep { $_ } map { $_ * 1 } split m/\s+/, $form->{oe_ids};
+ delete $form->{oe_ids};
+ if (scalar @oe_ids) {
+ my @links = map { { 'from_table' => 'oe', 'from_id' => $_, 'to_table' => 'delivery_orders', 'to_id' => $form->{id} } } @oe_ids;
+ RecordLinks->create_links('dbh' => $dbh, 'links' => \@links);
+ }
+
+ $self->mark_orders_if_delivered('do_id' => $form->{id},
+ 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
+ 'dbh' => $dbh,);
my $rc = $dbh->commit();
return $rc;
}
-sub mark_order_if_delivered {
+sub mark_orders_if_delivered {
$main::lxdebug->enter_sub();
my $self = shift;
Common::check_params(\%params, qw(do_id type));
- my $myconfig = \%main::myconfig;
- my $form = $main::form;
-
- my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
+ my $myconfig = \%main::myconfig;
+ my $form = $main::form;
- my $query = qq|SELECT ordnumber FROM delivery_orders WHERE id = ?|;
- my ($ordnumber) = selectfirst_array_query($form, $dbh, $query, conv_i($params{do_id}));
+ my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
- return $main::lxdebug->leave_sub() if (!$ordnumber);
+ my @links = RecordLinks->get_links('dbh' => $dbh,
+ 'from_table' => 'oe',
+ 'to_table' => 'delivery_orders',
+ 'to_id' => $params{do_id});
- 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) = $links[0]->{from_id} if (scalar @links);
- my ($oe_id) = selectfirst_array_query($form, $dbh, $query, $ordnumber);
+ $main::lxdebug->message(0, "oe_id $oe_id");
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);
+ my %shipped = $self->get_shipped_qty('type' => $params{type},
+ 'oe_id' => $oe_id,);
+ my %ordered = ();
+
+ do_statement($form, $sth, $query, $oe_id);
+
while (my $ref = $sth->fetchrow_hashref()) {
$ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
if ($delivered) {
$query = qq|UPDATE oe
- SET delivered = TRUE, closed = TRUE
+ SET delivered = TRUE
WHERE id = ?|;
do_query($form, $dbh, $query, $oe_id);
-
$dbh->commit() if (!$params{dbh});
}
my $self = shift;
my %params = @_;
- Common::check_params(\%params, qw(type ordnumber));
+ Common::check_params(\%params, qw(type oe_id));
my $myconfig = \%main::myconfig;
my $form = $main::form;
my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
- my $notsales = $params{type} eq 'sales' ? '' : 'NOT';
+ my @links = RecordLinks->get_links('dbh' => $dbh,
+ 'from_table' => 'oe',
+ 'from_id' => $params{oe_id},
+ 'to_table' => 'delivery_orders');
+ my @values = map { $_->{to_id} } @links;
+
+ if (!scalar @values) {
+ $main::lxdebug->leave_sub();
+ return ();
+ }
- my $query =
+ my $query =
qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
FROM delivery_order_items doi
LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
LEFT JOIN parts p ON (doi.parts_id = p.id)
- WHERE ($notsales o.is_sales)
- AND (o.ordnumber = ?)|;
+ WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
my %ship = ();
- my $entries = selectall_hashref_query($form, $dbh, $query, $params{ordnumber});
+ my $entries = selectall_hashref_query($form, $dbh, $query, @values);
my $all_units = AM->retrieve_all_units();
foreach my $entry (@{ $entries }) {
--- /dev/null
+package RecordLinks;
+
+use SL::Common;
+use SL::DBUtils;
+
+sub create_links {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ Common::check_params(\%params, qw(links));
+
+ if (!scalar @{ $params{links} }) {
+ $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|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
+ my $sth = prepare_query($form, $dbh, $query);
+
+ foreach my $link (@{ $params{links} }) {
+ next if ('HASH' ne ref $link);
+ next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
+
+ do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
+ }
+
+ $dbh->commit() unless ($params{dbh});
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_links {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
+
+ my $myconfig = \%main::myconfig;
+ my $form = $main::form;
+
+ my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+ my @conditions = ();
+ my @values = ();
+
+ foreach my $col (qw(from_table from_id to_table to_id)) {
+ next unless ($params{$col});
+
+ if ('ARRAY' eq ref $params{$col}) {
+ push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
+ push @values, $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
+
+ } else {
+ push @conditions, "$col = ?";
+ push @values, $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
+ }
+ }
+
+ my $query = qq|SELECT from_table, from_id, to_table, to_id
+ FROM record_links|;
+
+ if (scalar @conditions) {
+ $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
+ }
+
+ my $links = selectall_hashref_query($form, $dbh, $query, @values);
+
+ $main::lxdebug->leave_sub();
+
+ return wantarray ? @{ $links } : $links;
+}
+
+1;