I need to create a database solution to provide product discounting.
Current tables:
Products
Columns: ProductId, ProductTypeId, ReleaseDate
ProductPrices
Columns: ProductPriceId, ProductPriceTypeId (one product may have n prices), ProductId, Price
We want to be able to discount by ProductId and/or ProductTypeId and/or ProductPriceTypeId and/or ReleaseDate.
Example sales:
- Discount a single ProductId.
- Discount all products of a specified ProductTypeId and ProductPriceTypeId.
- Discount all products of a specified ProductTypeId with a ReleaseDate within the last month.
The challenging aspect of #2 is not the literal example, but considering long term scalability in the event new fields are added in the future.
I am stumped how to handle #3 because of the ReleaseDate.
Below, is what I mentally thought out before I realized I needed to come to Stackoverflow. You can see that the rigid structure will not allow for good scalability because of the explicitly included columns - if we added new criteria in the future then those columns would need to be added to the table - and not to mention it does not even handle the ReleaseDate requirement.
New table:
ProductPriceDiscounts
Columns: ProductPriceDiscountId, ProductPriceId, ProductTypeId, ProductPriceTypeId, Discount, DiscountTypeId (1 for percentage, 2 for fixed)
Then I could use something like this to get the pricing:
from p in Products
join pp in ProductPrices on p.ProductId equals pp.ProductId
let ppd = (
from ppd in ProductPriceDiscounts
.WhereIf(ppd.ProductPriceId != null, ppd.ProductPriceId == pp.ProductPriceId)
.WhereIf(ppd.ProductTypeId != null, ppd.ProductTypeId == pp.ProductTypeId )
.WhereIf(ppd.ProductPriceTypeId != null, ppd.ProductPriceTypeId == pp.ProductPriceId)
select ppd).FirstOrDefault()
where p.ProductId = productId
select new
{
...,
Price = pp.Price,
Discount = pp.Discount,
DiscountedPrice =
(ppd.DiscountTypeId == 1) ?
(pp.Price - (pp.Price * pp.Discount)) :
(pp.Price - pp.Discount) :
}
I only included this bad example I came up with to show the end result of how I need to be able to use this new product discounting functionality. Can anyone offer advice for a good way to handle this situation? Thanks.