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