-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoshl.php
161 lines (147 loc) · 4.59 KB
/
toshl.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
<?php
include("./google-sheets/get-google-sheets-rules.php");
function get_accesstoken($filename = "credentials.txt")
{
$f = fopen($filename, 'r');
$line = fgets($f);
fclose($f);
return $line;
}
function list_entries($from = "2020-12-01", $to = "2020-12-31")
{
global $accesstoken;
// create curl resource
$ch = curl_init();
// set url
$endpoint = 'https://api.toshl.com/entries';
$params = array('from' => $from, "to" => $to);
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $accesstoken);
$output = curl_exec($ch);
$output = json_decode($output);
// close curl resource to free up system resources
curl_close($ch);
return $output;
}
function get_entries($id = "235733109")
{
global $accesstoken;
// create curl resource
$ch = curl_init();
// set url
$endpoint = 'https://api.toshl.com/entries/';
$url = $endpoint . $id;
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $accesstoken);
$output = curl_exec($ch);
$output = json_decode($output);
// close curl resource to free up system resources
curl_close($ch);
return $output;
}
function list_accounts()
{
global $accesstoken;
// create curl resource
$ch = curl_init();
// set url
$endpoint = 'https://api.toshl.com/accounts';
curl_setopt($ch, CURLOPT_URL, $endpoint);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $accesstoken);
$output = curl_exec($ch);
$output = json_decode($output);
//print_r($output);
foreach ($output as $account) {
if (!empty($account->connection)) {
print_r($account->connection->name . "\t");
print_r($account->name . "\t");
print_r($account->id . "\n");
}
}
// close curl resource to free up system resources
curl_close($ch);
return $output;
}
/**
* @brief get array with all existing categories
* @return array with the following objects:
* [id] => 50171653
* [name] => Food & Drinks
* [name_override] =>
* [modified] => 2016-04-04 18:50:17.653
* [type] => expense
* [deleted] =>
* [counts] => stdClass Object
* (
* [entries] => 49
* [tags_used_with_category] => 5
* [tags] => 6
* [budgets] => 0
* )
*/
function list_categories()
{
global $accesstoken;
// create curl resource
$ch = curl_init();
// set url
$endpoint = 'https://api.toshl.com/categories';
$params = array('per_page' => 500);
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $accesstoken);
$output = curl_exec($ch);
$output = json_decode($output);
//print_r($output);
/* foreach ($output as $category) {
print_r($category);
} */
// close curl resource to free up system resources
curl_close($ch);
return $output;
}
function list_tags()
{
global $accesstoken;
// create curl resource
$ch = curl_init();
// set url
$endpoint = 'https://api.toshl.com/tags';
$params = array('per_page' => 500);
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $accesstoken);
$output = curl_exec($ch);
$output = json_decode($output);
//print_r($output);
/* foreach ($output as $category) {
print_r($category);
} */
// close curl resource to free up system resources
curl_close($ch);
return $output;
}
$accesstoken = get_accesstoken();
$errors = array();
$dry_run_messages = array();
$entries = list_entries();
$rules = get_toshl_rules();
print_r(get_changing_entries($entries, $rules));
print_r($errors);
/**
* Tags can exist as subtags of categories and as standalone global tags.
* An expense can have only one category but more than one tag.
* Global tags are a way to assign several "categories" to one expense
* Category tags are a way to assign subcategories to an expense
*/