dolibarr  17.0.3
api_products.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-Fran├žois Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2019 Cedric Ancelin <icedo.anc@gmail.com>
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 <https://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
22 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
23 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
24 require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
25 require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
26 require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
28 
35 class Products extends DolibarrApi
36 {
40  public static $FIELDS = array(
41  'ref',
42  'label'
43  );
44 
48  public $product;
49 
53  public $productsupplier;
54 
58  public function __construct()
59  {
60  global $db, $conf;
61 
62  $this->db = $db;
63  $this->product = new Product($this->db);
64  $this->productsupplier = new ProductFournisseur($this->db);
65  }
66 
83  public function get($id, $includestockdata = 0, $includesubproducts = false, $includeparentid = false, $includetrans = false)
84  {
85  return $this->_fetch($id, '', '', '', $includestockdata, $includesubproducts, $includeparentid, false, $includetrans);
86  }
87 
107  public function getByRef($ref, $includestockdata = 0, $includesubproducts = false, $includeparentid = false, $includetrans = false)
108  {
109  return $this->_fetch('', $ref, '', '', $includestockdata, $includesubproducts, $includeparentid, false, $includetrans);
110  }
111 
131  public function getByRefExt($ref_ext, $includestockdata = 0, $includesubproducts = false, $includeparentid = false, $includetrans = false)
132  {
133  return $this->_fetch('', '', $ref_ext, '', $includestockdata, $includesubproducts, $includeparentid, false, $includetrans);
134  }
135 
155  public function getByBarcode($barcode, $includestockdata = 0, $includesubproducts = false, $includeparentid = false, $includetrans = false)
156  {
157  return $this->_fetch('', '', '', $barcode, $includestockdata, $includesubproducts, $includeparentid, false, $includetrans);
158  }
159 
178  public function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $ids_only = false, $variant_filter = 0, $pagination_data = false, $includestockdata = 0)
179  {
180  global $db, $conf;
181 
182  if (!DolibarrApiAccess::$user->rights->produit->lire) {
183  throw new RestException(403);
184  }
185 
186  $obj_ret = array();
187 
188  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
189 
190  $sql = "SELECT t.rowid, t.ref, t.ref_ext";
191  $sql .= " FROM ".$this->db->prefix()."product as t";
192  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_extrafields AS ef ON ef.fk_object = t.rowid"; // So we will be able to filter on extrafields
193  if ($category > 0) {
194  $sql .= ", ".$this->db->prefix()."categorie_product as c";
195  }
196  $sql .= ' WHERE t.entity IN ('.getEntity('product').')';
197 
198  if ($variant_filter == 1) {
199  $sql .= ' AND t.rowid not in (select distinct fk_product_parent from '.$this->db->prefix().'product_attribute_combination)';
200  $sql .= ' AND t.rowid not in (select distinct fk_product_child from '.$this->db->prefix().'product_attribute_combination)';
201  }
202  if ($variant_filter == 2) {
203  $sql .= ' AND t.rowid in (select distinct fk_product_parent from '.$this->db->prefix().'product_attribute_combination)';
204  }
205  if ($variant_filter == 3) {
206  $sql .= ' AND t.rowid in (select distinct fk_product_child from '.$this->db->prefix().'product_attribute_combination)';
207  }
208 
209  // Select products of given category
210  if ($category > 0) {
211  $sql .= " AND c.fk_categorie = ".((int) $category);
212  $sql .= " AND c.fk_product = t.rowid";
213  }
214  if ($mode == 1) {
215  // Show only products
216  $sql .= " AND t.fk_product_type = 0";
217  } elseif ($mode == 2) {
218  // Show only services
219  $sql .= " AND t.fk_product_type = 1";
220  }
221 
222  // Add sql filters
223  if ($sqlfilters) {
224  $errormessage = '';
225  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
226  if ($errormessage) {
227  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
228  }
229  }
230 
231  //this query will return total products with the filters given
232  $sqlTotals = str_replace('SELECT t.rowid, t.ref, t.ref_ext', 'SELECT count(t.rowid) as total', $sql);
233 
234  $sql .= $this->db->order($sortfield, $sortorder);
235  if ($limit) {
236  if ($page < 0) {
237  $page = 0;
238  }
239  $offset = $limit * $page;
240 
241  $sql .= $this->db->plimit($limit + 1, $offset);
242  }
243 
244  $result = $this->db->query($sql);
245  if ($result) {
246  $num = $this->db->num_rows($result);
247  $min = min($num, ($limit <= 0 ? $num : $limit));
248  $i = 0;
249  while ($i < $min) {
250  $obj = $this->db->fetch_object($result);
251  if (!$ids_only) {
252  $product_static = new Product($this->db);
253  if ($product_static->fetch($obj->rowid)) {
254  if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
255  $product_static->load_stock();
256 
257  if (is_array($product_static->stock_warehouse)) {
258  foreach ($product_static->stock_warehouse as $keytmp => $valtmp) {
259  if (isset($product_static->stock_warehouse[$keytmp]->detail_batch) && is_array($product_static->stock_warehouse[$keytmp]->detail_batch)) {
260  foreach ($product_static->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
261  unset($product_static->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
262  }
263  }
264  }
265  }
266  }
267 
268 
269  $obj_ret[] = $this->_cleanObjectDatas($product_static);
270  }
271  } else {
272  $obj_ret[] = $obj->rowid;
273  }
274  $i++;
275  }
276  } else {
277  throw new RestException(503, 'Error when retrieve product list : '.$this->db->lasterror());
278  }
279  if (!count($obj_ret)) {
280  throw new RestException(404, 'No product found');
281  }
282 
283  //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
284  if ($pagination_data) {
285  $totalsResult = $this->db->query($sqlTotals);
286  $total = $this->db->fetch_object($totalsResult)->total;
287 
288  $tmp = $obj_ret;
289  $obj_ret = array();
290 
291  $obj_ret['data'] = $tmp;
292  $obj_ret['pagination'] = array(
293  'total' => (int) $total,
294  'page' => $page, //count starts from 0
295  'page_count' => ceil((int) $total/$limit),
296  'limit' => $limit
297  );
298  }
299 
300  return $obj_ret;
301  }
302 
309  public function post($request_data = null)
310  {
311  if (!DolibarrApiAccess::$user->rights->produit->creer) {
312  throw new RestException(401);
313  }
314  // Check mandatory fields
315  $result = $this->_validate($request_data);
316 
317  foreach ($request_data as $field => $value) {
318  $this->product->$field = $value;
319  }
320  if ($this->product->create(DolibarrApiAccess::$user) < 0) {
321  throw new RestException(500, "Error creating product", array_merge(array($this->product->error), $this->product->errors));
322  }
323 
324  return $this->product->id;
325  }
326 
338  public function put($id, $request_data = null)
339  {
340  global $conf;
341 
342  if (!DolibarrApiAccess::$user->rights->produit->creer) {
343  throw new RestException(401);
344  }
345 
346  $result = $this->product->fetch($id);
347  if (!$result) {
348  throw new RestException(404, 'Product not found');
349  }
350 
351  if (!DolibarrApi::_checkAccessToResource('product', $this->product->id)) {
352  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
353  }
354 
355  $oldproduct = dol_clone($this->product);
356 
357  foreach ($request_data as $field => $value) {
358  if ($field == 'id') {
359  continue;
360  }
361  if ($field == 'stock_reel') {
362  throw new RestException(400, 'Stock reel cannot be updated here. Use the /stockmovements endpoint instead');
363  }
364  $this->product->$field = $value;
365  }
366 
367  $updatetype = false;
368  if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
369  $updatetype = true;
370  }
371 
372  $result = $this->product->update($id, DolibarrApiAccess::$user, 1, 'update', $updatetype);
373 
374  // If price mode is 1 price per product
375  if ($result > 0 && !empty($conf->global->PRODUCT_PRICE_UNIQ)) {
376  // We update price only if it was changed
377  $pricemodified = false;
378  if ($this->product->price_base_type != $oldproduct->price_base_type) {
379  $pricemodified = true;
380  } else {
381  if ($this->product->tva_tx != $oldproduct->tva_tx) {
382  $pricemodified = true;
383  }
384  if ($this->product->tva_npr != $oldproduct->tva_npr) {
385  $pricemodified = true;
386  }
387  if ($this->product->default_vat_code != $oldproduct->default_vat_code) {
388  $pricemodified = true;
389  }
390 
391  if ($this->product->price_base_type == 'TTC') {
392  if ($this->product->price_ttc != $oldproduct->price_ttc) {
393  $pricemodified = true;
394  }
395  if ($this->product->price_min_ttc != $oldproduct->price_min_ttc) {
396  $pricemodified = true;
397  }
398  } else {
399  if ($this->product->price != $oldproduct->price) {
400  $pricemodified = true;
401  }
402  if ($this->product->price_min != $oldproduct->price_min) {
403  $pricemodified = true;
404  }
405  }
406  }
407 
408  if ($pricemodified) {
409  $newvat = $this->product->tva_tx;
410  $newnpr = $this->product->tva_npr;
411  $newvatsrccode = $this->product->default_vat_code;
412 
413  $newprice = $this->product->price;
414  $newpricemin = $this->product->price_min;
415  if ($this->product->price_base_type == 'TTC') {
416  $newprice = $this->product->price_ttc;
417  $newpricemin = $this->product->price_min_ttc;
418  }
419 
420  $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode);
421  }
422  }
423 
424  if ($result <= 0) {
425  throw new RestException(500, "Error updating product", array_merge(array($this->product->error), $this->product->errors));
426  }
427 
428  return $this->get($id);
429  }
430 
437  public function delete($id)
438  {
439  if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
440  throw new RestException(401);
441  }
442  $result = $this->product->fetch($id);
443  if (!$result) {
444  throw new RestException(404, 'Product not found');
445  }
446 
447  if (!DolibarrApi::_checkAccessToResource('product', $this->product->id)) {
448  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
449  }
450 
451  // The Product::delete() method uses the global variable $user.
452  global $user;
453  $user = DolibarrApiAccess::$user;
454 
455  $res = $this->product->delete(DolibarrApiAccess::$user);
456  if ($res < 0) {
457  throw new RestException(500, "Can't delete, error occurs");
458  } elseif ($res == 0) {
459  throw new RestException(409, "Can't delete, that product is probably used");
460  }
461 
462  return array(
463  'success' => array(
464  'code' => 200,
465  'message' => 'Object deleted'
466  )
467  );
468  }
469 
482  public function getSubproducts($id)
483  {
484  if (!DolibarrApiAccess::$user->rights->produit->lire) {
485  throw new RestException(401);
486  }
487 
488  if (!DolibarrApi::_checkAccessToResource('product', $id)) {
489  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
490  }
491 
492  $childsArbo = $this->product->getChildsArbo($id, 1);
493 
494  $keys = array('rowid', 'qty', 'fk_product_type', 'label', 'incdec', 'ref', 'fk_association', 'rang');
495  $childs = array();
496  foreach ($childsArbo as $values) {
497  $childs[] = array_combine($keys, $values);
498  }
499 
500  return $childs;
501  }
502 
520  public function addSubproducts($id, $subproduct_id, $qty, $incdec = 1)
521  {
522  if (!DolibarrApiAccess::$user->rights->produit->creer) {
523  throw new RestException(401);
524  }
525 
526  if (!DolibarrApi::_checkAccessToResource('product', $id)) {
527  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
528  }
529 
530  $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
531  if ($result <= 0) {
532  throw new RestException(500, "Error adding product child");
533  }
534  return $result;
535  }
536 
550  public function delSubproducts($id, $subproduct_id)
551  {
552  if (!DolibarrApiAccess::$user->rights->produit->creer) {
553  throw new RestException(401);
554  }
555 
556  if (!DolibarrApi::_checkAccessToResource('product', $id)) {
557  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
558  }
559 
560  $result = $this->product->del_sousproduit($id, $subproduct_id);
561  if ($result <= 0) {
562  throw new RestException(500, "Error while removing product child");
563  }
564  return $result;
565  }
566 
567 
581  public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
582  {
583  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
584  throw new RestException(401);
585  }
586 
587  $categories = new Categorie($this->db);
588 
589  $result = $categories->getListForItem($id, 'product', $sortfield, $sortorder, $limit, $page);
590 
591  if (empty($result)) {
592  throw new RestException(404, 'No category found');
593  }
594 
595  if ($result < 0) {
596  throw new RestException(503, 'Error when retrieve category list : '.join(',', array_merge(array($categories->error), $categories->errors)));
597  }
598 
599  return $result;
600  }
601 
611  public function getCustomerPricesPerSegment($id)
612  {
613  global $conf;
614 
615  if (!DolibarrApiAccess::$user->rights->produit->lire) {
616  throw new RestException(401);
617  }
618 
619  if (empty($conf->global->PRODUIT_MULTIPRICES)) {
620  throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup');
621  }
622 
623  $result = $this->product->fetch($id);
624  if (!$result) {
625  throw new RestException(404, 'Product not found');
626  }
627 
628  if ($result < 0) {
629  throw new RestException(503, 'Error when retrieve prices list : '.join(',', array_merge(array($this->product->error), $this->product->errors)));
630  }
631 
632  return array(
633  'multiprices'=>$this->product->multiprices,
634  'multiprices_inc_tax'=>$this->product->multiprices_ttc,
635  'multiprices_min'=>$this->product->multiprices_min,
636  'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
637  'multiprices_vat'=>$this->product->multiprices_tva_tx,
638  'multiprices_base_type'=>$this->product->multiprices_base_type,
639  //'multiprices_default_vat_code'=>$this->product->multiprices_default_vat_code
640  );
641  }
642 
653  public function getCustomerPricesPerCustomer($id, $thirdparty_id = '')
654  {
655  global $conf;
656 
657  if (!DolibarrApiAccess::$user->rights->produit->lire) {
658  throw new RestException(401);
659  }
660 
661  if (empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
662  throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup');
663  }
664 
665  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
666  if ($socid > 0 && $socid != $thirdparty_id) {
667  throw new RestException(401, 'Getting prices for all customers or for the customer ID '.$thirdparty_id.' is not allowed for login '.DolibarrApiAccess::$user->login);
668  }
669 
670  $result = $this->product->fetch($id);
671  if (!$result) {
672  throw new RestException(404, 'Product not found');
673  }
674 
675  if ($result > 0) {
676  require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php';
677  $prodcustprice = new Productcustomerprice($this->db);
678  $filter = array();
679  $filter['t.fk_product'] = $id;
680  if ($thirdparty_id) {
681  $filter['t.fk_soc'] = $thirdparty_id;
682  }
683  $result = $prodcustprice->fetchAll('', '', 0, 0, $filter);
684  }
685 
686  if (empty($prodcustprice->lines)) {
687  throw new RestException(404, 'Prices not found');
688  }
689 
690  return $prodcustprice->lines;
691  }
692 
702  public function getCustomerPricesPerQuantity($id)
703  {
704  global $conf;
705 
706  if (!DolibarrApiAccess::$user->rights->produit->lire) {
707  throw new RestException(401);
708  }
709 
710  if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
711  throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup');
712  }
713 
714  $result = $this->product->fetch($id);
715  if (!$result) {
716  throw new RestException(404, 'Product not found');
717  }
718 
719  if ($result < 0) {
720  throw new RestException(503, 'Error when retrieve prices list : '.join(',', array_merge(array($this->product->error), $this->product->errors)));
721  }
722 
723  return array(
724  'prices_by_qty'=>$this->product->prices_by_qty[0], // 1 if price by quantity was activated for the product
725  'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
726  );
727  }
728 
762  public function addPurchasePrice($id, $qty, $buyprice, $price_base_type, $fourn_id, $availability, $ref_fourn, $tva_tx, $charges = 0, $remise_percent = 0, $remise = 0, $newnpr = 0, $delivery_time_days = 0, $supplier_reputation = '', $localtaxes_array = array(), $newdefaultvatcode = '', $multicurrency_buyprice = 0, $multicurrency_price_base_type = 'HT', $multicurrency_tx = 1, $multicurrency_code = '', $desc_fourn = '', $barcode = '', $fk_barcode_type = null)
763  {
764  if (!DolibarrApiAccess::$user->rights->produit->creer) {
765  throw new RestException(401);
766  }
767 
768  $result = $this->productsupplier->fetch($id);
769  if (!$result) {
770  throw new RestException(404, 'Product not found');
771  }
772 
773  if (!DolibarrApi::_checkAccessToResource('product', $this->productsupplier->id)) {
774  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
775  }
776 
777  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
778  if ($socid > 0 && $socid != $fourn_id) {
779  throw new RestException(401, 'Adding purchase price for the supplier ID '.$fourn_id.' is not allowed for login '.DolibarrApiAccess::$user->login);
780  }
781 
782  $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty);
783  if ($result < 0) {
784  throw new RestException(500, "Error adding supplier to product : ".$this->db->lasterror());
785  }
786 
787  $fourn = new Fournisseur($this->db);
788  $result = $fourn->fetch($fourn_id);
789  if ($result <= 0) {
790  throw new RestException(404, 'Supplier not found');
791  }
792 
793  // Clean data
794  $ref_fourn = sanitizeVal($ref_fourn, 'alphanohtml');
795  $desc_fourn = sanitizeVal($desc_fourn, 'restricthtml');
796  $barcode = sanitizeVal($barcode, 'alphanohtml');
797 
798  $result = $this->productsupplier->update_buyprice($qty, $buyprice, DolibarrApiAccess::$user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges, $remise_percent, $remise, $newnpr, $delivery_time_days, $supplier_reputation, $localtaxes_array, $newdefaultvatcode, $multicurrency_buyprice, $multicurrency_price_base_type, $multicurrency_tx, $multicurrency_code, $desc_fourn, $barcode, $fk_barcode_type);
799 
800  if ($result <= 0) {
801  throw new RestException(500, "Error updating buy price : ".$this->db->lasterror());
802  }
803  return (int) $this->productsupplier->product_fourn_price_id;
804  }
805 
820  public function deletePurchasePrice($id, $priceid)
821  {
822  if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
823  throw new RestException(401);
824  }
825  $result = $this->productsupplier->fetch($id);
826  if (!$result) {
827  throw new RestException(404, 'Product not found');
828  }
829 
830  if (!DolibarrApi::_checkAccessToResource('product', $this->productsupplier->id)) {
831  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
832  }
833 
834  $resultsupplier = 0;
835  if ($result > 0) {
836  $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid);
837  }
838 
839  return $resultsupplier;
840  }
841 
857  public function getSupplierProducts($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters = '')
858  {
859  global $db, $conf;
860 
861  if (!DolibarrApiAccess::$user->rights->produit->lire) {
862  throw new RestException(401);
863  }
864 
865  $obj_ret = array();
866 
867  // Force id of company for external users
868  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
869  if ($socid > 0) {
870  if ($supplier != $socid || empty($supplier)) {
871  throw new RestException(401, 'As an external user, you can request only for your supplier id = '.$socid);
872  }
873  }
874 
875  $sql = "SELECT t.rowid, t.ref, t.ref_ext";
876  $sql .= " FROM ".$this->db->prefix()."product as t";
877  if ($category > 0) {
878  $sql .= ", ".$this->db->prefix()."categorie_product as c";
879  }
880  $sql .= ", ".$this->db->prefix()."product_fournisseur_price as s";
881 
882  $sql .= ' WHERE t.entity IN ('.getEntity('product').')';
883 
884  if ($supplier > 0) {
885  $sql .= " AND s.fk_soc = ".((int) $supplier);
886  }
887  if ($socid > 0) { // if external user
888  $sql .= " AND s.fk_soc = ".((int) $socid);
889  }
890  $sql .= " AND s.fk_product = t.rowid";
891  // Select products of given category
892  if ($category > 0) {
893  $sql .= " AND c.fk_categorie = ".((int) $category);
894  $sql .= " AND c.fk_product = t.rowid";
895  }
896  if ($mode == 1) {
897  // Show only products
898  $sql .= " AND t.fk_product_type = 0";
899  } elseif ($mode == 2) {
900  // Show only services
901  $sql .= " AND t.fk_product_type = 1";
902  }
903  // Add sql filters
904  if ($sqlfilters) {
905  $errormessage = '';
906  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
907  if ($errormessage) {
908  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
909  }
910  }
911 
912  $sql .= $this->db->order($sortfield, $sortorder);
913  if ($limit) {
914  if ($page < 0) {
915  $page = 0;
916  }
917  $offset = $limit * $page;
918  $sql .= $this->db->plimit($limit + 1, $offset);
919  }
920  $result = $this->db->query($sql);
921  if ($result) {
922  $num = $this->db->num_rows($result);
923  $min = min($num, ($limit <= 0 ? $num : $limit));
924  $i = 0;
925  while ($i < $min) {
926  $obj = $this->db->fetch_object($result);
927 
928  $product_fourn = new ProductFournisseur($this->db);
929  $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid, '', '', 0, 0);
930  foreach ($product_fourn_list as $tmpobj) {
931  $this->_cleanObjectDatas($tmpobj);
932  }
933 
934  //var_dump($product_fourn_list->db);exit;
935  $obj_ret[$obj->rowid] = $product_fourn_list;
936 
937  $i++;
938  }
939  } else {
940  throw new RestException(503, 'Error when retrieve product list : '.$this->db->lasterror());
941  }
942  if (!count($obj_ret)) {
943  throw new RestException(404, 'No product found');
944  }
945  return $obj_ret;
946  }
947 
967  public function getPurchasePrices($id, $ref = '', $ref_ext = '', $barcode = '')
968  {
969  if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
970  throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode');
971  }
972 
973  $id = (empty($id) ? 0 : $id);
974 
975  if (!DolibarrApiAccess::$user->rights->produit->lire) {
976  throw new RestException(403);
977  }
978 
979  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
980 
981  $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
982  if (!$result) {
983  throw new RestException(404, 'Product not found');
984  }
985 
986  if (!DolibarrApi::_checkAccessToResource('product', $this->product->id)) {
987  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
988  }
989 
990  $product_fourn_list = array();
991 
992  if ($result) {
993  $product_fourn = new ProductFournisseur($this->db);
994  $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id, '', '', 0, 0, ($socid > 0 ? $socid : 0));
995  }
996 
997  foreach ($product_fourn_list as $tmpobj) {
998  $this->_cleanObjectDatas($tmpobj);
999  }
1000 
1001  return $this->_cleanObjectDatas($product_fourn_list);
1002  }
1003 
1020  public function getAttributes($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
1021  {
1022  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1023  throw new RestException(401);
1024  }
1025 
1026  $sql = "SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1027  $sql .= " FROM ".$this->db->prefix()."product_attribute as t";
1028  $sql .= ' WHERE t.entity IN ('.getEntity('product').')';
1029 
1030  // Add sql filters
1031  if ($sqlfilters) {
1032  $errormessage = '';
1033  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
1034  if ($errormessage) {
1035  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
1036  }
1037  }
1038 
1039  $sql .= $this->db->order($sortfield, $sortorder);
1040  if ($limit) {
1041  if ($page < 0) {
1042  $page = 0;
1043  }
1044  $offset = $limit * $page;
1045 
1046  $sql .= $this->db->plimit($limit, $offset);
1047  }
1048 
1049  $result = $this->db->query($sql);
1050 
1051  if (!$result) {
1052  throw new RestException(503, 'Error when retrieve product attribute list : '.$this->db->lasterror());
1053  }
1054 
1055  $return = array();
1056  while ($result = $this->db->fetch_object($query)) {
1057  $tmp = new ProductAttribute($this->db);
1058  $tmp->id = $result->rowid;
1059  $tmp->ref = $result->ref;
1060  $tmp->ref_ext = $result->ref_ext;
1061  $tmp->label = $result->label;
1062  $tmp->position = $result->position;
1063  $tmp->entity = $result->entity;
1064 
1065  $return[] = $this->_cleanObjectDatas($tmp);
1066  }
1067 
1068  if (!count($return)) {
1069  throw new RestException(404, 'No product attribute found');
1070  }
1071 
1072  return $return;
1073  }
1074 
1086  public function getAttributeById($id)
1087  {
1088  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1089  throw new RestException(401);
1090  }
1091 
1092  $prodattr = new ProductAttribute($this->db);
1093  $result = $prodattr->fetch((int) $id);
1094 
1095  if ($result < 0) {
1096  throw new RestException(404, "Product attribute not found");
1097  }
1098 
1099  $fields = ["id", "ref", "ref_ext", "label", "position", "entity"];
1100 
1101  foreach ($prodattr as $field => $value) {
1102  if (!in_array($field, $fields)) {
1103  unset($prodattr->{$field});
1104  }
1105  }
1106 
1107  $sql = "SELECT COUNT(*) as nb FROM ".$this->db->prefix()."product_attribute_combination2val as pac2v";
1108  $sql .= " JOIN ".$this->db->prefix()."product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1109  $sql .= " WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id)." AND pac.entity IN (".getEntity('product').")";
1110 
1111  $resql = $this->db->query($sql);
1112  $obj = $this->db->fetch_object($resql);
1113  $prodattr->is_used_by_products = (int) $obj->nb;
1114 
1115  return $prodattr;
1116  }
1117 
1129  public function getAttributesByRef($ref)
1130  {
1131  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1132  throw new RestException(401);
1133  }
1134 
1135  $ref = trim($ref);
1136 
1137  $sql = "SELECT rowid, ref, ref_ext, label, position, entity FROM ".$this->db->prefix()."product_attribute WHERE ref LIKE '".$this->db->escape($ref)."' AND entity IN (".getEntity('product').")";
1138 
1139  $query = $this->db->query($sql);
1140 
1141  if (!$this->db->num_rows($query)) {
1142  throw new RestException(404);
1143  }
1144 
1145  $result = $this->db->fetch_object($query);
1146 
1147  $attr = array();
1148  $attr['id'] = $result->rowid;
1149  $attr['ref'] = $result->ref;
1150  $attr['ref_ext'] = $result->ref_ext;
1151  $attr['label'] = $result->label;
1152  $attr['rang'] = $result->position;
1153  $attr['position'] = $result->position;
1154  $attr['entity'] = $result->entity;
1155 
1156  $sql = "SELECT COUNT(*) as nb FROM ".$this->db->prefix()."product_attribute_combination2val as pac2v";
1157  $sql .= " JOIN ".$this->db->prefix()."product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1158  $sql .= " WHERE pac2v.fk_prod_attr = ".((int) $result->rowid)." AND pac.entity IN (".getEntity('product').")";
1159 
1160  $resql = $this->db->query($sql);
1161  $obj = $this->db->fetch_object($resql);
1162 
1163  $attr["is_used_by_products"] = (int) $obj->nb;
1164 
1165  return $attr;
1166  }
1167 
1179  public function getAttributesByRefExt($ref_ext)
1180  {
1181  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1182  throw new RestException(401);
1183  }
1184 
1185  $ref_ext = trim($ref_ext);
1186 
1187  $sql = "SELECT rowid, ref, ref_ext, label, position, entity FROM ".$this->db->prefix()."product_attribute WHERE ref_ext LIKE '".$this->db->escape($ref_ext)."' AND entity IN (".getEntity('product').")";
1188 
1189  $query = $this->db->query($sql);
1190 
1191  if (!$this->db->num_rows($query)) {
1192  throw new RestException(404);
1193  }
1194 
1195  $result = $this->db->fetch_object($query);
1196 
1197  $attr = array();
1198  $attr['id'] = $result->rowid;
1199  $attr['ref'] = $result->ref;
1200  $attr['ref_ext'] = $result->ref_ext;
1201  $attr['label'] = $result->label;
1202  $attr['rang'] = $result->position;
1203  $attr['position'] = $result->position;
1204  $attr['entity'] = $result->entity;
1205 
1206  $sql = "SELECT COUNT(*) as nb FROM ".$this->db->prefix()."product_attribute_combination2val as pac2v";
1207  $sql .= " JOIN ".$this->db->prefix()."product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1208  $sql .= " WHERE pac2v.fk_prod_attr = ".((int) $result->rowid)." AND pac.entity IN (".getEntity('product').")";
1209 
1210  $resql = $this->db->query($sql);
1211  $obj = $this->db->fetch_object($resql);
1212  $attr["is_used_by_products"] = (int) $obj->nb;
1213 
1214  return $attr;
1215  }
1216 
1230  public function addAttributes($ref, $label, $ref_ext = '')
1231  {
1232  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1233  throw new RestException(401);
1234  }
1235 
1236  $prodattr = new ProductAttribute($this->db);
1237  $prodattr->label = $label;
1238  $prodattr->ref = $ref;
1239  $prodattr->ref_ext = $ref_ext;
1240 
1241  $resid = $prodattr->create(DolibarrApiAccess::$user);
1242  if ($resid <= 0) {
1243  throw new RestException(500, "Error creating new attribute");
1244  }
1245  return $resid;
1246  }
1247 
1261  public function putAttributes($id, $request_data = null)
1262  {
1263  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1264  throw new RestException(401);
1265  }
1266 
1267  $prodattr = new ProductAttribute($this->db);
1268 
1269  $result = $prodattr->fetch((int) $id);
1270  if ($result == 0) {
1271  throw new RestException(404, 'Attribute not found');
1272  } elseif ($result < 0) {
1273  throw new RestException(500, "Error fetching attribute");
1274  }
1275 
1276  foreach ($request_data as $field => $value) {
1277  if ($field == 'rowid') {
1278  continue;
1279  }
1280  $prodattr->$field = $value;
1281  }
1282 
1283  if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1284  $result = $prodattr->fetch((int) $id);
1285  if ($result == 0) {
1286  throw new RestException(404, 'Attribute not found');
1287  } elseif ($result < 0) {
1288  throw new RestException(500, "Error fetching attribute");
1289  } else {
1290  return $prodattr;
1291  }
1292  }
1293  throw new RestException(500, "Error updating attribute");
1294  }
1295 
1307  public function deleteAttributes($id)
1308  {
1309  if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1310  throw new RestException(401);
1311  }
1312 
1313  $prodattr = new ProductAttribute($this->db);
1314  $prodattr->id = (int) $id;
1315  $result = $prodattr->delete(DolibarrApiAccess::$user);
1316 
1317  if ($result <= 0) {
1318  throw new RestException(500, "Error deleting attribute");
1319  }
1320 
1321  return $result;
1322  }
1323 
1335  public function getAttributeValueById($id)
1336  {
1337  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1338  throw new RestException(401);
1339  }
1340 
1341  $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix()."product_attribute_value WHERE rowid = ".(int) $id." AND entity IN (".getEntity('product').")";
1342 
1343  $query = $this->db->query($sql);
1344 
1345  if (!$query) {
1346  throw new RestException(401);
1347  }
1348 
1349  if (!$this->db->num_rows($query)) {
1350  throw new RestException(404, 'Attribute value not found');
1351  }
1352 
1353  $result = $this->db->fetch_object($query);
1354 
1355  $attrval = array();
1356  $attrval['id'] = $result->rowid;
1357  $attrval['fk_product_attribute'] = $result->fk_product_attribute;
1358  $attrval['ref'] = $result->ref;
1359  $attrval['value'] = $result->value;
1360 
1361  return $attrval;
1362  }
1363 
1376  public function getAttributeValueByRef($id, $ref)
1377  {
1378  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1379  throw new RestException(401);
1380  }
1381 
1382  $ref = trim($ref);
1383 
1384  $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix()."product_attribute_value";
1385  $sql .= " WHERE ref LIKE '".$this->db->escape($ref)."' AND fk_product_attribute = ".((int) $id)." AND entity IN (".getEntity('product').")";
1386 
1387  $query = $this->db->query($sql);
1388 
1389  if (!$query) {
1390  throw new RestException(401);
1391  }
1392 
1393  if (!$this->db->num_rows($query)) {
1394  throw new RestException(404, 'Attribute value not found');
1395  }
1396 
1397  $result = $this->db->fetch_object($query);
1398 
1399  $attrval = array();
1400  $attrval['id'] = $result->rowid;
1401  $attrval['fk_product_attribute'] = $result->fk_product_attribute;
1402  $attrval['ref'] = $result->ref;
1403  $attrval['value'] = $result->value;
1404 
1405  return $attrval;
1406  }
1407 
1419  public function deleteAttributeValueByRef($id, $ref)
1420  {
1421  if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1422  throw new RestException(401);
1423  }
1424 
1425  $ref = trim($ref);
1426 
1427  $sql = "SELECT rowid FROM ".$this->db->prefix()."product_attribute_value";
1428  $sql .= " WHERE ref LIKE '".$this->db->escape($ref)."' AND fk_product_attribute = ".((int) $id)." AND entity IN (".getEntity('product').")";
1429  $query = $this->db->query($sql);
1430 
1431  if (!$query) {
1432  throw new RestException(401);
1433  }
1434 
1435  if (!$this->db->num_rows($query)) {
1436  throw new RestException(404, 'Attribute value not found');
1437  }
1438 
1439  $result = $this->db->fetch_object($query);
1440 
1441  $attrval = new ProductAttributeValue($this->db);
1442  $attrval->id = $result->rowid;
1443  $result = $attrval->delete(DolibarrApiAccess::$user);
1444  if ($result > 0) {
1445  return 1;
1446  }
1447 
1448  throw new RestException(500, "Error deleting attribute value");
1449  }
1450 
1462  public function getAttributeValues($id)
1463  {
1464  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1465  throw new RestException(401);
1466  }
1467 
1468  $objectval = new ProductAttributeValue($this->db);
1469 
1470  $return = $objectval->fetchAllByProductAttribute((int) $id);
1471 
1472  if (count($return) == 0) {
1473  throw new RestException(404, 'Attribute values not found');
1474  }
1475 
1476  foreach ($return as $key => $val) {
1477  $return[$key] = $this->_cleanObjectDatas($return[$key]);
1478  }
1479 
1480  return $return;
1481  }
1482 
1493  public function getAttributeValuesByRef($ref)
1494  {
1495  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1496  throw new RestException(401);
1497  }
1498 
1499  $ref = trim($ref);
1500 
1501  $return = array();
1502 
1503  $sql = "SELECT ";
1504  $sql .= "v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix()."product_attribute_value as v";
1505  $sql .= " WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix()."product_attribute WHERE ref LIKE '".$this->db->escape($ref)."')";
1506 
1507  $resql = $this->db->query($sql);
1508 
1509  while ($result = $this->db->fetch_object($resql)) {
1510  $tmp = new ProductAttributeValue($this->db);
1511  $tmp->fk_product_attribute = $result->fk_product_attribute;
1512  $tmp->id = $result->rowid;
1513  $tmp->ref = $result->ref;
1514  $tmp->value = $result->value;
1515 
1516  $return[] = $this->_cleanObjectDatas($tmp);
1517  }
1518 
1519  return $return;
1520  }
1521 
1535  public function addAttributeValue($id, $ref, $value)
1536  {
1537  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1538  throw new RestException(401);
1539  }
1540 
1541  if (empty($ref) || empty($value)) {
1542  throw new RestException(401);
1543  }
1544 
1545  $objectval = new ProductAttributeValue($this->db);
1546  $objectval->fk_product_attribute = ((int) $id);
1547  $objectval->ref = $ref;
1548  $objectval->value = $value;
1549 
1550  if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1551  return $objectval->id;
1552  }
1553  throw new RestException(500, "Error creating new attribute value");
1554  }
1555 
1568  public function putAttributeValue($id, $request_data)
1569  {
1570  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1571  throw new RestException(401);
1572  }
1573 
1574  $objectval = new ProductAttributeValue($this->db);
1575  $result = $objectval->fetch((int) $id);
1576 
1577  if ($result == 0) {
1578  throw new RestException(404, 'Attribute value not found');
1579  } elseif ($result < 0) {
1580  throw new RestException(500, "Error fetching attribute value");
1581  }
1582 
1583  foreach ($request_data as $field => $value) {
1584  if ($field == 'rowid') {
1585  continue;
1586  }
1587  $objectval->$field = $value;
1588  }
1589 
1590  if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1591  $result = $objectval->fetch((int) $id);
1592  if ($result == 0) {
1593  throw new RestException(404, 'Attribute not found');
1594  } elseif ($result < 0) {
1595  throw new RestException(500, "Error fetching attribute");
1596  } else {
1597  return $objectval;
1598  }
1599  }
1600  throw new RestException(500, "Error updating attribute");
1601  }
1602 
1614  public function deleteAttributeValueById($id)
1615  {
1616  if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1617  throw new RestException(401);
1618  }
1619 
1620  $objectval = new ProductAttributeValue($this->db);
1621  $objectval->id = (int) $id;
1622 
1623  if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1624  return 1;
1625  }
1626  throw new RestException(500, "Error deleting attribute value");
1627  }
1628 
1641  public function getVariants($id, $includestock = 0)
1642  {
1643  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1644  throw new RestException(401);
1645  }
1646 
1647  $prodcomb = new ProductCombination($this->db);
1648  $combinations = $prodcomb->fetchAllByFkProductParent((int) $id);
1649 
1650  foreach ($combinations as $key => $combination) {
1651  $prodc2vp = new ProductCombination2ValuePair($this->db);
1652  $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id);
1653  $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1654 
1655  if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1656  $productModel = new Product($this->db);
1657  $productModel->fetch((int) $combination->fk_product_child);
1658  $productModel->load_stock($includestock);
1659  $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1660  }
1661  }
1662 
1663  return $combinations;
1664  }
1665 
1677  public function getVariantsByProdRef($ref)
1678  {
1679  if (!DolibarrApiAccess::$user->rights->produit->lire) {
1680  throw new RestException(401);
1681  }
1682 
1683  $result = $this->product->fetch('', $ref);
1684  if (!$result) {
1685  throw new RestException(404, 'Product not found');
1686  }
1687 
1688  $prodcomb = new ProductCombination($this->db);
1689  $combinations = $prodcomb->fetchAllByFkProductParent((int) $this->product->id);
1690 
1691  foreach ($combinations as $key => $combination) {
1692  $prodc2vp = new ProductCombination2ValuePair($this->db);
1693  $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id);
1694  $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1695  }
1696 
1697  return $combinations;
1698  }
1699 
1720  public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference = '', $ref_ext = '')
1721  {
1722  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1723  throw new RestException(401);
1724  }
1725 
1726  if (empty($id) || empty($features) || !is_array($features)) {
1727  throw new RestException(401);
1728  }
1729 
1730  $weight_impact = price2num($weight_impact);
1731  $price_impact = price2num($price_impact);
1732 
1733  $prodattr = new ProductAttribute($this->db);
1734  $prodattr_val = new ProductAttributeValue($this->db);
1735  foreach ($features as $id_attr => $id_value) {
1736  if ($prodattr->fetch((int) $id_attr) < 0) {
1737  throw new RestException(401);
1738  }
1739  if ($prodattr_val->fetch((int) $id_value) < 0) {
1740  throw new RestException(401);
1741  }
1742  }
1743 
1744  $result = $this->product->fetch((int) $id);
1745  if (!$result) {
1746  throw new RestException(404, 'Product not found');
1747  }
1748 
1749  $prodcomb = new ProductCombination($this->db);
1750 
1751  $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1752  if ($result > 0) {
1753  return $result;
1754  } else {
1755  throw new RestException(500, "Error creating new product variant");
1756  }
1757  }
1758 
1777  public function addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features)
1778  {
1779  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1780  throw new RestException(401);
1781  }
1782 
1783  if (empty($ref) || empty($features) || !is_array($features)) {
1784  throw new RestException(401);
1785  }
1786 
1787  $weight_impact = price2num($weight_impact);
1788  $price_impact = price2num($price_impact);
1789 
1790  $prodattr = new ProductAttribute($this->db);
1791  $prodattr_val = new ProductAttributeValue($this->db);
1792  foreach ($features as $id_attr => $id_value) {
1793  if ($prodattr->fetch((int) $id_attr) < 0) {
1794  throw new RestException(404);
1795  }
1796  if ($prodattr_val->fetch((int) $id_value) < 0) {
1797  throw new RestException(404);
1798  }
1799  }
1800 
1801  $result = $this->product->fetch('', trim($ref));
1802  if (!$result) {
1803  throw new RestException(404, 'Product not found');
1804  }
1805 
1806  $prodcomb = new ProductCombination($this->db);
1807  if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1808  $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1809  if ($result > 0) {
1810  return $result;
1811  } else {
1812  throw new RestException(500, "Error creating new product variant");
1813  }
1814  } else {
1815  return $prodcomb->id;
1816  }
1817  }
1818 
1831  public function putVariant($id, $request_data = null)
1832  {
1833  if (!DolibarrApiAccess::$user->rights->produit->creer) {
1834  throw new RestException(401);
1835  }
1836 
1837  $prodcomb = new ProductCombination($this->db);
1838  $prodcomb->fetch((int) $id);
1839 
1840  foreach ($request_data as $field => $value) {
1841  if ($field == 'rowid') {
1842  continue;
1843  }
1844  $prodcomb->$field = $value;
1845  }
1846 
1847  $result = $prodcomb->update(DolibarrApiAccess::$user);
1848  if ($result > 0) {
1849  return 1;
1850  }
1851  throw new RestException(500, "Error editing variant");
1852  }
1853 
1865  public function deleteVariant($id)
1866  {
1867  if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1868  throw new RestException(401);
1869  }
1870 
1871  $prodcomb = new ProductCombination($this->db);
1872  $prodcomb->id = (int) $id;
1873  $result = $prodcomb->delete(DolibarrApiAccess::$user);
1874  if ($result <= 0) {
1875  throw new RestException(500, "Error deleting variant");
1876  }
1877  return $result;
1878  }
1879 
1894  public function getStock($id, $selected_warehouse_id = null)
1895  {
1896 
1897  if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1898  throw new RestException(401);
1899  }
1900 
1901  if (!DolibarrApi::_checkAccessToResource('product', $id)) {
1902  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1903  }
1904 
1905  $product_model = new Product($this->db);
1906  $product_model->fetch($id);
1907  $product_model->load_stock();
1908 
1909  $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1910  if ($selected_warehouse_id) {
1911  foreach ($stockData as $warehouse_id => $warehouse) {
1912  if ($warehouse_id != $selected_warehouse_id) {
1913  unset($stockData[$warehouse_id]);
1914  }
1915  }
1916  }
1917 
1918  if (empty($stockData)) {
1919  throw new RestException(404, 'No stock found');
1920  }
1921 
1922  return ['stock_warehouses'=>$stockData];
1923  }
1924 
1925  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1932  protected function _cleanObjectDatas($object)
1933  {
1934  // phpcs:enable
1935  $object = parent::_cleanObjectDatas($object);
1936 
1937  unset($object->statut);
1938 
1939  unset($object->regeximgext);
1940  unset($object->price_by_qty);
1941  unset($object->prices_by_qty_id);
1942  unset($object->libelle);
1943  unset($object->product_id_already_linked);
1944  unset($object->reputations);
1945  unset($object->db);
1946  unset($object->name);
1947  unset($object->firstname);
1948  unset($object->lastname);
1949  unset($object->civility_id);
1950  unset($object->contact);
1951  unset($object->contact_id);
1952  unset($object->thirdparty);
1953  unset($object->user);
1954  unset($object->origin);
1955  unset($object->origin_id);
1956  unset($object->fourn_pu);
1957  unset($object->fourn_price_base_type);
1958  unset($object->fourn_socid);
1959  unset($object->ref_fourn);
1960  unset($object->ref_supplier);
1961  unset($object->product_fourn_id);
1962  unset($object->fk_project);
1963 
1964  unset($object->mode_reglement_id);
1965  unset($object->cond_reglement_id);
1966  unset($object->demand_reason_id);
1967  unset($object->transport_mode_id);
1968  unset($object->cond_reglement);
1969  unset($object->shipping_method_id);
1970  unset($object->model_pdf);
1971  unset($object->note);
1972 
1973  unset($object->nbphoto);
1974  unset($object->recuperableonly);
1975  unset($object->multiprices_recuperableonly);
1976  unset($object->tva_npr);
1977  unset($object->lines);
1978  unset($object->fk_bank);
1979  unset($object->fk_account);
1980 
1981  unset($object->supplierprices); // Mut use another API to get them
1982 
1983  if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1984  unset($object->stock_reel);
1985  unset($object->stock_theorique);
1986  unset($object->stock_warehouse);
1987  }
1988 
1989  return $object;
1990  }
1991 
1999  private function _validate($data)
2000  {
2001  $product = array();
2002  foreach (Products::$FIELDS as $field) {
2003  if (!isset($data[$field])) {
2004  throw new RestException(400, "$field field missing");
2005  }
2006  $product[$field] = $data[$field];
2007  }
2008  return $product;
2009  }
2010 
2030  private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0, $includesubproducts = false, $includeparentid = false, $includeifobjectisused = false, $includetrans = false)
2031  {
2032  if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2033  throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode');
2034  }
2035 
2036  $id = (empty($id) ? 0 : $id);
2037 
2038  if (!DolibarrApiAccess::$user->rights->produit->lire) {
2039  throw new RestException(403);
2040  }
2041 
2042  $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2043  if (!$result) {
2044  throw new RestException(404, 'Product not found');
2045  }
2046 
2047  if (!DolibarrApi::_checkAccessToResource('product', $this->product->id)) {
2048  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
2049  }
2050 
2051  if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2052  $this->product->load_stock($includestockdata);
2053 
2054  if (is_array($this->product->stock_warehouse)) {
2055  foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2056  if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2057  foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2058  unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2059  }
2060  }
2061  }
2062  }
2063  }
2064 
2065  if ($includesubproducts) {
2066  $childsArbo = $this->product->getChildsArbo($id, 1);
2067 
2068  $keys = array('rowid', 'qty', 'fk_product_type', 'label', 'incdec', 'ref', 'fk_association', 'rang');
2069  $childs = array();
2070  foreach ($childsArbo as $values) {
2071  $childs[] = array_combine($keys, $values);
2072  }
2073 
2074  $this->product->sousprods = $childs;
2075  }
2076 
2077  if ($includeparentid) {
2078  $prodcomb = new ProductCombination($this->db);
2079  $this->product->fk_product_parent = null;
2080  if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2081  $this->product->fk_product_parent = $fk_product_parent;
2082  }
2083  }
2084 
2085  if ($includeifobjectisused) {
2086  $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2087  }
2088 
2089  return $this->_cleanObjectDatas($this->product);
2090  }
2091 }
Products\getByRefExt
getByRefExt($ref_ext, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref_ext.
Definition: api_products.class.php:131
Products\addAttributes
addAttributes($ref, $label, $ref_ext='')
Add attributes.
Definition: api_products.class.php:1230
Products\delSubproducts
delSubproducts($id, $subproduct_id)
Remove subproduct.
Definition: api_products.class.php:550
db
$conf db
API class for accounts.
Definition: inc.php:41
Products\getCategories
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a product.
Definition: api_products.class.php:581
Products\putAttributes
putAttributes($id, $request_data=null)
Update attributes by id.
Definition: api_products.class.php:1261
ProductCombination
Class ProductCombination Used to represent a product combination.
Definition: ProductCombination.class.php:24
Productcustomerprice
File of class to manage predefined price products or services by customer.
Definition: productcustomerprice.class.php:29
ProductFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.product.class.php:40
Products\getAttributeValuesByRef
getAttributeValuesByRef($ref)
Get all values for an attribute ref.
Definition: api_products.class.php:1493
Products\deletePurchasePrice
deletePurchasePrice($id, $priceid)
Delete purchase price for a product.
Definition: api_products.class.php:820
Products\getAttributesByRef
getAttributesByRef($ref)
Get attributes by ref.
Definition: api_products.class.php:1129
Products\getVariantsByProdRef
getVariantsByProdRef($ref)
Get product variants by Product ref.
Definition: api_products.class.php:1677
Products\getAttributesByRefExt
getAttributesByRefExt($ref_ext)
Get attributes by ref_ext.
Definition: api_products.class.php:1179
ProductAttributeValue
Class ProductAttributeValue Used to represent a product attribute value.
Definition: ProductAttributeValue.class.php:24
Products\getByBarcode
getByBarcode($barcode, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by barcode.
Definition: api_products.class.php:155
Products\getSubproducts
getSubproducts($id)
Get the list of subproducts of the product.
Definition: api_products.class.php:482
sanitizeVal
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
Definition: functions.lib.php:867
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Categorie
Class to manage categories.
Definition: categorie.class.php:47
dol_clone
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
Definition: functions.lib.php:1157
Products\getByRef
getByRef($ref, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref.
Definition: api_products.class.php:107
Products\getCustomerPricesPerSegment
getCustomerPricesPerSegment($id)
Get prices per segment for a product.
Definition: api_products.class.php:611
Products\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_products.class.php:1999
Products\addVariantByProductRef
addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features)
Add variant by product ref.
Definition: api_products.class.php:1777
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5823
Products\addPurchasePrice
addPurchasePrice($id, $qty, $buyprice, $price_base_type, $fourn_id, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='', $multicurrency_buyprice=0, $multicurrency_price_base_type='HT', $multicurrency_tx=1, $multicurrency_code='', $desc_fourn='', $barcode='', $fk_barcode_type=null)
Add/Update purchase prices for a product.
Definition: api_products.class.php:762
Products\getAttributeValues
getAttributeValues($id)
Get all values for an attribute id.
Definition: api_products.class.php:1462
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Products\_fetch
_fetch($id, $ref='', $ref_ext='', $barcode='', $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includeifobjectisused=false, $includetrans=false)
Get properties of 1 product object.
Definition: api_products.class.php:2030
Products\post
post($request_data=null)
Create product object.
Definition: api_products.class.php:309
ProductAttribute
Class ProductAttribute Used to represent a product attribute.
Definition: ProductAttribute.class.php:24
Products\deleteAttributeValueById
deleteAttributeValueById($id)
Delete attribute value by id.
Definition: api_products.class.php:1614
Products\getAttributes
getAttributes($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get attributes.
Definition: api_products.class.php:1020
Products\addVariant
addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference='', $ref_ext='')
Add variant.
Definition: api_products.class.php:1720
Products\addAttributeValue
addAttributeValue($id, $ref, $value)
Add attribute value.
Definition: api_products.class.php:1535
Products\getAttributeValueByRef
getAttributeValueByRef($id, $ref)
Get attribute value by ref.
Definition: api_products.class.php:1376
getEntity
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
Definition: functions.lib.php:180
Products\deleteAttributes
deleteAttributes($id)
Delete attributes by id.
Definition: api_products.class.php:1307
Fournisseur
Class to manage suppliers.
Definition: fournisseur.class.php:34
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
Products\getAttributeById
getAttributeById($id)
Get attribute by ID.
Definition: api_products.class.php:1086
Products\getPurchasePrices
getPurchasePrices($id, $ref='', $ref_ext='', $barcode='')
Get purchase prices for a product.
Definition: api_products.class.php:967
Products\getVariants
getVariants($id, $includestock=0)
Get product variants.
Definition: api_products.class.php:1641
Products\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_products.class.php:1932
forgeSQLFromUniversalSearchCriteria
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
Definition: functions.lib.php:11561
Products\__construct
__construct()
Constructor.
Definition: api_products.class.php:58
Products\deleteAttributeValueByRef
deleteAttributeValueByRef($id, $ref)
Delete attribute value by ref.
Definition: api_products.class.php:1419
Products\addSubproducts
addSubproducts($id, $subproduct_id, $qty, $incdec=1)
Add subproduct.
Definition: api_products.class.php:520
Products\put
put($id, $request_data=null)
Update product.
Definition: api_products.class.php:338
Product
Class to manage products or services.
Definition: product.class.php:46
Products\getSupplierProducts
getSupplierProducts($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $supplier=0, $sqlfilters='')
Get a list of all purchase prices of products.
Definition: api_products.class.php:857
Products\getCustomerPricesPerQuantity
getCustomerPricesPerQuantity($id)
Get prices per quantity for a product.
Definition: api_products.class.php:702
Products\putVariant
putVariant($id, $request_data=null)
Put product variants.
Definition: api_products.class.php:1831
Products\getCustomerPricesPerCustomer
getCustomerPricesPerCustomer($id, $thirdparty_id='')
Get prices per customer for a product.
Definition: api_products.class.php:653
Products\index
index($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $ids_only=false, $variant_filter=0, $pagination_data=false, $includestockdata=0)
List products.
Definition: api_products.class.php:178
Products\getAttributeValueById
getAttributeValueById($id)
Get attribute value by id.
Definition: api_products.class.php:1335
ProductCombination2ValuePair
Class ProductCombination2ValuePair Used to represent the relation between a product combination,...
Definition: ProductCombination2ValuePair.class.php:23
Products
Definition: api_products.class.php:35
Products\deleteVariant
deleteVariant($id)
Delete product variants.
Definition: api_products.class.php:1865
Products\putAttributeValue
putAttributeValue($id, $request_data)
Update attribute value.
Definition: api_products.class.php:1568
Products\getStock
getStock($id, $selected_warehouse_id=null)
Get stock data for the product id given.
Definition: api_products.class.php:1894