dolibarr 22.0.5
api_thirdparties.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2018 Pierre Chéné <pierre.chene44@gmail.com>
4 * Copyright (C) 2019 Cedric Ancelin <icedo.anc@gmail.com>
5 * Copyright (C) 2020-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
7 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
9 * Copyright (C) 2025 William Mead <william@m34d.com>
10 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
11 * Copyright (C) 2026 Benjamin Falière <benjamin@faliere.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
27use Luracast\Restler\RestException;
28
38{
42 public static $FIELDS = array(
43 'name'
44 );
45
49 public $company;
50
54 public function __construct()
55 {
56 global $db;
57 $this->db = $db;
58
59 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
60 require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
61 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
62 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
63 require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
64
65 $this->company = new Societe($this->db);
66
67 if (getDolGlobalString('SOCIETE_EMAIL_MANDATORY')) {
68 static::$FIELDS[] = 'email';
69 }
70 }
71
84 public function get($id)
85 {
86 return $this->_fetch($id);
87 }
88
105 public function getByEmail($email)
106 {
107 return $this->_fetch(null, '', '', '', '', '', '', '', '', '', $email);
108 }
109
124 public function getByBarcode($barcode)
125 {
126 return $this->_fetch(null, '', '', $barcode);
127 }
128
151 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
152 {
153 $obj_ret = array();
154
155 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
156 throw new RestException(403);
157 }
158
159 // case of external user, we force socids
160 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid : '';
161
162 // If the internal user must only see his customers, force searching by him
163 $search_sale = 0;
164 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
165 $search_sale = DolibarrApiAccess::$user->id;
166 }
167
168 $sql = "SELECT t.rowid";
169 $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
170 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields AS ef ON ef.fk_object = t.rowid"; // So we will be able to filter on extrafields
171 if ($category > 0) {
172 if ($mode != 4) {
173 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
174 }
175 if (!in_array($mode, array(1, 2, 3))) {
176 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
177 }
178 }
179 $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
180 $sql .= " WHERE t.entity IN (".getEntity('societe').")";
181 $sql .= " AND t.fk_stcomm = st.id";
182 if ($mode == 1) {
183 $sql .= " AND t.client IN (1, 3)";
184 } elseif ($mode == 2) {
185 $sql .= " AND t.client IN (2, 3)";
186 } elseif ($mode == 3) {
187 $sql .= " AND t.client IN (0)";
188 } elseif ($mode == 4) {
189 $sql .= " AND t.fournisseur IN (1)";
190 }
191 // Select thirdparties of given category
192 if ($category > 0) {
193 if (!empty($mode) && $mode != 4) {
194 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
195 } elseif (!empty($mode) && $mode == 4) {
196 $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
197 } else {
198 $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))";
199 }
200 }
201 if ($socids) {
202 $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
203 }
204 // Search on sale representative
205 if ($search_sale && $search_sale != '-1') {
206 if ($search_sale == -2) {
207 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
208 } elseif ($search_sale > 0) {
209 $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).")";
210 }
211 }
212 // Add sql filters
213 if ($sqlfilters) {
214 $errormessage = '';
215 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
216 if ($errormessage) {
217 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
218 }
219 }
220
221 //this query will return total thirdparties with the filters given
222 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
223
224 $sql .= $this->db->order($sortfield, $sortorder);
225 if ($limit) {
226 if ($page < 0) {
227 $page = 0;
228 }
229 $offset = $limit * $page;
230
231 $sql .= $this->db->plimit($limit + 1, $offset);
232 }
233
234 $result = $this->db->query($sql);
235 if ($result) {
236 $num = $this->db->num_rows($result);
237 $min = min($num, ($limit <= 0 ? $num : $limit));
238 $i = 0;
239 while ($i < $min) {
240 $obj = $this->db->fetch_object($result);
241 $soc_static = new Societe($this->db);
242 if ($soc_static->fetch($obj->rowid)) {
243 if (isModEnabled('mailing')) {
244 $soc_static->getNoEmail();
245 }
246 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($soc_static), $properties);
247 }
248 $i++;
249 }
250 } else {
251 throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
252 }
253 if (!count($obj_ret)) {
254 $message = '';
255 switch ($mode) {
256 case 0:
257 $message = 'No third parties found';
258 break;
259 case 1:
260 $message = 'No customers found';
261 break;
262 case 2:
263 $message = 'No prospects found';
264 break;
265 case 3:
266 $message = 'No other third parties found';
267 break;
268 case 4:
269 $message = 'No suppliers found';
270 }
271 throw new RestException(404, $message);
272 }
273
274 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
275 if ($pagination_data) {
276 $totalsResult = $this->db->query($sqlTotals);
277 $total = $this->db->fetch_object($totalsResult)->total;
278
279 $tmp = $obj_ret;
280 $obj_ret = [];
281
282 $obj_ret['data'] = $tmp;
283 $obj_ret['pagination'] = [
284 'total' => (int) $total,
285 'page' => $page, //count starts from 0
286 'page_count' => ceil((int) $total / $limit),
287 'limit' => $limit
288 ];
289 }
290
291 return $obj_ret;
292 }
293
306 public function post($request_data = null)
307 {
308 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
309 throw new RestException(403);
310 }
311 // Check mandatory fields
312 $result = $this->_validate($request_data);
313
314 foreach ($request_data as $field => $value) {
315 if ($field === 'caller') {
316 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
317 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
318 continue;
319 }
320
321 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
322 }
323
324 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
325 throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
326 }
327 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
328 $this->company->setNoEmail($this->company->no_email);
329 }
330
331 return $this->company->id;
332 }
333
351 public function put($id, $request_data = null)
352 {
353 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
354 throw new RestException(403);
355 }
356
357 $result = $this->company->fetch($id);
358 if (!$result) {
359 throw new RestException(404, 'Thirdparty not found');
360 }
361
362 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
363 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
364 }
365
366 foreach ($request_data as $field => $value) {
367 if ($field == 'id') {
368 continue;
369 }
370 if ($field === 'caller') {
371 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
372 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
373 continue;
374 }
375 if ($field == 'array_options' && is_array($value)) {
376 foreach ($value as $index => $val) {
377 $this->company->array_options[$index] = $this->_checkValForAPI($field, $val, $this->company);
378 }
379 continue;
380 }
381 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
382 }
383
384 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
385 $this->company->setNoEmail($this->company->no_email);
386 }
387
388 if ($this->company->update($id, DolibarrApiAccess::$user, 1, 1, 1, 'update', 1) > 0) {
389 return $this->get($id);
390 } else {
391 throw new RestException(500, $this->company->error);
392 }
393 }
394
416 public function merge($id, $idtodelete)
417 {
418 if ($id == $idtodelete) {
419 throw new RestException(400, 'Try to merge a thirdparty into itself');
420 }
421
422 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
423 throw new RestException(403);
424 }
425
426 $result = $this->company->fetch($id); // include the fetch of extra fields
427 if (!$result) {
428 throw new RestException(404, 'Thirdparty not found');
429 }
430
431 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
432 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
433 }
434
435 $companytoremove = new Societe($this->db);
436 $result = $companytoremove->fetch($idtodelete); // include the fetch of extra fields
437 if (!$result) {
438 throw new RestException(404, 'Thirdparty not found');
439 }
440
441 if (!DolibarrApi::_checkAccessToResource('societe', $companytoremove->id)) {
442 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
443 }
444
445 $user = DolibarrApiAccess::$user;
446 $result = $this->company->mergeCompany($companytoremove->id);
447 if ($result < 0) {
448 throw new RestException(500, 'Error failed to merged thirdparty '.$companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
449 }
450
451 return $this->get($id);
452 }
453
466 public function delete($id)
467 {
468 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
469 throw new RestException(403);
470 }
471 $result = $this->company->fetch($id);
472 if (!$result) {
473 throw new RestException(404, 'Thirdparty not found');
474 }
475 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
476 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
477 }
478 $this->company->oldcopy = clone $this->company; // @phan-suppress-current-line PhanTypeMismatchProperty
479
480 $res = $this->company->delete($id);
481 if ($res < 0) {
482 throw new RestException(500, "Can't delete, error occurs");
483 } elseif ($res == 0) {
484 throw new RestException(409, "Can't delete, that product is probably used");
485 }
486
487 return array(
488 'success' => array(
489 'code' => 200,
490 'message' => 'Object deleted'
491 )
492 );
493 }
494
512 public function setThirdpartyPriceLevel($id, $priceLevel)
513 {
514 global $conf;
515
516 if (!isModEnabled('societe')) {
517 throw new RestException(501, 'Module "Thirdparties" needed for this request');
518 }
519
520 if (!isModEnabled("product")) {
521 throw new RestException(501, 'Module "Products" needed for this request');
522 }
523
524 if (!getDolGlobalString('PRODUIT_MULTIPRICES')) {
525 throw new RestException(501, 'Multiprices features activation needed for this request');
526 }
527
528 if ($priceLevel < 1 || $priceLevel > getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT')) {
529 throw new RestException(400, 'Price level must be between 1 and ' . getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT'));
530 }
531
532 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
533 throw new RestException(403, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
534 }
535
536 $result = $this->company->fetch($id);
537 if ($result < 0) {
538 throw new RestException(404, 'Thirdparty '.$id.' not found');
539 }
540
541 if (empty($result)) {
542 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
543 }
544
545 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
546 throw new RestException(403, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
547 }
548
549 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
550 if ($result <= 0) {
551 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
552 }
553
554 return $this->_cleanObjectDatas($this->company);
555 }
556
571 public function addRepresentative($id, $representative_id)
572 {
573 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
574 throw new RestException(403);
575 }
576 $result = $this->company->fetch($id);
577 if (!$result) {
578 throw new RestException(404, 'Thirdparty not found');
579 }
580 $usertmp = new User($this->db);
581 $result = $usertmp->fetch($representative_id);
582 if (!$result) {
583 throw new RestException(404, 'User not found');
584 }
585 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
586 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
587 }
588 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
589
590 return $result;
591 }
592
607 public function deleteRepresentative($id, $representative_id)
608 {
609 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
610 throw new RestException(403);
611 }
612 $result = $this->company->fetch($id);
613 if (!$result) {
614 throw new RestException(404, 'Thirdparty not found');
615 }
616 $usertmp = new User($this->db);
617 $result = $usertmp->fetch($representative_id);
618 if (!$result) {
619 throw new RestException(404, 'User not found');
620 }
621 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
622 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
623 }
624 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
625
626 return $result;
627 }
628
647 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
648 {
649 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
650 throw new RestException(403);
651 }
652
653 $result = $this->company->fetch($id);
654 if (!$result) {
655 throw new RestException(404, 'Thirdparty not found');
656 }
657
658 $categories = new Categorie($this->db);
659
660 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
661
662 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
663 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
664 }
665
666 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
667 return array();
668 }
669
670 return $arrayofcateg;
671 }
672
689 public function addCategory($id, $category_id)
690 {
691 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
692 throw new RestException(403);
693 }
694
695 $result = $this->company->fetch($id);
696 if (!$result) {
697 throw new RestException(404, 'Thirdparty not found');
698 }
699 $category = new Categorie($this->db);
700 $result = $category->fetch($category_id);
701 if (!$result) {
702 throw new RestException(404, 'category not found');
703 }
704
705 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
706 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
707 }
708 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
709 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
710 }
711
712 $category->add_type($this->company, 'customer');
713
714 return $this->_cleanObjectDatas($this->company);
715 }
716
733 public function deleteCategory($id, $category_id)
734 {
735 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
736 throw new RestException(403);
737 }
738
739 $result = $this->company->fetch($id);
740 if (!$result) {
741 throw new RestException(404, 'Thirdparty not found');
742 }
743 $category = new Categorie($this->db);
744 $result = $category->fetch($category_id);
745 if (!$result) {
746 throw new RestException(404, 'category not found');
747 }
748
749 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
750 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
751 }
752 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
753 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
754 }
755
756 $category->del_type($this->company, 'customer');
757
758 return $this->_cleanObjectDatas($this->company);
759 }
760
780 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
781 {
782 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
783 throw new RestException(403);
784 }
785
786 $result = $this->company->fetch($id);
787 if (!$result) {
788 throw new RestException(404, 'Thirdparty not found');
789 }
790
791 $categories = new Categorie($this->db);
792
793 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
794
795 if (is_numeric($result) && $result < 0) {
796 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
797 }
798
799 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
800 return array();
801 }
802
803 return $result;
804 }
805
822 public function addSupplierCategory($id, $category_id)
823 {
824 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
825 throw new RestException(403);
826 }
827
828 $result = $this->company->fetch($id);
829 if (!$result) {
830 throw new RestException(404, 'Thirdparty not found');
831 }
832 $category = new Categorie($this->db);
833 $result = $category->fetch($category_id);
834 if (!$result) {
835 throw new RestException(404, 'category not found');
836 }
837
838 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
839 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
840 }
841 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
842 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 }
844
845 $category->add_type($this->company, 'supplier');
846
847 return $this->_cleanObjectDatas($this->company);
848 }
849
866 public function deleteSupplierCategory($id, $category_id)
867 {
868 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
869 throw new RestException(403);
870 }
871
872 $result = $this->company->fetch($id);
873 if (!$result) {
874 throw new RestException(404, 'Thirdparty not found');
875 }
876 $category = new Categorie($this->db);
877 $result = $category->fetch($category_id);
878 if (!$result) {
879 throw new RestException(404, 'category not found');
880 }
881
882 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
883 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
884 }
885 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
886 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
887 }
888
889 $category->del_type($this->company, 'supplier');
890
891 return $this->_cleanObjectDatas($this->company);
892 }
893
894
913 public function getOutStandingProposals($id, $mode = 'customer')
914 {
915 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
916 throw new RestException(403);
917 }
918
919 if (empty($id)) {
920 throw new RestException(400, 'Thirdparty ID is mandatory');
921 }
922
923 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
924 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
925 }
926
927 $result = $this->company->fetch($id);
928 if (!$result) {
929 throw new RestException(404, 'Thirdparty not found');
930 }
931
932 $result = $this->company->getOutstandingProposals($mode);
933
934 unset($result['total_ht']);
935 unset($result['total_ttc']);
936
937 return $result;
938 }
939
940
959 public function getOutStandingOrder($id, $mode = 'customer')
960 {
961 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
962 throw new RestException(403);
963 }
964
965 if (empty($id)) {
966 throw new RestException(400, 'Thirdparty ID is mandatory');
967 }
968
969 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
970 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
971 }
972
973 $result = $this->company->fetch($id);
974 if (!$result) {
975 throw new RestException(404, 'Thirdparty not found');
976 }
977
978 $result = $this->company->getOutstandingOrders($mode);
979
980 unset($result['total_ht']);
981 unset($result['total_ttc']);
982
983 return $result;
984 }
985
1004 public function getOutStandingInvoices($id, $mode = 'customer')
1005 {
1006 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1007 throw new RestException(403);
1008 }
1009
1010 if (empty($id)) {
1011 throw new RestException(400, 'Thirdparty ID is mandatory');
1012 }
1013
1014 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1015 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1016 }
1017
1018 $result = $this->company->fetch($id);
1019 if (!$result) {
1020 throw new RestException(404, 'Thirdparty not found');
1021 }
1022
1023 $result = $this->company->getOutstandingBills($mode);
1024
1025 unset($result['total_ht']);
1026 unset($result['total_ttc']);
1027
1028 return $result;
1029 }
1030
1049 public function getSalesRepresentatives($id, $mode = 0)
1050 {
1051 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1052 throw new RestException(403);
1053 }
1054
1055 if (empty($id)) {
1056 throw new RestException(400, 'Thirdparty ID is mandatory');
1057 }
1058
1059 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1060 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1061 }
1062
1063 $result = $this->company->fetch($id);
1064 if (!is_array($result)) {
1065 throw new RestException(404, 'Thirdparty not found');
1066 }
1067
1068 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1069
1070 return $result;
1071 }
1072
1096 public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1097 {
1098 $obj_ret = array();
1099
1100 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1101 throw new RestException(403);
1102 }
1103
1104 if (empty($id)) {
1105 throw new RestException(400, 'Thirdparty ID is mandatory');
1106 }
1107
1108 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1109 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1110 }
1111
1112 $result = $this->company->fetch($id);
1113 if (!$result) {
1114 throw new RestException(404, 'Thirdparty not found');
1115 }
1116
1117
1118 $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";
1119 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1120 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = re.fk_facture_source";
1121 $sql .= " WHERE re.fk_soc = ".((int) $id);
1122 if ($filter == "available") {
1123 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1124 }
1125 if ($filter == "used") {
1126 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1127 }
1128
1129 $sql .= $this->db->order($sortfield, $sortorder);
1130
1131 $result = $this->db->query($sql);
1132 if (!$result) {
1133 throw new RestException(503, $this->db->lasterror());
1134 } else {
1135 $num = $this->db->num_rows($result);
1136 while ($obj = $this->db->fetch_object($result)) {
1137 $obj_ret[] = $obj;
1138 }
1139 }
1140
1141 return $obj_ret;
1142 }
1143
1144
1145
1164 {
1165 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1166 throw new RestException(403);
1167 }
1168 if (empty($id)) {
1169 throw new RestException(400, 'Thirdparty ID is mandatory');
1170 }
1171
1172 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1173 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1174 }
1175
1176 /*$result = $this->thirdparty->fetch($id);
1177 if( ! $result ) {
1178 throw new RestException(404, 'Thirdparty not found');
1179 }*/
1180
1181 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1182 $invoice = new Facture($this->db);
1183 $result = $invoice->list_replacable_invoices($id);
1184 if ($result < 0) {
1185 throw new RestException(405, $invoice->error);
1186 }
1187
1188 return $result;
1189 }
1190
1213 {
1214 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1215 throw new RestException(403);
1216 }
1217 if (empty($id)) {
1218 throw new RestException(400, 'Thirdparty ID is mandatory');
1219 }
1220
1221 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1222 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1223 }
1224
1225 /*$result = $this->thirdparty->fetch($id);
1226 if( ! $result ) {
1227 throw new RestException(404, 'Thirdparty not found');
1228 }*/
1229
1230 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1231 $invoice = new Facture($this->db);
1232 $result = $invoice->list_qualified_avoir_invoices($id);
1233 if (!is_array($result) && $result < 0) {
1234 throw new RestException(405, $invoice->error);
1235 }
1236
1237 return $result;
1238 }
1239
1256 {
1257 if (empty($id)) {
1258 throw new RestException(400, 'Thirdparty ID is mandatory');
1259 }
1260 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1261 throw new RestException(403);
1262 }
1263 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1264 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1265 }
1266
1271 $sql = "SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1272 $sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1273 if ($id) {
1274 $sql .= " WHERE fk_soc = ".((int) $id);
1275 }
1276
1277 $result = $this->db->query($sql);
1278 if ($this->db->num_rows($result) == 0) {
1279 throw new RestException(404, 'Notification not found');
1280 }
1281
1282 $i = 0;
1283
1284 $notifications = array();
1285
1286 if ($result) {
1287 $num = $this->db->num_rows($result);
1288 while ($i < $num) {
1289 $obj = $this->db->fetch_object($result);
1290 $notifications[] = $obj;
1291 $i++;
1292 }
1293 } else {
1294 throw new RestException(404, 'No notifications found');
1295 }
1296
1297 $fields = array('id', 'socid', 'event', 'contact_id', 'datec', 'tms', 'type');
1298
1299 $returnNotifications = array();
1300
1301 foreach ($notifications as $notification) {
1302 $object = array();
1303 foreach ($notification as $key => $value) {
1304 if (in_array($key, $fields)) {
1305 $object[$key] = $value;
1306 }
1307 }
1308 $returnNotifications[] = $object;
1309 }
1310
1311 // Too complex for phan ?: @phan-suppress-next-line PhanTypeMismatchReturn
1312 return $returnNotifications;
1313 }
1314
1331 public function createCompanyNotification($id, $request_data = null)
1332 {
1333 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1334 throw new RestException(403, "User has no right to update thirdparties");
1335 }
1336 if ($this->company->fetch($id) <= 0) {
1337 throw new RestException(404, 'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1338 }
1339 $notification = new Notify($this->db);
1340
1341 $notification->socid = $id;
1342
1343 foreach ($request_data as $field => $value) {
1344 $notification->$field = $value;
1345 }
1346
1347 $event = $notification->event;
1348 if (!$event) {
1349 throw new RestException(500, 'Error creating Thirdparty Notification, request_data missing event');
1350 }
1351 $socid = $notification->socid;
1352 $contact_id = $notification->contact_id;
1353
1354 $exists_sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1355 $exists_sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1356 $exists_sql .= " WHERE fk_action = '".$this->db->escape((string) $event)."'";
1357 $exists_sql .= " AND fk_soc = '".$this->db->escape((string) $socid)."'";
1358 $exists_sql .= " AND fk_contact = '".$this->db->escape((string) $contact_id)."'";
1359
1360 $exists_result = $this->db->query($exists_sql);
1361 if ($this->db->num_rows($exists_result) > 0) {
1362 throw new RestException(403, 'Notification already exists');
1363 }
1364
1365 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1366 throw new RestException(500, 'Error creating Thirdparty Notification');
1367 }
1368
1369 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1370 throw new RestException(500, 'Error updating values');
1371 }
1372
1373 return $this->_cleanObjectDatas($notification);
1374 }
1375
1394 public function createCompanyNotificationByCode($id, $code, $request_data = null)
1395 {
1396 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1397 throw new RestException(403, "User has no right to update thirdparties");
1398 }
1399 if ($this->company->fetch($id) <= 0) {
1400 throw new RestException(404, 'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1401 }
1402 $notification = new Notify($this->db);
1403 $notification->socid = $id;
1404
1405 $sql = "SELECT t.rowid as id FROM ".MAIN_DB_PREFIX."c_action_trigger as t";
1406 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
1407
1408 $result = $this->db->query($sql);
1409 if ($this->db->num_rows($result) == 0) {
1410 throw new RestException(404, 'Action Trigger code not found');
1411 }
1412
1413 $notification->event = $this->db->fetch_row($result)[0];
1414 foreach ($request_data as $field => $value) {
1415 if ($field === 'event') {
1416 throw new RestException(500, 'Error creating Thirdparty Notification, request_data contains event key');
1417 }
1418 if ($field === 'fk_action') {
1419 throw new RestException(500, 'Error creating Thirdparty Notification, request_data contains fk_action key');
1420 }
1421 $notification->$field = $value;
1422 }
1423
1424 $event = $notification->event;
1425 $socid = $notification->socid;
1426 $contact_id = $notification->contact_id;
1427
1428 $exists_sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1429 $exists_sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1430 $exists_sql .= " WHERE fk_action = '".$this->db->escape((string) $event)."'";
1431 $exists_sql .= " AND fk_soc = '".$this->db->escape((string) $socid)."'";
1432 $exists_sql .= " AND fk_contact = '".$this->db->escape((string) $contact_id)."'";
1433
1434 $exists_result = $this->db->query($exists_sql);
1435 if ($this->db->num_rows($exists_result) > 0) {
1436 throw new RestException(403, 'Notification already exists');
1437 }
1438
1439 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1440 throw new RestException(500, 'Error creating Thirdparty Notification, are request_data well formed?');
1441 }
1442
1443 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1444 throw new RestException(500, 'Error updating values');
1445 }
1446
1447 return $this->_cleanObjectDatas($notification);
1448 }
1449
1464 public function deleteCompanyNotification($id, $notification_id)
1465 {
1466 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1467 throw new RestException(403);
1468 }
1469
1470 $notification = new Notify($this->db);
1471
1472 $notification->fetch($notification_id);
1473
1474 $socid = (int) $notification->socid;
1475
1476 if ($socid == $id) {
1477 return $notification->delete(DolibarrApiAccess::$user);
1478 } else {
1479 throw new RestException(403, "Not allowed due to bad consistency of input data");
1480 }
1481 }
1482
1500 public function updateCompanyNotification($id, $notification_id, $request_data = null)
1501 {
1502 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1503 throw new RestException(403, "User has no right to update thirdparties");
1504 }
1505 if ($this->company->fetch($id) <= 0) {
1506 throw new RestException(404, 'Error creating Company Notification, Company doesn\'t exists');
1507 }
1508 $notification = new Notify($this->db);
1509
1510 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1511 $notification->fetch($notification_id, $id);
1512
1513 if ($notification->socid != $id) {
1514 throw new RestException(403, "Not allowed due to bad consistency of input data");
1515 }
1516
1517 foreach ($request_data as $field => $value) {
1518 $notification->$field = $value;
1519 }
1520
1521 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1522 throw new RestException(500, 'Error updating values');
1523 }
1524
1525 return $this->_cleanObjectDatas($notification);
1526 }
1527
1544 {
1545 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1546 throw new RestException(403);
1547 }
1548 if (empty($id)) {
1549 throw new RestException(400, 'Thirdparty ID is mandatory');
1550 }
1551
1552 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1553 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1554 }
1555
1560 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1561 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1562 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1563 if ($id) {
1564 $sql .= " WHERE fk_soc = ".((int) $id);
1565 }
1566
1567 $result = $this->db->query($sql);
1568
1569 if ($this->db->num_rows($result) == 0) {
1570 throw new RestException(404, 'Account not found');
1571 }
1572
1573 $i = 0;
1574
1575 $accounts = array();
1576
1577 if ($result) {
1578 $num = $this->db->num_rows($result);
1579 while ($i < $num) {
1580 $obj = $this->db->fetch_object($result);
1581
1582 $account = new CompanyBankAccount($this->db);
1583 if ($account->fetch($obj->rowid)) {
1584 $accounts[] = $account;
1585 }
1586 $i++;
1587 }
1588 } else {
1589 throw new RestException(404, 'Account not found');
1590 }
1591
1592
1593 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1594
1595 $returnAccounts = array();
1596
1597 foreach ($accounts as $account) {
1598 $object = array();
1599 foreach ($account as $key => $value) {
1600 if (in_array($key, $fields)) {
1601 if ($key == 'iban') {
1602 $object[$key] = dolDecrypt($value);
1603 } else {
1604 $object[$key] = $value;
1605 }
1606 }
1607 }
1608 $returnAccounts[] = $object;
1609 }
1610
1611 return $returnAccounts;
1612 }
1613
1630 public function createCompanyBankAccount($id, $request_data = null)
1631 {
1632 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1633 throw new RestException(403);
1634 }
1635 if ($this->company->fetch($id) <= 0) {
1636 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1637 }
1638 $account = new CompanyBankAccount($this->db);
1639
1640 $account->socid = $id;
1641
1642 foreach ($request_data as $field => $value) {
1643 if ($field === 'caller') {
1644 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
1645 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1646 continue;
1647 }
1648
1649 $account->$field = $this->_checkValForAPI('extrafields', $value, $account);
1650 }
1651
1652 if ($account->create(DolibarrApiAccess::$user) < 0) {
1653 throw new RestException(500, 'Error creating Company Bank account');
1654 }
1655
1656 if (empty($account->rum)) {
1657 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1658 $prelevement = new BonPrelevement($this->db);
1659 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (string) $account->id);
1660 $account->date_rum = dol_now();
1661 }
1662
1663 if ($account->update(DolibarrApiAccess::$user) < 0) {
1664 throw new RestException(500, 'Error updating values');
1665 }
1666
1667 return $this->_cleanObjectDatas($account);
1668 }
1669
1687 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1688 {
1689 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1690 throw new RestException(403);
1691 }
1692 if ($this->company->fetch($id) <= 0) {
1693 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1694 }
1695 $account = new CompanyBankAccount($this->db);
1696
1697 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1698 $account->fetch($bankaccount_id, '', $id, -1, '');
1699
1700 if ($account->socid != $id) {
1701 throw new RestException(403);
1702 }
1703
1704
1705 foreach ($request_data as $field => $value) {
1706 if ($field === 'caller') {
1707 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
1708 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1709 continue;
1710 }
1711
1712 $account->$field = $this->_checkValForAPI($field, $value, $account);
1713 }
1714
1715 if (empty($account->rum)) {
1716 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1717 $prelevement = new BonPrelevement($this->db);
1718 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (string) $account->id);
1719 $account->date_rum = dol_now();
1720 }
1721
1722 if ($account->update(DolibarrApiAccess::$user) < 0) {
1723 throw new RestException(500, 'Error updating values');
1724 }
1725
1726 return $this->_cleanObjectDatas($account);
1727 }
1728
1743 public function deleteCompanyBankAccount($id, $bankaccount_id)
1744 {
1745 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1746 throw new RestException(403);
1747 }
1748
1749 $account = new CompanyBankAccount($this->db);
1750
1751 $account->fetch($bankaccount_id);
1752
1753 $socid = (int) $account->socid;
1754
1755 if ($socid == $id) {
1756 return $account->delete(DolibarrApiAccess::$user);
1757 } else {
1758 throw new RestException(403, "Not allowed due to bad consistency of input data");
1759 }
1760 }
1761
1780 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1781 {
1782 global $conf, $langs;
1783
1784 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1785
1786 if ($this->company->fetch($id) <= 0) {
1787 throw new RestException(404, 'Thirdparty not found');
1788 }
1789
1790 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1791 throw new RestException(403);
1792 }
1793
1794 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1795
1796 $this->company->fk_bank = $this->company->fk_account;
1797 // $this->company->fk_account = $this->company->fk_account;
1798
1799 $outputlangs = $langs;
1800 $newlang = '';
1801
1802 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1803 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1804 if (isset($this->company->thirdparty->default_lang)) {
1805 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1806 } elseif (isset($this->company->default_lang)) {
1807 $newlang = $this->company->default_lang; // for thirdparty
1808 }
1809 }
1810 if (!empty($newlang)) {
1811 $outputlangs = new Translate("", $conf);
1812 $outputlangs->setDefaultLang($newlang);
1813 }
1814
1815 $sql = "SELECT rowid";
1816 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1817 if ($id) {
1818 $sql .= " WHERE fk_soc = ".((int) $id);
1819 }
1820 if ($companybankid) {
1821 $sql .= " AND rowid = ".((int) $companybankid);
1822 }
1823
1824 $i = 0;
1825 $accounts = array();
1826
1827 $result = $this->db->query($sql);
1828 if ($result) {
1829 if ($this->db->num_rows($result) == 0) {
1830 throw new RestException(404, 'Bank account not found');
1831 }
1832
1833 $num = $this->db->num_rows($result);
1834 while ($i < $num) {
1835 $obj = $this->db->fetch_object($result);
1836
1837 $account = new CompanyBankAccount($this->db);
1838 if ($account->fetch($obj->rowid)) {
1839 $accounts[] = $account;
1840 }
1841 $i++;
1842 }
1843 } else {
1844 throw new RestException(500, 'Sql error '.$this->db->lasterror());
1845 }
1846
1847 $moreparams = array(
1848 'use_companybankid' => $accounts[0]->id,
1849 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName((string) $this->company->id)
1850 );
1851
1852 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1853
1854 if ($result > 0) {
1855 return array("success" => $result);
1856 } else {
1857 throw new RestException(500, 'Error generating the document '.$this->company->error);
1858 }
1859 }
1860
1878 public function getSocieteAccounts($id, $site = null)
1879 {
1880 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1881 throw new RestException(403);
1882 }
1883
1884 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1885 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1886 }
1887
1891 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1892 $sql .= " WHERE fk_soc = ".((int) $id);
1893 if ($site) {
1894 $sql .= " AND site ='".$this->db->escape($site)."'";
1895 }
1896
1897 $result = $this->db->query($sql);
1898
1899 if ($result && $this->db->num_rows($result) == 0) {
1900 throw new RestException(404, 'This thirdparty does not have any account attached or does not exist.');
1901 }
1902
1903 $i = 0;
1904
1905 $accounts = array();
1906
1907 $num = $this->db->num_rows($result);
1908 while ($i < $num) {
1909 $obj = $this->db->fetch_object($result);
1910 $account = new SocieteAccount($this->db);
1911
1912 if ($account->fetch($obj->rowid)) {
1913 $accounts[] = $account;
1914 }
1915 $i++;
1916 }
1917
1918 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1919
1920 $returnAccounts = array();
1921
1922 foreach ($accounts as $account) {
1923 $object = array();
1924 foreach ($account as $key => $value) {
1925 if (in_array($key, $fields)) {
1926 $object[$key] = $value;
1927 }
1928 }
1929 $returnAccounts[] = $object;
1930 }
1931
1932 return $returnAccounts;
1933 }
1934
1952 public function getSocieteByAccounts($site, $key_account)
1953 {
1954 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1955 throw new RestException(403);
1956 }
1957
1958 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1959 $sql .= " WHERE site = '".$this->db->escape($site)."' AND key_account = '".$this->db->escape($key_account)."'";
1960 $sql .= " AND entity IN (".getEntity('societe').")";
1961
1962 $result = $this->db->query($sql);
1963
1964 if ($result && $this->db->num_rows($result) == 1) {
1965 $obj = $this->db->fetch_object($result);
1966 $returnThirdparty = $this->_fetch($obj->fk_soc);
1967 } else {
1968 throw new RestException(404, 'This account have many thirdparties attached or does not exist.');
1969 }
1970
1971 if (!DolibarrApi::_checkAccessToResource('societe', $returnThirdparty->id)) {
1972 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1973 }
1974
1975 return $returnThirdparty;
1976 }
1977
2001 public function createSocieteAccount($id, $request_data = null)
2002 {
2003 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2004 throw new RestException(403);
2005 }
2006
2007 if (!isset($request_data['site'])) {
2008 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
2009 }
2010
2011 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
2012 $result = $this->db->query($sql);
2013
2014 if ($result && $this->db->num_rows($result) == 0) {
2015 $account = new SocieteAccount($this->db);
2016 if (!isset($request_data['login'])) {
2017 $account->login = "";
2018 }
2019 $account->fk_soc = $id;
2020
2021 foreach ($request_data as $field => $value) {
2022 if ($field === 'caller') {
2023 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2024 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2025 continue;
2026 }
2027
2028 $account->$field = $this->_checkValForAPI($field, $value, $account);
2029 }
2030
2031 if ($account->create(DolibarrApiAccess::$user) < 0) {
2032 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2033 }
2034
2035 $this->_cleanObjectDatas($account);
2036
2037 return $account;
2038 } else {
2039 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
2040 }
2041 }
2042
2069 public function postSocieteAccount($id, $site, $request_data = null)
2070 {
2071 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2072 throw new RestException(403);
2073 }
2074
2075 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
2076 $result = $this->db->query($sql);
2077
2078 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
2079 if ($result && $this->db->num_rows($result) == 0) {
2080 if (!isset($request_data['key_account'])) {
2081 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2082 }
2083 $account = new SocieteAccount($this->db);
2084 if (!isset($request_data['login'])) {
2085 $account->login = "";
2086 }
2087
2088 foreach ($request_data as $field => $value) {
2089 if ($field === 'caller') {
2090 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2091 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2092 continue;
2093 }
2094
2095 $account->$field = $this->_checkValForAPI($field, $value, $account);
2096 }
2097
2098 $account->fk_soc = $id;
2099 $account->site = $site;
2100
2101 if ($account->create(DolibarrApiAccess::$user) < 0) {
2102 throw new RestException(500, 'Error creating SocieteAccount entity.');
2103 }
2104 // We found an existing SocieteAccount entity, we are replacing it
2105 } else {
2106 if (isset($request_data['site']) && $request_data['site'] !== $site) {
2107 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
2108 $result = $this->db->query($sql);
2109
2110 if ($result && $this->db->num_rows($result) !== 0) {
2111 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.");
2112 }
2113 }
2114
2115 $obj = $this->db->fetch_object($result);
2116
2117 $account = new SocieteAccount($this->db);
2118 $account->id = $obj->rowid;
2119 $account->fk_soc = $id;
2120 $account->site = $site;
2121 if (!isset($request_data['login'])) {
2122 $account->login = "";
2123 }
2124 $account->fk_user_creat = $obj->fk_user_creat;
2125 $account->date_creation = $obj->date_creation;
2126
2127 foreach ($request_data as $field => $value) {
2128 if ($field === 'caller') {
2129 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2130 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2131 continue;
2132 }
2133
2134 $account->$field = $this->_checkValForAPI($field, $value, $account);
2135 }
2136
2137 if ($account->update(DolibarrApiAccess::$user) < 0) {
2138 throw new RestException(500, 'Error updating SocieteAccount entity.');
2139 }
2140 }
2141
2142 $this->_cleanObjectDatas($account);
2143
2144 return $account;
2145 }
2146
2167 public function putSocieteAccount($id, $site, $request_data = null)
2168 {
2169 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2170 throw new RestException(403);
2171 }
2172
2173 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
2174 $result = $this->db->query($sql);
2175
2176 if ($result && $this->db->num_rows($result) == 0) {
2177 throw new RestException(404, "This thirdparty does not have $site account attached or does not exist.");
2178 } else {
2179 // If the user tries to edit the site member, we check first if
2180 if (isset($request_data['site']) && $request_data['site'] !== $site) {
2181 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
2182 $result = $this->db->query($sql);
2183
2184 if ($result && $this->db->num_rows($result) !== 0) {
2185 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.");
2186 }
2187 }
2188
2189 $obj = $this->db->fetch_object($result);
2190 $account = new SocieteAccount($this->db);
2191 $account->fetch($obj->rowid);
2192
2193 foreach ($request_data as $field => $value) {
2194 if ($field === 'caller') {
2195 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2196 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2197 continue;
2198 }
2199
2200 $account->$field = $this->_checkValForAPI($field, $value, $account);
2201 }
2202
2203 if ($account->update(DolibarrApiAccess::$user) < 0) {
2204 throw new RestException(500, 'Error updating SocieteAccount account');
2205 }
2206
2207 $this->_cleanObjectDatas($account);
2208
2209 return $account;
2210 }
2211 }
2212
2231 public function deleteSocieteAccount($id, $site)
2232 {
2233 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2234 throw new RestException(403);
2235 }
2236
2237 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
2238 $result = $this->db->query($sql);
2239
2240 if ($result && $this->db->num_rows($result) == 0) {
2241 throw new RestException(404);
2242 } else {
2243 $obj = $this->db->fetch_object($result);
2244 $account = new SocieteAccount($this->db);
2245 $account->fetch($obj->rowid);
2246
2247 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2248 throw new RestException(500, "Error while deleting $site account attached to this third party");
2249 }
2250 }
2251 }
2252
2269 {
2270 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2271 throw new RestException(403);
2272 }
2273
2278 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2279 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
2280
2281 $result = $this->db->query($sql);
2282
2283 if ($result && $this->db->num_rows($result) == 0) {
2284 throw new RestException(404, 'This third party does not have any account attached or does not exist.');
2285 } else {
2286 $i = 0;
2287
2288 $num = $this->db->num_rows($result);
2289 while ($i < $num) {
2290 $obj = $this->db->fetch_object($result);
2291 $account = new SocieteAccount($this->db);
2292 $account->fetch($obj->rowid);
2293
2294 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2295 throw new RestException(500, 'Error while deleting account attached to this third party');
2296 }
2297 $i++;
2298 }
2299 }
2300 }
2301
2302 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2309 protected function _cleanObjectDatas($object)
2310 {
2311 // phpcs:enable
2312 $object = parent::_cleanObjectDatas($object);
2313
2314 unset($object->nom); // ->name already defined and nom deprecated
2315 unset($object->name_bis); // ->name_alias already defined
2316 unset($object->note); // ->note_private and note_public already defined
2317 unset($object->departement);
2318 unset($object->departement_code);
2319 unset($object->pays);
2320 unset($object->particulier);
2321 unset($object->prefix_comm);
2322
2323 unset($object->siren);
2324 unset($object->siret);
2325 unset($object->ape);
2326
2327 unset($object->commercial_id); // This property is used in create/update only. It does not exists in read mode because there is several sales representatives.
2328
2329 unset($object->total_ht);
2330 unset($object->total_tva);
2331 unset($object->total_localtax1);
2332 unset($object->total_localtax2);
2333 unset($object->total_ttc);
2334
2335 unset($object->lines);
2336 unset($object->thirdparty);
2337
2338 unset($object->fk_delivery_address); // deprecated feature
2339
2340 return $object;
2341 }
2342
2351 private function _validate($data)
2352 {
2353 if ($data === null) {
2354 $data = array();
2355 }
2356 $thirdparty = array();
2357 foreach (Thirdparties::$FIELDS as $field) {
2358 if (!isset($data[$field])) {
2359 throw new RestException(400, "$field field missing");
2360 }
2361 $thirdparty[$field] = $data[$field];
2362 }
2363 return $thirdparty;
2364 }
2365
2389 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
2390 {
2391 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
2392 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
2393 }
2394
2395 if ($rowid === 0) {
2396 $result = $this->company->initAsSpecimen();
2397 } else {
2398 $result = $this->company->fetch((int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2399 }
2400 if (!$result) {
2401 throw new RestException(404, 'Thirdparty not found');
2402 }
2403
2404 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
2405 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
2406 }
2407 if (isModEnabled('mailing')) {
2408 $this->company->getNoEmail();
2409 }
2410
2411 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
2412 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be subtracted to payments only and not to total of final invoice
2413 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be subtracted to payments only and not to total of final invoice
2414 } else {
2415 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2416 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2417 }
2418
2419 $absolute_discount = $this->company->getAvailableDiscounts(null, $filterabsolutediscount);
2420 $absolute_creditnote = $this->company->getAvailableDiscounts(null, $filtercreditnote);
2421 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
2422 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
2423
2424 return $this->_cleanObjectDatas($this->company);
2425 }
2426}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
Class for API REST v1.
Definition api.class.php:33
_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.
Definition api.class.php:98
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 a company notification for a third party.
setThirdpartyPriceLevel($id, $priceLevel)
Set a new price level for the given third party.
_cleanObjectDatas($object)
Clean sensible object datas.
getSocieteByAccounts($site, $key_account)
Get a specific third party by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a third party.
deleteCompanyNotification($id, $notification_id)
Delete a company notification attached to a third party.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a third party.
getOutStandingOrder($id, $mode='customer')
Get outstanding orders for a third party.
addRepresentative($id, $representative_id)
Add a customer representative to a third party.
getByBarcode($barcode)
Get a third party by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a document from a bank account record.
createCompanyNotificationByCode($id, $code, $request_data=null)
Create a company notification for a third party using action trigger code.
getCompanyNotification($id)
Get company notifications for a third party.
addCategory($id, $category_id)
Add a customer category to a third party.
getCompanyBankAccount($id)
Get company bank accounts of a third party.
getInvoicesQualifiedForReplacement($id)
Return invoices qualified to be replaced by another invoice.
post($request_data=null)
Create a third party.
put($id, $request_data=null)
Update third party.
getByEmail($email)
Get properties of a third party by email.
_validate($data)
Validate fields before create or update object.
addSupplierCategory($id, $category_id)
Add a supplier category to a third party.
merge($id, $idtodelete)
Merge a third party into another third party.
deleteSocieteAccounts($id)
Delete all accounts attached to a third party.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a third party.
postSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account for a third party.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the third party.
deleteRepresentative($id, $representative_id)
Remove the link between a customer representative and a third party.
createCompanyNotification($id, $request_data=null)
Create a company notification for a third party.
putSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a third party.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update a company bank account of a third party.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a third party.
getInvoicesQualifiedForCreditNote($id)
Return invoices qualified to be corrected by a credit note.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a third party.
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals for a third party.
_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 a third party.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices for a third party.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List third parties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a third party.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing third party.
createCompanyBankAccount($id, $request_data=null)
Create a company bank account for a third party.
deleteCategory($id, $category_id)
Remove the link between a customer category and the third party.
Class to manage translations.
Class to manage Dolibarr users.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.