epic-s6ts
[kivitendo-erp.git] / SL / PriceSource / PriceRules.pm
index 140706d..48ab563 100644 (file)
@@ -45,10 +45,39 @@ sub price_from_source {
   my ($self, $source, $spec) = @_;
 
   my $rule = SL::DB::Manager::PriceRule->find_by(id => $spec);
+
+  return SL::PriceSource::Discount->new(
+    price_source => $self,
+    missing      => t8('The price rule for this price does not exist anymore'),
+  ) if !$rule;
+
+  if ($rule->price_type != SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
+    return $self->make_price_from_rule($rule);
+  } else {
+    return SL::PriceSource::Price->new(
+      price_source => $self,
+      invalid      => t8('The price rule is not a rule for prices'),
+    );
+  }
+}
+
+sub discount_from_source {
+  my ($self, $source, $spec) = @_;
+
+  my $rule = SL::DB::Manager::PriceRule->find_by(id => $spec);
+
+  return SL::PriceSource::Discount->new(
+    price_source => $self,
+    missing      => t8('The price rule for this discount does not exist anymore'),
+  ) if !$rule;
+
   if ($rule->price_type == SL::DB::Manager::PriceRule::PRICE_DISCOUNT()) {
     return $self->make_discount_from_rule($rule);
   } else {
-    return $self->make_price_from_rule($rule);
+    return SL::PriceSource::Discount->new(
+      price_source => $self,
+      invalid      => t8('The price rule is not a rule for discounts'),
+    );
   }
 }
 
@@ -92,6 +121,7 @@ sub make_price_from_rule {
     price        => $self->price_for_rule($rule),
     spec         => $rule->id,
     description  => $rule->name,
+    priority     => $rule->priority,
     price_source => $self,
     (invalid      => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,
   )
@@ -104,6 +134,7 @@ sub make_discount_from_rule {
     discount     => $rule->discount / 100,
     spec         => $rule->id,
     description  => $rule->name,
+    priority     => $rule->priority,
     price_source => $self,
     (invalid      => t8('This Price Rule is no longer valid'))x!!$rule->obsolete,
   )