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 $this->product->$field = $value;
368 if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
372 $result = $this->product->update($id, DolibarrApiAccess::$user, 1,
'update', $updatetype);
375 if ($result > 0 && !empty($conf->global->PRODUCT_PRICE_UNIQ)) {
377 $pricemodified =
false;
378 if ($this->product->price_base_type != $oldproduct->price_base_type) {
379 $pricemodified =
true;
381 if ($this->product->tva_tx != $oldproduct->tva_tx) {
382 $pricemodified =
true;
384 if ($this->product->tva_npr != $oldproduct->tva_npr) {
385 $pricemodified =
true;
387 if ($this->product->default_vat_code != $oldproduct->default_vat_code) {
388 $pricemodified =
true;
391 if ($this->product->price_base_type ==
'TTC') {
392 if ($this->product->price_ttc != $oldproduct->price_ttc) {
393 $pricemodified =
true;
395 if ($this->product->price_min_ttc != $oldproduct->price_min_ttc) {
396 $pricemodified =
true;
399 if ($this->product->price != $oldproduct->price) {
400 $pricemodified =
true;
402 if ($this->product->price_min != $oldproduct->price_min) {
403 $pricemodified =
true;
408 if ($pricemodified) {
409 $newvat = $this->product->tva_tx;
410 $newnpr = $this->product->tva_npr;
411 $newvatsrccode = $this->product->default_vat_code;
413 $newprice = $this->product->price;
414 $newpricemin = $this->product->price_min;
415 if ($this->product->price_base_type ==
'TTC') {
416 $newprice = $this->product->price_ttc;
417 $newpricemin = $this->product->price_min_ttc;
420 $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode);
425 throw new RestException(500,
"Error updating product", array_merge(array($this->product->error), $this->product->errors));
428 return $this->
get($id);
437 public function delete($id)
439 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
440 throw new RestException(401);
442 $result = $this->product->fetch($id);
444 throw new RestException(404,
'Product not found');
448 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 $user = DolibarrApiAccess::$user;
455 $res = $this->product->delete(DolibarrApiAccess::$user);
457 throw new RestException(500,
"Can't delete, error occurs");
458 } elseif ($res == 0) {
459 throw new RestException(409,
"Can't delete, that product is probably used");
465 'message' =>
'Object deleted'
484 if (!DolibarrApiAccess::$user->rights->produit->lire) {
485 throw new RestException(401);
489 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
492 $childsArbo = $this->product->getChildsArbo($id, 1);
494 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
496 foreach ($childsArbo as $values) {
497 $childs[] = array_combine($keys, $values);
522 if (!DolibarrApiAccess::$user->rights->produit->creer) {
523 throw new RestException(401);
527 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
530 $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec);
532 throw new RestException(500,
"Error adding product child");
552 if (!DolibarrApiAccess::$user->rights->produit->creer) {
553 throw new RestException(401);
557 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
560 $result = $this->product->del_sousproduit($id, $subproduct_id);
562 throw new RestException(500,
"Error while removing product child");
581 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
583 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
584 throw new RestException(401);
589 $result = $categories->getListForItem($id,
'product', $sortfield, $sortorder, $limit, $page);
591 if (empty($result)) {
592 throw new RestException(404,
'No category found');
596 throw new RestException(503,
'Error when retrieve category list : '.join(
',', array_merge(array($categories->error), $categories->errors)));
615 if (!DolibarrApiAccess::$user->rights->produit->lire) {
616 throw new RestException(401);
619 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
620 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
623 $result = $this->product->fetch($id);
625 throw new RestException(404,
'Product not found');
629 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
633 'multiprices'=>$this->product->multiprices,
634 'multiprices_inc_tax'=>$this->product->multiprices_ttc,
635 'multiprices_min'=>$this->product->multiprices_min,
636 'multiprices_min_inc_tax'=>$this->product->multiprices_min_ttc,
637 'multiprices_vat'=>$this->product->multiprices_tva_tx,
638 'multiprices_base_type'=>$this->product->multiprices_base_type,
657 if (!DolibarrApiAccess::$user->rights->produit->lire) {
658 throw new RestException(401);
661 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES)) {
662 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
665 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
666 if ($socid > 0 && $socid != $thirdparty_id) {
667 throw new RestException(401,
'Getting prices for all customers or for the customer ID '.$thirdparty_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
670 $result = $this->product->fetch($id);
672 throw new RestException(404,
'Product not found');
676 require_once DOL_DOCUMENT_ROOT.
'/product/class/productcustomerprice.class.php';
679 $filter[
't.fk_product'] = $id;
680 if ($thirdparty_id) {
681 $filter[
't.fk_soc'] = $thirdparty_id;
683 $result = $prodcustprice->fetchAll(
'',
'', 0, 0, $filter);
686 if (empty($prodcustprice->lines)) {
687 throw new RestException(404,
'Prices not found');
690 return $prodcustprice->lines;
706 if (!DolibarrApiAccess::$user->rights->produit->lire) {
707 throw new RestException(401);
710 if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
711 throw new RestException(400,
'API not available: this mode of pricing is not enabled by setup');
714 $result = $this->product->fetch($id);
716 throw new RestException(404,
'Product not found');
720 throw new RestException(503,
'Error when retrieve prices list : '.join(
',', array_merge(array($this->product->error), $this->product->errors)));
724 'prices_by_qty'=>$this->product->prices_by_qty[0],
725 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0]
762 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)
764 if (!DolibarrApiAccess::$user->rights->produit->creer) {
765 throw new RestException(401);
768 $result = $this->productsupplier->fetch($id);
770 throw new RestException(404,
'Product not found');
774 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
777 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
778 if ($socid > 0 && $socid != $fourn_id) {
779 throw new RestException(401,
'Adding purchase price for the supplier ID '.$fourn_id.
' is not allowed for login '.DolibarrApiAccess::$user->login);
782 $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty);
784 throw new RestException(500,
"Error adding supplier to product : ".$this->db->lasterror());
788 $result = $fourn->fetch($fourn_id);
790 throw new RestException(404,
'Supplier not found');
794 $ref_fourn =
sanitizeVal($ref_fourn,
'alphanohtml');
795 $desc_fourn =
sanitizeVal($desc_fourn,
'restricthtml');
798 $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);
801 throw new RestException(500,
"Error updating buy price : ".$this->db->lasterror());
803 return (
int) $this->productsupplier->product_fourn_price_id;
822 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
823 throw new RestException(401);
825 $result = $this->productsupplier->fetch($id);
827 throw new RestException(404,
'Product not found');
831 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
836 $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid);
839 return $resultsupplier;
857 public function getSupplierProducts($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters =
'')
861 if (!DolibarrApiAccess::$user->rights->produit->lire) {
862 throw new RestException(401);
868 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
870 if ($supplier != $socid || empty($supplier)) {
871 throw new RestException(401,
'As an external user, you can request only for your supplier id = '.$socid);
875 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
876 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product AS t LEFT JOIN ".MAIN_DB_PREFIX.
"product_extrafields AS ef ON (ef.fk_object = t.rowid)";
879 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
881 $sql .=
", ".$this->db->prefix().
"product_fournisseur_price as s";
883 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
886 $sql .=
" AND s.fk_soc = ".((int) $supplier);
889 $sql .=
" AND s.fk_soc = ".((int) $socid);
891 $sql .=
" AND s.fk_product = t.rowid";
894 $sql .=
" AND c.fk_categorie = ".((int) $category);
895 $sql .=
" AND c.fk_product = t.rowid";
899 $sql .=
" AND t.fk_product_type = 0";
900 } elseif ($mode == 2) {
902 $sql .=
" AND t.fk_product_type = 1";
909 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
913 $sql .= $this->db->order($sortfield, $sortorder);
918 $offset = $limit * $page;
919 $sql .= $this->db->plimit($limit + 1, $offset);
921 $result = $this->db->query($sql);
923 $num = $this->db->num_rows($result);
924 $min = min($num, ($limit <= 0 ? $num : $limit));
927 $obj = $this->db->fetch_object($result);
930 $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid,
'',
'', 0, 0);
931 foreach ($product_fourn_list as $tmpobj) {
936 $obj_ret[$obj->rowid] = $product_fourn_list;
941 throw new RestException(503,
'Error when retrieve product list : '.$this->db->lasterror());
943 if (!count($obj_ret)) {
944 throw new RestException(404,
'No product found');
970 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
971 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
974 $id = (empty($id) ? 0 : $id);
976 if (!DolibarrApiAccess::$user->rights->produit->lire) {
977 throw new RestException(403);
980 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
982 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
984 throw new RestException(404,
'Product not found');
988 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
991 $product_fourn_list = array();
995 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
998 foreach ($product_fourn_list as $tmpobj) {
1021 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
1023 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1024 throw new RestException(401);
1027 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1028 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1029 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1035 if ($errormessage) {
1036 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
1040 $sql .= $this->db->order($sortfield, $sortorder);
1045 $offset = $limit * $page;
1047 $sql .= $this->db->plimit($limit, $offset);
1050 $resql = $this->db->query($sql);
1053 throw new RestException(503,
'Error when retrieving product attribute list : '.$this->db->lasterror());
1057 while ($obj = $this->db->fetch_object($resql)) {
1059 $tmp->id = $obj->rowid;
1060 $tmp->ref = $obj->ref;
1061 $tmp->ref_ext = $obj->ref_ext;
1062 $tmp->label = $obj->label;
1063 $tmp->position = $obj->position;
1064 $tmp->entity = $obj->entity;
1069 if (!count($return)) {
1070 throw new RestException(404,
'No product attribute found');
1089 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1090 throw new RestException(401);
1094 $result = $prodattr->fetch((
int) $id);
1097 throw new RestException(404,
"Product attribute not found");
1100 $fields = [
"id",
"ref",
"ref_ext",
"label",
"position",
"entity"];
1102 foreach ($prodattr as $field => $value) {
1103 if (!in_array($field, $fields)) {
1104 unset($prodattr->{$field});
1108 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1109 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1110 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1112 $resql = $this->db->query($sql);
1113 $obj = $this->db->fetch_object($resql);
1114 $prodattr->is_used_by_products = (int) $obj->nb;
1132 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1133 throw new RestException(401);
1138 $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').
")";
1140 $query = $this->db->query($sql);
1142 if (!$this->db->num_rows($query)) {
1143 throw new RestException(404);
1146 $result = $this->db->fetch_object($query);
1149 $attr[
'id'] = $result->rowid;
1150 $attr[
'ref'] = $result->ref;
1151 $attr[
'ref_ext'] = $result->ref_ext;
1152 $attr[
'label'] = $result->label;
1153 $attr[
'rang'] = $result->position;
1154 $attr[
'position'] = $result->position;
1155 $attr[
'entity'] = $result->entity;
1157 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1158 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1159 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1161 $resql = $this->db->query($sql);
1162 $obj = $this->db->fetch_object($resql);
1164 $attr[
"is_used_by_products"] = (int) $obj->nb;
1182 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1183 throw new RestException(401);
1186 $ref_ext = trim($ref_ext);
1188 $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').
")";
1190 $query = $this->db->query($sql);
1192 if (!$this->db->num_rows($query)) {
1193 throw new RestException(404);
1196 $result = $this->db->fetch_object($query);
1199 $attr[
'id'] = $result->rowid;
1200 $attr[
'ref'] = $result->ref;
1201 $attr[
'ref_ext'] = $result->ref_ext;
1202 $attr[
'label'] = $result->label;
1203 $attr[
'rang'] = $result->position;
1204 $attr[
'position'] = $result->position;
1205 $attr[
'entity'] = $result->entity;
1207 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1208 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1209 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1211 $resql = $this->db->query($sql);
1212 $obj = $this->db->fetch_object($resql);
1214 $attr[
"is_used_by_products"] = (int) $obj->nb;
1234 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1235 throw new RestException(401);
1239 $prodattr->label = $label;
1240 $prodattr->ref = $ref;
1241 $prodattr->ref_ext = $ref_ext;
1243 $resid = $prodattr->create(DolibarrApiAccess::$user);
1245 throw new RestException(500,
"Error creating new attribute");
1266 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1267 throw new RestException(401);
1272 $result = $prodattr->fetch((
int) $id);
1274 throw new RestException(404,
'Attribute not found');
1275 } elseif ($result < 0) {
1276 throw new RestException(500,
"Error fetching attribute");
1279 foreach ($request_data as $field => $value) {
1280 if ($field ==
'rowid') {
1283 $prodattr->$field = $value;
1286 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1287 $result = $prodattr->fetch((
int) $id);
1289 throw new RestException(404,
'Attribute not found');
1290 } elseif ($result < 0) {
1291 throw new RestException(500,
"Error fetching attribute");
1293 return $this->_cleanObjectDatas($prodattr);
1296 throw new RestException(500,
"Error updating attribute");
1312 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1313 throw new RestException(401);
1317 $prodattr->id = (int) $id;
1318 $result = $prodattr->delete(DolibarrApiAccess::$user);
1321 throw new RestException(500,
"Error deleting attribute");
1340 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1341 throw new RestException(401);
1344 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1346 $query = $this->db->query($sql);
1349 throw new RestException(401);
1352 if (!$this->db->num_rows($query)) {
1353 throw new RestException(404,
'Attribute value not found');
1356 $result = $this->db->fetch_object($query);
1359 $attrval[
'id'] = $result->rowid;
1360 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1361 $attrval[
'ref'] = $result->ref;
1362 $attrval[
'value'] = $result->value;
1381 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1382 throw new RestException(401);
1387 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1388 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1390 $query = $this->db->query($sql);
1393 throw new RestException(401);
1396 if (!$this->db->num_rows($query)) {
1397 throw new RestException(404,
'Attribute value not found');
1400 $result = $this->db->fetch_object($query);
1403 $attrval[
'id'] = $result->rowid;
1404 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1405 $attrval[
'ref'] = $result->ref;
1406 $attrval[
'value'] = $result->value;
1424 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1425 throw new RestException(401);
1430 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1431 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1432 $query = $this->db->query($sql);
1435 throw new RestException(401);
1438 if (!$this->db->num_rows($query)) {
1439 throw new RestException(404,
'Attribute value not found');
1442 $result = $this->db->fetch_object($query);
1445 $attrval->id = $result->rowid;
1446 $result = $attrval->delete(DolibarrApiAccess::$user);
1451 throw new RestException(500,
"Error deleting attribute value");
1467 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1468 throw new RestException(401);
1473 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1475 if (count($return) == 0) {
1476 throw new RestException(404,
'Attribute values not found');
1479 foreach ($return as $key => $val) {
1480 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1498 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1499 throw new RestException(401);
1507 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1508 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->db->escape($ref).
"')";
1510 $resql = $this->db->query($sql);
1512 while ($result = $this->db->fetch_object($resql)) {
1514 $tmp->fk_product_attribute = $result->fk_product_attribute;
1515 $tmp->id = $result->rowid;
1516 $tmp->ref = $result->ref;
1517 $tmp->value = $result->value;
1519 $return[] = $this->_cleanObjectDatas($tmp);
1540 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1541 throw new RestException(401);
1544 if (empty($ref) || empty($value)) {
1545 throw new RestException(401);
1549 $objectval->fk_product_attribute = ((int) $id);
1550 $objectval->ref = $ref;
1551 $objectval->value = $value;
1553 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1554 return $objectval->id;
1556 throw new RestException(500,
"Error creating new attribute value");
1573 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1574 throw new RestException(401);
1578 $result = $objectval->fetch((
int) $id);
1581 throw new RestException(404,
'Attribute value not found');
1582 } elseif ($result < 0) {
1583 throw new RestException(500,
"Error fetching attribute value");
1586 foreach ($request_data as $field => $value) {
1587 if ($field ==
'rowid') {
1590 $objectval->$field = $value;
1593 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1594 $result = $objectval->fetch((
int) $id);
1596 throw new RestException(404,
'Attribute not found');
1597 } elseif ($result < 0) {
1598 throw new RestException(500,
"Error fetching attribute");
1600 return $this->_cleanObjectDatas($objectval);
1603 throw new RestException(500,
"Error updating attribute");
1619 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1620 throw new RestException(401);
1624 $objectval->id = (int) $id;
1626 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1629 throw new RestException(500,
"Error deleting attribute value");
1646 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1647 throw new RestException(401);
1651 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1653 foreach ($combinations as $key => $combination) {
1655 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1656 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1658 if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1659 $productModel =
new Product($this->db);
1660 $productModel->fetch((
int) $combination->fk_product_child);
1661 $productModel->load_stock($includestock);
1662 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1666 return $combinations;
1682 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1683 throw new RestException(401);
1686 $result = $this->product->fetch(
'', $ref);
1688 throw new RestException(404,
'Product not found');
1692 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1694 foreach ($combinations as $key => $combination) {
1696 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1697 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1700 return $combinations;
1723 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1725 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1726 throw new RestException(401);
1729 if (empty($id) || empty($features) || !is_array($features)) {
1730 throw new RestException(401);
1733 $weight_impact =
price2num($weight_impact);
1734 $price_impact =
price2num($price_impact);
1738 foreach ($features as $id_attr => $id_value) {
1739 if ($prodattr->fetch((
int) $id_attr) < 0) {
1740 throw new RestException(401);
1742 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1743 throw new RestException(401);
1747 $result = $this->product->fetch((
int) $id);
1749 throw new RestException(404,
'Product not found');
1754 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1758 throw new RestException(500,
"Error creating new product variant");
1782 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1783 throw new RestException(401);
1786 if (empty($ref) || empty($features) || !is_array($features)) {
1787 throw new RestException(401);
1790 $weight_impact =
price2num($weight_impact);
1791 $price_impact =
price2num($price_impact);
1795 foreach ($features as $id_attr => $id_value) {
1796 if ($prodattr->fetch((
int) $id_attr) < 0) {
1797 throw new RestException(404);
1799 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1800 throw new RestException(404);
1804 $result = $this->product->fetch(
'', trim($ref));
1806 throw new RestException(404,
'Product not found');
1810 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1811 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1815 throw new RestException(500,
"Error creating new product variant");
1818 return $prodcomb->id;
1836 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1837 throw new RestException(401);
1841 $prodcomb->fetch((
int) $id);
1843 foreach ($request_data as $field => $value) {
1844 if ($field ==
'rowid') {
1847 $prodcomb->$field = $value;
1850 $result = $prodcomb->update(DolibarrApiAccess::$user);
1854 throw new RestException(500,
"Error editing variant");
1870 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1871 throw new RestException(401);
1875 $prodcomb->id = (int) $id;
1876 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1878 throw new RestException(500,
"Error deleting variant");
1897 public function getStock($id, $selected_warehouse_id =
null)
1899 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1900 throw new RestException(401);
1904 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1907 $product_model =
new Product($this->db);
1908 $product_model->fetch($id);
1909 $product_model->load_stock();
1911 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1912 if ($selected_warehouse_id) {
1913 foreach ($stockData as $warehouse_id => $warehouse) {
1914 if ($warehouse_id != $selected_warehouse_id) {
1915 unset($stockData[$warehouse_id]);
1920 if (empty($stockData)) {
1921 throw new RestException(404,
'No stock found');
1924 return array(
'stock_warehouses'=>$stockData);
1937 $object = parent::_cleanObjectDatas($object);
1939 unset($object->statut);
1941 unset($object->regeximgext);
1942 unset($object->price_by_qty);
1943 unset($object->prices_by_qty_id);
1944 unset($object->libelle);
1945 unset($object->product_id_already_linked);
1946 unset($object->reputations);
1948 unset($object->name);
1949 unset($object->firstname);
1950 unset($object->lastname);
1951 unset($object->civility_id);
1952 unset($object->contact);
1953 unset($object->contact_id);
1954 unset($object->thirdparty);
1955 unset($object->user);
1956 unset($object->origin);
1957 unset($object->origin_id);
1958 unset($object->fourn_pu);
1959 unset($object->fourn_price_base_type);
1960 unset($object->fourn_socid);
1961 unset($object->ref_fourn);
1962 unset($object->ref_supplier);
1963 unset($object->product_fourn_id);
1964 unset($object->fk_project);
1966 unset($object->mode_reglement_id);
1967 unset($object->cond_reglement_id);
1968 unset($object->demand_reason_id);
1969 unset($object->transport_mode_id);
1970 unset($object->cond_reglement);
1971 unset($object->shipping_method_id);
1972 unset($object->model_pdf);
1973 unset($object->note);
1975 unset($object->nbphoto);
1976 unset($object->recuperableonly);
1977 unset($object->multiprices_recuperableonly);
1978 unset($object->tva_npr);
1979 unset($object->lines);
1980 unset($object->fk_bank);
1981 unset($object->fk_account);
1983 unset($object->supplierprices);
1985 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1986 unset($object->stock_reel);
1987 unset($object->stock_theorique);
1988 unset($object->stock_warehouse);
2004 foreach (Products::$FIELDS as $field) {
2005 if (!isset($data[$field])) {
2006 throw new RestException(400,
"$field field missing");
2008 $product[$field] = $data[$field];
2032 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2034 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2035 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2038 $id = (empty($id) ? 0 : $id);
2040 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2041 throw new RestException(403);
2044 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2046 throw new RestException(404,
'Product not found');
2050 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2053 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2054 $this->product->load_stock($includestockdata);
2056 if (is_array($this->product->stock_warehouse)) {
2057 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2058 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2059 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2060 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2067 if ($includesubproducts) {
2068 $childsArbo = $this->product->getChildsArbo($id, 1);
2070 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2072 foreach ($childsArbo as $values) {
2073 $childs[] = array_combine($keys, $values);
2076 $this->product->sousprods = $childs;
2079 if ($includeparentid) {
2081 $this->product->fk_product_parent =
null;
2082 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2083 $this->product->fk_product_parent = $fk_product_parent;
2087 if ($includeifobjectisused) {
2088 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2091 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.
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.