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 if ($field ==
'array_options' && is_array($value)) {
375 foreach ($value as $index => $val) {
376 $this->product->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->product);
381 $this->product->$field = $this->
_checkValForAPI($field, $value, $this->product);
385 if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
389 $result = $this->product->update($id, DolibarrApiAccess::$user, 1,
'update', $updatetype);
394 $pricemodified =
false;
395 if ($this->product->price_base_type != $oldproduct->price_base_type) {
396 $pricemodified =
true;
398 if ($this->product->tva_tx != $oldproduct->tva_tx) {
399 $pricemodified =
true;
401 if ($this->product->tva_npr != $oldproduct->tva_npr) {
402 $pricemodified =
true;
404 if ($this->product->default_vat_code != $oldproduct->default_vat_code) {
405 $pricemodified =
true;
408 if ($this->product->price_base_type ==
'TTC') {
409 if ($this->product->price_ttc != $oldproduct->price_ttc) {
410 $pricemodified =
true;
412 if ($this->product->price_min_ttc != $oldproduct->price_min_ttc) {
413 $pricemodified =
true;
416 if ($this->product->price != $oldproduct->price) {
417 $pricemodified =
true;
419 if ($this->product->price_min != $oldproduct->price_min) {
420 $pricemodified =
true;
425 if ($pricemodified) {
426 $newvat = $this->product->tva_tx;
427 $newnpr = $this->product->tva_npr;
428 $newvatsrccode = $this->product->default_vat_code;
430 $newprice = $this->product->price;
431 $newpricemin = $this->product->price_min;
432 if ($this->product->price_base_type ==
'TTC') {
433 $newprice = $this->product->price_ttc;
434 $newpricemin = $this->product->price_min_ttc;
437 $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode);
442 throw new RestException(500,
"Error updating product", array_merge(array($this->product->error), $this->product->errors));
445 return $this->
get($id);
454 public function delete($id)
456 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
457 throw new RestException(401);
459 $result = $this->product->fetch($id);
461 throw new RestException(404,
'Product not found');
465 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
470 $user = DolibarrApiAccess::$user;
472 $res = $this->product->delete(DolibarrApiAccess::$user);
474 throw new RestException(500,
"Can't delete, error occurs");
475 } elseif ($res == 0) {
476 throw new RestException(409,
"Can't delete, that product is probably used");
482 'message' =>
'Object deleted'
501 if (!DolibarrApiAccess::$user->rights->produit->lire) {
502 throw new RestException(401);
506 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
509 $childsArbo = $this->product->getChildsArbo($id, 1);
511 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
513 foreach ($childsArbo as $values) {
514 $childs[] = array_combine($keys, $values);
539 if (!DolibarrApiAccess::$user->rights->produit->creer) {
540 throw new RestException(401);
544 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
547 $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
549 throw new RestException(500,
"Error adding product child");
569 if (!DolibarrApiAccess::$user->rights->produit->creer) {
570 throw new RestException(401);
574 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
577 $result = $this->product->del_sousproduit($id, $subproduct_id);
579 throw new RestException(500,
"Error while removing product child");
598 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
600 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
601 throw new RestException(401);
606 $result = $categories->getListForItem($id,
'product', $sortfield, $sortorder, $limit, $page);
609 throw new RestException(503,
'Error when retrieve category list : '.join(
',', array_merge(array($categories->error), $categories->errors)));
628 if (!DolibarrApiAccess::$user->rights->produit->lire) {
629 throw new RestException(401);
633 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
636 $result = $this->product->fetch($id);
638 throw new RestException(404,
'Product not found');
642 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
646 'multiprices'=>$this->product->multiprices,
647 'multiprices_inc_tax'=>$this->product->multiprices_ttc,
648 'multiprices_min'=>$this->product->multiprices_min,
649 'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
650 'multiprices_vat'=>$this->product->multiprices_tva_tx,
651 'multiprices_base_type'=>$this->product->multiprices_base_type,
670 if (!DolibarrApiAccess::$user->rights->produit->lire) {
671 throw new RestException(401);
675 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
678 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
679 if ($socid > 0 && $socid != $thirdparty_id) {
680 throw new RestException(401,
'Getting prices for all customers or for the customer ID '.$thirdparty_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
683 $result = $this->product->fetch($id);
685 throw new RestException(404,
'Product not found');
689 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
692 $filter[
't.fk_product'] = $id;
693 if ($thirdparty_id) {
694 $filter[
't.fk_soc'] = $thirdparty_id;
696 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
699 if (empty($prodcustprice->lines)) {
700 throw new RestException(404,
'Prices not found');
703 return $prodcustprice->lines;
719 if (!DolibarrApiAccess::$user->rights->produit->lire) {
720 throw new RestException(401);
724 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
727 $result = $this->product->fetch($id);
729 throw new RestException(404,
'Product not found');
733 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
737 'prices_by_qty'=>$this->product->prices_by_qty[0],
738 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
775 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)
777 if (!DolibarrApiAccess::$user->rights->produit->creer) {
778 throw new RestException(401);
781 $result = $this->productsupplier->fetch($id);
783 throw new RestException(404,
'Product not found');
787 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
791 if ($socid > 0 && $socid != $fourn_id) {
792 throw new RestException(401,
'Adding purchase price for the supplier ID '.$fourn_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
795 $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty);
797 throw new RestException(500,
"Error adding supplier to product : ".$this->db->lasterror());
801 $result = $fourn->fetch($fourn_id);
803 throw new RestException(404,
'Supplier not found');
807 $ref_fourn =
sanitizeVal($ref_fourn,
'alphanohtml');
808 $desc_fourn =
sanitizeVal($desc_fourn,
'restricthtml');
811 $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);
814 throw new RestException(500,
"Error updating buy price : ".$this->db->lasterror());
816 return (
int) $this->productsupplier->product_fourn_price_id;
835 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
836 throw new RestException(401);
838 $result = $this->productsupplier->fetch($id);
840 throw new RestException(404,
'Product not found');
844 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
849 $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid);
852 return $resultsupplier;
870 public function getSupplierProducts($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters =
'')
874 if (!DolibarrApiAccess::$user->rights->produit->lire) {
875 throw new RestException(401);
881 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
883 if ($supplier != $socid || empty($supplier)) {
884 throw new RestException(401,
'As an external user, you can request only for your supplier id = '.$socid);
888 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
889 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product AS t LEFT JOIN ".MAIN_DB_PREFIX.
"product_extrafields AS ef ON (ef.fk_object = t.rowid)";
892 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
894 $sql .=
", ".$this->db->prefix().
"product_fournisseur_price as s";
896 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
899 $sql .=
" AND s.fk_soc = ".((int) $supplier);
902 $sql .=
" AND s.fk_soc = ".((int) $socid);
904 $sql .=
" AND s.fk_product = t.rowid";
907 $sql .=
" AND c.fk_categorie = ".((int) $category);
908 $sql .=
" AND c.fk_product = t.rowid";
912 $sql .=
" AND t.fk_product_type = 0";
913 } elseif ($mode == 2) {
915 $sql .=
" AND t.fk_product_type = 1";
922 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
926 $sql .= $this->db->order($sortfield, $sortorder);
931 $offset = $limit * $page;
932 $sql .= $this->db->plimit($limit + 1, $offset);
934 $result = $this->db->query($sql);
936 $num = $this->db->num_rows($result);
937 $min = min($num, ($limit <= 0 ? $num : $limit));
940 $obj = $this->db->fetch_object($result);
943 $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid,
'',
'', 0, 0);
944 foreach ($product_fourn_list as $tmpobj) {
949 $obj_ret[$obj->rowid] = $product_fourn_list;
954 throw new RestException(503,
'Error when retrieve product list : '.$this->db->lasterror());
981 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
982 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
985 $id = (empty($id) ? 0 : $id);
987 if (!DolibarrApiAccess::$user->rights->produit->lire) {
988 throw new RestException(403);
991 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
993 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
995 throw new RestException(404,
'Product not found');
999 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1002 $product_fourn_list = array();
1006 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
1009 foreach ($product_fourn_list as $tmpobj) {
1033 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
1035 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1036 throw new RestException(401);
1039 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1040 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1041 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1047 if ($errormessage) {
1048 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
1052 $sql .= $this->db->order($sortfield, $sortorder);
1057 $offset = $limit * $page;
1059 $sql .= $this->db->plimit($limit, $offset);
1062 $resql = $this->db->query($sql);
1065 throw new RestException(503,
'Error when retrieving product attribute list : '.$this->db->lasterror());
1069 while ($obj = $this->db->fetch_object($resql)) {
1071 $tmp->id = $obj->rowid;
1072 $tmp->ref = $obj->ref;
1073 $tmp->ref_ext = $obj->ref_ext;
1074 $tmp->label = $obj->label;
1075 $tmp->position = $obj->position;
1076 $tmp->entity = $obj->entity;
1097 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1098 throw new RestException(401);
1102 $result = $prodattr->fetch((
int) $id);
1105 throw new RestException(404,
"Product attribute not found");
1108 $fields = [
"id",
"ref",
"ref_ext",
"label",
"position",
"entity"];
1110 foreach ($prodattr as $field => $value) {
1111 if (!in_array($field, $fields)) {
1112 unset($prodattr->{$field});
1116 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1117 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1118 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1120 $resql = $this->db->query($sql);
1121 $obj = $this->db->fetch_object($resql);
1122 $prodattr->is_used_by_products = (int) $obj->nb;
1140 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1141 throw new RestException(401);
1146 $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').
")";
1148 $query = $this->db->query($sql);
1150 if (!$this->db->num_rows($query)) {
1151 throw new RestException(404);
1154 $result = $this->db->fetch_object($query);
1157 $attr[
'id'] = $result->rowid;
1158 $attr[
'ref'] = $result->ref;
1159 $attr[
'ref_ext'] = $result->ref_ext;
1160 $attr[
'label'] = $result->label;
1161 $attr[
'rang'] = $result->position;
1162 $attr[
'position'] = $result->position;
1163 $attr[
'entity'] = $result->entity;
1165 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1166 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1167 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1169 $resql = $this->db->query($sql);
1170 $obj = $this->db->fetch_object($resql);
1172 $attr[
"is_used_by_products"] = (int) $obj->nb;
1190 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1191 throw new RestException(401);
1194 $ref_ext = trim($ref_ext);
1196 $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').
")";
1198 $query = $this->db->query($sql);
1200 if (!$this->db->num_rows($query)) {
1201 throw new RestException(404);
1204 $result = $this->db->fetch_object($query);
1207 $attr[
'id'] = $result->rowid;
1208 $attr[
'ref'] = $result->ref;
1209 $attr[
'ref_ext'] = $result->ref_ext;
1210 $attr[
'label'] = $result->label;
1211 $attr[
'rang'] = $result->position;
1212 $attr[
'position'] = $result->position;
1213 $attr[
'entity'] = $result->entity;
1215 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1216 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1217 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1219 $resql = $this->db->query($sql);
1220 $obj = $this->db->fetch_object($resql);
1222 $attr[
"is_used_by_products"] = (int) $obj->nb;
1242 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1243 throw new RestException(401);
1247 $prodattr->label = $label;
1248 $prodattr->ref = $ref;
1249 $prodattr->ref_ext = $ref_ext;
1251 $resid = $prodattr->create(DolibarrApiAccess::$user);
1253 throw new RestException(500,
"Error creating new attribute");
1274 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1275 throw new RestException(401);
1280 $result = $prodattr->fetch((
int) $id);
1282 throw new RestException(404,
'Attribute not found');
1283 } elseif ($result < 0) {
1284 throw new RestException(500,
"Error fetching attribute");
1287 foreach ($request_data as $field => $value) {
1288 if ($field ==
'rowid') {
1291 if ($field ===
'caller') {
1293 $prodattr->context[
'caller'] = $request_data[
'caller'];
1297 $prodattr->$field = $value;
1300 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1301 $result = $prodattr->fetch((
int) $id);
1303 throw new RestException(404,
'Attribute not found');
1304 } elseif ($result < 0) {
1305 throw new RestException(500,
"Error fetching attribute");
1307 return $this->_cleanObjectDatas($prodattr);
1310 throw new RestException(500,
"Error updating attribute");
1326 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1327 throw new RestException(401);
1331 $prodattr->id = (int) $id;
1332 $result = $prodattr->delete(DolibarrApiAccess::$user);
1335 throw new RestException(500,
"Error deleting attribute");
1354 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1355 throw new RestException(401);
1358 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1360 $query = $this->db->query($sql);
1363 throw new RestException(401);
1366 if (!$this->db->num_rows($query)) {
1367 throw new RestException(404,
'Attribute value not found');
1370 $result = $this->db->fetch_object($query);
1373 $attrval[
'id'] = $result->rowid;
1374 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1375 $attrval[
'ref'] = $result->ref;
1376 $attrval[
'value'] = $result->value;
1395 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1396 throw new RestException(401);
1401 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1402 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1404 $query = $this->db->query($sql);
1407 throw new RestException(401);
1410 if (!$this->db->num_rows($query)) {
1411 throw new RestException(404,
'Attribute value not found');
1414 $result = $this->db->fetch_object($query);
1417 $attrval[
'id'] = $result->rowid;
1418 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1419 $attrval[
'ref'] = $result->ref;
1420 $attrval[
'value'] = $result->value;
1438 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1439 throw new RestException(401);
1444 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1445 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1446 $query = $this->db->query($sql);
1449 throw new RestException(401);
1452 if (!$this->db->num_rows($query)) {
1453 throw new RestException(404,
'Attribute value not found');
1456 $result = $this->db->fetch_object($query);
1459 $attrval->id = $result->rowid;
1460 $result = $attrval->delete(DolibarrApiAccess::$user);
1465 throw new RestException(500,
"Error deleting attribute value");
1481 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1482 throw new RestException(401);
1487 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1489 if (count($return) == 0) {
1490 throw new RestException(404,
'Attribute values not found');
1493 foreach ($return as $key => $val) {
1494 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1512 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1513 throw new RestException(401);
1521 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1522 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->db->escape($ref).
"')";
1524 $resql = $this->db->query($sql);
1526 while ($result = $this->db->fetch_object($resql)) {
1528 $tmp->fk_product_attribute = $result->fk_product_attribute;
1529 $tmp->id = $result->rowid;
1530 $tmp->ref = $result->ref;
1531 $tmp->value = $result->value;
1533 $return[] = $this->_cleanObjectDatas($tmp);
1554 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1555 throw new RestException(401);
1558 if (empty($ref) || empty($value)) {
1559 throw new RestException(401);
1563 $objectval->fk_product_attribute = ((int) $id);
1564 $objectval->ref = $ref;
1565 $objectval->value = $value;
1567 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1568 return $objectval->id;
1570 throw new RestException(500,
"Error creating new attribute value");
1587 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1588 throw new RestException(401);
1592 $result = $objectval->fetch((
int) $id);
1595 throw new RestException(404,
'Attribute value not found');
1596 } elseif ($result < 0) {
1597 throw new RestException(500,
"Error fetching attribute value");
1600 foreach ($request_data as $field => $value) {
1601 if ($field ==
'rowid') {
1604 if ($field ===
'caller') {
1606 $objectval->context[
'caller'] = $request_data[
'caller'];
1610 $objectval->$field = $value;
1613 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1614 $result = $objectval->fetch((
int) $id);
1616 throw new RestException(404,
'Attribute not found');
1617 } elseif ($result < 0) {
1618 throw new RestException(500,
"Error fetching attribute");
1620 return $this->_cleanObjectDatas($objectval);
1623 throw new RestException(500,
"Error updating attribute");
1639 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1640 throw new RestException(401);
1644 $objectval->id = (int) $id;
1646 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1649 throw new RestException(500,
"Error deleting attribute value");
1666 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1667 throw new RestException(401);
1671 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1673 foreach ($combinations as $key => $combination) {
1675 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1676 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1678 if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1679 $productModel =
new Product($this->db);
1680 $productModel->fetch((
int) $combination->fk_product_child);
1681 $productModel->load_stock($includestock);
1682 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1686 return $combinations;
1702 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1703 throw new RestException(401);
1706 $result = $this->product->fetch(
'', $ref);
1708 throw new RestException(404,
'Product not found');
1712 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1714 foreach ($combinations as $key => $combination) {
1716 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1717 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1720 return $combinations;
1743 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1745 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1746 throw new RestException(401);
1749 if (empty($id) || empty($features) || !is_array($features)) {
1750 throw new RestException(401);
1753 $weight_impact =
price2num($weight_impact);
1754 $price_impact =
price2num($price_impact);
1758 foreach ($features as $id_attr => $id_value) {
1759 if ($prodattr->fetch((
int) $id_attr) < 0) {
1760 throw new RestException(401);
1762 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1763 throw new RestException(401);
1767 $result = $this->product->fetch((
int) $id);
1769 throw new RestException(404,
'Product not found');
1774 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1778 throw new RestException(500,
"Error creating new product variant");
1802 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1803 throw new RestException(401);
1806 if (empty($ref) || empty($features) || !is_array($features)) {
1807 throw new RestException(401);
1810 $weight_impact =
price2num($weight_impact);
1811 $price_impact =
price2num($price_impact);
1815 foreach ($features as $id_attr => $id_value) {
1816 if ($prodattr->fetch((
int) $id_attr) < 0) {
1817 throw new RestException(404);
1819 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1820 throw new RestException(404);
1824 $result = $this->product->fetch(
'', trim($ref));
1826 throw new RestException(404,
'Product not found');
1830 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1831 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1835 throw new RestException(500,
"Error creating new product variant");
1838 return $prodcomb->id;
1856 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1857 throw new RestException(401);
1861 $prodcomb->fetch((
int) $id);
1863 foreach ($request_data as $field => $value) {
1864 if ($field ==
'rowid') {
1867 if ($field ===
'caller') {
1869 $prodcomb->context[
'caller'] = $request_data[
'caller'];
1873 $prodcomb->$field = $value;
1876 $result = $prodcomb->update(DolibarrApiAccess::$user);
1880 throw new RestException(500,
"Error editing variant");
1896 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1897 throw new RestException(401);
1901 $prodcomb->id = (int) $id;
1902 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1904 throw new RestException(500,
"Error deleting variant");
1923 public function getStock($id, $selected_warehouse_id =
null)
1925 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1926 throw new RestException(401);
1930 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1933 $product_model =
new Product($this->db);
1934 $product_model->fetch($id);
1935 $product_model->load_stock();
1937 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1938 if ($selected_warehouse_id) {
1939 foreach ($stockData as $warehouse_id => $warehouse) {
1940 if ($warehouse_id != $selected_warehouse_id) {
1941 unset($stockData[$warehouse_id]);
1946 return array(
'stock_warehouses'=>$stockData);
1959 $object = parent::_cleanObjectDatas($object);
1961 unset($object->statut);
1963 unset($object->regeximgext);
1964 unset($object->price_by_qty);
1965 unset($object->prices_by_qty_id);
1966 unset($object->libelle);
1967 unset($object->product_id_already_linked);
1968 unset($object->reputations);
1970 unset($object->name);
1971 unset($object->firstname);
1972 unset($object->lastname);
1973 unset($object->civility_id);
1974 unset($object->contact);
1975 unset($object->contact_id);
1976 unset($object->thirdparty);
1977 unset($object->user);
1978 unset($object->origin);
1979 unset($object->origin_id);
1980 unset($object->fourn_pu);
1981 unset($object->fourn_price_base_type);
1982 unset($object->fourn_socid);
1983 unset($object->ref_fourn);
1984 unset($object->ref_supplier);
1985 unset($object->product_fourn_id);
1986 unset($object->fk_project);
1988 unset($object->mode_reglement_id);
1989 unset($object->cond_reglement_id);
1990 unset($object->demand_reason_id);
1991 unset($object->transport_mode_id);
1992 unset($object->cond_reglement);
1993 unset($object->shipping_method_id);
1994 unset($object->model_pdf);
1995 unset($object->note);
1997 unset($object->nbphoto);
1998 unset($object->recuperableonly);
1999 unset($object->multiprices_recuperableonly);
2000 unset($object->tva_npr);
2001 unset($object->lines);
2002 unset($object->fk_bank);
2003 unset($object->fk_account);
2005 unset($object->supplierprices);
2007 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
2008 unset($object->stock_reel);
2009 unset($object->stock_theorique);
2010 unset($object->stock_warehouse);
2026 foreach (Products::$FIELDS as $field) {
2027 if (!isset($data[$field])) {
2028 throw new RestException(400,
"$field field missing");
2030 $product[$field] = $data[$field];
2054 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2056 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2057 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2060 $id = (empty($id) ? 0 : $id);
2062 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2063 throw new RestException(403);
2066 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2068 throw new RestException(404,
'Product not found');
2072 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2075 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2076 $this->product->load_stock($includestockdata);
2078 if (is_array($this->product->stock_warehouse)) {
2079 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2080 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2081 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2082 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2089 if ($includesubproducts) {
2090 $childsArbo = $this->product->getChildsArbo($id, 1);
2092 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2094 foreach ($childsArbo as $values) {
2095 $childs[] = array_combine($keys, $values);
2098 $this->product->sousprods = $childs;
2101 if ($includeparentid) {
2103 $this->product->fk_product_parent =
null;
2104 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2105 $this->product->fk_product_parent = $fk_product_parent;
2109 if ($includeifobjectisused) {
2110 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2113 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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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.