dolibarr  9.0.0
api_supplier_invoices.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-Fran├žois Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
22 
30 {
35  static $FIELDS = array(
36  'socid',
37  );
38 
42  public $invoice;
43 
47  function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  $this->invoice = new FactureFournisseur($this->db);
52  }
53 
64  function get($id)
65  {
66  if(! DolibarrApiAccess::$user->rights->fournisseur->facture->lire) {
67  throw new RestException(401);
68  }
69 
70  $result = $this->invoice->fetch($id);
71  if( ! $result ) {
72  throw new RestException(404, 'Supplier invoice not found');
73  }
74 
75  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->invoice->id,'facture_fourn','facture')) {
76  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77  }
78 
79  $this->invoice->fetchObjectLinked();
80  return $this->_cleanObjectDatas($this->invoice);
81  }
82 
99  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids='', $status='', $sqlfilters = '')
100  {
101  global $db, $conf;
102 
103  $obj_ret = array();
104 
105  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
106  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
107 
108  // If the internal user must only see his customers, force searching by him
109  $search_sale = 0;
110  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
111 
112  $sql = "SELECT t.rowid";
113  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $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)
114  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as t";
115 
116  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $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
117 
118  $sql.= ' WHERE t.entity IN ('.getEntity('supplier_invoice').')';
119  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
120  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
121  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
122 
123  // Filter by status
124  if ($status == 'draft') $sql.= " AND t.fk_statut IN (0)";
125  if ($status == 'unpaid') $sql.= " AND t.fk_statut IN (1)";
126  if ($status == 'paid') $sql.= " AND t.fk_statut IN (2)";
127  if ($status == 'cancelled') $sql.= " AND t.fk_statut IN (3)";
128  // Insert sale filter
129  if ($search_sale > 0)
130  {
131  $sql .= " AND sc.fk_user = ".$search_sale;
132  }
133  // Add sql filters
134  if ($sqlfilters)
135  {
136  if (! DolibarrApi::_checkFilters($sqlfilters))
137  {
138  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
139  }
140  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
141  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
142  }
143 
144  $sql.= $db->order($sortfield, $sortorder);
145  if ($limit) {
146  if ($page < 0)
147  {
148  $page = 0;
149  }
150  $offset = $limit * $page;
151 
152  $sql.= $db->plimit($limit + 1, $offset);
153  }
154 
155  $result = $db->query($sql);
156  if ($result) {
157  $i = 0;
158  $num = $db->num_rows($result);
159  $min = min($num, ($limit <= 0 ? $num : $limit));
160  while ($i < $min)
161  {
162  $obj = $db->fetch_object($result);
163  $invoice_static = new FactureFournisseur($db);
164  if($invoice_static->fetch($obj->rowid)) {
165  $obj_ret[] = $this->_cleanObjectDatas($invoice_static);
166  }
167  $i++;
168  }
169  }
170  else {
171  throw new RestException(503, 'Error when retrieve supplier invoice list : '.$db->lasterror());
172  }
173  if ( ! count($obj_ret)) {
174  throw new RestException(404, 'No supplier invoice found');
175  }
176  return $obj_ret;
177  }
178 
185  function post($request_data = null)
186  {
187  if(! DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
188  throw new RestException(401, "Insuffisant rights");
189  }
190  // Check mandatory fields
191  $result = $this->_validate($request_data);
192 
193  foreach($request_data as $field => $value) {
194  $this->invoice->$field = $value;
195  }
196  if(! array_keys($request_data,'date')) {
197  $this->invoice->date = dol_now();
198  }
199  /* We keep lines as an array
200  if (isset($request_data["lines"])) {
201  $lines = array();
202  foreach ($request_data["lines"] as $line) {
203  array_push($lines, (object) $line);
204  }
205  $this->invoice->lines = $lines;
206  }*/
207 
208  if ($this->invoice->create(DolibarrApiAccess::$user) < 0) {
209  throw new RestException(500, "Error creating order", array_merge(array($this->invoice->error), $this->invoice->errors));
210  }
211  return $this->invoice->id;
212  }
213 
221  function put($id, $request_data = null)
222  {
223  if(! DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
224  throw new RestException(401);
225  }
226 
227  $result = $this->invoice->fetch($id);
228  if( ! $result ) {
229  throw new RestException(404, 'Supplier invoice not found');
230  }
231 
232  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->invoice->id,'facture_fourn','facture')) {
233  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
234  }
235 
236  foreach($request_data as $field => $value) {
237  if ($field == 'id') continue;
238  $this->invoice->$field = $value;
239  }
240 
241  if($this->invoice->update($id, DolibarrApiAccess::$user))
242  return $this->get ($id);
243 
244  return false;
245  }
246 
253  function delete($id)
254  {
255  if(! DolibarrApiAccess::$user->rights->fournisseur->facture->supprimer) {
256  throw new RestException(401);
257  }
258  $result = $this->invoice->fetch($id);
259  if( ! $result ) {
260  throw new RestException(404, 'Supplier invoice not found');
261  }
262 
263  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->invoice->id,'facture_fourn','facture')) {
264  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
265  }
266 
267  if( $this->invoice->delete(DolibarrApiAccess::$user) < 0)
268  {
269  throw new RestException(500);
270  }
271 
272  return array(
273  'success' => array(
274  'code' => 200,
275  'message' => 'Supplier invoice deleted'
276  )
277  );
278  }
279 
280 
299  function validate($id, $idwarehouse=0, $notrigger=0)
300  {
301  if(! DolibarrApiAccess::$user->rights->fournisseur->facture->creer) {
302  throw new RestException(401);
303  }
304  $result = $this->invoice->fetch($id);
305  if( ! $result ) {
306  throw new RestException(404, 'Invoice not found');
307  }
308 
309  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->invoice->id,'facture_fourn','facture')) {
310  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
311  }
312 
313  $result = $this->invoice->validate(DolibarrApiAccess::$user, '', $idwarehouse, $notrigger);
314  if ($result == 0) {
315  throw new RestException(304, 'Error nothing done. May be object is already validated');
316  }
317  if ($result < 0) {
318  throw new RestException(500, 'Error when validating Invoice: '.$this->invoice->error);
319  }
320 
321  return array(
322  'success' => array(
323  'code' => 200,
324  'message' => 'Invoice validated (Ref='.$this->invoice->ref.')'
325  )
326  );
327  }
328 
335  function _cleanObjectDatas($object)
336  {
337 
338  $object = parent::_cleanObjectDatas($object);
339 
340  unset($object->rowid);
341  unset($object->barcode_type);
342  unset($object->barcode_type_code);
343  unset($object->barcode_type_label);
344  unset($object->barcode_type_coder);
345 
346  return $object;
347  }
348 
357  function _validate($data)
358  {
359  $invoice = array();
360  foreach (SupplierInvoices::$FIELDS as $field) {
361  if (!isset($data[$field]))
362  throw new RestException(400, "$field field missing");
363  $invoice[$field] = $data[$field];
364  }
365  return $invoice;
366  }
367 }
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create supplier invoice object.
Class to manage suppliers invoices.
_cleanObjectDatas($object)
Clean sensible object datas.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
put($id, $request_data=null)
Update supplier invoice.
dol_now($mode='gmt')
Return date for now.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check user access to a resource.
Definition: api.class.php:225