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 ($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);
357 $oldproduct =
dol_clone($this->product, 0);
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 : '.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 : '.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->fetch_all(
'',
'', 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 : '.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;
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);
1216 $attr[
"is_used_by_products"] = (int) $obj->nb;
1236 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1237 throw new RestException(401);
1241 $prodattr->label = $label;
1242 $prodattr->ref = $ref;
1243 $prodattr->ref_ext = $ref_ext;
1245 $resid = $prodattr->create(DolibarrApiAccess::$user);
1247 throw new RestException(500,
"Error creating new attribute");
1267 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1268 throw new RestException(401);
1273 $result = $prodattr->fetch((
int) $id);
1275 throw new RestException(404,
'Attribute not found');
1276 } elseif ($result < 0) {
1277 throw new RestException(500,
"Error fetching attribute");
1280 foreach ($request_data as $field => $value) {
1281 if ($field ==
'rowid') {
1284 $prodattr->$field = $value;
1287 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1288 $result = $prodattr->fetch((
int) $id);
1290 throw new RestException(404,
'Attribute not found');
1291 } elseif ($result < 0) {
1292 throw new RestException(500,
"Error fetching attribute");
1297 throw new RestException(500,
"Error updating attribute");
1313 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1314 throw new RestException(401);
1318 $prodattr->id = (int) $id;
1319 $result = $prodattr->delete(DolibarrApiAccess::$user);
1322 throw new RestException(500,
"Error deleting attribute");
1341 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1342 throw new RestException(401);
1345 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1347 $query = $this->
db->query($sql);
1350 throw new RestException(401);
1353 if (!$this->
db->num_rows($query)) {
1354 throw new RestException(404,
'Attribute value not found');
1357 $result = $this->
db->fetch_object($query);
1360 $attrval[
'id'] = $result->rowid;
1361 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1362 $attrval[
'ref'] = $result->ref;
1363 $attrval[
'value'] = $result->value;
1382 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1383 throw new RestException(401);
1388 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1389 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1391 $query = $this->
db->query($sql);
1394 throw new RestException(401);
1397 if (!$this->
db->num_rows($query)) {
1398 throw new RestException(404,
'Attribute value not found');
1401 $result = $this->
db->fetch_object($query);
1404 $attrval[
'id'] = $result->rowid;
1405 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1406 $attrval[
'ref'] = $result->ref;
1407 $attrval[
'value'] = $result->value;
1425 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1426 throw new RestException(401);
1431 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1432 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1433 $query = $this->
db->query($sql);
1436 throw new RestException(401);
1439 if (!$this->
db->num_rows($query)) {
1440 throw new RestException(404,
'Attribute value not found');
1443 $result = $this->
db->fetch_object($query);
1446 $attrval->id = $result->rowid;
1447 $result = $attrval->delete(DolibarrApiAccess::$user);
1452 throw new RestException(500,
"Error deleting attribute value");
1468 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1469 throw new RestException(401);
1474 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1476 if (count($return) == 0) {
1477 throw new RestException(404,
'Attribute values not found');
1480 foreach ($return as $key => $val) {
1481 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1499 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1500 throw new RestException(401);
1508 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1509 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->
db->escape($ref).
"')";
1513 while ($result = $this->
db->fetch_object(
$resql)) {
1515 $tmp->fk_product_attribute = $result->fk_product_attribute;
1516 $tmp->id = $result->rowid;
1517 $tmp->ref = $result->ref;
1518 $tmp->value = $result->value;
1520 $return[] = $this->_cleanObjectDatas($tmp);
1541 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1542 throw new RestException(401);
1545 if (empty($ref) || empty($value)) {
1546 throw new RestException(401);
1550 $objectval->fk_product_attribute = ((int) $id);
1551 $objectval->ref = $ref;
1552 $objectval->value = $value;
1554 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1555 return $objectval->id;
1557 throw new RestException(500,
"Error creating new attribute value");
1574 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1575 throw new RestException(401);
1579 $result = $objectval->fetch((
int) $id);
1582 throw new RestException(404,
'Attribute value not found');
1583 } elseif ($result < 0) {
1584 throw new RestException(500,
"Error fetching attribute value");
1587 foreach ($request_data as $field => $value) {
1588 if ($field ==
'rowid') {
1591 $objectval->$field = $value;
1594 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1595 $result = $objectval->fetch((
int) $id);
1597 throw new RestException(404,
'Attribute not found');
1598 } elseif ($result < 0) {
1599 throw new RestException(500,
"Error fetching attribute");
1604 throw new RestException(500,
"Error updating attribute");
1620 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1621 throw new RestException(401);
1625 $objectval->id = (int) $id;
1627 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1630 throw new RestException(500,
"Error deleting attribute value");
1647 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1648 throw new RestException(401);
1652 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1654 foreach ($combinations as $key => $combination) {
1656 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1657 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1659 if ($includestock==1 && DolibarrApiAccess::$user->rights->stock->lire) {
1661 $productModel->fetch((
int) $combination->fk_product_child);
1662 $productModel->load_stock();
1663 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1667 return $combinations;
1683 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1684 throw new RestException(401);
1687 $result = $this->product->fetch(
'', $ref);
1689 throw new RestException(404,
'Product not found');
1693 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1695 foreach ($combinations as $key => $combination) {
1697 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1698 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1701 return $combinations;
1724 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1726 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1727 throw new RestException(401);
1730 if (empty($id) || empty($features) || !is_array($features)) {
1731 throw new RestException(401);
1734 $weight_impact =
price2num($weight_impact);
1735 $price_impact =
price2num($price_impact);
1739 foreach ($features as $id_attr => $id_value) {
1740 if ($prodattr->fetch((
int) $id_attr) < 0) {
1741 throw new RestException(401);
1743 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1744 throw new RestException(401);
1748 $result = $this->product->fetch((
int) $id);
1750 throw new RestException(404,
'Product not found');
1755 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1759 throw new RestException(500,
"Error creating new product variant");
1783 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1784 throw new RestException(401);
1787 if (empty($ref) || empty($features) || !is_array($features)) {
1788 throw new RestException(401);
1791 $weight_impact =
price2num($weight_impact);
1792 $price_impact =
price2num($price_impact);
1796 foreach ($features as $id_attr => $id_value) {
1797 if ($prodattr->fetch((
int) $id_attr) < 0) {
1798 throw new RestException(404);
1800 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1801 throw new RestException(404);
1805 $result = $this->product->fetch(
'', trim($ref));
1807 throw new RestException(404,
'Product not found');
1811 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1812 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1816 throw new RestException(500,
"Error creating new product variant");
1819 return $prodcomb->id;
1837 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1838 throw new RestException(401);
1842 $prodcomb->fetch((
int) $id);
1844 foreach ($request_data as $field => $value) {
1845 if ($field ==
'rowid') {
1848 $prodcomb->$field = $value;
1851 $result = $prodcomb->update(DolibarrApiAccess::$user);
1855 throw new RestException(500,
"Error editing variant");
1871 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1872 throw new RestException(401);
1876 $prodcomb->id = (int) $id;
1877 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1879 throw new RestException(500,
"Error deleting variant");
1898 public function getStock($id, $selected_warehouse_id =
null)
1901 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1902 throw new RestException(401);
1906 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1909 $product_model =
new Product($this->
db);
1910 $product_model->fetch($id);
1911 $product_model->load_stock();
1913 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1914 if ($selected_warehouse_id) {
1915 foreach ($stockData as $warehouse_id => $warehouse) {
1916 if ($warehouse_id != $selected_warehouse_id) {
1917 unset($stockData[$warehouse_id]);
1922 if (empty($stockData)) {
1923 throw new RestException(404,
'No stock found');
1926 return [
'stock_warehouses'=>$stockData];
1939 $object = parent::_cleanObjectDatas($object);
1941 unset($object->statut);
1943 unset($object->regeximgext);
1944 unset($object->price_by_qty);
1945 unset($object->prices_by_qty_id);
1946 unset($object->libelle);
1947 unset($object->product_id_already_linked);
1948 unset($object->reputations);
1950 unset($object->name);
1951 unset($object->firstname);
1952 unset($object->lastname);
1953 unset($object->civility_id);
1954 unset($object->contact);
1955 unset($object->contact_id);
1956 unset($object->thirdparty);
1957 unset($object->user);
1958 unset($object->origin);
1959 unset($object->origin_id);
1960 unset($object->fourn_pu);
1961 unset($object->fourn_price_base_type);
1962 unset($object->fourn_socid);
1963 unset($object->ref_fourn);
1964 unset($object->ref_supplier);
1965 unset($object->product_fourn_id);
1966 unset($object->fk_project);
1968 unset($object->mode_reglement_id);
1969 unset($object->cond_reglement_id);
1970 unset($object->demand_reason_id);
1971 unset($object->transport_mode_id);
1972 unset($object->cond_reglement);
1973 unset($object->shipping_method_id);
1974 unset($object->model_pdf);
1975 unset($object->note);
1977 unset($object->nbphoto);
1978 unset($object->recuperableonly);
1979 unset($object->multiprices_recuperableonly);
1980 unset($object->tva_npr);
1981 unset($object->lines);
1982 unset($object->fk_bank);
1983 unset($object->fk_account);
1985 unset($object->supplierprices);
1987 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1988 unset($object->stock_reel);
1989 unset($object->stock_theorique);
1990 unset($object->stock_warehouse);
2006 foreach (Products::$FIELDS as $field) {
2007 if (!isset($data[$field])) {
2008 throw new RestException(400,
"$field field missing");
2010 $product[$field] = $data[$field];
2034 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2036 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2037 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2040 $id = (empty($id) ? 0 : $id);
2042 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2043 throw new RestException(403);
2046 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2048 throw new RestException(404,
'Product not found');
2052 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2055 if ($includestockdata && DolibarrApiAccess::$user->rights->stock->lire) {
2056 $this->product->load_stock();
2058 if (is_array($this->product->stock_warehouse)) {
2059 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2060 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2061 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2062 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2069 if ($includesubproducts) {
2070 $childsArbo = $this->product->getChildsArbo($id, 1);
2072 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2074 foreach ($childsArbo as $values) {
2075 $childs[] = array_combine($keys, $values);
2078 $this->product->sousprods = $childs;
2081 if ($includeparentid) {
2083 $this->product->fk_product_parent =
null;
2084 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2085 $this->product->fk_product_parent = $fk_product_parent;
2089 if ($includeifobjectisused) {
2090 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2093 return $this->_cleanObjectDatas($this->product);