dolibarr  7.0.0-beta
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 
19 use Luracast\Restler\RestException;
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  global $db, $conf;
101 
102  $obj_ret = array();
103 
104  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
105  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
106 
107  // If the internal user must only see his customers, force searching by him
108  $search_sale = 0;
109  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
110 
111  $sql = "SELECT t.rowid";
112  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)
113  $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn as t";
114 
115  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
116 
117  $sql.= ' WHERE t.entity IN ('.getEntity('supplier_invoice').')';
118  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
119  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
120  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
121 
122  // Filter by status
123  if ($status == 'draft') $sql.= " AND t.fk_statut IN (0)";
124  if ($status == 'unpaid') $sql.= " AND t.fk_statut IN (1)";
125  if ($status == 'paid') $sql.= " AND t.fk_statut IN (2)";
126  if ($status == 'cancelled') $sql.= " AND t.fk_statut IN (3)";
127  // Insert sale filter
128  if ($search_sale > 0)
129  {
130  $sql .= " AND sc.fk_user = ".$search_sale;
131  }
132  // Add sql filters
133  if ($sqlfilters)
134  {
135  if (! DolibarrApi::_checkFilters($sqlfilters))
136  {
137  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
138  }
139  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
140  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
141  }
142 
143  $sql.= $db->order($sortfield, $sortorder);
144  if ($limit) {
145  if ($page < 0)
146  {
147  $page = 0;
148  }
149  $offset = $limit * $page;
150 
151  $sql.= $db->plimit($limit + 1, $offset);
152  }
153 
154  $result = $db->query($sql);
155  if ($result)
156  {
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  $object = parent::_cleanObjectDatas($object);
338 
339  unset($object->rowid);
340  unset($object->barcode_type);
341  unset($object->barcode_type_code);
342  unset($object->barcode_type_label);
343  unset($object->barcode_type_coder);
344 
345  return $object;
346  }
347 
356  function _validate($data)
357  {
358  $invoice = array();
359  foreach (SupplierInvoices::$FIELDS as $field) {
360  if (!isset($data[$field]))
361  throw new RestException(400, "$field field missing");
362  $invoice[$field] = $data[$field];
363  }
364  return $invoice;
365  }
366 }
post($request_data=NULL)
Create supplier invoice object.
_validate($data)
Validate fields before create or update object.
Class to manage suppliers invoices.
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=NULL)
Update supplier invoice.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
Class for API REST v1.
Definition: api.class.php:29
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
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:221