DeliveryOrder: type_data nach SL/DB verschoben, damit das model auch drauf zugreifen...
authorSven Schöling <s.schoeling@googlemail.com>
Fri, 1 Oct 2021 13:57:00 +0000 (15:57 +0200)
committerJan Büren <jan@kivitendo.de>
Mon, 14 Feb 2022 13:53:49 +0000 (14:53 +0100)
SL/Controller/DeliveryOrder/TypeData.pm
SL/DB/DeliveryOrder/TypeData.pm [new file with mode: 0644]

index 6e49d42..52366f2 100644 (file)
@@ -4,214 +4,65 @@ use strict;
 use Exporter qw(import);
 use Scalar::Util qw(weaken);
 use SL::Locale::String qw(t8);
-
-use constant {
-  SALES_DELIVERY_ORDER_TYPE    => 'sales_delivery_order',
-  PURCHASE_DELIVERY_ORDER_TYPE => 'purchase_delivery_order',
-  SUPPLIER_DELIVERY_ORDER_TYPE => 'supplier_delivery_order',
-  RMA_DELIVERY_ORDER_TYPE      => 'rma_delivery_order',
-};
-
-our @EXPORT_OK = qw(SALES_DELIVERY_ORDER_TYPE PURCHASE_DELIVERY_ORDER_TYPE SUPPLIER_DELIVERY_ORDER_TYPE RMA_DELIVERY_ORDER_TYPE);
+use SL::DB::DeliveryOrder::TypeData qw(:types :subs);
 
 use Rose::Object::MakeMethods::Generic scalar => [ qw(c) ];
 
