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