Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f7e3b59b

Von Sven Schöling vor fast 2 Jahren hinzugefügt

  • ID f7e3b59b3c19a79055e4288fe328a5e249eaabc5
  • Vorgänger fd96961c
  • Nachfolger cdb719c4

PriceRule: Experimenteller Support für CVar Preisregeln

Nur implementiert für Waren cvars mit type select.
Es wird erwartet dass die Preisregel die cvar config verlinkt, und den
ausgewählten Typ als value_text enthält.

Grober "editable" support - wobei erwartet wird, dass editable cvars
auch überschrieben werden beim speichern.

cvar Preisregeln triggern nicht auf Belege, in dessen Artikel die cvar
deaktiviert ist.

Unterschiede anzeigen:

SL/DB/Manager/PriceRuleItem.pm
14 14

  
15 15
use SL::Locale::String qw(t8);
16 16

  
17
my @types = qw(
18
  part customer vendor business partsgroup qty reqdate transdate pricegroup
19
);
20

  
21 17
my %ops = (
22 18
  'num'  => { eq => '=', le => '<=', ge => '>=' },
23 19
  'date' => { eq => '=', lt => '<', gt => '>' },
24 20
);
25 21

  
26
my %types = (
27
  'customer'            => { description => t8('Customer'),           customer => 1, vendor => 0, data_type => 'int',  data => sub { $_[0]->customer->id }, },
28
  'vendor'              => { description => t8('Vendor'),             customer => 0, vendor => 1, data_type => 'int',  data => sub { $_[0]->vendor->id }, },
29
  'business'            => { description => t8('Type of Business'),   customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[0]->customervendor->business_id }, exclude_nulls => 1 },
30
  'reqdate'             => { description => t8('Reqdate'),            customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->reqdate }, ops => 'date' },
31
  'transdate'           => { description => t8('Transdate'),          customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->transdate }, ops => 'date' },
32
  'part'                => { description => t8('Part'),               customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->id }, },
