commit 5a6857e5f6e2df87943f369f2c6969659a9df6de
Author: Moritz Bunkus <m.bunkus@linet.de>
Date:   Mon Oct 25 15:48:23 2021 +0200

    Zusätzliche Rechnungsadressen: in Verkaufsbelegmasken auswählbar

diff --git a/SL/Common.pm b/SL/Common.pm
index fb1b4b850..a0d54feb6 100644
--- a/SL/Common.pm
+++ b/SL/Common.pm
@@ -342,6 +342,11 @@ sub get_vc_details {
   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
+  if ($vc eq 'customer') {
+    $query = qq|SELECT * FROM additional_billing_addresses WHERE (customer_id = ?)|;
+    $form->{ADDITIONAL_BILLING_ADDRESSES} = selectall_hashref_query($form, $dbh, $query, $vc_id);
+  }
+
   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
 
diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm
index 302a5eacf..37ba87cfa 100644
--- a/SL/Controller/Order.pm
+++ b/SL/Controller/Order.pm
@@ -13,6 +13,7 @@ use SL::File;
 use SL::MIME;
 use SL::Util qw(trim);
 use SL::YAML;
+use SL::DB::AdditionalBillingAddress;
 use SL::DB::History;
 use SL::DB::Order;
 use SL::DB::Default;
@@ -699,20 +700,26 @@ sub action_customer_vendor_changed {
     $self->js->hide('#shipto_selection');
   }
 
+  if ($cv_method eq 'customer') {
+    my $show_hide = scalar @{ $self->order->customer->additional_billing_addresses } > 0 ? 'show' : 'hide';
+    $self->js->$show_hide('#billing_address_row');
+  }
+
   $self->js->val( '#order_salesman_id',      $self->order->salesman_id)        if $self->order->is_sales;
 
   $self->js
-    ->replaceWith('#order_cp_id',            $self->build_contact_select)
-    ->replaceWith('#order_shipto_id',        $self->build_shipto_select)
-    ->replaceWith('#shipto_inputs  ',        $self->build_shipto_inputs)
-    ->replaceWith('#business_info_row',      $self->build_business_info_row)
-    ->val(        '#order_taxzone_id',       $self->order->taxzone_id)
-    ->val(        '#order_taxincluded',      $self->order->taxincluded)
-    ->val(        '#order_currency_id',      $self->order->currency_id)
-    ->val(        '#order_payment_id',       $self->order->payment_id)
-    ->val(        '#order_delivery_term_id', $self->order->delivery_term_id)
-    ->val(        '#order_intnotes',         $self->order->intnotes)
-    ->val(        '#order_language_id',      $self->order->$cv_method->language_id)
+    ->replaceWith('#order_cp_id',              $self->build_contact_select)
+    ->replaceWith('#order_shipto_id',          $self->build_shipto_select)
+    ->replaceWith('#shipto_inputs  ',          $self->build_shipto_inputs)
+    ->replaceWith('#order_billing_address_id', $self->build_billing_address_select)
+    ->replaceWith('#business_info_row',        $self->build_business_info_row)
+    ->val(        '#order_taxzone_id',         $self->order->taxzone_id)
+    ->val(        '#order_taxincluded',        $self->order->taxincluded)
+    ->val(        '#order_currency_id',        $self->order->currency_id)
+    ->val(        '#order_payment_id',         $self->order->payment_id)
+    ->val(        '#order_delivery_term_id',   $self->order->delivery_term_id)
+    ->val(        '#order_intnotes',           $self->order->intnotes)
+    ->val(        '#order_language_id',        $self->order->$cv_method->language_id)
     ->focus(      '#order_' . $self->cv . '_id')
     ->run('kivi.Order.update_exchangerate');
 
@@ -742,6 +749,9 @@ sub action_show_customer_vendor_details_dialog {
   $details{payment_terms}       = $cv->payment->description       if $cv->payment;
   $details{pricegroup}          = $cv->pricegroup->pricegroup     if $is_customer && $cv->pricegroup;
 
+  foreach my $entry (@{ $cv->additional_billing_addresses }) {
+    push @{ $details{ADDITIONAL_BILLING_ADDRESSES} },   { map { $_ => $entry->$_ } @{$entry->meta->columns} };
+  }
   foreach my $entry (@{ $cv->shipto }) {
     push @{ $details{SHIPTO} },   { map { $_ => $entry->$_ } @{$entry->meta->columns} };
   }
@@ -1308,6 +1318,22 @@ sub build_contact_select {
   );
 }
 
+# build the selection box for the additional billing address
+#
+# Needed, if customer/vendor changed.
+sub build_billing_address_select {
+  my ($self) = @_;
+
+  select_tag('order.billing_address_id',
+             [ {displayable_id => '', id => ''}, $self->order->{$self->cv}->additional_billing_addresses ],
+             value_key  => 'id',
+             title_key  => 'displayable_id',
+             default    => $self->order->billing_address_id,
+             with_empty => 0,
+             style      => 'width: 300px',
+  );
+}
+
 # build the selection box for shiptos
 #
 # Needed, if customer/vendor changed.
@@ -1565,13 +1591,15 @@ sub setup_order_from_cv {
 
   $order->intnotes($order->customervendor->notes);
 
-  if ($order->is_sales) {
-    $order->salesman_id($order->customer->salesman_id || SL::DB::Manager::Employee->current->id);
-    $order->taxincluded(defined($order->customer->taxincluded_checked)
-                        ? $order->customer->taxincluded_checked
-                        : $::myconfig{taxincluded_checked});
-  }
+  return if !$order->is_sales;
+
+  $order->salesman_id($order->customer->salesman_id || SL::DB::Manager::Employee->current->id);
+  $order->taxincluded(defined($order->customer->taxincluded_checked)
+                      ? $order->customer->taxincluded_checked
+                      : $::myconfig{taxincluded_checked});
 
+  my $address = $order->customer->default_billing_address;;
+  $order->billing_address_id($address ? $address->id : undef);
 }
 
 # setup custom shipto from form
diff --git a/SL/DB/Customer.pm b/SL/DB/Customer.pm
index 2032a4179..d72d0c91a 100644
--- a/SL/DB/Customer.pm
+++ b/SL/DB/Customer.pm
@@ -2,6 +2,7 @@ package SL::DB::Customer;
 
 use strict;
 
+use List::Util qw(first);
 use Rose::DB::Object::Helpers qw(as_tree);
 
 use SL::Locale::String qw(t8);
@@ -114,4 +115,11 @@ sub create_zugferd_invoices_for_this_customer {
   return $self->create_zugferd_invoices;
 }
 
+sub default_billing_address {
+  my $self = shift;
+
+  die 'not an accessor' if @_ > 1;
+  return first { $_->default_address } @{ $self->additional_billing_addresses };
+}
+
 1;
diff --git a/SL/DB/DeliveryOrder.pm b/SL/DB/DeliveryOrder.pm
index 395a44944..f4e630148 100644
--- a/SL/DB/DeliveryOrder.pm
+++ b/SL/DB/DeliveryOrder.pm
@@ -127,7 +127,7 @@ sub new_from {
   }
 
   my %args = ( map({ ( $_ => $source->$_ ) } qw(cp_id currency_id customer_id cusordnumber delivery_term_id department_id employee_id globalproject_id intnotes language_id notes
-                                                ordnumber payment_id reqdate salesman_id shippingpoint shipvia taxincluded taxzone_id transaction_description vendor_id
+                                                ordnumber payment_id reqdate salesman_id shippingpoint shipvia taxincluded taxzone_id transaction_description vendor_id billing_address_id
                                              )),
                closed    => 0,
                is_sales  => !!$source->customer_id,
diff --git a/SL/DB/Invoice.pm b/SL/DB/Invoice.pm
index 26faca1a3..c5c29dd8e 100644
--- a/SL/DB/Invoice.pm
+++ b/SL/DB/Invoice.pm
@@ -173,7 +173,8 @@ sub new_from {
   $terms = $source->customer->payment_terms if !defined $terms && $source->customer;
 
   my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes salesman_id cusordnumber ordnumber department_id
-                                                cp_id language_id taxzone_id tax_point globalproject_id transaction_description currency_id delivery_term_id), @columns),
+                                                cp_id language_id taxzone_id tax_point globalproject_id transaction_description currency_id delivery_term_id
+                                                billing_address_id), @columns),
                transdate   => $params{transdate} // DateTime->today_local,
                gldate      => DateTime->today_local,
                duedate     => $terms ? $terms->calc_date(reference_date => DateTime->today_local) : DateTime->today_local,
