dolibarr 19.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-2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2026 Benjamin Falière <benjamin@faliere.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24use Luracast\Restler\RestException;
25
34{
39 public static $FIELDS = array(
40 'name'
41 );
42
46 public $company;
47
51 public function __construct()
52 {
53 global $db;
54 $this->db = $db;
55
56 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
57 require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
58 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
59 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
60
61 $this->company = new Societe($this->db);
62
63 if (getDolGlobalString('SOCIETE_EMAIL_MANDATORY')) {
64 static::$FIELDS[] = 'email';
65 }
66 }
67
78 public function get($id)
79 {
80 return $this->_fetch($id);
81 }
82
95 public function getByEmail($email)
96 {
97 return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
98 }
99
112 public function getByBarcode($barcode)
113 {
114 return $this->_fetch('', '', '', $barcode);
115 }
116
135 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '', $properties = '')
136 {
137 $obj_ret = array();
138
139 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
140 throw new RestException(401);
141 }
142
143 // case of external user, we force socids
144 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
145
146 // If the internal user must only see his customers, force searching by him
147 $search_sale = 0;
148 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
149 $search_sale = DolibarrApiAccess::$user->id;
150 }
151
152 $sql = "SELECT t.rowid";
153 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
154 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
155 }
156 $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
157 $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
158 if ($category > 0) {
159 if ($mode != 4) {
160 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
161 }
162 if (!in_array($mode, array(1, 2, 3))) {
163 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
164 }
165 }
166 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
167 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
168 }
169 $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
170 $sql .= " WHERE t.entity IN (".getEntity('societe').")";
171 $sql .= " AND t.fk_stcomm = st.id";
172
173 if ($mode == 1) {
174 $sql .= " AND t.client IN (1, 3)";
175 } elseif ($mode == 2) {
176 $sql .= " AND t.client IN (2, 3)";
177 } elseif ($mode == 3) {
178 $sql .= " AND t.client IN (0)";
179 } elseif ($mode == 4) {
180 $sql .= " AND t.fournisseur IN (1)";
181 }
182
183 // Select thirdparties of given category
184 if ($category > 0) {
185 if (!empty($mode) && $mode != 4) {
186 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
187 } elseif (!empty($mode) && $mode == 4) {
188 $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
189 } else {
190 $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))";
191 }
192 }
193
194 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
195 $sql .= " AND t.rowid = sc.fk_soc";
196 }
197 //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
198 if ($socids) {
199 $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
200 }
201 if ($search_sale > 0) {
202 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
203 }
204 // Insert sale filter
205 if ($search_sale > 0) {
206 $sql .= " AND sc.fk_user = ".((int) $search_sale);
207 }
208 // Add sql filters
209 if ($sqlfilters) {
210 $errormessage = '';
211 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
212 if ($errormessage) {
213 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
214 }
215 }
216
217 $sql .= $this->db->order($sortfield, $sortorder);
218
219 if ($limit) {
220 if ($page < 0) {
221 $page = 0;
222 }
223 $offset = $limit * $page;
224
225 $sql .= $this->db->plimit($limit + 1, $offset);
226 }
227
228 $result = $this->db->query($sql);
229 if ($result) {
230 $num = $this->db->num_rows($result);
231 $min = min($num, ($limit <= 0 ? $num : $limit));
232 $i = 0;
233 while ($i < $min) {
234 $obj = $this->db->fetch_object($result);
235 $soc_static = new Societe($this->db);
236 if ($soc_static->fetch($obj->rowid)) {
237 if (isModEnabled('mailing')) {
238 $soc_static->getNoEmail();
239 }
240 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($soc_static), $properties);
241 }
242 $i++;
243 }
244 } else {
245 throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
246 }
247 if (!count($obj_ret)) {
248 throw new RestException(404, 'Thirdparties not found');
249 }
250 return $obj_ret;
251 }
252
259 public function post($request_data = null)
260 {
261 if (!DolibarrApiAccess::$user->rights->societe->creer) {
262 throw new RestException(401);
263 }
264 // Check mandatory fields
265 $result = $this->_validate($request_data);
266
267 foreach ($request_data as $field => $value) {
268 if ($field === 'caller') {
269 // 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 whith the caller
270 $this->company->context['caller'] = $request_data['caller'];
271 continue;
272 }
273
274 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
275 }
276
277 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
279 }
280 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
281 $this->company->setNoEmail($this->company->no_email);
282 }
283
284 return $this->company->id;
285 }
286
297 public function put($id, $request_data = null)
298 {
299 if (!DolibarrApiAccess::$user->rights->societe->creer) {
300 throw new RestException(401);
301 }
302
303 $result = $this->company->fetch($id);
304 if (!$result) {
305 throw new RestException(404, 'Thirdparty not found');
306 }
307
308 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
309 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
310 }
311
312 foreach ($request_data as $field => $value) {
313 if ($field == 'id') {
314 continue;
315 }
316 if ($field === 'caller') {
317 // 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 whith the caller
318 $this->company->context['caller'] = $request_data['caller'];
319 continue;
320 }
321
322 if ($field == 'array_options' && is_array($value)) {
323 foreach ($value as $index => $val) {
324 $this->company->array_options[$index] = $this->_checkValForAPI($field, $val, $this->company);
325 }
326 continue;
327 }
328 $this->company->$field = $this->_checkValForAPI($field, $value, $this->company);
329 }
330
331 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
332 $this->company->setNoEmail($this->company->no_email);
333 }
334
335 if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update', 1) > 0) {
336 return $this->get($id);
337 } else {
338 throw new RestException(500, $this->company->error);
339 }
340 }
341
356 public function merge($id, $idtodelete)
357 {
358 global $user;
359
360 $error = 0;
361
362 if ($id == $idtodelete) {
363 throw new RestException(400, 'Try to merge a thirdparty into itself');
364 }
365
366 if (!DolibarrApiAccess::$user->rights->societe->creer) {
367 throw new RestException(401);
368 }
369
370 $result = $this->company->fetch($id); // include the fetch of extra fields
371 if (!$result) {
372 throw new RestException(404, 'Thirdparty not found');
373 }
374
375 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
376 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
377 }
378
379 $companytoremove = new Societe($this->db);
380 $result = $companytoremove->fetch($idtodelete); // include the fetch of extra fields
381 if (!$result) {
382 throw new RestException(404, 'Thirdparty not found');
383 }
384
385 if (!DolibarrApi::_checkAccessToResource('societe', $companytoremove->id)) {
386 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 }
388
389 $user = DolibarrApiAccess::$user;
390 $result = $this->company->mergeCompany($companytoremove->id);
391 if ($result < 0) {
392 throw new RestException(500, 'Error failed to merged thirdparty '.$companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
393 }
394
395 return $this->get($id);
396 }
397
404 public function delete($id)
405 {
406 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
407 throw new RestException(401);
408 }
409 $result = $this->company->fetch($id);
410 if (!$result) {
411 throw new RestException(404, 'Thirdparty not found');
412 }
413 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
414 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
415 }
416 $this->company->oldcopy = clone $this->company;
417
418 $res = $this->company->delete($id);
419 if ($res < 0) {
420 throw new RestException(500, "Can't delete, error occurs");
421 } elseif ($res == 0) {
422 throw new RestException(409, "Can't delete, that product is probably used");
423 }
424
425 return array(
426 'success' => array(
427 'code' => 200,
428 'message' => 'Object deleted'
429 )
430 );
431 }
432
448 public function setThirdpartyPriceLevel($id, $priceLevel)
449 {
450 global $conf;
451
452 if (!isModEnabled('societe')) {
453 throw new RestException(501, 'Module "Thirdparties" needed for this request');
454 }
455
456 if (!isModEnabled("product")) {
457 throw new RestException(501, 'Module "Products" needed for this request');
458 }
459
460 if (!getDolGlobalString('PRODUIT_MULTIPRICES')) {
461 throw new RestException(501, 'Multiprices features activation needed for this request');
462 }
463
464 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
465 throw new RestException(400, 'Price level must be between 1 and ' . getDolGlobalString('PRODUIT_MULTIPRICES_LIMIT'));
466 }
467
468 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
469 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
470 }
471
472 $result = $this->company->fetch($id);
473 if ($result < 0) {
474 throw new RestException(404, 'Thirdparty '.$id.' not found');
475 }
476
477 if (empty($result)) {
478 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
479 }
480
481 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
482 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
483 }
484
485 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
486 if ($result <= 0) {
487 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
488 }
489
490 return $this->_cleanObjectDatas($this->company);
491 }
492
505 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
506 {
507 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
508 throw new RestException(401);
509 }
510
511 $result = $this->company->fetch($id);
512 if (!$result) {
513 throw new RestException(404, 'Thirdparty not found');
514 }
515
516 $categories = new Categorie($this->db);
517
518 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
519
520 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
521 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
522 }
523
524 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
525 return array();
526 }
527
528 return $arrayofcateg;
529 }
530
540 public function addCategory($id, $category_id)
541 {
542 if (!DolibarrApiAccess::$user->rights->societe->creer) {
543 throw new RestException(401);
544 }
545
546 $result = $this->company->fetch($id);
547 if (!$result) {
548 throw new RestException(404, 'Thirdparty not found');
549 }
550 $category = new Categorie($this->db);
551 $result = $category->fetch($category_id);
552 if (!$result) {
553 throw new RestException(404, 'category not found');
554 }
555
556 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
557 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
558 }
559 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
560 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
561 }
562
563 $category->add_type($this->company, 'customer');
564
565 return $this->_cleanObjectDatas($this->company);
566 }
567
578 public function deleteCategory($id, $category_id)
579 {
580 if (!DolibarrApiAccess::$user->rights->societe->creer) {
581 throw new RestException(401);
582 }
583
584 $result = $this->company->fetch($id);
585 if (!$result) {
586 throw new RestException(404, 'Thirdparty not found');
587 }
588 $category = new Categorie($this->db);
589 $result = $category->fetch($category_id);
590 if (!$result) {
591 throw new RestException(404, 'category not found');
592 }
593
594 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
595 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
596 }
597 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
598 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
599 }
600
601 $category->del_type($this->company, 'customer');
602
603 return $this->_cleanObjectDatas($this->company);
604 }
605
619 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
620 {
621 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
622 throw new RestException(401);
623 }
624
625 $result = $this->company->fetch($id);
626 if (!$result) {
627 throw new RestException(404, 'Thirdparty not found');
628 }
629
630 $categories = new Categorie($this->db);
631
632 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
633
634 if (is_numeric($result) && $result < 0) {
635 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
636 }
637
638 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
639 return array();
640 }
641
642 return $result;
643 }
644
655 public function addSupplierCategory($id, $category_id)
656 {
657 if (!DolibarrApiAccess::$user->rights->societe->creer) {
658 throw new RestException(401);
659 }
660
661 $result = $this->company->fetch($id);
662 if (!$result) {
663 throw new RestException(404, 'Thirdparty not found');
664 }
665 $category = new Categorie($this->db);
666 $result = $category->fetch($category_id);
667 if (!$result) {
668 throw new RestException(404, 'category not found');
669 }
670
671 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
672 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
673 }
674 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
675 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 }
677
678 $category->add_type($this->company, 'supplier');
679
680 return $this->_cleanObjectDatas($this->company);
681 }
682
693 public function deleteSupplierCategory($id, $category_id)
694 {
695 if (!DolibarrApiAccess::$user->rights->societe->creer) {
696 throw new RestException(401);
697 }
698
699 $result = $this->company->fetch($id);
700 if (!$result) {
701 throw new RestException(404, 'Thirdparty not found');
702 }
703 $category = new Categorie($this->db);
704 $result = $category->fetch($category_id);
705 if (!$result) {
706 throw new RestException(404, 'category not found');
707 }
708
709 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
710 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
711 }
712 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
713 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
714 }
715
716 $category->del_type($this->company, 'supplier');
717
718 return $this->_cleanObjectDatas($this->company);
719 }
720
721
736 public function getOutStandingProposals($id, $mode = 'customer')
737 {
738 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
739 throw new RestException(401);
740 }
741
742 if (empty($id)) {
743 throw new RestException(400, 'Thirdparty ID is mandatory');
744 }
745
746 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
747 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
748 }
749
750 $result = $this->company->fetch($id);
751 if (!$result) {
752 throw new RestException(404, 'Thirdparty not found');
753 }
754
755 $result = $this->company->getOutstandingProposals($mode);
756
757 unset($result['total_ht']);
758 unset($result['total_ttc']);
759
760 return $result;
761 }
762
763
778 public function getOutStandingOrder($id, $mode = 'customer')
779 {
780 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
781 throw new RestException(401);
782 }
783
784 if (empty($id)) {
785 throw new RestException(400, 'Thirdparty ID is mandatory');
786 }
787
788 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
789 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
790 }
791
792 $result = $this->company->fetch($id);
793 if (!$result) {
794 throw new RestException(404, 'Thirdparty not found');
795 }
796
797 $result = $this->company->getOutstandingOrders($mode);
798
799 unset($result['total_ht']);
800 unset($result['total_ttc']);
801
802 return $result;
803 }
804
819 public function getOutStandingInvoices($id, $mode = 'customer')
820 {
821 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
822 throw new RestException(401);
823 }
824
825 if (empty($id)) {
826 throw new RestException(400, 'Thirdparty ID is mandatory');
827 }
828
829 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
830 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
831 }
832
833 $result = $this->company->fetch($id);
834 if (!$result) {
835 throw new RestException(404, 'Thirdparty not found');
836 }
837
838 $result = $this->company->getOutstandingBills($mode);
839
840 unset($result['total_ht']);
841 unset($result['total_ttc']);
842
843 return $result;
844 }
845
860 public function getSalesRepresentatives($id, $mode = 0)
861 {
862 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
863 throw new RestException(401);
864 }
865
866 if (empty($id)) {
867 throw new RestException(400, 'Thirdparty ID is mandatory');
868 }
869
870 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
871 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
872 }
873
874 $result = $this->company->fetch($id);
875 if (!$result) {
876 throw new RestException(404, 'Thirdparty not found');
877 }
878
879 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
880
881 return $result;
882 }
883
901 public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
902 {
903 $obj_ret = array();
904
905 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
906 throw new RestException(401);
907 }
908
909 if (empty($id)) {
910 throw new RestException(400, 'Thirdparty ID is mandatory');
911 }
912
913 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
914 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
915 }
916
917 $result = $this->company->fetch($id);
918 if (!$result) {
919 throw new RestException(404, 'Thirdparty not found');
920 }
921
922
923 $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";
924 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
925 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = re.fk_facture_source";
926 $sql .= " WHERE re.fk_soc = ".((int) $id);
927 if ($filter == "available") {
928 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
929 }
930 if ($filter == "used") {
931 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
932 }
933
934 $sql .= $this->db->order($sortfield, $sortorder);
935
936 $result = $this->db->query($sql);
937 if (!$result) {
938 throw new RestException(503, $this->db->lasterror());
939 } else {
940 $num = $this->db->num_rows($result);
941 while ($obj = $this->db->fetch_object($result)) {
942 $obj_ret[] = $obj;
943 }
944 }
945
946 return $obj_ret;
947 }
948
949
950
965 {
966 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
967 throw new RestException(401);
968 }
969 if (empty($id)) {
970 throw new RestException(400, 'Thirdparty ID is mandatory');
971 }
972
973 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
974 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
975 }
976
977 /*$result = $this->thirdparty->fetch($id);
978 if( ! $result ) {
979 throw new RestException(404, 'Thirdparty not found');
980 }*/
981
982 $invoice = new Facture($this->db);
983 $result = $invoice->list_replacable_invoices($id);
984 if ($result < 0) {
985 throw new RestException(405, $invoice->error);
986 }
987
988 return $result;
989 }
990
1008 {
1009 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1010 throw new RestException(401);
1011 }
1012 if (empty($id)) {
1013 throw new RestException(400, 'Thirdparty ID is mandatory');
1014 }
1015
1016 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1017 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1018 }
1019
1020 /*$result = $this->thirdparty->fetch($id);
1021 if( ! $result ) {
1022 throw new RestException(404, 'Thirdparty not found');
1023 }*/
1024
1025 $invoice = new Facture($this->db);
1026 $result = $invoice->list_qualified_avoir_invoices($id);
1027 if ($result < 0) {
1028 throw new RestException(405, $invoice->error);
1029 }
1030
1031 return $result;
1032 }
1033
1043 public function getCompanyBankAccount($id)
1044 {
1045 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1046 throw new RestException(401);
1047 }
1048 if (empty($id)) {
1049 throw new RestException(400, 'Thirdparty ID is mandatory');
1050 }
1051
1052 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1053 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1054 }
1055
1060 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1061 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1062 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1063 if ($id) {
1064 $sql .= " WHERE fk_soc = ".((int) $id);
1065 }
1066
1067 $result = $this->db->query($sql);
1068
1069 if ($this->db->num_rows($result) == 0) {
1070 throw new RestException(404, 'Account not found');
1071 }
1072
1073 $i = 0;
1074
1075 $accounts = array();
1076
1077 if ($result) {
1078 $num = $this->db->num_rows($result);
1079 while ($i < $num) {
1080 $obj = $this->db->fetch_object($result);
1081 $account = new CompanyBankAccount($this->db);
1082 if ($account->fetch($obj->rowid)) {
1083 $accounts[] = $account;
1084 }
1085 $i++;
1086 }
1087 } else {
1088 throw new RestException(404, 'Account not found');
1089 }
1090
1091
1092 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1093
1094 $returnAccounts = array();
1095
1096 foreach ($accounts as $account) {
1097 $object = array();
1098 foreach ($account as $key => $value) {
1099 if (in_array($key, $fields)) {
1100 $object[$key] = $value;
1101 }
1102 }
1103 $returnAccounts[] = $object;
1104 }
1105
1106 return $returnAccounts;
1107 }
1108
1118 public function createCompanyBankAccount($id, $request_data = null)
1119 {
1120 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1121 throw new RestException(401);
1122 }
1123 if ($this->company->fetch($id) <= 0) {
1124 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1125 }
1126 $account = new CompanyBankAccount($this->db);
1127
1128 $account->socid = $id;
1129
1130 foreach ($request_data as $field => $value) {
1131 if ($field === 'caller') {
1132 // 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 whith the caller
1133 $this->company->context['caller'] = $request_data['caller'];
1134 continue;
1135 }
1136
1137 $account->$field = $value;
1138 }
1139
1140 if ($account->create(DolibarrApiAccess::$user) < 0) {
1141 throw new RestException(500, 'Error creating Company Bank account');
1142 }
1143
1144 if (empty($account->rum)) {
1145 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1146 $prelevement = new BonPrelevement($this->db);
1147 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1148 $account->date_rum = dol_now();
1149 }
1150
1151 if ($account->update(DolibarrApiAccess::$user) < 0) {
1152 throw new RestException(500, 'Error updating values');
1153 }
1154
1155 return $this->_cleanObjectDatas($account);
1156 }
1157
1169 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1170 {
1171 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1172 throw new RestException(401);
1173 }
1174 if ($this->company->fetch($id) <= 0) {
1175 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1176 }
1177 $account = new CompanyBankAccount($this->db);
1178
1179 $account->fetch($bankaccount_id, $id, -1, '');
1180
1181 if ($account->socid != $id) {
1182 throw new RestException(401);
1183 }
1184
1185
1186 foreach ($request_data as $field => $value) {
1187 if ($field === 'caller') {
1188 // 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 whith the caller
1189 $account->context['caller'] = $request_data['caller'];
1190 continue;
1191 }
1192
1193 $account->$field = $value;
1194 }
1195
1196 if (empty($account->rum)) {
1197 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1198 $prelevement = new BonPrelevement($this->db);
1199 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1200 $account->date_rum = dol_now();
1201 }
1202
1203 if ($account->update(DolibarrApiAccess::$user) < 0) {
1204 throw new RestException(500, 'Error updating values');
1205 }
1206
1207 return $this->_cleanObjectDatas($account);
1208 }
1209
1220 public function deleteCompanyBankAccount($id, $bankaccount_id)
1221 {
1222 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1223 throw new RestException(401);
1224 }
1225
1226 $account = new CompanyBankAccount($this->db);
1227
1228 $account->fetch($bankaccount_id);
1229
1230 if (!$account->socid == $id) {
1231 throw new RestException(401);
1232 }
1233
1234 return $account->delete(DolibarrApiAccess::$user);
1235 }
1236
1247 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1248 {
1249 global $conf, $langs;
1250
1251 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1252
1253 if ($this->company->fetch($id) <= 0) {
1254 throw new RestException(404, 'Thirdparty not found');
1255 }
1256
1257 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1258 throw new RestException(401);
1259 }
1260
1261 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1262
1263 $this->company->fk_bank = $this->company->fk_account;
1264 $this->company->fk_account = $this->company->fk_account;
1265
1266 $outputlangs = $langs;
1267 $newlang = '';
1268
1269 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1270 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1271 if (isset($this->company->thirdparty->default_lang)) {
1272 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1273 } elseif (isset($this->company->default_lang)) {
1274 $newlang = $this->company->default_lang; // for thirdparty
1275 }
1276 }
1277 if (!empty($newlang)) {
1278 $outputlangs = new Translate("", $conf);
1279 $outputlangs->setDefaultLang($newlang);
1280 }
1281
1282 $sql = "SELECT rowid";
1283 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1284 if ($id) {
1285 $sql .= " WHERE fk_soc = ".((int) $id);
1286 }
1287 if ($companybankid) {
1288 $sql .= " AND rowid = ".((int) $companybankid);
1289 }
1290
1291 $i = 0;
1292 $accounts = array();
1293
1294 $result = $this->db->query($sql);
1295 if ($result) {
1296 if ($this->db->num_rows($result) == 0) {
1297 throw new RestException(404, 'Bank account not found');
1298 }
1299
1300 $num = $this->db->num_rows($result);
1301 while ($i < $num) {
1302 $obj = $this->db->fetch_object($result);
1303
1304 $account = new CompanyBankAccount($this->db);
1305 if ($account->fetch($obj->rowid)) {
1306 $accounts[] = $account;
1307 }
1308 $i++;
1309 }
1310 } else {
1311 throw new RestException(500, 'Sql error '.$this->db->lasterror());
1312 }
1313
1314 $moreparams = array(
1315 'use_companybankid' => $accounts[0]->id,
1316 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1317 );
1318
1319 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1320
1321 if ($result > 0) {
1322 return array("success" => $result);
1323 } else {
1324 throw new RestException(500, 'Error generating the document '.$this->company->error);
1325 }
1326 }
1327
1340 public function getSocieteAccounts($id, $site = null)
1341 {
1342 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1343 throw new RestException(401);
1344 }
1345
1346 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1347 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1348 }
1349
1353 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1354 $sql .= " WHERE fk_soc = ".((int) $id);
1355 if ($site) {
1356 $sql .= " AND site ='".$this->db->escape($site)."'";
1357 }
1358
1359 $result = $this->db->query($sql);
1360
1361 if ($result && $this->db->num_rows($result) == 0) {
1362 throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1363 }
1364
1365 $i = 0;
1366
1367 $accounts = array();
1368
1369 $num = $this->db->num_rows($result);
1370 while ($i < $num) {
1371 $obj = $this->db->fetch_object($result);
1372 $account = new SocieteAccount($this->db);
1373
1374 if ($account->fetch($obj->rowid)) {
1375 $accounts[] = $account;
1376 }
1377 $i++;
1378 }
1379
1380 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1381
1382 $returnAccounts = array();
1383
1384 foreach ($accounts as $account) {
1385 $object = array();
1386 foreach ($account as $key => $value) {
1387 if (in_array($key, $fields)) {
1388 $object[$key] = $value;
1389 }
1390 }
1391 $returnAccounts[] = $object;
1392 }
1393
1394 return $returnAccounts;
1395 }
1396
1416 public function createSocieteAccount($id, $request_data = null)
1417 {
1418 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1419 throw new RestException(401);
1420 }
1421
1422 if (!isset($request_data['site'])) {
1423 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1424 }
1425
1426 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1427 $result = $this->db->query($sql);
1428
1429 if ($result && $this->db->num_rows($result) == 0) {
1430 $account = new SocieteAccount($this->db);
1431 if (!isset($request_data['login'])) {
1432 $account->login = "";
1433 }
1434 $account->fk_soc = $id;
1435
1436 foreach ($request_data as $field => $value) {
1437 if ($field === 'caller') {
1438 // 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 whith the caller
1439 $account->context['caller'] = $request_data['caller'];
1440 continue;
1441 }
1442
1443 $account->$field = $value;
1444 }
1445
1446 if ($account->create(DolibarrApiAccess::$user) < 0) {
1447 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1448 }
1449
1450 $this->_cleanObjectDatas($account);
1451
1452 return $account;
1453 } else {
1454 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1455 }
1456 }
1457
1480 public function putSocieteAccount($id, $site, $request_data = null)
1481 {
1482 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1483 throw new RestException(401);
1484 }
1485
1486 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1487 $result = $this->db->query($sql);
1488
1489 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1490 if ($result && $this->db->num_rows == 0) {
1491 if (!isset($request_data['key_account'])) {
1492 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1493 }
1494 $account = new SocieteAccount($this->db);
1495 if (!isset($request_data['login'])) {
1496 $account->login = "";
1497 }
1498
1499 foreach ($request_data as $field => $value) {
1500 if ($field === 'caller') {
1501 // 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 whith the caller
1502 $account->context['caller'] = $request_data['caller'];
1503 continue;
1504 }
1505
1506 $account->$field = $value;
1507 }
1508
1509 $account->fk_soc = $id;
1510 $account->site = $site;
1511
1512 if ($account->create(DolibarrApiAccess::$user) < 0) {
1513 throw new RestException(500, 'Error creating SocieteAccount entity.');
1514 }
1515 // We found an existing SocieteAccount entity, we are replacing it
1516 } else {
1517 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1518 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1519 $result = $this->db->query($sql);
1520
1521 if ($result && $this->db->num_rows($result) !== 0) {
1522 throw new RestException(409, "You are trying to update this thirdparty SocieteAccount (gateway record) from $site to ".$request_data['site']." but another SocieteAccount entity already exists with this site key.");
1523 }
1524 }
1525
1526 $obj = $this->db->fetch_object($result);
1527
1528 $account = new SocieteAccount($this->db);
1529 $account->id = $obj->rowid;
1530 $account->fk_soc = $id;
1531 $account->site = $site;
1532 if (!isset($request_data['login'])) {
1533 $account->login = "";
1534 }
1535 $account->fk_user_creat = $obj->fk_user_creat;
1536 $account->date_creation = $obj->date_creation;
1537
1538 foreach ($request_data as $field => $value) {
1539 if ($field === 'caller') {
1540 // 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 whith the caller
1541 $account->context['caller'] = $request_data['caller'];
1542 continue;
1543 }
1544
1545 $account->$field = $value;
1546 }
1547
1548 if ($account->update(DolibarrApiAccess::$user) < 0) {
1549 throw new RestException(500, 'Error updating SocieteAccount entity.');
1550 }
1551 }
1552
1553 $this->_cleanObjectDatas($account);
1554
1555 return $account;
1556 }
1557
1574 public function patchSocieteAccount($id, $site, $request_data = null)
1575 {
1576 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1577 throw new RestException(401);
1578 }
1579
1580 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1581 $result = $this->db->query($sql);
1582
1583 if ($result && $this->db->num_rows($result) == 0) {
1584 throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1585 } else {
1586 // If the user tries to edit the site member, we check first if
1587 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1588 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1589 $result = $this->db->query($sql);
1590
1591 if ($result && $this->db->num_rows($result) !== 0) {
1592 throw new RestException(409, "You are trying to update this thirdparty SocieteAccount (gateway record) site member from ".$site." to ".$request_data['site']." but another SocieteAccount entity already exists for this thirdparty with this site key.");
1593 }
1594 }
1595
1596 $obj = $this->db->fetch_object($result);
1597 $account = new SocieteAccount($this->db);
1598 $account->fetch($obj->rowid);
1599
1600 foreach ($request_data as $field => $value) {
1601 if ($field === 'caller') {
1602 // 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 whith the caller
1603 $account->context['caller'] = $request_data['caller'];
1604 continue;
1605 }
1606
1607 $account->$field = $value;
1608 }
1609
1610 if ($account->update(DolibarrApiAccess::$user) < 0) {
1611 throw new RestException(500, 'Error updating SocieteAccount account');
1612 }
1613
1614 $this->_cleanObjectDatas($account);
1615
1616 return $account;
1617 }
1618 }
1619
1633 public function deleteSocieteAccount($id, $site)
1634 {
1635 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1636 throw new RestException(401);
1637 }
1638
1639 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1640 $result = $this->db->query($sql);
1641
1642 if ($result && $this->db->num_rows($result) == 0) {
1643 throw new RestException(404);
1644 } else {
1645 $obj = $this->db->fetch_object($result);
1646 $account = new SocieteAccount($this->db);
1647 $account->fetch($obj->rowid);
1648
1649 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1650 throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1651 }
1652 }
1653 }
1654
1667 public function deleteSocieteAccounts($id)
1668 {
1669 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1670 throw new RestException(401);
1671 }
1672
1677 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1678 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1679
1680 $result = $this->db->query($sql);
1681
1682 if ($result && $this->db->num_rows($result) == 0) {
1683 throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1684 } else {
1685 $i = 0;
1686
1687 $num = $this->db->num_rows($result);
1688 while ($i < $num) {
1689 $obj = $this->db->fetch_object($result);
1690 $account = new SocieteAccount($this->db);
1691 $account->fetch($obj->rowid);
1692
1693 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1694 throw new RestException(500, 'Error while deleting gateways attached to this third party');
1695 }
1696 $i++;
1697 }
1698 }
1699 }
1700
1701 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1708 protected function _cleanObjectDatas($object)
1709 {
1710 // phpcs:enable
1711 $object = parent::_cleanObjectDatas($object);
1712
1713 unset($object->nom); // ->name already defined and nom deprecated
1714 unset($object->name_bis); // ->name_alias already defined
1715 unset($object->note); // ->note_private and note_public already defined
1716 unset($object->departement);
1717 unset($object->departement_code);
1718 unset($object->pays);
1719 unset($object->particulier);
1720 unset($object->prefix_comm);
1721
1722 unset($object->siren);
1723 unset($object->siret);
1724 unset($object->ape);
1725
1726 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.
1727
1728 unset($object->total_ht);
1729 unset($object->total_tva);
1730 unset($object->total_localtax1);
1731 unset($object->total_localtax2);
1732 unset($object->total_ttc);
1733
1734 unset($object->lines);
1735 unset($object->thirdparty);
1736
1737 unset($object->fk_delivery_address); // deprecated feature
1738
1739 return $object;
1740 }
1741
1750 private function _validate($data)
1751 {
1752 $thirdparty = array();
1753 foreach (Thirdparties::$FIELDS as $field) {
1754 if (!isset($data[$field])) {
1755 throw new RestException(400, "$field field missing");
1756 }
1757 $thirdparty[$field] = $data[$field];
1758 }
1759 return $thirdparty;
1760 }
1761
1783 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1784 {
1785 global $conf;
1786
1787 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1788 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
1789 }
1790
1791 if ($rowid === 0) {
1792 $result = $this->company->initAsSpecimen();
1793 } else {
1794 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1795 }
1796 if (!$result) {
1797 throw new RestException(404, 'Thirdparty not found');
1798 }
1799
1800 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1801 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
1802 }
1803 if (isModEnabled('mailing')) {
1804 $this->company->getNoEmail();
1805 }
1806
1807 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
1808 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1809 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1810 } else {
1811 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1812 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1813 }
1814
1815 $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1816 $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1817 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1818 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1819
1820 return $this->_cleanObjectDatas($this->company);
1821 }
1822}
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
Class for API REST v1.
Definition api.class.php:32
_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:99
Class to manage invoices.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
_cleanObjectDatas($object)
Clean sensible object datas.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
getOutStandingOrder($id, $mode='customer')
Get outstanding orders of thirdparty.
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
addCategory($id, $category_id)
Add a customer category to a thirdparty.
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
post($request_data=null)
Create thirdparty object.
put($id, $request_data=null)
Update thirdparty.
getByEmail($email)
Get properties of a thirdparty object by email.
_validate($data)
Validate fields before create or update object.
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
merge($id, $idtodelete)
Merge a thirdparty into another one.
deleteSocieteAccounts($id)
Delete all gateways attached to a thirdparty.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway id)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='')
List thirdparties.
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific gateway attached to a thirdparty.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers....
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals of thirdparty.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of thirdparty.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices of thirdparty.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
createSocieteAccount($id, $request_data=null)
Create and attach a new gateway to an existing thirdparty.
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
Class to manage translations.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.