-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnergexData.class.php
executable file
·148 lines (131 loc) · 5.23 KB
/
EnergexData.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
class EnergexData {
private $data;
private $usageCharge; //in cents
private $dailySupplyCharge; //in cents
private $discount; //in whole percentages (e.g. 11% would be 11)
private $solarFeedInTariff; //in cents
public function __construct($filename,$dailySupplyCharge,$usageCharge,$discount,$solarFeedInTariff) {
$this->data = $this->getCSV($filename);
//set fees
$this->discount = $discount / 100;
$this->dailySupplyCharge = ($dailySupplyCharge / 100) - (($dailySupplyCharge / 100) * $this->discount);
$this->usageCharge = ($usageCharge / 100) - (($usageCharge / 100) * $this->discount);
$this->solarFeedInTariff = $solarFeedInTariff / 100;
}
public function getProviderCharges() {
return array(
"daily_supply" => $this->dailySupplyCharge,
"usage" => $this->usageCharge,
"solar_feed_in_tariff" => $this->solarFeedInTariff,
"discount" => $this->discount,
);
}
public function getCSV($filename) {
$data = array_map('str_getcsv', file($filename));
return $data;
}
public function getData() {
return $this->data;
}
public function getDataForIndex($index,$type) {
if ($index==-1) {
return false;
}
$dataStr = "";
$labelStr = "";
$runningTotal = 0;
$counter = 1;
$loop = 0;
$total = 0;
foreach ($this->data[$index] as $currentData) {
if ($loop>=2) {
if (!is_float($currentData) && !is_numeric($currentData)) {
break;
}
$runningTotal += $currentData;
$total += $currentData;
if ($counter && $counter % 4 == 0) {
$labelStr .= (($counter/4)-1).",";
$dataStr .= $runningTotal.",";
$runningTotal = 0;
}
$counter++;
}
$loop++;
}
if ($type == "electricity") {
$charges = $this->dailySupplyCharge + ($this->usageCharge * $total);
} else if ($type == "solar") {
$charges = $this->solarFeedInTariff * $total;
}
$dataStr = rtrim($dataStr,",");
$labelStr = rtrim($labelStr,",");
return array(
"labels" => $labelStr,
"data" => $dataStr,
"total" => $total,
"charges" => $charges
);
}
public function lookupDate($date) {
$count = 0;
$solarIndex = -1;
$electricityIndex = -1;
foreach ($this->data as $i=>$data) {
if ($data[0] == "300" && $data[1] == $date) {
if (!$count) {
$solarIndex = $i;
$electricityIndex = $i;
} else {
$electricityIndex = $i;
}
$count++;
}
}
$returnData = array();
$returnData["electricity"] = $this->getDataForIndex($electricityIndex,"electricity");
if ($electricityIndex != $solarIndex) { //then no solar
$returnData["solar"] = $this->getDataForIndex($solarIndex,"solar");
}
return $returnData;
}
public function getPeriod($startDate, $endDate) {
$start = (int)strtotime($startDate);
$end = (int)strtotime($endDate);
$electricityCharges = 0;
$totalElectricityConsumption = 0;
$totalSolarFeedIn = 0;
$solarCharges = 0;
$days = 0;
while ($start <= $end) {
$currentDate = date("Ymd",$start);
$data = $this->lookupDate($currentDate);
$electricityCharges += $data["electricity"]["charges"];
$totalElectricityConsumption += $data["electricity"]["total"];
$totalSolarFeedIn += (isset($data["solar"])?$data["solar"]["total"]:0);
$solarCharges += (isset($data["solar"])?$data["solar"]["charges"]:0);
$start += 86400;
$days++;
}
$averageUsage = $totalElectricityConsumption / $days;
$averageFeedIn = $totalSolarFeedIn / $days;
$payment = ($electricityCharges - $solarCharges);
$quarterly = ($payment / $days) * 90;
return array(
"start_date" => $startDate,
"end_date" => $endDate,
"total_days" => $days,
"electricity_charges" => $electricityCharges,
"electricity_kw_total" => $totalElectricityConsumption,
"solar_feed_in_tariff" => $solarCharges,
"solar_feed_in_kw_total" => $totalSolarFeedIn,
"average_daily_kw_usage" => $averageUsage,
"average_daily_solar_feed_in_kw" => $averageFeedIn,
"payment" => $payment,
"payment_type" => (($payment > 0)?"payment":"credit"),
"estimated_quarterly_bill" => $quarterly
);
}
}
?>