PriceRule: Fehler vermeiden wenn noch keine Preisregeln angelegt sind.
[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
21 sub get_matching_filter {
22   my ($class, %params) = @_;
23
24   die 'need record'      unless $params{record};
25   die 'need record_item' unless $params{record_item};
26
27   my $type = $params{record}->is_sales ? 'customer' : 'vendor';
28
29   # plan: 1. search all rule_items that do NOT match this record/record item combo
30   my ($sub_where, @value_subs) = SL::DB::Manager::PriceRuleItem->not_matching_sql_and_values(type => $type);
31   my @values = map { $_->($params{record}, $params{record_item}) } @value_subs;
32
33   # now union all NOT matching, invert ids, load these
34   my $matching_rule_ids = <<SQL;
35     SELECT id FROM price_rules
36     WHERE id NOT IN (
37       SELECT price_rules_id FROM price_rule_items WHERE $sub_where
38     )
39     AND type = ? AND NOT obsolete
40 SQL
41
42   push @values, $type;
43
44   return $matching_rule_ids, @values;
45 }
46
47 sub get_all_matching {
48   my ($self, %params) = @_;
49
50   my ($query, @values) = $self->get_matching_filter(%params);
51   my @ids = selectall_ids($::form, $::form->get_standard_dbh, $query, 0, @values);
52
53   return [] unless @ids;
54
55   $self->get_all(query => [ id => \@ids ]);
56 }
57
58 sub _sort_spec {
59   return ( columns => { SIMPLE => 'ALL', },
60            default => [ 'name', 1 ],
61            nulls   => { price => 'LAST', discount => 'LAST'  }
62          );
63 }
64
65 1;