25use Luracast\Restler\RestException;
40 public static $FIELDS = array(
57 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
58 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
59 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
63 $this->company =
new Societe($this->db);
66 static::$FIELDS[] =
'email';
80 public function get(
$id)
99 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
116 return $this->
_fetch(
null,
'',
'', $barcode);
138 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
142 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
143 throw new RestException(403);
147 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
151 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
152 $search_sale = DolibarrApiAccess::$user->id;
155 $sql =
"SELECT t.rowid";
156 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
157 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
160 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
162 if (!in_array($mode, array(1, 2, 3))) {
163 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
166 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
167 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
168 $sql .=
" AND t.fk_stcomm = st.id";
170 $sql .=
" AND t.client IN (1, 3)";
171 } elseif ($mode == 2) {
172 $sql .=
" AND t.client IN (2, 3)";
173 } elseif ($mode == 3) {
174 $sql .=
" AND t.client IN (0)";
175 } elseif ($mode == 4) {
176 $sql .=
" AND t.fournisseur IN (1)";
180 if (!empty($mode) && $mode != 4) {
181 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
182 } elseif (!empty($mode) && $mode == 4) {
183 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
185 $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))";
189 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
192 if ($search_sale && $search_sale !=
'-1') {
193 if ($search_sale == -2) {
194 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
195 } elseif ($search_sale > 0) {
196 $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).
")";
204 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
209 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
211 $sql .= $this->db->order($sortfield, $sortorder);
216 $offset = $limit * $page;
218 $sql .= $this->db->plimit($limit + 1, $offset);
221 $result = $this->db->query($sql);
223 $num = $this->db->num_rows($result);
224 $min = min($num, ($limit <= 0 ? $num : $limit));
227 $obj = $this->db->fetch_object($result);
228 $soc_static =
new Societe($this->db);
229 if ($soc_static->fetch($obj->rowid)) {
230 if (isModEnabled(
'mailing')) {
231 $soc_static->getNoEmail();
238 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
240 if (!count($obj_ret)) {
241 throw new RestException(404,
'Thirdparties not found');
245 if ($pagination_data) {
246 $totalsResult = $this->db->query($sqlTotals);
247 $total = $this->db->fetch_object($totalsResult)->total;
252 $obj_ret[
'data'] = $tmp;
253 $obj_ret[
'pagination'] = [
254 'total' => (int) $total,
256 'page_count' => ceil((
int) $total / $limit),
270 public function post($request_data =
null)
272 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
273 throw new RestException(403);
276 $result = $this->
_validate($request_data);
278 foreach ($request_data as $field => $value) {
279 if ($field ===
'caller') {
281 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
285 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
288 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
289 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
291 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
292 $this->company->setNoEmail($this->company->no_email);
295 return $this->company->id;
309 public function put(
$id, $request_data =
null)
311 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
312 throw new RestException(403);
315 $result = $this->company->fetch(
$id);
317 throw new RestException(404,
'Thirdparty not found');
321 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
324 foreach ($request_data as $field => $value) {
325 if ($field ==
'id') {
328 if ($field ===
'caller') {
330 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
333 if ($field ==
'array_options' && is_array($value)) {
334 foreach ($value as $index => $val) {
335 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
339 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
342 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
343 $this->company->setNoEmail($this->company->no_email);
346 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
347 return $this->
get(
$id);
349 throw new RestException(500, $this->company->error);
369 if (
$id == $idtodelete) {
370 throw new RestException(400,
'Try to merge a thirdparty into itself');
373 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
374 throw new RestException(403);
377 $result = $this->company->fetch(
$id);
379 throw new RestException(404,
'Thirdparty not found');
383 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
386 $companytoremove =
new Societe($this->db);
387 $result = $companytoremove->fetch($idtodelete);
389 throw new RestException(404,
'Thirdparty not found');
393 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
396 $user = DolibarrApiAccess::$user;
397 $result = $this->company->mergeCompany($companytoremove->id);
399 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
402 return $this->
get(
$id);
411 public function delete(
$id)
413 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
414 throw new RestException(403);
416 $result = $this->company->fetch(
$id);
418 throw new RestException(404,
'Thirdparty not found');
421 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
423 $this->company->oldcopy = clone $this->company;
425 $res = $this->company->delete(
$id);
427 throw new RestException(500,
"Can't delete, error occurs");
428 } elseif ($res == 0) {
429 throw new RestException(409,
"Can't delete, that product is probably used");
435 'message' =>
'Object deleted'
459 if (!isModEnabled(
'societe')) {
460 throw new RestException(501,
'Module "Thirdparties" needed for this request');
463 if (!isModEnabled(
"product")) {
464 throw new RestException(501,
'Module "Products" needed for this request');
468 throw new RestException(501,
'Multiprices features activation needed for this request');
471 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
472 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
475 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
476 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
479 $result = $this->company->fetch(
$id);
481 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
484 if (empty($result)) {
485 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
489 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
492 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
494 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
514 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
515 throw new RestException(403);
517 $result = $this->company->fetch(
$id);
519 throw new RestException(404,
'Thirdparty not found');
521 $usertmp =
new User($this->db);
522 $result = $usertmp->fetch($representative_id);
524 throw new RestException(404,
'User not found');
527 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
529 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
548 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
549 throw new RestException(403);
551 $result = $this->company->fetch(
$id);
553 throw new RestException(404,
'Thirdparty not found');
555 $usertmp =
new User($this->db);
556 $result = $usertmp->fetch($representative_id);
558 throw new RestException(404,
'User not found');
561 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
563 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
580 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
582 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
583 throw new RestException(403);
586 $result = $this->company->fetch(
$id);
588 throw new RestException(404,
'Thirdparty not found');
593 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
595 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
596 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
599 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
603 return $arrayofcateg;
617 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
618 throw new RestException(403);
621 $result = $this->company->fetch(
$id);
623 throw new RestException(404,
'Thirdparty not found');
626 $result = $category->fetch($category_id);
628 throw new RestException(404,
'category not found');
632 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
635 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
638 $category->add_type($this->company,
'customer');
655 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
656 throw new RestException(403);
659 $result = $this->company->fetch(
$id);
661 throw new RestException(404,
'Thirdparty not found');
664 $result = $category->fetch($category_id);
666 throw new RestException(404,
'category not found');
670 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
673 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 $category->del_type($this->company,
'customer');
696 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
697 throw new RestException(403);
700 $result = $this->company->fetch(
$id);
702 throw new RestException(404,
'Thirdparty not found');
707 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
709 if (is_numeric($result) && $result < 0) {
710 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
713 if (is_numeric($result) && $result == 0) {
732 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
733 throw new RestException(403);
736 $result = $this->company->fetch(
$id);
738 throw new RestException(404,
'Thirdparty not found');
741 $result = $category->fetch($category_id);
743 throw new RestException(404,
'category not found');
747 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
750 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
753 $category->add_type($this->company,
'supplier');
770 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
771 throw new RestException(403);
774 $result = $this->company->fetch(
$id);
776 throw new RestException(404,
'Thirdparty not found');
779 $result = $category->fetch($category_id);
781 throw new RestException(404,
'category not found');
785 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
788 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
791 $category->del_type($this->company,
'supplier');
813 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
814 throw new RestException(403);
818 throw new RestException(400,
'Thirdparty ID is mandatory');
822 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
825 $result = $this->company->fetch(
$id);
827 throw new RestException(404,
'Thirdparty not found');
830 $result = $this->company->getOutstandingProposals($mode);
832 unset($result[
'total_ht']);
833 unset($result[
'total_ttc']);
855 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
856 throw new RestException(403);
860 throw new RestException(400,
'Thirdparty ID is mandatory');
864 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
867 $result = $this->company->fetch(
$id);
869 throw new RestException(404,
'Thirdparty not found');
872 $result = $this->company->getOutstandingOrders($mode);
874 unset($result[
'total_ht']);
875 unset($result[
'total_ttc']);
896 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
897 throw new RestException(403);
901 throw new RestException(400,
'Thirdparty ID is mandatory');
905 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
908 $result = $this->company->fetch(
$id);
910 throw new RestException(404,
'Thirdparty not found');
913 $result = $this->company->getOutstandingBills($mode);
915 unset($result[
'total_ht']);
916 unset($result[
'total_ttc']);
937 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
938 throw new RestException(403);
942 throw new RestException(400,
'Thirdparty ID is mandatory');
946 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
949 $result = $this->company->fetch(
$id);
951 throw new RestException(404,
'Thirdparty not found');
954 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
980 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
981 throw new RestException(403);
985 throw new RestException(400,
'Thirdparty ID is mandatory');
989 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
992 $result = $this->company->fetch(
$id);
994 throw new RestException(404,
'Thirdparty not found');
998 $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";
999 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1000 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1001 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1002 if ($filter ==
"available") {
1003 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1005 if ($filter ==
"used") {
1006 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1009 $sql .= $this->db->order($sortfield, $sortorder);
1011 $result = $this->db->query($sql);
1013 throw new RestException(503, $this->db->lasterror());
1015 $num = $this->db->num_rows($result);
1016 while ($obj = $this->db->fetch_object($result)) {
1041 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1042 throw new RestException(403);
1045 throw new RestException(400,
'Thirdparty ID is mandatory');
1049 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1057 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1058 $invoice =
new Facture($this->db);
1059 $result = $invoice->list_replacable_invoices(
$id);
1061 throw new RestException(405, $invoice->error);
1085 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1086 throw new RestException(403);
1089 throw new RestException(400,
'Thirdparty ID is mandatory');
1093 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1101 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1102 $invoice =
new Facture($this->db);
1103 $result = $invoice->list_qualified_avoir_invoices(
$id);
1105 throw new RestException(405, $invoice->error);
1123 throw new RestException(400,
'Thirdparty ID is mandatory');
1125 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1126 throw new RestException(403);
1129 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1136 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1137 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1139 $sql .=
" WHERE fk_soc = ".((int)
$id);
1142 $result = $this->db->query($sql);
1143 if ($this->db->num_rows($result) == 0) {
1144 throw new RestException(404,
'Notification not found');
1149 $notifications = array();
1152 $num = $this->db->num_rows($result);
1154 $obj = $this->db->fetch_object($result);
1155 $notifications[] = $obj;
1159 throw new RestException(404,
'No notifications found');
1162 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1164 $returnNotifications = array();
1166 foreach ($notifications as $notification) {
1168 foreach ($notification as $key => $value) {
1169 if (in_array($key, $fields)) {
1173 $returnNotifications[] =
$object;
1176 return $returnNotifications;
1190 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1191 throw new RestException(403,
"User has no right to update thirdparties");
1193 if ($this->company->fetch(
$id) <= 0) {
1194 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1196 $notification =
new Notify($this->db);
1198 $notification->socid =
$id;
1200 foreach ($request_data as $field => $value) {
1201 $notification->$field = $value;
1204 $event = $notification->event;
1206 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1208 $socid = $notification->socid;
1209 $contact_id = $notification->contact_id;
1211 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1212 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1213 $exists_sql .=
" WHERE fk_action = '".$this->db->escape($event).
"'";
1214 $exists_sql .=
" AND fk_soc = '".$this->db->escape($socid).
"'";
1215 $exists_sql .=
" AND fk_contact = '".$this->db->escape($contact_id).
"'";
1217 $exists_result = $this->db->query($exists_sql);
1218 if ($this->db->num_rows($exists_result) > 0) {
1219 throw new RestException(403,
'Notification already exists');
1222 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1223 throw new RestException(500,
'Error creating Thirdparty Notification');
1226 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1227 throw new RestException(500,
'Error updating values');
1246 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1247 throw new RestException(403,
"User has no right to update thirdparties");
1249 if ($this->company->fetch(
$id) <= 0) {
1250 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1252 $notification =
new Notify($this->db);
1253 $notification->socid =
$id;
1255 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1256 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1258 $result = $this->db->query($sql);
1259 if ($this->db->num_rows($result) == 0) {
1260 throw new RestException(404,
'Action Trigger code not found');
1263 $notification->event = $this->db->fetch_row($result)[0];
1264 foreach ($request_data as $field => $value) {
1265 if ($field ===
'event') {
1266 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1268 if ($field ===
'fk_action') {
1269 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1271 $notification->$field = $value;
1274 $event = $notification->event;
1275 $socid = $notification->socid;
1276 $contact_id = $notification->contact_id;
1278 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1279 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1280 $exists_sql .=
" WHERE fk_action = '".$this->db->escape($event).
"'";
1281 $exists_sql .=
" AND fk_soc = '".$this->db->escape($socid).
"'";
1282 $exists_sql .=
" AND fk_contact = '".$this->db->escape($contact_id).
"'";
1284 $exists_result = $this->db->query($exists_sql);
1285 if ($this->db->num_rows($exists_result) > 0) {
1286 throw new RestException(403,
'Notification already exists');
1289 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1290 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1293 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1294 throw new RestException(500,
'Error updating values');
1312 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1313 throw new RestException(403);
1316 $notification =
new Notify($this->db);
1318 $notification->fetch($notification_id);
1320 $socid = (int) $notification->socid;
1322 if ($socid ==
$id) {
1323 return $notification->delete(DolibarrApiAccess::$user);
1325 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1342 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1343 throw new RestException(403,
"User has no right to update thirdparties");
1345 if ($this->company->fetch(
$id) <= 0) {
1346 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1348 $notification =
new Notify($this->db);
1351 $notification->fetch($notification_id,
$id);
1353 if ($notification->socid !=
$id) {
1354 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1357 foreach ($request_data as $field => $value) {
1358 $notification->$field = $value;
1361 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1362 throw new RestException(500,
'Error updating values');
1379 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1380 throw new RestException(403);
1383 throw new RestException(400,
'Thirdparty ID is mandatory');
1387 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1394 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1395 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1396 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1398 $sql .=
" WHERE fk_soc = ".((int)
$id);
1401 $result = $this->db->query($sql);
1403 if ($this->db->num_rows($result) == 0) {
1404 throw new RestException(404,
'Account not found');
1409 $accounts = array();
1412 $num = $this->db->num_rows($result);
1414 $obj = $this->db->fetch_object($result);
1417 if ($account->fetch($obj->rowid)) {
1418 $accounts[] = $account;
1423 throw new RestException(404,
'Account not found');
1427 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1429 $returnAccounts = array();
1431 foreach ($accounts as $account) {
1433 foreach ($account as $key => $value) {
1434 if (in_array($key, $fields)) {
1435 if ($key ==
'iban') {
1445 return $returnAccounts;
1459 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1460 throw new RestException(403);
1462 if ($this->company->fetch(
$id) <= 0) {
1463 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1467 $account->socid =
$id;
1469 foreach ($request_data as $field => $value) {
1470 if ($field ===
'caller') {
1472 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1476 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1479 if ($account->create(DolibarrApiAccess::$user) < 0) {
1480 throw new RestException(500,
'Error creating Company Bank account');
1483 if (empty($account->rum)) {
1484 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1486 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1487 $account->date_rum =
dol_now();
1490 if ($account->update(DolibarrApiAccess::$user) < 0) {
1491 throw new RestException(500,
'Error updating values');
1510 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1511 throw new RestException(403);
1513 if ($this->company->fetch(
$id) <= 0) {
1514 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1519 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
1521 if ($account->socid !=
$id) {
1522 throw new RestException(403);
1526 foreach ($request_data as $field => $value) {
1527 if ($field ===
'caller') {
1529 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1536 if (empty($account->rum)) {
1537 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1539 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1540 $account->date_rum =
dol_now();
1543 if ($account->update(DolibarrApiAccess::$user) < 0) {
1544 throw new RestException(500,
'Error updating values');
1562 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1563 throw new RestException(403);
1568 $account->fetch($bankaccount_id);
1570 $socid = (int) $account->socid;
1572 if ($socid ==
$id) {
1573 return $account->delete(DolibarrApiAccess::$user);
1575 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1591 global
$conf, $langs;
1593 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1595 if ($this->company->fetch(
$id) <= 0) {
1596 throw new RestException(404,
'Thirdparty not found');
1599 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1600 throw new RestException(403);
1603 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1605 $this->company->fk_bank = $this->company->fk_account;
1608 $outputlangs = $langs;
1613 if (isset($this->company->thirdparty->default_lang)) {
1614 $newlang = $this->company->thirdparty->default_lang;
1615 } elseif (isset($this->company->default_lang)) {
1616 $newlang = $this->company->default_lang;
1619 if (!empty($newlang)) {
1621 $outputlangs->setDefaultLang($newlang);
1624 $sql =
"SELECT rowid";
1625 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1627 $sql .=
" WHERE fk_soc = ".((int)
$id);
1629 if ($companybankid) {
1630 $sql .=
" AND rowid = ".((int) $companybankid);
1634 $accounts = array();
1636 $result = $this->db->query($sql);
1638 if ($this->db->num_rows($result) == 0) {
1639 throw new RestException(404,
'Bank account not found');
1642 $num = $this->db->num_rows($result);
1644 $obj = $this->db->fetch_object($result);
1647 if ($account->fetch($obj->rowid)) {
1648 $accounts[] = $account;
1653 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1656 $moreparams = array(
1657 'use_companybankid' => $accounts[0]->
id,
1658 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
1661 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1664 return array(
"success" => $result);
1666 throw new RestException(500,
'Error generating the document '.$this->company->error);
1684 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1685 throw new RestException(403);
1689 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1695 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1696 $sql .=
" WHERE fk_soc = ".((int)
$id);
1698 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1701 $result = $this->db->query($sql);
1703 if ($result && $this->db->num_rows($result) == 0) {
1704 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
1709 $accounts = array();
1711 $num = $this->db->num_rows($result);
1713 $obj = $this->db->fetch_object($result);
1716 if ($account->fetch($obj->rowid)) {
1717 $accounts[] = $account;
1722 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1724 $returnAccounts = array();
1726 foreach ($accounts as $account) {
1728 foreach ($account as $key => $value) {
1729 if (in_array($key, $fields)) {
1736 return $returnAccounts;
1753 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1754 throw new RestException(403);
1757 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1758 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
1759 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
1761 $result = $this->db->query($sql);
1763 if ($result && $this->db->num_rows($result) == 1) {
1764 $obj = $this->db->fetch_object($result);
1765 $returnThirdparty = $this->
_fetch($obj->fk_soc);
1767 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
1771 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1774 return $returnThirdparty;
1798 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1799 throw new RestException(403);
1802 if (!isset($request_data[
'site'])) {
1803 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1806 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1807 $result = $this->db->query($sql);
1809 if ($result && $this->db->num_rows($result) == 0) {
1811 if (!isset($request_data[
'login'])) {
1812 $account->login =
"";
1814 $account->fk_soc =
$id;
1816 foreach ($request_data as $field => $value) {
1817 if ($field ===
'caller') {
1819 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1826 if ($account->create(DolibarrApiAccess::$user) < 0) {
1827 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1834 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1862 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1863 throw new RestException(403);
1866 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1867 $result = $this->db->query($sql);
1870 if ($result && $this->db->num_rows($result) == 0) {
1871 if (!isset($request_data[
'key_account'])) {
1872 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1875 if (!isset($request_data[
'login'])) {
1876 $account->login =
"";
1879 foreach ($request_data as $field => $value) {
1880 if ($field ===
'caller') {
1882 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1889 $account->fk_soc =
$id;
1890 $account->site = $site;
1892 if ($account->create(DolibarrApiAccess::$user) < 0) {
1893 throw new RestException(500,
'Error creating SocieteAccount entity.');
1897 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1898 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1899 $result = $this->db->query($sql);
1901 if ($result && $this->db->num_rows($result) !== 0) {
1902 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.");
1906 $obj = $this->db->fetch_object($result);
1909 $account->id = $obj->rowid;
1910 $account->fk_soc =
$id;
1911 $account->site = $site;
1912 if (!isset($request_data[
'login'])) {
1913 $account->login =
"";
1915 $account->fk_user_creat = $obj->fk_user_creat;
1916 $account->date_creation = $obj->date_creation;
1918 foreach ($request_data as $field => $value) {
1919 if ($field ===
'caller') {
1921 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1928 if ($account->update(DolibarrApiAccess::$user) < 0) {
1929 throw new RestException(500,
'Error updating SocieteAccount entity.');
1956 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1957 throw new RestException(403);
1960 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
1961 $result = $this->db->query($sql);
1963 if ($result && $this->db->num_rows($result) == 0) {
1964 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
1967 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1968 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1969 $result = $this->db->query($sql);
1971 if ($result && $this->db->num_rows($result) !== 0) {
1972 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.");
1976 $obj = $this->db->fetch_object($result);
1978 $account->fetch($obj->rowid);
1980 foreach ($request_data as $field => $value) {
1981 if ($field ===
'caller') {
1983 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1990 if ($account->update(DolibarrApiAccess::$user) < 0) {
1991 throw new RestException(500,
'Error updating SocieteAccount account');
2015 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2016 throw new RestException(403);
2019 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2020 $result = $this->db->query($sql);
2022 if ($result && $this->db->num_rows($result) == 0) {
2023 throw new RestException(404);
2025 $obj = $this->db->fetch_object($result);
2027 $account->fetch($obj->rowid);
2029 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2030 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2049 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2050 throw new RestException(403);
2057 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2058 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2060 $result = $this->db->query($sql);
2062 if ($result && $this->db->num_rows($result) == 0) {
2063 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2067 $num = $this->db->num_rows($result);
2069 $obj = $this->db->fetch_object($result);
2071 $account->fetch($obj->rowid);
2073 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2074 throw new RestException(500,
'Error while deleting account attached to this third party');
2097 unset(
$object->departement_code);
2106 unset(
$object->commercial_id);
2110 unset(
$object->total_localtax1);
2111 unset(
$object->total_localtax2);
2117 unset(
$object->fk_delivery_address);
2132 $thirdparty = array();
2133 foreach (Thirdparties::$FIELDS as $field) {
2134 if (!isset($data[$field])) {
2135 throw new RestException(400,
"$field field missing");
2137 $thirdparty[$field] = $data[$field];
2163 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2165 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2166 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2170 $result = $this->company->initAsSpecimen();
2172 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2175 throw new RestException(404,
'Thirdparty not found');
2179 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2181 if (isModEnabled(
'mailing')) {
2182 $this->company->getNoEmail();
2186 $filterabsolutediscount =
"fk_facture_source IS NULL";
2187 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2189 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2190 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2193 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
2194 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
2195 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2196 $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, $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.