This repository was archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·277 lines (246 loc) · 9.03 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
require_once 'vendor/autoload.php';
use Aws\CostExplorer\CostExplorerClient;
use XeroPHP\Models\Accounting\Invoice;
use XeroPHP\Models\Accounting\InvoiceReminder;
use XeroPHP\Models\Accounting\Invoice\LineItem;
use XeroPHP\Models\Accounting\Contact;
use XeroPHP\Models\Accounting\TrackingCategory;
use XeroPHP\Application\PrivateApplication;
//define('DS', DIRECTORY_SEPARATOR);
//define('APP_ROOT', realpath(__DIR__ . DS . '..' . DS));
define('APP_ROOT', realpath(__DIR__));
/**
* Helper function for quick logging.
*
* @param $message string
* The log message.
*
* @param $log_level mixed
* The system log level for syslog.
* Set to NULL to not log to syslog.
**/
function log_output($message, $log_level = LOG_INFO, $to_screen = TRUE) {
if ($to_screen) {
echo "Billder: " . $message . "\n";
}
if ($log_level) {
syslog($log_level, "Billder: " . $message);
}
}
############################################
# SETUP
// Parse incoming cli arguments
if (count($argv) < 2) {
log_output("Not enough arguments!", LOG_ALERT);
exit(1);
}
else {
array_shift($argv);
}
// See if a month was provided and set a default if not.
if (isset($argv[1])) {
log_output("Received a month to process: " . $argv[1]);
$target_month = $argv[1];
}
else {
$target_month = date('Y-m', strtotime('first day of last month'));
log_output("Did not receive a month to process, using default month: " . $target_month);
}
// Load the config file
$config_path = APP_ROOT . "/" . $argv[0] . ".json";
if ($config_file = fopen($config_path, "r")) {
$config = json_decode(fread($config_file, filesize($config_path)));
fclose($config_file);
log_output("Successfully loaded config file: " . $config_path);
}
else {
log_output("Could not open file: " . $config_path, LOG_ALERT);
exit(1);
}
// Load the accounts file
$accounts_path = APP_ROOT . "/" . $config->aws->accountsFile . ".json";
if ($accounts_file = fopen($accounts_path, "r")) {
$accounts = json_decode(fread($accounts_file, filesize($accounts_path)));
fclose($accounts_file);
log_output("Successfully loaded accounts file: " . $accounts_path);
}
else {
log_output("Could not open file: " . $accounts_path, LOG_ALERT);
exit(1);
}
################################
# AWS
# Instantiate the CostExplorerClient class with our credentials
$billing = new CostExplorerClient([
'version' => 'latest',
'region' => $config->aws->region,
'credentials' => [
'key' => $config->aws->key,
'secret' => $config->aws->secret,
],
]);
// Looping through accounts
foreach ($accounts as $client => $data) {
// Currency look-up
if ($data->currency != 'USD') {
$curl = new Curl\Curl();
$curl->get('https://api.frankfurter.app/latest', array(
'from' => 'USD',
'to' => $data->currency,
));
if ($curl->error) {
log_output("Problem with currency API", LOG_ALERT);
log_output("API output: " . $curl->error_code, LOG_ALERT);
exit(1);
}
else {
$currency_data = json_decode($curl->response);
// Stash exchange rate in the $accounts object
$accounts->$client->exchange_rate = $currency_data->rates->{$data->currency};
log_output("Currency for $client set to: " . $currency_data->rates->{$data->currency});
}
}
else {
$accounts->$client->exchange_rate = 1;
log_output("Currency for $client already USD, rate set to 1");
}
# Get a billing report
$billing_data = $billing->getCostAndUsage([
'Filter' => [
'Dimensions' => [
'Key' => 'LINKED_ACCOUNT',
'Values' => [$data->AWS],
],
],
'Granularity' => 'MONTHLY',
'GroupBy' => [
/**
* USAGE_TYPE / OPERATION don't show EC2 usage
* PURCHASE_TYPE shows billing but no breakdown
* PLATFORM shows only EC2 usage (by EC2 platform type)
* SERVICE looks like the correct grouping!
*/
[
'Key' => 'SERVICE',
'Type' => 'DIMENSION', // can be TAG or DIMENSION
],
],
'Metrics' => array('UnblendedCost'),
'TimePeriod' => [
'Start' => $target_month . '-01',
'End' => $target_month . "-" . date('t',strtotime($target_month)),
],
]);
// Stash billing data in the $accounts object
$accounts->$client->billing_data = $billing_data['ResultsByTime'][0];
// Just tot things up and log, for safety
$total = 0;
foreach ($billing_data['ResultsByTime'][0]['Groups'] as $subtotals) {
$total = $total + (float)$subtotals['Metrics']['UnblendedCost']['Amount'];
}
log_output("Total to bill from AWS for $client is: $" . round($total, 2));
}
###########################
# XERO
$xero_config = [
'oauth' => [
'callback' => 'http://localhost/', // not really needed for a private app
'consumer_key' => $config->provider->oauth->client->key, // get from Xero developer portal
'consumer_secret' => $config->provider->oauth->client->secret, // get from Xero developer portal
'rsa_private_key' => 'file://' . APP_ROOT . '/' . $config->provider->oauth->client->rsaKey, // self-signed - upload public part to Xero developer portal
],
//'curl' => [
// CURLOPT_USERAGENT => 'xero-php sample app',
// CURLOPT_CAINFO => __DIR__ . '/certs/ca-bundle.crt',
//],
];
// Instantiate our application
log_output("Connecting to Xero");
$xero = new PrivateApplication($xero_config);
// Looping through accounts
foreach ($accounts as $client => $data) {
// Build our line items
// @TODO: there does not yet seem to be any way to add a line item to a project via the API
// I have raised a ticket with Xero to find out
log_output("Building line items for $client");
$line_items = array();
foreach ($data->billing_data['Groups'] as $aws_item) {
$line_item = new LineItem($xero);
// Do any currency calculation
$line_item_value = round((float)$aws_item['Metrics']['UnblendedCost']['Amount'], 2);
// If we're not including tax, zero that line so it gets ignored
// @TODO: this would be better if we can filter it in the AWS call
// See https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_Expression.html
if (!(bool)$config->general->includeTax && $aws_item['Keys'][0] == 'Tax') {
$line_item_value = 0;
}
// No need to create empty lines
if ($line_item_value > 0) {
// Convert the currency if necessary
if ($data->exchange_rate != 1) {
$line_item_value = $line_item_value * $data->exchange_rate;
}
// Handling VAT
// See https://developer.xero.com/documentation/api/types#TaxTypes
$tax_type = 'CAPEXOUTPUT2'; // default to 20% VAT on sales
// @TODO: needs reviewing in light of Brexit
if ($data->EU == TRUE) {
$tax_type = 'ECZROUTPUTSERVICES'; // no VAT on EU services
log_output("$client is in the EU and provided a VAT number, no VAT to be charged");
}
elseif ($data->VAT == FALSE) {
$tax_type = 'NONE'; // customer VAT exempt (e.g. US-based businesses or UN body)
log_output("$client is VAT exempt");
}
// We cannot setTaxType if our incomeCategory assumes VAT
// @TODO: we ought to check the tax settings of the AccountCode in config.json and react accordingly.
if ($tax_type != 'CAPEXOUTPUT2') {
$line_item->setTaxType($tax_type);
}
// Optionally handle tracking category
if (isset($config->provider->trackingCategory)) {
$tracking = new TrackingCategory($xero);
$tracking->setName($config->provider->trackingCategory)
->setOption($config->provider->trackingCategoryOption);
$line_item->addTracking($tracking);
}
// Build the rest of the line item
$line_item->setDescription($aws_item['Keys'][0])
->setQuantity(1)
->setAccountCode($config->provider->incomeCategory)
->setUnitAmount($line_item_value * $config->general->costPadding);
$line_items[] = $line_item;
}
}
// Build our invoice
log_output("Building invoice for $client");
$invoice = new Invoice($xero);
// Load our contact
$contact = $xero->loadByGUID(Contact::class, $data->contact);
// Add our line items
foreach ($line_items as $line_item) {
$invoice->addLineItem($line_item);
}
// Optionally set a reference, e.g. a purchase order number
if (isset($data->reference)) {
$invoice->setReference($data->reference);
log_output("Reference found and set to: $data->reference");
}
// Optionally set a default reference where there is no account specific one, e.g. 'AWS Rebilling'
elseif (isset($config->general->defaultReference)) {
$invoice->setReference($config->general->defaultReference);
}
// Optionally set the status of the invoice
if (isset($config->provider->defaultStatus)) {
$invoice->setStatus($config->provider->defaultStatus);
}
$invoice->setType('ACCREC')
->setCurrencyCode($data->currency)
->setLineAmountType('Exclusive')
->setDueDate(\DateTime::createFromFormat('Y-m-d', date('Y-m-d', strtotime("+30 days"))))
->setContact($contact);
// Save the invoice
$invoice->save();
log_output("Invoice saved for $client with ID: " . $invoice->getInvoiceID());
}