23use Luracast\Restler\RestException;
38 public static $FIELDS = array(
55 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
56 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
57 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
58 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
60 $this->company =
new Societe($this->db);
63 static::$FIELDS[] =
'email';
77 public function get($id)
96 return $this->
_fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
113 return $this->
_fetch(
'',
'',
'', $barcode);
134 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'')
138 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
139 throw new RestException(401);
143 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
147 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
148 $search_sale = DolibarrApiAccess::$user->id;
151 $sql =
"SELECT t.rowid";
152 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
153 $sql .=
", sc.fk_soc, sc.fk_user";
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 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
166 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
168 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
169 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
170 $sql .=
" AND t.fk_stcomm = st.id";
173 $sql .=
" AND t.client IN (1, 3)";
174 } elseif ($mode == 2) {
175 $sql .=
" AND t.client IN (2, 3)";
176 } elseif ($mode == 3) {
177 $sql .=
" AND t.client IN (0)";
178 } elseif ($mode == 4) {
179 $sql .=
" AND t.fournisseur IN (1)";
184 if (!empty($mode) && $mode != 4) {
185 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
186 } elseif (!empty($mode) && $mode == 4) {
187 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
189 $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))";
193 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
194 $sql .=
" AND t.rowid = sc.fk_soc";
198 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
200 if ($search_sale > 0) {
201 $sql .=
" AND t.rowid = sc.fk_soc";
204 if ($search_sale > 0) {
205 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
212 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
216 $sql .= $this->db->order($sortfield, $sortorder);
222 $offset = $limit * $page;
224 $sql .= $this->db->plimit($limit + 1, $offset);
227 $result = $this->db->query($sql);
229 $num = $this->db->num_rows($result);
230 $min = min($num, ($limit <= 0 ? $num : $limit));
233 $obj = $this->db->fetch_object($result);
234 $soc_static =
new Societe($this->db);
235 if ($soc_static->fetch($obj->rowid)) {
236 if (isModEnabled(
'mailing')) {
237 $soc_static->getNoEmail();
244 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
246 if (!count($obj_ret)) {
247 throw new RestException(404,
'Thirdparties not found');
258 public function post($request_data =
null)
260 if (!DolibarrApiAccess::$user->rights->societe->creer) {
261 throw new RestException(401);
264 $result = $this->
_validate($request_data);
266 foreach ($request_data as $field => $value) {
267 if ($field ===
'caller') {
269 $this->company->context[
'caller'] = $request_data[
'caller'];
273 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
276 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
277 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
279 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
280 $this->company->setNoEmail($this->company->no_email);
283 return $this->company->id;
296 public function put($id, $request_data =
null)
298 if (!DolibarrApiAccess::$user->rights->societe->creer) {
299 throw new RestException(401);
302 $result = $this->company->fetch($id);
304 throw new RestException(404,
'Thirdparty not found');
308 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
311 foreach ($request_data as $field => $value) {
312 if ($field ==
'id') {
315 if ($field ===
'caller') {
317 $this->company->context[
'caller'] = $request_data[
'caller'];
321 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
324 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
325 $this->company->setNoEmail($this->company->no_email);
328 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1) > 0) {
329 return $this->
get($id);
331 throw new RestException(500, $this->company->error);
349 public function merge($id, $idtodelete)
355 if ($id == $idtodelete) {
356 throw new RestException(400,
'Try to merge a thirdparty into itself');
359 if (!DolibarrApiAccess::$user->rights->societe->creer) {
360 throw new RestException(401);
363 $result = $this->company->fetch($id);
365 throw new RestException(404,
'Thirdparty not found');
369 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
372 $companytoremove =
new Societe($this->db);
373 $result = $companytoremove->fetch($idtodelete);
375 throw new RestException(404,
'Thirdparty not found');
379 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
382 $user = DolibarrApiAccess::$user;
383 $result = $this->company->mergeCompany($companytoremove->id);
385 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
388 return $this->
get($id);
397 public function delete($id)
399 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
400 throw new RestException(401);
402 $result = $this->company->fetch($id);
404 throw new RestException(404,
'Thirdparty not found');
407 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
409 $this->company->oldcopy = clone $this->company;
411 $res = $this->company->delete($id);
413 throw new RestException(500,
"Can't delete, error occurs");
414 } elseif ($res == 0) {
415 throw new RestException(409,
"Can't delete, that product is probably used");
421 'message' =>
'Object deleted'
445 if (!isModEnabled(
'societe')) {
446 throw new RestException(501,
'Module "Thirdparties" needed for this request');
449 if (!isModEnabled(
"product")) {
450 throw new RestException(501,
'Module "Products" needed for this request');
454 throw new RestException(501,
'Multiprices features activation needed for this request');
457 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
458 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
461 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
462 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
465 $result = $this->company->fetch($id);
467 throw new RestException(404,
'Thirdparty '.$id.
' not found');
470 if (empty($result)) {
471 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
475 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
478 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
480 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
498 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
500 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
501 throw new RestException(401);
504 $result = $this->company->fetch($id);
506 throw new RestException(404,
'Thirdparty not found');
511 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
513 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
514 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
517 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
521 return $arrayofcateg;
535 if (!DolibarrApiAccess::$user->rights->societe->creer) {
536 throw new RestException(401);
539 $result = $this->company->fetch($id);
541 throw new RestException(404,
'Thirdparty not found');
544 $result = $category->fetch($category_id);
546 throw new RestException(404,
'category not found');
550 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
553 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
556 $category->add_type($this->company,
'customer');
573 if (!DolibarrApiAccess::$user->rights->societe->creer) {
574 throw new RestException(401);
577 $result = $this->company->fetch($id);
579 throw new RestException(404,
'Thirdparty not found');
582 $result = $category->fetch($category_id);
584 throw new RestException(404,
'category not found');
588 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
591 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
594 $category->del_type($this->company,
'customer');
614 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
615 throw new RestException(401);
618 $result = $this->company->fetch($id);
620 throw new RestException(404,
'Thirdparty not found');
625 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
627 if (is_numeric($result) && $result < 0) {
628 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
631 if (is_numeric($result) && $result == 0) {
650 if (!DolibarrApiAccess::$user->rights->societe->creer) {
651 throw new RestException(401);
654 $result = $this->company->fetch($id);
656 throw new RestException(404,
'Thirdparty not found');
659 $result = $category->fetch($category_id);
661 throw new RestException(404,
'category not found');
665 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
668 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
671 $category->add_type($this->company,
'supplier');
688 if (!DolibarrApiAccess::$user->rights->societe->creer) {
689 throw new RestException(401);
692 $result = $this->company->fetch($id);
694 throw new RestException(404,
'Thirdparty not found');
697 $result = $category->fetch($category_id);
699 throw new RestException(404,
'category not found');
703 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
706 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
709 $category->del_type($this->company,
'supplier');
731 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
732 throw new RestException(401);
736 throw new RestException(400,
'Thirdparty ID is mandatory');
740 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
743 $result = $this->company->fetch($id);
745 throw new RestException(404,
'Thirdparty not found');
748 $result = $this->company->getOutstandingProposals($mode);
750 unset($result[
'total_ht']);
751 unset($result[
'total_ttc']);
773 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
774 throw new RestException(401);
778 throw new RestException(400,
'Thirdparty ID is mandatory');
782 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
785 $result = $this->company->fetch($id);
787 throw new RestException(404,
'Thirdparty not found');
790 $result = $this->company->getOutstandingOrders($mode);
792 unset($result[
'total_ht']);
793 unset($result[
'total_ttc']);
814 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
815 throw new RestException(401);
819 throw new RestException(400,
'Thirdparty ID is mandatory');
823 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
826 $result = $this->company->fetch($id);
828 throw new RestException(404,
'Thirdparty not found');
831 $result = $this->company->getOutstandingBills($mode);
833 unset($result[
'total_ht']);
834 unset($result[
'total_ttc']);
855 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
856 throw new RestException(401);
860 throw new RestException(400,
'Thirdparty ID is mandatory');
864 throw new RestException(401,
'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->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
898 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
899 throw new RestException(401);
903 throw new RestException(400,
'Thirdparty ID is mandatory');
907 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
910 $result = $this->company->fetch($id);
912 throw new RestException(404,
'Thirdparty not found');
916 $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";
917 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
918 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
919 if ($filter ==
"available") {
920 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
922 if ($filter ==
"used") {
923 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
926 $sql .= $this->db->order($sortfield, $sortorder);
928 $result = $this->db->query($sql);
930 throw new RestException(503, $this->db->lasterror());
932 $num = $this->db->num_rows($result);
933 while ($obj = $this->db->fetch_object($result)) {
958 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
959 throw new RestException(401);
962 throw new RestException(400,
'Thirdparty ID is mandatory');
966 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
974 $invoice =
new Facture($this->db);
975 $result = $invoice->list_replacable_invoices($id);
977 throw new RestException(405, $invoice->error);
1001 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1002 throw new RestException(401);
1005 throw new RestException(400,
'Thirdparty ID is mandatory');
1009 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1017 $invoice =
new Facture($this->db);
1018 $result = $invoice->list_qualified_avoir_invoices($id);
1020 throw new RestException(405, $invoice->error);
1037 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1038 throw new RestException(401);
1041 throw new RestException(400,
'Thirdparty ID is mandatory');
1045 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1052 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1053 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1054 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1056 $sql .=
" WHERE fk_soc = ".((int) $id);
1059 $result = $this->db->query($sql);
1061 if ($this->db->num_rows($result) == 0) {
1062 throw new RestException(404,
'Account not found');
1067 $accounts = array();
1070 $num = $this->db->num_rows($result);
1072 $obj = $this->db->fetch_object($result);
1074 if ($account->fetch($obj->rowid)) {
1075 $accounts[] = $account;
1080 throw new RestException(404,
'Account not found');
1084 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1086 $returnAccounts = array();
1088 foreach ($accounts as $account) {
1090 foreach ($account as $key => $value) {
1091 if (in_array($key, $fields)) {
1092 $object[$key] = $value;
1095 $returnAccounts[] = $object;
1098 return $returnAccounts;
1112 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1113 throw new RestException(401);
1115 if ($this->company->fetch($id) <= 0) {
1116 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1120 $account->socid = $id;
1122 foreach ($request_data as $field => $value) {
1123 if ($field ===
'caller') {
1125 $this->company->context[
'caller'] = $request_data[
'caller'];
1129 $account->$field = $value;
1132 if ($account->create(DolibarrApiAccess::$user) < 0) {
1133 throw new RestException(500,
'Error creating Company Bank account');
1136 if (empty($account->rum)) {
1137 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1139 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1140 $account->date_rum =
dol_now();
1143 if ($account->update(DolibarrApiAccess::$user) < 0) {
1144 throw new RestException(500,
'Error updating values');
1163 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1164 throw new RestException(401);
1166 if ($this->company->fetch($id) <= 0) {
1167 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1171 $account->fetch($bankaccount_id, $id, -1,
'');
1173 if ($account->socid != $id) {
1174 throw new RestException(401);
1178 foreach ($request_data as $field => $value) {
1179 if ($field ===
'caller') {
1181 $account->context[
'caller'] = $request_data[
'caller'];
1185 $account->$field = $value;
1188 if (empty($account->rum)) {
1189 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1191 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1192 $account->date_rum =
dol_now();
1195 if ($account->update(DolibarrApiAccess::$user) < 0) {
1196 throw new RestException(500,
'Error updating values');
1214 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1215 throw new RestException(401);
1220 $account->fetch($bankaccount_id);
1222 if (!$account->socid == $id) {
1223 throw new RestException(401);
1226 return $account->delete(DolibarrApiAccess::$user);
1241 global $conf, $langs;
1243 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1245 if ($this->company->fetch($id) <= 0) {
1246 throw new RestException(404,
'Thirdparty not found');
1249 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1250 throw new RestException(401);
1253 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1255 $this->company->fk_bank = $this->company->fk_account;
1256 $this->company->fk_account = $this->company->fk_account;
1258 $outputlangs = $langs;
1263 if (isset($this->company->thirdparty->default_lang)) {
1264 $newlang = $this->company->thirdparty->default_lang;
1265 } elseif (isset($this->company->default_lang)) {
1266 $newlang = $this->company->default_lang;
1269 if (!empty($newlang)) {
1270 $outputlangs =
new Translate(
"", $conf);
1271 $outputlangs->setDefaultLang($newlang);
1274 $sql =
"SELECT rowid";
1275 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1277 $sql .=
" WHERE fk_soc = ".((int) $id);
1279 if ($companybankid) {
1280 $sql .=
" AND rowid = ".((int) $companybankid);
1284 $accounts = array();
1286 $result = $this->db->query($sql);
1288 if ($this->db->num_rows($result) == 0) {
1289 throw new RestException(404,
'Bank account not found');
1292 $num = $this->db->num_rows($result);
1294 $obj = $this->db->fetch_object($result);
1297 if ($account->fetch($obj->rowid)) {
1298 $accounts[] = $account;
1303 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1306 $moreparams = array(
1307 'use_companybankid' => $accounts[0]->
id,
1308 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1311 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1314 return array(
"success" => $result);
1316 throw new RestException(500,
'Error generating the document '.$this->company->error);
1334 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1335 throw new RestException(401);
1339 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1345 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1346 $sql .=
" WHERE fk_soc = ".((int) $id);
1348 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1351 $result = $this->db->query($sql);
1353 if ($result && $this->db->num_rows($result) == 0) {
1354 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1359 $accounts = array();
1361 $num = $this->db->num_rows($result);
1363 $obj = $this->db->fetch_object($result);
1366 if ($account->fetch($obj->rowid)) {
1367 $accounts[] = $account;
1372 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1374 $returnAccounts = array();
1376 foreach ($accounts as $account) {
1378 foreach ($account as $key => $value) {
1379 if (in_array($key, $fields)) {
1380 $object[$key] = $value;
1383 $returnAccounts[] = $object;
1386 return $returnAccounts;
1410 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1411 throw new RestException(401);
1414 if (!isset($request_data[
'site'])) {
1415 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1418 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1419 $result = $this->db->query($sql);
1421 if ($result && $this->db->num_rows($result) == 0) {
1423 if (!isset($request_data[
'login'])) {
1424 $account->login =
"";
1426 $account->fk_soc = $id;
1428 foreach ($request_data as $field => $value) {
1429 if ($field ===
'caller') {
1431 $account->context[
'caller'] = $request_data[
'caller'];
1435 $account->$field = $value;
1438 if ($account->create(DolibarrApiAccess::$user) < 0) {
1439 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1446 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1474 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1475 throw new RestException(401);
1478 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1479 $result = $this->db->query($sql);
1482 if ($result && $this->db->num_rows == 0) {
1483 if (!isset($request_data[
'key_account'])) {
1484 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1487 if (!isset($request_data[
'login'])) {
1488 $account->login =
"";
1491 foreach ($request_data as $field => $value) {
1492 if ($field ===
'caller') {
1494 $account->context[
'caller'] = $request_data[
'caller'];
1498 $account->$field = $value;
1501 $account->fk_soc = $id;
1502 $account->site = $site;
1504 if ($account->create(DolibarrApiAccess::$user) < 0) {
1505 throw new RestException(500,
'Error creating SocieteAccount entity.');
1509 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1510 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1511 $result = $this->db->query($sql);
1513 if ($result && $this->db->num_rows($result) !== 0) {
1514 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.");
1518 $obj = $this->db->fetch_object($result);
1521 $account->id = $obj->rowid;
1522 $account->fk_soc = $id;
1523 $account->site = $site;
1524 if (!isset($request_data[
'login'])) {
1525 $account->login =
"";
1527 $account->fk_user_creat = $obj->fk_user_creat;
1528 $account->date_creation = $obj->date_creation;
1530 foreach ($request_data as $field => $value) {
1531 if ($field ===
'caller') {
1533 $account->context[
'caller'] = $request_data[
'caller'];
1537 $account->$field = $value;
1540 if ($account->update(DolibarrApiAccess::$user) < 0) {
1541 throw new RestException(500,
'Error updating SocieteAccount entity.');
1568 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1569 throw new RestException(401);
1572 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1573 $result = $this->db->query($sql);
1575 if ($result && $this->db->num_rows($result) == 0) {
1576 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1579 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1580 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1581 $result = $this->db->query($sql);
1583 if ($result && $this->db->num_rows($result) !== 0) {
1584 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.");
1588 $obj = $this->db->fetch_object($result);
1590 $account->fetch($obj->rowid);
1592 foreach ($request_data as $field => $value) {
1593 if ($field ===
'caller') {
1595 $account->context[
'caller'] = $request_data[
'caller'];
1599 $account->$field = $value;
1602 if ($account->update(DolibarrApiAccess::$user) < 0) {
1603 throw new RestException(500,
'Error updating SocieteAccount account');
1627 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1628 throw new RestException(401);
1631 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1632 $result = $this->db->query($sql);
1634 if ($result && $this->db->num_rows($result) == 0) {
1635 throw new RestException(404);
1637 $obj = $this->db->fetch_object($result);
1639 $account->fetch($obj->rowid);
1641 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1642 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1661 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1662 throw new RestException(401);
1669 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1670 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1672 $result = $this->db->query($sql);
1674 if ($result && $this->db->num_rows($result) == 0) {
1675 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1679 $num = $this->db->num_rows($result);
1681 $obj = $this->db->fetch_object($result);
1683 $account->fetch($obj->rowid);
1685 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1686 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1703 $object = parent::_cleanObjectDatas($object);
1705 unset($object->nom);
1706 unset($object->name_bis);
1707 unset($object->note);
1708 unset($object->departement);
1709 unset($object->departement_code);
1710 unset($object->pays);
1711 unset($object->particulier);
1712 unset($object->prefix_comm);
1714 unset($object->siren);
1715 unset($object->siret);
1716 unset($object->ape);
1718 unset($object->commercial_id);
1720 unset($object->total_ht);
1721 unset($object->total_tva);
1722 unset($object->total_localtax1);
1723 unset($object->total_localtax2);
1724 unset($object->total_ttc);
1726 unset($object->lines);
1727 unset($object->thirdparty);
1729 unset($object->fk_delivery_address);
1744 $thirdparty = array();
1745 foreach (Thirdparties::$FIELDS as $field) {
1746 if (!isset($data[$field])) {
1747 throw new RestException(400,
"$field field missing");
1749 $thirdparty[$field] = $data[$field];
1775 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1779 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1780 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1784 $result = $this->company->initAsSpecimen();
1786 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1789 throw new RestException(404,
'Thirdparty not found');
1793 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1795 if (isModEnabled(
'mailing')) {
1796 $this->company->getNoEmail();
1800 $filterabsolutediscount =
"fk_facture_source IS NULL";
1801 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1803 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1804 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1807 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1808 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1809 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1810 $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.