-
Notifications
You must be signed in to change notification settings - Fork 1
/
bxquery.php
447 lines (405 loc) · 7.55 KB
/
bxquery.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
<?php
/**
* @author Etienne Bagnoud <etienne@artnum.ch>
* @license MIT
* @copyright 2023 Etienne Bagnoud
*
*/
namespace BizCuit\BXQuery;
use stdClass;
abstract class BXQuery {
const allowedCriteria = [
'=',
'equal',
'!=',
'not_equal',
'>',
'greater_than',
'<',
'less_than',
'>=',
'greater_equal',
'<=',
'less_equal',
'like',
'not_like',
'is_null',
'not_null',
'in',
'not_in'
];
protected bool $anyField = false;
protected array $allowedField = ['name'];
protected array $query = [];
protected array $localFields = [];
/**
* Create a search object
*
* @param array $allowedField Fields allowed in the query as per Bexio documentation
* @return void
*/
function __construct(array $allowedField = ['name']) {
$this->allowedField = $allowedField;
}
function add(String $field, String $term, String $criteria = '='):bool {
$field = strtolower($field);
$criteria = strtolower($criteria);
if (!in_array($criteria, self::allowedCriteria)) { return false; }
if (!$this->anyField && !in_array($field, $this->allowedField)) { return false; }
$q = new stdClass();
$q->field = $field;
$q->value = $term;
$q->criteria = $criteria;
if ($this->anyField && !in_array($field, $this->allowedField)) {
$this->localFields[] = $q;
} else {
$this->query[] = $q;
}
return true;
}
function remove(String $field):true {
$this->query = array_filter($this->query, fn ($e) => $e->field !== $field );
return true;
}
function replace(String $field, String $term, String $criteria):bool {
$this->remove($field);
return $this->add($field, $term, $criteria);
}
function toJson():string {
return json_encode($this->query);
}
function getRawQuery():array {
return $this->query;
}
function getRawQueryLocal():array {
return $this->localFields;
}
function isWithAnyfields():bool {
return $this->anyField;
}
/**
* The query will run with any fields. Fields that are note searchable on
* Bexio side will be searched locally.
* @return void
*/
function setWithAnyfields():void {
$this->anyField = true;
}
/**
* The query will run with only fields that are searchable on Bexio side.
* @return void
*/
function unsetWithAnyfields():void {
$this->anyField = false;
}
}
class ROObject extends BXQuery {
function add(String $field, String $term, string $criteria = '='):bool {
$field = strtolower($field);
$criteria = strtolower($criteria);
if (!in_array($criteria, self::allowedCriteria)) { return false; }
$q = new stdClass();
$q->field = $field;
$q->value = $term;
$q->criteria = $criteria;
$this->query[] = $q;
return true;
}
}
class ContactSector extends BXQuery { }
class Payment extends BXQuery { }
class Unit extends BXQuery { }
class StockLocation extends BXQuery { }
class BusinessActivity extends BXQuery { }
class CommunicationType extends BXQuery { }
class Currency extends ROObject { }
class ProjectStatus extends ROObject { }
class ProjectType extends ROObject { }
class Title extends ROObject { }
class ClientService extends ROObject { }
class Expense extends ROObject {}
class BankAccount extends ROObject { }
class Project extends BXQuery {
function __construct() {
parent::__construct([
'name',
'contact_id',
'pr_state_id'
]);
}
}
class File extends BXQuery {
function __construct() {
parent::__construct([
'id',
'uuid',
'created_at',
'name',
'extension',
'size_in_bytes',
'mime_type',
'user_id',
'is_archived',
'source_id'
]);
}
}
class Contact extends BXQuery {
function __construct() {
parent::__construct([
'id',
'name_1',
'name_2',
'nr',
'address',
'mail',
'mail_second',
'postcode',
'city',
'country_id',
'contact_group_ids',
'contact_type_id',
'updated_at',
'user_id',
'phone_fixed',
'phone_mobile',
'fax'
]);
}
}
class ContactRelation extends BXQuery {
function __construct() {
parent::__construct([
'contact_id',
'contact_sub_id',
'updated_at'
]);
}
}
class AdditionalAddress extends BXQuery {
function __construct() {
parent::__construct([
'name',
'address',
'postcode',
'city',
'country_id',
'subject',
'email'
]);
}
}
class Quote extends BXQuery {
function __construct() {
parent::__construct([
'id',
'kb_item_status_id',
'document_nr',
'title',
'contact_id',
'contact_sub_id',
'user_id',
'currency_id',
'total_gross',
'total_net',
'total',
'is_valid_from',
'is_valid_to',
'is_valid_until',
'updated_at'
]);
}
}
class Order extends BXQuery {
function __construct() {
parent::__construct([
'id',
'kb_item_status_id',
'document_nr',
'title',
'contact_id',
'contact_sub_id',
'user_id',
'currency_id',
'total_gross',
'total_net',
'total',
'is_valid_from',
'is_valid_to',
'updated_at'
]);
}
}
class Invoice extends BXQuery {
function __construct() {
parent::__construct([
'id',
'kb_item_status_id',
'document_nr',
'title',
'api_reference',
'contact_id',
'contact_sub_id',
'user_id',
'currency_id',
'total_gross',
'total_net',
'total',
'is_valid_from',
'is_valid_to',
'updated_at'
]);
}
}
class InvoiceReminder extends BXQuery {
function __construct() {
parent::__construct([
'title',
'reminder_level',
'is_sent',
'is_valid_from',
'is_valid_to',
]);
}
}
class Account extends BXQuery {
function __construct() {
parent::__construct([
'account_no',
'fibu_account_group_id',
'name',
'account_type'
]);
}
}
class Item extends BXQuery {
function __construct() {
parent::__construct([
'intern_name',
'intern_code'
]);
}
}
class StockArea extends BXQuery {
function __construct() {
parent::__construct([
'name',
'stock_id'
]);
}
}
class Timesheet extends BXQuery {
function __construct() {
parent::__construct([
'id',
'client_service_id',
'contact_id',
'user_id',
'pr_project_id',
'status_id'
]);
}
}
class Country extends BXQuery {
function __construct() {
parent::__construct([
'name',
'name_short'
]);
}
}
class Language extends BXQuery {
function __construct() {
parent::__construct([
'name',
'iso_639_1'
]);
}
}
class Note extends BXQuery {
function __construct() {
parent::__construct([
'event_start',
'contact_id',
'user_id',
'subject',
'module_id',
'entry_id'
]);
}
}
class Task extends BXQuery {
function __construct() {
parent::__construct([
'subject',
'updated_at',
'user_id',
'contact_id',
'todo_status_id',
'module_id',
'entry_id'
]);
}
}
class User extends BXQuery {
function __construct() {
parent::__construct([
'salutation_type',
'firstname',
'lastname',
'email',
'is_superadmin',
'is_accountant'
]);
}
}
class Bills extends BXQuery {
function __construct() {
parent::__construct([
'firstname_suffix',
'lastname_company',
'vendor_ref',
'currency_code',
'document_no',
'title',
'supplier_id',
'document_no',
'net_max',
'net_min',
'gross_max',
'gross_min',
'vendor',
'pending_amount_max',
'pending_amount_min',
'currency_code',
'title',
'vendor_ref',
'due_date_end',
'due_date_start',
'bill_date_end',
'bill_date_start',
'status'
]);
}
}
class ContactGroup extends BXQuery {
function __construct() {
parent::__construct([
'name'
]);
}
}
class OutgoingPayment extends BXQuery {
function __construct() {
parent::__construct([
'bill_id'
]);
}
}
class Salutation extends BXQuery {
function __construct() {
parent::__construct([
'name'
]);
}
}