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';
61 $this->company =
new Societe($this->db);
64 static::$FIELDS[] =
'email';
78 public function get($id)
97 return $this->
_fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
114 return $this->
_fetch(
'',
'',
'', $barcode);
135 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'')
139 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
140 throw new RestException(401);
144 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
148 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
149 $search_sale = DolibarrApiAccess::$user->id;
152 $sql =
"SELECT t.rowid";
153 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
154 $sql .=
", sc.fk_soc, sc.fk_user";
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 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
167 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
169 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
170 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
171 $sql .=
" AND t.fk_stcomm = st.id";
174 $sql .=
" AND t.client IN (1, 3)";
175 } elseif ($mode == 2) {
176 $sql .=
" AND t.client IN (2, 3)";
177 } elseif ($mode == 3) {
178 $sql .=
" AND t.client IN (0)";
179 } elseif ($mode == 4) {
180 $sql .=
" AND t.fournisseur IN (1)";
185 if (!empty($mode) && $mode != 4) {
186 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
187 } elseif (!empty($mode) && $mode == 4) {
188 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
190 $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))";
194 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
195 $sql .=
" AND t.rowid = sc.fk_soc";
199 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
201 if ($search_sale > 0) {
202 $sql .=
" AND t.rowid = sc.fk_soc";
205 if ($search_sale > 0) {
206 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
213 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
217 $sql .= $this->db->order($sortfield, $sortorder);
223 $offset = $limit * $page;
225 $sql .= $this->db->plimit($limit + 1, $offset);
228 $result = $this->db->query($sql);
230 $num = $this->db->num_rows($result);
231 $min = min($num, ($limit <= 0 ? $num : $limit));
234 $obj = $this->db->fetch_object($result);
235 $soc_static =
new Societe($this->db);
236 if ($soc_static->fetch($obj->rowid)) {
237 if (isModEnabled(
'mailing')) {
238 $soc_static->getNoEmail();
245 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
247 if (!count($obj_ret)) {
248 throw new RestException(404,
'Thirdparties not found');
259 public function post($request_data =
null)
261 if (!DolibarrApiAccess::$user->rights->societe->creer) {
262 throw new RestException(401);
265 $result = $this->
_validate($request_data);
267 foreach ($request_data as $field => $value) {
268 if ($field ===
'caller') {
270 $this->company->context[
'caller'] = $request_data[
'caller'];
274 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
277 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
280 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
281 $this->company->setNoEmail($this->company->no_email);
284 return $this->company->id;
297 public function put($id, $request_data =
null)
299 if (!DolibarrApiAccess::$user->rights->societe->creer) {
300 throw new RestException(401);
303 $result = $this->company->fetch($id);
305 throw new RestException(404,
'Thirdparty not found');
309 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
312 foreach ($request_data as $field => $value) {
313 if ($field ==
'id') {
316 if ($field ===
'caller') {
318 $this->company->context[
'caller'] = $request_data[
'caller'];
322 if ($field ==
'array_options' && is_array($value)) {
323 foreach ($value as $index => $val) {
324 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
328 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
331 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
332 $this->company->setNoEmail($this->company->no_email);
335 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1) > 0) {
336 return $this->
get($id);
338 throw new RestException(500, $this->company->error);
356 public function merge($id, $idtodelete)
362 if ($id == $idtodelete) {
363 throw new RestException(400,
'Try to merge a thirdparty into itself');
366 if (!DolibarrApiAccess::$user->rights->societe->creer) {
367 throw new RestException(401);
370 $result = $this->company->fetch($id);
372 throw new RestException(404,
'Thirdparty not found');
376 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
379 $companytoremove =
new Societe($this->db);
380 $result = $companytoremove->fetch($idtodelete);
382 throw new RestException(404,
'Thirdparty not found');
386 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
389 $user = DolibarrApiAccess::$user;
390 $result = $this->company->mergeCompany($companytoremove->id);
392 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
395 return $this->
get($id);
404 public function delete($id)
406 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
407 throw new RestException(401);
409 $result = $this->company->fetch($id);
411 throw new RestException(404,
'Thirdparty not found');
414 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 $this->company->oldcopy = clone $this->company;
418 $res = $this->company->delete($id);
420 throw new RestException(500,
"Can't delete, error occurs");
421 } elseif ($res == 0) {
422 throw new RestException(409,
"Can't delete, that product is probably used");
428 'message' =>
'Object deleted'
452 if (!isModEnabled(
'societe')) {
453 throw new RestException(501,
'Module "Thirdparties" needed for this request');
456 if (!isModEnabled(
"product")) {
457 throw new RestException(501,
'Module "Products" needed for this request');
461 throw new RestException(501,
'Multiprices features activation needed for this request');
464 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
465 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
468 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
469 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
472 $result = $this->company->fetch($id);
474 throw new RestException(404,
'Thirdparty '.$id.
' not found');
477 if (empty($result)) {
478 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
482 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
485 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
487 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
505 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
507 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
508 throw new RestException(401);
511 $result = $this->company->fetch($id);
513 throw new RestException(404,
'Thirdparty not found');
518 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
520 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
521 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
524 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
528 return $arrayofcateg;
542 if (!DolibarrApiAccess::$user->rights->societe->creer) {
543 throw new RestException(401);
546 $result = $this->company->fetch($id);
548 throw new RestException(404,
'Thirdparty not found');
551 $result = $category->fetch($category_id);
553 throw new RestException(404,
'category not found');
557 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
560 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
563 $category->add_type($this->company,
'customer');
580 if (!DolibarrApiAccess::$user->rights->societe->creer) {
581 throw new RestException(401);
584 $result = $this->company->fetch($id);
586 throw new RestException(404,
'Thirdparty not found');
589 $result = $category->fetch($category_id);
591 throw new RestException(404,
'category not found');
595 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
598 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
601 $category->del_type($this->company,
'customer');
621 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
622 throw new RestException(401);
625 $result = $this->company->fetch($id);
627 throw new RestException(404,
'Thirdparty not found');
632 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
634 if (is_numeric($result) && $result < 0) {
635 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
638 if (is_numeric($result) && $result == 0) {
657 if (!DolibarrApiAccess::$user->rights->societe->creer) {
658 throw new RestException(401);
661 $result = $this->company->fetch($id);
663 throw new RestException(404,
'Thirdparty not found');
666 $result = $category->fetch($category_id);
668 throw new RestException(404,
'category not found');
672 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
675 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
678 $category->add_type($this->company,
'supplier');
695 if (!DolibarrApiAccess::$user->rights->societe->creer) {
696 throw new RestException(401);
699 $result = $this->company->fetch($id);
701 throw new RestException(404,
'Thirdparty not found');
704 $result = $category->fetch($category_id);
706 throw new RestException(404,
'category not found');
710 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
713 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
716 $category->del_type($this->company,
'supplier');
738 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
739 throw new RestException(401);
743 throw new RestException(400,
'Thirdparty ID is mandatory');
747 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
750 $result = $this->company->fetch($id);
752 throw new RestException(404,
'Thirdparty not found');
755 $result = $this->company->getOutstandingProposals($mode);
757 unset($result[
'total_ht']);
758 unset($result[
'total_ttc']);
780 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
781 throw new RestException(401);
785 throw new RestException(400,
'Thirdparty ID is mandatory');
789 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
792 $result = $this->company->fetch($id);
794 throw new RestException(404,
'Thirdparty not found');
797 $result = $this->company->getOutstandingOrders($mode);
799 unset($result[
'total_ht']);
800 unset($result[
'total_ttc']);
821 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
822 throw new RestException(401);
826 throw new RestException(400,
'Thirdparty ID is mandatory');
830 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
833 $result = $this->company->fetch($id);
835 throw new RestException(404,
'Thirdparty not found');
838 $result = $this->company->getOutstandingBills($mode);
840 unset($result[
'total_ht']);
841 unset($result[
'total_ttc']);
862 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
863 throw new RestException(401);
867 throw new RestException(400,
'Thirdparty ID is mandatory');
871 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
874 $result = $this->company->fetch($id);
876 throw new RestException(404,
'Thirdparty not found');
879 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
905 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
906 throw new RestException(401);
910 throw new RestException(400,
'Thirdparty ID is mandatory');
914 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
917 $result = $this->company->fetch($id);
919 throw new RestException(404,
'Thirdparty not found');
923 $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";
924 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
925 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
926 $sql .=
" WHERE re.fk_soc = ".((int) $id);
927 if ($filter ==
"available") {
928 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
930 if ($filter ==
"used") {
931 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
934 $sql .= $this->db->order($sortfield, $sortorder);
936 $result = $this->db->query($sql);
938 throw new RestException(503, $this->db->lasterror());
940 $num = $this->db->num_rows($result);
941 while ($obj = $this->db->fetch_object($result)) {
966 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
967 throw new RestException(401);
970 throw new RestException(400,
'Thirdparty ID is mandatory');
974 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
982 $invoice =
new Facture($this->db);
983 $result = $invoice->list_replacable_invoices($id);
985 throw new RestException(405, $invoice->error);
1009 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1010 throw new RestException(401);
1013 throw new RestException(400,
'Thirdparty ID is mandatory');
1017 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1025 $invoice =
new Facture($this->db);
1026 $result = $invoice->list_qualified_avoir_invoices($id);
1028 throw new RestException(405, $invoice->error);
1045 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1046 throw new RestException(401);
1049 throw new RestException(400,
'Thirdparty ID is mandatory');
1053 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1060 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1061 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1062 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1064 $sql .=
" WHERE fk_soc = ".((int) $id);
1067 $result = $this->db->query($sql);
1069 if ($this->db->num_rows($result) == 0) {
1070 throw new RestException(404,
'Account not found');
1075 $accounts = array();
1078 $num = $this->db->num_rows($result);
1080 $obj = $this->db->fetch_object($result);
1082 if ($account->fetch($obj->rowid)) {
1083 $accounts[] = $account;
1088 throw new RestException(404,
'Account not found');
1092 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1094 $returnAccounts = array();
1096 foreach ($accounts as $account) {
1098 foreach ($account as $key => $value) {
1099 if (in_array($key, $fields)) {
1100 $object[$key] = $value;
1103 $returnAccounts[] = $object;
1106 return $returnAccounts;
1120 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1121 throw new RestException(401);
1123 if ($this->company->fetch($id) <= 0) {
1124 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1128 $account->socid = $id;
1130 foreach ($request_data as $field => $value) {
1131 if ($field ===
'caller') {
1133 $this->company->context[
'caller'] = $request_data[
'caller'];
1137 $account->$field = $value;
1140 if ($account->create(DolibarrApiAccess::$user) < 0) {
1141 throw new RestException(500,
'Error creating Company Bank account');
1144 if (empty($account->rum)) {
1145 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1147 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1148 $account->date_rum =
dol_now();
1151 if ($account->update(DolibarrApiAccess::$user) < 0) {
1152 throw new RestException(500,
'Error updating values');
1171 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1172 throw new RestException(401);
1174 if ($this->company->fetch($id) <= 0) {
1175 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1179 $account->fetch($bankaccount_id, $id, -1,
'');
1181 if ($account->socid != $id) {
1182 throw new RestException(401);
1186 foreach ($request_data as $field => $value) {
1187 if ($field ===
'caller') {
1189 $account->context[
'caller'] = $request_data[
'caller'];
1193 $account->$field = $value;
1196 if (empty($account->rum)) {
1197 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1199 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1200 $account->date_rum =
dol_now();
1203 if ($account->update(DolibarrApiAccess::$user) < 0) {
1204 throw new RestException(500,
'Error updating values');
1222 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1223 throw new RestException(401);
1228 $account->fetch($bankaccount_id);
1230 if (!$account->socid == $id) {
1231 throw new RestException(401);
1234 return $account->delete(DolibarrApiAccess::$user);
1249 global $conf, $langs;
1251 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1253 if ($this->company->fetch($id) <= 0) {
1254 throw new RestException(404,
'Thirdparty not found');
1257 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1258 throw new RestException(401);
1261 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1263 $this->company->fk_bank = $this->company->fk_account;
1264 $this->company->fk_account = $this->company->fk_account;
1266 $outputlangs = $langs;
1271 if (isset($this->company->thirdparty->default_lang)) {
1272 $newlang = $this->company->thirdparty->default_lang;
1273 } elseif (isset($this->company->default_lang)) {
1274 $newlang = $this->company->default_lang;
1277 if (!empty($newlang)) {
1278 $outputlangs =
new Translate(
"", $conf);
1279 $outputlangs->setDefaultLang($newlang);
1282 $sql =
"SELECT rowid";
1283 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1285 $sql .=
" WHERE fk_soc = ".((int) $id);
1287 if ($companybankid) {
1288 $sql .=
" AND rowid = ".((int) $companybankid);
1292 $accounts = array();
1294 $result = $this->db->query($sql);
1296 if ($this->db->num_rows($result) == 0) {
1297 throw new RestException(404,
'Bank account not found');
1300 $num = $this->db->num_rows($result);
1302 $obj = $this->db->fetch_object($result);
1305 if ($account->fetch($obj->rowid)) {
1306 $accounts[] = $account;
1311 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1314 $moreparams = array(
1315 'use_companybankid' => $accounts[0]->
id,
1316 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1319 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1322 return array(
"success" => $result);
1324 throw new RestException(500,
'Error generating the document '.$this->company->error);
1342 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1343 throw new RestException(401);
1347 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1353 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1354 $sql .=
" WHERE fk_soc = ".((int) $id);
1356 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1359 $result = $this->db->query($sql);
1361 if ($result && $this->db->num_rows($result) == 0) {
1362 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1367 $accounts = array();
1369 $num = $this->db->num_rows($result);
1371 $obj = $this->db->fetch_object($result);
1374 if ($account->fetch($obj->rowid)) {
1375 $accounts[] = $account;
1380 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1382 $returnAccounts = array();
1384 foreach ($accounts as $account) {
1386 foreach ($account as $key => $value) {
1387 if (in_array($key, $fields)) {
1388 $object[$key] = $value;
1391 $returnAccounts[] = $object;
1394 return $returnAccounts;
1418 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1419 throw new RestException(401);
1422 if (!isset($request_data[
'site'])) {
1423 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1426 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1427 $result = $this->db->query($sql);
1429 if ($result && $this->db->num_rows($result) == 0) {
1431 if (!isset($request_data[
'login'])) {
1432 $account->login =
"";
1434 $account->fk_soc = $id;
1436 foreach ($request_data as $field => $value) {
1437 if ($field ===
'caller') {
1439 $account->context[
'caller'] = $request_data[
'caller'];
1443 $account->$field = $value;
1446 if ($account->create(DolibarrApiAccess::$user) < 0) {
1447 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1454 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1482 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1483 throw new RestException(401);
1486 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1487 $result = $this->db->query($sql);
1490 if ($result && $this->db->num_rows == 0) {
1491 if (!isset($request_data[
'key_account'])) {
1492 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1495 if (!isset($request_data[
'login'])) {
1496 $account->login =
"";
1499 foreach ($request_data as $field => $value) {
1500 if ($field ===
'caller') {
1502 $account->context[
'caller'] = $request_data[
'caller'];
1506 $account->$field = $value;
1509 $account->fk_soc = $id;
1510 $account->site = $site;
1512 if ($account->create(DolibarrApiAccess::$user) < 0) {
1513 throw new RestException(500,
'Error creating SocieteAccount entity.');
1517 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1518 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1519 $result = $this->db->query($sql);
1521 if ($result && $this->db->num_rows($result) !== 0) {
1522 throw new RestException(409,
"You are trying to update this thirdparty SocieteAccount (gateway record) from $site to ".$request_data[
'site'].
" but another SocieteAccount entity already exists with this site key.");
1526 $obj = $this->db->fetch_object($result);
1529 $account->id = $obj->rowid;
1530 $account->fk_soc = $id;
1531 $account->site = $site;
1532 if (!isset($request_data[
'login'])) {
1533 $account->login =
"";
1535 $account->fk_user_creat = $obj->fk_user_creat;
1536 $account->date_creation = $obj->date_creation;
1538 foreach ($request_data as $field => $value) {
1539 if ($field ===
'caller') {
1541 $account->context[
'caller'] = $request_data[
'caller'];
1545 $account->$field = $value;
1548 if ($account->update(DolibarrApiAccess::$user) < 0) {
1549 throw new RestException(500,
'Error updating SocieteAccount entity.');
1576 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1577 throw new RestException(401);
1580 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1581 $result = $this->db->query($sql);
1583 if ($result && $this->db->num_rows($result) == 0) {
1584 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1587 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1588 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1589 $result = $this->db->query($sql);
1591 if ($result && $this->db->num_rows($result) !== 0) {
1592 throw new RestException(409,
"You are trying to update this thirdparty SocieteAccount (gateway record) site member from ".$site.
" to ".$request_data[
'site'].
" but another SocieteAccount entity already exists for this thirdparty with this site key.");
1596 $obj = $this->db->fetch_object($result);
1598 $account->fetch($obj->rowid);
1600 foreach ($request_data as $field => $value) {
1601 if ($field ===
'caller') {
1603 $account->context[
'caller'] = $request_data[
'caller'];
1607 $account->$field = $value;
1610 if ($account->update(DolibarrApiAccess::$user) < 0) {
1611 throw new RestException(500,
'Error updating SocieteAccount account');
1635 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1636 throw new RestException(401);
1639 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1640 $result = $this->db->query($sql);
1642 if ($result && $this->db->num_rows($result) == 0) {
1643 throw new RestException(404);
1645 $obj = $this->db->fetch_object($result);
1647 $account->fetch($obj->rowid);
1649 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1650 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1669 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1670 throw new RestException(401);
1677 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1678 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1680 $result = $this->db->query($sql);
1682 if ($result && $this->db->num_rows($result) == 0) {
1683 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1687 $num = $this->db->num_rows($result);
1689 $obj = $this->db->fetch_object($result);
1691 $account->fetch($obj->rowid);
1693 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1694 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1711 $object = parent::_cleanObjectDatas($object);
1713 unset($object->nom);
1714 unset($object->name_bis);
1715 unset($object->note);
1716 unset($object->departement);
1717 unset($object->departement_code);
1718 unset($object->pays);
1719 unset($object->particulier);
1720 unset($object->prefix_comm);
1722 unset($object->siren);
1723 unset($object->siret);
1724 unset($object->ape);
1726 unset($object->commercial_id);
1728 unset($object->total_ht);
1729 unset($object->total_tva);
1730 unset($object->total_localtax1);
1731 unset($object->total_localtax2);
1732 unset($object->total_ttc);
1734 unset($object->lines);
1735 unset($object->thirdparty);
1737 unset($object->fk_delivery_address);
1752 $thirdparty = array();
1753 foreach (Thirdparties::$FIELDS as $field) {
1754 if (!isset($data[$field])) {
1755 throw new RestException(400,
"$field field missing");
1757 $thirdparty[$field] = $data[$field];
1783 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1787 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1788 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1792 $result = $this->company->initAsSpecimen();
1794 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1797 throw new RestException(404,
'Thirdparty not found');
1801 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1803 if (isModEnabled(
'mailing')) {
1804 $this->company->getNoEmail();
1808 $filterabsolutediscount =
"fk_facture_source IS NULL";
1809 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1811 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1812 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1815 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1816 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1817 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1818 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
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 for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
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.
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
getOutStandingOrder($id, $mode='customer')
Get outstanding orders of 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)
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 thirdparty into another one.
deleteSocieteAccounts($id)
Delete all gateways 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.
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway 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 gateway 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 gateway 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.
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.