Zusätzliche Rechnungsadressen: Datenbankupgrade, MetaSetup, Rose-Models
authorMoritz Bunkus <m.bunkus@linet.de>
Mon, 25 Oct 2021 12:51:33 +0000 (14:51 +0200)
committerMoritz Bunkus <m.bunkus@linet.de>
Wed, 10 Nov 2021 15:06:56 +0000 (16:06 +0100)
SL/DB/AdditionalBillingAddress.pm [new file with mode: 0644]
SL/DB/Customer.pm
SL/DB/Helper/ALL.pm
SL/DB/Helper/Mappings.pm
SL/DB/Manager/AdditionalBillingAddress.pm [new file with mode: 0644]
SL/DB/MetaSetup/AdditionalBillingAddress.pm [new file with mode: 0644]
SL/DB/MetaSetup/DeliveryOrder.pm
SL/DB/MetaSetup/Invoice.pm
SL/DB/MetaSetup/Order.pm
sql/Pg-upgrade2/customer_additional_billing_addresses.sql [new file with mode: 0644]

diff --git a/SL/DB/AdditionalBillingAddress.pm b/SL/DB/AdditionalBillingAddress.pm
new file mode 100644 (file)
index 0000000..940dce8
--- /dev/null
@@ -0,0 +1,41 @@
+package SL::DB::AdditionalBillingAddress;
+
+use strict;
+
+use SL::DB::MetaSetup::AdditionalBillingAddress;
+use SL::DB::Manager::AdditionalBillingAddress;
+
+__PACKAGE__->meta->initialize;
+
+sub displayable_id {
+  my $self = shift;
+  my $text = join('; ', grep { $_ } (map({ $self->$_ } qw(name street)),
+                                     join(' ', grep { $_ }
+                                               map  { $self->$_ }
+                                               qw(zipcode city))));
+
+  return $text;
+}
+
+sub used {
+  my ($self) = @_;
+
+  return unless $self->id;
+
+  require SL::DB::Order;
+  require SL::DB::Invoice;
+  require SL::DB::DeliveryOrder;
+
+  my %args = (query => [ billing_address_id => $self->id ]);
+
+  return SL::DB::Manager::Invoice->get_all_count(%args)
+      || SL::DB::Manager::Order->get_all_count(%args)
+      || SL::DB::Manager::DeliveryOrder->get_all_count(%args);
+}
+
+sub detach {
+  $_[0]->customer_id(undef);
+  return $_[0];
+}
+
+1;
index 71c762a..2032a41 100644 (file)
@@ -29,6 +29,12 @@ use SL::DB::Helper::DisplayableNamePreferences (
 use SL::DB::VC;
 
 __PACKAGE__->meta->add_relationship(
+  additional_billing_addresses => {
+    type         => 'one to many',
+    class        => 'SL::DB::AdditionalBillingAddress',
+    column_map   => { id      => 'customer_id' },
+    manager_args => { sort_by => 'lower(additional_billing_addresses.name)' },
+  },
   shipto => {
     type         => 'one to many',
     class        => 'SL::DB::Shipto',
index e0ab578..aff03d9 100644 (file)
@@ -3,6 +3,7 @@ package SL::DB::Helper::ALL;
 use strict;
 
 use SL::DB::AccTransaction;
+use SL::DB::AdditionalBillingAddress;
 use SL::DB::Assembly;
 use SL::DB::AssortmentItem;
 use SL::DB::AuthClient;
index 94f7e47..0e9f230 100644 (file)
@@ -84,6 +84,7 @@ my @kivitendo_blacklist = (@kivitendo_blacklist_permanent, @kivitendo_blacklist_
 my %kivitendo_package_names = (
   # TABLE                           # MODEL (given in C style)
   acc_trans                      => 'acc_transaction',
+  additional_billing_addresses   => 'additional_billing_address',
   'auth.clients'                 => 'auth_client',
   'auth.clients_users'           => 'auth_client_user',
   'auth.clients_groups'          => 'auth_client_group',
diff --git a/SL/DB/Manager/AdditionalBillingAddress.pm b/SL/DB/Manager/AdditionalBillingAddress.pm
new file mode 100644 (file)
index 0000000..297d534
--- /dev/null
@@ -0,0 +1,14 @@
+# This file has been auto-generated only because it didn't exist.
+# Feel free to modify it at will; it will not be overwritten automatically.
+
+package SL::DB::Manager::AdditionalBillingAddress;
+
+use strict;
+
+use parent qw(SL::DB::Helper::Manager);
+
+sub object_class { 'SL::DB::AdditionalBillingAddress' }
+
+__PACKAGE__->make_manager_methods;
+
+1;
diff --git a/SL/DB/MetaSetup/AdditionalBillingAddress.pm b/SL/DB/MetaSetup/AdditionalBillingAddress.pm
new file mode 100644 (file)
index 0000000..3e37db9
--- /dev/null
@@ -0,0 +1,43 @@
+# This file has been auto-generated. Do not modify it; it will be overwritten
+# by rose_auto_create_model.pl automatically.
+package SL::DB::AdditionalBillingAddress;
+
+use strict;
+
+use parent qw(SL::DB::Object);
+
+__PACKAGE__->meta->table('additional_billing_addresses');
+
+__PACKAGE__->meta->columns(
+  city            => { type => 'text' },
+  contact         => { type => 'text' },
+  country         => { type => 'text' },
+  customer_id     => { type => 'integer' },
+  default_address => { type => 'boolean', default => 'false', not_null => 1 },
+  department_1    => { type => 'text' },
+  department_2    => { type => 'text' },
+  email           => { type => 'text' },
+  fax             => { type => 'text' },
+  gln             => { type => 'text' },
+  id              => { type => 'serial', not_null => 1 },
+  itime           => { type => 'timestamp', default => 'now()', not_null => 1 },
+  mtime           => { type => 'timestamp', default => 'now()', not_null => 1 },
+  name            => { type => 'text' },
+  phone           => { type => 'text' },
+  street          => { type => 'text' },
+  zipcode         => { type => 'text' },
+);
+
+__PACKAGE__->meta->primary_key_columns([ 'id' ]);
+
+__PACKAGE__->meta->allow_inline_column_values(1);
+
+__PACKAGE__->meta->foreign_keys(
+  customer => {
+    class       => 'SL::DB::Customer',
+    key_columns => { customer_id => 'id' },
+  },
+);
+
+1;
+;
index 780f431..f24fc9f 100644 (file)
@@ -9,6 +9,7 @@ use parent qw(SL::DB::Object);
 __PACKAGE__->meta->table('delivery_orders');
 
 __PACKAGE__->meta->columns(
+  billing_address_id      => { type => 'integer' },
   closed                  => { type => 'boolean', default => 'false' },
   cp_id                   => { type => 'integer' },
   currency_id             => { type => 'integer', not_null => 1 },
@@ -48,6 +49,11 @@ __PACKAGE__->meta->primary_key_columns([ 'id' ]);
 __PACKAGE__->meta->allow_inline_column_values(1);
 
 __PACKAGE__->meta->foreign_keys(
+  billing_address => {
+    class       => 'SL::DB::AdditionalBillingAddress',
+    key_columns => { billing_address_id => 'id' },
+  },
+
   contact => {
     class       => 'SL::DB::Contact',
     key_columns => { cp_id => 'cp_id' },
index 9b10c93..441f9c5 100644 (file)
@@ -10,6 +10,7 @@ __PACKAGE__->meta->table('ar');
 
 __PACKAGE__->meta->columns(
   amount                    => { type => 'numeric', default => '0', not_null => 1, precision => 15, scale => 5 },
+  billing_address_id        => { type => 'integer' },
   cp_id                     => { type => 'integer' },
   currency_id               => { type => 'integer', not_null => 1 },
   cusordnumber              => { type => 'text' },
@@ -64,6 +65,11 @@ __PACKAGE__->meta->primary_key_columns([ 'id' ]);
 __PACKAGE__->meta->allow_inline_column_values(1);
 
 __PACKAGE__->meta->foreign_keys(
+  billing_address => {
+    class       => 'SL::DB::AdditionalBillingAddress',
+    key_columns => { billing_address_id => 'id' },
+  },
+
   contact => {
     class       => 'SL::DB::Contact',
     key_columns => { cp_id => 'cp_id' },
index 6e707ae..5dda642 100644 (file)
@@ -10,6 +10,7 @@ __PACKAGE__->meta->table('oe');
 
 __PACKAGE__->meta->columns(
   amount                  => { type => 'numeric', precision => 15, scale => 5 },
+  billing_address_id      => { type => 'integer' },
   closed                  => { type => 'boolean', default => 'false' },
   cp_id                   => { type => 'integer' },
   currency_id             => { type => 'integer', not_null => 1 },
@@ -57,6 +58,11 @@ __PACKAGE__->meta->primary_key_columns([ 'id' ]);
 __PACKAGE__->meta->allow_inline_column_values(1);
 
 __PACKAGE__->meta->foreign_keys(
+  billing_address => {
+    class       => 'SL::DB::AdditionalBillingAddress',
+    key_columns => { billing_address_id => 'id' },
+  },
+
   contact => {
     class       => 'SL::DB::Contact',
     key_columns => { cp_id => 'cp_id' },
diff --git a/sql/Pg-upgrade2/customer_additional_billing_addresses.sql b/sql/Pg-upgrade2/customer_additional_billing_addresses.sql
new file mode 100644 (file)
index 0000000..11fcfb0
--- /dev/null
@@ -0,0 +1,45 @@
+-- @tag: customer_additional_billing_addresses
+-- @description: Kundenstammdaten: zusätzliche Rechnungsadressen
+-- @depends: release_3_5_8
+CREATE TABLE additional_billing_addresses (
+  id              SERIAL,
+  customer_id     INTEGER,
+  name            TEXT,
+  department_1    TEXT,
+  department_2    TEXT,
+  contact         TEXT,
+  street          TEXT,
+  zipcode         TEXT,
+  city            TEXT,
+  country         TEXT,
+  gln             TEXT,
+  email           TEXT,
+  phone           TEXT,
+  fax             TEXT,
+  default_address BOOLEAN NOT NULL DEFAULT FALSE,
+
+  itime           TIMESTAMP NOT NULL DEFAULT now(),
+  mtime           TIMESTAMP NOT NULL DEFAULT now(),
+
+  PRIMARY KEY (id),
+  FOREIGN KEY (customer_id) REFERENCES customer (id)
+);
+
+CREATE TRIGGER mtime_additional_billing_addresses
+BEFORE UPDATE ON additional_billing_addresses
+FOR EACH ROW EXECUTE PROCEDURE set_mtime();
+
+ALTER TABLE oe
+  ADD COLUMN billing_address_id INTEGER,
+  ADD FOREIGN KEY (billing_address_id)
+    REFERENCES additional_billing_addresses (id);
+
+ALTER TABLE delivery_orders
+  ADD COLUMN billing_address_id INTEGER,
+  ADD FOREIGN KEY (billing_address_id)
+    REFERENCES additional_billing_addresses (id);
+
+ALTER TABLE ar
+  ADD COLUMN billing_address_id INTEGER,
+  ADD FOREIGN KEY (billing_address_id)
+    REFERENCES additional_billing_addresses (id);