19 use Luracast\Restler\RestException;
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';
40 public static $FIELDS = array(
53 public $productsupplier;
83 public function get($id, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
85 return $this->
_fetch($id,
'',
'',
'', $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
107 public function getByRef($ref, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
109 return $this->
_fetch(
'', $ref,
'',
'', $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
131 public function getByRefExt($ref_ext, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
133 return $this->
_fetch(
'',
'', $ref_ext,
'', $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
155 public function getByBarcode($barcode, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
157 return $this->
_fetch(
'',
'',
'', $barcode, $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
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)
182 if (!DolibarrApiAccess::$user->rights->produit->lire) {
183 throw new RestException(403);
188 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
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";
194 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
196 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
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)';
202 if ($variant_filter == 2) {
203 $sql .=
' AND t.rowid in (select distinct fk_product_parent from '.$this->db->prefix().
'product_attribute_combination)';
205 if ($variant_filter == 3) {
206 $sql .=
' AND t.rowid in (select distinct fk_product_child from '.$this->db->prefix().
'product_attribute_combination)';
211 $sql .=
" AND c.fk_categorie = ".((int) $category);
212 $sql .=
" AND c.fk_product = t.rowid";
216 $sql .=
" AND t.fk_product_type = 0";
217 } elseif ($mode == 2) {
219 $sql .=
" AND t.fk_product_type = 1";
226 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
229 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
230 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
234 $sqlTotals = str_replace(
'SELECT t.rowid, t.ref, t.ref_ext',
'SELECT count(t.rowid) as total', $sql);
236 $sql .= $this->
db->order($sortfield, $sortorder);
241 $offset = $limit * $page;
243 $sql .= $this->
db->plimit($limit + 1, $offset);
246 $result = $this->
db->query($sql);
248 $num = $this->
db->num_rows($result);
249 $min = min($num, ($limit <= 0 ? $num : $limit));
252 $obj = $this->
db->fetch_object($result);
254 $product_static =
new Product($this->
db);
255 if ($product_static->fetch($obj->rowid)) {
256 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
257 $product_static->load_stock();
259 if (is_array($product_static->stock_warehouse)) {
260 foreach ($product_static->stock_warehouse as $keytmp => $valtmp) {
261 if (isset($product_static->stock_warehouse[$keytmp]->detail_batch) && is_array($product_static->stock_warehouse[$keytmp]->detail_batch)) {
262 foreach ($product_static->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
263 unset($product_static->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
274 $obj_ret[] = $obj->rowid;
279 throw new RestException(503,
'Error when retrieve product list : '.$this->
db->lasterror());
281 if (!count($obj_ret)) {
282 throw new RestException(404,
'No product found');
286 if ($pagination_data) {
287 $totalsResult = $this->
db->query($sqlTotals);
288 $total = $this->
db->fetch_object($totalsResult)->total;
293 $obj_ret[
'data'] = $tmp;
294 $obj_ret[
'pagination'] = array(
295 'total' => (
int) $total,
297 'page_count' => ceil((
int) $total/$limit),
311 public function post($request_data =
null)
313 if (!DolibarrApiAccess::$user->rights->produit->creer) {
314 throw new RestException(401);
317 $result = $this->
_validate($request_data);
319 foreach ($request_data as $field => $value) {
320 $this->product->$field = $value;
322 if ($this->product->create(DolibarrApiAccess::$user) < 0) {
323 throw new RestException(500,
"Error creating product", array_merge(array($this->product->error), $this->product->errors));
326 return $this->product->id;
340 public function put($id, $request_data =
null)
344 if (!DolibarrApiAccess::$user->rights->produit->creer) {
345 throw new RestException(401);
348 $result = $this->product->fetch($id);
350 throw new RestException(404,
'Product not found');
354 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
359 foreach ($request_data as $field => $value) {
360 if ($field ==
'id') {
363 if ($field ==
'stock_reel') {
364 throw new RestException(400,
'Stock reel cannot be updated here. Use the /stockmovements endpoint instead');
366 $this->product->$field = $value;
370 if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
374 $result = $this->product->update($id, DolibarrApiAccess::$user, 1,
'update', $updatetype);
377 if ($result > 0 && !empty($conf->global->PRODUCT_PRICE_UNIQ)) {
379 $pricemodified =
false;
380 if ($this->product->price_base_type != $oldproduct->price_base_type) {
381 $pricemodified =
true;
383 if ($this->product->tva_tx != $oldproduct->tva_tx) {
384 $pricemodified =
true;
386 if ($this->product->tva_npr != $oldproduct->tva_npr) {
387 $pricemodified =
true;
389 if ($this->product->default_vat_code != $oldproduct->default_vat_code) {
390 $pricemodified =
true;
393 if ($this->product->price_base_type ==
'TTC') {
394 if ($this->product->price_ttc != $oldproduct->price_ttc) {
395 $pricemodified =
true;
397 if ($this->product->price_min_ttc != $oldproduct->price_min_ttc) {
398 $pricemodified =
true;
401 if ($this->product->price != $oldproduct->price) {
402 $pricemodified =
true;
404 if ($this->product->price_min != $oldproduct->price_min) {
405 $pricemodified =
true;
410 if ($pricemodified) {
411 $newvat = $this->product->tva_tx;
412 $newnpr = $this->product->tva_npr;
413 $newvatsrccode = $this->product->default_vat_code;
415 $newprice = $this->product->price;
416 $newpricemin = $this->product->price_min;
417 if ($this->product->price_base_type ==
'TTC') {
418 $newprice = $this->product->price_ttc;
419 $newpricemin = $this->product->price_min_ttc;
422 $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode);
427 throw new RestException(500,
"Error updating product", array_merge(array($this->product->error), $this->product->errors));
430 return $this->
get($id);
439 public function delete($id)
441 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
442 throw new RestException(401);
444 $result = $this->product->fetch($id);
446 throw new RestException(404,
'Product not found');
450 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
455 $user = DolibarrApiAccess::$user;
457 $res = $this->product->delete(DolibarrApiAccess::$user);
459 throw new RestException(500,
"Can't delete, error occurs");
460 } elseif ($res == 0) {
461 throw new RestException(409,
"Can't delete, that product is probably used");
467 'message' =>
'Object deleted'
486 if (!DolibarrApiAccess::$user->rights->produit->lire) {
487 throw new RestException(401);
491 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
494 $childsArbo = $this->product->getChildsArbo($id, 1);
496 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
498 foreach ($childsArbo as $values) {
499 $childs[] = array_combine($keys, $values);
524 if (!DolibarrApiAccess::$user->rights->produit->creer) {
525 throw new RestException(401);
529 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
532 $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
534 throw new RestException(500,
"Error adding product child");
554 if (!DolibarrApiAccess::$user->rights->produit->creer) {
555 throw new RestException(401);
559 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
562 $result = $this->product->del_sousproduit($id, $subproduct_id);
564 throw new RestException(500,
"Error while removing product child");
583 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
585 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
586 throw new RestException(401);
591 $result = $categories->getListForItem($id,
'product', $sortfield, $sortorder, $limit, $page);
593 if (empty($result)) {
594 throw new RestException(404,
'No category found');
598 throw new RestException(503,
'Error when retrieve category list : '.join(
',', array_merge(array($categories->error), $categories->errors)));
617 if (!DolibarrApiAccess::$user->rights->produit->lire) {
618 throw new RestException(401);
621 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
622 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
625 $result = $this->product->fetch($id);
627 throw new RestException(404,
'Product not found');
631 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
635 'multiprices'=>$this->product->multiprices,
636 'multiprices_inc_tax'=>$this->product->multiprices_ttc,
637 'multiprices_min'=>$this->product->multiprices_min,
638 'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
639 'multiprices_vat'=>$this->product->multiprices_tva_tx,
640 'multiprices_base_type'=>$this->product->multiprices_base_type,
659 if (!DolibarrApiAccess::$user->rights->produit->lire) {
660 throw new RestException(401);
663 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
664 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
667 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
668 if ($socid > 0 && $socid != $thirdparty_id) {
669 throw new RestException(401,
'Getting prices for all customers or for the customer ID '.$thirdparty_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
672 $result = $this->product->fetch($id);
674 throw new RestException(404,
'Product not found');
678 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
681 $filter[
't.fk_product'] .= $id;
682 if ($thirdparty_id) {
683 $filter[
't.fk_soc'] .= $thirdparty_id;
685 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
688 if (empty($prodcustprice->lines)) {
689 throw new RestException(404,
'Prices not found');
692 return $prodcustprice->lines;
708 if (!DolibarrApiAccess::$user->rights->produit->lire) {
709 throw new RestException(401);
712 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
713 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
716 $result = $this->product->fetch($id);
718 throw new RestException(404,
'Product not found');
722 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
726 'prices_by_qty'=>$this->product->prices_by_qty[0],
727 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
764 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)
766 if (!DolibarrApiAccess::$user->rights->produit->creer) {
767 throw new RestException(401);
770 $result = $this->productsupplier->fetch($id);
772 throw new RestException(404,
'Product not found');
776 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
779 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
780 if ($socid > 0 && $socid != $fourn_id) {
781 throw new RestException(401,
'Adding purchase price for the supplier ID '.$fourn_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
784 $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty);
786 throw new RestException(500,
"Error adding supplier to product : ".$this->
db->lasterror());
790 $result = $fourn->fetch($fourn_id);
792 throw new RestException(404,
'Supplier not found');
796 $ref_fourn =
sanitizeVal($ref_fourn,
'alphanohtml');
797 $desc_fourn =
sanitizeVal($desc_fourn,
'restricthtml');
800 $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);
803 throw new RestException(500,
"Error updating buy price : ".$this->
db->lasterror());
805 return (
int) $this->productsupplier->product_fourn_price_id;
824 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
825 throw new RestException(401);
827 $result = $this->productsupplier->fetch($id);
829 throw new RestException(404,
'Product not found');
833 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
838 $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid);
841 return $resultsupplier;
859 public function getSupplierProducts($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters =
'')
863 if (!DolibarrApiAccess::$user->rights->produit->lire) {
864 throw new RestException(401);
870 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
872 if ($supplier != $socid || empty($supplier)) {
873 throw new RestException(401,
'As an external user, you can request only for your supplier id = '.$socid);
877 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
878 $sql .=
" FROM ".$this->db->prefix().
"product as t";
880 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
882 $sql .=
", ".$this->db->prefix().
"product_fournisseur_price as s";
884 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
887 $sql .=
" AND s.fk_soc = ".((int) $supplier);
890 $sql .=
" AND s.fk_soc = ".((int) $socid);
892 $sql .=
" AND s.fk_product = t.rowid";
895 $sql .=
" AND c.fk_categorie = ".((int) $category);
896 $sql .=
" AND c.fk_product = t.rowid";
900 $sql .=
" AND t.fk_product_type = 0";
901 } elseif ($mode == 2) {
903 $sql .=
" AND t.fk_product_type = 1";
909 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
911 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
912 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
915 $sql .= $this->
db->order($sortfield, $sortorder);
920 $offset = $limit * $page;
921 $sql .= $this->
db->plimit($limit + 1, $offset);
923 $result = $this->
db->query($sql);
925 $num = $this->
db->num_rows($result);
926 $min = min($num, ($limit <= 0 ? $num : $limit));
929 $obj = $this->
db->fetch_object($result);
932 $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid,
'',
'', 0, 0);
933 foreach ($product_fourn_list as $tmpobj) {
938 $obj_ret[$obj->rowid] = $product_fourn_list;
943 throw new RestException(503,
'Error when retrieve product list : '.$this->
db->lasterror());
945 if (!count($obj_ret)) {
946 throw new RestException(404,
'No product found');
972 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
973 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
976 $id = (empty($id) ? 0 : $id);
978 if (!DolibarrApiAccess::$user->rights->produit->lire) {
979 throw new RestException(403);
982 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
984 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
986 throw new RestException(404,
'Product not found');
990 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
993 $product_fourn_list = array();
997 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
1000 foreach ($product_fourn_list as $tmpobj) {
1023 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
1025 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1026 throw new RestException(401);
1029 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1030 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1031 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1037 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
1039 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
1040 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
1043 $sql .= $this->
db->order($sortfield, $sortorder);
1048 $offset = $limit * $page;
1050 $sql .= $this->
db->plimit($limit, $offset);
1053 $result = $this->
db->query($sql);
1056 throw new RestException(503,
'Error when retrieve product attribute list : '.$this->
db->lasterror());
1060 while ($result = $this->
db->fetch_object($query)) {
1062 $tmp->id = $result->rowid;
1063 $tmp->ref = $result->ref;
1064 $tmp->ref_ext = $result->ref_ext;
1065 $tmp->label = $result->label;
1066 $tmp->position = $result->position;
1067 $tmp->entity = $result->entity;
1072 if (!count($return)) {
1073 throw new RestException(404,
'No product attribute found');
1092 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1093 throw new RestException(401);
1097 $result = $prodattr->fetch((
int) $id);
1100 throw new RestException(404,
"Product attribute not found");
1103 $fields = [
"id",
"ref",
"ref_ext",
"label",
"position",
"entity"];
1105 foreach ($prodattr as $field => $value) {
1106 if (!in_array($field, $fields)) {
1107 unset($prodattr->{$field});
1111 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1112 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1113 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1116 $obj = $this->
db->fetch_object(
$resql);
1117 $prodattr->is_used_by_products = (int) $obj->nb;
1119 return $this->_cleanObjectDatas($prodattr);
1135 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1136 throw new RestException(401);
1141 $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').
")";
1143 $query = $this->
db->query($sql);
1145 if (!$this->
db->num_rows($query)) {
1146 throw new RestException(404);
1149 $result = $this->
db->fetch_object($query);
1152 $attr[
'id'] = $result->rowid;
1153 $attr[
'ref'] = $result->ref;
1154 $attr[
'ref_ext'] = $result->ref_ext;
1155 $attr[
'label'] = $result->label;
1156 $attr[
'rang'] = $result->position;
1157 $attr[
'position'] = $result->position;
1158 $attr[
'entity'] = $result->entity;
1160 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1161 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1162 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1165 $obj = $this->
db->fetch_object(
$resql);
1167 $attr[
"is_used_by_products"] = (int) $obj->nb;
1185 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1186 throw new RestException(401);
1189 $ref_ext = trim($ref_ext);
1191 $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').
")";
1193 $query = $this->
db->query($sql);
1195 if (!$this->
db->num_rows($query)) {
1196 throw new RestException(404);
1199 $result = $this->
db->fetch_object($query);
1202 $attr[
'id'] = $result->rowid;
1203 $attr[
'ref'] = $result->ref;
1204 $attr[
'ref_ext'] = $result->ref_ext;
1205 $attr[
'label'] = $result->label;
1206 $attr[
'rang'] = $result->position;
1207 $attr[
'position'] = $result->position;
1208 $attr[
'entity'] = $result->entity;
1210 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1211 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1212 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1215 $obj = $this->
db->fetch_object(
$resql);
1217 $attr[
"is_used_by_products"] = (int) $obj->nb;
1237 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1238 throw new RestException(401);
1242 $prodattr->label = $label;
1243 $prodattr->ref = $ref;
1244 $prodattr->ref_ext = $ref_ext;
1246 $resid = $prodattr->create(DolibarrApiAccess::$user);
1248 throw new RestException(500,
"Error creating new attribute");
1269 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1270 throw new RestException(401);
1275 $result = $prodattr->fetch((
int) $id);
1277 throw new RestException(404,
'Attribute not found');
1278 } elseif ($result < 0) {
1279 throw new RestException(500,
"Error fetching attribute");
1282 foreach ($request_data as $field => $value) {
1283 if ($field ==
'rowid') {
1286 $prodattr->$field = $value;
1289 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1290 $result = $prodattr->fetch((
int) $id);
1292 throw new RestException(404,
'Attribute not found');
1293 } elseif ($result < 0) {
1294 throw new RestException(500,
"Error fetching attribute");
1296 return $this->_cleanObjectDatas($prodattr);
1299 throw new RestException(500,
"Error updating attribute");
1315 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1316 throw new RestException(401);
1320 $prodattr->id = (int) $id;
1321 $result = $prodattr->delete(DolibarrApiAccess::$user);
1324 throw new RestException(500,
"Error deleting attribute");
1343 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1344 throw new RestException(401);
1347 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1349 $query = $this->
db->query($sql);
1352 throw new RestException(401);
1355 if (!$this->
db->num_rows($query)) {
1356 throw new RestException(404,
'Attribute value not found');
1359 $result = $this->
db->fetch_object($query);
1362 $attrval[
'id'] = $result->rowid;
1363 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1364 $attrval[
'ref'] = $result->ref;
1365 $attrval[
'value'] = $result->value;
1384 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1385 throw new RestException(401);
1390 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1391 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1393 $query = $this->
db->query($sql);
1396 throw new RestException(401);
1399 if (!$this->
db->num_rows($query)) {
1400 throw new RestException(404,
'Attribute value not found');
1403 $result = $this->
db->fetch_object($query);
1406 $attrval[
'id'] = $result->rowid;
1407 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1408 $attrval[
'ref'] = $result->ref;
1409 $attrval[
'value'] = $result->value;
1427 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1428 throw new RestException(401);
1433 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1434 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1435 $query = $this->
db->query($sql);
1438 throw new RestException(401);
1441 if (!$this->
db->num_rows($query)) {
1442 throw new RestException(404,
'Attribute value not found');
1445 $result = $this->
db->fetch_object($query);
1448 $attrval->id = $result->rowid;
1449 $result = $attrval->delete(DolibarrApiAccess::$user);
1454 throw new RestException(500,
"Error deleting attribute value");
1470 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1471 throw new RestException(401);
1476 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1478 if (count($return) == 0) {
1479 throw new RestException(404,
'Attribute values not found');
1482 foreach ($return as $key => $val) {
1483 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1501 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1502 throw new RestException(401);
1510 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1511 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->
db->escape($ref).
"')";
1515 while ($result = $this->
db->fetch_object(
$resql)) {
1517 $tmp->fk_product_attribute = $result->fk_product_attribute;
1518 $tmp->id = $result->rowid;
1519 $tmp->ref = $result->ref;
1520 $tmp->value = $result->value;
1522 $return[] = $this->_cleanObjectDatas($tmp);
1543 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1544 throw new RestException(401);
1547 if (empty($ref) || empty($value)) {
1548 throw new RestException(401);
1552 $objectval->fk_product_attribute = ((int) $id);
1553 $objectval->ref = $ref;
1554 $objectval->value = $value;
1556 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1557 return $objectval->id;
1559 throw new RestException(500,
"Error creating new attribute value");
1576 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1577 throw new RestException(401);
1581 $result = $objectval->fetch((
int) $id);
1584 throw new RestException(404,
'Attribute value not found');
1585 } elseif ($result < 0) {
1586 throw new RestException(500,
"Error fetching attribute value");
1589 foreach ($request_data as $field => $value) {
1590 if ($field ==
'rowid') {
1593 $objectval->$field = $value;
1596 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1597 $result = $objectval->fetch((
int) $id);
1599 throw new RestException(404,
'Attribute not found');
1600 } elseif ($result < 0) {
1601 throw new RestException(500,
"Error fetching attribute");
1603 return $this->_cleanObjectDatas($objectval);
1606 throw new RestException(500,
"Error updating attribute");
1622 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1623 throw new RestException(401);
1627 $objectval->id = (int) $id;
1629 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1632 throw new RestException(500,
"Error deleting attribute value");
1649 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1650 throw new RestException(401);
1654 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1656 foreach ($combinations as $key => $combination) {
1658 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1659 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1661 if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1663 $productModel->fetch((
int) $combination->fk_product_child);
1664 $productModel->load_stock($includestock);
1665 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1669 return $combinations;
1685 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1686 throw new RestException(401);
1689 $result = $this->product->fetch(
'', $ref);
1691 throw new RestException(404,
'Product not found');
1695 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1697 foreach ($combinations as $key => $combination) {
1699 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1700 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1703 return $combinations;
1726 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1728 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1729 throw new RestException(401);
1732 if (empty($id) || empty($features) || !is_array($features)) {
1733 throw new RestException(401);
1736 $weight_impact =
price2num($weight_impact);
1737 $price_impact =
price2num($price_impact);
1741 foreach ($features as $id_attr => $id_value) {
1742 if ($prodattr->fetch((
int) $id_attr) < 0) {
1743 throw new RestException(401);
1745 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1746 throw new RestException(401);
1750 $result = $this->product->fetch((
int) $id);
1752 throw new RestException(404,
'Product not found');
1757 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1761 throw new RestException(500,
"Error creating new product variant");
1785 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1786 throw new RestException(401);
1789 if (empty($ref) || empty($features) || !is_array($features)) {
1790 throw new RestException(401);
1793 $weight_impact =
price2num($weight_impact);
1794 $price_impact =
price2num($price_impact);
1798 foreach ($features as $id_attr => $id_value) {
1799 if ($prodattr->fetch((
int) $id_attr) < 0) {
1800 throw new RestException(404);
1802 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1803 throw new RestException(404);
1807 $result = $this->product->fetch(
'', trim($ref));
1809 throw new RestException(404,
'Product not found');
1813 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1814 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1818 throw new RestException(500,
"Error creating new product variant");
1821 return $prodcomb->id;
1839 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1840 throw new RestException(401);
1844 $prodcomb->fetch((
int) $id);
1846 foreach ($request_data as $field => $value) {
1847 if ($field ==
'rowid') {
1850 $prodcomb->$field = $value;
1853 $result = $prodcomb->update(DolibarrApiAccess::$user);
1857 throw new RestException(500,
"Error editing variant");
1873 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1874 throw new RestException(401);
1878 $prodcomb->id = (int) $id;
1879 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1881 throw new RestException(500,
"Error deleting variant");
1900 public function getStock($id, $selected_warehouse_id =
null)
1902 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1903 throw new RestException(401);
1907 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1910 $product_model =
new Product($this->
db);
1911 $product_model->fetch($id);
1912 $product_model->load_stock();
1914 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1915 if ($selected_warehouse_id) {
1916 foreach ($stockData as $warehouse_id => $warehouse) {
1917 if ($warehouse_id != $selected_warehouse_id) {
1918 unset($stockData[$warehouse_id]);
1923 if (empty($stockData)) {
1924 throw new RestException(404,
'No stock found');
1927 return array(
'stock_warehouses'=>$stockData);
1940 $object = parent::_cleanObjectDatas($object);
1942 unset($object->statut);
1944 unset($object->regeximgext);
1945 unset($object->price_by_qty);
1946 unset($object->prices_by_qty_id);
1947 unset($object->libelle);
1948 unset($object->product_id_already_linked);
1949 unset($object->reputations);
1951 unset($object->name);
1952 unset($object->firstname);
1953 unset($object->lastname);
1954 unset($object->civility_id);
1955 unset($object->contact);
1956 unset($object->contact_id);
1957 unset($object->thirdparty);
1958 unset($object->user);
1959 unset($object->origin);
1960 unset($object->origin_id);
1961 unset($object->fourn_pu);
1962 unset($object->fourn_price_base_type);
1963 unset($object->fourn_socid);
1964 unset($object->ref_fourn);
1965 unset($object->ref_supplier);
1966 unset($object->product_fourn_id);
1967 unset($object->fk_project);
1969 unset($object->mode_reglement_id);
1970 unset($object->cond_reglement_id);
1971 unset($object->demand_reason_id);
1972 unset($object->transport_mode_id);
1973 unset($object->cond_reglement);
1974 unset($object->shipping_method_id);
1975 unset($object->model_pdf);
1976 unset($object->note);
1978 unset($object->nbphoto);
1979 unset($object->recuperableonly);
1980 unset($object->multiprices_recuperableonly);
1981 unset($object->tva_npr);
1982 unset($object->lines);
1983 unset($object->fk_bank);
1984 unset($object->fk_account);
1986 unset($object->supplierprices);
1988 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1989 unset($object->stock_reel);
1990 unset($object->stock_theorique);
1991 unset($object->stock_warehouse);
2007 foreach (Products::$FIELDS as $field) {
2008 if (!isset($data[$field])) {
2009 throw new RestException(400,
"$field field missing");
2011 $product[$field] = $data[$field];
2035 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2037 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2038 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2041 $id = (empty($id) ? 0 : $id);
2043 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2044 throw new RestException(403);
2047 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2049 throw new RestException(404,
'Product not found');
2053 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2056 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2057 $this->product->load_stock($includestockdata);
2059 if (is_array($this->product->stock_warehouse)) {
2060 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2061 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2062 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2063 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2070 if ($includesubproducts) {
2071 $childsArbo = $this->product->getChildsArbo($id, 1);
2073 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2075 foreach ($childsArbo as $values) {
2076 $childs[] = array_combine($keys, $values);
2079 $this->product->sousprods = $childs;
2082 if ($includeparentid) {
2084 $this->product->fk_product_parent =
null;
2085 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2086 $this->product->fk_product_parent = $fk_product_parent;
2090 if ($includeifobjectisused) {
2091 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2094 return $this->_cleanObjectDatas($this->product);
Class to manage categories.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Class to manage suppliers.
Class ProductAttribute Used to represent a product attribute.
Class ProductAttributeValue Used to represent a product attribute value.
Class ProductCombination2ValuePair Used to represent the relation between a product combination,...
Class ProductCombination Used to represent a product combination.
Class to manage predefined suppliers products.
Class to manage products or services.
File of class to manage predefined price products or services by customer.
_cleanObjectDatas($object)
Clean sensible object datas.
putAttributeValue($id, $request_data)
Update attribute value.
deleteAttributes($id)
Delete attributes by id.
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.
deletePurchasePrice($id, $priceid)
Delete purchase price for a product.
getAttributeValuesByRef($ref)
Get all values for an attribute ref.
putVariant($id, $request_data=null)
Put product variants.
put($id, $request_data=null)
Update product.
addAttributeValue($id, $ref, $value)
Add attribute value.
addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features)
Add variant by product ref.
getAttributeValueById($id)
Get attribute value by id.
getVariantsByProdRef($ref)
Get product variants by Product ref.
getCustomerPricesPerQuantity($id)
Get prices per quantity for a product.
__construct()
Constructor.
getPurchasePrices($id, $ref='', $ref_ext='', $barcode='')
Get purchase prices for a product.
delSubproducts($id, $subproduct_id)
Remove subproduct.
getVariants($id, $includestock=0)
Get product variants.
putAttributes($id, $request_data=null)
Update attributes by id.
deleteAttributeValueById($id)
Delete attribute value by id.
getAttributeValues($id)
Get all values for an attribute id.
addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference='', $ref_ext='')
Add variant.
getSubproducts($id)
Get the list of subproducts of the product.
getAttributesByRefExt($ref_ext)
Get attributes by ref_ext.
getByRef($ref, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref.
deleteAttributeValueByRef($id, $ref)
Delete attribute value by ref.
post($request_data=null)
Create product object.
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.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a product.
getByBarcode($barcode, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by barcode.
getByRefExt($ref_ext, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref_ext.
getCustomerPricesPerCustomer($id, $thirdparty_id='')
Get prices per customer for a product.
deleteVariant($id)
Delete product variants.
_validate($data)
Validate fields before create or update object.
getCustomerPricesPerSegment($id)
Get prices per segment for a product.
_fetch($id, $ref='', $ref_ext='', $barcode='', $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includeifobjectisused=false, $includetrans=false)
Get properties of 1 product object.
addAttributes($ref, $label, $ref_ext='')
Add attributes.
getAttributeValueByRef($id, $ref)
Get attribute value by ref.
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.
getAttributeById($id)
Get attribute by ID.
getStock($id, $selected_warehouse_id=null)
Get stock data for the product id given.
getAttributes($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get attributes.
getAttributesByRef($ref)
Get attributes by ref.
addSubproducts($id, $subproduct_id, $qty, $incdec=1)
Add subproduct.
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.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
$conf db
API class for accounts.