|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU General Public License as published by |
|
6
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use Luracast\Restler\RestException; |
|
19
|
|
|
|
|
20
|
|
|
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* API class for invoices |
|
24
|
|
|
* |
|
25
|
|
|
* @access protected |
|
26
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
|
27
|
|
|
*/ |
|
28
|
|
|
class Invoices extends DolibarrApi |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @var array $FIELDS Mandatory fields, checked when create and update object |
|
33
|
|
|
*/ |
|
34
|
|
|
static $FIELDS = array( |
|
35
|
|
|
'socid' |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Facture $invoice {@type Facture} |
|
40
|
|
|
*/ |
|
41
|
|
|
public $invoice; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
*/ |
|
46
|
|
|
function __construct() |
|
47
|
|
|
{ |
|
48
|
|
|
global $db, $conf; |
|
49
|
|
|
$this->db = $db; |
|
50
|
|
|
$this->invoice = new Facture($this->db); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get properties of a invoice object |
|
55
|
|
|
* |
|
56
|
|
|
* Return an array with invoice informations |
|
57
|
|
|
* |
|
58
|
|
|
* @param int $id ID of invoice |
|
59
|
|
|
* @return array|mixed data without useless information |
|
60
|
|
|
* |
|
61
|
|
|
* @throws RestException |
|
62
|
|
|
*/ |
|
63
|
|
|
function get($id) |
|
64
|
|
|
{ |
|
65
|
|
|
if(! DolibarrApiAccess::$user->rights->facture->lire) { |
|
66
|
|
|
throw new RestException(401); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$result = $this->invoice->fetch($id); |
|
70
|
|
|
if( ! $result ) { |
|
71
|
|
|
throw new RestException(404, 'Invoice not found'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { |
|
75
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->_cleanObjectDatas($this->invoice); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* List invoices |
|
83
|
|
|
* |
|
84
|
|
|
* Get a list of invoices |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $sortfield Sort field |
|
87
|
|
|
* @param string $sortorder Sort order |
|
88
|
|
|
* @param int $limit Limit for list |
|
89
|
|
|
* @param int $page Page number |
|
90
|
|
|
* @param string $thirdparty_ids Thirdparty ids to filter orders of. {@example '1' or '1,2,3'} {@pattern /^[0-9,]*$/i} |
|
91
|
|
|
* @param string $status Filter by invoice status : draft | unpaid | paid | cancelled |
|
92
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
|
93
|
|
|
* @return array Array of invoice objects |
|
94
|
|
|
* |
|
95
|
|
|
* @throws RestException |
|
96
|
|
|
*/ |
|
97
|
|
|
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $thirdparty_ids='', $status='', $sqlfilters = '') { |
|
98
|
|
|
global $db, $conf; |
|
99
|
|
|
|
|
100
|
|
|
$obj_ret = array(); |
|
101
|
|
|
|
|
102
|
|
|
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid |
|
103
|
|
|
$socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids; |
|
104
|
|
|
|
|
105
|
|
|
// If the internal user must only see his customers, force searching by him |
|
106
|
|
|
$search_sale = 0; |
|
107
|
|
|
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; |
|
108
|
|
|
|
|
109
|
|
|
$sql = "SELECT t.rowid"; |
|
110
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) |
|
111
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."facture as t"; |
|
112
|
|
|
|
|
113
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale |
|
114
|
|
|
|
|
115
|
|
|
$sql.= ' WHERE t.entity IN ('.getEntity('facture', 1).')'; |
|
116
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc"; |
|
117
|
|
|
if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")"; |
|
118
|
|
|
|
|
119
|
|
|
if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
|
120
|
|
|
|
|
121
|
|
|
// Filter by status |
|
122
|
|
|
if ($status == 'draft') $sql.= " AND t.fk_statut IN (0)"; |
|
123
|
|
|
if ($status == 'unpaid') $sql.= " AND t.fk_statut IN (1)"; |
|
124
|
|
|
if ($status == 'paid') $sql.= " AND t.fk_statut IN (2)"; |
|
125
|
|
|
if ($status == 'cancelled') $sql.= " AND t.fk_statut IN (3)"; |
|
126
|
|
|
// Insert sale filter |
|
127
|
|
|
if ($search_sale > 0) |
|
128
|
|
|
{ |
|
129
|
|
|
$sql .= " AND sc.fk_user = ".$search_sale; |
|
130
|
|
|
} |
|
131
|
|
|
// Add sql filters |
|
132
|
|
|
if ($sqlfilters) |
|
133
|
|
|
{ |
|
134
|
|
|
if (! DolibarrApi::_checkFilters($sqlfilters)) |
|
135
|
|
|
{ |
|
136
|
|
|
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
|
137
|
|
|
} |
|
138
|
|
|
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
|
139
|
|
|
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$sql.= $db->order($sortfield, $sortorder); |
|
143
|
|
|
if ($limit) { |
|
144
|
|
|
if ($page < 0) |
|
145
|
|
|
{ |
|
146
|
|
|
$page = 0; |
|
147
|
|
|
} |
|
148
|
|
|
$offset = $limit * $page; |
|
149
|
|
|
|
|
150
|
|
|
$sql.= $db->plimit($limit + 1, $offset); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$result = $db->query($sql); |
|
154
|
|
|
if ($result) |
|
155
|
|
|
{ |
|
156
|
|
|
$i=0; |
|
157
|
|
|
$num = $db->num_rows($result); |
|
158
|
|
|
while ($i < min($num, ($limit <= 0 ? $num : $limit))) |
|
159
|
|
|
{ |
|
160
|
|
|
$obj = $db->fetch_object($result); |
|
161
|
|
|
$invoice_static = new Facture($db); |
|
162
|
|
|
if($invoice_static->fetch($obj->rowid)) { |
|
163
|
|
|
$obj_ret[] = $this->_cleanObjectDatas($invoice_static); |
|
164
|
|
|
} |
|
165
|
|
|
$i++; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
else { |
|
169
|
|
|
throw new RestException(503, 'Error when retrieve invoice list : '.$db->lasterror()); |
|
170
|
|
|
} |
|
171
|
|
|
if( ! count($obj_ret)) { |
|
172
|
|
|
throw new RestException(404, 'No invoice found'); |
|
173
|
|
|
} |
|
174
|
|
|
return $obj_ret; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Create invoice object |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $request_data Request datas |
|
181
|
|
|
* @return int ID of invoice |
|
182
|
|
|
*/ |
|
183
|
|
|
function post($request_data = NULL) |
|
184
|
|
|
{ |
|
185
|
|
|
if(! DolibarrApiAccess::$user->rights->facture->creer) { |
|
186
|
|
|
throw new RestException(401, "Insuffisant rights"); |
|
187
|
|
|
} |
|
188
|
|
|
// Check mandatory fields |
|
189
|
|
|
$result = $this->_validate($request_data); |
|
190
|
|
|
|
|
191
|
|
|
foreach($request_data as $field => $value) { |
|
192
|
|
|
$this->invoice->$field = $value; |
|
193
|
|
|
} |
|
194
|
|
|
if(! array_keys($request_data,'date')) { |
|
195
|
|
|
$this->invoice->date = dol_now(); |
|
196
|
|
|
} |
|
197
|
|
|
/* We keep lines as an array |
|
198
|
|
|
if (isset($request_data["lines"])) { |
|
199
|
|
|
$lines = array(); |
|
200
|
|
|
foreach ($request_data["lines"] as $line) { |
|
201
|
|
|
array_push($lines, (object) $line); |
|
202
|
|
|
} |
|
203
|
|
|
$this->invoice->lines = $lines; |
|
204
|
|
|
}*/ |
|
205
|
|
|
|
|
206
|
|
|
if ($this->invoice->create(DolibarrApiAccess::$user) < 0) { |
|
207
|
|
|
throw new RestException(500, "Error creating invoice", array_merge(array($this->invoice->error), $this->invoice->errors)); |
|
208
|
|
|
} |
|
209
|
|
|
return $this->invoice->id; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Update invoice |
|
214
|
|
|
* |
|
215
|
|
|
* @param int $id Id of invoice to update |
|
216
|
|
|
* @param array $request_data Datas |
|
217
|
|
|
* @return int |
|
218
|
|
|
*/ |
|
219
|
|
|
function put($id, $request_data = NULL) |
|
220
|
|
|
{ |
|
221
|
|
|
if(! DolibarrApiAccess::$user->rights->facture->creer) { |
|
222
|
|
|
throw new RestException(401); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$result = $this->invoice->fetch($id); |
|
226
|
|
|
if( ! $result ) { |
|
227
|
|
|
throw new RestException(404, 'Invoice not found'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { |
|
231
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
foreach($request_data as $field => $value) { |
|
235
|
|
|
if ($field == 'id') continue; |
|
236
|
|
|
$this->invoice->$field = $value; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
if($this->invoice->update($id, DolibarrApiAccess::$user)) |
|
240
|
|
|
return $this->get ($id); |
|
241
|
|
|
|
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Delete invoice |
|
247
|
|
|
* |
|
248
|
|
|
* @param int $id Invoice ID |
|
249
|
|
|
* @return type |
|
250
|
|
|
*/ |
|
251
|
|
|
function delete($id) |
|
252
|
|
|
{ |
|
253
|
|
|
if(! DolibarrApiAccess::$user->rights->facture->supprimer) { |
|
254
|
|
|
throw new RestException(401); |
|
255
|
|
|
} |
|
256
|
|
|
$result = $this->invoice->fetch($id); |
|
257
|
|
|
if( ! $result ) { |
|
258
|
|
|
throw new RestException(404, 'Invoice not found'); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
if( ! DolibarrApi::_checkAccessToResource('facture',$this->invoice->id)) { |
|
262
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if( $this->invoice->delete($id) < 0) |
|
266
|
|
|
{ |
|
267
|
|
|
throw new RestException(500); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return array( |
|
271
|
|
|
'success' => array( |
|
272
|
|
|
'code' => 200, |
|
273
|
|
|
'message' => 'Invoice deleted' |
|
274
|
|
|
) |
|
275
|
|
|
); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Validate fields before create or update object |
|
280
|
|
|
* |
|
281
|
|
|
* @param array|null $data Datas to validate |
|
282
|
|
|
* @return array |
|
283
|
|
|
* |
|
284
|
|
|
* @throws RestException |
|
285
|
|
|
*/ |
|
286
|
|
|
function _validate($data) |
|
287
|
|
|
{ |
|
288
|
|
|
$invoice = array(); |
|
289
|
|
|
foreach (Invoices::$FIELDS as $field) { |
|
290
|
|
|
if (!isset($data[$field])) |
|
291
|
|
|
throw new RestException(400, "$field field missing"); |
|
292
|
|
|
$invoice[$field] = $data[$field]; |
|
293
|
|
|
} |
|
294
|
|
|
return $invoice; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
|