27use Luracast\Restler\RestException;
42 public static $FIELDS = array(
59 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
62 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
63 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
65 $this->company =
new Societe($this->db);
68 static::$FIELDS[] =
'email';
84 public function get(
$id)
107 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
126 return $this->
_fetch(
null,
'',
'', $barcode);
151 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
155 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
156 throw new RestException(403);
160 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
164 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
165 $search_sale = DolibarrApiAccess::$user->id;
168 $sql =
"SELECT t.rowid";
169 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
170 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
173 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
175 if (!in_array($mode, array(1, 2, 3))) {
176 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
179 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
180 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
181 $sql .=
" AND t.fk_stcomm = st.id";
183 $sql .=
" AND t.client IN (1, 3)";
184 } elseif ($mode == 2) {
185 $sql .=
" AND t.client IN (2, 3)";
186 } elseif ($mode == 3) {
187 $sql .=
" AND t.client IN (0)";
188 } elseif ($mode == 4) {
189 $sql .=
" AND t.fournisseur IN (1)";
193 if (!empty($mode) && $mode != 4) {
194 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
195 } elseif (!empty($mode) && $mode == 4) {
196 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
198 $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))";
202 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
205 if ($search_sale && $search_sale !=
'-1') {
206 if ($search_sale == -2) {
207 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
208 } elseif ($search_sale > 0) {
209 $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).
")";
217 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
222 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
224 $sql .= $this->db->order($sortfield, $sortorder);
229 $offset = $limit * $page;
231 $sql .= $this->db->plimit($limit + 1, $offset);
234 $result = $this->db->query($sql);
236 $num = $this->db->num_rows($result);
237 $min = min($num, ($limit <= 0 ? $num : $limit));
240 $obj = $this->db->fetch_object($result);
241 $soc_static =
new Societe($this->db);
242 if ($soc_static->fetch($obj->rowid)) {
243 if (isModEnabled(
'mailing')) {
244 $soc_static->getNoEmail();
251 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
253 if (!count($obj_ret)) {
257 $message =
'No third parties found';
260 $message =
'No customers found';
263 $message =
'No prospects found';
266 $message =
'No other third parties found';
269 $message =
'No suppliers found';
271 throw new RestException(404, $message);
275 if ($pagination_data) {
276 $totalsResult = $this->db->query($sqlTotals);
277 $total = $this->db->fetch_object($totalsResult)->total;
282 $obj_ret[
'data'] = $tmp;
283 $obj_ret[
'pagination'] = [
284 'total' => (int) $total,
286 'page_count' => ceil((
int) $total / $limit),
306 public function post($request_data =
null)
308 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
309 throw new RestException(403);
312 $result = $this->
_validate($request_data);
314 foreach ($request_data as $field => $value) {
315 if ($field ===
'caller') {
317 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
321 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
324 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
325 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
327 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
328 $this->company->setNoEmail($this->company->no_email);
331 return $this->company->id;
351 public function put(
$id, $request_data =
null)
353 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
354 throw new RestException(403);
357 $result = $this->company->fetch(
$id);
359 throw new RestException(404,
'Thirdparty not found');
363 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
366 foreach ($request_data as $field => $value) {
367 if ($field ==
'id') {
370 if ($field ===
'caller') {
372 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
375 if ($field ==
'array_options' && is_array($value)) {
376 foreach ($value as $index => $val) {
377 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
381 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
384 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
385 $this->company->setNoEmail($this->company->no_email);
388 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
389 return $this->
get(
$id);
391 throw new RestException(500, $this->company->error);
418 if (
$id == $idtodelete) {
419 throw new RestException(400,
'Try to merge a thirdparty into itself');
422 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
423 throw new RestException(403);
426 $result = $this->company->fetch(
$id);
428 throw new RestException(404,
'Thirdparty not found');
432 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 $companytoremove =
new Societe($this->db);
436 $result = $companytoremove->fetch($idtodelete);
438 throw new RestException(404,
'Thirdparty not found');
442 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
445 $user = DolibarrApiAccess::$user;
446 $result = $this->company->mergeCompany($companytoremove->id);
448 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
451 return $this->
get(
$id);
466 public function delete(
$id)
468 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
469 throw new RestException(403);
471 $result = $this->company->fetch(
$id);
473 throw new RestException(404,
'Thirdparty not found');
476 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
478 $this->company->oldcopy = clone $this->company;
480 $res = $this->company->delete(
$id);
482 throw new RestException(500,
"Can't delete, error occurs");
483 } elseif ($res == 0) {
484 throw new RestException(409,
"Can't delete, that product is probably used");
490 'message' =>
'Object deleted'
516 if (!isModEnabled(
'societe')) {
517 throw new RestException(501,
'Module "Thirdparties" needed for this request');
520 if (!isModEnabled(
"product")) {
521 throw new RestException(501,
'Module "Products" needed for this request');
525 throw new RestException(501,
'Multiprices features activation needed for this request');
528 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
529 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
532 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
533 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
536 $result = $this->company->fetch(
$id);
538 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
541 if (empty($result)) {
542 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
546 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
549 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
551 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
573 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
574 throw new RestException(403);
576 $result = $this->company->fetch(
$id);
578 throw new RestException(404,
'Thirdparty not found');
580 $usertmp =
new User($this->db);
581 $result = $usertmp->fetch($representative_id);
583 throw new RestException(404,
'User not found');
586 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
588 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
609 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
610 throw new RestException(403);
612 $result = $this->company->fetch(
$id);
614 throw new RestException(404,
'Thirdparty not found');
616 $usertmp =
new User($this->db);
617 $result = $usertmp->fetch($representative_id);
619 throw new RestException(404,
'User not found');
622 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
624 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
647 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
649 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
650 throw new RestException(403);
653 $result = $this->company->fetch(
$id);
655 throw new RestException(404,
'Thirdparty not found');
660 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
662 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
663 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
666 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
670 return $arrayofcateg;
691 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
692 throw new RestException(403);
695 $result = $this->company->fetch(
$id);
697 throw new RestException(404,
'Thirdparty not found');
700 $result = $category->fetch($category_id);
702 throw new RestException(404,
'category not found');
706 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
709 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
712 $category->add_type($this->company,
'customer');
735 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
736 throw new RestException(403);
739 $result = $this->company->fetch(
$id);
741 throw new RestException(404,
'Thirdparty not found');
744 $result = $category->fetch($category_id);
746 throw new RestException(404,
'category not found');
750 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
753 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
756 $category->del_type($this->company,
'customer');
782 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
783 throw new RestException(403);
786 $result = $this->company->fetch(
$id);
788 throw new RestException(404,
'Thirdparty not found');
793 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
795 if (is_numeric($result) && $result < 0) {
796 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
799 if (is_numeric($result) && $result == 0) {
824 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
825 throw new RestException(403);
828 $result = $this->company->fetch(
$id);
830 throw new RestException(404,
'Thirdparty not found');
833 $result = $category->fetch($category_id);
835 throw new RestException(404,
'category not found');
839 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
842 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 $category->add_type($this->company,
'supplier');
868 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
869 throw new RestException(403);
872 $result = $this->company->fetch(
$id);
874 throw new RestException(404,
'Thirdparty not found');
877 $result = $category->fetch($category_id);
879 throw new RestException(404,
'category not found');
883 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
886 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
889 $category->del_type($this->company,
'supplier');
915 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
916 throw new RestException(403);
920 throw new RestException(400,
'Thirdparty ID is mandatory');
924 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
927 $result = $this->company->fetch(
$id);
929 throw new RestException(404,
'Thirdparty not found');
932 $result = $this->company->getOutstandingProposals($mode);
934 unset($result[
'total_ht']);
935 unset($result[
'total_ttc']);
961 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
962 throw new RestException(403);
966 throw new RestException(400,
'Thirdparty ID is mandatory');
970 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
973 $result = $this->company->fetch(
$id);
975 throw new RestException(404,
'Thirdparty not found');
978 $result = $this->company->getOutstandingOrders($mode);
980 unset($result[
'total_ht']);
981 unset($result[
'total_ttc']);
1006 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1007 throw new RestException(403);
1011 throw new RestException(400,
'Thirdparty ID is mandatory');
1015 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1018 $result = $this->company->fetch(
$id);
1020 throw new RestException(404,
'Thirdparty not found');
1023 $result = $this->company->getOutstandingBills($mode);
1025 unset($result[
'total_ht']);
1026 unset($result[
'total_ttc']);
1051 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1052 throw new RestException(403);
1056 throw new RestException(400,
'Thirdparty ID is mandatory');
1060 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1063 $result = $this->company->fetch(
$id);
1064 if (!is_array($result)) {
1065 throw new RestException(404,
'Thirdparty not found');
1068 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1100 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1101 throw new RestException(403);
1105 throw new RestException(400,
'Thirdparty ID is mandatory');
1109 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1112 $result = $this->company->fetch(
$id);
1114 throw new RestException(404,
'Thirdparty not found');
1118 $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";
1119 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1120 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1121 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1122 if ($filter ==
"available") {
1123 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1125 if ($filter ==
"used") {
1126 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1129 $sql .= $this->db->order($sortfield, $sortorder);
1131 $result = $this->db->query($sql);
1133 throw new RestException(503, $this->db->lasterror());
1135 $num = $this->db->num_rows($result);
1136 while ($obj = $this->db->fetch_object($result)) {
1165 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1166 throw new RestException(403);
1169 throw new RestException(400,
'Thirdparty ID is mandatory');
1173 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1181 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1182 $invoice =
new Facture($this->db);
1183 $result = $invoice->list_replacable_invoices(
$id);
1185 throw new RestException(405, $invoice->error);
1214 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1215 throw new RestException(403);
1218 throw new RestException(400,
'Thirdparty ID is mandatory');
1222 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1230 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1231 $invoice =
new Facture($this->db);
1232 $result = $invoice->list_qualified_avoir_invoices(
$id);
1233 if (!is_array($result) && $result < 0) {
1234 throw new RestException(405, $invoice->error);
1258 throw new RestException(400,
'Thirdparty ID is mandatory');
1260 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1261 throw new RestException(403);
1264 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1271 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1272 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1274 $sql .=
" WHERE fk_soc = ".((int)
$id);
1277 $result = $this->db->query($sql);
1278 if ($this->db->num_rows($result) == 0) {
1279 throw new RestException(404,
'Notification not found');
1284 $notifications = array();
1287 $num = $this->db->num_rows($result);
1289 $obj = $this->db->fetch_object($result);
1290 $notifications[] = $obj;
1294 throw new RestException(404,
'No notifications found');
1297 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1299 $returnNotifications = array();
1301 foreach ($notifications as $notification) {
1303 foreach ($notification as $key => $value) {
1304 if (in_array($key, $fields)) {
1308 $returnNotifications[] =
$object;
1312 return $returnNotifications;
1333 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1334 throw new RestException(403,
"User has no right to update thirdparties");
1336 if ($this->company->fetch(
$id) <= 0) {
1337 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1339 $notification =
new Notify($this->db);
1341 $notification->socid =
$id;
1343 foreach ($request_data as $field => $value) {
1344 $notification->$field = $value;
1347 $event = $notification->event;
1349 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1351 $socid = $notification->socid;
1352 $contact_id = $notification->contact_id;
1354 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1355 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1356 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1357 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1358 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1360 $exists_result = $this->db->query($exists_sql);
1361 if ($this->db->num_rows($exists_result) > 0) {
1362 throw new RestException(403,
'Notification already exists');
1365 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1366 throw new RestException(500,
'Error creating Thirdparty Notification');
1369 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1370 throw new RestException(500,
'Error updating values');
1396 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1397 throw new RestException(403,
"User has no right to update thirdparties");
1399 if ($this->company->fetch(
$id) <= 0) {
1400 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1402 $notification =
new Notify($this->db);
1403 $notification->socid =
$id;
1405 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1406 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1408 $result = $this->db->query($sql);
1409 if ($this->db->num_rows($result) == 0) {
1410 throw new RestException(404,
'Action Trigger code not found');
1413 $notification->event = $this->db->fetch_row($result)[0];
1414 foreach ($request_data as $field => $value) {
1415 if ($field ===
'event') {
1416 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1418 if ($field ===
'fk_action') {
1419 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1421 $notification->$field = $value;
1424 $event = $notification->event;
1425 $socid = $notification->socid;
1426 $contact_id = $notification->contact_id;
1428 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1429 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1430 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1431 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1432 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1434 $exists_result = $this->db->query($exists_sql);
1435 if ($this->db->num_rows($exists_result) > 0) {
1436 throw new RestException(403,
'Notification already exists');
1439 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1440 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1443 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1444 throw new RestException(500,
'Error updating values');
1466 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1467 throw new RestException(403);
1470 $notification =
new Notify($this->db);
1472 $notification->fetch($notification_id);
1474 $socid = (int) $notification->socid;
1476 if ($socid ==
$id) {
1477 return $notification->delete(DolibarrApiAccess::$user);
1479 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1502 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1503 throw new RestException(403,
"User has no right to update thirdparties");
1505 if ($this->company->fetch(
$id) <= 0) {
1506 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1508 $notification =
new Notify($this->db);
1511 $notification->fetch($notification_id,
$id);
1513 if ($notification->socid !=
$id) {
1514 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1517 foreach ($request_data as $field => $value) {
1518 $notification->$field = $value;
1521 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1522 throw new RestException(500,
'Error updating values');
1545 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1546 throw new RestException(403);
1549 throw new RestException(400,
'Thirdparty ID is mandatory');
1553 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1560 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1561 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1562 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1564 $sql .=
" WHERE fk_soc = ".((int)
$id);
1567 $result = $this->db->query($sql);
1569 if ($this->db->num_rows($result) == 0) {
1570 throw new RestException(404,
'Account not found');
1575 $accounts = array();
1578 $num = $this->db->num_rows($result);
1580 $obj = $this->db->fetch_object($result);
1583 if ($account->fetch($obj->rowid)) {
1584 $accounts[] = $account;
1589 throw new RestException(404,
'Account not found');
1593 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1595 $returnAccounts = array();
1597 foreach ($accounts as $account) {
1599 foreach ($account as $key => $value) {
1600 if (in_array($key, $fields)) {
1601 if ($key ==
'iban') {
1611 return $returnAccounts;
1632 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1633 throw new RestException(403);
1635 if ($this->company->fetch(
$id) <= 0) {
1636 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1640 $account->socid =
$id;
1642 foreach ($request_data as $field => $value) {
1643 if ($field ===
'caller') {
1645 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1649 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1652 if ($account->create(DolibarrApiAccess::$user) < 0) {
1653 throw new RestException(500,
'Error creating Company Bank account');
1656 if (empty($account->rum)) {
1657 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1659 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1660 $account->date_rum =
dol_now();
1663 if ($account->update(DolibarrApiAccess::$user) < 0) {
1664 throw new RestException(500,
'Error updating values');
1689 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1690 throw new RestException(403);
1692 if ($this->company->fetch(
$id) <= 0) {
1693 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1698 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
1700 if ($account->socid !=
$id) {
1701 throw new RestException(403);
1705 foreach ($request_data as $field => $value) {
1706 if ($field ===
'caller') {
1708 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1715 if (empty($account->rum)) {
1716 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1718 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1719 $account->date_rum =
dol_now();
1722 if ($account->update(DolibarrApiAccess::$user) < 0) {
1723 throw new RestException(500,
'Error updating values');
1745 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1746 throw new RestException(403);
1751 $account->fetch($bankaccount_id);
1753 $socid = (int) $account->socid;
1755 if ($socid ==
$id) {
1756 return $account->delete(DolibarrApiAccess::$user);
1758 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1782 global
$conf, $langs;
1784 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1786 if ($this->company->fetch(
$id) <= 0) {
1787 throw new RestException(404,
'Thirdparty not found');
1790 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1791 throw new RestException(403);
1794 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1796 $this->company->fk_bank = $this->company->fk_account;
1799 $outputlangs = $langs;
1804 if (isset($this->company->thirdparty->default_lang)) {
1805 $newlang = $this->company->thirdparty->default_lang;
1806 } elseif (isset($this->company->default_lang)) {
1807 $newlang = $this->company->default_lang;
1810 if (!empty($newlang)) {
1812 $outputlangs->setDefaultLang($newlang);
1815 $sql =
"SELECT rowid";
1816 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1818 $sql .=
" WHERE fk_soc = ".((int)
$id);
1820 if ($companybankid) {
1821 $sql .=
" AND rowid = ".((int) $companybankid);
1825 $accounts = array();
1827 $result = $this->db->query($sql);
1829 if ($this->db->num_rows($result) == 0) {
1830 throw new RestException(404,
'Bank account not found');
1833 $num = $this->db->num_rows($result);
1835 $obj = $this->db->fetch_object($result);
1838 if ($account->fetch($obj->rowid)) {
1839 $accounts[] = $account;
1844 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1847 $moreparams = array(
1848 'use_companybankid' => $accounts[0]->
id,
1849 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
1852 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1855 return array(
"success" => $result);
1857 throw new RestException(500,
'Error generating the document '.$this->company->error);
1880 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1881 throw new RestException(403);
1885 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1891 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1892 $sql .=
" WHERE fk_soc = ".((int)
$id);
1894 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1897 $result = $this->db->query($sql);
1899 if ($result && $this->db->num_rows($result) == 0) {
1900 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
1905 $accounts = array();
1907 $num = $this->db->num_rows($result);
1909 $obj = $this->db->fetch_object($result);
1912 if ($account->fetch($obj->rowid)) {
1913 $accounts[] = $account;
1918 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1920 $returnAccounts = array();
1922 foreach ($accounts as $account) {
1924 foreach ($account as $key => $value) {
1925 if (in_array($key, $fields)) {
1932 return $returnAccounts;
1954 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1955 throw new RestException(403);
1958 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1959 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
1960 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
1962 $result = $this->db->query($sql);
1964 if ($result && $this->db->num_rows($result) == 1) {
1965 $obj = $this->db->fetch_object($result);
1966 $returnThirdparty = $this->
_fetch($obj->fk_soc);
1968 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
1972 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1975 return $returnThirdparty;
2003 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2004 throw new RestException(403);
2007 if (!isset($request_data[
'site'])) {
2008 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2011 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2012 $result = $this->db->query($sql);
2014 if ($result && $this->db->num_rows($result) == 0) {
2016 if (!isset($request_data[
'login'])) {
2017 $account->login =
"";
2019 $account->fk_soc =
$id;
2021 foreach ($request_data as $field => $value) {
2022 if ($field ===
'caller') {
2024 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2031 if ($account->create(DolibarrApiAccess::$user) < 0) {
2032 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2039 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2071 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2072 throw new RestException(403);
2075 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2076 $result = $this->db->query($sql);
2079 if ($result && $this->db->num_rows($result) == 0) {
2080 if (!isset($request_data[
'key_account'])) {
2081 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2084 if (!isset($request_data[
'login'])) {
2085 $account->login =
"";
2088 foreach ($request_data as $field => $value) {
2089 if ($field ===
'caller') {
2091 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2098 $account->fk_soc =
$id;
2099 $account->site = $site;
2101 if ($account->create(DolibarrApiAccess::$user) < 0) {
2102 throw new RestException(500,
'Error creating SocieteAccount entity.');
2106 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2107 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2108 $result = $this->db->query($sql);
2110 if ($result && $this->db->num_rows($result) !== 0) {
2111 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.");
2115 $obj = $this->db->fetch_object($result);
2118 $account->id = $obj->rowid;
2119 $account->fk_soc =
$id;
2120 $account->site = $site;
2121 if (!isset($request_data[
'login'])) {
2122 $account->login =
"";
2124 $account->fk_user_creat = $obj->fk_user_creat;
2125 $account->date_creation = $obj->date_creation;
2127 foreach ($request_data as $field => $value) {
2128 if ($field ===
'caller') {
2130 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2137 if ($account->update(DolibarrApiAccess::$user) < 0) {
2138 throw new RestException(500,
'Error updating SocieteAccount entity.');
2169 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2170 throw new RestException(403);
2173 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2174 $result = $this->db->query($sql);
2176 if ($result && $this->db->num_rows($result) == 0) {
2177 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2180 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2181 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2182 $result = $this->db->query($sql);
2184 if ($result && $this->db->num_rows($result) !== 0) {
2185 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.");
2189 $obj = $this->db->fetch_object($result);
2191 $account->fetch($obj->rowid);
2193 foreach ($request_data as $field => $value) {
2194 if ($field ===
'caller') {
2196 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2203 if ($account->update(DolibarrApiAccess::$user) < 0) {
2204 throw new RestException(500,
'Error updating SocieteAccount account');
2233 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2234 throw new RestException(403);
2237 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2238 $result = $this->db->query($sql);
2240 if ($result && $this->db->num_rows($result) == 0) {
2241 throw new RestException(404);
2243 $obj = $this->db->fetch_object($result);
2245 $account->fetch($obj->rowid);
2247 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2248 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2270 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2271 throw new RestException(403);
2278 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2279 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2281 $result = $this->db->query($sql);
2283 if ($result && $this->db->num_rows($result) == 0) {
2284 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2288 $num = $this->db->num_rows($result);
2290 $obj = $this->db->fetch_object($result);
2292 $account->fetch($obj->rowid);
2294 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2295 throw new RestException(500,
'Error while deleting account attached to this third party');
2318 unset(
$object->departement_code);
2327 unset(
$object->commercial_id);
2331 unset(
$object->total_localtax1);
2332 unset(
$object->total_localtax2);
2338 unset(
$object->fk_delivery_address);
2353 if ($data ===
null) {
2356 $thirdparty = array();
2357 foreach (Thirdparties::$FIELDS as $field) {
2358 if (!isset($data[$field])) {
2359 throw new RestException(400,
"$field field missing");
2361 $thirdparty[$field] = $data[$field];
2389 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2391 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2392 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2396 $result = $this->company->initAsSpecimen();
2398 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2401 throw new RestException(404,
'Thirdparty not found');
2405 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2407 if (isModEnabled(
'mailing')) {
2408 $this->company->getNoEmail();
2412 $filterabsolutediscount =
"fk_facture_source IS NULL";
2413 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2415 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2416 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2419 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2420 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2421 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2422 $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.