PriceRule: Mehr Filteroptionen
[kivitendo-erp.git] / SL / DB / Manager / PriceRule.pm
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.
3
4 package SL::DB::Manager::PriceRule;
5
6 use strict;
7
8 use parent qw(SL::DB::Helper::Manager);
9
10 use SL::DB::Helper::Filtered;
11 use SL::DB::Helper::Paginated;
12 use SL::DB::Helper::Sorted;
13 use SL::DBUtils;
14
15 use SL::Locale::String qw(t8);
16
17 sub object_class { 'SL::DB::PriceRule' }
18
19 __PACKAGE__->make_manager_methods;
20 __PACKAGE__->add_filter_specs(
21   has_item_type => sub {
22     my ($key, $values, $prefix) = @_;
23     return unless @$values;
24
25     my $each_type = "SELECT DISTINCT price_rules_id FROM price_rule_items WHERE type = %s";
26     my $sub_query = join ' INTERSECT ', map { sprintf $each_type, $::form->get_standard_dbh->quote($_) } @$values;
27     return or => [ ${prefix} . 'id' => [ \$sub_query ] ];
28   },
29   item_type_matches => sub {
30     my ($key, $values, $prefix) = @_;
31     return unless @$values;
32     return unless 'HASH' eq ref $values->[0];
33     return unless grep $_, values %{ $values->[0] };
34
35     my $each_type = "SELECT DISTINCT price_rules_id FROM price_rule_items WHERE type = %s AND (%s)";
36     my $sub_query = join ' INTERSECT ', map {
37       sprintf $each_type, $::form->get_standard_dbh->quote($_), SL::DB::Manager::PriceRuleItem->filter_match($_, $values->[0]{$_})
38     } grep { $values->[0]{$_} } keys %{ $values->[0] };
39     return or => [ ${prefix} . 'id' => [ \$sub_query ] ];
40   },
41 );
42
43 sub get_matching_filter {
44   my ($class, %params) = @_;
45
46   die 'need record'      unless $params{record};
47   die 'need record_item' unless $params{record_item};
48
49   my $type = $params{record}->is_sales ? 'customer' : 'vendor';
50
51   # plan: 1. search all rule_items that do NOT match this record/record item combo
52   my ($sub_where, @values) = SL::DB::Manager::PriceRuleItem->not_matching_sql_and_values(type => $type, %params);
53
54   # now union all NOT matching, invert ids, load these
55   my $matching_rule_ids = <<SQL;
56     SELECT id FROM price_rules
57     WHERE id NOT IN (
58       SELECT price_rules_id FROM price_rule_items WHERE $sub_where
59     )
60     AND type = ? AND NOT obsolete
61 SQL
62
63   push @values, $type;
64
65   return $matching_rule_ids, @values;
66 }
67
68 sub get_all_matching {
69   my ($self, %params) = @_;
70
71   my ($query, @values) = $self->get_matching_filter(%params);
72   my @ids = selectall_ids($::form, $::form->get_standard_dbh, $query, 0, @values);
73
74   return [] unless @ids;
75
76   $self->get_all(query => [ id => \@ids ]);
77 }
78
79 sub _sort_spec {
80   return ( columns => { SIMPLE => 'ALL', },
81            default => [ 'name', 1 ],
82            nulls   => { price => 'LAST', discount => 'LAST'  }
83          );
84 }
85
86 1;