24use Luracast\Restler\RestException;
39 public static $FIELDS = array(
56 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
57 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
58 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
59 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
62 $this->company =
new Societe($this->db);
65 static::$FIELDS[] =
'email';
79 public function get(
$id)
98 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
115 return $this->
_fetch(
null,
'',
'', $barcode);
137 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
141 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
142 throw new RestException(403);
146 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
150 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
151 $search_sale = DolibarrApiAccess::$user->id;
154 $sql =
"SELECT t.rowid";
155 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
156 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
159 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
161 if (!in_array($mode, array(1, 2, 3))) {
162 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
165 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
166 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
167 $sql .=
" AND t.fk_stcomm = st.id";
169 $sql .=
" AND t.client IN (1, 3)";
170 } elseif ($mode == 2) {
171 $sql .=
" AND t.client IN (2, 3)";
172 } elseif ($mode == 3) {
173 $sql .=
" AND t.client IN (0)";
174 } elseif ($mode == 4) {
175 $sql .=
" AND t.fournisseur IN (1)";
179 if (!empty($mode) && $mode != 4) {
180 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
181 } elseif (!empty($mode) && $mode == 4) {
182 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
184 $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))";
188 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
191 if ($search_sale && $search_sale !=
'-1') {
192 if ($search_sale == -2) {
193 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
194 } elseif ($search_sale > 0) {
195 $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).
")";
203 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
208 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
210 $sql .= $this->db->order($sortfield, $sortorder);
215 $offset = $limit * $page;
217 $sql .= $this->db->plimit($limit + 1, $offset);
220 $result = $this->db->query($sql);
222 $num = $this->db->num_rows($result);
223 $min = min($num, ($limit <= 0 ? $num : $limit));
226 $obj = $this->db->fetch_object($result);
227 $soc_static =
new Societe($this->db);
228 if ($soc_static->fetch($obj->rowid)) {
229 if (isModEnabled(
'mailing')) {
230 $soc_static->getNoEmail();
237 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
239 if (!count($obj_ret)) {
240 throw new RestException(404,
'Thirdparties not found');
244 if ($pagination_data) {
245 $totalsResult = $this->db->query($sqlTotals);
246 $total = $this->db->fetch_object($totalsResult)->total;
251 $obj_ret[
'data'] = $tmp;
252 $obj_ret[
'pagination'] = [
253 'total' => (int) $total,
255 'page_count' => ceil((
int) $total / $limit),
269 public function post($request_data =
null)
271 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
272 throw new RestException(403);
275 $result = $this->
_validate($request_data);
277 foreach ($request_data as $field => $value) {
278 if ($field ===
'caller') {
280 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
284 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
287 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
288 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
290 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
291 $this->company->setNoEmail($this->company->no_email);
294 return $this->company->id;
308 public function put(
$id, $request_data =
null)
310 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
311 throw new RestException(403);
314 $result = $this->company->fetch(
$id);
316 throw new RestException(404,
'Thirdparty not found');
320 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
323 foreach ($request_data as $field => $value) {
324 if ($field ==
'id') {
327 if ($field ===
'caller') {
329 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
332 if ($field ==
'array_options' && is_array($value)) {
333 foreach ($value as $index => $val) {
334 $this->company->array_options[$index] = $val;
338 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
341 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
342 $this->company->setNoEmail($this->company->no_email);
345 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1) > 0) {
346 return $this->
get(
$id);
348 throw new RestException(500, $this->company->error);
368 if (
$id == $idtodelete) {
369 throw new RestException(400,
'Try to merge a thirdparty into itself');
372 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
373 throw new RestException(403);
376 $result = $this->company->fetch(
$id);
378 throw new RestException(404,
'Thirdparty not found');
382 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
385 $companytoremove =
new Societe($this->db);
386 $result = $companytoremove->fetch($idtodelete);
388 throw new RestException(404,
'Thirdparty not found');
392 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
395 $user = DolibarrApiAccess::$user;
396 $result = $this->company->mergeCompany($companytoremove->id);
398 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
401 return $this->
get(
$id);
410 public function delete(
$id)
412 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
413 throw new RestException(403);
415 $result = $this->company->fetch(
$id);
417 throw new RestException(404,
'Thirdparty not found');
420 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
422 $this->company->oldcopy = clone $this->company;
424 $res = $this->company->delete(
$id);
426 throw new RestException(500,
"Can't delete, error occurs");
427 } elseif ($res == 0) {
428 throw new RestException(409,
"Can't delete, that product is probably used");
434 'message' =>
'Object deleted'
458 if (!isModEnabled(
'societe')) {
459 throw new RestException(501,
'Module "Thirdparties" needed for this request');
462 if (!isModEnabled(
"product")) {
463 throw new RestException(501,
'Module "Products" needed for this request');
467 throw new RestException(501,
'Multiprices features activation needed for this request');
470 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
471 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
474 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
475 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
478 $result = $this->company->fetch(
$id);
480 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
483 if (empty($result)) {
484 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
488 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
491 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
493 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
513 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
514 throw new RestException(403);
516 $result = $this->company->fetch(
$id);
518 throw new RestException(404,
'Thirdparty not found');
520 $usertmp =
new User($this->db);
521 $result = $usertmp->fetch($representative_id);
523 throw new RestException(404,
'User not found');
526 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
528 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
547 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
548 throw new RestException(403);
550 $result = $this->company->fetch(
$id);
552 throw new RestException(404,
'Thirdparty not found');
554 $usertmp =
new User($this->db);
555 $result = $usertmp->fetch($representative_id);
557 throw new RestException(404,
'User not found');
560 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
562 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
579 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
581 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
582 throw new RestException(403);
585 $result = $this->company->fetch(
$id);
587 throw new RestException(404,
'Thirdparty not found');
592 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
594 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
595 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
598 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
602 return $arrayofcateg;
616 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
617 throw new RestException(403);
620 $result = $this->company->fetch(
$id);
622 throw new RestException(404,
'Thirdparty not found');
625 $result = $category->fetch($category_id);
627 throw new RestException(404,
'category not found');
631 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
634 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
637 $category->add_type($this->company,
'customer');
654 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
655 throw new RestException(403);
658 $result = $this->company->fetch(
$id);
660 throw new RestException(404,
'Thirdparty not found');
663 $result = $category->fetch($category_id);
665 throw new RestException(404,
'category not found');
669 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
672 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
675 $category->del_type($this->company,
'customer');
695 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
696 throw new RestException(403);
699 $result = $this->company->fetch(
$id);
701 throw new RestException(404,
'Thirdparty not found');
706 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
708 if (is_numeric($result) && $result < 0) {
709 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
712 if (is_numeric($result) && $result == 0) {
731 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
732 throw new RestException(403);
735 $result = $this->company->fetch(
$id);
737 throw new RestException(404,
'Thirdparty not found');
740 $result = $category->fetch($category_id);
742 throw new RestException(404,
'category not found');
746 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
749 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
752 $category->add_type($this->company,
'supplier');
769 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
770 throw new RestException(403);
773 $result = $this->company->fetch(
$id);
775 throw new RestException(404,
'Thirdparty not found');
778 $result = $category->fetch($category_id);
780 throw new RestException(404,
'category not found');
784 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
787 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 $category->del_type($this->company,
'supplier');
812 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
813 throw new RestException(403);
817 throw new RestException(400,
'Thirdparty ID is mandatory');
821 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
824 $result = $this->company->fetch(
$id);
826 throw new RestException(404,
'Thirdparty not found');
829 $result = $this->company->getOutstandingProposals($mode);
831 unset($result[
'total_ht']);
832 unset($result[
'total_ttc']);
854 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
855 throw new RestException(403);
859 throw new RestException(400,
'Thirdparty ID is mandatory');
863 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
866 $result = $this->company->fetch(
$id);
868 throw new RestException(404,
'Thirdparty not found');
871 $result = $this->company->getOutstandingOrders($mode);
873 unset($result[
'total_ht']);
874 unset($result[
'total_ttc']);
895 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
896 throw new RestException(403);
900 throw new RestException(400,
'Thirdparty ID is mandatory');
904 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 $result = $this->company->fetch(
$id);
909 throw new RestException(404,
'Thirdparty not found');
912 $result = $this->company->getOutstandingBills($mode);
914 unset($result[
'total_ht']);
915 unset($result[
'total_ttc']);
936 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
937 throw new RestException(403);
941 throw new RestException(400,
'Thirdparty ID is mandatory');
945 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
948 $result = $this->company->fetch(
$id);
950 throw new RestException(404,
'Thirdparty not found');
953 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
979 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
980 throw new RestException(403);
984 throw new RestException(400,
'Thirdparty ID is mandatory');
988 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
991 $result = $this->company->fetch(
$id);
993 throw new RestException(404,
'Thirdparty not found');
997 $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";
998 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
999 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1000 if ($filter ==
"available") {
1001 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1003 if ($filter ==
"used") {
1004 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1007 $sql .= $this->db->order($sortfield, $sortorder);
1009 $result = $this->db->query($sql);
1011 throw new RestException(503, $this->db->lasterror());
1013 $num = $this->db->num_rows($result);
1014 while ($obj = $this->db->fetch_object($result)) {
1039 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1040 throw new RestException(403);
1043 throw new RestException(400,
'Thirdparty ID is mandatory');
1047 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1055 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1056 $invoice =
new Facture($this->db);
1057 $result = $invoice->list_replacable_invoices(
$id);
1059 throw new RestException(405, $invoice->error);
1083 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1084 throw new RestException(403);
1087 throw new RestException(400,
'Thirdparty ID is mandatory');
1091 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1099 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1100 $invoice =
new Facture($this->db);
1101 $result = $invoice->list_qualified_avoir_invoices(
$id);
1103 throw new RestException(405, $invoice->error);
1121 throw new RestException(400,
'Thirdparty ID is mandatory');
1123 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1124 throw new RestException(403);
1127 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1134 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1135 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1137 $sql .=
" WHERE fk_soc = ".((int)
$id);
1140 $result = $this->db->query($sql);
1141 if ($this->db->num_rows($result) == 0) {
1142 throw new RestException(404,
'Notification not found');
1147 $notifications = array();
1150 $num = $this->db->num_rows($result);
1152 $obj = $this->db->fetch_object($result);
1153 $notifications[] = $obj;
1157 throw new RestException(404,
'No notifications found');
1160 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1162 $returnNotifications = array();
1164 foreach ($notifications as $notification) {
1166 foreach ($notification as $key => $value) {
1167 if (in_array($key, $fields)) {
1171 $returnNotifications[] =
$object;
1174 return $returnNotifications;
1188 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1189 throw new RestException(403,
"User has no right to update thirdparties");
1191 if ($this->company->fetch(
$id) <= 0) {
1192 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1194 $notification =
new Notify($this->db);
1196 $notification->socid =
$id;
1198 foreach ($request_data as $field => $value) {
1199 $notification->$field = $value;
1202 $event = $notification->event;
1204 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1206 $socid = $notification->socid;
1207 $contact_id = $notification->contact_id;
1209 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1210 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1211 $exists_sql .=
" WHERE fk_action = '".$this->db->escape($event).
"'";
1212 $exists_sql .=
" AND fk_soc = '".$this->db->escape($socid).
"'";
1213 $exists_sql .=
" AND fk_contact = '".$this->db->escape($contact_id).
"'";
1215 $exists_result = $this->db->query($exists_sql);
1216 if ($this->db->num_rows($exists_result) > 0) {
1217 throw new RestException(403,
'Notification already exists');
1220 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1221 throw new RestException(500,
'Error creating Thirdparty Notification');
1224 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1225 throw new RestException(500,
'Error updating values');
1244 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1245 throw new RestException(403,
"User has no right to update thirdparties");
1247 if ($this->company->fetch(
$id) <= 0) {
1248 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1250 $notification =
new Notify($this->db);
1251 $notification->socid =
$id;
1253 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1254 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1256 $result = $this->db->query($sql);
1257 if ($this->db->num_rows($result) == 0) {
1258 throw new RestException(404,
'Action Trigger code not found');
1261 $notification->event = $this->db->fetch_row($result)[0];
1262 foreach ($request_data as $field => $value) {
1263 if ($field ===
'event') {
1264 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1266 if ($field ===
'fk_action') {
1267 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1269 $notification->$field = $value;
1272 $event = $notification->event;
1273 $socid = $notification->socid;
1274 $contact_id = $notification->contact_id;
1276 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1277 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1278 $exists_sql .=
" WHERE fk_action = '".$this->db->escape($event).
"'";
1279 $exists_sql .=
" AND fk_soc = '".$this->db->escape($socid).
"'";
1280 $exists_sql .=
" AND fk_contact = '".$this->db->escape($contact_id).
"'";
1282 $exists_result = $this->db->query($exists_sql);
1283 if ($this->db->num_rows($exists_result) > 0) {
1284 throw new RestException(403,
'Notification already exists');
1287 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1288 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1291 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1292 throw new RestException(500,
'Error updating values');
1310 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1311 throw new RestException(403);
1314 $notification =
new Notify($this->db);
1316 $notification->fetch($notification_id);
1318 $socid = (int) $notification->socid;
1320 if ($socid ==
$id) {
1321 return $notification->delete(DolibarrApiAccess::$user);
1323 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1340 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1341 throw new RestException(403,
"User has no right to update thirdparties");
1343 if ($this->company->fetch(
$id) <= 0) {
1344 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1346 $notification =
new Notify($this->db);
1349 $notification->fetch($notification_id,
$id);
1351 if ($notification->socid !=
$id) {
1352 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1355 foreach ($request_data as $field => $value) {
1356 $notification->$field = $value;
1359 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1360 throw new RestException(500,
'Error updating values');
1377 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1378 throw new RestException(403);
1381 throw new RestException(400,
'Thirdparty ID is mandatory');
1385 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1392 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1393 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1394 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1396 $sql .=
" WHERE fk_soc = ".((int)
$id);
1399 $result = $this->db->query($sql);
1401 if ($this->db->num_rows($result) == 0) {
1402 throw new RestException(404,
'Account not found');
1407 $accounts = array();
1410 $num = $this->db->num_rows($result);
1412 $obj = $this->db->fetch_object($result);
1414 if ($account->fetch($obj->rowid)) {
1415 $accounts[] = $account;
1420 throw new RestException(404,
'Account not found');
1424 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1426 $returnAccounts = array();
1428 foreach ($accounts as $account) {
1430 foreach ($account as $key => $value) {
1431 if (in_array($key, $fields)) {
1432 if ($key ==
'iban') {
1442 return $returnAccounts;
1456 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1457 throw new RestException(403);
1459 if ($this->company->fetch(
$id) <= 0) {
1460 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1464 $account->socid =
$id;
1466 foreach ($request_data as $field => $value) {
1467 if ($field ===
'caller') {
1469 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1473 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1476 if ($account->create(DolibarrApiAccess::$user) < 0) {
1477 throw new RestException(500,
'Error creating Company Bank account');
1480 if (empty($account->rum)) {
1481 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1483 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1484 $account->date_rum =
dol_now();
1487 if ($account->update(DolibarrApiAccess::$user) < 0) {
1488 throw new RestException(500,
'Error updating values');
1507 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1508 throw new RestException(403);
1510 if ($this->company->fetch(
$id) <= 0) {
1511 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1516 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
1518 if ($account->socid !=
$id) {
1519 throw new RestException(403);
1523 foreach ($request_data as $field => $value) {
1524 if ($field ===
'caller') {
1526 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1533 if (empty($account->rum)) {
1534 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1536 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1537 $account->date_rum =
dol_now();
1540 if ($account->update(DolibarrApiAccess::$user) < 0) {
1541 throw new RestException(500,
'Error updating values');
1559 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1560 throw new RestException(403);
1565 $account->fetch($bankaccount_id);
1567 $socid = (int) $account->socid;
1569 if ($socid ==
$id) {
1570 return $account->delete(DolibarrApiAccess::$user);
1572 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1588 global $conf, $langs;
1590 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1592 if ($this->company->fetch(
$id) <= 0) {
1593 throw new RestException(404,
'Thirdparty not found');
1596 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1597 throw new RestException(403);
1600 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1602 $this->company->fk_bank = $this->company->fk_account;
1605 $outputlangs = $langs;
1610 if (isset($this->company->thirdparty->default_lang)) {
1611 $newlang = $this->company->thirdparty->default_lang;
1612 } elseif (isset($this->company->default_lang)) {
1613 $newlang = $this->company->default_lang;
1616 if (!empty($newlang)) {
1617 $outputlangs =
new Translate(
"", $conf);
1618 $outputlangs->setDefaultLang($newlang);
1621 $sql =
"SELECT rowid";
1622 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1624 $sql .=
" WHERE fk_soc = ".((int)
$id);
1626 if ($companybankid) {
1627 $sql .=
" AND rowid = ".((int) $companybankid);
1631 $accounts = array();
1633 $result = $this->db->query($sql);
1635 if ($this->db->num_rows($result) == 0) {
1636 throw new RestException(404,
'Bank account not found');
1639 $num = $this->db->num_rows($result);
1641 $obj = $this->db->fetch_object($result);
1644 if ($account->fetch($obj->rowid)) {
1645 $accounts[] = $account;
1650 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1653 $moreparams = array(
1654 'use_companybankid' => $accounts[0]->
id,
1655 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
1658 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1661 return array(
"success" => $result);
1663 throw new RestException(500,
'Error generating the document '.$this->company->error);
1681 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1682 throw new RestException(403);
1686 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1692 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1693 $sql .=
" WHERE fk_soc = ".((int)
$id);
1695 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1698 $result = $this->db->query($sql);
1700 if ($result && $this->db->num_rows($result) == 0) {
1701 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
1706 $accounts = array();
1708 $num = $this->db->num_rows($result);
1710 $obj = $this->db->fetch_object($result);
1713 if ($account->fetch($obj->rowid)) {
1714 $accounts[] = $account;
1719 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1721 $returnAccounts = array();
1723 foreach ($accounts as $account) {
1725 foreach ($account as $key => $value) {
1726 if (in_array($key, $fields)) {
1733 return $returnAccounts;
1750 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1751 throw new RestException(403);
1754 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1755 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
1756 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
1758 $result = $this->db->query($sql);
1760 if ($result && $this->db->num_rows($result) == 1) {
1761 $obj = $this->db->fetch_object($result);
1762 $returnThirdparty = $this->
_fetch($obj->fk_soc);
1764 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
1768 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1771 return $returnThirdparty;
1795 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1796 throw new RestException(403);
1799 if (!isset($request_data[
'site'])) {
1800 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1803 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1804 $result = $this->db->query($sql);
1806 if ($result && $this->db->num_rows($result) == 0) {
1808 if (!isset($request_data[
'login'])) {
1809 $account->login =
"";
1811 $account->fk_soc =
$id;
1813 foreach ($request_data as $field => $value) {
1814 if ($field ===
'caller') {
1816 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1823 if ($account->create(DolibarrApiAccess::$user) < 0) {
1824 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1831 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1859 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1860 throw new RestException(403);
1863 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1864 $result = $this->db->query($sql);
1867 if ($result && $this->db->num_rows == 0) {
1868 if (!isset($request_data[
'key_account'])) {
1869 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1872 if (!isset($request_data[
'login'])) {
1873 $account->login =
"";
1876 foreach ($request_data as $field => $value) {
1877 if ($field ===
'caller') {
1879 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1886 $account->fk_soc =
$id;
1887 $account->site = $site;
1889 if ($account->create(DolibarrApiAccess::$user) < 0) {
1890 throw new RestException(500,
'Error creating SocieteAccount entity.');
1894 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1895 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1896 $result = $this->db->query($sql);
1898 if ($result && $this->db->num_rows($result) !== 0) {
1899 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.");
1903 $obj = $this->db->fetch_object($result);
1906 $account->id = $obj->rowid;
1907 $account->fk_soc =
$id;
1908 $account->site = $site;
1909 if (!isset($request_data[
'login'])) {
1910 $account->login =
"";
1912 $account->fk_user_creat = $obj->fk_user_creat;
1913 $account->date_creation = $obj->date_creation;
1915 foreach ($request_data as $field => $value) {
1916 if ($field ===
'caller') {
1918 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1925 if ($account->update(DolibarrApiAccess::$user) < 0) {
1926 throw new RestException(500,
'Error updating SocieteAccount entity.');
1953 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1954 throw new RestException(403);
1957 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
1958 $result = $this->db->query($sql);
1960 if ($result && $this->db->num_rows($result) == 0) {
1961 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
1964 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1965 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1966 $result = $this->db->query($sql);
1968 if ($result && $this->db->num_rows($result) !== 0) {
1969 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.");
1973 $obj = $this->db->fetch_object($result);
1975 $account->fetch($obj->rowid);
1977 foreach ($request_data as $field => $value) {
1978 if ($field ===
'caller') {
1980 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1987 if ($account->update(DolibarrApiAccess::$user) < 0) {
1988 throw new RestException(500,
'Error updating SocieteAccount account');
2012 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2013 throw new RestException(403);
2016 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2017 $result = $this->db->query($sql);
2019 if ($result && $this->db->num_rows($result) == 0) {
2020 throw new RestException(404);
2022 $obj = $this->db->fetch_object($result);
2024 $account->fetch($obj->rowid);
2026 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2027 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2046 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2047 throw new RestException(403);
2054 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2055 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2057 $result = $this->db->query($sql);
2059 if ($result && $this->db->num_rows($result) == 0) {
2060 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2064 $num = $this->db->num_rows($result);
2066 $obj = $this->db->fetch_object($result);
2068 $account->fetch($obj->rowid);
2070 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2071 throw new RestException(500,
'Error while deleting account attached to this third party');
2094 unset(
$object->departement_code);
2103 unset(
$object->commercial_id);
2107 unset(
$object->total_localtax1);
2108 unset(
$object->total_localtax2);
2114 unset(
$object->fk_delivery_address);
2129 $thirdparty = array();
2130 foreach (Thirdparties::$FIELDS as $field) {
2131 if (!isset($data[$field])) {
2132 throw new RestException(400,
"$field field missing");
2134 $thirdparty[$field] = $data[$field];
2160 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2162 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2163 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2167 $result = $this->company->initAsSpecimen();
2169 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2172 throw new RestException(404,
'Thirdparty not found');
2176 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2178 if (isModEnabled(
'mailing')) {
2179 $this->company->getNoEmail();
2183 $filterabsolutediscount =
"fk_facture_source IS NULL";
2184 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2186 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2187 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2190 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
2191 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
2192 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2193 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
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 CompanyNotification object for thirdparty.
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
_cleanObjectDatas($object)
Clean sensible object datas.
getSocieteByAccounts($site, $key_account)
Get a specific thirdparty by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
deleteCompanyNotification($id, $notification_id)
Delete a CompanyNotification attached to a thirdparty.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a thirdparty (by specifying the site key)
getOutStandingOrder($id, $mode='customer')
Get outstanding orders of thirdparty.
addRepresentative($id, $representative_id)
Add a customer representative to a thirdparty.
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
createCompanyNotificationByCode($id, $code, $request_data=null)
Create CompanyNotification object for thirdparty using action trigger code.
getCompanyNotification($id)
Get CompanyNotification objects for thirdparty.
addCategory($id, $category_id)
Add a customer category to a thirdparty.
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
post($request_data=null)
Create thirdparty object.
put($id, $request_data=null)
Update thirdparty.
getByEmail($email)
Get properties of a thirdparty object by email.
_validate($data)
Validate fields before create or update object.
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
merge($id, $idtodelete)
Merge a third party into another one.
deleteSocieteAccounts($id)
Delete all accounts attached to a thirdparty.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
deleteRepresentative($id, $representative_id)
Delete a customer representative to a thirdparty.
createCompanyNotification($id, $request_data=null)
Create CompanyNotification object for thirdparty.
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account to a thirdparty.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a thirdparty (by account id)
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a thirdparty.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers....
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals of thirdparty.
_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 thirdparty.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices of thirdparty.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List thirdparties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing thirdparty.
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
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)
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.
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.