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);
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";
227 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
232 $sqlTotals = str_replace(
'SELECT t.rowid, t.ref, t.ref_ext',
'SELECT count(t.rowid) as total', $sql);
234 $sql .= $this->db->order($sortfield, $sortorder);
239 $offset = $limit * $page;
241 $sql .= $this->db->plimit($limit + 1, $offset);
244 $result = $this->db->query($sql);
246 $num = $this->db->num_rows($result);
247 $min = min($num, ($limit <= 0 ? $num : $limit));
250 $obj = $this->db->fetch_object($result);
252 $product_static =
new Product($this->db);
253 if ($product_static->fetch($obj->rowid)) {
254 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
255 $product_static->load_stock();
257 if (is_array($product_static->stock_warehouse)) {
258 foreach ($product_static->stock_warehouse as $keytmp => $valtmp) {
259 if (isset($product_static->stock_warehouse[$keytmp]->detail_batch) && is_array($product_static->stock_warehouse[$keytmp]->detail_batch)) {
260 foreach ($product_static->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
261 unset($product_static->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
272 $obj_ret[] = $obj->rowid;
277 throw new RestException(503,
'Error when retrieve product list : '.$this->db->lasterror());
279 if (!count($obj_ret)) {
280 throw new RestException(404,
'No product found');
284 if ($pagination_data) {
285 $totalsResult = $this->db->query($sqlTotals);
286 $total = $this->db->fetch_object($totalsResult)->total;
291 $obj_ret[
'data'] = $tmp;
292 $obj_ret[
'pagination'] = array(
293 'total' => (
int) $total,
295 'page_count' => ceil((
int) $total/$limit),
309 public function post($request_data =
null)
311 if (!DolibarrApiAccess::$user->rights->produit->creer) {
312 throw new RestException(401);
315 $result = $this->
_validate($request_data);
317 foreach ($request_data as $field => $value) {
318 $this->product->$field = $value;
320 if ($this->product->create(DolibarrApiAccess::$user) < 0) {
321 throw new RestException(500,
"Error creating product", array_merge(array($this->product->error), $this->product->errors));
324 return $this->product->id;
338 public function put($id, $request_data =
null)
342 if (!DolibarrApiAccess::$user->rights->produit->creer) {
343 throw new RestException(401);
346 $result = $this->product->fetch($id);
348 throw new RestException(404,
'Product not found');
352 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
357 foreach ($request_data as $field => $value) {
358 if ($field ==
'id') {
361 if ($field ==
'stock_reel') {
362 throw new RestException(400,
'Stock reel cannot be updated here. Use the /stockmovements endpoint instead');
364 if ($field ==
'array_options' && is_array($value)) {
365 foreach ($value as $index => $val) {
366 $this->product->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->product);
370 $this->product->$field = $value;
374 if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
378 $result = $this->product->update($id, DolibarrApiAccess::$user, 1,
'update', $updatetype);
381 if ($result > 0 && !empty($conf->global->PRODUCT_PRICE_UNIQ)) {
383 $pricemodified =
false;
384 if ($this->product->price_base_type != $oldproduct->price_base_type) {
385 $pricemodified =
true;
387 if ($this->product->tva_tx != $oldproduct->tva_tx) {
388 $pricemodified =
true;
390 if ($this->product->tva_npr != $oldproduct->tva_npr) {
391 $pricemodified =
true;
393 if ($this->product->default_vat_code != $oldproduct->default_vat_code) {
394 $pricemodified =
true;
397 if ($this->product->price_base_type ==
'TTC') {
398 if ($this->product->price_ttc != $oldproduct->price_ttc) {
399 $pricemodified =
true;
401 if ($this->product->price_min_ttc != $oldproduct->price_min_ttc) {
402 $pricemodified =
true;
405 if ($this->product->price != $oldproduct->price) {
406 $pricemodified =
true;
408 if ($this->product->price_min != $oldproduct->price_min) {
409 $pricemodified =
true;
414 if ($pricemodified) {
415 $newvat = $this->product->tva_tx;
416 $newnpr = $this->product->tva_npr;
417 $newvatsrccode = $this->product->default_vat_code;
419 $newprice = $this->product->price;
420 $newpricemin = $this->product->price_min;
421 if ($this->product->price_base_type ==
'TTC') {
422 $newprice = $this->product->price_ttc;
423 $newpricemin = $this->product->price_min_ttc;
426 $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode);
431 throw new RestException(500,
"Error updating product", array_merge(array($this->product->error), $this->product->errors));
434 return $this->
get($id);
443 public function delete($id)
445 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
446 throw new RestException(401);
448 $result = $this->product->fetch($id);
450 throw new RestException(404,
'Product not found');
454 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 $user = DolibarrApiAccess::$user;
461 $res = $this->product->delete(DolibarrApiAccess::$user);
463 throw new RestException(500,
"Can't delete, error occurs");
464 } elseif ($res == 0) {
465 throw new RestException(409,
"Can't delete, that product is probably used");
471 'message' =>
'Object deleted'
490 if (!DolibarrApiAccess::$user->rights->produit->lire) {
491 throw new RestException(401);
495 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
498 $childsArbo = $this->product->getChildsArbo($id, 1);
500 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
502 foreach ($childsArbo as $values) {
503 $childs[] = array_combine($keys, $values);
528 if (!DolibarrApiAccess::$user->rights->produit->creer) {
529 throw new RestException(401);
533 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
536 $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
538 throw new RestException(500,
"Error adding product child");
558 if (!DolibarrApiAccess::$user->rights->produit->creer) {
559 throw new RestException(401);
563 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
566 $result = $this->product->del_sousproduit($id, $subproduct_id);
568 throw new RestException(500,
"Error while removing product child");
587 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
589 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
590 throw new RestException(401);
595 $result = $categories->getListForItem($id,
'product', $sortfield, $sortorder, $limit, $page);
597 if (empty($result)) {
598 throw new RestException(404,
'No category found');
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);
625 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
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);
667 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
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);
716 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
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());
949 if (!count($obj_ret)) {
950 throw new RestException(404,
'No product found');
976 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
977 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
980 $id = (empty($id) ? 0 : $id);
982 if (!DolibarrApiAccess::$user->rights->produit->lire) {
983 throw new RestException(403);
986 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
988 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
990 throw new RestException(404,
'Product not found');
994 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
997 $product_fourn_list = array();
1001 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
1004 foreach ($product_fourn_list as $tmpobj) {
1027 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
1029 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1030 throw new RestException(401);
1033 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1034 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1035 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1041 if ($errormessage) {
1042 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
1046 $sql .= $this->db->order($sortfield, $sortorder);
1051 $offset = $limit * $page;
1053 $sql .= $this->db->plimit($limit, $offset);
1056 $resql = $this->db->query($sql);
1059 throw new RestException(503,
'Error when retrieving product attribute list : '.$this->db->lasterror());
1063 while ($obj = $this->db->fetch_object($resql)) {
1065 $tmp->id = $obj->rowid;
1066 $tmp->ref = $obj->ref;
1067 $tmp->ref_ext = $obj->ref_ext;
1068 $tmp->label = $obj->label;
1069 $tmp->position = $obj->position;
1070 $tmp->entity = $obj->entity;
1075 if (!count($return)) {
1076 throw new RestException(404,
'No product attribute found');
1095 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1096 throw new RestException(401);
1100 $result = $prodattr->fetch((
int) $id);
1103 throw new RestException(404,
"Product attribute not found");
1106 $fields = [
"id",
"ref",
"ref_ext",
"label",
"position",
"entity"];
1108 foreach ($prodattr as $field => $value) {
1109 if (!in_array($field, $fields)) {
1110 unset($prodattr->{$field});
1114 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1115 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1116 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1118 $resql = $this->db->query($sql);
1119 $obj = $this->db->fetch_object($resql);
1120 $prodattr->is_used_by_products = (int) $obj->nb;
1138 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1139 throw new RestException(401);
1144 $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').
")";
1146 $query = $this->db->query($sql);
1148 if (!$this->db->num_rows($query)) {
1149 throw new RestException(404);
1152 $result = $this->db->fetch_object($query);
1155 $attr[
'id'] = $result->rowid;
1156 $attr[
'ref'] = $result->ref;
1157 $attr[
'ref_ext'] = $result->ref_ext;
1158 $attr[
'label'] = $result->label;
1159 $attr[
'rang'] = $result->position;
1160 $attr[
'position'] = $result->position;
1161 $attr[
'entity'] = $result->entity;
1163 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1164 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1165 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1167 $resql = $this->db->query($sql);
1168 $obj = $this->db->fetch_object($resql);
1170 $attr[
"is_used_by_products"] = (int) $obj->nb;
1188 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1189 throw new RestException(401);
1192 $ref_ext = trim($ref_ext);
1194 $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').
")";
1196 $query = $this->db->query($sql);
1198 if (!$this->db->num_rows($query)) {
1199 throw new RestException(404);
1202 $result = $this->db->fetch_object($query);
1205 $attr[
'id'] = $result->rowid;
1206 $attr[
'ref'] = $result->ref;
1207 $attr[
'ref_ext'] = $result->ref_ext;
1208 $attr[
'label'] = $result->label;
1209 $attr[
'rang'] = $result->position;
1210 $attr[
'position'] = $result->position;
1211 $attr[
'entity'] = $result->entity;
1213 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1214 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1215 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1217 $resql = $this->db->query($sql);
1218 $obj = $this->db->fetch_object($resql);
1220 $attr[
"is_used_by_products"] = (int) $obj->nb;
1240 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1241 throw new RestException(401);
1245 $prodattr->label = $label;
1246 $prodattr->ref = $ref;
1247 $prodattr->ref_ext = $ref_ext;
1249 $resid = $prodattr->create(DolibarrApiAccess::$user);
1251 throw new RestException(500,
"Error creating new attribute");
1272 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1273 throw new RestException(401);
1278 $result = $prodattr->fetch((
int) $id);
1280 throw new RestException(404,
'Attribute not found');
1281 } elseif ($result < 0) {
1282 throw new RestException(500,
"Error fetching attribute");
1285 foreach ($request_data as $field => $value) {
1286 if ($field ==
'rowid') {
1289 $prodattr->$field = $value;
1292 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1293 $result = $prodattr->fetch((
int) $id);
1295 throw new RestException(404,
'Attribute not found');
1296 } elseif ($result < 0) {
1297 throw new RestException(500,
"Error fetching attribute");
1299 return $this->_cleanObjectDatas($prodattr);
1302 throw new RestException(500,
"Error updating attribute");
1318 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1319 throw new RestException(401);
1323 $prodattr->id = (int) $id;
1324 $result = $prodattr->delete(DolibarrApiAccess::$user);
1327 throw new RestException(500,
"Error deleting attribute");
1346 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1347 throw new RestException(401);
1350 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1352 $query = $this->db->query($sql);
1355 throw new RestException(401);
1358 if (!$this->db->num_rows($query)) {
1359 throw new RestException(404,
'Attribute value not found');
1362 $result = $this->db->fetch_object($query);
1365 $attrval[
'id'] = $result->rowid;
1366 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1367 $attrval[
'ref'] = $result->ref;
1368 $attrval[
'value'] = $result->value;
1387 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1388 throw new RestException(401);
1393 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1394 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1396 $query = $this->db->query($sql);
1399 throw new RestException(401);
1402 if (!$this->db->num_rows($query)) {
1403 throw new RestException(404,
'Attribute value not found');
1406 $result = $this->db->fetch_object($query);
1409 $attrval[
'id'] = $result->rowid;
1410 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1411 $attrval[
'ref'] = $result->ref;
1412 $attrval[
'value'] = $result->value;
1430 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1431 throw new RestException(401);
1436 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1437 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1438 $query = $this->db->query($sql);
1441 throw new RestException(401);
1444 if (!$this->db->num_rows($query)) {
1445 throw new RestException(404,
'Attribute value not found');
1448 $result = $this->db->fetch_object($query);
1451 $attrval->id = $result->rowid;
1452 $result = $attrval->delete(DolibarrApiAccess::$user);
1457 throw new RestException(500,
"Error deleting attribute value");
1473 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1474 throw new RestException(401);
1479 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1481 if (count($return) == 0) {
1482 throw new RestException(404,
'Attribute values not found');
1485 foreach ($return as $key => $val) {
1486 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1504 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1505 throw new RestException(401);
1513 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1514 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->db->escape($ref).
"')";
1516 $resql = $this->db->query($sql);
1518 while ($result = $this->db->fetch_object($resql)) {
1520 $tmp->fk_product_attribute = $result->fk_product_attribute;
1521 $tmp->id = $result->rowid;
1522 $tmp->ref = $result->ref;
1523 $tmp->value = $result->value;
1525 $return[] = $this->_cleanObjectDatas($tmp);
1546 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1547 throw new RestException(401);
1550 if (empty($ref) || empty($value)) {
1551 throw new RestException(401);
1555 $objectval->fk_product_attribute = ((int) $id);
1556 $objectval->ref = $ref;
1557 $objectval->value = $value;
1559 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1560 return $objectval->id;
1562 throw new RestException(500,
"Error creating new attribute value");
1579 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1580 throw new RestException(401);
1584 $result = $objectval->fetch((
int) $id);
1587 throw new RestException(404,
'Attribute value not found');
1588 } elseif ($result < 0) {
1589 throw new RestException(500,
"Error fetching attribute value");
1592 foreach ($request_data as $field => $value) {
1593 if ($field ==
'rowid') {
1596 $objectval->$field = $value;
1599 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1600 $result = $objectval->fetch((
int) $id);
1602 throw new RestException(404,
'Attribute not found');
1603 } elseif ($result < 0) {
1604 throw new RestException(500,
"Error fetching attribute");
1606 return $this->_cleanObjectDatas($objectval);
1609 throw new RestException(500,
"Error updating attribute");
1625 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1626 throw new RestException(401);
1630 $objectval->id = (int) $id;
1632 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1635 throw new RestException(500,
"Error deleting attribute value");
1652 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1653 throw new RestException(401);
1657 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1659 foreach ($combinations as $key => $combination) {
1661 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1662 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1664 if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1665 $productModel =
new Product($this->db);
1666 $productModel->fetch((
int) $combination->fk_product_child);
1667 $productModel->load_stock($includestock);
1668 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1672 return $combinations;
1688 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1689 throw new RestException(401);
1692 $result = $this->product->fetch(
'', $ref);
1694 throw new RestException(404,
'Product not found');
1698 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1700 foreach ($combinations as $key => $combination) {
1702 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1703 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1706 return $combinations;
1729 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1731 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1732 throw new RestException(401);
1735 if (empty($id) || empty($features) || !is_array($features)) {
1736 throw new RestException(401);
1739 $weight_impact =
price2num($weight_impact);
1740 $price_impact =
price2num($price_impact);
1744 foreach ($features as $id_attr => $id_value) {
1745 if ($prodattr->fetch((
int) $id_attr) < 0) {
1746 throw new RestException(401);
1748 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1749 throw new RestException(401);
1753 $result = $this->product->fetch((
int) $id);
1755 throw new RestException(404,
'Product not found');
1760 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1764 throw new RestException(500,
"Error creating new product variant");
1788 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1789 throw new RestException(401);
1792 if (empty($ref) || empty($features) || !is_array($features)) {
1793 throw new RestException(401);
1796 $weight_impact =
price2num($weight_impact);
1797 $price_impact =
price2num($price_impact);
1801 foreach ($features as $id_attr => $id_value) {
1802 if ($prodattr->fetch((
int) $id_attr) < 0) {
1803 throw new RestException(404);
1805 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1806 throw new RestException(404);
1810 $result = $this->product->fetch(
'', trim($ref));
1812 throw new RestException(404,
'Product not found');
1816 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1817 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1821 throw new RestException(500,
"Error creating new product variant");
1824 return $prodcomb->id;
1842 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1843 throw new RestException(401);
1847 $prodcomb->fetch((
int) $id);
1849 foreach ($request_data as $field => $value) {
1850 if ($field ==
'rowid') {
1853 $prodcomb->$field = $value;
1856 $result = $prodcomb->update(DolibarrApiAccess::$user);
1860 throw new RestException(500,
"Error editing variant");
1876 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1877 throw new RestException(401);
1881 $prodcomb->id = (int) $id;
1882 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1884 throw new RestException(500,
"Error deleting variant");
1903 public function getStock($id, $selected_warehouse_id =
null)
1905 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1906 throw new RestException(401);
1910 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1913 $product_model =
new Product($this->db);
1914 $product_model->fetch($id);
1915 $product_model->load_stock();
1917 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1918 if ($selected_warehouse_id) {
1919 foreach ($stockData as $warehouse_id => $warehouse) {
1920 if ($warehouse_id != $selected_warehouse_id) {
1921 unset($stockData[$warehouse_id]);
1926 if (empty($stockData)) {
1927 throw new RestException(404,
'No stock found');
1930 return array(
'stock_warehouses'=>$stockData);
1943 $object = parent::_cleanObjectDatas($object);
1945 unset($object->statut);
1947 unset($object->regeximgext);
1948 unset($object->price_by_qty);
1949 unset($object->prices_by_qty_id);
1950 unset($object->libelle);
1951 unset($object->product_id_already_linked);
1952 unset($object->reputations);
1954 unset($object->name);
1955 unset($object->firstname);
1956 unset($object->lastname);
1957 unset($object->civility_id);
1958 unset($object->contact);
1959 unset($object->contact_id);
1960 unset($object->thirdparty);
1961 unset($object->user);
1962 unset($object->origin);
1963 unset($object->origin_id);
1964 unset($object->fourn_pu);
1965 unset($object->fourn_price_base_type);
1966 unset($object->fourn_socid);
1967 unset($object->ref_fourn);
1968 unset($object->ref_supplier);
1969 unset($object->product_fourn_id);
1970 unset($object->fk_project);
1972 unset($object->mode_reglement_id);
1973 unset($object->cond_reglement_id);
1974 unset($object->demand_reason_id);
1975 unset($object->transport_mode_id);
1976 unset($object->cond_reglement);
1977 unset($object->shipping_method_id);
1978 unset($object->model_pdf);
1979 unset($object->note);
1981 unset($object->nbphoto);
1982 unset($object->recuperableonly);
1983 unset($object->multiprices_recuperableonly);
1984 unset($object->tva_npr);
1985 unset($object->lines);
1986 unset($object->fk_bank);
1987 unset($object->fk_account);
1989 unset($object->supplierprices);
1991 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1992 unset($object->stock_reel);
1993 unset($object->stock_theorique);
1994 unset($object->stock_warehouse);
2010 foreach (Products::$FIELDS as $field) {
2011 if (!isset($data[$field])) {
2012 throw new RestException(400,
"$field field missing");
2014 $product[$field] = $data[$field];
2038 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2040 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2041 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2044 $id = (empty($id) ? 0 : $id);
2046 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2047 throw new RestException(403);
2050 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2052 throw new RestException(404,
'Product not found');
2056 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2059 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2060 $this->product->load_stock($includestockdata);
2062 if (is_array($this->product->stock_warehouse)) {
2063 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2064 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2065 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2066 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2073 if ($includesubproducts) {
2074 $childsArbo = $this->product->getChildsArbo($id, 1);
2076 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2078 foreach ($childsArbo as $values) {
2079 $childs[] = array_combine($keys, $values);
2082 $this->product->sousprods = $childs;
2085 if ($includeparentid) {
2087 $this->product->fk_product_parent =
null;
2088 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2089 $this->product->fk_product_parent = $fk_product_parent;
2093 if ($includeifobjectisused) {
2094 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2097 return $this->_cleanObjectDatas($this->product);
Class to manage categories.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_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.
Class to manage predefined suppliers products.
Class to manage products or services.
File of class to manage predefined price products or services by customer.
_cleanObjectDatas($object)
Clean sensible object datas.
putAttributeValue($id, $request_data)
Update attribute value.
deleteAttributes($id)
Delete attributes by id.
index($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $ids_only=false, $variant_filter=0, $pagination_data=false, $includestockdata=0)
List products.
deletePurchasePrice($id, $priceid)
Delete purchase price for a product.
getAttributeValuesByRef($ref)
Get all values for an attribute ref.
putVariant($id, $request_data=null)
Put product variants.
put($id, $request_data=null)
Update product.
addAttributeValue($id, $ref, $value)
Add attribute value.
addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features)
Add variant by product ref.
getAttributeValueById($id)
Get attribute value by id.
getVariantsByProdRef($ref)
Get product variants by Product ref.
getCustomerPricesPerQuantity($id)
Get prices per quantity for a product.
__construct()
Constructor.
getPurchasePrices($id, $ref='', $ref_ext='', $barcode='')
Get purchase prices for a product.
delSubproducts($id, $subproduct_id)
Remove subproduct.
getVariants($id, $includestock=0)
Get product variants.
putAttributes($id, $request_data=null)
Update attributes by id.
deleteAttributeValueById($id)
Delete attribute value by id.
getAttributeValues($id)
Get all values for an attribute id.
addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference='', $ref_ext='')
Add variant.
getSubproducts($id)
Get the list of subproducts of the product.
getAttributesByRefExt($ref_ext)
Get attributes by ref_ext.
getByRef($ref, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref.
deleteAttributeValueByRef($id, $ref)
Delete attribute value by ref.
post($request_data=null)
Create product object.
addPurchasePrice($id, $qty, $buyprice, $price_base_type, $fourn_id, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='', $multicurrency_buyprice=0, $multicurrency_price_base_type='HT', $multicurrency_tx=1, $multicurrency_code='', $desc_fourn='', $barcode='', $fk_barcode_type=null)
Add/Update purchase prices for a product.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a product.
getByBarcode($barcode, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by barcode.
getByRefExt($ref_ext, $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includetrans=false)
Get properties of a product object by ref_ext.
getCustomerPricesPerCustomer($id, $thirdparty_id='')
Get prices per customer for a product.
deleteVariant($id)
Delete product variants.
_validate($data)
Validate fields before create or update object.
getCustomerPricesPerSegment($id)
Get prices per segment for a product.
_fetch($id, $ref='', $ref_ext='', $barcode='', $includestockdata=0, $includesubproducts=false, $includeparentid=false, $includeifobjectisused=false, $includetrans=false)
Get properties of 1 product object.
addAttributes($ref, $label, $ref_ext='')
Add attributes.
getAttributeValueByRef($id, $ref)
Get attribute value by ref.
getSupplierProducts($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $supplier=0, $sqlfilters='')
Get a list of all purchase prices of products.
getAttributeById($id)
Get attribute by ID.
getStock($id, $selected_warehouse_id=null)
Get stock data for the product id given.
getAttributes($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get attributes.
getAttributesByRef($ref)
Get attributes by ref.
addSubproducts($id, $subproduct_id, $qty, $incdec=1)
Add subproduct.
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 =...
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.