1 # This file as been auto-generated only because it didn't exist.
 
   2 # Feel free to modify it at will; it will not be overwritten automatically.
 
   4 package SL::DB::Manager::PriceRule;
 
   8 use parent qw(SL::DB::Helper::Manager);
 
  10 use constant PRICE_NEW                 => 0;
 
  11 use constant PRICE_REDUCED_MASTER_DATA => 1;
 
  12 use constant PRICE_DISCOUNT            => 2;
 
  14 use SL::DB::Helper::Filtered;
 
  15 use SL::DB::Helper::Paginated;
 
  16 use SL::DB::Helper::Sorted;
 
  19 use SL::Locale::String qw(t8);
 
  21 sub object_class { 'SL::DB::PriceRule' }
 
  23 __PACKAGE__->make_manager_methods;
 
  24 __PACKAGE__->add_filter_specs(
 
  25   has_item_type => sub {
 
  26     my ($key, $values, $prefix) = @_;
 
  27     return unless @$values;
 
  29     my $each_type = "SELECT DISTINCT price_rules_id FROM price_rule_items WHERE type = %s";
 
  30     my $sub_query = join ' INTERSECT ', map { sprintf $each_type, $::form->get_standard_dbh->quote($_) } @$values;
 
  31     return or => [ ${prefix} . 'id' => [ \$sub_query ] ];
 
  33   item_type_matches => sub {
 
  34     my ($key, $values, $prefix) = @_;
 
  35     return unless @$values;
 
  36     return unless 'HASH' eq ref $values->[0];
 
  37     return unless grep $_, values %{ $values->[0] };
 
  39     my $each_type = "SELECT DISTINCT price_rules_id FROM price_rule_items WHERE type = %s AND (%s)";
 
  40     my $sub_query = join ' INTERSECT ', map {
 
  41       sprintf $each_type, $::form->get_standard_dbh->quote($_), SL::DB::Manager::PriceRuleItem->filter_match($_, $values->[0]{$_})
 
  42     } grep { $values->[0]{$_} } keys %{ $values->[0] };
 
  43     return or => [ ${prefix} . 'id' => [ \$sub_query ] ];
 
  47 sub get_matching_filter {
 
  48   my ($class, %params) = @_;
 
  50   die 'need record'      unless $params{record};
 
  51   die 'need record_item' unless $params{record_item};
 
  53   my $type = $params{record}->is_sales ? 'customer' : 'vendor';
 
  55   # plan: 1. search all rule_items that do NOT match this record/record item combo
 
  56   my ($sub_where, @values) = SL::DB::Manager::PriceRuleItem->not_matching_sql_and_values(type => $type, %params);
 
  58   # now union all NOT matching, invert ids, load these
 
  59   my $matching_rule_ids = <<SQL;
 
  60     SELECT id FROM price_rules
 
  62       SELECT price_rules_id FROM price_rule_items WHERE $sub_where
 
  64     AND type = ? AND NOT obsolete
 
  69   return $matching_rule_ids, @values;
 
  72 sub get_all_matching {
 
  73   my ($self, %params) = @_;
 
  75   my ($query, @values) = $self->get_matching_filter(%params);
 
  76   my @ids = selectall_ids($::form, $::form->get_standard_dbh, $query, 0, @values);
 
  78   return [] unless @ids;
 
  80   $self->get_all(query => [ id => \@ids ]);
 
  84   [ PRICE_NEW,                 t8('Price')               ],
 
  85   [ PRICE_REDUCED_MASTER_DATA, t8('Reduced Master Data') ],
 
  86   [ PRICE_DISCOUNT,            t8('Discount')            ],
 
  90   return ( columns => { SIMPLE => 'ALL', },
 
  91            default => [ 'name', 1 ],
 
  92            nulls   => { price => 'LAST', discount => 'LAST'  }