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(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
116 return $this->
_fetch(
'',
'',
'', $barcode);
137 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'')
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);
207 $sql .= $this->db->order($sortfield, $sortorder);
213 $offset = $limit * $page;
215 $sql .= $this->db->plimit($limit + 1, $offset);
218 $result = $this->db->query($sql);
220 $num = $this->db->num_rows($result);
221 $min = min($num, ($limit <= 0 ? $num : $limit));
224 $obj = $this->db->fetch_object($result);
225 $soc_static =
new Societe($this->db);
226 if ($soc_static->fetch($obj->rowid)) {
227 if (isModEnabled(
'mailing')) {
228 $soc_static->getNoEmail();
235 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
237 if (!count($obj_ret)) {
238 throw new RestException(404,
'Thirdparties not found');
249 public function post($request_data =
null)
251 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
252 throw new RestException(403);
255 $result = $this->
_validate($request_data);
257 foreach ($request_data as $field => $value) {
258 if ($field ===
'caller') {
260 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
264 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
267 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
268 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
270 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
271 $this->company->setNoEmail($this->company->no_email);
274 return $this->company->id;
288 public function put($id, $request_data =
null)
290 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
291 throw new RestException(403);
294 $result = $this->company->fetch($id);
296 throw new RestException(404,
'Thirdparty not found');
300 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
303 foreach ($request_data as $field => $value) {
304 if ($field ==
'id') {
307 if ($field ===
'caller') {
309 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
312 if ($field ==
'array_options' && is_array($value)) {
313 foreach ($value as $index => $val) {
314 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
318 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
321 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
322 $this->company->setNoEmail($this->company->no_email);
325 if ($this->company->update($id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
326 return $this->
get($id);
328 throw new RestException(500, $this->company->error);
346 public function merge($id, $idtodelete)
348 if ($id == $idtodelete) {
349 throw new RestException(400,
'Try to merge a thirdparty into itself');
352 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
353 throw new RestException(403);
356 $result = $this->company->fetch($id);
358 throw new RestException(404,
'Thirdparty not found');
362 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
365 $companytoremove =
new Societe($this->db);
366 $result = $companytoremove->fetch($idtodelete);
368 throw new RestException(404,
'Thirdparty not found');
372 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
375 $user = DolibarrApiAccess::$user;
376 $result = $this->company->mergeCompany($companytoremove->id);
378 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
381 return $this->
get($id);
390 public function delete($id)
392 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
393 throw new RestException(403);
395 $result = $this->company->fetch($id);
397 throw new RestException(404,
'Thirdparty not found');
400 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
402 $this->company->oldcopy = clone $this->company;
404 $res = $this->company->delete($id);
406 throw new RestException(500,
"Can't delete, error occurs");
407 } elseif ($res == 0) {
408 throw new RestException(409,
"Can't delete, that product is probably used");
414 'message' =>
'Object deleted'
438 if (!isModEnabled(
'societe')) {
439 throw new RestException(501,
'Module "Thirdparties" needed for this request');
442 if (!isModEnabled(
"product")) {
443 throw new RestException(501,
'Module "Products" needed for this request');
447 throw new RestException(501,
'Multiprices features activation needed for this request');
450 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
451 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
454 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
455 throw new RestException(403,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
458 $result = $this->company->fetch($id);
460 throw new RestException(404,
'Thirdparty '.$id.
' not found');
463 if (empty($result)) {
464 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
468 throw new RestException(403,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
471 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
473 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
493 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
494 throw new RestException(403);
496 $result = $this->company->fetch($id);
498 throw new RestException(404,
'Thirdparty not found');
500 $usertmp =
new User($this->db);
501 $result = $usertmp->fetch($representative_id);
503 throw new RestException(404,
'User not found');
506 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
527 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
528 throw new RestException(403);
530 $result = $this->company->fetch($id);
532 throw new RestException(404,
'Thirdparty not found');
534 $usertmp =
new User($this->db);
535 $result = $usertmp->fetch($representative_id);
537 throw new RestException(404,
'User not found');
540 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
542 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
559 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
561 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
562 throw new RestException(403);
565 $result = $this->company->fetch($id);
567 throw new RestException(404,
'Thirdparty not found');
572 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
574 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
575 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
578 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
582 return $arrayofcateg;
596 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
597 throw new RestException(403);
600 $result = $this->company->fetch($id);
602 throw new RestException(404,
'Thirdparty not found');
605 $result = $category->fetch($category_id);
607 throw new RestException(404,
'category not found');
611 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
614 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
617 $category->add_type($this->company,
'customer');
634 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
635 throw new RestException(403);
638 $result = $this->company->fetch($id);
640 throw new RestException(404,
'Thirdparty not found');
643 $result = $category->fetch($category_id);
645 throw new RestException(404,
'category not found');
649 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
652 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
655 $category->del_type($this->company,
'customer');
675 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
676 throw new RestException(403);
679 $result = $this->company->fetch($id);
681 throw new RestException(404,
'Thirdparty not found');
686 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
688 if (is_numeric($result) && $result < 0) {
689 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
692 if (is_numeric($result) && $result == 0) {
711 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
712 throw new RestException(403);
715 $result = $this->company->fetch($id);
717 throw new RestException(404,
'Thirdparty not found');
720 $result = $category->fetch($category_id);
722 throw new RestException(404,
'category not found');
726 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
729 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
732 $category->add_type($this->company,
'supplier');
749 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
750 throw new RestException(403);
753 $result = $this->company->fetch($id);
755 throw new RestException(404,
'Thirdparty not found');
758 $result = $category->fetch($category_id);
760 throw new RestException(404,
'category not found');
764 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
767 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
770 $category->del_type($this->company,
'supplier');
792 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
793 throw new RestException(403);
797 throw new RestException(400,
'Thirdparty ID is mandatory');
801 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
804 $result = $this->company->fetch($id);
806 throw new RestException(404,
'Thirdparty not found');
809 $result = $this->company->getOutstandingProposals($mode);
811 unset($result[
'total_ht']);
812 unset($result[
'total_ttc']);
834 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
835 throw new RestException(403);
839 throw new RestException(400,
'Thirdparty ID is mandatory');
843 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
846 $result = $this->company->fetch($id);
848 throw new RestException(404,
'Thirdparty not found');
851 $result = $this->company->getOutstandingOrders($mode);
853 unset($result[
'total_ht']);
854 unset($result[
'total_ttc']);
875 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
876 throw new RestException(403);
880 throw new RestException(400,
'Thirdparty ID is mandatory');
884 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
887 $result = $this->company->fetch($id);
889 throw new RestException(404,
'Thirdparty not found');
892 $result = $this->company->getOutstandingBills($mode);
894 unset($result[
'total_ht']);
895 unset($result[
'total_ttc']);
916 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
917 throw new RestException(403);
921 throw new RestException(400,
'Thirdparty ID is mandatory');
925 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
928 $result = $this->company->fetch($id);
930 throw new RestException(404,
'Thirdparty not found');
933 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
959 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
960 throw new RestException(403);
964 throw new RestException(400,
'Thirdparty ID is mandatory');
968 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
971 $result = $this->company->fetch($id);
973 throw new RestException(404,
'Thirdparty not found');
977 $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";
978 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
979 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
980 $sql .=
" WHERE re.fk_soc = ".((int) $id);
981 if ($filter ==
"available") {
982 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
984 if ($filter ==
"used") {
985 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
988 $sql .= $this->db->order($sortfield, $sortorder);
990 $result = $this->db->query($sql);
992 throw new RestException(503, $this->db->lasterror());
994 $num = $this->db->num_rows($result);
995 while ($obj = $this->db->fetch_object($result)) {
1020 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1021 throw new RestException(403);
1024 throw new RestException(400,
'Thirdparty ID is mandatory');
1028 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1036 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1037 $invoice =
new Facture($this->db);
1038 $result = $invoice->list_replacable_invoices($id);
1040 throw new RestException(405, $invoice->error);
1064 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1065 throw new RestException(403);
1068 throw new RestException(400,
'Thirdparty ID is mandatory');
1072 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1080 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1081 $invoice =
new Facture($this->db);
1082 $result = $invoice->list_qualified_avoir_invoices($id);
1084 throw new RestException(405, $invoice->error);
1102 throw new RestException(400,
'Thirdparty ID is mandatory');
1104 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1105 throw new RestException(403);
1108 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1115 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1116 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1118 $sql .=
" WHERE fk_soc = ".((int) $id);
1121 $result = $this->db->query($sql);
1122 if ($this->db->num_rows($result) == 0) {
1123 throw new RestException(404,
'Notification not found');
1128 $notifications = array();
1131 $num = $this->db->num_rows($result);
1133 $obj = $this->db->fetch_object($result);
1134 $notifications[] = $obj;
1138 throw new RestException(404,
'No notifications found');
1141 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1143 $returnNotifications = array();
1145 foreach ($notifications as $notification) {
1147 foreach ($notification as $key => $value) {
1148 if (in_array($key, $fields)) {
1152 $returnNotifications[] =
$object;
1155 return $returnNotifications;
1169 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1170 throw new RestException(403,
"User has no right to update thirdparties");
1172 if ($this->company->fetch($id) <= 0) {
1173 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1175 $notification =
new Notify($this->db);
1177 $notification->socid = $id;
1179 foreach ($request_data as $field => $value) {
1180 $notification->$field = $value;
1183 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1184 throw new RestException(500,
'Error creating Thirdparty Notification');
1187 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1188 throw new RestException(500,
'Error updating values');
1206 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1207 throw new RestException(403);
1210 $notification =
new Notify($this->db);
1212 $notification->fetch($notification_id);
1214 $socid = (int) $notification->socid;
1216 if ($socid == $id) {
1217 return $notification->delete(DolibarrApiAccess::$user);
1219 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1236 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1237 throw new RestException(403,
"User has no right to update thirdparties");
1239 if ($this->company->fetch($id) <= 0) {
1240 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1242 $notification =
new Notify($this->db);
1245 $notification->fetch($notification_id, $id);
1247 if ($notification->socid != $id) {
1248 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1251 foreach ($request_data as $field => $value) {
1252 $notification->$field = $value;
1255 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1256 throw new RestException(500,
'Error updating values');
1273 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1274 throw new RestException(403);
1277 throw new RestException(400,
'Thirdparty ID is mandatory');
1281 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1288 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1289 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1290 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1292 $sql .=
" WHERE fk_soc = ".((int) $id);
1295 $result = $this->db->query($sql);
1297 if ($this->db->num_rows($result) == 0) {
1298 throw new RestException(404,
'Account not found');
1303 $accounts = array();
1306 $num = $this->db->num_rows($result);
1308 $obj = $this->db->fetch_object($result);
1310 if ($account->fetch($obj->rowid)) {
1311 $accounts[] = $account;
1316 throw new RestException(404,
'Account not found');
1320 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1322 $returnAccounts = array();
1324 foreach ($accounts as $account) {
1326 foreach ($account as $key => $value) {
1327 if (in_array($key, $fields)) {
1334 return $returnAccounts;
1348 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1349 throw new RestException(403);
1351 if ($this->company->fetch($id) <= 0) {
1352 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1356 $account->socid = $id;
1358 foreach ($request_data as $field => $value) {
1359 if ($field ===
'caller') {
1361 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1365 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1368 if ($account->create(DolibarrApiAccess::$user) < 0) {
1369 throw new RestException(500,
'Error creating Company Bank account');
1372 if (empty($account->rum)) {
1373 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1375 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1376 $account->date_rum =
dol_now();
1379 if ($account->update(DolibarrApiAccess::$user) < 0) {
1380 throw new RestException(500,
'Error updating values');
1399 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1400 throw new RestException(403);
1402 if ($this->company->fetch($id) <= 0) {
1403 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1408 $account->fetch($bankaccount_id,
'', $id, -1,
'');
1410 if ($account->socid != $id) {
1411 throw new RestException(403);
1415 foreach ($request_data as $field => $value) {
1416 if ($field ===
'caller') {
1418 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1425 if (empty($account->rum)) {
1426 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1428 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1429 $account->date_rum =
dol_now();
1432 if ($account->update(DolibarrApiAccess::$user) < 0) {
1433 throw new RestException(500,
'Error updating values');
1451 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1452 throw new RestException(403);
1457 $account->fetch($bankaccount_id);
1459 $socid = (int) $account->socid;
1461 if ($socid == $id) {
1462 return $account->delete(DolibarrApiAccess::$user);
1464 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1480 global $conf, $langs;
1482 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1484 if ($this->company->fetch($id) <= 0) {
1485 throw new RestException(404,
'Thirdparty not found');
1488 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1489 throw new RestException(403);
1492 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1494 $this->company->fk_bank = $this->company->fk_account;
1497 $outputlangs = $langs;
1502 if (isset($this->company->thirdparty->default_lang)) {
1503 $newlang = $this->company->thirdparty->default_lang;
1504 } elseif (isset($this->company->default_lang)) {
1505 $newlang = $this->company->default_lang;
1508 if (!empty($newlang)) {
1509 $outputlangs =
new Translate(
"", $conf);
1510 $outputlangs->setDefaultLang($newlang);
1513 $sql =
"SELECT rowid";
1514 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1516 $sql .=
" WHERE fk_soc = ".((int) $id);
1518 if ($companybankid) {
1519 $sql .=
" AND rowid = ".((int) $companybankid);
1523 $accounts = array();
1525 $result = $this->db->query($sql);
1527 if ($this->db->num_rows($result) == 0) {
1528 throw new RestException(404,
'Bank account not found');
1531 $num = $this->db->num_rows($result);
1533 $obj = $this->db->fetch_object($result);
1536 if ($account->fetch($obj->rowid)) {
1537 $accounts[] = $account;
1542 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1545 $moreparams = array(
1546 'use_companybankid' => $accounts[0]->
id,
1547 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1550 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1553 return array(
"success" => $result);
1555 throw new RestException(500,
'Error generating the document '.$this->company->error);
1573 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1574 throw new RestException(403);
1578 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1584 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1585 $sql .=
" WHERE fk_soc = ".((int) $id);
1587 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1590 $result = $this->db->query($sql);
1592 if ($result && $this->db->num_rows($result) == 0) {
1593 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
1598 $accounts = array();
1600 $num = $this->db->num_rows($result);
1602 $obj = $this->db->fetch_object($result);
1605 if ($account->fetch($obj->rowid)) {
1606 $accounts[] = $account;
1611 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1613 $returnAccounts = array();
1615 foreach ($accounts as $account) {
1617 foreach ($account as $key => $value) {
1618 if (in_array($key, $fields)) {
1625 return $returnAccounts;
1649 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1650 throw new RestException(403);
1653 if (!isset($request_data[
'site'])) {
1654 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1657 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1658 $result = $this->db->query($sql);
1660 if ($result && $this->db->num_rows($result) == 0) {
1662 if (!isset($request_data[
'login'])) {
1663 $account->login =
"";
1665 $account->fk_soc = $id;
1667 foreach ($request_data as $field => $value) {
1668 if ($field ===
'caller') {
1670 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1677 if ($account->create(DolibarrApiAccess::$user) < 0) {
1678 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1685 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1713 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1714 throw new RestException(403);
1717 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1718 $result = $this->db->query($sql);
1721 if ($result && $this->db->num_rows == 0) {
1722 if (!isset($request_data[
'key_account'])) {
1723 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1726 if (!isset($request_data[
'login'])) {
1727 $account->login =
"";
1730 foreach ($request_data as $field => $value) {
1731 if ($field ===
'caller') {
1733 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1740 $account->fk_soc = $id;
1741 $account->site = $site;
1743 if ($account->create(DolibarrApiAccess::$user) < 0) {
1744 throw new RestException(500,
'Error creating SocieteAccount entity.');
1748 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1749 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1750 $result = $this->db->query($sql);
1752 if ($result && $this->db->num_rows($result) !== 0) {
1753 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.");
1757 $obj = $this->db->fetch_object($result);
1760 $account->id = $obj->rowid;
1761 $account->fk_soc = $id;
1762 $account->site = $site;
1763 if (!isset($request_data[
'login'])) {
1764 $account->login =
"";
1766 $account->fk_user_creat = $obj->fk_user_creat;
1767 $account->date_creation = $obj->date_creation;
1769 foreach ($request_data as $field => $value) {
1770 if ($field ===
'caller') {
1772 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1779 if ($account->update(DolibarrApiAccess::$user) < 0) {
1780 throw new RestException(500,
'Error updating SocieteAccount entity.');
1807 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1808 throw new RestException(403);
1811 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1812 $result = $this->db->query($sql);
1814 if ($result && $this->db->num_rows($result) == 0) {
1815 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
1818 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1819 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1820 $result = $this->db->query($sql);
1822 if ($result && $this->db->num_rows($result) !== 0) {
1823 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.");
1827 $obj = $this->db->fetch_object($result);
1829 $account->fetch($obj->rowid);
1831 foreach ($request_data as $field => $value) {
1832 if ($field ===
'caller') {
1834 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1841 if ($account->update(DolibarrApiAccess::$user) < 0) {
1842 throw new RestException(500,
'Error updating SocieteAccount account');
1866 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1867 throw new RestException(403);
1870 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1871 $result = $this->db->query($sql);
1873 if ($result && $this->db->num_rows($result) == 0) {
1874 throw new RestException(404);
1876 $obj = $this->db->fetch_object($result);
1878 $account->fetch($obj->rowid);
1880 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1881 throw new RestException(500,
"Error while deleting $site account attached to this third party");
1900 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1901 throw new RestException(403);
1908 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1909 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1911 $result = $this->db->query($sql);
1913 if ($result && $this->db->num_rows($result) == 0) {
1914 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
1918 $num = $this->db->num_rows($result);
1920 $obj = $this->db->fetch_object($result);
1922 $account->fetch($obj->rowid);
1924 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1925 throw new RestException(500,
'Error while deleting account attached to this third party');
1948 unset(
$object->departement_code);
1957 unset(
$object->commercial_id);
1961 unset(
$object->total_localtax1);
1962 unset(
$object->total_localtax2);
1968 unset(
$object->fk_delivery_address);
1983 $thirdparty = array();
1984 foreach (Thirdparties::$FIELDS as $field) {
1985 if (!isset($data[$field])) {
1986 throw new RestException(400,
"$field field missing");
1988 $thirdparty[$field] = $data[$field];
2014 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2018 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2019 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2023 $result = $this->company->initAsSpecimen();
2025 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2028 throw new RestException(404,
'Thirdparty not found');
2032 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2034 if (isModEnabled(
'mailing')) {
2035 $this->company->getNoEmail();
2039 $filterabsolutediscount =
"fk_facture_source IS NULL";
2040 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2042 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2043 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2046 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
2047 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
2048 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2049 $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.
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)
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)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='')
List thirdparties.
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.
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 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.