24use Luracast\Restler\RestException;
39 public static $FIELDS = array(
56 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
57 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
58 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
59 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
62 $this->company =
new Societe($this->db);
65 static::$FIELDS[] =
'email';
79 public function get($id)
98 return $this->
_fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
115 return $this->
_fetch(
'',
'',
'', $barcode);
136 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'')
140 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
141 throw new RestException(403);
145 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
149 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
150 $search_sale = DolibarrApiAccess::$user->id;
153 $sql =
"SELECT t.rowid";
154 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
155 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
158 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
160 if (!in_array($mode, array(1, 2, 3))) {
161 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
164 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
165 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
166 $sql .=
" AND t.fk_stcomm = st.id";
168 $sql .=
" AND t.client IN (1, 3)";
169 } elseif ($mode == 2) {
170 $sql .=
" AND t.client IN (2, 3)";
171 } elseif ($mode == 3) {
172 $sql .=
" AND t.client IN (0)";
173 } elseif ($mode == 4) {
174 $sql .=
" AND t.fournisseur IN (1)";
178 if (!empty($mode) && $mode != 4) {
179 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
180 } elseif (!empty($mode) && $mode == 4) {
181 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
183 $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))";
187 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
190 if ($search_sale && $search_sale !=
'-1') {
191 if ($search_sale == -2) {
192 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
193 } elseif ($search_sale > 0) {
194 $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).
")";
202 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
206 $sql .= $this->db->order($sortfield, $sortorder);
212 $offset = $limit * $page;
214 $sql .= $this->db->plimit($limit + 1, $offset);
217 $result = $this->db->query($sql);
219 $num = $this->db->num_rows($result);
220 $min = min($num, ($limit <= 0 ? $num : $limit));
223 $obj = $this->db->fetch_object($result);
224 $soc_static =
new Societe($this->db);
225 if ($soc_static->fetch($obj->rowid)) {
226 if (isModEnabled(
'mailing')) {
227 $soc_static->getNoEmail();
234 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
236 if (!count($obj_ret)) {
237 throw new RestException(404,
'Thirdparties not found');
248 public function post($request_data =
null)
250 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
251 throw new RestException(403);
254 $result = $this->
_validate($request_data);
256 foreach ($request_data as $field => $value) {
257 if ($field ===
'caller') {
259 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
263 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
266 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
267 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
269 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
270 $this->company->setNoEmail($this->company->no_email);
273 return $this->company->id;
287 public function put($id, $request_data =
null)
289 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
290 throw new RestException(403);
293 $result = $this->company->fetch($id);
295 throw new RestException(404,
'Thirdparty not found');
299 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
302 foreach ($request_data as $field => $value) {
303 if ($field ==
'id') {
306 if ($field ===
'caller') {
308 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
311 if ($field ==
'array_options' && is_array($value)) {
312 foreach ($value as $index => $val) {
313 $this->company->array_options[$index] = $val;
317 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
320 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
321 $this->company->setNoEmail($this->company->no_email);
324 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1) > 0) {
325 return $this->
get($id);
327 throw new RestException(500, $this->company->error);
345 public function merge($id, $idtodelete)
347 if ($id == $idtodelete) {
348 throw new RestException(400,
'Try to merge a thirdparty into itself');
351 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
352 throw new RestException(403);
355 $result = $this->company->fetch($id);
357 throw new RestException(404,
'Thirdparty not found');
361 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
364 $companytoremove =
new Societe($this->db);
365 $result = $companytoremove->fetch($idtodelete);
367 throw new RestException(404,
'Thirdparty not found');
371 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
374 $user = DolibarrApiAccess::$user;
375 $result = $this->company->mergeCompany($companytoremove->id);
377 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
380 return $this->
get($id);
389 public function delete($id)
391 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
392 throw new RestException(403);
394 $result = $this->company->fetch($id);
396 throw new RestException(404,
'Thirdparty not found');
399 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
401 $this->company->oldcopy = clone $this->company;
403 $res = $this->company->delete($id);
405 throw new RestException(500,
"Can't delete, error occurs");
406 } elseif ($res == 0) {
407 throw new RestException(409,
"Can't delete, that product is probably used");
413 'message' =>
'Object deleted'
437 if (!isModEnabled(
'societe')) {
438 throw new RestException(501,
'Module "Thirdparties" needed for this request');
441 if (!isModEnabled(
"product")) {
442 throw new RestException(501,
'Module "Products" needed for this request');
446 throw new RestException(501,
'Multiprices features activation needed for this request');
449 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
450 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
453 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
454 throw new RestException(403,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
457 $result = $this->company->fetch($id);
459 throw new RestException(404,
'Thirdparty '.$id.
' not found');
462 if (empty($result)) {
463 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
467 throw new RestException(403,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
470 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
472 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
492 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
493 throw new RestException(403);
495 $result = $this->company->fetch($id);
497 throw new RestException(404,
'Thirdparty not found');
499 $usertmp =
new User($this->db);
500 $result = $usertmp->fetch($representative_id);
502 throw new RestException(404,
'User not found');
505 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
507 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
526 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
527 throw new RestException(403);
529 $result = $this->company->fetch($id);
531 throw new RestException(404,
'Thirdparty not found');
533 $usertmp =
new User($this->db);
534 $result = $usertmp->fetch($representative_id);
536 throw new RestException(404,
'User not found');
539 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
541 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
558 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
560 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
561 throw new RestException(403);
564 $result = $this->company->fetch($id);
566 throw new RestException(404,
'Thirdparty not found');
571 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
573 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
574 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
577 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
581 return $arrayofcateg;
595 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
596 throw new RestException(403);
599 $result = $this->company->fetch($id);
601 throw new RestException(404,
'Thirdparty not found');
604 $result = $category->fetch($category_id);
606 throw new RestException(404,
'category not found');
610 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
613 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
616 $category->add_type($this->company,
'customer');
633 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
634 throw new RestException(403);
637 $result = $this->company->fetch($id);
639 throw new RestException(404,
'Thirdparty not found');
642 $result = $category->fetch($category_id);
644 throw new RestException(404,
'category not found');
648 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
651 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
654 $category->del_type($this->company,
'customer');
674 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
675 throw new RestException(403);
678 $result = $this->company->fetch($id);
680 throw new RestException(404,
'Thirdparty not found');
685 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
687 if (is_numeric($result) && $result < 0) {
688 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
691 if (is_numeric($result) && $result == 0) {
710 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
711 throw new RestException(403);
714 $result = $this->company->fetch($id);
716 throw new RestException(404,
'Thirdparty not found');
719 $result = $category->fetch($category_id);
721 throw new RestException(404,
'category not found');
725 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
728 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
731 $category->add_type($this->company,
'supplier');
748 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
749 throw new RestException(403);
752 $result = $this->company->fetch($id);
754 throw new RestException(404,
'Thirdparty not found');
757 $result = $category->fetch($category_id);
759 throw new RestException(404,
'category not found');
763 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
766 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 $category->del_type($this->company,
'supplier');
791 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
792 throw new RestException(403);
796 throw new RestException(400,
'Thirdparty ID is mandatory');
800 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
803 $result = $this->company->fetch($id);
805 throw new RestException(404,
'Thirdparty not found');
808 $result = $this->company->getOutstandingProposals($mode);
810 unset($result[
'total_ht']);
811 unset($result[
'total_ttc']);
833 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
834 throw new RestException(403);
838 throw new RestException(400,
'Thirdparty ID is mandatory');
842 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 $result = $this->company->fetch($id);
847 throw new RestException(404,
'Thirdparty not found');
850 $result = $this->company->getOutstandingOrders($mode);
852 unset($result[
'total_ht']);
853 unset($result[
'total_ttc']);
874 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
875 throw new RestException(403);
879 throw new RestException(400,
'Thirdparty ID is mandatory');
883 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
886 $result = $this->company->fetch($id);
888 throw new RestException(404,
'Thirdparty not found');
891 $result = $this->company->getOutstandingBills($mode);
893 unset($result[
'total_ht']);
894 unset($result[
'total_ttc']);
915 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
916 throw new RestException(403);
920 throw new RestException(400,
'Thirdparty ID is mandatory');
924 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
927 $result = $this->company->fetch($id);
929 throw new RestException(404,
'Thirdparty not found');
932 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
958 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
959 throw new RestException(403);
963 throw new RestException(400,
'Thirdparty ID is mandatory');
967 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
970 $result = $this->company->fetch($id);
972 throw new RestException(404,
'Thirdparty not found');
976 $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";
977 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
978 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
979 if ($filter ==
"available") {
980 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
982 if ($filter ==
"used") {
983 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
986 $sql .= $this->db->order($sortfield, $sortorder);
988 $result = $this->db->query($sql);
990 throw new RestException(503, $this->db->lasterror());
992 $num = $this->db->num_rows($result);
993 while ($obj = $this->db->fetch_object($result)) {
1018 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1019 throw new RestException(403);
1022 throw new RestException(400,
'Thirdparty ID is mandatory');
1026 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1034 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1035 $invoice =
new Facture($this->db);
1036 $result = $invoice->list_replacable_invoices($id);
1038 throw new RestException(405, $invoice->error);
1062 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1063 throw new RestException(403);
1066 throw new RestException(400,
'Thirdparty ID is mandatory');
1070 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1078 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1079 $invoice =
new Facture($this->db);
1080 $result = $invoice->list_qualified_avoir_invoices($id);
1082 throw new RestException(405, $invoice->error);
1100 throw new RestException(400,
'Thirdparty ID is mandatory');
1102 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1103 throw new RestException(403);
1106 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1113 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1114 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1116 $sql .=
" WHERE fk_soc = ".((int) $id);
1119 $result = $this->db->query($sql);
1120 if ($this->db->num_rows($result) == 0) {
1121 throw new RestException(404,
'Notification not found');
1126 $notifications = array();
1129 $num = $this->db->num_rows($result);
1131 $obj = $this->db->fetch_object($result);
1132 $notifications[] = $obj;
1136 throw new RestException(404,
'No notifications found');
1139 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1141 $returnNotifications = array();
1143 foreach ($notifications as $notification) {
1145 foreach ($notification as $key => $value) {
1146 if (in_array($key, $fields)) {
1150 $returnNotifications[] =
$object;
1153 return $returnNotifications;
1167 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1168 throw new RestException(403,
"User has no right to update thirdparties");
1170 if ($this->company->fetch($id) <= 0) {
1171 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1173 $notification =
new Notify($this->db);
1175 $notification->socid = $id;
1177 foreach ($request_data as $field => $value) {
1178 $notification->$field = $value;
1181 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1182 throw new RestException(500,
'Error creating Thirdparty Notification');
1185 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1186 throw new RestException(500,
'Error updating values');
1204 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1205 throw new RestException(403);
1208 $notification =
new Notify($this->db);
1210 $notification->fetch($notification_id);
1212 $socid = (int) $notification->socid;
1214 if ($socid == $id) {
1215 return $notification->delete(DolibarrApiAccess::$user);
1217 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1234 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1235 throw new RestException(403,
"User has no right to update thirdparties");
1237 if ($this->company->fetch($id) <= 0) {
1238 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1240 $notification =
new Notify($this->db);
1243 $notification->fetch($notification_id, $id);
1245 if ($notification->socid != $id) {
1246 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1249 foreach ($request_data as $field => $value) {
1250 $notification->$field = $value;
1253 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1254 throw new RestException(500,
'Error updating values');
1271 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1272 throw new RestException(403);
1275 throw new RestException(400,
'Thirdparty ID is mandatory');
1279 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1286 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1287 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1288 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1290 $sql .=
" WHERE fk_soc = ".((int) $id);
1293 $result = $this->db->query($sql);
1295 if ($this->db->num_rows($result) == 0) {
1296 throw new RestException(404,
'Account not found');
1301 $accounts = array();
1304 $num = $this->db->num_rows($result);
1306 $obj = $this->db->fetch_object($result);
1308 if ($account->fetch($obj->rowid)) {
1309 $accounts[] = $account;
1314 throw new RestException(404,
'Account not found');
1318 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1320 $returnAccounts = array();
1322 foreach ($accounts as $account) {
1324 foreach ($account as $key => $value) {
1325 if (in_array($key, $fields)) {
1332 return $returnAccounts;
1346 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1347 throw new RestException(403);
1349 if ($this->company->fetch($id) <= 0) {
1350 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1354 $account->socid = $id;
1356 foreach ($request_data as $field => $value) {
1357 if ($field ===
'caller') {
1359 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1363 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1366 if ($account->create(DolibarrApiAccess::$user) < 0) {
1367 throw new RestException(500,
'Error creating Company Bank account');
1370 if (empty($account->rum)) {
1371 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1373 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1374 $account->date_rum =
dol_now();
1377 if ($account->update(DolibarrApiAccess::$user) < 0) {
1378 throw new RestException(500,
'Error updating values');
1397 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1398 throw new RestException(403);
1400 if ($this->company->fetch($id) <= 0) {
1401 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1406 $account->fetch($bankaccount_id,
'', $id, -1,
'');
1408 if ($account->socid != $id) {
1409 throw new RestException(403);
1413 foreach ($request_data as $field => $value) {
1414 if ($field ===
'caller') {
1416 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1423 if (empty($account->rum)) {
1424 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1426 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1427 $account->date_rum =
dol_now();
1430 if ($account->update(DolibarrApiAccess::$user) < 0) {
1431 throw new RestException(500,
'Error updating values');
1449 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1450 throw new RestException(403);
1455 $account->fetch($bankaccount_id);
1457 $socid = (int) $account->socid;
1459 if ($socid == $id) {
1460 return $account->delete(DolibarrApiAccess::$user);
1462 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1478 global $conf, $langs;
1480 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1482 if ($this->company->fetch($id) <= 0) {
1483 throw new RestException(404,
'Thirdparty not found');
1486 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1487 throw new RestException(403);
1490 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1492 $this->company->fk_bank = $this->company->fk_account;
1495 $outputlangs = $langs;
1500 if (isset($this->company->thirdparty->default_lang)) {
1501 $newlang = $this->company->thirdparty->default_lang;
1502 } elseif (isset($this->company->default_lang)) {
1503 $newlang = $this->company->default_lang;
1506 if (!empty($newlang)) {
1507 $outputlangs =
new Translate(
"", $conf);
1508 $outputlangs->setDefaultLang($newlang);
1511 $sql =
"SELECT rowid";
1512 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1514 $sql .=
" WHERE fk_soc = ".((int) $id);
1516 if ($companybankid) {
1517 $sql .=
" AND rowid = ".((int) $companybankid);
1521 $accounts = array();
1523 $result = $this->db->query($sql);
1525 if ($this->db->num_rows($result) == 0) {
1526 throw new RestException(404,
'Bank account not found');
1529 $num = $this->db->num_rows($result);
1531 $obj = $this->db->fetch_object($result);
1534 if ($account->fetch($obj->rowid)) {
1535 $accounts[] = $account;
1540 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1543 $moreparams = array(
1544 'use_companybankid' => $accounts[0]->
id,
1545 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1548 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1551 return array(
"success" => $result);
1553 throw new RestException(500,
'Error generating the document '.$this->company->error);
1571 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1572 throw new RestException(403);
1576 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1582 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1583 $sql .=
" WHERE fk_soc = ".((int) $id);
1585 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1588 $result = $this->db->query($sql);
1590 if ($result && $this->db->num_rows($result) == 0) {
1591 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
1596 $accounts = array();
1598 $num = $this->db->num_rows($result);
1600 $obj = $this->db->fetch_object($result);
1603 if ($account->fetch($obj->rowid)) {
1604 $accounts[] = $account;
1609 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1611 $returnAccounts = array();
1613 foreach ($accounts as $account) {
1615 foreach ($account as $key => $value) {
1616 if (in_array($key, $fields)) {
1623 return $returnAccounts;
1647 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1648 throw new RestException(403);
1651 if (!isset($request_data[
'site'])) {
1652 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1655 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1656 $result = $this->db->query($sql);
1658 if ($result && $this->db->num_rows($result) == 0) {
1660 if (!isset($request_data[
'login'])) {
1661 $account->login =
"";
1663 $account->fk_soc = $id;
1665 foreach ($request_data as $field => $value) {
1666 if ($field ===
'caller') {
1668 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1675 if ($account->create(DolibarrApiAccess::$user) < 0) {
1676 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1683 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1711 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1712 throw new RestException(403);
1715 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1716 $result = $this->db->query($sql);
1719 if ($result && $this->db->num_rows == 0) {
1720 if (!isset($request_data[
'key_account'])) {
1721 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1724 if (!isset($request_data[
'login'])) {
1725 $account->login =
"";
1728 foreach ($request_data as $field => $value) {
1729 if ($field ===
'caller') {
1731 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1738 $account->fk_soc = $id;
1739 $account->site = $site;
1741 if ($account->create(DolibarrApiAccess::$user) < 0) {
1742 throw new RestException(500,
'Error creating SocieteAccount entity.');
1746 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1747 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1748 $result = $this->db->query($sql);
1750 if ($result && $this->db->num_rows($result) !== 0) {
1751 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.");
1755 $obj = $this->db->fetch_object($result);
1758 $account->id = $obj->rowid;
1759 $account->fk_soc = $id;
1760 $account->site = $site;
1761 if (!isset($request_data[
'login'])) {
1762 $account->login =
"";
1764 $account->fk_user_creat = $obj->fk_user_creat;
1765 $account->date_creation = $obj->date_creation;
1767 foreach ($request_data as $field => $value) {
1768 if ($field ===
'caller') {
1770 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1777 if ($account->update(DolibarrApiAccess::$user) < 0) {
1778 throw new RestException(500,
'Error updating SocieteAccount entity.');
1805 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1806 throw new RestException(403);
1809 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1810 $result = $this->db->query($sql);
1812 if ($result && $this->db->num_rows($result) == 0) {
1813 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
1816 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1817 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1818 $result = $this->db->query($sql);
1820 if ($result && $this->db->num_rows($result) !== 0) {
1821 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.");
1825 $obj = $this->db->fetch_object($result);
1827 $account->fetch($obj->rowid);
1829 foreach ($request_data as $field => $value) {
1830 if ($field ===
'caller') {
1832 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1839 if ($account->update(DolibarrApiAccess::$user) < 0) {
1840 throw new RestException(500,
'Error updating SocieteAccount account');
1864 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1865 throw new RestException(403);
1868 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1869 $result = $this->db->query($sql);
1871 if ($result && $this->db->num_rows($result) == 0) {
1872 throw new RestException(404);
1874 $obj = $this->db->fetch_object($result);
1876 $account->fetch($obj->rowid);
1878 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1879 throw new RestException(500,
"Error while deleting $site account attached to this third party");
1898 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1899 throw new RestException(403);
1906 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1907 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1909 $result = $this->db->query($sql);
1911 if ($result && $this->db->num_rows($result) == 0) {
1912 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
1916 $num = $this->db->num_rows($result);
1918 $obj = $this->db->fetch_object($result);
1920 $account->fetch($obj->rowid);
1922 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1923 throw new RestException(500,
'Error while deleting account attached to this third party');
1946 unset(
$object->departement_code);
1955 unset(
$object->commercial_id);
1959 unset(
$object->total_localtax1);
1960 unset(
$object->total_localtax2);
1966 unset(
$object->fk_delivery_address);
1981 $thirdparty = array();
1982 foreach (Thirdparties::$FIELDS as $field) {
1983 if (!isset($data[$field])) {
1984 throw new RestException(400,
"$field field missing");
1986 $thirdparty[$field] = $data[$field];
2012 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2016 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2017 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2021 $result = $this->company->initAsSpecimen();
2023 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2026 throw new RestException(404,
'Thirdparty not found');
2030 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2032 if (isModEnabled(
'mailing')) {
2033 $this->company->getNoEmail();
2037 $filterabsolutediscount =
"fk_facture_source IS NULL";
2038 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2040 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2041 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2044 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
2045 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
2046 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2047 $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.