974011a55d4fd023415e58fdde92ab4b260d70a1
[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 constant PRICE_NEW                 => 0;
11 use constant PRICE_REDUCED_MASTER_DATA => 1;
12 use constant PRICE_DISCOUNT            => 2;
13
14 use SL::DB::Helper::Filtered;
15 use SL::DB::Helper::Paginated;
16 use SL::DB::Helper::Sorted;
17 use SL::DBUtils;
18
19 use SL::Locale::String qw(t8);
20
21 sub object_class { 'SL::DB::PriceRule' }
22
23 __PACKAGE__->make_manager_methods;
24 __PACKAGE__->add_filter_specs(
25   has_item_type => sub {
26     my ($key, $values, $prefix) = @_;
27     return unless @$values;
28
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 ] ];
32   },
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] };
38
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 ] ];
44   },
45 );
46
47 sub get_matching_filter {
48   my ($class, %params) = @_;
49
50   die 'need record'      unless $params{record};
51   die 'need record_item' unless $params{record_item};
52
53   my $type = $params{record}->is_sales ? 'customer' : 'vendor';
54
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);
57
58   # now union all NOT matching, invert ids, load these
59   my $matching_rule_ids = <<SQL;
60     SELECT id FROM price_rules
61     WHERE id NOT IN (
62       SELECT price_rules_id FROM price_rule_items WHERE $sub_where
63     )
64     AND type = ? AND NOT obsolete
65 SQL
66
67   push @values, $type;
68
69   return $matching_rule_ids, @values;
70 }
71
72 sub get_all_matching {
73   my ($self, %params) = @_;
74
75   my ($query, @values) = $self->get_matching_filter(%params);
76   my @ids = selectcol_array_query($::form, SL::DB->client->dbh, $query, @values);
77   return [] unless @ids;
78
79   $self->get_all(query => [ id => \@ids ]);
80 }
81
82 sub all_price_types {
83   [ PRICE_NEW,                 t8('Price')               ],
84   [ PRICE_REDUCED_MASTER_DATA, t8('Reduced Master Data') ],
85   [ PRICE_DISCOUNT,            t8('Discount')            ],
86 }
87
88 sub _sort_spec {
89   return ( columns => { SIMPLE => 'ALL', },
90            default => [ 'name', 1 ],
91            nulls   => { price => 'LAST', discount => 'LAST'  }
92          );
93 }
94
95 1;