-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekly_revenue.php
75 lines (58 loc) · 1.69 KB
/
weekly_revenue.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
<?php
error_reporting(E_ALL);
if (file_exists("config.php")) {
include "config.php";
} else {
die("Please rename config.example.php to config.php and enter your details to continue.\n");
}
include "functions.php";
check_api($api_key);
$url = "https://{$company}.freeagentcentral.com/invoices.xml?view=last_12_months";
$xml = perform_xml_request($url, $username, $password);
$results = array();
$return = array(
"item" => array(),
"settings" => array(
"axisx" => array(),
"axisy" => array(),
"colour" => 'ff9900'
)
);
foreach ($xml->invoice as $invoice) {
$status = strtolower($invoice->status);
$value = $invoice->{'net-value'};
$week = (int) date("W", strtotime($invoice->{'dated-on'}));
if (!isset($results[$week])) {
$results[$week] = array(
"value" => 0,
"count" => 0
);
}
$results[$week]['value'] += $value;
$results[$week]['count']++;
}
$max_average = 0;
for ($i = 0; $i < 52; $i++) {
$date = strtotime("-{$i} weeks");
$current_week = (int) date("W", $date);
if ($i % 12 == 0) {
$return['settings']['axisx'][] = date("M y", $date);
}
if (!isset($results[$current_week])) {
$average = 0;
} else {
$average = $results[$current_week]['value'] / $results[$current_week]['count'];
}
if ($average > $max_average) {
$max_average = $average;
}
$return['item'][] = (int) $average;
}
$return['settings']['axisy'] = array(
0,
number_format($max_average * 0.25),
number_format($max_average * 0.50),
number_format($max_average * 0.75),
number_format($max_average)
);
echo json_encode($return);