diff --git a/SL/BackgroundJob/CreatePeriodicInvoices.pm b/SL/BackgroundJob/CreatePeriodicInvoices.pm index 2df2a30..38fd3d0 100644 --- a/SL/BackgroundJob/CreatePeriodicInvoices.pm +++ b/SL/BackgroundJob/CreatePeriodicInvoices.pm @@ -213,16 +213,15 @@ sub _create_periodic_invoice { $invoice->post(ar_id => $config->ar_chart_id) || die; - # like $form->add_shipto, but we don't need to check for a manual exception, - # because we can already assume this (otherwise no shipto_id from order) - if ($order->shipto_id) { - - my $shipto_oe = SL::DB::Manager::Shipto->find_by(shipto_id => $order->shipto_id); - my $shipto_ar = $shipto_oe->clone_and_reset; - - $shipto_ar->module('AR'); # alter module OE -> AR - $shipto_ar->trans_id($invoice->id); # alter trans_id -> new id from invoice - $shipto_ar->save; + # custom shipto + if (!$order->shipto_id && $order->id) { + my $shipto_oe = SL::DB::Manager::Shipto->find_by(trans_id => $order->id, module => 'OE'); + if ($shipto_oe) { + my $shipto_ar = $shipto_oe->clone_and_reset; + $shipto_ar->module('AR'); + $shipto_ar->trans_id($invoice->id); + $shipto_ar->save; + } } $order->link_to_record($invoice); diff --git a/SL/Form.pm b/SL/Form.pm index a1b4198..6e8eabb 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -3368,8 +3368,18 @@ sub prepare_for_printing { } # Load shipping address from database if shipto_id is set. + my $shipto; if ($self->{shipto_id}) { - my $shipto = SL::DB::Shipto->new(shipto_id => $self->{shipto_id})->load; + $shipto = SL::DB::Shipto->new(shipto_id => $self->{shipto_id})->load; + } else { + # or try to find custom shipto in shiptos + my $module = ($self->{type} =~ /_delivery_order$/)? 'DO' + : ($self->{type} =~ /sales_order|sales_quotation|request_quotation|purchase_order/)? 'OE' + : ($self->{type} =~ /purchase_invoice/)? 'AP' + : 'AR'; + $shipto = SL::DB::Manager::Shipto->find_by(trans_id => $self->{id}, module => $module); + } + if ($shipto) { $self->{$_} = $shipto->$_ for grep { m{^shipto} } map { $_->name } @{ $shipto->meta->columns }; }