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 return $this->product->delete(DolibarrApiAccess::$user);
474 if (!DolibarrApiAccess::$user->rights->produit->lire) {
475 throw new RestException(401);
479 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
482 $childsArbo = $this->product->getChildsArbo($id, 1);
484 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
486 foreach ($childsArbo as $values) {
487 $childs[] = array_combine($keys, $values);
512 if (!DolibarrApiAccess::$user->rights->produit->creer) {
513 throw new RestException(401);
517 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
520 $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
522 throw new RestException(500,
"Error adding product child");
542 if (!DolibarrApiAccess::$user->rights->produit->creer) {
543 throw new RestException(401);
547 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
550 $result = $this->product->del_sousproduit($id, $subproduct_id);
552 throw new RestException(500,
"Error while removing product child");
571 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
573 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
574 throw new RestException(401);
579 $result = $categories->getListForItem($id,
'product', $sortfield, $sortorder, $limit, $page);
581 if (empty($result)) {
582 throw new RestException(404,
'No category found');
586 throw new RestException(503,
'Error when retrieve category list : '.array_merge(array($categories->error), $categories->errors));
605 if (!DolibarrApiAccess::$user->rights->produit->lire) {
606 throw new RestException(401);
609 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
610 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
613 $result = $this->product->fetch($id);
615 throw new RestException(404,
'Product not found');
619 throw new RestException(503,
'Error when retrieve prices list : '.array_merge(array($this->product->error), $this->product->errors));
623 'multiprices'=>$this->product->multiprices,
624 'multiprices_inc_tax'=>$this->product->multiprices_ttc,
625 'multiprices_min'=>$this->product->multiprices_min,
626 'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
627 'multiprices_vat'=>$this->product->multiprices_tva_tx,
628 'multiprices_base_type'=>$this->product->multiprices_base_type,
647 if (!DolibarrApiAccess::$user->rights->produit->lire) {
648 throw new RestException(401);
651 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
652 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
655 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
656 if ($socid > 0 && $socid != $thirdparty_id) {
657 throw new RestException(401,
'Getting prices for all customers or for the customer ID '.$thirdparty_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
660 $result = $this->product->fetch($id);
662 throw new RestException(404,
'Product not found');
666 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
669 $filter[
't.fk_product'] .= $id;
670 if ($thirdparty_id) {
671 $filter[
't.fk_soc'] .= $thirdparty_id;
673 $result = $prodcustprice->fetch_all(
'',
'', 0, 0, $filter);
676 if (empty($prodcustprice->lines)) {
677 throw new RestException(404,
'Prices not found');
680 return $prodcustprice->lines;
696 if (!DolibarrApiAccess::$user->rights->produit->lire) {
697 throw new RestException(401);
700 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
701 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
704 $result = $this->product->fetch($id);
706 throw new RestException(404,
'Product not found');
710 throw new RestException(503,
'Error when retrieve prices list : '.array_merge(array($this->product->error), $this->product->errors));
714 'prices_by_qty'=>$this->product->prices_by_qty[0],
715 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
752 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)
754 if (!DolibarrApiAccess::$user->rights->produit->creer) {
755 throw new RestException(401);
758 $result = $this->productsupplier->fetch($id);
760 throw new RestException(404,
'Product not found');
764 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
767 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
768 if ($socid > 0 && $socid != $fourn_id) {
769 throw new RestException(401,
'Adding purchase price for the supplier ID '.$fourn_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
772 $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty);
774 throw new RestException(500,
"Error adding supplier to product : ".$this->
db->lasterror());
778 $result = $fourn->fetch($fourn_id);
780 throw new RestException(404,
'Supplier not found');
784 $ref_fourn =
sanitizeVal($ref_fourn,
'alphanohtml');
785 $desc_fourn =
sanitizeVal($desc_fourn,
'restricthtml');
788 $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);
791 throw new RestException(500,
"Error updating buy price : ".$this->
db->lasterror());
793 return (
int) $this->productsupplier->product_fourn_price_id;
812 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
813 throw new RestException(401);
815 $result = $this->productsupplier->fetch($id);
817 throw new RestException(404,
'Product not found');
821 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
826 $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid);
829 return $resultsupplier;
847 public function getSupplierProducts($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters =
'')
851 if (!DolibarrApiAccess::$user->rights->produit->lire) {
852 throw new RestException(401);
858 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
860 if ($supplier != $socid || empty($supplier)) {
861 throw new RestException(401,
'As an external user, you can request only for your supplier id = '.$socid);
865 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
866 $sql .=
" FROM ".$this->db->prefix().
"product as t";
868 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
870 $sql .=
", ".$this->db->prefix().
"product_fournisseur_price as s";
872 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
875 $sql .=
" AND s.fk_soc = ".((int) $supplier);
878 $sql .=
" AND s.fk_soc = ".((int) $socid);
880 $sql .=
" AND s.fk_product = t.rowid";
883 $sql .=
" AND c.fk_categorie = ".((int) $category);
884 $sql .=
" AND c.fk_product = t.rowid";
888 $sql .=
" AND t.fk_product_type = 0";
889 } elseif ($mode == 2) {
891 $sql .=
" AND t.fk_product_type = 1";
897 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
899 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
900 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
903 $sql .= $this->
db->order($sortfield, $sortorder);
908 $offset = $limit * $page;
909 $sql .= $this->
db->plimit($limit + 1, $offset);
911 $result = $this->
db->query($sql);
913 $num = $this->
db->num_rows($result);
914 $min = min($num, ($limit <= 0 ? $num : $limit));
917 $obj = $this->
db->fetch_object($result);
920 $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid,
'',
'', 0, 0);
921 foreach ($product_fourn_list as $tmpobj) {
926 $obj_ret[$obj->rowid] = $product_fourn_list;
931 throw new RestException(503,
'Error when retrieve product list : '.$this->
db->lasterror());
933 if (!count($obj_ret)) {
934 throw new RestException(404,
'No product found');
960 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
961 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
964 $id = (empty($id) ? 0 : $id);
966 if (!DolibarrApiAccess::$user->rights->produit->lire) {
967 throw new RestException(403);
970 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
972 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
974 throw new RestException(404,
'Product not found');
978 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
981 $product_fourn_list = array();
985 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
988 foreach ($product_fourn_list as $tmpobj) {
1011 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
1013 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1014 throw new RestException(401);
1017 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.rang, t.entity";
1018 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1019 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1025 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
1027 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
1028 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
1031 $sql .= $this->
db->order($sortfield, $sortorder);
1036 $offset = $limit * $page;
1038 $sql .= $this->
db->plimit($limit, $offset);
1041 $result = $this->
db->query($sql);
1044 throw new RestException(503,
'Error when retrieve product attribute list : '.$this->
db->lasterror());
1048 while ($result = $this->
db->fetch_object($query)) {
1050 $tmp->id = $result->rowid;
1051 $tmp->ref = $result->ref;
1052 $tmp->ref_ext = $result->ref_ext;
1053 $tmp->label = $result->label;
1054 $tmp->rang = $result->rang;
1055 $tmp->entity = $result->entity;
1060 if (!count($return)) {
1061 throw new RestException(404,
'No product attribute found');
1080 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1081 throw new RestException(401);
1085 $result = $prodattr->fetch((
int) $id);
1088 throw new RestException(404,
"Product attribute not found");
1091 $fields = [
"id",
"ref",
"ref_ext",
"label",
"rang",
"entity"];
1093 foreach ($prodattr as $field => $value) {
1094 if (!in_array($field, $fields)) {
1095 unset($prodattr->{$field});
1099 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1100 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1101 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1104 $obj = $this->
db->fetch_object(
$resql);
1105 $prodattr->is_used_by_products = (int) $obj->nb;
1123 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1124 throw new RestException(401);
1129 $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').
")";
1131 $query = $this->
db->query($sql);
1133 if (!$this->
db->num_rows($query)) {
1134 throw new RestException(404);
1137 $result = $this->
db->fetch_object($query);
1140 $attr[
'id'] = $result->rowid;
1141 $attr[
'ref'] = $result->ref;
1142 $attr[
'ref_ext'] = $result->ref_ext;
1143 $attr[
'label'] = $result->label;
1144 $attr[
'rang'] = $result->position;
1145 $attr[
'position'] = $result->position;
1146 $attr[
'entity'] = $result->entity;
1148 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1149 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1150 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1153 $obj = $this->
db->fetch_object(
$resql);
1155 $attr[
"is_used_by_products"] = (int) $obj->nb;
1173 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1174 throw new RestException(401);
1177 $ref_ext = trim($ref_ext);
1179 $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').
")";
1181 $query = $this->
db->query($sql);
1183 if (!$this->
db->num_rows($query)) {
1184 throw new RestException(404);
1187 $result = $this->
db->fetch_object($query);
1190 $attr[
'id'] = $result->rowid;
1191 $attr[
'ref'] = $result->ref;
1192 $attr[
'ref_ext'] = $result->ref_ext;
1193 $attr[
'label'] = $result->label;
1194 $attr[
'rang'] = $result->position;
1195 $attr[
'position'] = $result->position;
1196 $attr[
'entity'] = $result->entity;
1198 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1199 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1200 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1203 $obj = $this->
db->fetch_object(
$resql);
1204 $attr[
"is_used_by_products"] = (int) $obj->nb;
1224 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1225 throw new RestException(401);
1229 $prodattr->label = $label;
1230 $prodattr->ref = $ref;
1231 $prodattr->ref_ext = $ref_ext;
1233 $resid = $prodattr->create(DolibarrApiAccess::$user);
1235 throw new RestException(500,
"Error creating new attribute");
1255 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1256 throw new RestException(401);
1261 $result = $prodattr->fetch((
int) $id);
1263 throw new RestException(404,
'Attribute not found');
1264 } elseif ($result < 0) {
1265 throw new RestException(500,
"Error fetching attribute");
1268 foreach ($request_data as $field => $value) {
1269 if ($field ==
'rowid') {
1272 $prodattr->$field = $value;
1275 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1276 $result = $prodattr->fetch((
int) $id);
1278 throw new RestException(404,
'Attribute not found');
1279 } elseif ($result < 0) {
1280 throw new RestException(500,
"Error fetching attribute");
1285 throw new RestException(500,
"Error updating attribute");
1301 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1302 throw new RestException(401);
1306 $prodattr->id = (int) $id;
1307 $result = $prodattr->delete(DolibarrApiAccess::$user);
1310 throw new RestException(500,
"Error deleting attribute");
1329 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1330 throw new RestException(401);
1333 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1335 $query = $this->
db->query($sql);
1338 throw new RestException(401);
1341 if (!$this->
db->num_rows($query)) {
1342 throw new RestException(404,
'Attribute value not found');
1345 $result = $this->
db->fetch_object($query);
1348 $attrval[
'id'] = $result->rowid;
1349 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1350 $attrval[
'ref'] = $result->ref;
1351 $attrval[
'value'] = $result->value;
1370 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1371 throw new RestException(401);
1376 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1377 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1379 $query = $this->
db->query($sql);
1382 throw new RestException(401);
1385 if (!$this->
db->num_rows($query)) {
1386 throw new RestException(404,
'Attribute value not found');
1389 $result = $this->
db->fetch_object($query);
1392 $attrval[
'id'] = $result->rowid;
1393 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1394 $attrval[
'ref'] = $result->ref;
1395 $attrval[
'value'] = $result->value;
1413 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1414 throw new RestException(401);
1419 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1420 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1421 $query = $this->
db->query($sql);
1424 throw new RestException(401);
1427 if (!$this->
db->num_rows($query)) {
1428 throw new RestException(404,
'Attribute value not found');
1431 $result = $this->
db->fetch_object($query);
1434 $attrval->id = $result->rowid;
1435 $result = $attrval->delete(DolibarrApiAccess::$user);
1440 throw new RestException(500,
"Error deleting attribute value");
1456 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1457 throw new RestException(401);
1462 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1464 if (count($return) == 0) {
1465 throw new RestException(404,
'Attribute values not found');
1468 foreach ($return as $key => $val) {
1469 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1487 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1488 throw new RestException(401);
1496 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1497 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->
db->escape($ref).
"')";
1501 while ($result = $this->
db->fetch_object(
$resql)) {
1503 $tmp->fk_product_attribute = $result->fk_product_attribute;
1504 $tmp->id = $result->rowid;
1505 $tmp->ref = $result->ref;
1506 $tmp->value = $result->value;
1508 $return[] = $this->_cleanObjectDatas($tmp);
1529 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1530 throw new RestException(401);
1533 if (empty($ref) || empty($value)) {
1534 throw new RestException(401);
1538 $objectval->fk_product_attribute = ((int) $id);
1539 $objectval->ref = $ref;
1540 $objectval->value = $value;
1542 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1543 return $objectval->id;
1545 throw new RestException(500,
"Error creating new attribute value");
1562 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1563 throw new RestException(401);
1567 $result = $objectval->fetch((
int) $id);
1570 throw new RestException(404,
'Attribute value not found');
1571 } elseif ($result < 0) {
1572 throw new RestException(500,
"Error fetching attribute value");
1575 foreach ($request_data as $field => $value) {
1576 if ($field ==
'rowid') {
1579 $objectval->$field = $value;
1582 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1583 $result = $objectval->fetch((
int) $id);
1585 throw new RestException(404,
'Attribute not found');
1586 } elseif ($result < 0) {
1587 throw new RestException(500,
"Error fetching attribute");
1592 throw new RestException(500,
"Error updating attribute");
1608 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1609 throw new RestException(401);
1613 $objectval->id = (int) $id;
1615 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1618 throw new RestException(500,
"Error deleting attribute value");
1635 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1636 throw new RestException(401);
1640 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1642 foreach ($combinations as $key => $combination) {
1644 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1645 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1647 if ($includestock==1 && DolibarrApiAccess::$user->rights->stock->lire) {
1649 $productModel->fetch((
int) $combination->fk_product_child);
1650 $productModel->load_stock();
1651 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1655 return $combinations;
1671 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1672 throw new RestException(401);
1675 $result = $this->product->fetch(
'', $ref);
1677 throw new RestException(404,
'Product not found');
1681 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1683 foreach ($combinations as $key => $combination) {
1685 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1686 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1689 return $combinations;
1712 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1714 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1715 throw new RestException(401);
1718 if (empty($id) || empty($features) || !is_array($features)) {
1719 throw new RestException(401);
1722 $weight_impact =
price2num($weight_impact);
1723 $price_impact =
price2num($price_impact);
1727 foreach ($features as $id_attr => $id_value) {
1728 if ($prodattr->fetch((
int) $id_attr) < 0) {
1729 throw new RestException(401);
1731 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1732 throw new RestException(401);
1736 $result = $this->product->fetch((
int) $id);
1738 throw new RestException(404,
'Product not found');
1743 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1747 throw new RestException(500,
"Error creating new product variant");
1771 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1772 throw new RestException(401);
1775 if (empty($ref) || empty($features) || !is_array($features)) {
1776 throw new RestException(401);
1779 $weight_impact =
price2num($weight_impact);
1780 $price_impact =
price2num($price_impact);
1784 foreach ($features as $id_attr => $id_value) {
1785 if ($prodattr->fetch((
int) $id_attr) < 0) {
1786 throw new RestException(404);
1788 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1789 throw new RestException(404);
1793 $result = $this->product->fetch(
'', trim($ref));
1795 throw new RestException(404,
'Product not found');
1799 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1800 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1804 throw new RestException(500,
"Error creating new product variant");
1807 return $prodcomb->id;
1825 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1826 throw new RestException(401);
1830 $prodcomb->fetch((
int) $id);
1832 foreach ($request_data as $field => $value) {
1833 if ($field ==
'rowid') {
1836 $prodcomb->$field = $value;
1839 $result = $prodcomb->update(DolibarrApiAccess::$user);
1843 throw new RestException(500,
"Error editing variant");
1859 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1860 throw new RestException(401);
1864 $prodcomb->id = (int) $id;
1865 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1867 throw new RestException(500,
"Error deleting variant");
1886 public function getStock($id, $selected_warehouse_id = null)
1889 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1890 throw new RestException(401);
1894 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1897 $product_model =
new Product($this->
db);
1898 $product_model->fetch($id);
1899 $product_model->load_stock();
1901 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1902 if ($selected_warehouse_id) {
1903 foreach ($stockData as $warehouse_id => $warehouse) {
1904 if ($warehouse_id != $selected_warehouse_id) {
1905 unset($stockData[$warehouse_id]);
1910 if (empty($stockData)) {
1911 throw new RestException(404,
'No stock found');
1914 return [
'stock_warehouses'=>$stockData];
1927 $object = parent::_cleanObjectDatas($object);
1929 unset($object->statut);
1931 unset($object->regeximgext);
1932 unset($object->price_by_qty);
1933 unset($object->prices_by_qty_id);
1934 unset($object->libelle);
1935 unset($object->product_id_already_linked);
1936 unset($object->reputations);
1938 unset($object->name);
1939 unset($object->firstname);
1940 unset($object->lastname);
1941 unset($object->civility_id);
1942 unset($object->contact);
1943 unset($object->contact_id);
1944 unset($object->thirdparty);
1945 unset($object->user);
1946 unset($object->origin);
1947 unset($object->origin_id);
1948 unset($object->fourn_pu);
1949 unset($object->fourn_price_base_type);
1950 unset($object->fourn_socid);
1951 unset($object->ref_fourn);
1952 unset($object->ref_supplier);
1953 unset($object->product_fourn_id);
1954 unset($object->fk_project);
1956 unset($object->mode_reglement_id);
1957 unset($object->cond_reglement_id);
1958 unset($object->demand_reason_id);
1959 unset($object->transport_mode_id);
1960 unset($object->cond_reglement);
1961 unset($object->shipping_method_id);
1962 unset($object->model_pdf);
1963 unset($object->note);
1965 unset($object->nbphoto);
1966 unset($object->recuperableonly);
1967 unset($object->multiprices_recuperableonly);
1968 unset($object->tva_npr);
1969 unset($object->lines);
1970 unset($object->fk_bank);
1971 unset($object->fk_account);
1973 unset($object->supplierprices);
1975 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1976 unset($object->stock_reel);
1977 unset($object->stock_theorique);
1978 unset($object->stock_warehouse);
1994 foreach (Products::$FIELDS as $field) {
1995 if (!isset($data[$field])) {
1996 throw new RestException(400,
"$field field missing");
1998 $product[$field] = $data[$field];
2022 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2024 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2025 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2028 $id = (empty($id) ? 0 : $id);
2030 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2031 throw new RestException(403);
2034 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2036 throw new RestException(404,
'Product not found');
2040 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2043 if ($includestockdata && DolibarrApiAccess::$user->rights->stock->lire) {
2044 $this->product->load_stock();
2046 if (is_array($this->product->stock_warehouse)) {
2047 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2048 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2049 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2050 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2057 if ($includesubproducts) {
2058 $childsArbo = $this->product->getChildsArbo($id, 1);
2060 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2062 foreach ($childsArbo as $values) {
2063 $childs[] = array_combine($keys, $values);
2066 $this->product->sousprods = $childs;
2069 if ($includeparentid) {
2071 $this->product->fk_product_parent = null;
2072 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2073 $this->product->fk_product_parent = $fk_product_parent;
2077 if ($includeifobjectisused) {
2078 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2081 return $this->_cleanObjectDatas($this->product);
File of class to manage predefined price products or services by customer.
getAttributeById($id)
Get attribute by ID.
putVariant($id, $request_data=null)
Put product variants.
getAttributeValuesByRef($ref)
Get all values for an attribute ref.
deleteVariant($id)
Delete product variants.
getCategories($id, $sortfield="s.rowid", $sortorder= 'ASC', $limit=0, $page=0)
Get categories for a product.
getAttributesByRef($ref)
Get attributes by ref.
_cleanObjectDatas($object)
Clean sensible object datas.
getAttributes($sortfield="t.ref", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
Get attributes.
$conf db
API class for accounts.
Class to manage products or services.
getByRef($ref, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref.
getPurchasePrices($id, $ref= '', $ref_ext= '', $barcode= '')
Get purchase prices for a product.
getCustomerPricesPerQuantity($id)
Get prices per quantity for a product.
putAttributeValue($id, $request_data)
Update attribute value.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for properties) With native = 0: P...
getStock($id, $selected_warehouse_id=null)
Get stock data for the product id given.
_fetch($id, $ref= '', $ref_ext= '', $barcode= '', $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includeifobjectisused=false, $includetrans=false)
Get properties of 1 product object.
Class ProductAttributeValue Used to represent a product attribute value.
__construct()
Constructor.
addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference= '', $ref_ext= '')
Add variant.
Class to manage suppliers.
Class ProductAttribute Used to represent a product attribute.
getAttributeValueById($id)
Get attribute value by id.
putAttributes($id, $request_data=null)
Update attributes by id.
_checkFilters($sqlfilters, &$error= '')
Return if a $sqlfilters parameter is valid.
deletePurchasePrice($id, $priceid)
Delete purchase price for a product.
put($id, $request_data=null)
Update product.
getAttributesByRefExt($ref_ext)
Get attributes by ref_ext.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
getByRefExt($ref_ext, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref_ext.
Class to manage categories.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
getAttributeValueByRef($id, $ref)
Get attribute value by ref.
deleteAttributes($id)
Delete attributes by id.
getSubproducts($id)
Get the list of subproducts of the product.
addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features)
Add variant by product ref.
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.
post($request_data=null)
Create product object.
Class ProductCombination Used to represent a product combination.
getCustomerPricesPerCustomer($id, $thirdparty_id= '')
Get prices per customer for a product.
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check access by user to a given resource.
Class ProductCombination2ValuePair Used to represent the relation between a product combination...
addSubproducts($id, $subproduct_id, $qty, $incdec=1)
Add subproduct.
delSubproducts($id, $subproduct_id)
Remove subproduct.
_validate($data)
Validate fields before create or update object.
deleteAttributeValueByRef($id, $ref)
Delete attribute value by ref.
getCustomerPricesPerSegment($id)
Get prices per segment for a product.
getAttributeValues($id)
Get all values for an attribute id.
getVariantsByProdRef($ref)
Get product variants by Product ref.
addAttributeValue($id, $ref, $value)
Add attribute value.
getVariants($id, $includestock=0)
Get product variants.
deleteAttributeValueById($id)
Delete attribute value by id.
addAttributes($ref, $label, $ref_ext= '')
Add attributes.
getByBarcode($barcode, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by barcode.
sanitizeVal($out= '', $check= 'alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
Class to manage predefined suppliers products.
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.
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.