28use Luracast\Restler\RestException;
43 public static $FIELDS = array(
60 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
62 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
63 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
64 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
66 $this->company =
new Societe($this->db);
69 static::$FIELDS[] =
'email';
85 public function get(
$id)
108 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
127 return $this->
_fetch(
null,
'',
'', $barcode);
152 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
156 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
157 throw new RestException(403);
161 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
165 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
166 $search_sale = DolibarrApiAccess::$user->id;
169 $sql =
"SELECT t.rowid";
170 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
171 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
174 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
176 if (!in_array($mode, array(1, 2, 3))) {
177 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
180 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
181 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
182 $sql .=
" AND t.fk_stcomm = st.id";
184 $sql .=
" AND t.client IN (1, 3)";
185 } elseif ($mode == 2) {
186 $sql .=
" AND t.client IN (2, 3)";
187 } elseif ($mode == 3) {
188 $sql .=
" AND t.client IN (0)";
189 } elseif ($mode == 4) {
190 $sql .=
" AND t.fournisseur IN (1)";
194 if (!empty($mode) && $mode != 4) {
195 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
196 } elseif (!empty($mode) && $mode == 4) {
197 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
199 $sql .=
" AND ((c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid))";
203 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
206 if ($search_sale && $search_sale !=
'-1') {
207 if ($search_sale == -2) {
208 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
209 } elseif ($search_sale > 0) {
210 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid AND sc.fk_user = ".((int) $search_sale).
")";
218 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
223 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
225 $sql .= $this->db->order($sortfield, $sortorder);
230 $offset = $limit * $page;
232 $sql .= $this->db->plimit($limit + 1, $offset);
235 $result = $this->db->query($sql);
237 $num = $this->db->num_rows($result);
238 $min = min($num, ($limit <= 0 ? $num : $limit));
241 $obj = $this->db->fetch_object($result);
242 $soc_static =
new Societe($this->db);
243 if ($soc_static->fetch($obj->rowid)) {
244 if (isModEnabled(
'mailing')) {
245 $soc_static->getNoEmail();
252 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
254 if (!count($obj_ret)) {
258 $message =
'No third parties found';
261 $message =
'No customers found';
264 $message =
'No prospects found';
267 $message =
'No other third parties found';
270 $message =
'No suppliers found';
272 throw new RestException(404, $message);
276 if ($pagination_data) {
277 $totalsResult = $this->db->query($sqlTotals);
278 $total = $this->db->fetch_object($totalsResult)->total;
283 $obj_ret[
'data'] = $tmp;
284 $obj_ret[
'pagination'] = [
285 'total' => (int) $total,
287 'page_count' => ceil((
int) $total / $limit),
307 public function post($request_data =
null)
309 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
310 throw new RestException(403);
313 $result = $this->
_validate($request_data);
315 foreach ($request_data as $field => $value) {
316 if ($field ===
'caller') {
318 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
322 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
325 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
326 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
328 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
329 $this->company->setNoEmail($this->company->no_email);
332 return $this->company->id;
352 public function put(
$id, $request_data =
null)
354 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
355 throw new RestException(403);
358 $result = $this->company->fetch(
$id);
360 throw new RestException(404,
'Thirdparty not found');
364 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
367 foreach ($request_data as $field => $value) {
368 if ($field ==
'id') {
371 if ($field ===
'caller') {
373 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
376 if ($field ==
'array_options' && is_array($value)) {
377 foreach ($value as $index => $val) {
378 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
382 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
385 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
386 $this->company->setNoEmail($this->company->no_email);
389 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
390 return $this->
get(
$id);
392 throw new RestException(500, $this->company->error);
419 if (
$id == $idtodelete) {
420 throw new RestException(400,
'Try to merge a thirdparty into itself');
423 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
424 throw new RestException(403);
427 $result = $this->company->fetch(
$id);
429 throw new RestException(404,
'Thirdparty not found');
433 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 $companytoremove =
new Societe($this->db);
437 $result = $companytoremove->fetch($idtodelete);
439 throw new RestException(404,
'Thirdparty not found');
443 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
446 $user = DolibarrApiAccess::$user;
447 $result = $this->company->mergeCompany($companytoremove->id);
449 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
452 return $this->
get(
$id);
467 public function delete(
$id)
469 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
470 throw new RestException(403);
472 $result = $this->company->fetch(
$id);
474 throw new RestException(404,
'Thirdparty not found');
477 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 $this->company->oldcopy = clone $this->company;
481 $res = $this->company->delete(
$id);
483 throw new RestException(500,
"Can't delete, error occurs");
484 } elseif ($res == 0) {
485 throw new RestException(409,
"Can't delete, that product is probably used");
491 'message' =>
'Object deleted'
517 if (!isModEnabled(
'societe')) {
518 throw new RestException(501,
'Module "Thirdparties" needed for this request');
521 if (!isModEnabled(
"product")) {
522 throw new RestException(501,
'Module "Products" needed for this request');
526 throw new RestException(501,
'Multiprices features activation needed for this request');
529 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
530 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
533 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
534 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
537 $result = $this->company->fetch(
$id);
539 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
542 if (empty($result)) {
543 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
547 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
550 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
552 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
574 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
575 throw new RestException(403);
577 $result = $this->company->fetch(
$id);
579 throw new RestException(404,
'Thirdparty not found');
581 $usertmp =
new User($this->db);
582 $result = $usertmp->fetch($representative_id);
584 throw new RestException(404,
'User not found');
587 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
589 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
610 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
611 throw new RestException(403);
613 $result = $this->company->fetch(
$id);
615 throw new RestException(404,
'Thirdparty not found');
617 $usertmp =
new User($this->db);
618 $result = $usertmp->fetch($representative_id);
620 throw new RestException(404,
'User not found');
623 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
625 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
648 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
650 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
651 throw new RestException(403);
654 $result = $this->company->fetch(
$id);
656 throw new RestException(404,
'Thirdparty not found');
661 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
663 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
664 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
667 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
671 return $arrayofcateg;
692 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
693 throw new RestException(403);
696 $result = $this->company->fetch(
$id);
698 throw new RestException(404,
'Thirdparty not found');
701 $result = $category->fetch($category_id);
703 throw new RestException(404,
'category not found');
707 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
710 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
713 $category->add_type($this->company,
'customer');
736 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
737 throw new RestException(403);
740 $result = $this->company->fetch(
$id);
742 throw new RestException(404,
'Thirdparty not found');
745 $result = $category->fetch($category_id);
747 throw new RestException(404,
'category not found');
751 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
754 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
757 $category->del_type($this->company,
'customer');
783 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
784 throw new RestException(403);
787 $result = $this->company->fetch(
$id);
789 throw new RestException(404,
'Thirdparty not found');
794 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
796 if (is_numeric($result) && $result < 0) {
797 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
800 if (is_numeric($result) && $result == 0) {
825 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
826 throw new RestException(403);
829 $result = $this->company->fetch(
$id);
831 throw new RestException(404,
'Thirdparty not found');
834 $result = $category->fetch($category_id);
836 throw new RestException(404,
'category not found');
840 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
846 $category->add_type($this->company,
'supplier');
869 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
870 throw new RestException(403);
873 $result = $this->company->fetch(
$id);
875 throw new RestException(404,
'Thirdparty not found');
878 $result = $category->fetch($category_id);
880 throw new RestException(404,
'category not found');
884 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
887 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
890 $category->del_type($this->company,
'supplier');
916 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
917 throw new RestException(403);
921 throw new RestException(400,
'Thirdparty ID is mandatory');
925 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
928 $result = $this->company->fetch(
$id);
930 throw new RestException(404,
'Thirdparty not found');
933 $result = $this->company->getOutstandingProposals($mode);
935 unset($result[
'total_ht']);
936 unset($result[
'total_ttc']);
962 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
963 throw new RestException(403);
967 throw new RestException(400,
'Thirdparty ID is mandatory');
971 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
974 $result = $this->company->fetch(
$id);
976 throw new RestException(404,
'Thirdparty not found');
979 $result = $this->company->getOutstandingOrders($mode);
981 unset($result[
'total_ht']);
982 unset($result[
'total_ttc']);
1007 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1008 throw new RestException(403);
1012 throw new RestException(400,
'Thirdparty ID is mandatory');
1016 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1019 $result = $this->company->fetch(
$id);
1021 throw new RestException(404,
'Thirdparty not found');
1024 $result = $this->company->getOutstandingBills($mode);
1026 unset($result[
'total_ht']);
1027 unset($result[
'total_ttc']);
1052 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1053 throw new RestException(403);
1057 throw new RestException(400,
'Thirdparty ID is mandatory');
1061 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1064 $result = $this->company->fetch(
$id);
1066 throw new RestException(404,
'Thirdparty not found');
1069 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1101 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1102 throw new RestException(403);
1106 throw new RestException(400,
'Thirdparty ID is mandatory');
1110 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1113 $result = $this->company->fetch(
$id);
1115 throw new RestException(404,
'Thirdparty not found');
1119 $sql =
"SELECT f.ref, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line";
1120 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1121 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1122 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1123 if ($filter ==
"available") {
1124 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1126 if ($filter ==
"used") {
1127 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1130 $sql .= $this->db->order($sortfield, $sortorder);
1132 $result = $this->db->query($sql);
1134 throw new RestException(503, $this->db->lasterror());
1136 $num = $this->db->num_rows($result);
1137 while ($obj = $this->db->fetch_object($result)) {
1166 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1167 throw new RestException(403);
1170 throw new RestException(400,
'Thirdparty ID is mandatory');
1174 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1182 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1183 $invoice =
new Facture($this->db);
1184 $result = $invoice->list_replacable_invoices(
$id);
1186 throw new RestException(405, $invoice->error);
1215 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1216 throw new RestException(403);
1219 throw new RestException(400,
'Thirdparty ID is mandatory');
1223 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1231 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1232 $invoice =
new Facture($this->db);
1233 $result = $invoice->list_qualified_avoir_invoices(
$id);
1234 if (!is_array($result) && $result < 0) {
1235 throw new RestException(405, $invoice->error);
1259 throw new RestException(400,
'Thirdparty ID is mandatory');
1261 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1262 throw new RestException(403);
1265 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1272 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1273 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1275 $sql .=
" WHERE fk_soc = ".((int)
$id);
1278 $result = $this->db->query($sql);
1279 if ($this->db->num_rows($result) == 0) {
1280 throw new RestException(404,
'Notification not found');
1285 $notifications = array();
1288 $num = $this->db->num_rows($result);
1290 $obj = $this->db->fetch_object($result);
1291 $notifications[] = $obj;
1295 throw new RestException(404,
'No notifications found');
1298 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1300 $returnNotifications = array();
1302 foreach ($notifications as $notification) {
1304 foreach ($notification as $key => $value) {
1305 if (in_array($key, $fields)) {
1309 $returnNotifications[] =
$object;
1313 return $returnNotifications;
1334 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1335 throw new RestException(403,
"User has no right to update thirdparties");
1337 if ($this->company->fetch(
$id) <= 0) {
1338 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1340 $notification =
new Notify($this->db);
1342 $notification->socid =
$id;
1344 foreach ($request_data as $field => $value) {
1345 $notification->$field = $value;
1348 $event = $notification->event;
1350 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1352 $socid = $notification->socid;
1353 $contact_id = $notification->contact_id;
1355 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1356 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1357 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1358 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1359 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1361 $exists_result = $this->db->query($exists_sql);
1362 if ($this->db->num_rows($exists_result) > 0) {
1363 throw new RestException(403,
'Notification already exists');
1366 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1367 throw new RestException(500,
'Error creating Thirdparty Notification');
1370 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1371 throw new RestException(500,
'Error updating values');
1397 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1398 throw new RestException(403,
"User has no right to update thirdparties");
1400 if ($this->company->fetch(
$id) <= 0) {
1401 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1403 $notification =
new Notify($this->db);
1404 $notification->socid =
$id;
1406 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1407 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1409 $result = $this->db->query($sql);
1410 if ($this->db->num_rows($result) == 0) {
1411 throw new RestException(404,
'Action Trigger code not found');
1414 $notification->event = $this->db->fetch_row($result)[0];
1415 foreach ($request_data as $field => $value) {
1416 if ($field ===
'event') {
1417 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1419 if ($field ===
'fk_action') {
1420 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1422 $notification->$field = $value;
1425 $event = $notification->event;
1426 $socid = $notification->socid;
1427 $contact_id = $notification->contact_id;
1429 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1430 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1431 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1432 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1433 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1435 $exists_result = $this->db->query($exists_sql);
1436 if ($this->db->num_rows($exists_result) > 0) {
1437 throw new RestException(403,
'Notification already exists');
1440 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1441 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1444 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1445 throw new RestException(500,
'Error updating values');
1467 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1468 throw new RestException(403);
1471 $notification =
new Notify($this->db);
1473 $notification->fetch($notification_id);
1475 $socid = (int) $notification->socid;
1477 if ($socid ==
$id) {
1478 return $notification->delete(DolibarrApiAccess::$user);
1480 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1503 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1504 throw new RestException(403,
"User has no right to update thirdparties");
1506 if ($this->company->fetch(
$id) <= 0) {
1507 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1509 $notification =
new Notify($this->db);
1512 $notification->fetch($notification_id,
$id);
1514 if ($notification->socid !=
$id) {
1515 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1518 foreach ($request_data as $field => $value) {
1519 $notification->$field = $value;
1522 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1523 throw new RestException(500,
'Error updating values');
1546 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1547 throw new RestException(403);
1550 throw new RestException(400,
'Thirdparty ID is mandatory');
1554 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1561 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1562 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1563 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1565 $sql .=
" WHERE fk_soc = ".((int)
$id);
1568 $result = $this->db->query($sql);
1570 if ($this->db->num_rows($result) == 0) {
1571 throw new RestException(404,
'Account not found');
1576 $accounts = array();
1579 $num = $this->db->num_rows($result);
1581 $obj = $this->db->fetch_object($result);
1584 if ($account->fetch($obj->rowid)) {
1585 $accounts[] = $account;
1590 throw new RestException(404,
'Account not found');
1594 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1596 $returnAccounts = array();
1598 foreach ($accounts as $account) {
1600 foreach ($account as $key => $value) {
1601 if (in_array($key, $fields)) {
1602 if ($key ==
'iban') {
1612 return $returnAccounts;
1633 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1634 throw new RestException(403);
1636 if ($this->company->fetch(
$id) <= 0) {
1637 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1641 $account->socid =
$id;
1643 foreach ($request_data as $field => $value) {
1644 if ($field ===
'caller') {
1646 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1650 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1653 if ($account->create(DolibarrApiAccess::$user) < 0) {
1654 throw new RestException(500,
'Error creating Company Bank account');
1657 if (empty($account->rum)) {
1658 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1660 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1661 $account->date_rum =
dol_now();
1664 if ($account->update(DolibarrApiAccess::$user) < 0) {
1665 throw new RestException(500,
'Error updating values');
1690 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1691 throw new RestException(403);
1693 if ($this->company->fetch(
$id) <= 0) {
1694 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1699 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
1701 if ($account->socid !=
$id) {
1702 throw new RestException(403);
1706 foreach ($request_data as $field => $value) {
1707 if ($field ===
'caller') {
1709 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1716 if (empty($account->rum)) {
1717 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1719 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1720 $account->date_rum =
dol_now();
1723 if ($account->update(DolibarrApiAccess::$user) < 0) {
1724 throw new RestException(500,
'Error updating values');
1746 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1747 throw new RestException(403);
1752 $account->fetch($bankaccount_id);
1754 $socid = (int) $account->socid;
1756 if ($socid ==
$id) {
1757 return $account->delete(DolibarrApiAccess::$user);
1759 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1783 global
$conf, $langs;
1785 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1787 if ($this->company->fetch(
$id) <= 0) {
1788 throw new RestException(404,
'Thirdparty not found');
1791 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1792 throw new RestException(403);
1795 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1797 $this->company->fk_bank = $this->company->fk_account;
1800 $outputlangs = $langs;
1805 if (isset($this->company->thirdparty->default_lang)) {
1806 $newlang = $this->company->thirdparty->default_lang;
1807 } elseif (isset($this->company->default_lang)) {
1808 $newlang = $this->company->default_lang;
1811 if (!empty($newlang)) {
1813 $outputlangs->setDefaultLang($newlang);
1816 $sql =
"SELECT rowid";
1817 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1819 $sql .=
" WHERE fk_soc = ".((int)
$id);
1821 if ($companybankid) {
1822 $sql .=
" AND rowid = ".((int) $companybankid);
1826 $accounts = array();
1828 $result = $this->db->query($sql);
1830 if ($this->db->num_rows($result) == 0) {
1831 throw new RestException(404,
'Bank account not found');
1834 $num = $this->db->num_rows($result);
1836 $obj = $this->db->fetch_object($result);
1839 if ($account->fetch($obj->rowid)) {
1840 $accounts[] = $account;
1845 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1848 $moreparams = array(
1849 'use_companybankid' => $accounts[0]->
id,
1850 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
1853 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1856 return array(
"success" => $result);
1858 throw new RestException(500,
'Error generating the document '.$this->company->error);
1881 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1882 throw new RestException(403);
1886 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1892 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1893 $sql .=
" WHERE fk_soc = ".((int)
$id);
1895 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1898 $result = $this->db->query($sql);
1900 if ($result && $this->db->num_rows($result) == 0) {
1901 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
1906 $accounts = array();
1908 $num = $this->db->num_rows($result);
1910 $obj = $this->db->fetch_object($result);
1913 if ($account->fetch($obj->rowid)) {
1914 $accounts[] = $account;
1919 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1921 $returnAccounts = array();
1923 foreach ($accounts as $account) {
1925 foreach ($account as $key => $value) {
1926 if (in_array($key, $fields)) {
1933 return $returnAccounts;
1955 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1956 throw new RestException(403);
1959 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1960 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
1961 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
1963 $result = $this->db->query($sql);
1965 if ($result && $this->db->num_rows($result) == 1) {
1966 $obj = $this->db->fetch_object($result);
1967 $returnThirdparty = $this->
_fetch($obj->fk_soc);
1969 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
1973 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1976 return $returnThirdparty;
2004 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2005 throw new RestException(403);
2008 if (!isset($request_data[
'site'])) {
2009 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2012 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2013 $result = $this->db->query($sql);
2015 if ($result && $this->db->num_rows($result) == 0) {
2017 if (!isset($request_data[
'login'])) {
2018 $account->login =
"";
2020 $account->fk_soc =
$id;
2022 foreach ($request_data as $field => $value) {
2023 if ($field ===
'caller') {
2025 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2032 if ($account->create(DolibarrApiAccess::$user) < 0) {
2033 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2040 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2072 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2073 throw new RestException(403);
2076 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2077 $result = $this->db->query($sql);
2080 if ($result && $this->db->num_rows($result) == 0) {
2081 if (!isset($request_data[
'key_account'])) {
2082 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2085 if (!isset($request_data[
'login'])) {
2086 $account->login =
"";
2089 foreach ($request_data as $field => $value) {
2090 if ($field ===
'caller') {
2092 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2099 $account->fk_soc =
$id;
2100 $account->site = $site;
2102 if ($account->create(DolibarrApiAccess::$user) < 0) {
2103 throw new RestException(500,
'Error creating SocieteAccount entity.');
2107 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2108 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2109 $result = $this->db->query($sql);
2111 if ($result && $this->db->num_rows($result) !== 0) {
2112 throw new RestException(409,
"You are trying to update this thirdparty Account for $site to ".$request_data[
'site'].
" but another Account already exists with this site key.");
2116 $obj = $this->db->fetch_object($result);
2119 $account->id = $obj->rowid;
2120 $account->fk_soc =
$id;
2121 $account->site = $site;
2122 if (!isset($request_data[
'login'])) {
2123 $account->login =
"";
2125 $account->fk_user_creat = $obj->fk_user_creat;
2126 $account->date_creation = $obj->date_creation;
2128 foreach ($request_data as $field => $value) {
2129 if ($field ===
'caller') {
2131 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2138 if ($account->update(DolibarrApiAccess::$user) < 0) {
2139 throw new RestException(500,
'Error updating SocieteAccount entity.');
2170 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2171 throw new RestException(403);
2174 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2175 $result = $this->db->query($sql);
2177 if ($result && $this->db->num_rows($result) == 0) {
2178 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2181 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2182 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2183 $result = $this->db->query($sql);
2185 if ($result && $this->db->num_rows($result) !== 0) {
2186 throw new RestException(409,
"You are trying to update this thirdparty Account for ".$site.
" to ".$request_data[
'site'].
" but another Account already exists for this thirdparty with this site key.");
2190 $obj = $this->db->fetch_object($result);
2192 $account->fetch($obj->rowid);
2194 foreach ($request_data as $field => $value) {
2195 if ($field ===
'caller') {
2197 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2204 if ($account->update(DolibarrApiAccess::$user) < 0) {
2205 throw new RestException(500,
'Error updating SocieteAccount account');
2234 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2235 throw new RestException(403);
2238 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2239 $result = $this->db->query($sql);
2241 if ($result && $this->db->num_rows($result) == 0) {
2242 throw new RestException(404);
2244 $obj = $this->db->fetch_object($result);
2246 $account->fetch($obj->rowid);
2248 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2249 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2271 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2272 throw new RestException(403);
2279 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2280 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2282 $result = $this->db->query($sql);
2284 if ($result && $this->db->num_rows($result) == 0) {
2285 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2289 $num = $this->db->num_rows($result);
2291 $obj = $this->db->fetch_object($result);
2293 $account->fetch($obj->rowid);
2295 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2296 throw new RestException(500,
'Error while deleting account attached to this third party');
2319 unset(
$object->departement_code);
2328 unset(
$object->commercial_id);
2332 unset(
$object->total_localtax1);
2333 unset(
$object->total_localtax2);
2339 unset(
$object->fk_delivery_address);
2354 if ($data ===
null) {
2357 $thirdparty = array();
2358 foreach (Thirdparties::$FIELDS as $field) {
2359 if (!isset($data[$field])) {
2360 throw new RestException(400,
"$field field missing");
2362 $thirdparty[$field] = $data[$field];
2390 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2392 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2393 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2397 $result = $this->company->initAsSpecimen();
2399 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2402 throw new RestException(404,
'Thirdparty not found');
2406 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2408 if (isModEnabled(
'mailing')) {
2409 $this->company->getNoEmail();
2413 $filterabsolutediscount =
"fk_facture_source IS NULL";
2414 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2416 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2417 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2420 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2421 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2422 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2423 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage invoices.
Class to manage the table of subscription to notifications.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
updateCompanyNotification($id, $notification_id, $request_data=null)
Update a company notification for a third party.
setThirdpartyPriceLevel($id, $priceLevel)
Set a new price level for the given third party.
_cleanObjectDatas($object)
Clean sensible object datas.
getSocieteByAccounts($site, $key_account)
Get a specific third party by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a third party.
deleteCompanyNotification($id, $notification_id)
Delete a company notification attached to a third party.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a third party.
getOutStandingOrder($id, $mode='customer')
Get outstanding orders for a third party.
addRepresentative($id, $representative_id)
Add a customer representative to a third party.
getByBarcode($barcode)
Get a third party by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a document from a bank account record.
createCompanyNotificationByCode($id, $code, $request_data=null)
Create a company notification for a third party using action trigger code.
getCompanyNotification($id)
Get company notifications for a third party.
addCategory($id, $category_id)
Add a customer category to a third party.
getCompanyBankAccount($id)
Get company bank accounts of a third party.
getInvoicesQualifiedForReplacement($id)
Return invoices qualified to be replaced by another invoice.
post($request_data=null)
Create a third party.
put($id, $request_data=null)
Update third party.
getByEmail($email)
Get properties of a third party by email.
_validate($data)
Validate fields before create or update object.
addSupplierCategory($id, $category_id)
Add a supplier category to a third party.
merge($id, $idtodelete)
Merge a third party into another third party.
deleteSocieteAccounts($id)
Delete all accounts attached to a third party.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a third party.
postSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account for a third party.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the third party.
deleteRepresentative($id, $representative_id)
Remove the link between a customer representative and a third party.
createCompanyNotification($id, $request_data=null)
Create a company notification for a third party.
putSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a third party.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update a company bank account of a third party.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a third party.
getInvoicesQualifiedForCreditNote($id)
Return invoices qualified to be corrected by a credit note.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a third party.
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals for a third party.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of a third party.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices for a third party.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List third parties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a third party.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing third party.
createCompanyBankAccount($id, $request_data=null)
Create a company bank account for a third party.
deleteCategory($id, $category_id)
Remove the link between a customer category and the third party.
Class to manage translations.
Class to manage Dolibarr users.
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_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.