PriceRule: Erste Version
[kivitendo-erp.git] / SL / DB / Manager / PriceRuleItem.pm
diff --git a/SL/DB/Manager/PriceRuleItem.pm b/SL/DB/Manager/PriceRuleItem.pm
new file mode 100644 (file)
index 0000000..ef6cc9c
--- /dev/null
@@ -0,0 +1,72 @@
+# 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::PriceRuleItem;
+
+use strict;
+
+use SL::DB::Helper::Manager;
+use base qw(SL::DB::Helper::Manager);
+
+sub object_class { 'SL::DB::PriceRuleItem' }
+
+__PACKAGE__->make_manager_methods;
+
+use SL::Locale::String qw(t8);
+
+my @types = qw(
+  customer vendor business partsgroup qty reqdate pricegroup
+);
+
+my %ops = (
+  'num'  => { eq => '=', lt => '<', gt => '>' },
+  'date' => { eq => '=', lt => '<', gt => '>' },
+);
+
+my %types = (
+  'customer'            => { description => t8('Customer'),           customer => 1, vendor => 0, data_type => 'int',  data => sub { $_[0]->customer->id }, },
+  'vendor'              => { description => t8('Vendor'),             customer => 0, vendor => 1, data_type => 'int',  data => sub { $_[0]->vendor->id }, },
+  'business'            => { description => t8('Type of Business'),   customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[0]->customervendor->business_id }, },
+  'reqdate'             => { description => t8('Reqdate'),            customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->reqdate }, ops => 'date' },
+  'pricegroup'          => { description => t8('Pricegroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->pricegroup_id }, },
+  'partsgroup'          => { description => t8('Group'),              customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->partsgroup_id }, },
+  'qty'                 => { description => t8('Qty'),                customer => 1, vendor => 1, data_type => 'num',  data => sub { $_[1]->qty }, ops => 'num' },
+);
+
+sub not_matching_sql_and_values {
+  my ($class, %params) = @_;
+
+  die 'must be called with a customer/vendor type' unless $params{type};
+
+  my (@tokens, @values);
+
+  for my $type (@types) {
+    my $def = $types{$type};
+    next unless $def->{$params{type}};
+
+    if ($def->{ops}) {
+      my $ops = $ops{$def->{ops}};
+
+      my @sub_tokens;
+      for (keys %$ops) {
+        push @sub_tokens, "op = '$_' AND NOT value_$def->{data_type} $ops->{$_} ?";
+        push @values, $def->{data};
+      }
+
+      push @tokens, "type = '$type' AND " . join ' OR ', map "($_)", @sub_tokens;
+    } else {
+      push @tokens, "type = '$type' AND NOT value_$def->{data_type} = ?";
+      push @values, $def->{data};
+    }
+  }
+
+  return join(' OR ', map "($_)", @tokens), @values;
+}
+
+sub get_all_types {
+  my ($class, $vc) = @_;
+
+  [ map { [ $_, $types{$_}{description} ] } grep { $types{$_}{$vc} } map { $_ } @types ];
+}
+
+1;