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