From f3e689f841c4b1a0a62779e32866d18d41f347a8 Mon Sep 17 00:00:00 2001 From: ATM-Nicolas Date: Sat, 26 Nov 2022 00:02:17 +0100 Subject: [PATCH 1/6] FIX : Documents API inconsistency --- htdocs/api/class/api_documents.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/htdocs/api/class/api_documents.class.php b/htdocs/api/class/api_documents.class.php index 3b6e89c6643..ba9035a1fbe 100644 --- a/htdocs/api/class/api_documents.class.php +++ b/htdocs/api/class/api_documents.class.php @@ -462,6 +462,9 @@ class Documents extends DolibarrApi throw new RestException(500, 'Modulepart '.$modulepart.' not implemented yet.'); } + $objectType = $modulepart; + if(! empty($object->id) && ! empty($object->table_element)) $objectType = $object->table_element; + $filearray = dol_dir_list($upload_dir, $type, $recursive, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1); if (empty($filearray)) { throw new RestException(404, 'Search for modulepart '.$modulepart.' with Id '.$object->id.(!empty($object->ref) ? ' or Ref '.$object->ref : '').' does not return any document.'); @@ -469,11 +472,13 @@ class Documents extends DolibarrApi if (($object->id) > 0 && !empty($modulepart)) { require_once DOL_DOCUMENT_ROOT . '/ecm/class/ecmfiles.class.php'; $ecmfile = new EcmFiles($this->db); - $result = $ecmfile->fetchAll('', '', 0, 0, array('t.src_object_type' => $modulepart, 't.src_object_id' => $object->id)); + $result = $ecmfile->fetchAll('', '', 0, 0, array('t.src_object_type' => $objectType, 't.src_object_id' => $object->id)); if ($result < 0) { throw new RestException(503, 'Error when retrieve ecm list : ' . $this->db->lasterror()); } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) { - $filearray['ecmfiles_infos'] = $ecmfile->lines; + for($i = 0 ; $i < count($filearray) ; $i++) { + if($filearray[$i]['name'] == $ecmfile->lines[$i]->filename) $filearray[$i] = array_merge($filearray[$i], (array) $ecmfile->lines[0]); + } } } } From a0553c6e5253e589fbf538c5cd678548b8534620 Mon Sep 17 00:00:00 2001 From: ATM-Nicolas Date: Sat, 26 Nov 2022 00:35:08 +0100 Subject: [PATCH 2/6] FIX : Stickler --- htdocs/api/class/api_documents.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/api/class/api_documents.class.php b/htdocs/api/class/api_documents.class.php index ba9035a1fbe..88c5ecd604c 100644 --- a/htdocs/api/class/api_documents.class.php +++ b/htdocs/api/class/api_documents.class.php @@ -463,7 +463,7 @@ class Documents extends DolibarrApi } $objectType = $modulepart; - if(! empty($object->id) && ! empty($object->table_element)) $objectType = $object->table_element; + if (! empty($object->id) && ! empty($object->table_element)) $objectType = $object->table_element; $filearray = dol_dir_list($upload_dir, $type, $recursive, '', '(\.meta|_preview.*\.png)$', $sortfield, (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC), 1); if (empty($filearray)) { @@ -476,8 +476,8 @@ class Documents extends DolibarrApi if ($result < 0) { throw new RestException(503, 'Error when retrieve ecm list : ' . $this->db->lasterror()); } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) { - for($i = 0 ; $i < count($filearray) ; $i++) { - if($filearray[$i]['name'] == $ecmfile->lines[$i]->filename) $filearray[$i] = array_merge($filearray[$i], (array) $ecmfile->lines[0]); + for ($i = 0 ; $i < count($filearray); $i++) { + if ($filearray[$i]['name'] == $ecmfile->lines[$i]->filename) $filearray[$i] = array_merge($filearray[$i], (array) $ecmfile->lines[0]); } } } From e767c9b4a98427ead0204d2933d6391f934b8cf0 Mon Sep 17 00:00:00 2001 From: ATM-Nicolas Date: Sat, 26 Nov 2022 00:42:50 +0100 Subject: [PATCH 3/6] FIX : Stickler --- htdocs/api/class/api_documents.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/htdocs/api/class/api_documents.class.php b/htdocs/api/class/api_documents.class.php index 88c5ecd604c..197ca42469d 100644 --- a/htdocs/api/class/api_documents.class.php +++ b/htdocs/api/class/api_documents.class.php @@ -476,7 +476,8 @@ class Documents extends DolibarrApi if ($result < 0) { throw new RestException(503, 'Error when retrieve ecm list : ' . $this->db->lasterror()); } elseif (is_array($ecmfile->lines) && count($ecmfile->lines) > 0) { - for ($i = 0 ; $i < count($filearray); $i++) { + $count = count($filearray); + for ($i = 0 ; $i < $count ; $i++) { if ($filearray[$i]['name'] == $ecmfile->lines[$i]->filename) $filearray[$i] = array_merge($filearray[$i], (array) $ecmfile->lines[0]); } } From 616e2b79efb0381377190ccdd1fe9dfee48cd876 Mon Sep 17 00:00:00 2001 From: Eric Seigne Date: Sat, 26 Nov 2022 00:50:45 +0100 Subject: [PATCH 4/6] fix #22766: better API response HTTP code --- htdocs/adherents/class/api_members.class.php | 10 +++++++--- .../adherents/class/api_memberstypes.class.php | 9 ++++++--- .../adherents/class/api_subscriptions.class.php | 9 ++++++--- htdocs/product/class/api_products.class.php | 14 +++++++++++++- htdocs/societe/class/api_thirdparties.class.php | 17 ++++++++++++++++- htdocs/user/class/api_users.class.php | 13 ++++++++++++- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/htdocs/adherents/class/api_members.class.php b/htdocs/adherents/class/api_members.class.php index d57a01510d4..f5efbd8582e 100644 --- a/htdocs/adherents/class/api_members.class.php +++ b/htdocs/adherents/class/api_members.class.php @@ -382,14 +382,18 @@ class Members extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - if (!$member->delete($member->id, DolibarrApiAccess::$user)) { - throw new RestException(401, 'error when deleting member'); + + $res = $member->delete($member->id, DolibarrApiAccess::$user); + if($res < 0) { + throw new RestException(500, "Can't delete, error occurs"); + }elseif($res == 0) { + throw new RestException(409, "Can't delete, that product is probably used"); } return array( 'success' => array( 'code' => 200, - 'message' => 'member deleted' + 'message' => 'Member deleted' ) ); } diff --git a/htdocs/adherents/class/api_memberstypes.class.php b/htdocs/adherents/class/api_memberstypes.class.php index b02a81dc5c9..a4ce0ddf1f4 100644 --- a/htdocs/adherents/class/api_memberstypes.class.php +++ b/htdocs/adherents/class/api_memberstypes.class.php @@ -228,14 +228,17 @@ class MembersTypes extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - if (!$membertype->delete()) { - throw new RestException(401, 'error when deleting member type'); + $res = $membertype->delete(); + if($res < 0) { + throw new RestException(500, "Can't delete, error occurs"); + }elseif($res == 0) { + throw new RestException(409, "Can't delete, that product is probably used"); } return array( 'success' => array( 'code' => 200, - 'message' => 'member type deleted' + 'message' => 'Member type deleted' ) ); } diff --git a/htdocs/adherents/class/api_subscriptions.class.php b/htdocs/adherents/class/api_subscriptions.class.php index 831be2882f1..1cada6c7f3e 100644 --- a/htdocs/adherents/class/api_subscriptions.class.php +++ b/htdocs/adherents/class/api_subscriptions.class.php @@ -214,14 +214,17 @@ class Subscriptions extends DolibarrApi throw new RestException(404, 'Subscription not found'); } - if (!$subscription->delete(DolibarrApiAccess::$user)) { - throw new RestException(401, 'error when deleting subscription'); + $res = $subscription->delete(DolibarrApiAccess::$user); + if($res < 0) { + throw new RestException(500, "Can't delete, error occurs"); + }elseif($res == 0) { + throw new RestException(409, "Can't delete, that product is probably used"); } return array( 'success' => array( 'code' => 200, - 'message' => 'subscription deleted' + 'message' => 'Subscription deleted' ) ); } diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 6283b692a28..b3dd2c019df 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -435,7 +435,19 @@ class Products extends DolibarrApi global $user; $user = DolibarrApiAccess::$user; - return $this->product->delete(DolibarrApiAccess::$user); + $res = $this->product->delete(DolibarrApiAccess::$user); + if($res < 0) { + throw new RestException(500, "Can't delete, error occurs"); + }elseif($res == 0) { + throw new RestException(409, "Can't delete, that product is probably used"); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Object deleted' + ) + ); } /** diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 0257b692b62..4646175878c 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -525,7 +525,22 @@ class Thirdparties extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } $this->company->oldcopy = clone $this->company; - return $this->company->delete($id); + + $res = $this->company->delete($id); + if($res < 0) { + throw new RestException(500, "Can't delete, error occurs"); + }elseif($res == 0) { + throw new RestException(409, "Can't delete, that product is probably used"); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Object deleted' + ) + ); + + } /** diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index e0cbd849092..83cd88f8ade 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -628,7 +628,18 @@ class Users extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } $this->useraccount->oldcopy = clone $this->useraccount; - return $this->useraccount->delete(DolibarrApiAccess::$user); + + if (!$this->useraccount->delete(DolibarrApiAccess::$user)) { + throw new RestException(500); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Ticket deleted' + ) + ); + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore From 3fd81293a389cb8e0ad2fa3a98ac624da8b43424 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 25 Nov 2022 23:57:35 +0000 Subject: [PATCH 5/6] Fixing style errors. --- htdocs/adherents/class/api_members.class.php | 4 ++-- htdocs/adherents/class/api_memberstypes.class.php | 4 ++-- htdocs/adherents/class/api_subscriptions.class.php | 4 ++-- htdocs/product/class/api_products.class.php | 4 ++-- htdocs/societe/class/api_thirdparties.class.php | 6 ++---- htdocs/user/class/api_users.class.php | 1 - 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/htdocs/adherents/class/api_members.class.php b/htdocs/adherents/class/api_members.class.php index f5efbd8582e..9038ada74d3 100644 --- a/htdocs/adherents/class/api_members.class.php +++ b/htdocs/adherents/class/api_members.class.php @@ -384,9 +384,9 @@ class Members extends DolibarrApi $res = $member->delete($member->id, DolibarrApiAccess::$user); - if($res < 0) { + if ($res < 0) { throw new RestException(500, "Can't delete, error occurs"); - }elseif($res == 0) { + } elseif ($res == 0) { throw new RestException(409, "Can't delete, that product is probably used"); } diff --git a/htdocs/adherents/class/api_memberstypes.class.php b/htdocs/adherents/class/api_memberstypes.class.php index a4ce0ddf1f4..eb561ff6c60 100644 --- a/htdocs/adherents/class/api_memberstypes.class.php +++ b/htdocs/adherents/class/api_memberstypes.class.php @@ -229,9 +229,9 @@ class MembersTypes extends DolibarrApi } $res = $membertype->delete(); - if($res < 0) { + if ($res < 0) { throw new RestException(500, "Can't delete, error occurs"); - }elseif($res == 0) { + } elseif ($res == 0) { throw new RestException(409, "Can't delete, that product is probably used"); } diff --git a/htdocs/adherents/class/api_subscriptions.class.php b/htdocs/adherents/class/api_subscriptions.class.php index 1cada6c7f3e..b3871cb08d6 100644 --- a/htdocs/adherents/class/api_subscriptions.class.php +++ b/htdocs/adherents/class/api_subscriptions.class.php @@ -215,9 +215,9 @@ class Subscriptions extends DolibarrApi } $res = $subscription->delete(DolibarrApiAccess::$user); - if($res < 0) { + if ($res < 0) { throw new RestException(500, "Can't delete, error occurs"); - }elseif($res == 0) { + } elseif ($res == 0) { throw new RestException(409, "Can't delete, that product is probably used"); } diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index b3dd2c019df..445387fb2b5 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -436,9 +436,9 @@ class Products extends DolibarrApi $user = DolibarrApiAccess::$user; $res = $this->product->delete(DolibarrApiAccess::$user); - if($res < 0) { + if ($res < 0) { throw new RestException(500, "Can't delete, error occurs"); - }elseif($res == 0) { + } elseif ($res == 0) { throw new RestException(409, "Can't delete, that product is probably used"); } diff --git a/htdocs/societe/class/api_thirdparties.class.php b/htdocs/societe/class/api_thirdparties.class.php index 4646175878c..27da78e9456 100644 --- a/htdocs/societe/class/api_thirdparties.class.php +++ b/htdocs/societe/class/api_thirdparties.class.php @@ -527,9 +527,9 @@ class Thirdparties extends DolibarrApi $this->company->oldcopy = clone $this->company; $res = $this->company->delete($id); - if($res < 0) { + if ($res < 0) { throw new RestException(500, "Can't delete, error occurs"); - }elseif($res == 0) { + } elseif ($res == 0) { throw new RestException(409, "Can't delete, that product is probably used"); } @@ -539,8 +539,6 @@ class Thirdparties extends DolibarrApi 'message' => 'Object deleted' ) ); - - } /** diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index 83cd88f8ade..0acfe30d279 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -639,7 +639,6 @@ class Users extends DolibarrApi 'message' => 'Ticket deleted' ) ); - } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore From 7017b37354a8a0820eafc0eb46a4d19a22eef67b Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 26 Nov 2022 16:28:58 +0100 Subject: [PATCH 6/6] FIX product_type 9 --- htdocs/compta/facture/class/facture-rec.class.php | 4 ++-- htdocs/delivery/class/delivery.class.php | 2 +- htdocs/expensereport/class/expensereport.class.php | 10 +++++----- htdocs/fourn/class/fournisseur.commande.class.php | 6 +++--- htdocs/fourn/class/fournisseur.facture.class.php | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/htdocs/compta/facture/class/facture-rec.class.php b/htdocs/compta/facture/class/facture-rec.class.php index 8306a5c9c0d..9d0a8212ba5 100644 --- a/htdocs/compta/facture/class/facture-rec.class.php +++ b/htdocs/compta/facture/class/facture-rec.class.php @@ -992,7 +992,7 @@ class FactureRec extends CommonInvoice if ($this->db->query($sql)) { $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec"); $this->id = $facid; - $this->update_price(); + $this->update_price(1); return $lineId; } else { $this->error = $this->db->lasterror(); @@ -1166,7 +1166,7 @@ class FactureRec extends CommonInvoice dol_syslog(get_class($this)."::updateline", LOG_DEBUG); if ($this->db->query($sql)) { $this->id = $facid; - $this->update_price(); + $this->update_price(1); return 1; } else { $this->error = $this->db->lasterror(); diff --git a/htdocs/delivery/class/delivery.class.php b/htdocs/delivery/class/delivery.class.php index 6bfe3a33dd7..e7ccffd45af 100644 --- a/htdocs/delivery/class/delivery.class.php +++ b/htdocs/delivery/class/delivery.class.php @@ -634,7 +634,7 @@ class Delivery extends CommonObject $sql .= " WHERE rowid = ".((int) $lineid); if ($this->db->query($sql)) { - $this->update_price(); + $this->update_price(1); return 1; } else { diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index 3f62087dd0b..8d8de8a9d5a 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -359,7 +359,7 @@ class ExpenseReport extends CommonObject } if (!$error) { - $result = $this->update_price(); + $result = $this->update_price(1); if ($result > 0) { if (!$notrigger) { // Call trigger @@ -1841,7 +1841,7 @@ class ExpenseReport extends CommonObject $result = $this->line->insert(0, true); if ($result > 0) { - $result = $this->update_price(); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. + $result = $this->update_price(1); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. if ($result > 0) { $this->db->commit(); return $this->line->id; @@ -2180,7 +2180,7 @@ class ExpenseReport extends CommonObject return -1; } - $this->update_price(); + $this->update_price(1); $this->db->commit(); @@ -2747,7 +2747,7 @@ class ExpenseReportLine if (!$fromaddline) { $tmpparent = new ExpenseReport($this->db); $tmpparent->fetch($this->fk_expensereport); - $result = $tmpparent->update_price(); + $result = $tmpparent->update_price(1); if ($result < 0) { $error++; $this->error = $tmpparent->error; @@ -2874,7 +2874,7 @@ class ExpenseReportLine $tmpparent = new ExpenseReport($this->db); $result = $tmpparent->fetch($this->fk_expensereport); if ($result > 0) { - $result = $tmpparent->update_price(); + $result = $tmpparent->update_price(1); if ($result < 0) { $error++; $this->error = $tmpparent->error; diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index e78dd9dd02a..d7d9cb26194 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -2067,7 +2067,7 @@ class CommandeFournisseur extends CommonOrder } if ($line->delete($notrigger) > 0) { - $this->update_price(); + $this->update_price(1); return 1; } else { $this->error = $line->error; @@ -2564,7 +2564,7 @@ class CommandeFournisseur extends CommonOrder $sql .= ", ".$comclient->lines[$i]->qty.", ".$comclient->lines[$i]->tva_tx.", ".$comclient->lines[$i]->localtax1_tx.", ".$comclient->lines[$i]->localtax2_tx.", ".$comclient->lines[$i]->remise_percent; $sql .= ", '".price2num($comclient->lines[$i]->subprice)."','0', '".$this->db->escape($ref)."');"; if ($this->db->query($sql)) { - $this->update_price(); + $this->update_price(1); } } @@ -2817,7 +2817,7 @@ class CommandeFournisseur extends CommonOrder // Mise a jour info denormalisees au niveau facture if ($result >= 0) { - $this->update_price('', 'auto'); + $this->update_price('1', 'auto'); $this->db->commit(); return $result; } else { diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index c4635962368..c8036b5610d 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -590,7 +590,7 @@ class FactureFournisseur extends CommonInvoice } // Update total price - $result = $this->update_price(); + $result = $this->update_price(1); if ($result > 0) { // Actions on extra fields if (!$error) { @@ -2192,7 +2192,7 @@ class FactureFournisseur extends CommonInvoice $this->errors[] = $line->error; } else { // Update total price into invoice record - $res = $this->update_price('', 'auto', 0, $this->thirdparty); + $res = $this->update_price('1', 'auto', 0, $this->thirdparty); } return $res; @@ -2239,7 +2239,7 @@ class FactureFournisseur extends CommonInvoice $this->db->rollback(); return -3; } else { - $res = $this->update_price(); + $res = $this->update_price(1); if ($res > 0) { $this->db->commit();