Time-Dependent Prices and Discounts

The types that define prices, taxes, and discounts have a common supertype, the abstract PriceDiscountTaxRow, or PDTRow type.

The PDTRow type optionally accepts an attribute named daterange of type de.hybris.platform.util.DateRange (time span), whose range defines when the respective PriceRow, TaxRow, or DiscountRow instance applies. If the current date is earlier than the start date or later than the end date, respectively, the price, tax, or discount isn’t applied. A given date starts by 00:00:00 that day and ends by 23:59:59 that same day. You can’t define a DateRange to end by 15:23:34, for example.

Europe1 doesn’t allow one of the fields of the date range to be empty. You need to specify a value for both the start and end date, or leave both fields blank.

The following table provides an example.

Start Date

End Date

Daterange Valid

2005-03-16

2005-04-19

March 16, 2005, 00:00:00 through April 19, 2005, 23:59:59

(blank)

2005-04-19

(not possible)

2005-03-16

(blank)

(not possible)

(blank)

(blank)

Always valid

Defining Date Ranges

To assign a DateRange to a price or tax definition, create an instance of a DateRange, get the instance of the current PriceFactory, and refer the instantiated DateRange as a parameter in the call for the createPriceRow( ...) or createDiscountRow( ...) method, respectively, as in the following code snippet:

Date startDate = new Date( 2005, 06, 12 );
					Date endDate = new Date( 2006, 02, 01 );
					DateRange dateRange = new StandardDateRange( startDate, endDate );
					Europe1PriceFactory pf = (Europe1PriceFactory) OrderManager.getInstance().getPriceFactory();
					pf.createPriceRow( product, productPriceGroup, user, userPriceGroup, minQuantity,
					currency, unit, unitFactor, net, dateRange, price );
					pf.createDiscountRow( product, productPriceGroup, user, userPriceGroup, currency, value, dateRange, discount );

When you create price rows with start and end dates, make sure that they don't overlap. Otherwise, it's not guaranteed which price is chosen when Europe1 is asked for prices within the overlapping period.

As Dates in Java are precise to the millisecond, you should declare the end time and the start time of a successive price row to be at least one millisecond apart, as shown in this example:
PriceRowModel beforePrice = modelService.create(PriceRowModel.class);
							beforePrice.setStartTime( new Date(1L) );
							beforePrice.setEndTime( new Date(9999L) );
							...
							PriceRowModel afterPrice = modelService.create(PriceRowModel.class);
							afterPrice.setStartTime( new Date(10000L) );
							afterPrice.setEndTime( new Date(20000L) );
							...
							modelService.saveAll(beforePrice, afterPrice);
							
							// or using DateRange
							
							
							PriceRowModel beforePrice = modelService.create(PriceRowModel.class);
							beforePrice.setDateRange( new StandardDateRange( new Date(1L) , new Date(9999L) ) );
							...
							PriceRowModel afterPrice = modelService.create(PriceRowModel.class);
							afterPrice..setDateRange( new StandardDateRange( new Date(10000L), new Date(20000L) ) );
							...
							modelService.saveAll(beforePrice, afterPrice);

Defining Open-Ended Date Ranges

To allow for a daterange to be open-ended, for example "since February 1, 2016 until forever", or have no defined start date, for example "since forever in the past until February 1, 2017 ", set a fixed date and then set the other date to a point in time far in the future or far in the past. As in the following example, you could set it ten years or more from now.

						// setting a fixed date as a start date for an "open end" daterange
						Date startDate = new Date( 2016, 02, 01 );
						// setting an "open end" date as a time point 10 years in the future from the current time
						Date endDate = new Date( System.currentTimeMillis() + 10 * 365 * 24 * 60 * 60 * 1000 ) );
						DateRange dateRange = new StandardDateRange( startDate, endDate );

In this case, you need to make sure that there are no two dateranges of this kind for a given product as the Europe1 PriceFactory wouldn’t be able to determine which is the better match.