ef6cc9cfdde7e775ef396d28619b3aea4a761bfe
[kivitendo-erp.git] / SL / DB / Manager / PriceRuleItem.pm
1 # This file has 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::PriceRuleItem;
5
6 use strict;
7
8 use SL::DB::Helper::Manager;
9 use base qw(SL::DB::Helper::Manager);
10
11 sub object_class { 'SL::DB::PriceRuleItem' }
12
13 __PACKAGE__->make_manager_methods;
14
15 use SL::Locale::String qw(t8);
16
17 my @types = qw(
18   customer vendor business partsgroup qty reqdate pricegroup
19 );
20
21 my %ops = (
22   'num'  => { eq => '=', lt => '<', gt => '>' },
23   'date' => { eq => '=', lt => '<', gt => '>' },
24 );
25
26 my %types = (
27   'customer'            => { description => t8('Customer'),           customer => 1, vendor => 0, data_type => 'int',  data => sub { $_[0]->customer->id }, },
28   'vendor'              => { description => t8('Vendor'),             customer => 0, vendor => 1, data_type => 'int',  data => sub { $_[0]->vendor->id }, },
29   'business'            => { description => t8('Type of Business'),   customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[0]->customervendor->business_id }, },
30   'reqdate'             => { description => t8('Reqdate'),            customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->reqdate }, ops => 'date' },
31   'pricegroup'          => { description => t8('Pricegroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->pricegroup_id }, },
32   'partsgroup'          => { description => t8('Group'),              customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->partsgroup_id }, },
33   'qty'                 => { description => t8('Qty'),                customer => 1, vendor => 1, data_type => 'num',  data => sub { $_[1]->qty }, ops => 'num' },
34 );
35
36 sub not_matching_sql_and_values {
37   my ($class, %params) = @_;
38
39   die 'must be called with a customer/vendor type' unless $params{type};
40
41   my (@tokens, @values);
42
43   for my $type (@types) {
44     my $def = $types{$type};
45     next unless $def->{$params{type}};
46
47     if ($def->{ops}) {
48       my $ops = $ops{$def->{ops}};
49
50       my @sub_tokens;
51       for (keys %$ops) {
52         push @sub_tokens, "op = '$_' AND NOT value_$def->{data_type} $ops->{$_} ?";
53         push @values, $def->{data};
54       }
55
56       push @tokens, "type = '$type' AND " . join ' OR ', map "($_)", @sub_tokens;
57     } else {
58       push @tokens, "type = '$type' AND NOT value_$def->{data_type} = ?";
59       push @values, $def->{data};
60     }
61   }
62
63   return join(' OR ', map "($_)", @tokens), @values;
64 }
65
66 sub get_all_types {
67   my ($class, $vc) = @_;
68
69   [ map { [ $_, $types{$_}{description} ] } grep { $types{$_}{$vc} } map { $_ } @types ];
70 }
71
72 1;