views:

61

answers:

3

Classic scenario, where an order has order lines.

The client wants to be able to apply a rebate to the entire order, that is either a fixed amount or a percentage.

What would be the best way to implement that?

I am thinking storing two fields on the order-object:

  • Rebate fixed amount
  • Rebate percentage

And then I can calculate the total rebate - and make the rebate an order line, that I re-calc every time the rebate-fields or order lines are changed.

Caveats, hints, best practices?

+2  A: 

Take a look into using something like the composite pattern.

The rebates should not be stored within the objects themselves but should be applied as an external entity. Think about providing enough flexibility for realistic changes.

Bob Breznak
+2  A: 

There's more flexibility in keeping the rebate logic separate from the order. For example, business rules might change to have a more flexible rebate strategy, e.g. rebates only apply to some order lines based on whether there is a promotion for those items. That's more akin to a strategy pattern (which might or might not be overkill for your scenario and business environment).

In that scenario, the order would have a foreign key into a rebate table which identified the rebate strategy and any associated data such as percentage or fixed amount.

Vinay Sajip
+1  A: 

Did you think of adding rebates to all order lines. If no rebate - it`s a zero rebate. Any rebate can be added.

Pattern you should use in this case depends on how are you going to use rebates. If single rebate on the order - strategy is very well.

If rebates can work with each other (rebate for first buy, 10000 client and discount card) you should look at decorator as other way you`ll be have to implement as much strategies as rebates combination.

Yaroslav Yakovlev