forked from superfaktura/apiclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2.php
More file actions
291 lines (249 loc) · 7.78 KB
/
Copy pathsample2.php
File metadata and controls
291 lines (249 loc) · 7.78 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
$bench = true; // true = dump result | false = skip dump
$time_round = 4;
$tstart = microtime(true);
/*******************************************
* SFAPI v.: 1.1 example
*******************************************
* info@superfaktura.sk
*******************************************/
DEFINE('SFAPI_EMAIL', 'example@example.com'); // LOGIN EMAIL TO SUPERFAKTURA
DEFINE('SFAPI_KEY', 'apikey'); // SFAPI KEY
DEFINE('SFAPI_MODULE', 'API'); // TITLE OF MODULE FE. 'WOOCOMMERCE MODULE'
DEFINE('SFAPI_APPTITLE', 'Example API application'); // TITLE OF YOUR APPLICATION FE. 'SUPERFAKTURA.SK'
require_once('SFAPIclient/SFAPIclient.php');
// Create and init SFAPIclient
$api = new SFAPIclient(SFAPI_EMAIL, SFAPI_KEY, SFAPI_APPTITLE, SFAPI_MODULE);
/***********************************************
* Example; create new invoice
***********************************************/
// 1) set Client
$api->setClient(array(
'name' => 'Example s.r.o.', // Client name
'phone' => '+421000000000', // Client phone
'email' => 'exampleclient@example.com' // Client email
));
// 2) set Invoice
$api->setInvoice(array(
'name' => 'Invoice example', // Invoice name
'variable' => '123456', // Variable
'due' => date('Y-m-d', strtotime("+20 day")), // Due date
'already_paid' => true // If true invoice is paid
));
// 3) add new Invioce item
$api->addItem(array(
'name' => 'Example item', // Invoice item name
'descriptions' => 'Description of "Example item"', // Invoice item description
'unit_price' => 20, // Unit price without vat
'tax' => 20, // Vat
'quantity' => 5, // Quantity
'discount' => 50, // Invoice item discount in %
'discount_description' => 'Discount 50% COUPON: AA11BB22CC33'
));
// 4) save data
$response = $api->save();
if (!empty($bench)) {
_debug($response, 'Create new invoice example');
}
/********************************************
* Example; create new expense
********************************************/
// 1) Reset data
$api->resetData();
// 2) Set new Expense
$api->setExpense(array(
'name' => 'Example expense', // Expense name
'variable' => 123456, // Variable
'amount' => 100, // Price no vat
'vat' => 20, // Vat
'already_paid' => true // Expense is allready paid
));
// 3) Save Expense
$response = $api->save();
if (!empty($bench)) {
_debug($response, 'Create new expense example');
}
/********************************************
* Example; edit invoice
********************************************/
$invoice_id = 0; // existing invoice id; Invoice->id
if (!empty($invoice_id)) {
// 1) Do not update Client
$api->setClient(array());
// 2) Set invoice data to update
$api->setInvoice(array(
'id' => $invoice_id,
'name' => 'Example invoice "EDIT EXAMPLE"',
'comment' => 'Example comment "EDIT EXAMPLE"',
'variable' => 333
));
// 3) Add new InvoiceItem
$api->addItem(array(
'name' => 'New example item',
'description' => 'Example item created from edit',
'unit_price' => round(M_PI, 2),
'quantity' => 2
));
// 4) Update invoice data
$response = $api->edit();
if (!empty($bench)) {
_debug($response, 'Edit invoice example');
}
}
/*********************************************
* Example; editing example expense
*********************************************/
$expense_id = 0;
if (!empty($expense_id)) {
// 1) Set new Client
$api->setClient(array(
'name' => 'Example client (test)',
'ico' => mt_rand(10000000, 99999999),
'dic' => '012345678',
'email' => 'mynewexampleclient@example.com'
));
// 2) Set Expense data to update
$api->setExpense(array(
'id' => $expense_id,
'name' => 'Expense example EDIT',
'amount' => round(M_E * 100),
'currency' => 'CZK'
));
// 3) Update Expense data
$response = $api->edit();
if (!empty($bench)) {
_debug($response, 'Edit expense example');
}
}
/********************************************
* Example; send invoice by email
* , post
********************************************/
if (!empty($invoice_id)) {
$response = $api->sendInvoiceEmail(array(
'invoice_id' => $invoice_id,
'to' => 'diamonjohn@gmail.com',
'cc' => array(
'example2@example.com',
'example3@example.com'
),
'bcc' => array(
'example4@example.com'
)
// , 'subject' => 'From API'
// , 'body' => 'Ostra faktura test'
));
if (!empty($bench)) {
_debug($response, 'Send email example');
}
$send_invoice_post = false;
if ($send_invoice_post) {
$response = $api->sendInvoicePost(array(
'invoice_id' => $invoice_id,
/*
// uncomment to set custom delivery address
'delivery_address' => 'Address 333',
'delivery_city' => 'MyCity',
'delivery_zip' => '94911',
'delivery_country' => 'Slovenska republika'
*/
));
if (!empty($bench)) {
_debug($response, 'Send invoice post');
}
}
}
/*******************************************
* Example; update invoice item
*******************************************/
$invoice_item_id = 0; // set to your item id; belongs to invoice
if (!empty($invoice_id) && !empty($invoice_item_id)) {
// 1) Empty client, do not update
$api->setClient(array());
// 2) Set invoice id
$api->setInvoice(array(
'id' => $invoice_id
));
// 3) Set item
$api->addItem(array(
'id' => $invoice_item_id,
'name' => '*Edited invoice item.',
'unit_price' => round(M_E * 1000, 2),
'quantity' => round(M_PI)
));
// 4) Update expense data
$response = $api->edit();
if (!empty($bench)) {
_debug($response, 'Update invoice item');
}
}
/*******************************************
* Example; pay invoice
*******************************************/
if (!empty($invoice_id)) {
// 1) Add Inovice payment
$response = $api->payInvoice($invoice_id, 333);
if (!empty($bench)) {
_debug($response, 'Add invoice payment');
}
}
/*******************************************
* Example; pay expense
*******************************************/
if (!empty($expense_id)) {
// 1) Add Expense payment
$response = $api->payExpense($expense_id, 333);
if (!empty($bench)) {
_debug($response, 'Add expense payment.');
}
}
/*******************************************
* Example; create new stock item
*******************************************/
// 1) Add new stock item
$response = $api->addStockItem(array(
'name' => 'Stock item example',
'description' => 'Example stock item description.',
'sku' => 'STOCK'.mt_rand(100, 999).'ID',
'unit_price' => round(M_PI, 2),
'vat' => 20,
'stock' => 10
));
if (!empty($bench)) {
_debug($response, 'Add stock item');
}
/******************************************
* Pridame pohyb na sklade
******************************************/
$stock_item_id = 0;
if (!empty($stock_item_id)) {
// Add new stock movement
$response = $api->addStockMovement(array(
'stock_item_id' => $stock_item_id,
'quantity' => 50, // fe. -50 negative movement
'note' => 'Stock movement example from API'
));
if (!empty($bench)) {
_debug($response, 'Add stock movement');
}
}
$tend = microtime(true);
if (!empty($bench)) {
// output time
echo '<div style="background:red;color:white;padding:10px;margin-top:10px;">Bench is ON. Microtime in secs ~ <b>'.round($tend - $tstart, $time_round).'s</b><br></div>';
}
/***********************************************
* dump in readable format
***********************************************/
function _debug($obj, $title = '') {
if (!is_array($obj)) {
$obj = json_decode(json_encode($obj), true);
}
if (!empty($title)) {
echo "<h2>$title</h2>";
}
echo '<code style="white-space: pre;background:#FAFCAC;margin-top:10px;padding:15px;display:block;width:450px;">';
print_r($obj);
echo '</code>';
}
?>