1
|
|
|
<?php namespace Picqer\Financials\Exact\Query; |
2
|
|
|
|
3
|
|
|
use Picqer\Financials\Exact\Connection; |
4
|
|
|
|
5
|
|
|
trait Findable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @return Connection |
9
|
|
|
*/ |
10
|
|
|
abstract function connection(); |
|
|
|
|
11
|
|
|
|
12
|
|
|
abstract function isFillable($key); |
|
|
|
|
13
|
|
|
|
14
|
|
|
public function find($id) |
15
|
|
|
{ |
16
|
|
|
$filter = $this->primaryKey . " eq guid'$id'"; |
|
|
|
|
17
|
|
|
|
18
|
|
|
if ($this->primaryKey === 'Code') { |
19
|
|
|
$filter = $this->primaryKey . " eq $id"; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$records = $this->connection()->get($this->url, [ |
|
|
|
|
23
|
|
|
'$filter' => $filter, |
24
|
|
|
'$top' => 1, // The result will always be 1 but on some entities Exact gives an error without it. |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
$result = isset($records[0]) ? $records[0] : []; |
28
|
|
|
return new self($this->connection(), $result); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function findWithSelect($id, $select = '') |
32
|
|
|
{ |
33
|
|
|
//eg: $oAccounts->findWithSelect('5b7f4515-b7a0-4839-ac69-574968677d96', 'Code, Name'); |
34
|
|
|
$result = $this->connection()->get($this->url, [ |
|
|
|
|
35
|
|
|
'$filter' => $this->primaryKey . " eq guid'$id'", |
|
|
|
|
36
|
|
|
'$select' => $select |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
return new self($this->connection(), $result); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the value of the primary key |
45
|
|
|
* |
46
|
|
|
* @param string $code the value to search for |
47
|
|
|
* @param string $key the key being searched (defaults to 'Code') |
48
|
|
|
* |
49
|
|
|
* @return string (guid) |
50
|
|
|
*/ |
51
|
|
|
public function findId($code, $key='Code'){ |
52
|
|
|
if ( $this->isFillable($key) ) { |
53
|
|
|
$format = ($this->url == 'crm/Accounts' && $key === 'Code') ? '%18s' : '%s'; |
|
|
|
|
54
|
|
|
if (preg_match('/^[\w]{8}-([\w]{4}-){3}[\w]{12}$/', $code)) { |
55
|
|
|
$format = "guid'$format'"; |
56
|
|
|
} |
57
|
|
|
elseif (is_string($code)) { |
58
|
|
|
$format = "'$format'"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$filter = sprintf("$key eq $format", $code); |
62
|
|
|
$request = [ |
63
|
|
|
'$filter' => $filter, |
64
|
|
|
'$top' => 1, |
65
|
|
|
'$select' => $this->primaryKey, |
|
|
|
|
66
|
|
|
'$orderby' => $this->primaryKey, |
67
|
|
|
]; |
68
|
|
|
if( $records = $this->connection()->get($this->url, $request) ){ |
69
|
|
|
return $records[0][$this->primaryKey]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function filter($filter, $expand = '', $select = '', $system_query_options = null, array $headers = []) |
76
|
|
|
{ |
77
|
|
|
$originalDivision = $this->connection()->getDivision(); |
78
|
|
|
|
79
|
|
|
if ($this->isFillable('Division') && preg_match("@Division[\t\r\n ]+eq[\t\r\n ]+([0-9]+)@i", $filter, $divisionId)) { |
80
|
|
|
$this->connection()->setDivision($divisionId[1]); // Fix division |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$request = [ |
84
|
|
|
'$filter' => $filter |
85
|
|
|
]; |
86
|
|
|
if (strlen($expand) > 0) { |
87
|
|
|
$request['$expand'] = $expand; |
88
|
|
|
} |
89
|
|
|
if (strlen($select) > 0) { |
90
|
|
|
$request['$select'] = $select; |
91
|
|
|
} |
92
|
|
|
if (is_array($system_query_options)) { |
93
|
|
|
// merge in other options |
94
|
|
|
// no verification of proper system query options |
95
|
|
|
$request = array_merge($system_query_options, $request); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$result = $this->connection()->get($this->url, $request, $headers); |
|
|
|
|
99
|
|
|
|
100
|
|
|
if (!empty($divisionId)) { |
101
|
|
|
$this->connection()->setDivision($originalDivision); // Restore division |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->collectionFromResult($result); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the first Financial model in by applying $top=1 to the query string. |
110
|
|
|
* |
111
|
|
|
* @return \Picqer\Financials\Exact\Model|null |
112
|
|
|
*/ |
113
|
|
|
public function first() |
114
|
|
|
{ |
115
|
|
|
$results = $this->filter('', '', '', ['$top'=> 1]); |
116
|
|
|
$result = is_array($results) && count($results) > 0 ? $results[0] : null; |
117
|
|
|
|
118
|
|
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getResultSet(array $params = []) |
122
|
|
|
{ |
123
|
|
|
return new Resultset($this->connection(), $this->url, get_class($this), $params); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function get(array $params = []) |
127
|
|
|
{ |
128
|
|
|
$result = $this->connection()->get($this->url, $params); |
|
|
|
|
129
|
|
|
|
130
|
|
|
return $this->collectionFromResult($result); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
public function collectionFromResult($result) |
135
|
|
|
{ |
136
|
|
|
// If we have one result which is not an assoc array, make it the first element of an array for the |
137
|
|
|
// collectionFromResult function so we always return a collection from filter |
138
|
|
|
if ((bool) count(array_filter(array_keys($result), 'is_string'))) { |
139
|
|
|
$result = [ $result ]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
while ($this->connection()->nextUrl !== null) |
143
|
|
|
{ |
144
|
|
|
$nextResult = $this->connection()->get($this->connection()->nextUrl); |
145
|
|
|
|
146
|
|
|
// If we have one result which is not an assoc array, make it the first element of an array for the array_merge function |
147
|
|
|
if ((bool) count(array_filter(array_keys($nextResult), 'is_string'))) { |
148
|
|
|
$nextResult = [ $nextResult ]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$result = array_merge($result, $nextResult); |
152
|
|
|
} |
153
|
|
|
$collection = [ ]; |
154
|
|
|
foreach ($result as $r) { |
155
|
|
|
$collection[] = new self($this->connection(), $r); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $collection; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.