19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
23 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
24 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttribute.class.php';
25 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductAttributeValue.class.php';
26 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/variants/class/ProductCombination2ValuePair.class.php';
40 public static $FIELDS = array(
53 public $productsupplier;
83 public function get($id, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
85 return $this->
_fetch($id,
'',
'',
'', $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
107 public function getByRef($ref, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
109 return $this->
_fetch(
'', $ref,
'',
'', $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
131 public function getByRefExt($ref_ext, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
133 return $this->
_fetch(
'',
'', $ref_ext,
'', $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
155 public function getByBarcode($barcode, $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includetrans =
false)
157 return $this->
_fetch(
'',
'',
'', $barcode, $includestockdata, $includesubproducts, $includeparentid,
false, $includetrans);
178 public function index($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $ids_only =
false, $variant_filter = 0, $pagination_data =
false, $includestockdata = 0)
182 if (!DolibarrApiAccess::$user->rights->produit->lire) {
183 throw new RestException(403);
188 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
190 $sql =
"SELECT t.rowid, t.ref, t.ref_ext";
191 $sql .=
" FROM ".$this->db->prefix().
"product as t";
192 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product_extrafields AS ef ON ef.fk_object = t.rowid";
194 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
196 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
198 if ($variant_filter == 1) {
199 $sql .=
' AND t.rowid not in (select distinct fk_product_parent from '.$this->db->prefix().
'product_attribute_combination)';
200 $sql .=
' AND t.rowid not in (select distinct fk_product_child from '.$this->db->prefix().
'product_attribute_combination)';
202 if ($variant_filter == 2) {
203 $sql .=
' AND t.rowid in (select distinct fk_product_parent from '.$this->db->prefix().
'product_attribute_combination)';
205 if ($variant_filter == 3) {
206 $sql .=
' AND t.rowid in (select distinct fk_product_child from '.$this->db->prefix().
'product_attribute_combination)';
211 $sql .=
" AND c.fk_categorie = ".((int) $category);
212 $sql .=
" AND c.fk_product = t.rowid";
216 $sql .=
" AND t.fk_product_type = 0";
217 } elseif ($mode == 2) {
219 $sql .=
" AND t.fk_product_type = 1";
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 ".$this->db->prefix().
"product as t";
878 $sql .=
", ".$this->db->prefix().
"categorie_product as c";
880 $sql .=
", ".$this->db->prefix().
"product_fournisseur_price as s";
882 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
885 $sql .=
" AND s.fk_soc = ".((int) $supplier);
888 $sql .=
" AND s.fk_soc = ".((int) $socid);
890 $sql .=
" AND s.fk_product = t.rowid";
893 $sql .=
" AND c.fk_categorie = ".((int) $category);
894 $sql .=
" AND c.fk_product = t.rowid";
898 $sql .=
" AND t.fk_product_type = 0";
899 } elseif ($mode == 2) {
901 $sql .=
" AND t.fk_product_type = 1";
908 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
912 $sql .= $this->
db->order($sortfield, $sortorder);
917 $offset = $limit * $page;
918 $sql .= $this->
db->plimit($limit + 1, $offset);
920 $result = $this->
db->query($sql);
922 $num = $this->
db->num_rows($result);
923 $min = min($num, ($limit <= 0 ? $num : $limit));
926 $obj = $this->
db->fetch_object($result);
929 $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid,
'',
'', 0, 0);
930 foreach ($product_fourn_list as $tmpobj) {
935 $obj_ret[$obj->rowid] = $product_fourn_list;
940 throw new RestException(503,
'Error when retrieve product list : '.$this->
db->lasterror());
942 if (!count($obj_ret)) {
943 throw new RestException(404,
'No product found');
969 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
970 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
973 $id = (empty($id) ? 0 : $id);
975 if (!DolibarrApiAccess::$user->rights->produit->lire) {
976 throw new RestException(403);
979 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
981 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
983 throw new RestException(404,
'Product not found');
987 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
990 $product_fourn_list = array();
994 $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id,
'',
'', 0, 0, ($socid > 0 ? $socid : 0));
997 foreach ($product_fourn_list as $tmpobj) {
1020 public function getAttributes($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
1022 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1023 throw new RestException(401);
1026 $sql =
"SELECT t.rowid, t.ref, t.ref_ext, t.label, t.position, t.entity";
1027 $sql .=
" FROM ".$this->db->prefix().
"product_attribute as t";
1028 $sql .=
' WHERE t.entity IN ('.getEntity(
'product').
')';
1034 if ($errormessage) {
1035 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
1039 $sql .= $this->
db->order($sortfield, $sortorder);
1044 $offset = $limit * $page;
1046 $sql .= $this->
db->plimit($limit, $offset);
1049 $result = $this->
db->query($sql);
1052 throw new RestException(503,
'Error when retrieve product attribute list : '.$this->
db->lasterror());
1056 while ($result = $this->
db->fetch_object($query)) {
1058 $tmp->id = $result->rowid;
1059 $tmp->ref = $result->ref;
1060 $tmp->ref_ext = $result->ref_ext;
1061 $tmp->label = $result->label;
1062 $tmp->position = $result->position;
1063 $tmp->entity = $result->entity;
1068 if (!count($return)) {
1069 throw new RestException(404,
'No product attribute found');
1088 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1089 throw new RestException(401);
1093 $result = $prodattr->fetch((
int) $id);
1096 throw new RestException(404,
"Product attribute not found");
1099 $fields = [
"id",
"ref",
"ref_ext",
"label",
"position",
"entity"];
1101 foreach ($prodattr as $field => $value) {
1102 if (!in_array($field, $fields)) {
1103 unset($prodattr->{$field});
1107 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1108 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1109 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $prodattr->id).
" AND pac.entity IN (".
getEntity(
'product').
")";
1112 $obj = $this->
db->fetch_object(
$resql);
1113 $prodattr->is_used_by_products = (int) $obj->nb;
1131 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1132 throw new RestException(401);
1137 $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').
")";
1139 $query = $this->
db->query($sql);
1141 if (!$this->
db->num_rows($query)) {
1142 throw new RestException(404);
1145 $result = $this->
db->fetch_object($query);
1148 $attr[
'id'] = $result->rowid;
1149 $attr[
'ref'] = $result->ref;
1150 $attr[
'ref_ext'] = $result->ref_ext;
1151 $attr[
'label'] = $result->label;
1152 $attr[
'rang'] = $result->position;
1153 $attr[
'position'] = $result->position;
1154 $attr[
'entity'] = $result->entity;
1156 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1157 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1158 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1161 $obj = $this->
db->fetch_object(
$resql);
1163 $attr[
"is_used_by_products"] = (int) $obj->nb;
1181 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1182 throw new RestException(401);
1185 $ref_ext = trim($ref_ext);
1187 $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').
")";
1189 $query = $this->
db->query($sql);
1191 if (!$this->
db->num_rows($query)) {
1192 throw new RestException(404);
1195 $result = $this->
db->fetch_object($query);
1198 $attr[
'id'] = $result->rowid;
1199 $attr[
'ref'] = $result->ref;
1200 $attr[
'ref_ext'] = $result->ref_ext;
1201 $attr[
'label'] = $result->label;
1202 $attr[
'rang'] = $result->position;
1203 $attr[
'position'] = $result->position;
1204 $attr[
'entity'] = $result->entity;
1206 $sql =
"SELECT COUNT(*) as nb FROM ".$this->db->prefix().
"product_attribute_combination2val as pac2v";
1207 $sql .=
" JOIN ".$this->db->prefix().
"product_attribute_combination as pac ON pac2v.fk_prod_combination = pac.rowid";
1208 $sql .=
" WHERE pac2v.fk_prod_attr = ".((int) $result->rowid).
" AND pac.entity IN (".
getEntity(
'product').
")";
1211 $obj = $this->
db->fetch_object(
$resql);
1212 $attr[
"is_used_by_products"] = (int) $obj->nb;
1232 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1233 throw new RestException(401);
1237 $prodattr->label = $label;
1238 $prodattr->ref = $ref;
1239 $prodattr->ref_ext = $ref_ext;
1241 $resid = $prodattr->create(DolibarrApiAccess::$user);
1243 throw new RestException(500,
"Error creating new attribute");
1263 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1264 throw new RestException(401);
1269 $result = $prodattr->fetch((
int) $id);
1271 throw new RestException(404,
'Attribute not found');
1272 } elseif ($result < 0) {
1273 throw new RestException(500,
"Error fetching attribute");
1276 foreach ($request_data as $field => $value) {
1277 if ($field ==
'rowid') {
1280 $prodattr->$field = $value;
1283 if ($prodattr->update(DolibarrApiAccess::$user) > 0) {
1284 $result = $prodattr->fetch((
int) $id);
1286 throw new RestException(404,
'Attribute not found');
1287 } elseif ($result < 0) {
1288 throw new RestException(500,
"Error fetching attribute");
1293 throw new RestException(500,
"Error updating attribute");
1309 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1310 throw new RestException(401);
1314 $prodattr->id = (int) $id;
1315 $result = $prodattr->delete(DolibarrApiAccess::$user);
1318 throw new RestException(500,
"Error deleting attribute");
1337 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1338 throw new RestException(401);
1341 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value WHERE rowid = ".(int) $id.
" AND entity IN (".
getEntity(
'product').
")";
1343 $query = $this->
db->query($sql);
1346 throw new RestException(401);
1349 if (!$this->
db->num_rows($query)) {
1350 throw new RestException(404,
'Attribute value not found');
1353 $result = $this->
db->fetch_object($query);
1356 $attrval[
'id'] = $result->rowid;
1357 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1358 $attrval[
'ref'] = $result->ref;
1359 $attrval[
'value'] = $result->value;
1378 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1379 throw new RestException(401);
1384 $sql =
"SELECT rowid, fk_product_attribute, ref, value FROM ".$this->db->prefix().
"product_attribute_value";
1385 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1387 $query = $this->
db->query($sql);
1390 throw new RestException(401);
1393 if (!$this->
db->num_rows($query)) {
1394 throw new RestException(404,
'Attribute value not found');
1397 $result = $this->
db->fetch_object($query);
1400 $attrval[
'id'] = $result->rowid;
1401 $attrval[
'fk_product_attribute'] = $result->fk_product_attribute;
1402 $attrval[
'ref'] = $result->ref;
1403 $attrval[
'value'] = $result->value;
1421 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1422 throw new RestException(401);
1427 $sql =
"SELECT rowid FROM ".$this->db->prefix().
"product_attribute_value";
1428 $sql .=
" WHERE ref LIKE '".$this->db->escape($ref).
"' AND fk_product_attribute = ".((int) $id).
" AND entity IN (".
getEntity(
'product').
")";
1429 $query = $this->
db->query($sql);
1432 throw new RestException(401);
1435 if (!$this->
db->num_rows($query)) {
1436 throw new RestException(404,
'Attribute value not found');
1439 $result = $this->
db->fetch_object($query);
1442 $attrval->id = $result->rowid;
1443 $result = $attrval->delete(DolibarrApiAccess::$user);
1448 throw new RestException(500,
"Error deleting attribute value");
1464 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1465 throw new RestException(401);
1470 $return = $objectval->fetchAllByProductAttribute((
int) $id);
1472 if (count($return) == 0) {
1473 throw new RestException(404,
'Attribute values not found');
1476 foreach ($return as $key => $val) {
1477 $return[$key] = $this->_cleanObjectDatas($return[$key]);
1495 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1496 throw new RestException(401);
1504 $sql .=
"v.fk_product_attribute, v.rowid, v.ref, v.value FROM ".$this->db->prefix().
"product_attribute_value as v";
1505 $sql .=
" WHERE v.fk_product_attribute IN (SELECT rowid FROM ".$this->db->prefix().
"product_attribute WHERE ref LIKE '".$this->
db->escape($ref).
"')";
1509 while ($result = $this->
db->fetch_object(
$resql)) {
1511 $tmp->fk_product_attribute = $result->fk_product_attribute;
1512 $tmp->id = $result->rowid;
1513 $tmp->ref = $result->ref;
1514 $tmp->value = $result->value;
1516 $return[] = $this->_cleanObjectDatas($tmp);
1537 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1538 throw new RestException(401);
1541 if (empty($ref) || empty($value)) {
1542 throw new RestException(401);
1546 $objectval->fk_product_attribute = ((int) $id);
1547 $objectval->ref = $ref;
1548 $objectval->value = $value;
1550 if ($objectval->create(DolibarrApiAccess::$user) > 0) {
1551 return $objectval->id;
1553 throw new RestException(500,
"Error creating new attribute value");
1570 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1571 throw new RestException(401);
1575 $result = $objectval->fetch((
int) $id);
1578 throw new RestException(404,
'Attribute value not found');
1579 } elseif ($result < 0) {
1580 throw new RestException(500,
"Error fetching attribute value");
1583 foreach ($request_data as $field => $value) {
1584 if ($field ==
'rowid') {
1587 $objectval->$field = $value;
1590 if ($objectval->update(DolibarrApiAccess::$user) > 0) {
1591 $result = $objectval->fetch((
int) $id);
1593 throw new RestException(404,
'Attribute not found');
1594 } elseif ($result < 0) {
1595 throw new RestException(500,
"Error fetching attribute");
1600 throw new RestException(500,
"Error updating attribute");
1616 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1617 throw new RestException(401);
1621 $objectval->id = (int) $id;
1623 if ($objectval->delete(DolibarrApiAccess::$user) > 0) {
1626 throw new RestException(500,
"Error deleting attribute value");
1643 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1644 throw new RestException(401);
1648 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $id);
1650 foreach ($combinations as $key => $combination) {
1652 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1653 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1655 if (!empty($includestock) && DolibarrApiAccess::$user->rights->stock->lire) {
1657 $productModel->fetch((
int) $combination->fk_product_child);
1658 $productModel->load_stock($includestock);
1659 $combinations[$key]->stock_warehouse = $this->_cleanObjectDatas($productModel)->stock_warehouse;
1663 return $combinations;
1679 if (!DolibarrApiAccess::$user->rights->produit->lire) {
1680 throw new RestException(401);
1683 $result = $this->product->fetch(
'', $ref);
1685 throw new RestException(404,
'Product not found');
1689 $combinations = $prodcomb->fetchAllByFkProductParent((
int) $this->product->id);
1691 foreach ($combinations as $key => $combination) {
1693 $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((
int) $combination->id);
1694 $combinations[$key] = $this->_cleanObjectDatas($combinations[$key]);
1697 return $combinations;
1720 public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features, $reference =
'', $ref_ext =
'')
1722 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1723 throw new RestException(401);
1726 if (empty($id) || empty($features) || !is_array($features)) {
1727 throw new RestException(401);
1730 $weight_impact =
price2num($weight_impact);
1731 $price_impact =
price2num($price_impact);
1735 foreach ($features as $id_attr => $id_value) {
1736 if ($prodattr->fetch((
int) $id_attr) < 0) {
1737 throw new RestException(401);
1739 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1740 throw new RestException(401);
1744 $result = $this->product->fetch((
int) $id);
1746 throw new RestException(404,
'Product not found');
1751 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference, $ref_ext);
1755 throw new RestException(500,
"Error creating new product variant");
1779 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1780 throw new RestException(401);
1783 if (empty($ref) || empty($features) || !is_array($features)) {
1784 throw new RestException(401);
1787 $weight_impact =
price2num($weight_impact);
1788 $price_impact =
price2num($price_impact);
1792 foreach ($features as $id_attr => $id_value) {
1793 if ($prodattr->fetch((
int) $id_attr) < 0) {
1794 throw new RestException(404);
1796 if ($prodattr_val->fetch((
int) $id_value) < 0) {
1797 throw new RestException(404);
1801 $result = $this->product->fetch(
'', trim($ref));
1803 throw new RestException(404,
'Product not found');
1807 if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) {
1808 $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact);
1812 throw new RestException(500,
"Error creating new product variant");
1815 return $prodcomb->id;
1833 if (!DolibarrApiAccess::$user->rights->produit->creer) {
1834 throw new RestException(401);
1838 $prodcomb->fetch((
int) $id);
1840 foreach ($request_data as $field => $value) {
1841 if ($field ==
'rowid') {
1844 $prodcomb->$field = $value;
1847 $result = $prodcomb->update(DolibarrApiAccess::$user);
1851 throw new RestException(500,
"Error editing variant");
1867 if (!DolibarrApiAccess::$user->rights->produit->supprimer) {
1868 throw new RestException(401);
1872 $prodcomb->id = (int) $id;
1873 $result = $prodcomb->delete(DolibarrApiAccess::$user);
1875 throw new RestException(500,
"Error deleting variant");
1894 public function getStock($id, $selected_warehouse_id =
null)
1897 if (!DolibarrApiAccess::$user->rights->produit->lire || !DolibarrApiAccess::$user->rights->stock->lire) {
1898 throw new RestException(401);
1902 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1905 $product_model =
new Product($this->
db);
1906 $product_model->fetch($id);
1907 $product_model->load_stock();
1909 $stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
1910 if ($selected_warehouse_id) {
1911 foreach ($stockData as $warehouse_id => $warehouse) {
1912 if ($warehouse_id != $selected_warehouse_id) {
1913 unset($stockData[$warehouse_id]);
1918 if (empty($stockData)) {
1919 throw new RestException(404,
'No stock found');
1922 return [
'stock_warehouses'=>$stockData];
1935 $object = parent::_cleanObjectDatas($object);
1937 unset($object->statut);
1939 unset($object->regeximgext);
1940 unset($object->price_by_qty);
1941 unset($object->prices_by_qty_id);
1942 unset($object->libelle);
1943 unset($object->product_id_already_linked);
1944 unset($object->reputations);
1946 unset($object->name);
1947 unset($object->firstname);
1948 unset($object->lastname);
1949 unset($object->civility_id);
1950 unset($object->contact);
1951 unset($object->contact_id);
1952 unset($object->thirdparty);
1953 unset($object->user);
1954 unset($object->origin);
1955 unset($object->origin_id);
1956 unset($object->fourn_pu);
1957 unset($object->fourn_price_base_type);
1958 unset($object->fourn_socid);
1959 unset($object->ref_fourn);
1960 unset($object->ref_supplier);
1961 unset($object->product_fourn_id);
1962 unset($object->fk_project);
1964 unset($object->mode_reglement_id);
1965 unset($object->cond_reglement_id);
1966 unset($object->demand_reason_id);
1967 unset($object->transport_mode_id);
1968 unset($object->cond_reglement);
1969 unset($object->shipping_method_id);
1970 unset($object->model_pdf);
1971 unset($object->note);
1973 unset($object->nbphoto);
1974 unset($object->recuperableonly);
1975 unset($object->multiprices_recuperableonly);
1976 unset($object->tva_npr);
1977 unset($object->lines);
1978 unset($object->fk_bank);
1979 unset($object->fk_account);
1981 unset($object->supplierprices);
1983 if (empty(DolibarrApiAccess::$user->rights->stock->lire)) {
1984 unset($object->stock_reel);
1985 unset($object->stock_theorique);
1986 unset($object->stock_warehouse);
2002 foreach (Products::$FIELDS as $field) {
2003 if (!isset($data[$field])) {
2004 throw new RestException(400,
"$field field missing");
2006 $product[$field] = $data[$field];
2030 private function _fetch($id, $ref =
'', $ref_ext =
'', $barcode =
'', $includestockdata = 0, $includesubproducts =
false, $includeparentid =
false, $includeifobjectisused =
false, $includetrans =
false)
2032 if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
2033 throw new RestException(400,
'bad value for parameter id, ref, ref_ext or barcode');
2036 $id = (empty($id) ? 0 : $id);
2038 if (!DolibarrApiAccess::$user->rights->produit->lire) {
2039 throw new RestException(403);
2042 $result = $this->product->fetch($id, $ref, $ref_ext, $barcode, 0, 0, ($includetrans ? 0 : 1));
2044 throw new RestException(404,
'Product not found');
2048 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2051 if (!empty($includestockdata) && DolibarrApiAccess::$user->rights->stock->lire) {
2052 $this->product->load_stock($includestockdata);
2054 if (is_array($this->product->stock_warehouse)) {
2055 foreach ($this->product->stock_warehouse as $keytmp => $valtmp) {
2056 if (isset($this->product->stock_warehouse[$keytmp]->detail_batch) && is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) {
2057 foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) {
2058 unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db);
2065 if ($includesubproducts) {
2066 $childsArbo = $this->product->getChildsArbo($id, 1);
2068 $keys = array(
'rowid',
'qty',
'fk_product_type',
'label',
'incdec',
'ref',
'fk_association',
'rang');
2070 foreach ($childsArbo as $values) {
2071 $childs[] = array_combine($keys, $values);
2074 $this->product->sousprods = $childs;
2077 if ($includeparentid) {
2079 $this->product->fk_product_parent =
null;
2080 if (($fk_product_parent = $prodcomb->fetchByFkProductChild($this->product->id)) > 0) {
2081 $this->product->fk_product_parent = $fk_product_parent;
2085 if ($includeifobjectisused) {
2086 $this->product->is_object_used = ($this->product->isObjectUsed() > 0);
2089 return $this->_cleanObjectDatas($this->product);