1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Class IntervalValuator |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Roomify\Bat\Valuator; |
9
|
|
|
|
10
|
|
|
use Roomify\Bat\Calendar\Calendar; |
11
|
|
|
use Roomify\Bat\Store\Store; |
12
|
|
|
use Roomify\Bat\Valuator\AbstractValuator; |
13
|
|
|
use Roomify\Bat\Unit\Unit; |
14
|
|
|
use Roomify\Bat\Event\EventInterval; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The IntervalValuator sums the aggregate value of an event by dividing time |
18
|
|
|
* in discreet intervals and then assigning value to those |
19
|
|
|
* intervals based on the value the unit has during that interval. |
20
|
|
|
* |
21
|
|
|
* For example, if we are dealing with a hotel room and want to calculate nightly rates |
22
|
|
|
* we can set the date interval to P1D. We will query the calendar for events whose value |
23
|
|
|
* represents prices over that period and the split the time in 1-day intervals and sum up |
24
|
|
|
* accordingly. |
25
|
|
|
* |
26
|
|
|
* If we are selling activities at 15m intervals we can set the interval at PT15M and we would |
27
|
|
|
* be splitting events on 15m intervals and would assign the value of each interval to the value |
28
|
|
|
* of the event during that interval. |
29
|
|
|
*/ |
30
|
|
|
class IntervalValuator extends AbstractValuator { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \DateInterval |
34
|
|
|
*/ |
35
|
|
|
protected $duration_unit; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \DateTime $start_date |
39
|
|
|
* @param \DateTime $end_date |
40
|
|
|
* @param Unit $unit |
41
|
|
|
* @param Store $store |
42
|
|
|
* @param \DateInterval $duration_unit |
43
|
|
|
*/ |
44
|
|
|
public function __construct(\DateTime $start_date, \DateTime $end_date, Unit $unit, Store $store, \DateInterval $duration_unit) { |
45
|
|
|
parent::__construct($start_date, $end_date, $unit, $store); |
46
|
|
|
$this->duration_unit = $duration_unit; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function determineValue() { |
53
|
|
|
$value = 0; |
54
|
|
|
|
55
|
|
|
// Instantiate a calendar |
56
|
|
|
$calendar = new Calendar(array($this->unit), $this->store); |
57
|
|
|
|
58
|
|
|
$events = $calendar->getEvents($this->start_date, $this->end_date); |
59
|
|
|
|
60
|
|
|
foreach ($events as $unit => $unit_events) { |
61
|
|
|
if ($unit == $this->unit->getUnitId()) { |
62
|
|
|
foreach ($unit_events as $event) { |
63
|
|
|
$percentage = EventInterval::divide($event->getStartDate(), $event->getEndDate(), $this->duration_unit); |
64
|
|
|
$value = $value + $event->getValue() * $percentage; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return round($value, 2); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|