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 = selectcol_array_query($::form, SL::DB->client->dbh, $query, @values);
77 return [] unless @ids;
79 $self->get_all(query => [ id => \@ids ]);
83 [ PRICE_NEW, t8('Price') ],
84 [ PRICE_REDUCED_MASTER_DATA, t8('Reduced Master Data') ],
85 [ PRICE_DISCOUNT, t8('Discount') ],
89 return ( columns => { SIMPLE => 'ALL', },
90 default => [ 'name', 1 ],
91 nulls => { price => 'LAST', discount => 'LAST' }