kivitendo/SL/DB/Manager/PriceRuleItem.pm @ c364e43e
9589ecd7 | Sven Schöling | # This file has been auto-generated only because it didn't exist.
|
||
# Feel free to modify it at will; it will not be overwritten automatically.
|
||||
package SL::DB::Manager::PriceRuleItem;
|
||||
use strict;
|
||||
use SL::DB::Helper::Manager;
|
||||
use base qw(SL::DB::Helper::Manager);
|
||||
sub object_class { 'SL::DB::PriceRuleItem' }
|
||||
__PACKAGE__->make_manager_methods;
|
||||
use SL::Locale::String qw(t8);
|
||||
my @types = qw(
|
||||
3f5c7866 | Sven Schöling | part customer vendor business partsgroup qty reqdate transdate pricegroup
|
||
9589ecd7 | Sven Schöling | );
|
||
my %ops = (
|
||||
49eebab7 | Sven Schöling | 'num' => { eq => '=', le => '<=', ge => '>=' },
|
||
9589ecd7 | Sven Schöling | 'date' => { eq => '=', lt => '<', gt => '>' },
|
||
);
|
||||
my %types = (
|
||||
'customer' => { description => t8('Customer'), customer => 1, vendor => 0, data_type => 'int', data => sub { $_[0]->customer->id }, },
|
||||
'vendor' => { description => t8('Vendor'), customer => 0, vendor => 1, data_type => 'int', data => sub { $_[0]->vendor->id }, },
|
||||
0fe023a4 | Sven Schöling | 'business' => { description => t8('Type of Business'), customer => 1, vendor => 1, data_type => 'int', data => sub { $_[0]->customervendor->business_id }, exclude_nulls => 1 },
|
||
9589ecd7 | Sven Schöling | 'reqdate' => { description => t8('Reqdate'), customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->reqdate }, ops => 'date' },
|
||
3f5c7866 | Sven Schöling | 'transdate' => { description => t8('Transdate'), customer => 1, vendor => 1, data_type => 'date', data => sub { $_[0]->transdate }, ops => 'date' },
|
||
490b0f01 | Sven Schöling | 'part' => { description => t8('Part'), customer => 1, vendor => 1, data_type => 'int', data => sub { $_[1]->part->id }, },
|
||
0fe023a4 | Sven Schöling | 'pricegroup' => { description => t8('Pricegroup'), customer => 1, vendor => 1, data_type => 'int', data => sub { $_[1]->pricegroup_id }, exclude_nulls => 1 },
|
||
'partsgroup' => { description => t8('Group'), customer => 1, vendor => 1, data_type => 'int', data => sub { $_[1]->part->partsgroup_id }, exclude_nulls => 1 },
|
||||
9589ecd7 | Sven Schöling | 'qty' => { description => t8('Qty'), customer => 1, vendor => 1, data_type => 'num', data => sub { $_[1]->qty }, ops => 'num' },
|
||
);
|
||||
sub not_matching_sql_and_values {
|
||||
my ($class, %params) = @_;
|
||||
die 'must be called with a customer/vendor type' unless $params{type};
|
||||
0fe023a4 | Sven Schöling | my @args = @params{'record', 'record_item'};
|
||
9589ecd7 | Sven Schöling | |||
my (@tokens, @values);
|
||||
for my $type (@types) {
|
||||
my $def = $types{$type};
|
||||
next unless $def->{$params{type}};
|
||||
0fe023a4 | Sven Schöling | my $value = $def->{data}->(@args);
|
||
9589ecd7 | Sven Schöling | |||
0fe023a4 | Sven Schöling | if ($def->{exclude_nulls} && !defined $value) {
|
||
push @tokens, "type = '$type'";
|
||||
} else {
|
||||
9589ecd7 | Sven Schöling | my @sub_tokens;
|
||
0fe023a4 | Sven Schöling | if ($def->{ops}) {
|
||
my $ops = $ops{$def->{ops}};
|
||||
for (keys %$ops) {
|
||||
19586862 | Sven Schöling | push @sub_tokens, "op = '$_' AND NOT ? $ops->{$_} value_$def->{data_type}";
|
||
0fe023a4 | Sven Schöling | push @values, $value;
|
||
}
|
||||
} else {
|
||||
push @sub_tokens, "NOT value_$def->{data_type} = ?";
|
||||
push @values, $value;
|
||||
9589ecd7 | Sven Schöling | }
|
||
push @tokens, "type = '$type' AND " . join ' OR ', map "($_)", @sub_tokens;
|
||||
}
|
||||
}
|
||||
return join(' OR ', map "($_)", @tokens), @values;
|
||||
}
|
||||
sub get_all_types {
|
||||
my ($class, $vc) = @_;
|
||||
c383fc0b | Sven Schöling | $vc
|
||
? [ map { [ $_, $types{$_}{description} ] } grep { $types{$_}{$vc} } map { $_ } @types ]
|
||||
: [ map { [ $_, $types{$_}{description} ] } map { $_ } @types ]
|
||||
9589ecd7 | Sven Schöling | }
|
||
986282c1 | Sven Schöling | sub get_type {
|
||
$types{$_[1]}
|
||||
}
|
||||
sub filter_match {
|
||||
my ($self, $type, $value) = @_;
|
||||
my $type_def = $types{$type};
|
||||
if (!$type_def->{ops}) {
|
||||
my $evalue = $::form->get_standard_dbh->quote($value);
|
||||
return "value_$type_def->{data_type} = $evalue";
|
||||
} elsif ($type_def->{ops} eq 'date') {
|
||||
my $date_value = $::form->get_standard_dbh->quote(DateTime->from_kivitendo($value));
|
||||
return "
|
||||
(value_$type_def->{data_type} > $date_value AND op = 'lt') OR
|
||||
(value_$type_def->{data_type} < $date_value AND op = 'gt') OR
|
||||
(value_$type_def->{data_type} = $date_value AND op = 'eq')
|
||||
";
|
||||
} elsif ($type_def->{ops} eq 'num') {
|
||||
my $num_value = $::form->get_standard_dbh->quote($::form->parse_amount(\%::myconfig, $value));
|
||||
return "
|
||||
(value_$type_def->{data_type} >= $num_value AND op = 'le') OR
|
||||
(value_$type_def->{data_type} <= $num_value AND op = 'ge') OR
|
||||
(value_$type_def->{data_type} = $num_value AND op = 'eq')
|
||||
";
|
||||
}
|
||||
}
|
||||
9589ecd7 | Sven Schöling | 1;
|