diff --git a/SL/DB/Order.pm b/SL/DB/Order.pm
index 261cb7f23..397b7fa5c 100644
--- a/SL/DB/Order.pm
+++ b/SL/DB/Order.pm
@@ -345,7 +345,7 @@ sub new_from {
   my %args = ( map({ ( $_ => $source->$_ ) } qw(amount cp_id currency_id cusordnumber customer_id delivery_customer_id delivery_term_id delivery_vendor_id
                                                 department_id exchangerate globalproject_id intnotes marge_percent marge_total language_id netamount notes
                                                 ordnumber payment_id quonumber reqdate salesman_id shippingpoint shipvia taxincluded tax_point taxzone_id
-                                                transaction_description vendor_id
+                                                transaction_description vendor_id billing_address_id
                                              )),
                quotation => !!($destination_type =~ m{quotation$}),
                closed    => 0,
diff --git a/SL/DO.pm b/SL/DO.pm
index 8f04ce7c5..86c65ea14 100644
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -513,7 +513,7 @@ SQL
          donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
          customer_id = ?, reqdate = ?, tax_point = ?,
          shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
-         delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
+         delivered = ?, department_id = ?, language_id = ?, shipto_id = ?, billing_address_id = ?,
          globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
          is_sales = ?, taxzone_id = ?, taxincluded = ?, payment_id = ?, currency_id = (SELECT id FROM currencies WHERE name = ?),
          delivery_term_id = ?
@@ -525,7 +525,7 @@ SQL
              conv_date($form->{reqdate}), conv_date($form->{tax_point}), $form->{shippingpoint}, $form->{shipvia},
              $restricter->process($form->{notes}), $form->{intnotes},
              $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
-             conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
+             conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}), conv_i($form->{billing_address_id}),
              conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
              conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
              $form->{transaction_description},
