ba3694c4756d71a633ef44a7ba962a4c78ca9910
[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   part customer vendor business partsgroup qty reqdate transdate pricegroup
19 );
20
21 my %ops = (
22   'num'  => { eq => '=', le => '<=', ge => '>=' },
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 }, exclude_nulls => 1 },
30   'reqdate'             => { description => t8('Reqdate'),            customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->reqdate }, ops => 'date' },
31   'transdate'           => { description => t8('Transdate'),          customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->transdate }, ops => 'date' },
32   'part'                => { description => t8('Part'),               customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->id }, },
33   'pricegroup'          => { description => t8('Pricegroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->pricegroup_id }, exclude_nulls => 1 },
34   'partsgroup'          => { description => t8('Partsgroup'),         customer => 1, vendor => 1, data_type => 'int',  data => sub { $_[1]->part->partsgroup_id }, exclude_nulls => 1 },
35   'qty'                 => { description => t8('Qty'),                customer => 1, vendor => 1, data_type => 'num',  data => sub { $_[1]->qty }, ops => 'num' },
36 );
37
38 sub not_matching_sql_and_values {
39   my ($class, %params) = @_;
40
41   die 'must be called with a customer/vendor type' unless $params{type};
42   my @args = @params{'record', 'record_item'};
43
44   my (@tokens, @values);
45
46   for my $type (@types) {
47     my $def = $types{$type};
48     next unless $def->{$params{type}};
49
50     my $value = $def->{data}->(@args);
51
52     if ($def->{exclude_nulls} && !defined $value) {
53       push @tokens, "type = '$type'";
54     } else {
55       my @sub_tokens;
56       if ($def->{ops}) {
57         my $ops = $ops{$def->{ops}};
58
59         for (keys %$ops) {
60           push @sub_tokens, "op = '$_' AND NOT ? $ops->{$_} value_$def->{data_type}";
61           push @values, $value;
62         }
63       } else {
64         push @sub_tokens, "NOT value_$def->{data_type} = ?";
65         push @values, $value;
66       }
67
68       push @tokens, "type = '$type' AND (@{[ join(' OR ', map qq|($_)|, @sub_tokens) ]})";
69     }
70   }
71
72   return join(' OR ', map "($_)", @tokens), @values;
73 }
74
75 sub get_all_types {
76   my ($class, $vc) = @_;
77
78   $vc
79   ? [ map { [ $_, $types{$_}{description} ] } grep { $types{$_}{$vc} } map { $_ } @types ]
80   : [ map { [ $_, $types{$_}{description} ] } map { $_ } @types ]
81 }
82
83 sub get_type {
84   $types{$_[1]}
85 }
86
87 sub filter_match {
88   my ($self, $type, $value) = @_;
89
90   my $type_def = $types{$type};
91
92   if (!$type_def->{ops}) {
93     my $evalue   = $::form->get_standard_dbh->quote($value);
94     return "value_$type_def->{data_type} = $evalue";
95   } elsif ($type_def->{ops} eq 'date') {
96     my $date_value   = $::form->get_standard_dbh->quote(DateTime->from_kivitendo($value));
97     return "
98       (value_$type_def->{data_type} > $date_value AND op = 'lt') OR
99       (value_$type_def->{data_type} < $date_value AND op = 'gt') OR
100       (value_$type_def->{data_type} = $date_value AND op = 'eq')
101     ";
102   } elsif ($type_def->{ops} eq 'num') {
103     my $num_value   = $::form->get_standard_dbh->quote($::form->parse_amount(\%::myconfig, $value));
104     return "
105       (value_$type_def->{data_type} >= $num_value AND op = 'le') OR
106       (value_$type_def->{data_type} <= $num_value AND op = 'ge') OR
107       (value_$type_def->{data_type} =  $num_value AND op = 'eq')
108     ";
109   }
110 }
111
112
113 1;