19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
40 public static $FIELDS = array(
53 public $productsupplier;
63 $this->product =
new Product($this->db);
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);
179 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, $properties =
'')
183 if (!DolibarrApiAccess::$user->rights->produit->lire) {
184 throw new RestException(403);
189 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
191 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
192 $sql .=
" FROM ".$this->db->prefix().
"product as t";
193 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product_extrafields AS ef ON ef.fk_object = t.rowid";
195 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
197 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
199 if ($variant_filter == 1) {
200 $sql .=
' AND t.rowid not in (select distinct fk_product_parent from '.$this->db->prefix().
'product_attribute_combination)';
201 $sql .=
' AND t.rowid not in (select distinct fk_product_child from '.$this->db->prefix().
'product_attribute_combination)';
203 if ($variant_filter == 2) {
204 $sql .=
' AND t.rowid in (select distinct fk_product_parent from '.$this->db->prefix().
'product_attribute_combination)';
206 if ($variant_filter == 3) {
207 $sql .=
' AND t.rowid in (select distinct fk_product_child from '.$this->db->prefix().
'product_attribute_combination)';
212 $sql .=
" AND c.fk_categorie = ".((int) $category);
213 $sql .=
" AND c.fk_product = t.rowid";
217 $sql .=
" AND t.fk_product_type = 0";
218 } elseif ($mode == 2) {
220 $sql .=
" AND t.fk_product_type = 1";
228 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
233 $sqlTotals = str_replace(
'SELECT t.rowid, t.ref, t.ref_ext',
'SELECT count(t.rowid) as total', $sql);
235 $sql .= $this->db->order($sortfield, $sortorder);
240 $offset = $limit * $page;
242 $sql .= $this->db->plimit($limit + 1, $offset);
245 $result = $this->db->query($sql);
247 $num = $this->db->num_rows($result);
248 $min = min($num, ($limit <= 0 ? $num : $limit));
251 $obj = $this->db->fetch_object($result);
253 $product_static =
new Product($this->db);
254 if ($product_static->fetch($obj->rowid)) {
255 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
256 $product_static->load_stock();
258 if (is_array($product_static->stock_warehouse)) {
259 foreach ($product_static->stock_warehouse as $keytmp => $valtmp) {
260 if (isset($product_static->stock_warehouse[$keytmp]->detail_batch) && is_array($product_static->stock_warehouse[$keytmp]->detail_batch)) {
261 foreach ($product_static->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
262 unset($product_static->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
273 $obj_ret[] = $obj->rowid;
278 throw new RestException(503,
'Error when retrieve product list : '.$this->db->lasterror());
282 if ($pagination_data) {
283 $totalsResult = $this->db->query($sqlTotals);
284 $total = $this->db->fetch_object($totalsResult)->total;
289 $obj_ret[
'data'] = $tmp;
290 $obj_ret[
'pagination'] = array(
291 'total' => (
int) $total,
293 'page_count' => ceil((
int) $total/$limit),
307 public function post($request_data =
null)
309 if (!DolibarrApiAccess::$user->rights->produit->creer) {
310 throw new RestException(401);
313 $result = $this->
_validate($request_data);
315 foreach ($request_data as $field => $value) {
316 if ($field ===
'caller') {
318 $this->product->context[
'caller'] = $request_data[
'caller'];
322 $this->product->$field = $value;
324 if ($this->product->create(DolibarrApiAccess::$user) < 0) {
325 throw new RestException(500,
"Error creating product", array_merge(array($this->product->error), $this->product->errors));
328 return $this->product->id;
342 public function put($id, $request_data =
null)
346 if (!DolibarrApiAccess::$user->rights->produit->creer) {
347 throw new RestException(401);
350 $result = $this->product->fetch($id);
352 throw new RestException(404,
'Product not found');
356 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
361 foreach ($request_data as $field => $value) {
362 if ($field ==
'id') {
365 if ($field ==
'stock_reel') {
366 throw new RestException(400,
'Stock reel cannot be updated here. Use the /stockmovements endpoint instead');
368 if ($field ===
'caller') {
370 $this->product->context[
'caller'] = $request_data[
'caller'];
374 $this->product->$field = $value;
378 if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
382 $result = $this->product->update($id, DolibarrApiAccess::$user, 1,
'update', $updatetype);
387 $pricemodified =
false;
388 if ($this->product->price_base_type != $oldproduct->price_base_type) {
389 $pricemodified =
true;
391 if ($this->product->tva_tx != $oldproduct->tva_tx) {
392 $pricemodified =
true;
394 if ($this->product->tva_npr != $oldproduct->tva_npr) {
395 $pricemodified =
true;
397 if ($this->product->default_vat_code != $oldproduct->default_vat_code) {
398 $pricemodified =
true;
401 if ($this->product->price_base_type ==
'TTC') {
402 if ($this->product->price_ttc != $oldproduct->price_ttc) {
403 $pricemodified =
true;
405 if ($this->product->price_min_ttc != $oldproduct->price_min_ttc) {
406 $pricemodified =
true;
409 if ($this->product->price != $oldproduct->price) {
410 $pricemodified =
true;
412 if ($this->product->price_min != $oldproduct->price_min) {
413 $pricemodified =
true;
418 if ($pricemodified) {
419 $newvat = $this->product->tva_tx;
420 $newnpr = $this->product->tva_npr;
421 $newvatsrccode = $this->product->default_vat_code;
423 $newprice = $this->product->price;
424 $newpricemin = $this->product->price_min;
425 if ($this->product->price_base_type ==
'TTC') {
426 $newprice = $this->product->price_ttc;
427 $newpricemin = $this->product->price_min_ttc;
430 $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode);
435 throw new RestException(500,
"Error updating product", array_merge(array($this->product->error), $this->product->errors));
438 return $this->
get($id);
447 public function delete($id)
449 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
450 throw new RestException(401);
452 $result = $this->product->fetch($id);
454 throw new RestException(404,
'Product not found');
458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
463 $user = DolibarrApiAccess::$user;
465 $res = $this->product->delete(DolibarrApiAccess::$user);
467 throw new RestException(500,
"Can't delete, error occurs");
468 } elseif ($res == 0) {
469 throw new RestException(409,
"Can't delete, that product is probably used");
475 'message' =>
'Object deleted'
494 if (!DolibarrApiAccess::$user->rights->produit->lire) {
495 throw new RestException(401);
499 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
502 $childsArbo = $this->product->getChildsArbo($id, 1);
504 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
506 foreach ($childsArbo as $values) {
507 $childs[] = array_combine($keys, $values);
532 if (!DolibarrApiAccess::$user->rights->produit->creer) {
533 throw new RestException(401);
537 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
542 throw new RestException(500,
"Error adding product child");
562 if (!DolibarrApiAccess::$user->rights->produit->creer) {
563 throw new RestException(401);
567 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
570 $result = $this->product->del_sousproduit($id, $subproduct_id);
572 throw new RestException(500,
"Error while removing product child");
591 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
593 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
594 throw new RestException(401);
599 $result = $categories->getListForItem($id,
'product', $sortfield, $sortorder, $limit, $page);
602 throw new RestException(503,
'Error when retrieve category list : '.join(
',', array_merge(array($categories->error), $categories->errors)));
621 if (!DolibarrApiAccess::$user->rights->produit->lire) {
622 throw new RestException(401);
626 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
629 $result = $this->product->fetch($id);
631 throw new RestException(404,
'Product not found');
635 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
639 'multiprices'=>$this->product->multiprices,
640 'multiprices_inc_tax'=>$this->product->multiprices_ttc,
641 'multiprices_min'=>$this->product->multiprices_min,
642 'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
643 'multiprices_vat'=>$this->product->multiprices_tva_tx,
644 'multiprices_base_type'=>$this->product->multiprices_base_type,
663 if (!DolibarrApiAccess::$user->rights->produit->lire) {
664 throw new RestException(401);
668 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
671 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
672 if ($socid > 0 && $socid != $thirdparty_id) {
673 throw new RestException(401,
'Getting prices for all customers or for the customer ID '.$thirdparty_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
676 $result = $this->product->fetch($id);
678 throw new RestException(404,
'Product not found');
682 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
685 $filter[
't.fk_product'] = $id;
686 if ($thirdparty_id) {
687 $filter[
't.fk_soc'] = $thirdparty_id;
689 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
692 if (empty($prodcustprice->lines)) {
693 throw new RestException(404,
'Prices not found');
696 return $prodcustprice->lines;
712 if (!DolibarrApiAccess::$user->rights->produit->lire) {
713 throw new RestException(401);
717 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
720 $result = $this->product->fetch($id);
722 throw new RestException(404,
'Product not found');
726 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
730 'prices_by_qty'=>$this->product->prices_by_qty[0],
731 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
768 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)
770 if (!DolibarrApiAccess::$user->rights->produit->creer) {
771 throw new RestException(401);
774 $result = $this->productsupplier->fetch($id);
776 throw new RestException(404,
'Product not found');
780 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
783 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
784 if ($socid > 0 && $socid != $fourn_id) {
785 throw new RestException(401,
'Adding purchase price for the supplier ID '.$fourn_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
788 $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty);
790 throw new RestException(500,
"Error adding supplier to product : ".$this->db->lasterror());
794 $result = $fourn->fetch($fourn_id);
796 throw new RestException(404,
'Supplier not found');
800 $ref_fourn =
sanitizeVal($ref_fourn,
'alphanohtml');
801 $desc_fourn =
sanitizeVal($desc_fourn,
'restricthtml');
804 $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);
807 throw new RestException(500,
"Error updating buy price : ".$this->db->lasterror());
809 return (
int) $this->productsupplier->product_fourn_price_id;
828 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
829 throw new RestException(401);
831 $result = $this->productsupplier->fetch($id);
833 throw new RestException(404,
'Product not found');
837 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
842 $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid);
845 return $resultsupplier;
863 public function getSupplierProducts($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters =
'')
867 if (!DolibarrApiAccess::$user->rights->produit->lire) {
868 throw new RestException(401);
874 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
876 if ($supplier != $socid || empty($supplier)) {
877 throw new RestException(401,
'As an external user, you can request only for your supplier id = '.$socid);
881 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
882 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product AS t LEFT JOIN ".MAIN_DB_PREFIX.
"product_extrafields AS ef ON (ef.fk_object = t.rowid)";
885 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
887 $sql .=
", ".$this->db->prefix().
"product_fournisseur_price as s";
889 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
892 $sql .=
" AND s.fk_soc = ".((int) $supplier);
895 $sql .=
" AND s.fk_soc = ".((int) $socid);
897 $sql .=
" AND s.fk_product = t.rowid";
900 $sql .=
" AND c.fk_categorie = ".((int) $category);
901 $sql .=
" AND c.fk_product = t.rowid";
905 $sql .=
" AND t.fk_product_type = 0";
906 } elseif ($mode == 2) {
908 $sql .=
" AND t.fk_product_type = 1";
915 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
919 $sql .= $this->db->order($sortfield, $sortorder);
924 $offset = $limit * $page;
925 $sql .= $this->db->plimit($limit + 1, $offset);
927 $result = $this->db->query($sql);
929 $num = $this->db->num_rows($result);
930 $min = min($num, ($limit <= 0 ? $num : $limit));
933 $obj = $this->db->fetch_object($result);
936 $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid,
'',
'', 0, 0);
937 foreach ($product_fourn_list as $tmpobj) {
942 $obj_ret[$obj->rowid] = $product_fourn_list;
947 throw new RestException(503,
'Error when retrieve product list : '.$this->db->lasterror());
974 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
975 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
978 $id = (empty($id) ? 0 : $id);
980 if (!DolibarrApiAccess::$user->rights->produit->lire) {
981 throw new RestException(403);
984 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
986 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
988 throw new RestException(404,
'Product not found');
992 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
995 $product_fourn_list = array();
999 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
1002 foreach ($product_fourn_list as $tmpobj) {
1026 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
1028 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1029 throw new RestException(401);
1032 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1033 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1034 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1040 if ($errormessage) {
1041 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
1045 $sql .= $this->db->order($sortfield, $sortorder);
1050 $offset = $limit * $page;
1052 $sql .= $this->db->plimit($limit, $offset);
1055 $resql = $this->db->query($sql);
1058 throw new RestException(503,
'Error when retrieving product attribute list : '.$this->db->lasterror());
1062 while ($obj = $this->db->fetch_object($resql)) {
1064 $tmp->id = $obj->rowid;
1065 $tmp->ref = $obj->ref;
1066 $tmp->ref_ext = $obj->ref_ext;
1067 $tmp->label = $obj->label;
1068 $tmp->position = $obj->position;
1069 $tmp->entity = $obj->entity;
1090 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1091 throw new RestException(401);
1095 $result = $prodattr->fetch((
int) $id);
1098 throw new RestException(404,
"Product attribute not found");
1101 $fields = [
"id",
"ref",
"ref_ext",
"label",
"position",
"entity"];
1103 foreach ($prodattr as $field => $value) {
1104 if (!in_array($field, $fields)) {
1105 unset($prodattr->{$field});
1109 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1110 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1111 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1113 $resql = $this->db->query($sql);
1114 $obj = $this->db->fetch_object($resql);
1115 $prodattr->is_used_by_products = (int) $obj->nb;
1133 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1134 throw new RestException(401);
1139 $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').
")";
1141 $query = $this->db->query($sql);
1143 if (!$this->db->num_rows($query)) {
1144 throw new RestException(404);
1147 $result = $this->db->fetch_object($query);
1150 $attr[
'id'] = $result->rowid;
1151 $attr[
'ref'] = $result->ref;
1152 $attr[
'ref_ext'] = $result->ref_ext;
1153 $attr[
'label'] = $result->label;
1154 $attr[
'rang'] = $result->position;
1155 $attr[
'position'] = $result->position;
1156 $attr[
'entity'] = $result->entity;
1158 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1159 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1160 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1162 $resql = $this->db->query($sql);
1163 $obj = $this->db->fetch_object($resql);
1165 $attr[
"is_used_by_products"] = (int) $obj->nb;
1183 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1184 throw new RestException(401);
1187 $ref_ext = trim($ref_ext);
1189 $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').
")";
1191 $query = $this->db->query($sql);
1193 if (!$this->db->num_rows($query)) {
1194 throw new RestException(404);
1197 $result = $this->db->fetch_object($query);
1200 $attr[
'id'] = $result->rowid;
1201 $attr[
'ref'] = $result->ref;
1202 $attr[
'ref_ext'] = $result->ref_ext;
1203 $attr[
'label'] = $result->label;
1204 $attr[
'rang'] = $result->position;
1205 $attr[
'position'] = $result->position;
1206 $attr[
'entity'] = $result->entity;
1208 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1209 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1210 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1212 $resql = $this->db->query($sql);
1213 $obj = $this->db->fetch_object($resql);
1215 $attr[
"is_used_by_products"] = (int) $obj->nb;
1235 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1236 throw new RestException(401);
1240 $prodattr->label = $label;
1241 $prodattr->ref = $ref;
1242 $prodattr->ref_ext = $ref_ext;
1244 $resid = $prodattr->create(DolibarrApiAccess::$user);
1246 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 if ($field ===
'caller') {
1286 $prodattr->context[
'caller'] = $request_data[
'caller'];
1290 $prodattr->$field = $value;
1293 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1294 $result = $prodattr->fetch((
int) $id);
1296 throw new RestException(404,
'Attribute not found');
1297 } elseif ($result < 0) {
1298 throw new RestException(500,
"Error fetching attribute");
1300 return $this->_cleanObjectDatas($prodattr);
1303 throw new RestException(500,
"Error updating attribute");
1319 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1320 throw new RestException(401);
1324 $prodattr->id = (int) $id;
1325 $result = $prodattr->delete(DolibarrApiAccess::$user);
1328 throw new RestException(500,
"Error deleting attribute");
1347 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1348 throw new RestException(401);
1351 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1353 $query = $this->db->query($sql);
1356 throw new RestException(401);
1359 if (!$this->db->num_rows($query)) {
1360 throw new RestException(404,
'Attribute value not found');
1363 $result = $this->db->fetch_object($query);
1366 $attrval[
'id'] = $result->rowid;
1367 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1368 $attrval[
'ref'] = $result->ref;
1369 $attrval[
'value'] = $result->value;
1388 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1389 throw new RestException(401);
1394 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1395 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1397 $query = $this->db->query($sql);
1400 throw new RestException(401);
1403 if (!$this->db->num_rows($query)) {
1404 throw new RestException(404,
'Attribute value not found');
1407 $result = $this->db->fetch_object($query);
1410 $attrval[
'id'] = $result->rowid;
1411 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1412 $attrval[
'ref'] = $result->ref;
1413 $attrval[
'value'] = $result->value;
1431 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1432 throw new RestException(401);
1437 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1438 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1439 $query = $this->db->query($sql);
1442 throw new RestException(401);
1445 if (!$this->db->num_rows($query)) {
1446 throw new RestException(404,
'Attribute value not found');
1449 $result = $this->db->fetch_object($query);
1452 $attrval->id = $result->rowid;
1453 $result = $attrval->delete(DolibarrApiAccess::$user);
1458 throw new RestException(500,
"Error deleting attribute value");
1474 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1475 throw new RestException(401);
1480 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1482 if (count($return) == 0) {
1483 throw new RestException(404,
'Attribute values not found');
1486 foreach ($return as $key => $val) {
1487 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1505 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1506 throw new RestException(401);
1514 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1515 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->db->escape($ref).
"')";
1517 $resql = $this->db->query($sql);
1519 while ($result = $this->db->fetch_object($resql)) {
1521 $tmp->fk_product_attribute = $result->fk_product_attribute;
1522 $tmp->id = $result->rowid;
1523 $tmp->ref = $result->ref;
1524 $tmp->value = $result->value;
1526 $return[] = $this->_cleanObjectDatas($tmp);
1547 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1548 throw new RestException(401);
1551 if (empty($ref) || empty($value)) {
1552 throw new RestException(401);
1556 $objectval->fk_product_attribute = ((int) $id);
1557 $objectval->ref = $ref;
1558 $objectval->value = $value;
1560 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1561 return $objectval->id;
1563 throw new RestException(500,
"Error creating new attribute value");
1580 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1581 throw new RestException(401);
1585 $result = $objectval->fetch((
int) $id);
1588 throw new RestException(404,
'Attribute value not found');
1589 } elseif ($result < 0) {
1590 throw new RestException(500,
"Error fetching attribute value");
1593 foreach ($request_data as $field => $value) {
1594 if ($field ==
'rowid') {
1597 if ($field ===
'caller') {
1599 $objectval->context[
'caller'] = $request_data[
'caller'];
1603 $objectval->$field = $value;
1606 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1607 $result = $objectval->fetch((
int) $id);
1609 throw new RestException(404,
'Attribute not found');
1610 } elseif ($result < 0) {
1611 throw new RestException(500,
"Error fetching attribute");
1613 return $this->_cleanObjectDatas($objectval);
1616 throw new RestException(500,
"Error updating attribute");
1632 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1633 throw new RestException(401);
1637 $objectval->id = (int) $id;
1639 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1642 throw new RestException(500,
"Error deleting attribute value");
1659 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1660 throw new RestException(401);
1664 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1666 foreach ($combinations as $key => $combination) {
1668 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1669 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1671 if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1672 $productModel =
new Product($this->db);
1673 $productModel->fetch((
int) $combination->fk_product_child);
1674 $productModel->load_stock($includestock);
1675 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1679 return $combinations;
1695 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1696 throw new RestException(401);
1699 $result = $this->product->fetch(
'', $ref);
1701 throw new RestException(404,
'Product not found');
1705 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1707 foreach ($combinations as $key => $combination) {
1709 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1710 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1713 return $combinations;
1736 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1738 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1739 throw new RestException(401);
1742 if (empty($id) || empty($features) || !is_array($features)) {
1743 throw new RestException(401);
1746 $weight_impact =
price2num($weight_impact);
1747 $price_impact =
price2num($price_impact);
1751 foreach ($features as $id_attr => $id_value) {
1752 if ($prodattr->fetch((
int) $id_attr) < 0) {
1753 throw new RestException(401);
1755 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1756 throw new RestException(401);
1760 $result = $this->product->fetch((
int) $id);
1762 throw new RestException(404,
'Product not found');
1767 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1771 throw new RestException(500,
"Error creating new product variant");
1795 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1796 throw new RestException(401);
1799 if (empty($ref) || empty($features) || !is_array($features)) {
1800 throw new RestException(401);
1803 $weight_impact =
price2num($weight_impact);
1804 $price_impact =
price2num($price_impact);
1808 foreach ($features as $id_attr => $id_value) {
1809 if ($prodattr->fetch((
int) $id_attr) < 0) {
1810 throw new RestException(404);
1812 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1813 throw new RestException(404);
1817 $result = $this->product->fetch(
'', trim($ref));
1819 throw new RestException(404,
'Product not found');
1823 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1824 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1828 throw new RestException(500,
"Error creating new product variant");
1831 return $prodcomb->id;
1849 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1850 throw new RestException(401);
1854 $prodcomb->fetch((
int) $id);
1856 foreach ($request_data as $field => $value) {
1857 if ($field ==
'rowid') {
1860 if ($field ===
'caller') {
1862 $prodcomb->context[
'caller'] = $request_data[
'caller'];
1866 $prodcomb->$field = $value;
1869 $result = $prodcomb->update(DolibarrApiAccess::$user);
1873 throw new RestException(500,
"Error editing variant");
1889 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1890 throw new RestException(401);
1894 $prodcomb->id = (int) $id;
1895 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1897 throw new RestException(500,
"Error deleting variant");
1916 public function getStock($id, $selected_warehouse_id =
null)
1918 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1919 throw new RestException(401);
1923 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1926 $product_model =
new Product($this->db);
1927 $product_model->fetch($id);
1928 $product_model->load_stock();
1930 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1931 if ($selected_warehouse_id) {
1932 foreach ($stockData as $warehouse_id => $warehouse) {
1933 if ($warehouse_id != $selected_warehouse_id) {
1934 unset($stockData[$warehouse_id]);
1939 return array(
'stock_warehouses'=>$stockData);
1952 $object = parent::_cleanObjectDatas($object);
1954 unset($object->statut);
1956 unset($object->regeximgext);
1957 unset($object->price_by_qty);
1958 unset($object->prices_by_qty_id);
1959 unset($object->libelle);
1960 unset($object->product_id_already_linked);
1961 unset($object->reputations);
1963 unset($object->name);
1964 unset($object->firstname);
1965 unset($object->lastname);
1966 unset($object->civility_id);
1967 unset($object->contact);
1968 unset($object->contact_id);
1969 unset($object->thirdparty);
1970 unset($object->user);
1971 unset($object->origin);
1972 unset($object->origin_id);
1973 unset($object->fourn_pu);
1974 unset($object->fourn_price_base_type);
1975 unset($object->fourn_socid);
1976 unset($object->ref_fourn);
1977 unset($object->ref_supplier);
1978 unset($object->product_fourn_id);
1979 unset($object->fk_project);
1981 unset($object->mode_reglement_id);
1982 unset($object->cond_reglement_id);
1983 unset($object->demand_reason_id);
1984 unset($object->transport_mode_id);
1985 unset($object->cond_reglement);
1986 unset($object->shipping_method_id);
1987 unset($object->model_pdf);
1988 unset($object->note);
1990 unset($object->nbphoto);
1991 unset($object->recuperableonly);
1992 unset($object->multiprices_recuperableonly);
1993 unset($object->tva_npr);
1994 unset($object->lines);
1995 unset($object->fk_bank);
1996 unset($object->fk_account);
1998 unset($object->supplierprices);
2000 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
2001 unset($object->stock_reel);
2002 unset($object->stock_theorique);
2003 unset($object->stock_warehouse);
2019 foreach (Products::$FIELDS as $field) {
2020 if (!isset($data[$field])) {
2021 throw new RestException(400,
"$field field missing");
2023 $product[$field] = $data[$field];
2047 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2049 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2050 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2053 $id = (empty($id) ? 0 : $id);
2055 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2056 throw new RestException(403);
2059 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2061 throw new RestException(404,
'Product not found');
2065 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2068 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2069 $this->product->load_stock($includestockdata);
2071 if (is_array($this->product->stock_warehouse)) {
2072 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2073 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2074 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2075 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2082 if ($includesubproducts) {
2083 $childsArbo = $this->product->getChildsArbo($id, 1);
2085 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2087 foreach ($childsArbo as $values) {
2088 $childs[] = array_combine($keys, $values);
2091 $this->product->sousprods = $childs;
2094 if ($includeparentid) {
2096 $this->product->fk_product_parent =
null;
2097 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2098 $this->product->fk_product_parent = $fk_product_parent;
2102 if ($includeifobjectisused) {
2103 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2106 return $this->_cleanObjectDatas($this->product);
Class to manage categories.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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.
File of class to manage predefined price products or services by customer.
Class to manage predefined suppliers products.
Class to manage products or services.
_cleanObjectDatas($object)
Clean sensible object datas.
getAttributes($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
Get attributes.
putAttributeValue($id, $request_data)
Update attribute value.
deleteAttributes($id)
Delete attributes by id.
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.
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, $properties='')
List products.
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.
getAttributesByRef($ref)
Get attributes by ref.
addSubproducts($id, $subproduct_id, $qty, $incdec=1)
Add subproduct.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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.