dolibarr 23.0.4
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-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
7 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
9 * Copyright (C) 2025 William Mead <william@m34d.com>
10 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
11 * Copyright (C) 2026 Benjamin Falière <benjamin@faliere.com>
12 * Copyright (C) 2026 Noé Cendrier <noe.cendrier@altairis.fr>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27
28use Luracast\Restler\RestException;
29
39{
43 public static $FIELDS = array(
44 'name'
45 );
46
50 public $company;
51
55 public function __construct()
56 {
57 global $db;
58 $this->db = $db;
59
60 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
61 require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
62 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
63 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
64 require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
65
66 $this->company = new Societe($this->db);
67
68 if (getDolGlobalString('SOCIETE_EMAIL_MANDATORY')) {
69 static::$FIELDS[] = 'email';
70 }
71 }
72
85 public function get($id)
86 {
87 return $this->_fetch($id);
88 }
89
106 public function getByEmail($email)
107 {
108 return $this->_fetch(null, '', '', '', '', '', '', '', '', '', $email);
109 }
110
125 public function getByBarcode($barcode)
126 {
127 return $this->_fetch(null, '', '', $barcode);
128 }
129
152 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
153 {
154 $obj_ret = array();
155
156 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
157 throw new RestException(403);
158 }
159
160 // case of external user, we force socids
161 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid : '';
162
163 // If the internal user must only see his customers, force searching by him
164 $search_sale = 0;
165 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
166 $search_sale = DolibarrApiAccess::$user->id;
167 }
168
169 $sql = "SELECT t.rowid";
170 $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
171 $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
172 if ($category > 0) {
173 if ($mode != 4) {
174 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
175 }
176 if (!in_array($mode, array(1, 2, 3))) {
177 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
178 }
179 }
180 $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
181 $sql .= " WHERE t.entity IN (".getEntity('societe').")";
182 $sql .= " AND t.fk_stcomm = st.id";
183 if ($mode == 1) {
184 $sql .= " AND t.client IN (1, 3)";
185 } elseif ($mode == 2) {
186 $sql .= " AND t.client IN (2, 3)";
187 } elseif ($mode == 3) {
188 $sql .= " AND t.client IN (0)";
189 } elseif ($mode == 4) {
190 $sql .= " AND t.fournisseur IN (1)";
191 }
192 // Select thirdparties of given category
193 if ($category > 0) {
194 if (!empty($mode) && $mode != 4) {
195 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
196 } elseif (!empty($mode) && $mode == 4) {
197 $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
198 } else {
199 $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))";
200 }
201 }
202 if ($socids) {
203 $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
204 }
205 // Search on sale representative
206 if ($search_sale && $search_sale != '-1') {
207 if ($search_sale == -2) {
208 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
209 } elseif ($search_sale > 0) {
210 $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).")";
211 }
212 }
213 // Add sql filters
214 if ($sqlfilters) {
215 $errormessage = '';
216 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
217 if ($errormessage) {
218 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
219 }
220 }
221
222 //this query will return total thirdparties with the filters given
223 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
224
225 $sql .= $this->db->order($sortfield, $sortorder);
226 if ($limit) {
227 if ($page < 0) {
228 $page = 0;
229 }
230 $offset = $limit * $page;
231
232 $sql .= $this->db->plimit($limit + 1, $offset);
233 }
234
235 $result = $this->db->query($sql);
236 if ($result) {
237 $num = $this->db->num_rows($result);
238 $min = min($num, ($limit <= 0 ? $num : $limit));
239 $i = 0;
240 while ($i < $min) {
241 $obj = $this->db->fetch_object($result);
242 $soc_static = new Societe($this->db);
243 if ($soc_static->fetch($obj->rowid)) {
244 if (isModEnabled('mailing')) {
245 $soc_static->getNoEmail();
246 }
247 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($soc_static), $properties);
248 }
249 $i++;
250 }
251 } else {
252 throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
253 }
254 if (!count($obj_ret)) {
255 $message = '';
256 switch ($mode) {
257 case 0:
258 $message = 'No third parties found';
259 break;
260 case 1:
261 $message = 'No customers found';
262 break;
263 case 2:
264 $message = 'No prospects found';
265 break;
266 case 3:
267 $message = 'No other third parties found';
268 break;
269 case 4:
270 $message = 'No suppliers found';
271 }
272 throw new RestException(404, $message);
273 }
274
275 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
276 if ($pagination_data) {
277 $totalsResult = $this->db->query($sqlTotals);
278 $total = $this->db->fetch_object($totalsResult)->total;
279
280 $tmp = $obj_ret;
281 $obj_ret = [];
282
283 $obj_ret['data'] = $tmp;
284 $obj_ret['pagination'] = [
285 'total' => (int) $total,
286 'page' => $page, //count starts from 0
287 'page_count' => ceil((int) $total / $limit),
288 'limit' => $limit
289 ];
290 }
291
292 return $obj_ret;
293 }
294
307 public function post($request_data = null)
308 {
309 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
310 throw new RestException(403);
311 }
312
313 // External api user does not know internal country ID
314 if (!isset($request_data['country_id']) && isset($request_data['country_code'])) {
315 $field = strlen($request_data['country_code']) > 2 ? 'code_iso' : 'code';
316 $id = dol_getIdFromCode($this->db, $request_data['country_code'], "c_country", $field, "rowid");
317 if ($id < 0) {
318 throw new RestException(404, 'Country code not found in database: ' . $this->db->error);
319 }
320 $request_data['country_id'] = $id;
321 }
322
323 // Check mandatory fields
324 $result = $this->_validate($request_data);
325
326 foreach ($request_data as $field => $value) {
327 if ($field === 'caller') {
328 // 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
329 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
330 continue;
331 }
332 if ($field == 'array_options' && is_array($value)) {
333 foreach ($value as $index => $val) {
334 $this->company->array_options[$index] = $this->_checkValForAPI('extrafields', $val, $this->company);
335 }
336 continue;
337 }
338
339 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
340 }
341
342 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
343 throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
344 }
345 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
346 $this->company->setNoEmail($this->company->no_email);
347 }
348
349 return $this->company->id;
350 }
351
369 public function put($id, $request_data = null)
370 {
371 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
372 throw new RestException(403);
373 }
374
375 $result = $this->company->fetch($id);
376 if (!$result) {
377 throw new RestException(404, 'Thirdparty not found');
378 }
379
380 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
381 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
382 }
383
384 foreach ($request_data as $field => $value) {
385 if ($field == 'id') {
386 continue;
387 }
388 if ($field === 'caller') {
389 // 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
390 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
391 continue;
392 }
393 if ($field == 'array_options' && is_array($value)) {
394 foreach ($value as $index => $val) {
395 $this->company->array_options[$index] = $this->_checkValForAPI($field, $val, $this->company);
396 }
397 continue;
398 }
399 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
400 }
401
402 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
403 $this->company->setNoEmail($this->company->no_email);
404 }
405
406 if ($this->company->update($id, DolibarrApiAccess::$user, 1, 1, 1, 'update', 1) > 0) {
407 return $this->get($id);
408 } else {
409 throw new RestException(500, $this->company->error);
410 }
411 }
412
434 public function merge($id, $idtodelete)
435 {
436 if ($id == $idtodelete) {
437 throw new RestException(400, 'Try to merge a thirdparty into itself');
438 }
439
440 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
441 throw new RestException(403);
442 }
443
444 $result = $this->company->fetch($id); // include the fetch of extra fields
445 if (!$result) {
446 throw new RestException(404, 'Thirdparty not found');
447 }
448
449 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
450 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
451 }
452
453 $companytoremove = new Societe($this->db);
454 $result = $companytoremove->fetch($idtodelete); // include the fetch of extra fields
455 if (!$result) {
456 throw new RestException(404, 'Thirdparty not found');
457 }
458
459 if (!DolibarrApi::_checkAccessToResource('societe', $companytoremove->id)) {
460 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
461 }
462
463 $user = DolibarrApiAccess::$user;
464 $result = $this->company->mergeCompany($companytoremove->id);
465 if ($result < 0) {
466 throw new RestException(500, 'Error failed to merged thirdparty '.$companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
467 }
468
469 return $this->get($id);
470 }
471
484 public function delete($id)
485 {
486 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
487 throw new RestException(403);
488 }
489 $result = $this->company->fetch($id);
490 if (!$result) {
491 throw new RestException(404, 'Thirdparty not found');
492 }
493 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
494 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 }
496 $this->company->oldcopy = clone $this->company; // @phan-suppress-current-line PhanTypeMismatchProperty
497
498 $res = $this->company->delete($id);
499 if ($res < 0) {
500 throw new RestException(500, "Can't delete, error occurs");
501 } elseif ($res == 0) {
502 throw new RestException(409, "Can't delete, that product is probably used");
503 }
504
505 return array(
506 'success' => array(
507 'code' => 200,
508 'message' => 'Object deleted'
509 )
510 );
511 }
512
530 public function setThirdpartyPriceLevel($id, $priceLevel)
531 {
532 global $conf;
533
534 if (!isModEnabled('societe')) {
535 throw new RestException(501, 'Module "Thirdparties" needed for this request');
536 }
537
538 if (!isModEnabled("product")) {
539 throw new RestException(501, 'Module "Products" needed for this request');
540 }
541
542 if (!getDolGlobalString('PRODUIT_MULTIPRICES') && !getDolGlobalString('PRODUIT_CUSTOMER_PRICES_AND_MULTIPRICES')) {
543 throw new RestException(501, 'Multiprices features activation needed for this request');
544 }
545
546 if ($priceLevel < 1 || $priceLevel > getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT')) {
547 throw new RestException(400, 'Price level must be between 1 and ' . getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT'));
548 }
549
550 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
551 throw new RestException(403, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
552 }
553
554 $result = $this->company->fetch($id);
555 if ($result < 0) {
556 throw new RestException(404, 'Thirdparty '.$id.' not found');
557 }
558
559 if (empty($result)) {
560 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
561 }
562
563 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
564 throw new RestException(403, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
565 }
566
567 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
568 if ($result <= 0) {
569 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
570 }
571
572 return $this->_cleanObjectDatas($this->company);
573 }
574
589 public function addRepresentative($id, $representative_id)
590 {
591 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
592 throw new RestException(403);
593 }
594 $result = $this->company->fetch($id);
595 if (!$result) {
596 throw new RestException(404, 'Thirdparty not found');
597 }
598 $usertmp = new User($this->db);
599 $result = $usertmp->fetch($representative_id);
600 if (!$result) {
601 throw new RestException(404, 'User not found');
602 }
603 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
604 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
605 }
606 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
607
608 return $result;
609 }
610
625 public function deleteRepresentative($id, $representative_id)
626 {
627 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
628 throw new RestException(403);
629 }
630 $result = $this->company->fetch($id);
631 if (!$result) {
632 throw new RestException(404, 'Thirdparty not found');
633 }
634 $usertmp = new User($this->db);
635 $result = $usertmp->fetch($representative_id);
636 if (!$result) {
637 throw new RestException(404, 'User not found');
638 }
639 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
640 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
641 }
642 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
643
644 return $result;
645 }
646
665 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
666 {
667 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
668 throw new RestException(403);
669 }
670
671 $result = $this->company->fetch($id);
672 if (!$result) {
673 throw new RestException(404, 'Thirdparty not found');
674 }
675
676 $categories = new Categorie($this->db);
677
678 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
679
680 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
681 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
682 }
683
684 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
685 return array();
686 }
687
688 return $arrayofcateg;
689 }
690
707 public function addCategory($id, $category_id)
708 {
709 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
710 throw new RestException(403);
711 }
712
713 $result = $this->company->fetch($id);
714 if (!$result) {
715 throw new RestException(404, 'Thirdparty not found');
716 }
717 $category = new Categorie($this->db);
718 $result = $category->fetch($category_id);
719 if (!$result) {
720 throw new RestException(404, 'category not found');
721 }
722
723 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
724 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
725 }
726 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
727 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
728 }
729
730 $category->add_type($this->company, 'customer');
731
732 return $this->_cleanObjectDatas($this->company);
733 }
734
751 public function deleteCategory($id, $category_id)
752 {
753 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
754 throw new RestException(403);
755 }
756
757 $result = $this->company->fetch($id);
758 if (!$result) {
759 throw new RestException(404, 'Thirdparty not found');
760 }
761 $category = new Categorie($this->db);
762 $result = $category->fetch($category_id);
763 if (!$result) {
764 throw new RestException(404, 'category not found');
765 }
766
767 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
768 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 }
770 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
771 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
772 }
773
774 $category->del_type($this->company, 'customer');
775
776 return $this->_cleanObjectDatas($this->company);
777 }
778
798 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
799 {
800 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
801 throw new RestException(403);
802 }
803
804 $result = $this->company->fetch($id);
805 if (!$result) {
806 throw new RestException(404, 'Thirdparty not found');
807 }
808
809 $categories = new Categorie($this->db);
810
811 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
812
813 if (is_numeric($result) && $result < 0) {
814 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
815 }
816
817 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
818 return array();
819 }
820
821 return $result;
822 }
823
840 public function addSupplierCategory($id, $category_id)
841 {
842 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
843 throw new RestException(403);
844 }
845
846 $result = $this->company->fetch($id);
847 if (!$result) {
848 throw new RestException(404, 'Thirdparty not found');
849 }
850 $category = new Categorie($this->db);
851 $result = $category->fetch($category_id);
852 if (!$result) {
853 throw new RestException(404, 'category not found');
854 }
855
856 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
857 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
858 }
859 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
860 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
861 }
862
863 $category->add_type($this->company, 'supplier');
864
865 return $this->_cleanObjectDatas($this->company);
866 }
867
884 public function deleteSupplierCategory($id, $category_id)
885 {
886 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
887 throw new RestException(403);
888 }
889
890 $result = $this->company->fetch($id);
891 if (!$result) {
892 throw new RestException(404, 'Thirdparty not found');
893 }
894 $category = new Categorie($this->db);
895 $result = $category->fetch($category_id);
896 if (!$result) {
897 throw new RestException(404, 'category not found');
898 }
899
900 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
901 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
902 }
903 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
904 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
905 }
906
907 $category->del_type($this->company, 'supplier');
908
909 return $this->_cleanObjectDatas($this->company);
910 }
911
912
931 public function getOutStandingProposals($id, $mode = 'customer')
932 {
933 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
934 throw new RestException(403);
935 }
936
937 if (empty($id)) {
938 throw new RestException(400, 'Thirdparty ID is mandatory');
939 }
940
941 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
942 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
943 }
944
945 $result = $this->company->fetch($id);
946 if (!$result) {
947 throw new RestException(404, 'Thirdparty not found');
948 }
949
950 $result = $this->company->getOutstandingProposals($mode);
951
952 unset($result['total_ht']);
953 unset($result['total_ttc']);
954
955 return $result;
956 }
957
958
977 public function getOutStandingOrder($id, $mode = 'customer')
978 {
979 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
980 throw new RestException(403);
981 }
982
983 if (empty($id)) {
984 throw new RestException(400, 'Thirdparty ID is mandatory');
985 }
986
987 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
988 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
989 }
990
991 $result = $this->company->fetch($id);
992 if (!$result) {
993 throw new RestException(404, 'Thirdparty not found');
994 }
995
996 $result = $this->company->getOutstandingOrders($mode);
997
998 unset($result['total_ht']);
999 unset($result['total_ttc']);
1000
1001 return $result;
1002 }
1003
1022 public function getOutStandingInvoices($id, $mode = 'customer')
1023 {
1024 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1025 throw new RestException(403);
1026 }
1027
1028 if (empty($id)) {
1029 throw new RestException(400, 'Thirdparty ID is mandatory');
1030 }
1031
1032 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1033 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1034 }
1035
1036 $result = $this->company->fetch($id);
1037 if (!$result) {
1038 throw new RestException(404, 'Thirdparty not found');
1039 }
1040
1041 $result = $this->company->getOutstandingBills($mode);
1042
1043 unset($result['total_ht']);
1044 unset($result['total_ttc']);
1045
1046 return $result;
1047 }
1048
1067 public function getSalesRepresentatives($id, $mode = 0)
1068 {
1069 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1070 throw new RestException(403);
1071 }
1072
1073 if (empty($id)) {
1074 throw new RestException(400, 'Thirdparty ID is mandatory');
1075 }
1076
1077 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1078 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1079 }
1080
1081 $result = $this->company->fetch($id);
1082 if ($result < 1) {
1083 throw new RestException(404, 'Thirdparty not found');
1084 }
1085
1086 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1087
1088 return $result;
1089 }
1090
1115 public function getFixedAmountDiscounts($id, $mode = 'customer', $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1116 {
1117 $obj_ret = array();
1118
1119 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1120 throw new RestException(403);
1121 }
1122
1123 if (empty($id)) {
1124 throw new RestException(400, 'Thirdparty ID is mandatory');
1125 }
1126
1127 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1128 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1129 }
1130
1131 $result = $this->company->fetch($id);
1132 if (!$result) {
1133 throw new RestException(404, 'Thirdparty not found');
1134 }
1135
1136 $sql = '';
1137 if ($mode === 'customer') {
1138 $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";
1139 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1140 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = re.fk_facture_source";
1141 $sql .= " WHERE re.fk_soc = ".((int) $id);
1142 if ($filter == "available") {
1143 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1144 }
1145 if ($filter == "used") {
1146 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1147 }
1148 } elseif ($mode === 'supplier') {
1149 $sql = "SELECT f.ref, f.type as factype, re.fk_invoice_supplier_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_invoice_supplier, re.fk_invoice_supplier_line";
1150 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1151 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1152 $sql .= " WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int) $id);
1153 if ($filter == "available") {
1154 $sql .= " AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1155 }
1156 if ($filter == "used") {
1157 $sql .= " AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1158 }
1159 }
1160
1161 $sql .= $this->db->order($sortfield, $sortorder);
1162
1163 $result = $this->db->query($sql);
1164 if (!$result) {
1165 throw new RestException(503, $this->db->lasterror());
1166 } else {
1167 //$num = $this->db->num_rows($result);
1168 while ($obj = $this->db->fetch_object($result)) {
1169 $obj_ret[] = $obj;
1170 }
1171 }
1172
1173 return $obj_ret;
1174 }
1175
1199 public function splitdiscount($id, $discountid, $amount_ttc_1, $amount_ttc_2)
1200 {
1201 $obj_ret = array();
1202
1203 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer') || !DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1204 throw new RestException(403);
1205 }
1206
1207 if (empty($id)) {
1208 throw new RestException(400, 'Thirdparty ID is mandatory');
1209 }
1210 if (empty($discountid)) {
1211 throw new RestException(400, 'Discount ID is mandatory');
1212 }
1213 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1214 throw new RestException(400, 'Amount are mandatory');
1215 }
1216
1217 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1218 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1219 }
1220
1221 $result = $this->company->fetch($id);
1222 if (!$result) {
1223 throw new RestException(404, 'Thirdparty not found');
1224 }
1225 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
1226 $discount = new DiscountAbsolute($this->db);
1227 $res = $discount->fetch($discountid);
1228 if (!($res > 0)) {
1229 throw new RestException(404, 'Discount not found');
1230 }
1231 if ($discount->socid != $id) {
1232 throw new RestException(405, 'Discount not owned by this thirdpartie');
1233 }
1234
1235 if ( price2num((float) $amount_ttc_1 + (float) $amount_ttc_2) != $discount->amount_ttc) {
1236 throw new RestException(405, 'Sum of the 2 discounts is different that the original discount');
1237 }
1238 if ($discount->fk_facture_line) {
1239 throw new RestException(409, 'Discount is already used');
1240 }
1241
1242 $newdiscount1 = new DiscountAbsolute($this->db);
1243 $newdiscount2 = new DiscountAbsolute($this->db);
1244
1245 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1246 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1247 $newdiscount1->fk_facture = $discount->fk_facture;
1248 $newdiscount2->fk_facture = $discount->fk_facture;
1249 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1250 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1251 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1252 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1253 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1254 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1255 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1256 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1257 if ($discount->description == '(CREDIT_NOTE)' || $discount->description == '(DEPOSIT)') {
1258 $newdiscount1->description = $discount->description;
1259 $newdiscount2->description = $discount->description;
1260 } else {
1261 $newdiscount1->description = $discount->description.' (1)';
1262 $newdiscount2->description = $discount->description.' (2)';
1263 }
1264
1265 $newdiscount1->fk_user = $discount->fk_user;
1266 $newdiscount2->fk_user = $discount->fk_user;
1267 $newdiscount1->fk_soc = $discount->fk_soc;
1268 $newdiscount1->socid = $discount->socid;
1269 $newdiscount2->fk_soc = $discount->fk_soc;
1270 $newdiscount2->socid = $discount->socid;
1271 $newdiscount1->discount_type = $discount->discount_type;
1272 $newdiscount2->discount_type = $discount->discount_type;
1273 $newdiscount1->datec = $discount->datec;
1274 $newdiscount2->datec = $discount->datec;
1275 $newdiscount1->tva_tx = $discount->tva_tx;
1276 $newdiscount2->tva_tx = $discount->tva_tx;
1277 $newdiscount1->vat_src_code = $discount->vat_src_code;
1278 $newdiscount2->vat_src_code = $discount->vat_src_code;
1279 $newdiscount1->amount_ttc = $amount_ttc_1;
1280 $newdiscount2->amount_ttc = price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1281 $newdiscount1->amount_ht = price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
1282 $newdiscount2->amount_ht = price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
1283 $newdiscount1->amount_tva = price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1284 $newdiscount2->amount_tva = price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1285
1286 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1287 $newdiscount2->multicurrency_amount_ttc = price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1288 $newdiscount1->multicurrency_amount_ht = price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
1289 $newdiscount2->multicurrency_amount_ht = price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
1290 $newdiscount1->multicurrency_amount_tva = price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1291 $newdiscount2->multicurrency_amount_tva = price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1292
1293 // DiscountAbsolute->amount_ttc ->amount_ht ->amount_tva are marked as @deprecated but seems to yet be in use so we fill ->amout_xxx and ->total_xxx
1294 // the same for multicurrency_amount_xxx and multicurrency_total_xxx
1295 $newdiscount1->total_ttc = (float) price2num($newdiscount1->amount_ttc);
1296 $newdiscount1->total_ht = (float) price2num($newdiscount1->amount_ht);
1297 $newdiscount1->total_tva = (float) price2num($newdiscount1->amount_tva);
1298 $newdiscount2->total_ttc = (float) price2num($newdiscount2->amount_ttc);
1299 $newdiscount2->total_ht = (float) price2num($newdiscount2->amount_ht);
1300 $newdiscount2->total_tva = (float) price2num($newdiscount2->amount_tva);
1301 $newdiscount1->multicurrency_total_ttc = (float) price2num($newdiscount1->multicurrency_amount_ttc);
1302 $newdiscount1->multicurrency_total_ht = (float) price2num($newdiscount1->multicurrency_amount_ht);
1303 $newdiscount1->multicurrency_total_tva = (float) price2num($newdiscount1->multicurrency_amount_tva);
1304 $newdiscount2->multicurrency_total_ttc = (float) price2num($newdiscount2->multicurrency_amount_ttc);
1305 $newdiscount2->multicurrency_total_ht = (float) price2num($newdiscount2->multicurrency_amount_ht);
1306 $newdiscount2->multicurrency_total_tva = (float) price2num($newdiscount2->multicurrency_amount_tva);
1307
1308 $this->db->begin();
1309
1310 $discount->fk_facture_source = 0; // This is to delete only the require record (that we will recreate with two records) and not all family with same fk_facture_source
1311 // This is to delete only the require record (that we will recreate with two records) and not all family with same fk_invoice_supplier_source
1312 $discount->fk_invoice_supplier_source = 0;
1313 $res = $discount->delete(DolibarrApiAccess::$user);
1314 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1315 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1316 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1317 $this->db->rollback();
1318 throw new RestException(500, 'Operation fail');
1319 }
1320
1321 $this->db->commit();
1322
1323 $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";
1324 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1325 $sql .= " WHERE re.rowid IN ( $newid1, $newid2 ) AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1326
1327 $sql .= $this->db->order("f.type", "ASC");
1328
1329 $result = $this->db->query($sql);
1330 if (!$result) {
1331 throw new RestException(503, $this->db->lasterror());
1332 } else {
1333 // $num = $this->db->num_rows($result);
1334 while ($obj = $this->db->fetch_object($result)) {
1335 $obj_ret[] = $obj;
1336 }
1337 }
1338
1339 return $obj_ret;
1340 }
1341
1342
1361 {
1362 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1363 throw new RestException(403);
1364 }
1365 if (empty($id)) {
1366 throw new RestException(400, 'Thirdparty ID is mandatory');
1367 }
1368
1369 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1370 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1371 }
1372
1373 /*$result = $this->thirdparty->fetch($id);
1374 if( ! $result ) {
1375 throw new RestException(404, 'Thirdparty not found');
1376 }*/
1377
1378 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1379 $invoice = new Facture($this->db);
1380 $result = $invoice->list_replacable_invoices($id);
1381 if ($result < 0) {
1382 throw new RestException(405, $invoice->error);
1383 }
1384
1385 return $result;
1386 }
1387
1410 {
1411 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1412 throw new RestException(403);
1413 }
1414 if (empty($id)) {
1415 throw new RestException(400, 'Thirdparty ID is mandatory');
1416 }
1417
1418 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1419 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1420 }
1421
1422 /*$result = $this->thirdparty->fetch($id);
1423 if( ! $result ) {
1424 throw new RestException(404, 'Thirdparty not found');
1425 }*/
1426
1427 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1428 $invoice = new Facture($this->db);
1429 $result = $invoice->list_qualified_avoir_invoices($id);
1430 if (!is_array($result) && $result < 0) {
1431 throw new RestException(405, $invoice->error);
1432 }
1433
1434 return $result;
1435 }
1436
1453 {
1454 if (empty($id)) {
1455 throw new RestException(400, 'Thirdparty ID is mandatory');
1456 }
1457 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1458 throw new RestException(403);
1459 }
1460 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1461 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1462 }
1463
1468 $sql = "SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1469 $sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1470 if ($id) {
1471 $sql .= " WHERE fk_soc = ".((int) $id);
1472 }
1473
1474 $result = $this->db->query($sql);
1475 if ($this->db->num_rows($result) == 0) {
1476 throw new RestException(404, 'Notification not found');
1477 }
1478
1479 $i = 0;
1480
1481 $notifications = array();
1482
1483 if ($result) {
1484 $i = 0;
1485 $num = $this->db->num_rows($result);
1486 //$min = min($num, ($limit <= 0 ? $num : $limit));
1487 $min = $num;
1488 while ($i < $min) {
1489 $obj = $this->db->fetch_object($result);
1490 $notifications[] = $obj;
1491 $i++;
1492 }
1493 } else {
1494 throw new RestException(404, 'No notifications found');
1495 }
1496
1497 $fields = array('id', 'socid', 'event', 'contact_id', 'datec', 'tms', 'type');
1498
1499 $returnNotifications = array();
1500
1501 foreach ($notifications as $notification) {
1502 $object = array();
1503 foreach ($notification as $key => $value) {
1504 if (in_array($key, $fields)) {
1505 $object[$key] = $value;
1506 }
1507 }
1508 $returnNotifications[] = $object;
1509 }
1510
1511 // Too complex for phan ?: @phan-suppress-next-line PhanTypeMismatchReturn
1512 return $returnNotifications;
1513 }
1514
1531 public function createCompanyNotification($id, $request_data = null)
1532 {
1533 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1534 throw new RestException(403, "User has no right to update thirdparties");
1535 }
1536 if ($this->company->fetch($id) <= 0) {
1537 throw new RestException(404, 'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1538 }
1539 $notification = new Notify($this->db);
1540
1541 $notification->socid = $id;
1542
1543 foreach ($request_data as $field => $value) {
1544 $notification->$field = $value;
1545 }
1546
1547 $event = $notification->event;
1548 if (!$event) {
1549 throw new RestException(500, 'Error creating Thirdparty Notification, request_data missing event');
1550 }
1551 $socid = $notification->socid;
1552 $contact_id = $notification->contact_id;
1553
1554 $exists_sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1555 $exists_sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1556 $exists_sql .= " WHERE fk_action = '".$this->db->escape((string) $event)."'";
1557 $exists_sql .= " AND fk_soc = '".$this->db->escape((string) $socid)."'";
1558 $exists_sql .= " AND fk_contact = '".$this->db->escape((string) $contact_id)."'";
1559
1560 $exists_result = $this->db->query($exists_sql);
1561 if ($this->db->num_rows($exists_result) > 0) {
1562 throw new RestException(403, 'Notification already exists');
1563 }
1564
1565 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1566 throw new RestException(500, 'Error creating Thirdparty Notification');
1567 }
1568
1569 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1570 throw new RestException(500, 'Error updating values');
1571 }
1572
1573 return $this->_cleanObjectDatas($notification);
1574 }
1575
1594 public function createCompanyNotificationByCode($id, $code, $request_data = null)
1595 {
1596 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1597 throw new RestException(403, "User has no right to update thirdparties");
1598 }
1599 if ($this->company->fetch($id) <= 0) {
1600 throw new RestException(404, 'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1601 }
1602 $notification = new Notify($this->db);
1603 $notification->socid = $id;
1604
1605 $sql = "SELECT t.rowid as id FROM ".MAIN_DB_PREFIX."c_action_trigger as t";
1606 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
1607
1608 $result = $this->db->query($sql);
1609 if ($this->db->num_rows($result) == 0) {
1610 throw new RestException(404, 'Action Trigger code not found');
1611 }
1612
1613 $notification->event = $this->db->fetch_row($result)[0];
1614 foreach ($request_data as $field => $value) {
1615 if ($field === 'event') {
1616 throw new RestException(500, 'Error creating Thirdparty Notification, request_data contains event key');
1617 }
1618 if ($field === 'fk_action') {
1619 throw new RestException(500, 'Error creating Thirdparty Notification, request_data contains fk_action key');
1620 }
1621 $notification->$field = $value;
1622 }
1623
1624 $event = $notification->event;
1625 $socid = $notification->socid;
1626 $contact_id = $notification->contact_id;
1627
1628 $exists_sql = "SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1629 $exists_sql .= " FROM ".MAIN_DB_PREFIX."notify_def";
1630 $exists_sql .= " WHERE fk_action = '".$this->db->escape((string) $event)."'";
1631 $exists_sql .= " AND fk_soc = '".$this->db->escape((string) $socid)."'";
1632 $exists_sql .= " AND fk_contact = '".$this->db->escape((string) $contact_id)."'";
1633
1634 $exists_result = $this->db->query($exists_sql);
1635 if ($this->db->num_rows($exists_result) > 0) {
1636 throw new RestException(403, 'Notification already exists');
1637 }
1638
1639 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1640 throw new RestException(500, 'Error creating Thirdparty Notification, are request_data well formed?');
1641 }
1642
1643 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1644 throw new RestException(500, 'Error updating values');
1645 }
1646
1647 return $this->_cleanObjectDatas($notification);
1648 }
1649
1664 public function deleteCompanyNotification($id, $notification_id)
1665 {
1666 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1667 throw new RestException(403);
1668 }
1669
1670 $notification = new Notify($this->db);
1671
1672 $notification->fetch($notification_id);
1673
1674 $socid = (int) $notification->socid;
1675
1676 if ($socid == $id) {
1677 return $notification->delete(DolibarrApiAccess::$user);
1678 } else {
1679 throw new RestException(403, "Not allowed due to bad consistency of input data");
1680 }
1681 }
1682
1700 public function updateCompanyNotification($id, $notification_id, $request_data = null)
1701 {
1702 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1703 throw new RestException(403, "User has no right to update thirdparties");
1704 }
1705 if ($this->company->fetch($id) <= 0) {
1706 throw new RestException(404, 'Error creating Company Notification, Company doesn\'t exists');
1707 }
1708 $notification = new Notify($this->db);
1709
1710 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1711 $notification->fetch($notification_id, $id);
1712
1713 if ($notification->socid != $id) {
1714 throw new RestException(403, "Not allowed due to bad consistency of input data");
1715 }
1716
1717 foreach ($request_data as $field => $value) {
1718 $notification->$field = $value;
1719 }
1720
1721 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1722 throw new RestException(500, 'Error updating values');
1723 }
1724
1725 return $this->_cleanObjectDatas($notification);
1726 }
1727
1744 {
1745 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1746 throw new RestException(403);
1747 }
1748 if (empty($id)) {
1749 throw new RestException(400, 'Thirdparty ID is mandatory');
1750 }
1751
1752 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1753 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1754 }
1755
1760 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1761 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1762 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1763 if ($id) {
1764 $sql .= " WHERE fk_soc = ".((int) $id);
1765 }
1766
1767 $result = $this->db->query($sql);
1768
1769 if ($this->db->num_rows($result) == 0) {
1770 throw new RestException(404, 'Account not found');
1771 }
1772
1773 $i = 0;
1774
1775 $accounts = array();
1776
1777 if ($result) {
1778 $i = 0;
1779 $num = $this->db->num_rows($result);
1780 //$min = min($num, ($limit <= 0 ? $num : $limit));
1781 $min = $num;
1782 while ($i < $min) {
1783 $obj = $this->db->fetch_object($result);
1784
1785 $account = new CompanyBankAccount($this->db);
1786 if ($account->fetch($obj->rowid)) {
1787 $accounts[] = $account;
1788 }
1789 $i++;
1790 }
1791 } else {
1792 throw new RestException(404, 'Account not found');
1793 }
1794
1795
1796 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1797
1798 $returnAccounts = array();
1799
1800 foreach ($accounts as $account) {
1801 $object = array();
1802 foreach ($account as $key => $value) {
1803 if (in_array($key, $fields)) {
1804 if ($key == 'iban') {
1805 $object[$key] = dolDecrypt($value);
1806 } else {
1807 $object[$key] = $value;
1808 }
1809 }
1810 }
1811 $returnAccounts[] = $object;
1812 }
1813
1814 return $returnAccounts;
1815 }
1816
1833 public function createCompanyBankAccount($id, $request_data = null)
1834 {
1835 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1836 throw new RestException(403);
1837 }
1838 if ($this->company->fetch($id) <= 0) {
1839 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1840 }
1841 $account = new CompanyBankAccount($this->db);
1842
1843 $account->socid = $id;
1844
1845 foreach ($request_data as $field => $value) {
1846 if ($field === 'caller') {
1847 // 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
1848 $this->company->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1849 continue;
1850 }
1851
1852 $account->$field = $this->_checkValForAPI('extrafields', $value, $account);
1853 }
1854
1855 if ($account->create(DolibarrApiAccess::$user) < 0) {
1856 throw new RestException(500, 'Error creating Company Bank account');
1857 }
1858
1859 if (empty($account->rum)) {
1860 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1861 $prelevement = new BonPrelevement($this->db);
1862 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (string) $account->id);
1863 $account->date_rum = dol_now();
1864 }
1865
1866 if ($account->update(DolibarrApiAccess::$user) < 0) {
1867 throw new RestException(500, 'Error updating values');
1868 }
1869
1870 return $this->_cleanObjectDatas($account);
1871 }
1872
1890 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1891 {
1892 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1893 throw new RestException(403);
1894 }
1895 if ($this->company->fetch($id) <= 0) {
1896 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1897 }
1898 $account = new CompanyBankAccount($this->db);
1899
1900 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1901 $account->fetch($bankaccount_id, '', $id, -1, '');
1902
1903 if ($account->socid != $id) {
1904 throw new RestException(403);
1905 }
1906
1907
1908 foreach ($request_data as $field => $value) {
1909 if ($field === 'caller') {
1910 // 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
1911 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
1912 continue;
1913 }
1914
1915 $account->$field = $this->_checkValForAPI($field, $value, $account);
1916 }
1917
1918 if (empty($account->rum)) {
1919 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1920 $prelevement = new BonPrelevement($this->db);
1921 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (string) $account->id);
1922 $account->date_rum = dol_now();
1923 }
1924
1925 if ($account->update(DolibarrApiAccess::$user) < 0) {
1926 throw new RestException(500, 'Error updating values');
1927 }
1928
1929 return $this->_cleanObjectDatas($account);
1930 }
1931
1946 public function deleteCompanyBankAccount($id, $bankaccount_id)
1947 {
1948 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1949 throw new RestException(403);
1950 }
1951
1952 $account = new CompanyBankAccount($this->db);
1953
1954 $account->fetch($bankaccount_id);
1955
1956 $socid = (int) $account->socid;
1957
1958 if ($socid == $id) {
1959 return $account->delete(DolibarrApiAccess::$user);
1960 } else {
1961 throw new RestException(403, "Not allowed due to bad consistency of input data");
1962 }
1963 }
1964
1983 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1984 {
1985 global $conf, $langs;
1986
1987 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1988
1989 if ($this->company->fetch($id) <= 0) {
1990 throw new RestException(404, 'Thirdparty not found');
1991 }
1992
1993 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
1994 throw new RestException(403);
1995 }
1996
1997 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1998
1999 $this->company->fk_bank = $this->company->fk_account;
2000 // $this->company->fk_account = $this->company->fk_account;
2001
2002 $outputlangs = $langs;
2003 $newlang = '';
2004
2005 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
2006 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
2007 if (isset($this->company->thirdparty->default_lang)) {
2008 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
2009 } elseif (isset($this->company->default_lang)) {
2010 $newlang = $this->company->default_lang; // for thirdparty
2011 }
2012 }
2013 if (!empty($newlang)) {
2014 $outputlangs = new Translate("", $conf);
2015 $outputlangs->setDefaultLang($newlang);
2016 }
2017
2018 $sql = "SELECT rowid";
2019 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
2020 if ($id) {
2021 $sql .= " WHERE fk_soc = ".((int) $id);
2022 }
2023 if ($companybankid) {
2024 $sql .= " AND rowid = ".((int) $companybankid);
2025 }
2026
2027 $i = 0;
2028 $accounts = array();
2029
2030 $result = $this->db->query($sql);
2031 if ($result) {
2032 if ($this->db->num_rows($result) == 0) {
2033 throw new RestException(404, 'Bank account not found');
2034 }
2035
2036 $num = $this->db->num_rows($result);
2037 //$min = min($num, ($limit <= 0 ? $num : $limit));
2038 $min = $num;
2039 while ($i < $min) {
2040 $obj = $this->db->fetch_object($result);
2041
2042 $account = new CompanyBankAccount($this->db);
2043 if ($account->fetch($obj->rowid)) {
2044 $accounts[] = $account;
2045 }
2046 $i++;
2047 }
2048 } else {
2049 throw new RestException(500, 'Sql error '.$this->db->lasterror());
2050 }
2051
2052 $moreparams = array(
2053 'use_companybankid' => $accounts[0]->id,
2054 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName((string) $this->company->id)
2055 );
2056
2057 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2058
2059 if ($result > 0) {
2060 return array("success" => $result);
2061 } else {
2062 throw new RestException(500, 'Error generating the document '.$this->company->error);
2063 }
2064 }
2065
2083 public function getSocieteAccounts($id, $site = null)
2084 {
2085 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
2086 throw new RestException(403);
2087 }
2088
2089 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
2090 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
2091 }
2092
2096 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
2097 $sql .= " WHERE fk_soc = ".((int) $id);
2098 if ($site) {
2099 $sql .= " AND site ='".$this->db->escape($site)."'";
2100 }
2101
2102 $result = $this->db->query($sql);
2103
2104 if ($result && $this->db->num_rows($result) == 0) {
2105 throw new RestException(404, 'This thirdparty does not have any account attached or does not exist.');
2106 }
2107
2108 $i = 0;
2109
2110 $accounts = array();
2111
2112 $i = 0;
2113 $num = $this->db->num_rows($result);
2114 //$min = min($num, ($limit <= 0 ? $num : $limit));
2115 $min = $num;
2116 while ($i < $min) {
2117 $obj = $this->db->fetch_object($result);
2118 $account = new SocieteAccount($this->db);
2119
2120 if ($account->fetch($obj->rowid)) {
2121 $accounts[] = $account;
2122 }
2123 $i++;
2124 }
2125
2126 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
2127
2128 $returnAccounts = array();
2129
2130 foreach ($accounts as $account) {
2131 $object = array();
2132 foreach ($account as $key => $value) {
2133 if (in_array($key, $fields)) {
2134 $object[$key] = $value;
2135 }
2136 }
2137 $returnAccounts[] = $object;
2138 }
2139
2140 return $returnAccounts;
2141 }
2142
2160 public function getSocieteByAccounts($site, $key_account)
2161 {
2162 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
2163 throw new RestException(403);
2164 }
2165
2166 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
2167 $sql .= " WHERE site = '".$this->db->escape($site)."' AND key_account = '".$this->db->escape($key_account)."'";
2168 $sql .= " AND entity IN (".getEntity('societe').")";
2169
2170 $result = $this->db->query($sql);
2171
2172 if ($result && $this->db->num_rows($result) == 1) {
2173 $obj = $this->db->fetch_object($result);
2174 $returnThirdparty = $this->_fetch($obj->fk_soc);
2175 } else {
2176 throw new RestException(404, 'This account have many thirdparties attached or does not exist.');
2177 }
2178
2179 if (!DolibarrApi::_checkAccessToResource('societe', $returnThirdparty->id)) {
2180 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
2181 }
2182
2183 return $returnThirdparty;
2184 }
2185
2209 public function createSocieteAccount($id, $request_data = null)
2210 {
2211 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2212 throw new RestException(403);
2213 }
2214
2215 if (!isset($request_data['site'])) {
2216 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
2217 }
2218
2219 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
2220 $result = $this->db->query($sql);
2221
2222 if ($result && $this->db->num_rows($result) == 0) {
2223 $account = new SocieteAccount($this->db);
2224 if (!isset($request_data['login'])) {
2225 $account->login = "";
2226 }
2227 $account->fk_soc = $id;
2228
2229 foreach ($request_data as $field => $value) {
2230 if ($field === 'caller') {
2231 // 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
2232 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2233 continue;
2234 }
2235
2236 $account->$field = $this->_checkValForAPI($field, $value, $account);
2237 }
2238
2239 if ($account->create(DolibarrApiAccess::$user) < 0) {
2240 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2241 }
2242
2243 $this->_cleanObjectDatas($account);
2244
2245 return $account;
2246 } else {
2247 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
2248 }
2249 }
2250
2277 public function postSocieteAccount($id, $site, $request_data = null)
2278 {
2279 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2280 throw new RestException(403);
2281 }
2282
2283 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
2284 $result = $this->db->query($sql);
2285
2286 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
2287 if ($result && $this->db->num_rows($result) == 0) {
2288 if (!isset($request_data['key_account'])) {
2289 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2290 }
2291 $account = new SocieteAccount($this->db);
2292 if (!isset($request_data['login'])) {
2293 $account->login = "";
2294 }
2295
2296 foreach ($request_data as $field => $value) {
2297 if ($field === 'caller') {
2298 // 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
2299 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2300 continue;
2301 }
2302
2303 $account->$field = $this->_checkValForAPI($field, $value, $account);
2304 }
2305
2306 $account->fk_soc = $id;
2307 $account->site = $site;
2308
2309 if ($account->create(DolibarrApiAccess::$user) < 0) {
2310 throw new RestException(500, 'Error creating SocieteAccount entity.');
2311 }
2312 // We found an existing SocieteAccount entity, we are replacing it
2313 } else {
2314 if (isset($request_data['site']) && $request_data['site'] !== $site) {
2315 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
2316 $result = $this->db->query($sql);
2317
2318 if ($result && $this->db->num_rows($result) !== 0) {
2319 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.");
2320 }
2321 }
2322
2323 $obj = $this->db->fetch_object($result);
2324
2325 $account = new SocieteAccount($this->db);
2326 $account->id = $obj->rowid;
2327 $account->fk_soc = $id;
2328 $account->site = $site;
2329 if (!isset($request_data['login'])) {
2330 $account->login = "";
2331 }
2332 $account->fk_user_creat = $obj->fk_user_creat;
2333 $account->date_creation = $obj->date_creation;
2334
2335 foreach ($request_data as $field => $value) {
2336 if ($field === 'caller') {
2337 // 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
2338 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2339 continue;
2340 }
2341
2342 $account->$field = $this->_checkValForAPI($field, $value, $account);
2343 }
2344
2345 if ($account->update(DolibarrApiAccess::$user) < 0) {
2346 throw new RestException(500, 'Error updating SocieteAccount entity.');
2347 }
2348 }
2349
2350 $this->_cleanObjectDatas($account);
2351
2352 return $account;
2353 }
2354
2375 public function putSocieteAccount($id, $site, $request_data = null)
2376 {
2377 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2378 throw new RestException(403);
2379 }
2380
2381 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
2382 $result = $this->db->query($sql);
2383
2384 if ($result && $this->db->num_rows($result) == 0) {
2385 throw new RestException(404, "This thirdparty does not have $site account attached or does not exist.");
2386 } else {
2387 // If the user tries to edit the site member, we check first if
2388 if (isset($request_data['site']) && $request_data['site'] !== $site) {
2389 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
2390 $result = $this->db->query($sql);
2391
2392 if ($result && $this->db->num_rows($result) !== 0) {
2393 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.");
2394 }
2395 }
2396
2397 $obj = $this->db->fetch_object($result);
2398 $account = new SocieteAccount($this->db);
2399 $account->fetch($obj->rowid);
2400
2401 foreach ($request_data as $field => $value) {
2402 if ($field === 'caller') {
2403 // 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
2404 $account->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
2405 continue;
2406 }
2407
2408 $account->$field = $this->_checkValForAPI($field, $value, $account);
2409 }
2410
2411 if ($account->update(DolibarrApiAccess::$user) < 0) {
2412 throw new RestException(500, 'Error updating SocieteAccount account');
2413 }
2414
2415 $this->_cleanObjectDatas($account);
2416
2417 return $account;
2418 }
2419 }
2420
2439 public function deleteSocieteAccount($id, $site)
2440 {
2441 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2442 throw new RestException(403);
2443 }
2444
2445 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
2446 $result = $this->db->query($sql);
2447
2448 if ($result && $this->db->num_rows($result) == 0) {
2449 throw new RestException(404);
2450 } else {
2451 $obj = $this->db->fetch_object($result);
2452 $account = new SocieteAccount($this->db);
2453 $account->fetch($obj->rowid);
2454
2455 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2456 throw new RestException(500, "Error while deleting $site account attached to this third party");
2457 }
2458 }
2459 }
2460
2477 {
2478 if (!DolibarrApiAccess::$user->hasRight('societe', 'creer')) {
2479 throw new RestException(403);
2480 }
2481
2486 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2487 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
2488
2489 $result = $this->db->query($sql);
2490
2491 if ($result && $this->db->num_rows($result) == 0) {
2492 throw new RestException(404, 'This third party does not have any account attached or does not exist.');
2493 } else {
2494 $i = 0;
2495
2496 $i = 0;
2497 $num = $this->db->num_rows($result);
2498 //$min = min($num, ($limit <= 0 ? $num : $limit));
2499 $min = $num;
2500 while ($i < $min) {
2501 $obj = $this->db->fetch_object($result);
2502 $account = new SocieteAccount($this->db);
2503 $account->fetch($obj->rowid);
2504
2505 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2506 throw new RestException(500, 'Error while deleting account attached to this third party');
2507 }
2508 $i++;
2509 }
2510 }
2511 }
2512
2513 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2523 protected function _cleanObjectDatas($object)
2524 {
2525 // phpcs:enable
2526 $object = parent::_cleanObjectDatas($object);
2527
2528 unset($object->nom); // ->name already defined and nom deprecated
2529 unset($object->name_bis); // ->name_alias already defined
2530 unset($object->note); // ->note_private and note_public already defined
2531 unset($object->departement);
2532 unset($object->departement_code);
2533 unset($object->pays);
2534 unset($object->particulier);
2535 unset($object->prefix_comm);
2536
2537 unset($object->siren);
2538 unset($object->siret);
2539 unset($object->ape);
2540
2541 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.
2542
2543 unset($object->total_ht);
2544 unset($object->total_tva);
2545 unset($object->total_localtax1);
2546 unset($object->total_localtax2);
2547 unset($object->total_ttc);
2548
2549 unset($object->lines);
2550 unset($object->thirdparty);
2551
2552 unset($object->fk_delivery_address); // deprecated feature
2553
2554 return $object;
2555 }
2556
2565 private function _validate($data)
2566 {
2567 if ($data === null) {
2568 $data = array();
2569 }
2570 $thirdparty = array();
2571 foreach (Thirdparties::$FIELDS as $field) {
2572 if (!isset($data[$field])) {
2573 throw new RestException(400, "$field field missing");
2574 }
2575 $thirdparty[$field] = $data[$field];
2576 }
2577 return $thirdparty;
2578 }
2579
2603 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
2604 {
2605 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
2606 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
2607 }
2608
2609 if ($rowid === 0) {
2610 $result = $this->company->initAsSpecimen();
2611 } else {
2612 $result = $this->company->fetch((int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2613 }
2614 if (!$result) {
2615 throw new RestException(404, 'Thirdparty not found');
2616 }
2617
2618 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
2619 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
2620 }
2621 if (isModEnabled('mailing')) {
2622 $this->company->getNoEmail();
2623 }
2624
2625 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
2626 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be subtracted to payments only and not to total of final invoice
2627 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be subtracted to payments only and not to total of final invoice
2628 } else {
2629 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2630 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2631 }
2632
2633 $absolute_discount = $this->company->getAvailableDiscounts(null, $filterabsolutediscount);
2634 $absolute_creditnote = $this->company->getAvailableDiscounts(null, $filtercreditnote);
2635 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
2636 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
2637
2638 return $this->_cleanObjectDatas($this->company);
2639 }
2640}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
Class to manage absolute discounts.
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:98
Class to manage invoices.
Class to manage the table of subscription to notifications.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
updateCompanyNotification($id, $notification_id, $request_data=null)
Update a company notification for a third party.
setThirdpartyPriceLevel($id, $priceLevel)
Set a new price level for the given third party.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getSocieteByAccounts($site, $key_account)
Get a specific third party by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a third party.
deleteCompanyNotification($id, $notification_id)
Delete a company notification attached to a third party.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a third party.
getOutStandingOrder($id, $mode='customer')
Get outstanding orders for a third party.
addRepresentative($id, $representative_id)
Add a customer representative to a third party.
getByBarcode($barcode)
Get a third party by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a document from a bank account record.
createCompanyNotificationByCode($id, $code, $request_data=null)
Create a company notification for a third party using action trigger code.
getCompanyNotification($id)
Get company notifications for a third party.
addCategory($id, $category_id)
Add a customer category to a third party.
getCompanyBankAccount($id)
Get company bank accounts of a third party.
getInvoicesQualifiedForReplacement($id)
Return invoices qualified to be replaced by another invoice.
post($request_data=null)
Create a third party.
put($id, $request_data=null)
Update third party.
getByEmail($email)
Get properties of a third party by email.
_validate($data)
Validate fields before create or update object.
getFixedAmountDiscounts($id, $mode='customer', $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a third party.
addSupplierCategory($id, $category_id)
Add a supplier category to a third party.
merge($id, $idtodelete)
Merge a third party into another third party.
deleteSocieteAccounts($id)
Delete all accounts attached to a third party.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a third party.
postSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account for a third party.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the third party.
deleteRepresentative($id, $representative_id)
Remove the link between a customer representative and a third party.
createCompanyNotification($id, $request_data=null)
Create a company notification for a third party.
putSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a third party.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update a company bank account of a third party.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a third party.
getInvoicesQualifiedForCreditNote($id)
Return invoices qualified to be corrected by a credit note.
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals for a third party.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of a third party.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices for a third party.
splitdiscount($id, $discountid, $amount_ttc_1, $amount_ttc_2)
Split a discount in 2 smaller discount.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List third parties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a third party.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing third party.
createCompanyBankAccount($id, $request_data=null)
Create a company bank account for a third party.
deleteCategory($id, $category_id)
Remove the link between a customer category and the third party.
Class to manage translations.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.