-
Notifications
You must be signed in to change notification settings - Fork 1
/
bxobject.php
465 lines (422 loc) · 11.4 KB
/
bxobject.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* @author Etienne Bagnoud <etienne@artnum.ch>
* @license MIT
* @copyright 2023 Etienne Bagnoud
*
*/
namespace BizCuit\BXObject;
use stdClass;
abstract class BXObject {
/** @var string|null */
public const ID = 'id';
/** @var string|null */
public const NR = 'nr';
/** @var array */
protected const createProperties = [];
/** @var array */
protected const nullableProperties = [];
/** @var array */
protected const mapProperties = [];
/** @var array */
protected const removeOnSet = [];
/** @var bool */
protected const readonly = false;
protected array $changes = [];
protected stdClass $content;
function __construct(stdClass $object = new stdClass()) {
$this->content = new stdClass();
foreach($this::createProperties as $prop) {
$this->{$prop} = null;
}
foreach (get_object_vars($object) as $prop => $value) {
$this->{$prop} = $value;
}
$this->changes = [];
}
function getId():mixed {
if (!isset($this->content->{$this::ID})) { return null; }
return $this->content->{$this::ID};
}
function getType ():string {
$parts = explode('\\', get_class($this));
return array_pop($parts);
}
function getNumber():mixed {
if ($this::NR === null) { return null; }
return $this->content->{$this::NR};
}
function toJson():string {
$outClass = clone $this->content;
foreach (get_object_vars($outClass) as $k => $_) {
/* when requesting data, it appears that null properties are set to 0 and fail to write back */
if (in_array($k, $this::nullableProperties) && $outClass->{$k} === 0) { $outClass->{$k} = null; }
}
return json_encode($outClass);
}
function toObject():stdClass {
return clone $this->content;
}
function changesToJson():string {
$outClass = new stdClass();
foreach($this->changes as $key) {
$outClass->{$key} = $this->{$key};
}
return json_encode($outClass);
}
function isReadonly():bool {
return $this->readonly;
}
function getRemoveOnSet():array {
return $this::removeOnSet;
}
function __get(string $name):mixed {
if (isset($this::mapProperties[$name])) {
$name = $this::mapProperties[$name];
}
if (property_exists($this->content, $name)) { return $this->content->{$name}; }
return false;
}
function __set(string $name, mixed $value) {
if (isset($this::mapProperties[$name])) {
$name = $this::mapProperties[$name];
}
$this->content->{$name} = $value;
if (!in_array($name, $this->changes)) { $this->changes[] = $name; }
return $this->content->{$name};
}
function __unset($name) {
if (isset($this::mapProperties[$name])) {
$name = $this::mapProperties[$name];
}
unset($this->content->{$name});
if (in_array($name, $this->changes)) {
$this->changes = array_filter($this->changes, fn ($e) => $e !== $name );
}
}
}
class ROObject extends BXObject {
const NR = null;
const readonly = true;
function toJson():string {
return json_encode(clone $this->content);
}
function changesToJson():string {
return json_encode(new stdClass());
}
}
class File extends ROObject {}
class ProjectType extends ROObject {}
class ProjectStatus extends ROObject {}
class Title extends ROObject {}
class ClientService extends ROObject {}
class Currency extends ROObject {}
class Expense extends ROObject {}
class ContactSector extends ROObject { }
class Payment extends ROObject { }
class Unit extends ROObject { }
class StockLocation extends ROObject { }
class BusinessActivity extends ROObject { }
class CommunicationType extends ROObject { }
class BankAccount extends ROObject { }
class Country extends BXObject {
const NR = null;
const createProperties = [
'name',
'name_short',
'iso3166_alpha2'
];
/* API BUG when requesting a country, iso_3166_alpha2 is set with the value
* you expect to be in iso3166_alpha2 (which is what the documentation
* describe). If you create a country with the attribute iso_3166_alpha2,
* the server return an error, so you have to map that property
*/
const mapProperties = [
'iso_3166_alpha2' => 'iso3166_alpha2'
];
}
class Contact extends BXObject {
const NR = null;
const createProperties = [
'nr',
'contact_type_id',
'name_1',
'name_2',
'salutation_id',
'salutation_form',
'titel_id',
'birthday',
'address',
'postcode',
'city',
'country_id',
'mail',
'mail_second',
'phone_fixed',
'phone_fixed_second',
'phone_mobile',
'fax',
'url',
'skype_name',
'remarks',
'language_id',
'contact_group_ids',
'contact_branch_ids',
'user_id',
'owner_id'
];
/* API BUG when requesting a contact, if no salutation are set, the result
* will have salutation_id = 0, but when sending back to update, edit or
* replace the contact, the server complains with this value not being to
* "null", so this is to fix that.
*/
const nullableProperties = [
'salutation_id'
];
}
class ContactRelation extends BXObject {
const NR = null;
const createProperties = [
'contact_id',
'contact_sub_id',
'description'
];
}
class AdditionalAddress extends BXObject {
const NR = null;
const createProperties = [
'name',
'address',
'postcode',
'city',
'country_id',
'subject',
'description'
];
}
class Note extends BXObject {
const NR = null;
const createProperties = [
'user_id',
'event_start',
'subject',
'info',
'contact_id',
'pr_project_id',
'entry_id',
'module_id'
];
}
class Project extends BXObject {
const NR = null;
const createProperties = [
'name',
'start_date',
'end_date',
'comment',
'pr_state_id',
'pr_project_type_id',
'contact_id',
'contact_sub_id',
'pr_invoice_type_id',
'pr_invoice_type_amount',
'pr_budget_type_id',
'pr_budget_type_amount',
'user_id'
];
}
class Order extends BXObject {
const NR = 'document_nr';
const createProperties = [
'title',
'contact_id',
'contact_sub_id',
'user_id',
'project_id',
'language_id',
'bank_account_id',
'currency_id',
'payment_type_id',
'header',
'footer',
'mwst_type',
'mwst_is_net',
'show_position_taxes',
'is_valid_from',
'delivery_address_type',
'api_reference',
'template_slug',
'positions'
];
}
class Invoice extends BXObject {
const NR = 'document_nr';
const createProperties = [
'title',
'contact_id',
'contact_sub_id',
'user_id',
'language_id',
'bank_account_id',
'currency_id',
'payment_type_id',
'header',
'footer',
'mwst_type',
'show_position_taxes',
'is_valid_from',
'reference',
'api_reference',
'template_slug',
'positions'
];
}
class Quote extends BXObject {
const NR = 'document_nr';
const createProperties = [
'title',
'contact_id',
'contact_sub_id',
'user_id',
'language_id',
'bank_account_id',
'currency_id',
'payment_type_id',
'header',
'footer',
'mwst_type',
'mwst_is_net',
'show_position_taxes',
'is_valid_from',
'is_valid_until',
'delivery_address_type',
'api_reference',
'viewed_by_client_at',
'kb_terms_of_payment_template_id',
'template_slug',
'positions'
];
}
class User extends BXObject {
const NR = null;
const createProperties = [
'salutation_type',
'firstname',
'lastname',
'email',
'is_superadmin',
'is_accountant'
];
}
class Bills extends BXObject {
const NR = null;
const createProperties = [
'supplier_id',
'vendor_ref',
'title',
'contact_partner_id',
'bill_date',
'due_date',
'amount_man',
'amount_calc',
'manual_amount',
'currency_code',
'exchange_rate',
'base_currency_amount',
'item_net',
'attachment_ids',
'address',
'line_items',
'discounts',
'payment'
];
/* When updating a bill, the server complains about the purchase_order_id
* being in the body. When you get the bill, the server answer with that
* value set. So to fix that, we remove the value.
*/
const removeOnSet = [
'id',
'purchase_order_id',
'qr_bill_information',
'status',
'firstname_suffix',
'lastname_company',
'created_at',
'base_currency_code',
'pending_amount',
'overdue'
];
}
class PDF extends BXObject {
const createProperties = [
'name',
'size',
'mime',
'content'
];
function __construct(stdClass $object = new stdClass()) {
$this->content = new stdClass();
foreach($this::createProperties as $prop) {
$this->{$prop} = null;
}
foreach (get_object_vars($object) as $prop => $value) {
if ($prop === 'content') {
$this->{$prop} = base64_decode($value);
continue;
}
$this->{$prop} = $value;
}
$this->changes = [];
}
function toJson():string {
$outClass = new stdClass();
foreach (get_object_vars($this->content) as $k => $v) {
if ($k === 'content') {
$outClass->{$k} = base64_encode($v);
continue;
}
$outClass->{$k} = $v;
}
foreach (get_object_vars($outClass) as $k => $_) {
if (!in_array($k, $this::createProperties)) { unset($outClass->{$k}); }
/* when requesting data, it appears that null properties are set to 0 and fail to write back */
if (in_array($k, $this::nullableProperties) && $outClass->{$k} === 0) { $outClass->{$k} = null; }
}
return json_encode($outClass);
}
function changesToJson():string {
$outClass = new stdClass();
foreach($this->changes as $key) {
if ($key === 'content') {
$outClass->{$key} = base64_encode($this->{$key});
}
$outClass->{$key} = $this->{$key};
}
return json_encode($outClass);
}
}
class ContactGroup extends BXObject {
const NR = null;
const createProperties = [
'name'
];
}
class OutgoingPayment extends BXObject {
const NR = null;
const createProperties = [
'bill_id',
'payment_type',
'execution_date',
'amount',
'currency_code',
'exchange_rate',
'sender_bank_account_id',
'is_salary_payment'
];
}
class Salutation extends BXObject {
const NR = null;
const createProperties = [
'name'
];
}