1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Roomify\Bat\Valuator; |
8
|
|
|
|
9
|
|
|
use Roomify\Bat\Calendar\Calendar; |
10
|
|
|
use Roomify\Bat\Store\Store; |
11
|
|
|
use Roomify\Bat\Valuator\AbstractValuator; |
12
|
|
|
use Roomify\Bat\Unit\Unit; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The IntervalValuator sums the aggregate value of an event by dividing time |
16
|
|
|
* in discreet intervals (using \DateInterval) and then assigning value to those |
17
|
|
|
* intervals based on the value the unit has during that interval. |
18
|
|
|
* |
19
|
|
|
* For example, if we are dealing with a hotel room and want to calculate nightly rates |
20
|
|
|
* we can set the date interval to P1D. We will query the calendar for events whose value |
21
|
|
|
* represents prices over that period and the split the time in 1-day intervals and sum up |
22
|
|
|
* accordingly. |
23
|
|
|
* |
24
|
|
|
* If we are selling activities at 15m intervals we can set the interval at PT15M and we would |
25
|
|
|
* be splitting events on 15m intervals and would assign the value of each interval to the value |
26
|
|
|
* of the event during that interval. |
27
|
|
|
* |
28
|
|
|
* Class IntervalValuator |
29
|
|
|
* @package Roomify\Bat\Valuator |
30
|
|
|
*/ |
31
|
|
|
class IntervalValuator extends AbstractValuator { |
32
|
|
|
|
33
|
|
|
protected $duration_unit; |
34
|
|
|
|
35
|
|
|
public function __construct(\DateTime $start_date, \DateTime $end_date, Unit $unit, Store $store, \DateInterval $duration_unit){ |
36
|
|
|
parent::__construct($start_date, $end_date, $unit, $store); |
37
|
|
|
$this->duration_unit = $duration_unit; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function determineValue() { |
41
|
|
|
|
42
|
|
|
$value = 0; |
43
|
|
|
|
44
|
|
|
// Instantiate a calendar |
45
|
|
|
$calendar = new Calendar(array($this->unit), $this->store); |
46
|
|
|
|
47
|
|
|
$events = $calendar->getEvents($this->start_date, $this->end_date); |
48
|
|
|
|
49
|
|
|
foreach ($events as $unit => $unit_events) { |
50
|
|
|
// Create a period with the duration and dates supplied |
51
|
|
|
$period = new \DatePeriod($this->start_date, $this->duration_unit, $this->end_date); |
52
|
|
|
print_r("NEW CYCLE\n"); |
53
|
|
|
//var_dump($period); |
54
|
|
|
//var_dump($unit); |
55
|
|
|
//var_dump($unit_events); |
56
|
|
|
foreach ($unit_events as $event){ |
57
|
|
|
if ($unit = $this->unit->getUnitId()){ |
|
|
|
|
58
|
|
|
foreach ($period as $dt) { |
59
|
|
|
// If event in period involved add value of event |
60
|
|
|
// If event is not completely in period involved then add just the percentage that is |
61
|
|
|
$last_period = $dt; |
|
|
|
|
62
|
|
|
print_r("Event is: "); |
63
|
|
|
print_r($event->getStartDate()->format('Y-m-d H:i:s') ." to "); |
64
|
|
|
print_r($event->getEndDate()->format('Y-m-d H:i:s') ."\n"); |
65
|
|
|
print_r("Period is: "); |
66
|
|
|
print_r($dt->format('Y-m-d H:i:s') ."\n"); |
67
|
|
|
if ($event->dateIsInRange($dt)){ |
68
|
|
|
print_r("In range adding: " . $event->getValue() . " to event.\n"); |
69
|
|
|
$value = $value + $event->getValue(); |
70
|
|
|
print_r("Current Value " .$value . "\n"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
var_dump($value); |
|
|
|
|
77
|
|
|
return $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.