33
  'pricegroup'          => { description => t8('Pricegroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->pricegroup_id }, exclude_nulls => 1 },
34
  'partsgroup'          => { description => t8('Partsgroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->partsgroup_id }, exclude_nulls => 1 },
35
  'qty'                 => { description => t8('Qty'),                customer => 1, vendor => 1, data_type => 'num',  data => sub { $_[1]->qty }, ops => 'num' },
22
my @types = (
23
  { type => 'customer',   description => t8('Customer'),           customer => 1, vendor => 0, data_type => 'int',  data => sub { $_[0]->customer->id }, },
24
  { type => 'vendor',     description => t8('Vendor'),             customer => 0, vendor => 1, data_type => 'int',  data => sub { $_[0]->vendor->id }, },
25
  { type => 'business',   description => t8('Type of Business'),   customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[0]->customervendor->business_id }, exclude_nulls => 1 },
26
  { type => 'reqdate',    description => t8('Reqdate'),            customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->reqdate }, ops => 'date' },
27
  { type => 'transdate',  description => t8('Transdate'),          customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->transdate }, ops => 'date' },
28
  { type => 'part',       description => t8('Part'),               customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->id }, },
29
  { type => 'pricegroup', description => t8('Pricegroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->pricegroup_id }, exclude_nulls => 1 },
30
  { type => 'partsgroup', description => t8('Partsgroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->partsgroup_id }, exclude_nulls => 1 },
31
  { type => 'qty',        description => t8('Qty'),                customer => 1, vendor => 1, data_type => 'num',  data => sub { $_[1]->qty }, ops => 'num' },
36 32
);
37 33

  
38 34
sub not_matching_sql_and_values {
......
43 39

  
44 40
  my (@tokens, @values);
45 41

  
46
  for my $type (@types) {
47
    my $def = $types{$type};
42
  for my $def (@types, cached_cvar_types()) {
43
    my $type = $def->{type};
48 44
    next unless $def->{$params{type}};
49 45

  
50 46
    my $value = $def->{data}->(@args);
51 47

  
48
    my $type_token = $def->{cvar_config} ? "custom_variable_configs_id = '$def->{cvar_config}'" : "type = '$type'";
49

  
52 50
    if ($def->{exclude_nulls} && !defined $value) {
53
      push @tokens, "type = '$type'";
51
      push @tokens, $type_token;
54 52
    } else {
55 53
      my @sub_tokens;
56 54
      if ($def->{ops}) {
......
65 63
        push @values, $value;
66 64
      }
67 65

  
68
      push @tokens, "type = '$type' AND (@{[ join(' OR ', map qq|($_)|, @sub_tokens) ]})";
66
      push @tokens, "$type_token AND (@{[ join(' OR ', map qq|($_)|, @sub_tokens) ]})";
69 67
    }
70 68
  }
71 69

  
72 70
  return join(' OR ', map "($_)", @tokens), @values;
73 71
}
74 72

  
73
sub cached_cvar_types {
74
  my $cache = $::request->cache("SL::DB::PriceRuleItem::cvar_types", []);
75

  
76
  @$cache = generate_cvar_types() if !@$cache;
77
  @$cache
78
}
79

  
80
# we only generate cvar types for cvar price_rules that are actually used to keep the query smaller
81
# these are cached per request
82
sub generate_cvar_types {
83
  my $cvar_configs = SL::DB::Manager::CustomVariableConfig->get_all(query => [ id => \"(select distinct custom_variable_configs_id from price_rule_items where custom_variable_configs_id is not null)" ]);
84

  
85
  my @types;
86

  
87
  for my $config (@$cvar_configs) {
88

  
89
    # NOTE! this only works for non-editable selects.
90
    # if the cvar is editable, it needs to be pulled from the record itself
91
    push @types, {
92
      type          => "cvar_" . $config->id,
93
      description   => $config->description,
94
      customer      => 1,
95
      vendor        => 1,
96
      data_type     => 'text',
97
      data          => sub { $_[1]->part->cvar_by_name($config->name)->value },
98
      exclude_nulls => 1,
99
      cvar_config   => $config->id,
100
    } if $config->module eq 'IC' && !$config->processed_flags->{editable} && $config->type eq 'select';
101

  
102
    # NOTE! this only works for editable selects.
103
    # if the cvar is editable, it needs to be pulled from the record itself
104
    push @types, {
105
      type          => "cvar_" . $config->id,
106
      description   => $config->description,
107
      customer      => 1,
108
      vendor        => 1,
109
      data_type     => 'text',
110
      data          => sub { $_[1]->cvar_by_name($config->name)->value },
111
      exclude_nulls => 1,
112
      cvar_config   => $config->id,
113
    } if $config->module eq 'IC' && $config->processed_flags->{editable} && $config->type eq 'select';
114

  
115
  }
116

  
117
  @types;
118
}
119

  
75 120
sub get_all_types {
76 121
  my ($class, $vc) = @_;
77 122

  
78 123
  $vc
79
  ? [ map { [ $_, $types{$_}{description} ] } grep { $types{$_}{$vc} } map { $_ } @types ]
80
  : [ map { [ $_, $types{$_}{description} ] } map { $_ } @types ]
124
  ? [ map { [ $_->{type}, $_->{description} ] } grep { $_->{$vc} } @types ]
125
  : [ map { [ $_->{type}, $_->{description} ] } @types ]
81 126
}
82 127

  
83 128
sub get_type {
84
  $types{$_[1]}
129
  grep { $_->{type} eq $_[1] } @types
85 130
}
86 131

  
87 132
sub filter_match {
88 133
  my ($self, $type, $value) = @_;
89 134

  
90
  my $type_def = $types{$type};
135
  my $type_def = first { $_->{type} eq $type } @types;
91 136

  
92 137
  if (!$type_def->{ops}) {
93 138
    my $evalue   = $::form->get_standard_dbh->quote($value);

Auch abrufbar als: Unified diff