-my %type_data = (
-  SALES_DELIVERY_ORDER_TYPE() => {
-    text => {
-      delete => t8('Delivery Order has been deleted'),
-      saved  => t8('Delivery Order has been saved'),
-      add    => t8("Add Sales Delivery Order"),
-      edit   => t8("Edit Sales Delivery Order"),
-    },
-    show_menu => {
-      save_and_quotation      => 0,
-      save_and_rfq            => 0,
-      save_and_sales_order    => 0,
-      save_and_purchase_order => 0,
-      save_and_delivery_order => 0,
-      save_and_ap_transaction => 0,
-      save_and_invoice        => 0,
-      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
-    },
-    properties => {
-      customervendor => "customer",
-      is_customer    => 1,
-      nr_key         => "donumber",
-      transfer       => 'out',
-    },
-    part_classification_query => [ "used_for_sale" => 1 ],
-    right => "sales_delivery_order_edit",
-  },
-  PURCHASE_DELIVERY_ORDER_TYPE() => {
-    text => {
-      delete => t8('Delivery Order has been deleted'),
-      saved  => t8('Delivery Order has been saved'),
-      add    => t8("Add Purchase Delivery Order"),
-      edit   => t8("Edit Purchase Delivery Order"),
-    },
-    show_menu => {
-      save_and_quotation      => 0,
-      save_and_rfq            => 0,
-      save_and_sales_order    => 0,
-      save_and_purchase_order => 0,
-      save_and_delivery_order => 0,
-      save_and_ap_transaction => 0,
-      save_and_invoice        => 0,
-      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
-    },
-    properties => {
-      customervendor => "vendor",
-      is_customer    => 0,
-      nr_key         => "donumber",
-      transfer       => 'in',
-    },
-    part_classification_query => [ "used_for_purchase" => 1 ],
-    right => "purchase_delivery_order_edit",
-  },
-  SUPPLIER_DELIVERY_ORDER_TYPE() => {
-    text => {
-      delete => t8('Delivery Order has been deleted'),
-      saved  => t8('Delivery Order has been saved'),
-      add    => t8("Add Supplier Delivery Order"),
-      edit   => t8("Edit Supplier Delivery Order"),
-    },
-    show_menu => {
-      save_and_quotation      => 0,
-      save_and_rfq            => 0,
-      save_and_sales_order    => 0,
-      save_and_purchase_order => 0,
-      save_and_delivery_order => 0,
-      save_and_ap_transaction => 0,
-      save_and_invoice        => 0,
-      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
-    },
-    properties => {
-      customervendor => "vendor",
-      is_customer    => 0,
-      nr_key         => "donumber",
-      transfer       => 'out',
-    },
-    part_classification_query => [ "used_for_purchase" => 1 ],
-    right => "purchase_delivery_order_edit",
-  },
-  RMA_DELIVERY_ORDER_TYPE() => {
-    text => {
-      delete => t8('Delivery Order has been deleted'),
-      saved  => t8('Delivery Order has been saved'),
-      add    => t8("Add RMA Delivery Order"),
-      edit   => t8("Edit RMA Delivery Order"),
-    },
-    show_menu => {
-      save_and_quotation      => 0,
-      save_and_rfq            => 0,
-      save_and_sales_order    => 0,
-      save_and_purchase_order => 0,
-      save_and_delivery_order => 0,
-      save_and_ap_transaction => 0,
-      save_and_invoice        => 0,
-      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
-    },
-    properties => {
-      customervendor => "customer",
-      is_customer    => 0,
-      nr_key         => "donumber",
-      transfer       => 'in',
-    },
-    part_classification_query => [ "used_for_sale" => 1 ],
-    right => "sales_delivery_order_edit",
-  },
-);
-
 sub new {
   my ($class, $controller) = @_;
   my $o = bless {}, $class;
-  $o->c($controller);
-  weaken($o->{c});
-
-  return $o;
-}
-
-sub valid_types {
-  [
-    SALES_DELIVERY_ORDER_TYPE,
-    PURCHASE_DELIVERY_ORDER_TYPE,
-    SUPPLIER_DELIVERY_ORDER_TYPE,
-    RMA_DELIVERY_ORDER_TYPE,
-  ];
-}
-
-sub type {
-  $_[0]->c->type;
-}
-
-sub _get {
-  my ($self, $key) = @_;
-
-  my $ret = $type_data{$self->type}->{$key} // die "unknown property '$key'";
 
-  ref $ret eq 'CODE'
-    ? $ret->()
-    : $ret;
-}
-
-sub _get3 {
-  my ($self, $topic, $key) = @_;
-
-  my $ret = $type_data{$self->type}->{$topic}->{$key} // die "unknown property '$key' in topic '$topic'";
+  if ($controller) {
+    $o->c($controller);
+    weaken($o->{c});
+  }
 
-  ref $ret eq 'CODE'
-    ? $ret->()
-    : $ret;
+  return $o;
 }
 
 sub text {
   my ($self, $string) = @_;
-  _get3($self, "text", $string);
+  get3($self->c->type, "text", $string);
 }
 
 sub show_menu {
   my ($self, $string) = @_;
-  _get3($self, "show_menu", $string);
+  get3($self->c->type, "show_menu", $string);
 }
 
 sub workflow {
   my ($self, $string) = @_;
-  _get3($self, "workflow", $string);
+  get3($self->c->type, "workflow", $string);
 }
 
 sub properties {
   my ($self, $string) = @_;
-  _get3($self, "properties", $string);
-}
-
-sub is_valid_type {
-  !!exists $type_data{$_[1]};
-}
-
-sub type_data {
-  $type_data{ $_[0]->type } // die "unknown type";
+  get3($self->c->type, "properties", $string);
 }
 
 sub access {
-  _get($_[0], "right");
+  get($_[0]->c->type, "right");
 }
 
 sub is_quotation {
-  _get3($_[0], "properties", "is_quotation");
+  get3($_[0]->c->type, "properties", "is_quotation");
 }
 
 sub customervendor {
-  _get3($_[0], "properties", "customervendor");
+  get3($_[0]->c->type, "properties", "customervendor");
+}
+
+sub is_customer {
+  get3($_[0]->c->type, "properties", "is_customer");
 }
 
 sub nr_key {
-  _get3($_[0], "properties", "nr_key");
+  get3($_[0]->c->type, "properties", "nr_key");
 }
 
 sub part_classification_query {
   my ($self, $string) = @_;
-  _get($self, "part_classification_query");
+  get($self->c->type, "part_classification_query");
 }
 
 sub set_reqdate_by_type {
@@ -231,3 +82,5 @@ sub get_reqdate_by_type {
     return $reqdate;
   }
 }
