dolibarr 20.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 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
9 * Copyright (C) 2026 Benjamin Falière <benjamin@faliere.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25use Luracast\Restler\RestException;
26
35{
40 public static $FIELDS = array(
41 'name'
42 );
43
47 public $company;
48
52 public function __construct()
53 {
54 global $db;
55 $this->db = $db;
56
57 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
58 require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
59 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
60 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
61 require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
62
63 $this->company = new Societe($this->db);
64
65 if (getDolGlobalString('SOCIETE_EMAIL_MANDATORY')) {
66 static::$FIELDS[] = 'email';
67 }
68 }
69
80 public function get($id)
81 {
82 return $this->_fetch($id);
83 }
84
97 public function getByEmail($email)
98 {
99 return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
100 }
101
114 public function getByBarcode($barcode)
115 {
116 return $this->_fetch('', '', '', $barcode);
117 }
118
137 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $properties = '')
138 {
139 $obj_ret = array();
140
141 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
142 throw new RestException(403);
143 }
144
145 // case of external user, we force socids
146 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid : '';
147
148 // If the internal user must only see his customers, force searching by him
149 $search_sale = 0;
150 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
151 $search_sale = DolibarrApiAccess::$user->id;
152 }
153
154 $sql = "SELECT t.rowid";
155 $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
156 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_extrafields AS ef ON ef.fk_object = t.rowid"; // So we will be able to filter on extrafields
157 if ($category > 0) {
158 if ($mode != 4) {
159 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
160 }
161 if (!in_array($mode, array(1, 2, 3))) {
162 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
163 }
164 }
165 $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
166 $sql .= " WHERE t.entity IN (".getEntity('societe').")";
167 $sql .= " AND t.fk_stcomm = st.id";
168 if ($mode == 1) {
169 $sql .= " AND t.client IN (1, 3)";
170 } elseif ($mode == 2) {
171 $sql .= " AND t.client IN (2, 3)";
172 } elseif ($mode == 3) {
173 $sql .= " AND t.client IN (0)";
174 } elseif ($mode == 4) {
175 $sql .= " AND t.fournisseur IN (1)";
176 }
177 // Select thirdparties of given category
178 if ($category > 0) {
179 if (!empty($mode) && $mode != 4) {
180 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
181 } elseif (!empty($mode) && $mode == 4) {
182 $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
183 } else {
184 $sql .= " AND ((c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid))";
185 }
186 }
187 if ($socids) {
188 $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
189 }
190 // Search on sale representative
191 if ($search_sale && $search_sale != '-1') {
192 if ($search_sale == -2) {
193 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
194 } elseif ($search_sale > 0) {
195 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.rowid AND sc.fk_user = ".((int) $search_sale).")";
196 }
197 }
198 // Add sql filters
199 if ($sqlfilters) {
200 $errormessage = '';
201 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
202 if ($errormessage) {
203 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
204 }
205 }
206
207 $sql .= $this->db->order($sortfield, $sortorder);
208
209 if ($limit) {
210 if ($page < 0) {
211 $page = 0;
212 }
213 $offset = $limit * $page;
214
215 $sql .= $this->db->plimit($limit + 1, $offset);
216 }
217
218 $result = $this->db->query($sql);
219 if ($result) {
220 $num = $this->db->num_rows($result);
221 $min = min($num, ($limit <= 0 ? $num : $limit));
222 $i = 0;
223 while ($i < $min) {
224 $obj = $this->db->fetch_object($result);
225 $soc_static = new Societe($this->db);
226 if ($soc_static->fetch($obj->rowid)) {
227 if (isModEnabled('mailing')) {
228 $soc_static->getNoEmail();
229 }
230 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($soc_static), $properties);
231 }
232 $i++;
233 }
234 } else {
235 throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
236 }
237 if (!count($obj_ret)) {
238 throw new RestException(404, 'Thirdparties not found');
239 }
240 return $obj_ret;
241 }
242
249 public function post($request_data = null)
250 {
251 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
252 throw new RestException(403);
253 }
254 // Check mandatory fields
255 $result = $this->_validate($request_data);
256
257 foreach ($request_data as $field => $value) {
258 if ($field === 'caller') {
259 // 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
260 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
261 continue;
262 }
263
264 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
265 }
266
267 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
268 throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
269 }
270 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
271 $this->company->setNoEmail($this->company->no_email);
272 }
273
274 return $this->company->id;
275 }
276
288 public function put($id, $request_data = null)
289 {
290 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
291 throw new RestException(403);
292 }
293
294 $result = $this->company->fetch($id);
295 if (!$result) {
296 throw new RestException(404, 'Thirdparty not found');
297 }
298
299 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
300 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
301 }
302
303 foreach ($request_data as $field => $value) {
304 if ($field == 'id') {
305 continue;
306 }
307 if ($field === 'caller') {
308 // 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
309 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
310 continue;
311 }
312 if ($field == 'array_options' && is_array($value)) {
313 foreach ($value as $index => $val) {
314 $this->company->array_options[$index] = $this->_checkValForAPI($field, $val, $this->company);
315 }
316 continue;
317 }
318 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
319 }
320
321 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
322 $this->company->setNoEmail($this->company->no_email);
323 }
324
325 if ($this->company->update($id, DolibarrApiAccess::$user, 1, 1, 1, 'update', 1) > 0) {
326 return $this->get($id);
327 } else {
328 throw new RestException(500, $this->company->error);
329 }
330 }
331
346 public function merge($id, $idtodelete)
347 {
348 if ($id == $idtodelete) {
349 throw new RestException(400, 'Try to merge a thirdparty into itself');
350 }
351
352 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
353 throw new RestException(403);
354 }
355
356 $result = $this->company->fetch($id); // include the fetch of extra fields
357 if (!$result) {
358 throw new RestException(404, 'Thirdparty not found');
359 }
360
361 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
362 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
363 }
364
365 $companytoremove = new Societe($this->db);
366 $result = $companytoremove->fetch($idtodelete); // include the fetch of extra fields
367 if (!$result) {
368 throw new RestException(404, 'Thirdparty not found');
369 }
370
371 if (!DolibarrApi::_checkAccessToResource('societe', $companytoremove->id)) {
372 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
373 }
374
375 $user = DolibarrApiAccess::$user;
376 $result = $this->company->mergeCompany($companytoremove->id);
377 if ($result < 0) {
378 throw new RestException(500, 'Error failed to merged thirdparty '.$companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
379 }
380
381 return $this->get($id);
382 }
383
390 public function delete($id)
391 {
392 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
393 throw new RestException(403);
394 }
395 $result = $this->company->fetch($id);
396 if (!$result) {
397 throw new RestException(404, 'Thirdparty not found');
398 }
399 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
400 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
401 }
402 $this->company->oldcopy = clone $this->company;
403
404 $res = $this->company->delete($id);
405 if ($res < 0) {
406 throw new RestException(500, "Can't delete, error occurs");
407 } elseif ($res == 0) {
408 throw new RestException(409, "Can't delete, that product is probably used");
409 }
410
411 return array(
412 'success' => array(
413 'code' => 200,
414 'message' => 'Object deleted'
415 )
416 );
417 }
418
434 public function setThirdpartyPriceLevel($id, $priceLevel)
435 {
436 global $conf;
437
438 if (!isModEnabled('societe')) {
439 throw new RestException(501, 'Module "Thirdparties" needed for this request');
440 }
441
442 if (!isModEnabled("product")) {
443 throw new RestException(501, 'Module "Products" needed for this request');
444 }
445
446 if (!getDolGlobalString('PRODUIT_MULTIPRICES')) {
447 throw new RestException(501, 'Multiprices features activation needed for this request');
448 }
449
450 if ($priceLevel < 1 || $priceLevel > getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT')) {
451 throw new RestException(400, 'Price level must be between 1 and ' . getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT'));
452 }
453
454 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
455 throw new RestException(403, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
456 }
457
458 $result = $this->company->fetch($id);
459 if ($result < 0) {
460 throw new RestException(404, 'Thirdparty '.$id.' not found');
461 }
462
463 if (empty($result)) {
464 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
465 }
466
467 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
468 throw new RestException(403, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
469 }
470
471 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
472 if ($result <= 0) {
473 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
474 }
475
476 return $this->_cleanObjectDatas($this->company);
477 }
478
491 public function addRepresentative($id, $representative_id)
492 {
493 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
494 throw new RestException(403);
495 }
496 $result = $this->company->fetch($id);
497 if (!$result) {
498 throw new RestException(404, 'Thirdparty not found');
499 }
500 $usertmp = new User($this->db);
501 $result = $usertmp->fetch($representative_id);
502 if (!$result) {
503 throw new RestException(404, 'User not found');
504 }
505 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
506 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
507 }
508 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
509
510 return $result;
511 }
512
525 public function deleteRepresentative($id, $representative_id)
526 {
527 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
528 throw new RestException(403);
529 }
530 $result = $this->company->fetch($id);
531 if (!$result) {
532 throw new RestException(404, 'Thirdparty not found');
533 }
534 $usertmp = new User($this->db);
535 $result = $usertmp->fetch($representative_id);
536 if (!$result) {
537 throw new RestException(404, 'User not found');
538 }
539 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
540 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
541 }
542 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
543
544 return $result;
545 }
546
559 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
560 {
561 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
562 throw new RestException(403);
563 }
564
565 $result = $this->company->fetch($id);
566 if (!$result) {
567 throw new RestException(404, 'Thirdparty not found');
568 }
569
570 $categories = new Categorie($this->db);
571
572 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
573
574 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
575 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
576 }
577
578 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
579 return array();
580 }
581
582 return $arrayofcateg;
583 }
584
594 public function addCategory($id, $category_id)
595 {
596 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
597 throw new RestException(403);
598 }
599
600 $result = $this->company->fetch($id);
601 if (!$result) {
602 throw new RestException(404, 'Thirdparty not found');
603 }
604 $category = new Categorie($this->db);
605 $result = $category->fetch($category_id);
606 if (!$result) {
607 throw new RestException(404, 'category not found');
608 }
609
610 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
611 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 }
613 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
614 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 }
616
617 $category->add_type($this->company, 'customer');
618
619 return $this->_cleanObjectDatas($this->company);
620 }
621
632 public function deleteCategory($id, $category_id)
633 {
634 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
635 throw new RestException(403);
636 }
637
638 $result = $this->company->fetch($id);
639 if (!$result) {
640 throw new RestException(404, 'Thirdparty not found');
641 }
642 $category = new Categorie($this->db);
643 $result = $category->fetch($category_id);
644 if (!$result) {
645 throw new RestException(404, 'category not found');
646 }
647
648 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
649 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
650 }
651 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
652 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
653 }
654
655 $category->del_type($this->company, 'customer');
656
657 return $this->_cleanObjectDatas($this->company);
658 }
659
673 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
674 {
675 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
676 throw new RestException(403);
677 }
678
679 $result = $this->company->fetch($id);
680 if (!$result) {
681 throw new RestException(404, 'Thirdparty not found');
682 }
683
684 $categories = new Categorie($this->db);
685
686 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
687
688 if (is_numeric($result) && $result < 0) {
689 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
690 }
691
692 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
693 return array();
694 }
695
696 return $result;
697 }
698
709 public function addSupplierCategory($id, $category_id)
710 {
711 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
712 throw new RestException(403);
713 }
714
715 $result = $this->company->fetch($id);
716 if (!$result) {
717 throw new RestException(404, 'Thirdparty not found');
718 }
719 $category = new Categorie($this->db);
720 $result = $category->fetch($category_id);
721 if (!$result) {
722 throw new RestException(404, 'category not found');
723 }
724
725 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
726 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
727 }
728 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
729 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 }
731
732 $category->add_type($this->company, 'supplier');
733
734 return $this->_cleanObjectDatas($this->company);
735 }
736
747 public function deleteSupplierCategory($id, $category_id)
748 {
749 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
750 throw new RestException(403);
751 }
752
753 $result = $this->company->fetch($id);
754 if (!$result) {
755 throw new RestException(404, 'Thirdparty not found');
756 }
757 $category = new Categorie($this->db);
758 $result = $category->fetch($category_id);
759 if (!$result) {
760 throw new RestException(404, 'category not found');
761 }
762
763 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
764 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
765 }
766 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
767 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
768 }
769
770 $category->del_type($this->company, 'supplier');
771
772 return $this->_cleanObjectDatas($this->company);
773 }
774
775
790 public function getOutStandingProposals($id, $mode = 'customer')
791 {
792 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
793 throw new RestException(403);
794 }
795
796 if (empty($id)) {
797 throw new RestException(400, 'Thirdparty ID is mandatory');
798 }
799
800 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
801 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
802 }
803
804 $result = $this->company->fetch($id);
805 if (!$result) {
806 throw new RestException(404, 'Thirdparty not found');
807 }
808
809 $result = $this->company->getOutstandingProposals($mode);
810
811 unset($result['total_ht']);
812 unset($result['total_ttc']);
813
814 return $result;
815 }
816
817
832 public function getOutStandingOrder($id, $mode = 'customer')
833 {
834 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
835 throw new RestException(403);
836 }
837
838 if (empty($id)) {
839 throw new RestException(400, 'Thirdparty ID is mandatory');
840 }
841
842 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
843 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
844 }
845
846 $result = $this->company->fetch($id);
847 if (!$result) {
848 throw new RestException(404, 'Thirdparty not found');
849 }
850
851 $result = $this->company->getOutstandingOrders($mode);
852
853 unset($result['total_ht']);
854 unset($result['total_ttc']);
855
856 return $result;
857 }
858
873 public function getOutStandingInvoices($id, $mode = 'customer')
874 {
875 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
876 throw new RestException(403);
877 }
878
879 if (empty($id)) {
880 throw new RestException(400, 'Thirdparty ID is mandatory');
881 }
882
883 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
884 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
885 }
886
887 $result = $this->company->fetch($id);
888 if (!$result) {
889 throw new RestException(404, 'Thirdparty not found');
890 }
891
892 $result = $this->company->getOutstandingBills($mode);
893
894 unset($result['total_ht']);
895 unset($result['total_ttc']);
896
897 return $result;
898 }
899
914 public function getSalesRepresentatives($id, $mode = 0)
915 {
916 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
917 throw new RestException(403);
918 }
919
920 if (empty($id)) {
921 throw new RestException(400, 'Thirdparty ID is mandatory');
922 }
923
924 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
925 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
926 }
927
928 $result = $this->company->fetch($id);
929 if (!$result) {
930 throw new RestException(404, 'Thirdparty not found');
931 }
932
933 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
934
935 return $result;
936 }
937
955 public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
956 {
957 $obj_ret = array();
958
959 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
960 throw new RestException(403);
961 }
962
963 if (empty($id)) {
964 throw new RestException(400, 'Thirdparty ID is mandatory');
965 }
966
967 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
968 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
969 }
970
971 $result = $this->company->fetch($id);
972 if (!$result) {
973 throw new RestException(404, 'Thirdparty not found');
974 }
975
976
977 $sql = "SELECT f.ref, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line";
978 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
979 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = re.fk_facture_source";
980 $sql .= " WHERE re.fk_soc = ".((int) $id);
981 if ($filter == "available") {
982 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
983 }
984 if ($filter == "used") {
985 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
986 }
987
988 $sql .= $this->db->order($sortfield, $sortorder);
989
990 $result = $this->db->query($sql);
991 if (!$result) {
992 throw new RestException(503, $this->db->lasterror());
993 } else {
994 $num = $this->db->num_rows($result);
995 while ($obj = $this->db->fetch_object($result)) {
996 $obj_ret[] = $obj;
997 }
998 }
999
1000 return $obj_ret;
1001 }
1002
1003
1004
1019 {
1020 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1021 throw new RestException(403);
1022 }
1023 if (empty($id)) {
1024 throw new RestException(400, 'Thirdparty ID is mandatory');
1025 }
1026
1027 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1028 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1029 }
1030
1031 /*$result = $this->thirdparty->fetch($id);
1032 if( ! $result ) {
1033 throw new RestException(404, 'Thirdparty not found');
1034 }*/
1035
1036 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1037 $invoice = new Facture($this->db);
1038 $result = $invoice->list_replacable_invoices($id);
1039 if ($result < 0) {
1040 throw new RestException(405, $invoice->error);
1041 }
1042
1043 return $result;
1044 }
1045
1063 {
1064 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1065 throw new RestException(403);
1066 }
1067 if (empty($id)) {
1068 throw new RestException(400, 'Thirdparty ID is mandatory');
1069 }
1070
1071 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1072 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1073 }
1074
1075 /*$result = $this->thirdparty->fetch($id);
1076 if( ! $result ) {
1077 throw new RestException(404, 'Thirdparty not found');
1078 }*/
1079
1080 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1081 $invoice = new Facture($this->db);
1082 $result = $invoice->list_qualified_avoir_invoices($id);
1083 if ($result < 0) {
1084 throw new RestException(405, $invoice->error);
1085 }
1086
1087 return $result;
1088 }
1089
1099 public function getCompanyNotification($id)
1100 {
1101 if (empty($id)) {
1102 throw new RestException(400, 'Thirdparty ID is mandatory');
1103 }
1104 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1105 throw new RestException(403);
1106 }
1107 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1108 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1109 }
1110
1115 $sql = "SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1116 $sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1117 if ($id) {
1118 $sql .= " WHERE fk_soc = ".((int) $id);
1119 }
1120
1121 $result = $this->db->query($sql);
1122 if ($this->db->num_rows($result) == 0) {
1123 throw new RestException(404, 'Notification not found');
1124 }
1125
1126 $i = 0;
1127
1128 $notifications = array();
1129
1130 if ($result) {
1131 $num = $this->db->num_rows($result);
1132 while ($i < $num) {
1133 $obj = $this->db->fetch_object($result);
1134 $notifications[] = $obj;
1135 $i++;
1136 }
1137 } else {
1138 throw new RestException(404, 'No notifications found');
1139 }
1140
1141 $fields = array('id', 'socid', 'event', 'contact_id', 'datec', 'tms', 'type');
1142
1143 $returnNotifications = array();
1144
1145 foreach ($notifications as $notification) {
1146 $object = array();
1147 foreach ($notification as $key => $value) {
1148 if (in_array($key, $fields)) {
1149 $object[$key] = $value;
1150 }
1151 }
1152 $returnNotifications[] = $object;
1153 }
1154
1155 return $returnNotifications;
1156 }
1157
1167 public function createCompanyNotification($id, $request_data = null)
1168 {
1169 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1170 throw new RestException(403, "User has no right to update thirdparties");
1171 }
1172 if ($this->company->fetch($id) <= 0) {
1173 throw new RestException(404, 'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1174 }
1175 $notification = new Notify($this->db);
1176
1177 $notification->socid = $id;
1178
1179 foreach ($request_data as $field => $value) {
1180 $notification->$field = $value;
1181 }
1182
1183 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1184 throw new RestException(500, 'Error creating Thirdparty Notification');
1185 }
1186
1187 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1188 throw new RestException(500, 'Error updating values');
1189 }
1190
1191 return $this->_cleanObjectDatas($notification);
1192 }
1193
1204 public function deleteCompanyNotification($id, $notification_id)
1205 {
1206 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1207 throw new RestException(403);
1208 }
1209
1210 $notification = new Notify($this->db);
1211
1212 $notification->fetch($notification_id);
1213
1214 $socid = (int) $notification->socid;
1215
1216 if ($socid == $id) {
1217 return $notification->delete(DolibarrApiAccess::$user);
1218 } else {
1219 throw new RestException(403, "Not allowed due to bad consistency of input data");
1220 }
1221 }
1222
1234 public function updateCompanyNotification($id, $notification_id, $request_data = null)
1235 {
1236 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1237 throw new RestException(403, "User has no right to update thirdparties");
1238 }
1239 if ($this->company->fetch($id) <= 0) {
1240 throw new RestException(404, 'Error creating Company Notification, Company doesn\'t exists');
1241 }
1242 $notification = new Notify($this->db);
1243
1244 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1245 $notification->fetch($notification_id, $id);
1246
1247 if ($notification->socid != $id) {
1248 throw new RestException(403, "Not allowed due to bad consistency of input data");
1249 }
1250
1251 foreach ($request_data as $field => $value) {
1252 $notification->$field = $value;
1253 }
1254
1255 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1256 throw new RestException(500, 'Error updating values');
1257 }
1258
1259 return $this->_cleanObjectDatas($notification);
1260 }
1261
1271 public function getCompanyBankAccount($id)
1272 {
1273 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1274 throw new RestException(403);
1275 }
1276 if (empty($id)) {
1277 throw new RestException(400, 'Thirdparty ID is mandatory');
1278 }
1279
1280 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1281 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1282 }
1283
1288 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1289 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1290 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1291 if ($id) {
1292 $sql .= " WHERE fk_soc = ".((int) $id);
1293 }
1294
1295 $result = $this->db->query($sql);
1296
1297 if ($this->db->num_rows($result) == 0) {
1298 throw new RestException(404, 'Account not found');
1299 }
1300
1301 $i = 0;
1302
1303 $accounts = array();
1304
1305 if ($result) {
1306 $num = $this->db->num_rows($result);
1307 while ($i < $num) {
1308 $obj = $this->db->fetch_object($result);
1309 $account = new CompanyBankAccount($this->db);
1310 if ($account->fetch($obj->rowid)) {
1311 $accounts[] = $account;
1312 }
1313 $i++;
1314 }
1315 } else {
1316 throw new RestException(404, 'Account not found');
1317 }
1318
1319
1320 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1321
1322 $returnAccounts = array();
1323
1324 foreach ($accounts as $account) {
1325 $object = array();
1326 foreach ($account as $key => $value) {
1327 if (in_array($key, $fields)) {
1328 $object[$key] = $value;
1329 }
1330 }
1331 $returnAccounts[] = $object;
1332 }
1333
1334 return $returnAccounts;
1335 }
1336
1346 public function createCompanyBankAccount($id, $request_data = null)
1347 {
1348 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1349 throw new RestException(403);
1350 }
1351 if ($this->company->fetch($id) <= 0) {
1352 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1353 }
1354 $account = new CompanyBankAccount($this->db);
1355
1356 $account->socid = $id;
1357
1358 foreach ($request_data as $field => $value) {
1359 if ($field === 'caller') {
1360 // 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
1361 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1362 continue;
1363 }
1364
1365 $account->$field = $this->_checkValForAPI('extrafields', $value, $account);
1366 }
1367
1368 if ($account->create(DolibarrApiAccess::$user) < 0) {
1369 throw new RestException(500, 'Error creating Company Bank account');
1370 }
1371
1372 if (empty($account->rum)) {
1373 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1374 $prelevement = new BonPrelevement($this->db);
1375 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1376 $account->date_rum = dol_now();
1377 }
1378
1379 if ($account->update(DolibarrApiAccess::$user) < 0) {
1380 throw new RestException(500, 'Error updating values');
1381 }
1382
1383 return $this->_cleanObjectDatas($account);
1384 }
1385
1397 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1398 {
1399 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1400 throw new RestException(403);
1401 }
1402 if ($this->company->fetch($id) <= 0) {
1403 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1404 }
1405 $account = new CompanyBankAccount($this->db);
1406
1407 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1408 $account->fetch($bankaccount_id, '', $id, -1, '');
1409
1410 if ($account->socid != $id) {
1411 throw new RestException(403);
1412 }
1413
1414
1415 foreach ($request_data as $field => $value) {
1416 if ($field === 'caller') {
1417 // 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
1418 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1419 continue;
1420 }
1421
1422 $account->$field = $this->_checkValForAPI($field, $value, $account);
1423 }
1424
1425 if (empty($account->rum)) {
1426 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1427 $prelevement = new BonPrelevement($this->db);
1428 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1429 $account->date_rum = dol_now();
1430 }
1431
1432 if ($account->update(DolibarrApiAccess::$user) < 0) {
1433 throw new RestException(500, 'Error updating values');
1434 }
1435
1436 return $this->_cleanObjectDatas($account);
1437 }
1438
1449 public function deleteCompanyBankAccount($id, $bankaccount_id)
1450 {
1451 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1452 throw new RestException(403);
1453 }
1454
1455 $account = new CompanyBankAccount($this->db);
1456
1457 $account->fetch($bankaccount_id);
1458
1459 $socid = (int) $account->socid;
1460
1461 if ($socid == $id) {
1462 return $account->delete(DolibarrApiAccess::$user);
1463 } else {
1464 throw new RestException(403, "Not allowed due to bad consistency of input data");
1465 }
1466 }
1467
1478 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1479 {
1480 global $conf, $langs;
1481
1482 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1483
1484 if ($this->company->fetch($id) <= 0) {
1485 throw new RestException(404, 'Thirdparty not found');
1486 }
1487
1488 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1489 throw new RestException(403);
1490 }
1491
1492 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1493
1494 $this->company->fk_bank = $this->company->fk_account;
1495 // $this->company->fk_account = $this->company->fk_account;
1496
1497 $outputlangs = $langs;
1498 $newlang = '';
1499
1500 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1501 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1502 if (isset($this->company->thirdparty->default_lang)) {
1503 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1504 } elseif (isset($this->company->default_lang)) {
1505 $newlang = $this->company->default_lang; // for thirdparty
1506 }
1507 }
1508 if (!empty($newlang)) {
1509 $outputlangs = new Translate("", $conf);
1510 $outputlangs->setDefaultLang($newlang);
1511 }
1512
1513 $sql = "SELECT rowid";
1514 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1515 if ($id) {
1516 $sql .= " WHERE fk_soc = ".((int) $id);
1517 }
1518 if ($companybankid) {
1519 $sql .= " AND rowid = ".((int) $companybankid);
1520 }
1521
1522 $i = 0;
1523 $accounts = array();
1524
1525 $result = $this->db->query($sql);
1526 if ($result) {
1527 if ($this->db->num_rows($result) == 0) {
1528 throw new RestException(404, 'Bank account not found');
1529 }
1530
1531 $num = $this->db->num_rows($result);
1532 while ($i < $num) {
1533 $obj = $this->db->fetch_object($result);
1534
1535 $account = new CompanyBankAccount($this->db);
1536 if ($account->fetch($obj->rowid)) {
1537 $accounts[] = $account;
1538 }
1539 $i++;
1540 }
1541 } else {
1542 throw new RestException(500, 'Sql error '.$this->db->lasterror());
1543 }
1544
1545 $moreparams = array(
1546 'use_companybankid' => $accounts[0]->id,
1547 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1548 );
1549
1550 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1551
1552 if ($result > 0) {
1553 return array("success" => $result);
1554 } else {
1555 throw new RestException(500, 'Error generating the document '.$this->company->error);
1556 }
1557 }
1558
1571 public function getSocieteAccounts($id, $site = null)
1572 {
1573 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1574 throw new RestException(403);
1575 }
1576
1577 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1578 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1579 }
1580
1584 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1585 $sql .= " WHERE fk_soc = ".((int) $id);
1586 if ($site) {
1587 $sql .= " AND site ='".$this->db->escape($site)."'";
1588 }
1589
1590 $result = $this->db->query($sql);
1591
1592 if ($result && $this->db->num_rows($result) == 0) {
1593 throw new RestException(404, 'This thirdparty does not have any account attached or does not exist.');
1594 }
1595
1596 $i = 0;
1597
1598 $accounts = array();
1599
1600 $num = $this->db->num_rows($result);
1601 while ($i < $num) {
1602 $obj = $this->db->fetch_object($result);
1603 $account = new SocieteAccount($this->db);
1604
1605 if ($account->fetch($obj->rowid)) {
1606 $accounts[] = $account;
1607 }
1608 $i++;
1609 }
1610
1611 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1612
1613 $returnAccounts = array();
1614
1615 foreach ($accounts as $account) {
1616 $object = array();
1617 foreach ($account as $key => $value) {
1618 if (in_array($key, $fields)) {
1619 $object[$key] = $value;
1620 }
1621 }
1622 $returnAccounts[] = $object;
1623 }
1624
1625 return $returnAccounts;
1626 }
1627
1647 public function createSocieteAccount($id, $request_data = null)
1648 {
1649 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1650 throw new RestException(403);
1651 }
1652
1653 if (!isset($request_data['site'])) {
1654 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1655 }
1656
1657 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1658 $result = $this->db->query($sql);
1659
1660 if ($result && $this->db->num_rows($result) == 0) {
1661 $account = new SocieteAccount($this->db);
1662 if (!isset($request_data['login'])) {
1663 $account->login = "";
1664 }
1665 $account->fk_soc = $id;
1666
1667 foreach ($request_data as $field => $value) {
1668 if ($field === 'caller') {
1669 // 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
1670 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1671 continue;
1672 }
1673
1674 $account->$field = $this->_checkValForAPI($field, $value, $account);
1675 }
1676
1677 if ($account->create(DolibarrApiAccess::$user) < 0) {
1678 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1679 }
1680
1681 $this->_cleanObjectDatas($account);
1682
1683 return $account;
1684 } else {
1685 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1686 }
1687 }
1688
1711 public function putSocieteAccount($id, $site, $request_data = null)
1712 {
1713 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1714 throw new RestException(403);
1715 }
1716
1717 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1718 $result = $this->db->query($sql);
1719
1720 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1721 if ($result && $this->db->num_rows == 0) {
1722 if (!isset($request_data['key_account'])) {
1723 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1724 }
1725 $account = new SocieteAccount($this->db);
1726 if (!isset($request_data['login'])) {
1727 $account->login = "";
1728 }
1729
1730 foreach ($request_data as $field => $value) {
1731 if ($field === 'caller') {
1732 // 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
1733 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1734 continue;
1735 }
1736
1737 $account->$field = $this->_checkValForAPI($field, $value, $account);
1738 }
1739
1740 $account->fk_soc = $id;
1741 $account->site = $site;
1742
1743 if ($account->create(DolibarrApiAccess::$user) < 0) {
1744 throw new RestException(500, 'Error creating SocieteAccount entity.');
1745 }
1746 // We found an existing SocieteAccount entity, we are replacing it
1747 } else {
1748 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1749 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1750 $result = $this->db->query($sql);
1751
1752 if ($result && $this->db->num_rows($result) !== 0) {
1753 throw new RestException(409, "You are trying to update this thirdparty Account for $site to ".$request_data['site']." but another Account already exists with this site key.");
1754 }
1755 }
1756
1757 $obj = $this->db->fetch_object($result);
1758
1759 $account = new SocieteAccount($this->db);
1760 $account->id = $obj->rowid;
1761 $account->fk_soc = $id;
1762 $account->site = $site;
1763 if (!isset($request_data['login'])) {
1764 $account->login = "";
1765 }
1766 $account->fk_user_creat = $obj->fk_user_creat;
1767 $account->date_creation = $obj->date_creation;
1768
1769 foreach ($request_data as $field => $value) {
1770 if ($field === 'caller') {
1771 // 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
1772 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1773 continue;
1774 }
1775
1776 $account->$field = $this->_checkValForAPI($field, $value, $account);
1777 }
1778
1779 if ($account->update(DolibarrApiAccess::$user) < 0) {
1780 throw new RestException(500, 'Error updating SocieteAccount entity.');
1781 }
1782 }
1783
1784 $this->_cleanObjectDatas($account);
1785
1786 return $account;
1787 }
1788
1805 public function patchSocieteAccount($id, $site, $request_data = null)
1806 {
1807 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1808 throw new RestException(403);
1809 }
1810
1811 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1812 $result = $this->db->query($sql);
1813
1814 if ($result && $this->db->num_rows($result) == 0) {
1815 throw new RestException(404, "This thirdparty does not have $site account attached or does not exist.");
1816 } else {
1817 // If the user tries to edit the site member, we check first if
1818 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1819 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1820 $result = $this->db->query($sql);
1821
1822 if ($result && $this->db->num_rows($result) !== 0) {
1823 throw new RestException(409, "You are trying to update this thirdparty Account for ".$site." to ".$request_data['site']." but another Account already exists for this thirdparty with this site key.");
1824 }
1825 }
1826
1827 $obj = $this->db->fetch_object($result);
1828 $account = new SocieteAccount($this->db);
1829 $account->fetch($obj->rowid);
1830
1831 foreach ($request_data as $field => $value) {
1832 if ($field === 'caller') {
1833 // 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
1834 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1835 continue;
1836 }
1837
1838 $account->$field = $this->_checkValForAPI($field, $value, $account);
1839 }
1840
1841 if ($account->update(DolibarrApiAccess::$user) < 0) {
1842 throw new RestException(500, 'Error updating SocieteAccount account');
1843 }
1844
1845 $this->_cleanObjectDatas($account);
1846
1847 return $account;
1848 }
1849 }
1850
1864 public function deleteSocieteAccount($id, $site)
1865 {
1866 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1867 throw new RestException(403);
1868 }
1869
1870 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1871 $result = $this->db->query($sql);
1872
1873 if ($result && $this->db->num_rows($result) == 0) {
1874 throw new RestException(404);
1875 } else {
1876 $obj = $this->db->fetch_object($result);
1877 $account = new SocieteAccount($this->db);
1878 $account->fetch($obj->rowid);
1879
1880 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1881 throw new RestException(500, "Error while deleting $site account attached to this third party");
1882 }
1883 }
1884 }
1885
1898 public function deleteSocieteAccounts($id)
1899 {
1900 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1901 throw new RestException(403);
1902 }
1903
1908 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1909 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1910
1911 $result = $this->db->query($sql);
1912
1913 if ($result && $this->db->num_rows($result) == 0) {
1914 throw new RestException(404, 'This third party does not have any account attached or does not exist.');
1915 } else {
1916 $i = 0;
1917
1918 $num = $this->db->num_rows($result);
1919 while ($i < $num) {
1920 $obj = $this->db->fetch_object($result);
1921 $account = new SocieteAccount($this->db);
1922 $account->fetch($obj->rowid);
1923
1924 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1925 throw new RestException(500, 'Error while deleting account attached to this third party');
1926 }
1927 $i++;
1928 }
1929 }
1930 }
1931
1932 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1939 protected function _cleanObjectDatas($object)
1940 {
1941 // phpcs:enable
1942 $object = parent::_cleanObjectDatas($object);
1943
1944 unset($object->nom); // ->name already defined and nom deprecated
1945 unset($object->name_bis); // ->name_alias already defined
1946 unset($object->note); // ->note_private and note_public already defined
1947 unset($object->departement);
1948 unset($object->departement_code);
1949 unset($object->pays);
1950 unset($object->particulier);
1951 unset($object->prefix_comm);
1952
1953 unset($object->siren);
1954 unset($object->siret);
1955 unset($object->ape);
1956
1957 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.
1958
1959 unset($object->total_ht);
1960 unset($object->total_tva);
1961 unset($object->total_localtax1);
1962 unset($object->total_localtax2);
1963 unset($object->total_ttc);
1964
1965 unset($object->lines);
1966 unset($object->thirdparty);
1967
1968 unset($object->fk_delivery_address); // deprecated feature
1969
1970 return $object;
1971 }
1972
1981 private function _validate($data)
1982 {
1983 $thirdparty = array();
1984 foreach (Thirdparties::$FIELDS as $field) {
1985 if (!isset($data[$field])) {
1986 throw new RestException(400, "$field field missing");
1987 }
1988 $thirdparty[$field] = $data[$field];
1989 }
1990 return $thirdparty;
1991 }
1992
2014 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
2015 {
2016 global $conf;
2017
2018 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
2019 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
2020 }
2021
2022 if ($rowid === 0) {
2023 $result = $this->company->initAsSpecimen();
2024 } else {
2025 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2026 }
2027 if (!$result) {
2028 throw new RestException(404, 'Thirdparty not found');
2029 }
2030
2031 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
2032 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
2033 }
2034 if (isModEnabled('mailing')) {
2035 $this->company->getNoEmail();
2036 }
2037
2038 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
2039 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be subtracted to payments only and not to total of final invoice
2040 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be subtracted to payments only and not to total of final invoice
2041 } else {
2042 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2043 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2044 }
2045
2046 $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
2047 $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
2048 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
2049 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
2050
2051 return $this->_cleanObjectDatas($this->company);
2052 }
2053}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:31
_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:96
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.