@@ -731,7 +731,7 @@ sub retrieve {
          dord.${vc}_id, cv.name AS ${vc},
          dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
          d.description AS department, dord.language_id,
-         dord.shipto_id,
+         dord.shipto_id, dord.billing_address_id,
          dord.itime, dord.mtime,
          dord.globalproject_id, dord.delivered, dord.transaction_description,
          dord.taxzone_id, dord.taxincluded, dord.payment_id, (SELECT cu.name FROM currencies cu WHERE cu.id=dord.currency_id) AS currency,
diff --git a/SL/IS.pm b/SL/IS.pm
index 7cf800805..9474b18e0 100644
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -1318,7 +1318,7 @@ SQL
                 shipvia     = ?,                    notes         = ?, intnotes      = ?,
                 currency_id = (SELECT id FROM currencies WHERE name = ?),
                 department_id = ?, payment_id    = ?, taxincluded   = ?,
-                type        = ?, language_id   = ?, taxzone_id    = ?, shipto_id     = ?,
+                type        = ?, language_id   = ?, taxzone_id    = ?, shipto_id     = ?, billing_address_id = ?,
                 employee_id = ?, salesman_id   = ?, storno_id     = ?, storno        = ?,
                 cp_id       = ?, marge_total   = ?, marge_percent = ?,
                 globalproject_id               = ?, delivery_customer_id             = ?,
@@ -1332,7 +1332,7 @@ SQL
              conv_date($form->{"duedate"}),  conv_date($form->{"deliverydate"}),    '1',                                $form->{"shippingpoint"},
                        $form->{"shipvia"},                                $restricter->process($form->{"notes"}),       $form->{"intnotes"},
                        $form->{"currency"},     conv_i($form->{"department_id"}), conv_i($form->{"payment_id"}),        $form->{"taxincluded"} ? 't' : 'f',
-                       $form->{"type"},         conv_i($form->{"language_id"}),   conv_i($form->{"taxzone_id"}), conv_i($form->{"shipto_id"}),
+                       $form->{"type"},         conv_i($form->{"language_id"}),   conv_i($form->{"taxzone_id"}), conv_i($form->{"shipto_id"}), conv_i($form->{billing_address_id}),
                 conv_i($form->{"employee_id"}), conv_i($form->{"salesman_id"}),   conv_i($form->{storno_id}),           $form->{"storno"} ? 't' : 'f',
                 conv_i($form->{"cp_id"}),            1 * $form->{marge_total} ,      1 * $form->{marge_percent},
                 conv_i($form->{"globalproject_id"}),                              conv_i($form->{"delivery_customer_id"}),
@@ -2030,6 +2030,7 @@ sub _retrieve_invoice {
            a.transdate AS invdate, a.deliverydate, a.tax_point, a.paid, a.storno, a.storno_id, a.gldate,
            a.shippingpoint, a.shipvia, a.notes, a.intnotes, a.taxzone_id,
            a.duedate, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.shipto_id, a.cp_id,
+           a.billing_address_id,
            a.employee_id, a.salesman_id, a.payment_id,
            a.mtime, a.itime,
            a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type,
@@ -2218,6 +2219,10 @@ sub get_customer {
          c.street, c.zipcode, c.city, c.country,
          c.notes AS intnotes, c.pricegroup_id as customer_pricegroup_id, c.taxzone_id, c.salesman_id, cu.name AS curr,
          c.taxincluded_checked, c.direct_debit,
+         (SELECT aba.id
+          FROM additional_billing_addresses aba
+          WHERE aba.default_address
+          LIMIT 1) AS default_billing_address_id,
          b.discount AS tradediscount, b.description AS business
        FROM customer c
        LEFT JOIN business b ON (b.id = c.business_id)
diff --git a/SL/OE.pm b/SL/OE.pm
index f3d3d0cd5..332d6345a 100644
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -755,7 +755,7 @@ SQL
          customer_id = ?, amount = ?, netamount = ?, reqdate = ?, tax_point = ?, taxincluded = ?,
          shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, currency_id = (SELECT id FROM currencies WHERE name=?), closed = ?,
          delivered = ?, proforma = ?, quotation = ?, department_id = ?, language_id = ?,
-         taxzone_id = ?, shipto_id = ?, payment_id = ?, delivery_vendor_id = ?, delivery_customer_id = ?,delivery_term_id = ?,
+         taxzone_id = ?, shipto_id = ?, billing_address_id = ?, payment_id = ?, delivery_vendor_id = ?, delivery_customer_id = ?,delivery_term_id = ?,
          globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?, marge_total = ?, marge_percent = ?
          , order_probability = ?, expected_billing_date = ?
        WHERE id = ?|;
@@ -770,7 +770,7 @@ SQL
              $form->{delivered} ? "t" : "f", $form->{proforma} ? 't' : 'f',
              $quotation, conv_i($form->{department_id}),
              conv_i($form->{language_id}), conv_i($form->{taxzone_id}),
-             conv_i($form->{shipto_id}), conv_i($form->{payment_id}),
+             conv_i($form->{shipto_id}), conv_i($form->{billing_address_id}), conv_i($form->{payment_id}),
              conv_i($form->{delivery_vendor_id}),
              conv_i($form->{delivery_customer_id}),
              conv_i($form->{delivery_term_id}),
@@ -1100,7 +1100,7 @@ sub _retrieve {
            o.closed, o.reqdate, o.tax_point, o.quonumber, o.department_id, o.cusordnumber,
            o.mtime, o.itime,
            d.description AS department, o.payment_id, o.language_id, o.taxzone_id,
-           o.delivery_customer_id, o.delivery_vendor_id, o.proforma, o.shipto_id,
+           o.delivery_customer_id, o.delivery_vendor_id, o.proforma, o.shipto_id, o.billing_address_id,
            o.globalproject_id, o.delivered, o.transaction_description, o.delivery_term_id,
            o.itime::DATE AS insertdate, o.order_probability, o.expected_billing_date
          FROM oe o
diff --git a/bin/mozilla/do.pl b/bin/mozilla/do.pl
index faaea12fa..923ec3835 100644
--- a/bin/mozilla/do.pl
+++ b/bin/mozilla/do.pl
@@ -98,7 +98,7 @@ sub add {
   $form->{show_details} = $::myconfig{show_form_details};
   $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
 
-  order_links();
+  order_links(is_new => 1);
   prepare_order();
   display_form();
 
@@ -168,6 +168,7 @@ sub order_links {
 
   check_do_access();
 
+  my %params   = @_;
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
@@ -186,6 +187,7 @@ sub order_links {
   } else {
     IS->get_customer(\%myconfig, \%$form);
     $form->{discount} = $form->{customer_discount};
+    $form->{billing_address_id} = $form->{default_billing_address_id} if $params{is_new};
   }
 
   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id delivery_term_id));
@@ -548,8 +550,12 @@ sub update_delivery_order {
   if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
     $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
 
-    IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
-    IR->get_vendor(\%myconfig, $form)   if $vc eq 'vendor';
+    if ($vc eq 'customer') {
+      IS->get_customer(\%myconfig, $form);
+      $::form->{billing_address_id} = $::form->{default_billing_address_id};
+    } else {
+      IR->get_vendor(\%myconfig, $form);
+    }
   }
 
   $form->{discount} =  $form->{"$form->{vc}_discount"} if defined $form->{"$form->{vc}_discount"};
@@ -935,8 +941,12 @@ sub save {
   if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
     $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
 
-    IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
-    IR->get_vendor(\%myconfig, $form)   if $vc eq 'vendor';
+    if ($vc eq 'customer') {
+      IS->get_customer(\%myconfig, $form);
+      $::form->{billing_address_id} = $::form->{default_billing_address_id};
+    } else {
+      IR->get_vendor(\%myconfig, $form);
+    }
 
     update();
     $::dispatcher->end_request;
diff --git a/bin/mozilla/is.pl b/bin/mozilla/is.pl
index 551c1d869..76d4b3edb 100644
--- a/bin/mozilla/is.pl
+++ b/bin/mozilla/is.pl
@@ -97,7 +97,7 @@ sub add {
 
   $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
 
-  &invoice_links;
+  invoice_links(is_new => 1);
   &prepare_invoice;
   &display_form;
 
@@ -154,6 +154,7 @@ sub invoice_links {
   # Delay access check to after the invoice's been loaded so that
   # project-specific invoice rights can be evaluated.
 
+  my %params   = @_;
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
 
@@ -172,6 +173,8 @@ sub invoice_links {
 
   IS->get_customer(\%myconfig, \%$form);
 
+  $form->{billing_address_id} = $form->{default_billing_address_id} if $params{is_new};
+
   $form->restore_vars(qw(id));
 
   IS->retrieve_invoice(\%myconfig, \%$form);
@@ -731,6 +734,7 @@ sub update {
     $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
 
     IS->get_customer(\%myconfig, $form);
+    $::form->{billing_address_id} = $::form->{default_billing_address_id};
   }
 
   $form->{taxincluded} ||= $taxincluded;
diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl
index 2f3803b88..a3425ecde 100644
--- a/bin/mozilla/oe.pl
+++ b/bin/mozilla/oe.pl
@@ -160,7 +160,7 @@ sub add {
 
   $form->{show_details} = $::myconfig{show_form_details};
 
-  &order_links;
+  order_links(is_new => 1);
   &prepare_order;
   &display_form;
 
@@ -245,6 +245,8 @@ sub edit {
 sub order_links {
   $main::lxdebug->enter_sub();
 
+  my (%params) = @_;
+
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
@@ -266,8 +268,12 @@ sub order_links {
   $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes shipto_id delivery_term_id currency));
 
   # get customer / vendor
-  IR->get_vendor(\%myconfig, \%$form)   if $form->{type} =~ /(purchase_order|request_quotation)/;
-  IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
+  if ($form->{type} =~ /(purchase_order|request_quotation)/) {
+    IR->get_vendor(\%myconfig, \%$form);
+  } else {
+    IS->get_customer(\%myconfig, \%$form);
+    $form->{billing_address_id} = $form->{default_billing_address_id} if $params{is_new};
+  }
 
   $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id delivery_term_id));
   $form->restore_vars(qw(currency))    if $form->{id};
@@ -775,8 +781,12 @@ sub update {
   if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
     $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
 
-    IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
-    IR->get_vendor(\%myconfig, $form)   if $vc eq 'vendor';
+    if ($vc eq 'customer') {
+      IS->get_customer(\%myconfig, $form);
+      $::form->{billing_address_id} = $::form->{default_billing_address_id};
+    } else {
+      IR->get_vendor(\%myconfig, $form);
+    }
   }
 
   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
@@ -1378,6 +1388,8 @@ sub save_and_close {
     IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
     IR->get_vendor(\%myconfig, $form)   if $vc eq 'vendor';
 
+    $::form->{billing_address_id} = $::form->{default_billing_address_id};
+
     update();
     $::dispatcher->end_request;
   }
@@ -1480,8 +1492,13 @@ sub save {
   if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
     $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
 
-    IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
-    IR->get_vendor(\%myconfig, $form)   if $vc eq 'vendor';
+    if ($vc eq 'customer') {
+      IS->get_customer(\%myconfig, $form);
+      $::form->{billing_address_id} = $::form->{default_billing_address_id};
+
+    } else {
+      IR->get_vendor(\%myconfig, $form);
+    }
 
     update();
     $::dispatcher->end_request;
diff --git a/locale/de/all b/locale/de/all
index 9bfe339f5..fb0257284 100755
--- a/locale/de/all
+++ b/locale/de/all
@@ -254,6 +254,7 @@ $self->{texts} = {
   'Added sections and function blocks: #1' => 'Hinzugefügte Abschnitte und Funktionsblöcke: #1',
   'Added text blocks: #1'       => 'Hinzugefügte Textblöcke: #1',
   'Addition'                    => 'Zusatz',
+  'Additional Billing Address'  => 'Zusätzliche Rechnungsadresse',
   'Additional Billing Addresses' => 'Zusätzliche Rechnungsadressen',
   'Additional articles'         => 'Zusätzliche Artikel',
   'Additional articles actions' => 'Aktionen zu zusätzlichen Artikeln',
@@ -861,6 +862,7 @@ $self->{texts} = {
   'Current version'             => 'Aktuelle Version',
   'Current year'                => 'Aktuelles Jahr',
   'Currently #1 delivery orders can be converted into invoices and printed.' => 'Momentan können #1 Lieferscheine in Rechnungen umgewandelt werden.',
+  'Custom Billing Address'      => 'Abweichende Rechnungsadresse',
   'Custom CSV format'           => 'Eigenes CSV-Format',
   'Custom Variables'            => 'Benutzerdefinierte Variablen',
   'Custom data export'          => 'Benutzerdefinierter Datenexport',
diff --git a/templates/webpages/common/show_vc_details.html b/templates/webpages/common/show_vc_details.html
index f37498033..cd9d4b3a4 100644
--- a/templates/webpages/common/show_vc_details.html
+++ b/templates/webpages/common/show_vc_details.html
@@ -1,11 +1,18 @@
 [%- USE T8 %]
-[% USE HTML %]
+[% USE HTML %][%- USE LxERP -%]
 <h1>[% IF is_customer %][% 'Customer details' | $T8 %][% ELSE %][% 'Vendor details' | $T8 %][% END %] &quot;[% HTML.escape(name) %]&quot;</h1>
 
 [% BLOCK jump_block %]
 [%- IF SHIPTO.size || CONTACTS.size %]
  <p>
   [% 'Jump to' | $T8 %] <a href="#billing">[% 'Billing Address' | $T8 %]</a>
+  [%- FOREACH additional_billing_addresses = ADDITIONAL_BILLING_ADDRESSES %]
+   ,
+   <a href="#additional_billing_address[% loop.count %]">
+    [% 'Additional Billing Address' | $T8 %]
+    "[% HTML.escape(additional_billing_addresses.name) %]"
+   </a>
+  [%- END %]
   [%- FOREACH shipto = SHIPTO %]
    ,
    <a href="#shipping[% loop.count %]">
@@ -26,7 +33,7 @@
 [%- END %]
 [% END %]
 
- [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO %]
+ [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO, ADDITIONAL_BILLING_ADDRESSES = ADDITIONAL_BILLING_ADDRESSES %]
 
  <a name="billing"><h1>[% 'Billing Address' | $T8 %]</h1></a>
 
@@ -182,13 +189,79 @@
  </table>
 
 
+ [% FOREACH row = ADDITIONAL_BILLING_ADDRESSES %]
+
+  <hr>
+
+  [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO, ADDITIONAL_BILLING_ADDRESSES = ADDITIONAL_BILLING_ADDRESSES %]
+
+  <a name="additional_billing_address[% loop.count %]"><h1>[% 'Additional Billing Address' | $T8 %] "[% HTML.escape(row.name) %]"</h1></a>
+
+  <table>
+   <tr>
+    <td align="right">[% 'Default Billing Address' | $T8 %]</td>
+    <td>[% row.default_address ? LxERP.t8('yes') : LxERP.t8('no') %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Name' | $T8 %]</td>
+    <td>[% HTML.escape(row.name) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Department' | $T8 %]</td>
+    <td>[% HTML.escape(row.department_1) %][% IF row.department_2 %][% IF row.department_1 %]; [% END %][% HTML.escape(row.department_2) %][% END %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Street' | $T8 %]</td>
+    <td>[% HTML.escape(row.street) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Zip, City' | $T8 %]</td>
+    <td>[% HTML.escape(row.zipcode) %] [% HTML.escape(row.city) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Country' | $T8 %]</td>
+    <td>[% HTML.escape(row.country) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'GLN' | $T8 %]</td>
+    <td>[% HTML.escape(row.gln) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Contact' | $T8 %]</td>
+    <td>[% HTML.escape(row.contact) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Phone' | $T8 %]</td>
+    <td>[% HTML.escape(row.phone) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'Fax' | $T8 %]</td>
+    <td>[% HTML.escape(row.fax) %]</td>
+   </tr>
+
+   <tr>
+    <td align="right">[% 'E-mail' | $T8 %]</td>
+    <td>[% HTML.escape(row.email) %]</td>
+   </tr>
+
+  </table>
+ [% END %]
 
 
  [% FOREACH row = SHIPTO %]
 
   <hr>
 
-  [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO %]
+  [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO, ADDITIONAL_BILLING_ADDRESSES = ADDITIONAL_BILLING_ADDRESSES %]
 
   <a name="shipping[% loop.count %]"><h1>[% 'Shipping Address' | $T8 %] &quot;[% HTML.escape(row.shiptoname) %]&quot;</h1></a>
 
@@ -259,7 +332,7 @@
 
   <hr>
 
-  [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO %]
+  [%- INCLUDE jump_block CONTACTS = CONTACTS, SHIPTO = SHIPTO, ADDITIONAL_BILLING_ADDRESSES = ADDITIONAL_BILLING_ADDRESSES %]
 
   <a name="contact[% loop.count %]"><h1>[% 'Contact Person' | $T8 %] &quot;[% HTML.escape(row.cp_name) %]&quot;</h1></a>
 
diff --git a/templates/webpages/do/form_header.html b/templates/webpages/do/form_header.html
index e5230f032..bee0ee6c4 100644
--- a/templates/webpages/do/form_header.html
+++ b/templates/webpages/do/form_header.html
@@ -169,6 +169,16 @@
          </td>
         </tr>
 
+        [%- IF (vc == 'customer') && VC_OBJ.additional_billing_addresses.as_list.size %]
+        <tr>
+          <th align="right">[% 'Custom Billing Address' | $T8 %]</th>
+          <td>
+            [% L.select_tag('billing_address_id', VC_OBJ.additional_billing_addresses,
+                            with_empty=1, default=billing_address_id, value_key='id', title_key='displayable_id', style='width: 250px') %]
+          </td>
+        </tr>
+        [%- END %]
+
         [%- IF business %]
         <tr>
          <th align="right">[% IF is_customer %][% 'Customer type' | $T8 %][% ELSE %][% 'Vendor type' | $T8 %][% END %]</th>
diff --git a/templates/webpages/is/form_header.html b/templates/webpages/is/form_header.html
index 3a3c17070..8f307fde2 100644
--- a/templates/webpages/is/form_header.html
+++ b/templates/webpages/is/form_header.html
@@ -79,6 +79,17 @@
            [% L.button_tag("kivi.SalesPurchase.edit_custom_shipto()", LxERP.t8("Custom shipto")) %]
           </td>
         </tr>
+
+[%- IF customer_obj.additional_billing_addresses.as_list.size %]
+        <tr>
+          <th align="right">[% 'Custom Billing Address' | $T8 %]</th>
+          <td>
+            [% L.select_tag('billing_address_id', customer_obj.additional_billing_addresses,
+                            with_empty=1, default=billing_address_id, value_key='id', title_key='displayable_id', style='width: 250px') %]
+          </td>
+        </tr>
+[%- END %]
+
         <tr>
           <td align="right">[% 'Credit Limit' | $T8 %]</td>
           <td>
diff --git a/templates/webpages/oe/form_header.html b/templates/webpages/oe/form_header.html
index e8325c35e..002fa5545 100644
--- a/templates/webpages/oe/form_header.html
+++ b/templates/webpages/oe/form_header.html
@@ -83,6 +83,17 @@
                       [% L.button_tag("kivi.SalesPurchase.edit_custom_shipto()", LxERP.t8("Custom shipto")) %]
                     </td>
                   </tr>
+
+[%- IF is_sales && vc_obj.additional_billing_addresses.as_list.size %]
+                  <tr>
+                    <th align="right">[% 'Custom Billing Address' | $T8 %]</th>
+                    <td>
+                      [% L.select_tag('billing_address_id', vc_obj.additional_billing_addresses,
+                                      with_empty=1, default=billing_address_id, value_key='id', title_key='displayable_id', style='width: 250px') %]
+                    </td>
+                  </tr>
+[%- END %]
+
 [%- IF is_order %]
                   <tr>
                     <td align="right">[% 'Credit Limit' | $T8 %]</td>
diff --git a/templates/webpages/order/tabs/basic_data.html b/templates/webpages/order/tabs/basic_data.html
index 96ef3e79b..490f9ac29 100644
--- a/templates/webpages/order/tabs/basic_data.html
+++ b/templates/webpages/order/tabs/basic_data.html
@@ -51,6 +51,21 @@
             </td>
           </tr>
 
+          [%- IF SELF.cv == "customer" %]
+          <tr id="billing_address_row"[% IF !SELF.order.customer.additional_billing_addresses.as_list.size %] style="display:none"[% END %]>
+            <th align="right">[% 'Custom Billing Address' | $T8 %]</th>
+            <td>
+              [% L.select_tag('order.billing_address_id',
+                               SELF.order.customer.additional_billing_addresses,
+                               default=SELF.order.billing_address_id,
+                               title_key='displayable_id',
+                               value_key='id',
+                               with_empty=1,
+                               style='width: 300px') %]
+            </td>
+          </tr>
+          [%- END %]
+
           [%- PROCESS order/tabs/_business_info_row.html SELF=SELF %]
 
           <tr>