+
+1;
diff --git a/SL/DB/DeliveryOrder/TypeData.pm b/SL/DB/DeliveryOrder/TypeData.pm
new file mode 100644 (file)
index 0000000..c26b7c5
--- /dev/null
@@ -0,0 +1,171 @@
+package SL::DB::DeliveryOrder::TypeData;
+
+use strict;
+use Exporter qw(import);
+use Scalar::Util qw(weaken);
+use SL::Locale::String qw(t8);
+
+use constant {
+  SALES_DELIVERY_ORDER_TYPE    => 'sales_delivery_order',
+  PURCHASE_DELIVERY_ORDER_TYPE => 'purchase_delivery_order',
+  SUPPLIER_DELIVERY_ORDER_TYPE => 'supplier_delivery_order',
+  RMA_DELIVERY_ORDER_TYPE      => 'rma_delivery_order',
+};
+
+my @export_types = qw(SALES_DELIVERY_ORDER_TYPE PURCHASE_DELIVERY_ORDER_TYPE SUPPLIER_DELIVERY_ORDER_TYPE RMA_DELIVERY_ORDER_TYPE);
+my @export_subs = qw(valid_types validate_type is_valid_type get get3);
+
+our @EXPORT_OK = (@export_types, @export_subs);
+our %EXPORT_TAGS = (types => \@export_types, subs => \@export_subs);
+
+my %type_data = (
+  SALES_DELIVERY_ORDER_TYPE() => {
+    text => {
+      delete => t8('Delivery Order has been deleted'),
+      saved  => t8('Delivery Order has been saved'),
+      add    => t8("Add Sales Delivery Order"),
+      edit   => t8("Edit Sales Delivery Order"),
+    },
+    show_menu => {
+      save_and_quotation      => 0,
+      save_and_rfq            => 0,
+      save_and_sales_order    => 0,
+      save_and_purchase_order => 0,
+      save_and_delivery_order => 0,
+      save_and_ap_transaction => 0,
+      save_and_invoice        => 0,
+      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
+    },
+    properties => {
+      customervendor => "customer",
+      is_customer    => 1,
+      nr_key         => "donumber",
+      transfer       => 'out',
+    },
+    part_classification_query => [ "used_for_sale" => 1 ],
+    right => "sales_delivery_order_edit",
+  },
+  PURCHASE_DELIVERY_ORDER_TYPE() => {
+    text => {
+      delete => t8('Delivery Order has been deleted'),
+      saved  => t8('Delivery Order has been saved'),
+      add    => t8("Add Purchase Delivery Order"),
+      edit   => t8("Edit Purchase Delivery Order"),
+    },
+    show_menu => {
+      save_and_quotation      => 0,
+      save_and_rfq            => 0,
+      save_and_sales_order    => 0,
+      save_and_purchase_order => 0,
+      save_and_delivery_order => 0,
+      save_and_ap_transaction => 0,
+      save_and_invoice        => 0,
+      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
+    },
+    properties => {
+      customervendor => "vendor",
+      is_customer    => 0,
+      nr_key         => "donumber",
+      transfer       => 'in',
+    },
+    part_classification_query => [ "used_for_purchase" => 1 ],
+    right => "purchase_delivery_order_edit",
+  },
+  SUPPLIER_DELIVERY_ORDER_TYPE() => {
+    text => {
+      delete => t8('Delivery Order has been deleted'),
+      saved  => t8('Delivery Order has been saved'),
+      add    => t8("Add Supplier Delivery Order"),
+      edit   => t8("Edit Supplier Delivery Order"),
+    },
+    show_menu => {
+      save_and_quotation      => 0,
+      save_and_rfq            => 0,
+      save_and_sales_order    => 0,
+      save_and_purchase_order => 0,
+      save_and_delivery_order => 0,
+      save_and_ap_transaction => 0,
+      save_and_invoice        => 0,
+      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
+    },
+    properties => {
+      customervendor => "vendor",
+      is_customer    => 0,
+      nr_key         => "donumber",
+      transfer       => 'out',
+    },
+    part_classification_query => [ "used_for_purchase" => 1 ],
+    right => "purchase_delivery_order_edit",
+  },
+  RMA_DELIVERY_ORDER_TYPE() => {
+    text => {
+      delete => t8('Delivery Order has been deleted'),
+      saved  => t8('Delivery Order has been saved'),
+      add    => t8("Add RMA Delivery Order"),
+      edit   => t8("Edit RMA Delivery Order"),
+    },
+    show_menu => {
+      save_and_quotation      => 0,
+      save_and_rfq            => 0,
+      save_and_sales_order    => 0,
+      save_and_purchase_order => 0,
+      save_and_delivery_order => 0,
+      save_and_ap_transaction => 0,
+      save_and_invoice        => 0,
+      delete                  => sub { $::instance_conf->get_sales_delivery_order_show_delete },
+    },
+    properties => {
+      customervendor => "customer",
+      is_customer    => 0,
+      nr_key         => "donumber",
+      transfer       => 'in',
+    },
+    part_classification_query => [ "used_for_sale" => 1 ],
+    right => "sales_delivery_order_edit",
+  },
+);
+
+my @valid_types = (
+  SALES_DELIVERY_ORDER_TYPE,
+  PURCHASE_DELIVERY_ORDER_TYPE,
+  SUPPLIER_DELIVERY_ORDER_TYPE,
+  RMA_DELIVERY_ORDER_TYPE,
+);
+
+my %valid_types = map { $_ => $_ } @valid_types;
+
+sub valid_types {
+  \@valid_types;
+}
+
+sub is_valid_type {
+  !!exists $type_data{$_[0]};
+}
+
+sub validate_type {
+  my ($self, $type) = @_;
+
+  return $valid_types{$type} // die "invalid type";
+}
+
+sub get {
+  my ($type, $key) = @_;
+
+  my $ret = $type_data{$type}->{$key} // die "unknown property '$key'";
+
+  ref $ret eq 'CODE'
+    ? $ret->()
+    : $ret;
+}
+
+sub get3 {
+  my ($type, $topic, $key) = @_;
+
+  my $ret = $type_data{$type}{$topic}{$key} // die "unknown property '$key' in topic '$topic'";
+
+  ref $ret eq 'CODE'
+    ? $ret->()
+    : $ret;
+}
+
+1;