kivitendo/SL/DB/PriceRule.pm @ 4249ea44
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::PriceRule;
|
||||
use strict;
|
||||
use SL::DB::MetaSetup::PriceRule;
|
||||
use SL::DB::Manager::PriceRule;
|
||||
use Rose::DB::Object::Helpers qw(clone_and_reset);
|
||||
use SL::Locale::String qw(t8);
|
||||
__PACKAGE__->meta->add_relationship(
|
||||
items => {
|
||||
type => 'one to many',
|
||||
class => 'SL::DB::PriceRuleItem',
|
||||
column_map => { id => 'price_rules_id' },
|
||||
},
|
||||
);
|
||||
__PACKAGE__->meta->initialize;
|
||||
use Rose::Object::MakeMethods::Generic (
|
||||
'scalar --get_set_init' => [ qw(price_or_discount_state) ],
|
||||
);
|
||||
sub match {
|
||||
my ($self, %params) = @_;
|
||||
die 'need record' unless $params{record};
|
||||
die 'need record_item' unless $params{record_item};
|
||||
for ($self->items) {
|
||||
next if $_->match(%params);
|
||||
# TODO save for error
|
||||
return
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
sub is_sales {
|
||||
$_[0]->type eq 'customer' ? 1
|
||||
: $_[0]->type eq 'vendor' ? 0 : do { die 'wrong type' };
|
||||
}
|
||||
bc8c26f3 | Sven Schöling | sub price_type {
|
||
9589ecd7 | Sven Schöling | my ($self, $value) = @_;
|
||
if (@_ > 1) {
|
||||
my $number = $self->price || $self->discount;
|
||||
bc8c26f3 | Sven Schöling | if ($value == SL::DB::Manager::PriceRule::PRICE_NEW()) {
|
||
$self->price($number);
|
||||
} elsif ($value == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
|
||||
$self->reduction($number);
|
||||
} elsif ($value == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
|
||||
9589ecd7 | Sven Schöling | $self->discount($number);
|
||
} else {
|
||||
bc8c26f3 | Sven Schöling | die 'unknown price_or_discount value';
|
||
9589ecd7 | Sven Schöling | }
|
||
$self->price_or_discount_state($value);
|
||||
}
|
||||
$self->price_or_discount_state;
|
||||
}
|
||||
sub price_or_discount_as_number {
|
||||
my ($self, @slurp) = @_;
|
||||
bc8c26f3 | Sven Schöling | my $type = $self->price_type;
|
||
$self->price(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_NEW();
|
||||
$self->reduction(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA();
|
||||
$self->discount(undef) unless $type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT();
|
||||
9589ecd7 | Sven Schöling | |||
bc8c26f3 | Sven Schöling | |||
if ($type == SL::DB::Manager::PriceRule::PRICE_NEW()) {
|
||||
return $self->price_as_number(@slurp)
|
||||
} elsif ($type == SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()) {
|
||||
return $self->reduction_as_number(@slurp);
|
||||
} elsif ($type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
|
||||
return $self->discount_as_number(@slurp)
|
||||
} else {
|
||||
die 'unknown price_or_discount';
|
||||
}
|
||||
9589ecd7 | Sven Schöling | }
|
||
sub init_price_or_discount_state {
|
||||
bc8c26f3 | Sven Schöling | defined $_[0]->price ? SL::DB::Manager::PriceRule::PRICE_NEW()
|
||
: defined $_[0]->reduction ? SL::DB::Manager::PriceRule::PRICE_REDUCED_MASTER_DATA()
|
||||
: defined $_[0]->discount ? SL::DB::Manager::PriceRule::PRICE_DISCOUNT()
|
||||
: SL::DB::Manager::PriceRule::PRICE_NEW();
|
||||
9589ecd7 | Sven Schöling | }
|
||
sub validate {
|
||||
my ($self) = @_;
|
||||
my @errors;
|
||||
247ff327 | Sven Schöling | push @errors, $::locale->text('The name must not be empty.') if !$self->name;
|
||
bc8c26f3 | Sven Schöling | push @errors, $::locale->text('Price or discount must not be zero.') if !$self->price && !$self->discount && !$self->reduction;
|
||
247ff327 | Sven Schöling | push @errors, $::locale->text('Pirce rules must have at least one rule.') if !@{[ $self->items ]};
|
||
9589ecd7 | Sven Schöling | |||
return @errors;
|
||||
}
|
||||
sub clone_and_reset_deep {
|
||||
my ($self) = @_;
|
||||
my $clone = $self->clone_and_reset;
|
||||
$clone->items(map { $_->clone_and_reset } $self->items);
|
||||
$clone->name('');
|
||||
return $clone;
|
||||
}
|
||||
sub full_description {
|
||||
my ($self) = @_;
|
||||
5aa485a7 | Sven Schöling | my $items = $self->item_summary;
|
||
9589ecd7 | Sven Schöling | my $price = $self->price_or_discount
|
||
? t8('Discount #1%', $self->discount_as_number)
|
||||
: t8('Price #1', $self->price_as_number);
|
||||
sprintf "%s: %s (%s)", $self->name, $price, $items;
|
||||
}
|
||||
5aa485a7 | Sven Schöling | sub item_summary {
|
||
join ', ', map { $_->full_description } $_[0]->items;
|
||||
}
|
||||
9589ecd7 | Sven Schöling | sub in_use {
|
||
my ($self) = @_;
|
||||
# is use is in this case used by record_items for their current price source
|
||||
# so, get any of those that might have it
|
||||
require SL::DB::OrderItem;
|
||||
require SL::DB::DeliveryOrderItem;
|
||||
require SL::DB::InvoiceItem;
|
||||
my $price_source_spec = 'price_rules' . '/' . $self->id;
|
||||
SL::DB::Manager::OrderItem->get_all_count(query => [ active_price_source => $price_source_spec ])
|
||||
|| SL::DB::Manager::DeliveryOrderItem->get_all_count(query => [ active_price_source => $price_source_spec ])
|
||||
|| SL::DB::Manager::InvoiceItem->get_all_count(query => [ active_price_source => $price_source_spec ]);
|
||||
}
|
||||
72180323 | Sven Schöling | sub priority_as_text {
|
||
my ($self) = @_;
|
||||
return t8('Override') if $self->priority == 4;
|
||||
t8('Normal');
|
||||
}
|
||||
9589ecd7 | Sven Schöling | |||
1;
|