dolibarr 18.0.8
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-2021 Frédéric France <frederic.france@netlogic.fr>
6 * Copyright (C) 2026 Benjamin Falière <benjamin@faliere.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
32{
37 public static $FIELDS = array(
38 'name'
39 );
40
44 public $company;
45
49 public function __construct()
50 {
51 global $db, $conf;
52 $this->db = $db;
53
54 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
55 require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
56 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
57 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
58
59 $this->company = new Societe($this->db);
60
61 if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
62 static::$FIELDS[] = 'email';
63 }
64 }
65
76 public function get($id)
77 {
78 return $this->_fetch($id);
79 }
80
93 public function getByEmail($email)
94 {
95 return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
96 }
97
110 public function getByBarcode($barcode)
111 {
112 return $this->_fetch('', '', '', $barcode);
113 }
114
132 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '')
133 {
134 $obj_ret = array();
135
136 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
137 throw new RestException(401);
138 }
139
140 // case of external user, we force socids
141 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
142
143 // If the internal user must only see his customers, force searching by him
144 $search_sale = 0;
145 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
146 $search_sale = DolibarrApiAccess::$user->id;
147 }
148
149 $sql = "SELECT t.rowid";
150 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
151 $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)
152 }
153 $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
154 $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
155 if ($category > 0) {
156 if ($mode != 4) {
157 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
158 }
159 if (!in_array($mode, array(1, 2, 3))) {
160 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
161 }
162 }
163 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
164 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
165 }
166 $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
167 $sql .= " WHERE t.entity IN (".getEntity('societe').")";
168 $sql .= " AND t.fk_stcomm = st.id";
169
170 if ($mode == 1) {
171 $sql .= " AND t.client IN (1, 3)";
172 } elseif ($mode == 2) {
173 $sql .= " AND t.client IN (2, 3)";
174 } elseif ($mode == 3) {
175 $sql .= " AND t.client IN (0)";
176 } elseif ($mode == 4) {
177 $sql .= " AND t.fournisseur IN (1)";
178 }
179
180 // Select thirdparties of given category
181 if ($category > 0) {
182 if (!empty($mode) && $mode != 4) {
183 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
184 } elseif (!empty($mode) && $mode == 4) {
185 $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
186 } else {
187 $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))";
188 }
189 }
190
191 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
192 $sql .= " AND t.rowid = sc.fk_soc";
193 }
194 //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
195 if ($socids) {
196 $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
197 }
198 if ($search_sale > 0) {
199 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
200 }
201 // Insert sale filter
202 if ($search_sale > 0) {
203 $sql .= " AND sc.fk_user = ".((int) $search_sale);
204 }
205 // Add sql filters
206 if ($sqlfilters) {
207 $errormessage = '';
208 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
209 if ($errormessage) {
210 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
211 }
212 }
213
214 $sql .= $this->db->order($sortfield, $sortorder);
215
216 if ($limit) {
217 if ($page < 0) {
218 $page = 0;
219 }
220 $offset = $limit * $page;
221
222 $sql .= $this->db->plimit($limit + 1, $offset);
223 }
224
225 $result = $this->db->query($sql);
226 if ($result) {
227 $num = $this->db->num_rows($result);
228 $min = min($num, ($limit <= 0 ? $num : $limit));
229 $i = 0;
230 while ($i < $min) {
231 $obj = $this->db->fetch_object($result);
232 $soc_static = new Societe($this->db);
233 if ($soc_static->fetch($obj->rowid)) {
234 if (isModEnabled('mailing')) {
235 $soc_static->getNoEmail();
236 }
237 $obj_ret[] = $this->_cleanObjectDatas($soc_static);
238 }
239 $i++;
240 }
241 } else {
242 throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
243 }
244 if (!count($obj_ret)) {
245 throw new RestException(404, 'Thirdparties not found');
246 }
247 return $obj_ret;
248 }
249
256 public function post($request_data = null)
257 {
258 if (!DolibarrApiAccess::$user->rights->societe->creer) {
259 throw new RestException(401);
260 }
261 // Check mandatory fields
262 $result = $this->_validate($request_data);
263
264 foreach ($request_data as $field => $value) {
265 $this->company->$field = $value;
266 }
267 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
268 throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
269 }
270 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
271 $this->company->setNoEmail($this->company->no_email);
272 }
273
274 return $this->company->id;
275 }
276
287 public function put($id, $request_data = null)
288 {
289 if (!DolibarrApiAccess::$user->rights->societe->creer) {
290 throw new RestException(401);
291 }
292
293 $result = $this->company->fetch($id);
294 if (!$result) {
295 throw new RestException(404, 'Thirdparty not found');
296 }
297
298 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
299 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
300 }
301
302 foreach ($request_data as $field => $value) {
303 if ($field == 'id') {
304 continue;
305 }
306 if ($field == 'array_options' && is_array($value)) {
307 foreach ($value as $index => $val) {
308 $this->company->array_options[$index] = $this->_checkValForAPI($field, $val, $this->company);
309 }
310 continue;
311 }
312 $this->company->$field = $value;
313 }
314
315 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
316 $this->company->setNoEmail($this->company->no_email);
317 }
318
319 if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update', 1) > 0) {
320 return $this->get($id);
321 } else {
322 throw new RestException(500, $this->company->error);
323 }
324 }
325
340 public function merge($id, $idtodelete)
341 {
342 global $hookmanager;
343
344 $error = 0;
345
346 if ($id == $idtodelete) {
347 throw new RestException(400, 'Try to merge a thirdparty into itself');
348 }
349
350 if (!DolibarrApiAccess::$user->rights->societe->creer) {
351 throw new RestException(401);
352 }
353
354 $result = $this->company->fetch($id); // include the fetch of extra fields
355 if (!$result) {
356 throw new RestException(404, 'Thirdparty not found');
357 }
358
359 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
360 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
361 }
362
363 $this->companytoremove = new Societe($this->db);
364
365 $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
366 if (!$result) {
367 throw new RestException(404, 'Thirdparty not found');
368 }
369
370 if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
371 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
372 }
373
374 $soc_origin = $this->companytoremove;
375 $object = $this->company;
376 $user = DolibarrApiAccess::$user;
377
378
379 // Call same code than into action 'confirm_merge'
380
381
382 $this->db->begin();
383
384 // Recopy some data
385 $object->client = $object->client | $soc_origin->client;
386 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
387 $listofproperties = array(
388 'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
389 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
390 'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
391 'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
392 'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
393 'model_pdf', 'fk_projet'
394 );
395 foreach ($listofproperties as $property) {
396 if (empty($object->$property)) {
397 $object->$property = $soc_origin->$property;
398 }
399 }
400
401 // Concat some data
402 $listofproperties = array(
403 'note_public', 'note_private'
404 );
405 foreach ($listofproperties as $property) {
406 $object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
407 }
408
409 // Merge extrafields
410 if (is_array($soc_origin->array_options)) {
411 foreach ($soc_origin->array_options as $key => $val) {
412 if (empty($object->array_options[$key])) {
413 $object->array_options[$key] = $val;
414 }
415 }
416 }
417
418 // Merge categories
419 $static_cat = new Categorie($this->db);
420 $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
421 $object->setCategories($custcats, 'customer');
422 $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
423 $object->setCategories($suppcats, 'supplier');
424
425 // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
426 if ($soc_origin->code_client == $object->code_client
427 || $soc_origin->code_fournisseur == $object->code_fournisseur
428 || $soc_origin->barcode == $object->barcode) {
429 dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
430 $soc_origin->code_client = '';
431 $soc_origin->code_fournisseur = '';
432 $soc_origin->barcode = '';
433 $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
434 }
435
436 // Update
437 $result = $object->update($object->id, $user, 0, 1, 1, 'merge');
438 if ($result < 0) {
439 $error++;
440 }
441
442 // Move links
443 if (!$error) {
444 // This list is also into the societe/card.php file
445 // TODO Mutualise the list into object societe.class.php
446 $objects = array(
447 'Adherent' => '/adherents/class/adherent.class.php',
448 'Don' => '/don/class/don.class.php',
449 'Societe' => '/societe/class/societe.class.php',
450 //'Categorie' => '/categories/class/categorie.class.php',
451 'ActionComm' => '/comm/action/class/actioncomm.class.php',
452 'Propal' => '/comm/propal/class/propal.class.php',
453 'Commande' => '/commande/class/commande.class.php',
454 'Facture' => '/compta/facture/class/facture.class.php',
455 'FactureRec' => '/compta/facture/class/facture-rec.class.php',
456 'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
457 'Mo' => '/mrp/class/mo.class.php',
458 'Contact' => '/contact/class/contact.class.php',
459 'Contrat' => '/contrat/class/contrat.class.php',
460 'Expedition' => '/expedition/class/expedition.class.php',
461 'Fichinter' => '/fichinter/class/fichinter.class.php',
462 'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
463 'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
464 'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
465 'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
466 'Delivery' => '/delivery/class/delivery.class.php',
467 'Product' => '/product/class/product.class.php',
468 'Project' => '/projet/class/project.class.php',
469 'Ticket' => '/ticket/class/ticket.class.php',
470 'User' => '/user/class/user.class.php',
471 'Account' => '/compta/bank/class/account.class.php',
472 'ConferenceOrBoothAttendee' => '/eventorganization/class/conferenceorboothattendee.class.php'
473 );
474
475 //First, all core objects must update their tables
476 foreach ($objects as $object_name => $object_file) {
477 require_once DOL_DOCUMENT_ROOT.$object_file;
478
479 if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
480 $error++;
481 //setEventMessages($this->db->lasterror(), null, 'errors');
482 }
483 }
484 }
485
486 // External modules should update their ones too
487 if (!$error) {
488 $parameters = array('soc_origin' => $soc_origin->id, 'soc_dest' => $object->id);
489 $reshook = $hookmanager->executeHooks('replaceThirdparty', $parameters, $soc_dest, $action);
490
491 if ($reshook < 0) {
492 //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
493 $error++;
494 }
495 }
496
497
498 if (!$error) {
499 $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
500
501 // Call trigger
502 $result = $object->call_trigger('COMPANY_MODIFY', $user);
503 if ($result < 0) {
504 //setEventMessages($object->error, $object->errors, 'errors');
505 $error++;
506 }
507 // End call triggers
508 }
509
510 if (!$error) {
511 //We finally remove the old thirdparty
512 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
513 $error++;
514 }
515 }
516
517 // End of merge
518
519 if ($error) {
520 $this->db->rollback();
521
522 throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
523 } else {
524 $this->db->commit();
525 }
526
527 return $this->get($id);
528 }
529
536 public function delete($id)
537 {
538 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
539 throw new RestException(401);
540 }
541 $result = $this->company->fetch($id);
542 if (!$result) {
543 throw new RestException(404, 'Thirdparty not found');
544 }
545 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
546 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
547 }
548 $this->company->oldcopy = clone $this->company;
549
550 $res = $this->company->delete($id);
551 if ($res < 0) {
552 throw new RestException(500, "Can't delete, error occurs");
553 } elseif ($res == 0) {
554 throw new RestException(409, "Can't delete, that product is probably used");
555 }
556
557 return array(
558 'success' => array(
559 'code' => 200,
560 'message' => 'Object deleted'
561 )
562 );
563 }
564
580 public function setThirdpartyPriceLevel($id, $priceLevel)
581 {
582 global $conf;
583
584 if (!isModEnabled('societe')) {
585 throw new RestException(501, 'Module "Thirdparties" needed for this request');
586 }
587
588 if (!isModEnabled("product")) {
589 throw new RestException(501, 'Module "Products" needed for this request');
590 }
591
592 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
593 throw new RestException(501, 'Multiprices features activation needed for this request');
594 }
595
596 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
597 throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
598 }
599
600 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
601 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
602 }
603
604 $result = $this->company->fetch($id);
605 if ($result < 0) {
606 throw new RestException(404, 'Thirdparty '.$id.' not found');
607 }
608
609 if (empty($result)) {
610 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
611 }
612
613 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
614 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
615 }
616
617 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
618 if ($result <= 0) {
619 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
620 }
621
622 return $this->_cleanObjectDatas($this->company);
623 }
624
637 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
638 {
639 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
640 throw new RestException(401);
641 }
642
643 $result = $this->company->fetch($id);
644 if (!$result) {
645 throw new RestException(404, 'Thirdparty not found');
646 }
647
648 $categories = new Categorie($this->db);
649
650 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
651
652 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
653 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
654 }
655
656 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
657 return array();
658 }
659
660 return $arrayofcateg;
661 }
662
672 public function addCategory($id, $category_id)
673 {
674 if (!DolibarrApiAccess::$user->rights->societe->creer) {
675 throw new RestException(401);
676 }
677
678 $result = $this->company->fetch($id);
679 if (!$result) {
680 throw new RestException(404, 'Thirdparty not found');
681 }
682 $category = new Categorie($this->db);
683 $result = $category->fetch($category_id);
684 if (!$result) {
685 throw new RestException(404, 'category not found');
686 }
687
688 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
689 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
690 }
691 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
692 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 }
694
695 $category->add_type($this->company, 'customer');
696
697 return $this->_cleanObjectDatas($this->company);
698 }
699
710 public function deleteCategory($id, $category_id)
711 {
712 if (!DolibarrApiAccess::$user->rights->societe->creer) {
713 throw new RestException(401);
714 }
715
716 $result = $this->company->fetch($id);
717 if (!$result) {
718 throw new RestException(404, 'Thirdparty not found');
719 }
720 $category = new Categorie($this->db);
721 $result = $category->fetch($category_id);
722 if (!$result) {
723 throw new RestException(404, 'category not found');
724 }
725
726 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
727 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
728 }
729 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
730 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
731 }
732
733 $category->del_type($this->company, 'customer');
734
735 return $this->_cleanObjectDatas($this->company);
736 }
737
751 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
752 {
753 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
754 throw new RestException(401);
755 }
756
757 $result = $this->company->fetch($id);
758 if (!$result) {
759 throw new RestException(404, 'Thirdparty not found');
760 }
761
762 $categories = new Categorie($this->db);
763
764 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
765
766 if (is_numeric($result) && $result < 0) {
767 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
768 }
769
770 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
771 return array();
772 }
773
774 return $result;
775 }
776
787 public function addSupplierCategory($id, $category_id)
788 {
789 if (!DolibarrApiAccess::$user->rights->societe->creer) {
790 throw new RestException(401);
791 }
792
793 $result = $this->company->fetch($id);
794 if (!$result) {
795 throw new RestException(404, 'Thirdparty not found');
796 }
797 $category = new Categorie($this->db);
798 $result = $category->fetch($category_id);
799 if (!$result) {
800 throw new RestException(404, 'category not found');
801 }
802
803 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
804 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
805 }
806 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
807 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
808 }
809
810 $category->add_type($this->company, 'supplier');
811
812 return $this->_cleanObjectDatas($this->company);
813 }
814
825 public function deleteSupplierCategory($id, $category_id)
826 {
827 if (!DolibarrApiAccess::$user->rights->societe->creer) {
828 throw new RestException(401);
829 }
830
831 $result = $this->company->fetch($id);
832 if (!$result) {
833 throw new RestException(404, 'Thirdparty not found');
834 }
835 $category = new Categorie($this->db);
836 $result = $category->fetch($category_id);
837 if (!$result) {
838 throw new RestException(404, 'category not found');
839 }
840
841 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
842 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 }
844 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
845 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
846 }
847
848 $category->del_type($this->company, 'supplier');
849
850 return $this->_cleanObjectDatas($this->company);
851 }
852
853
868 public function getOutStandingProposals($id, $mode = 'customer')
869 {
870 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
871 throw new RestException(401);
872 }
873
874 if (empty($id)) {
875 throw new RestException(400, 'Thirdparty ID is mandatory');
876 }
877
878 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
879 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
880 }
881
882 $result = $this->company->fetch($id);
883 if (!$result) {
884 throw new RestException(404, 'Thirdparty not found');
885 }
886
887 $result = $this->company->getOutstandingProposals($mode);
888
889 unset($result['total_ht']);
890 unset($result['total_ttc']);
891
892 return $result;
893 }
894
895
910 public function getOutStandingOrder($id, $mode = 'customer')
911 {
912 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
913 throw new RestException(401);
914 }
915
916 if (empty($id)) {
917 throw new RestException(400, 'Thirdparty ID is mandatory');
918 }
919
920 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
921 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
922 }
923
924 $result = $this->company->fetch($id);
925 if (!$result) {
926 throw new RestException(404, 'Thirdparty not found');
927 }
928
929 $result = $this->company->getOutstandingOrders($mode);
930
931 unset($result['total_ht']);
932 unset($result['total_ttc']);
933
934 return $result;
935 }
936
951 public function getOutStandingInvoices($id, $mode = 'customer')
952 {
953 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
954 throw new RestException(401);
955 }
956
957 if (empty($id)) {
958 throw new RestException(400, 'Thirdparty ID is mandatory');
959 }
960
961 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
962 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
963 }
964
965 $result = $this->company->fetch($id);
966 if (!$result) {
967 throw new RestException(404, 'Thirdparty not found');
968 }
969
970 $result = $this->company->getOutstandingBills($mode);
971
972 unset($result['total_ht']);
973 unset($result['total_ttc']);
974
975 return $result;
976 }
977
992 public function getSalesRepresentatives($id, $mode = 0)
993 {
994 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
995 throw new RestException(401);
996 }
997
998 if (empty($id)) {
999 throw new RestException(400, 'Thirdparty ID is mandatory');
1000 }
1001
1002 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1003 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1004 }
1005
1006 $result = $this->company->fetch($id);
1007 if (!$result) {
1008 throw new RestException(404, 'Thirdparty not found');
1009 }
1010
1011 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1012
1013 return $result;
1014 }
1015
1033 public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1034 {
1035 $obj_ret = array();
1036
1037 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1038 throw new RestException(401);
1039 }
1040
1041 if (empty($id)) {
1042 throw new RestException(400, 'Thirdparty ID is mandatory');
1043 }
1044
1045 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1046 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1047 }
1048
1049 $result = $this->company->fetch($id);
1050 if (!$result) {
1051 throw new RestException(404, 'Thirdparty not found');
1052 }
1053
1054
1055 $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";
1056 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
1057 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = re.fk_facture_source";
1058 $sql .= " WHERE re.fk_soc = ".((int) $id);
1059 if ($filter == "available") {
1060 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1061 }
1062 if ($filter == "used") {
1063 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1064 }
1065
1066 $sql .= $this->db->order($sortfield, $sortorder);
1067
1068 $result = $this->db->query($sql);
1069 if (!$result) {
1070 throw new RestException(503, $this->db->lasterror());
1071 } else {
1072 $num = $this->db->num_rows($result);
1073 while ($obj = $this->db->fetch_object($result)) {
1074 $obj_ret[] = $obj;
1075 }
1076 }
1077
1078 return $obj_ret;
1079 }
1080
1081
1082
1097 {
1098 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1099 throw new RestException(401);
1100 }
1101 if (empty($id)) {
1102 throw new RestException(400, 'Thirdparty ID is mandatory');
1103 }
1104
1105 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1106 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1107 }
1108
1109 /*$result = $this->thirdparty->fetch($id);
1110 if( ! $result ) {
1111 throw new RestException(404, 'Thirdparty not found');
1112 }*/
1113
1114 $invoice = new Facture($this->db);
1115 $result = $invoice->list_replacable_invoices($id);
1116 if ($result < 0) {
1117 throw new RestException(405, $invoice->error);
1118 }
1119
1120 return $result;
1121 }
1122
1140 {
1141 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1142 throw new RestException(401);
1143 }
1144 if (empty($id)) {
1145 throw new RestException(400, 'Thirdparty ID is mandatory');
1146 }
1147
1148 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1149 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1150 }
1151
1152 /*$result = $this->thirdparty->fetch($id);
1153 if( ! $result ) {
1154 throw new RestException(404, 'Thirdparty not found');
1155 }*/
1156
1157 $invoice = new Facture($this->db);
1158 $result = $invoice->list_qualified_avoir_invoices($id);
1159 if ($result < 0) {
1160 throw new RestException(405, $invoice->error);
1161 }
1162
1163 return $result;
1164 }
1165
1175 public function getCompanyBankAccount($id)
1176 {
1177 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1178 throw new RestException(401);
1179 }
1180 if (empty($id)) {
1181 throw new RestException(400, 'Thirdparty ID is mandatory');
1182 }
1183
1184 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1185 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1186 }
1187
1192 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1193 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1194 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1195 if ($id) {
1196 $sql .= " WHERE fk_soc = ".((int) $id);
1197 }
1198
1199 $result = $this->db->query($sql);
1200
1201 if ($this->db->num_rows($result) == 0) {
1202 throw new RestException(404, 'Account not found');
1203 }
1204
1205 $i = 0;
1206
1207 $accounts = array();
1208
1209 if ($result) {
1210 $num = $this->db->num_rows($result);
1211 while ($i < $num) {
1212 $obj = $this->db->fetch_object($result);
1213 $account = new CompanyBankAccount($this->db);
1214 if ($account->fetch($obj->rowid)) {
1215 $accounts[] = $account;
1216 }
1217 $i++;
1218 }
1219 } else {
1220 throw new RestException(404, 'Account not found');
1221 }
1222
1223
1224 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1225
1226 $returnAccounts = array();
1227
1228 foreach ($accounts as $account) {
1229 $object = array();
1230 foreach ($account as $key => $value) {
1231 if (in_array($key, $fields)) {
1232 $object[$key] = $value;
1233 }
1234 }
1235 $returnAccounts[] = $object;
1236 }
1237
1238 return $returnAccounts;
1239 }
1240
1250 public function createCompanyBankAccount($id, $request_data = null)
1251 {
1252 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1253 throw new RestException(401);
1254 }
1255 if ($this->company->fetch($id) <= 0) {
1256 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1257 }
1258 $account = new CompanyBankAccount($this->db);
1259
1260 $account->socid = $id;
1261
1262 foreach ($request_data as $field => $value) {
1263 $account->$field = $value;
1264 }
1265
1266 if ($account->create(DolibarrApiAccess::$user) < 0) {
1267 throw new RestException(500, 'Error creating Company Bank account');
1268 }
1269
1270 if (empty($account->rum)) {
1271 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1272 $prelevement = new BonPrelevement($this->db);
1273 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1274 $account->date_rum = dol_now();
1275 }
1276
1277 if ($account->update(DolibarrApiAccess::$user) < 0) {
1278 throw new RestException(500, 'Error updating values');
1279 }
1280
1281 return $this->_cleanObjectDatas($account);
1282 }
1283
1295 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1296 {
1297 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1298 throw new RestException(401);
1299 }
1300 if ($this->company->fetch($id) <= 0) {
1301 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1302 }
1303 $account = new CompanyBankAccount($this->db);
1304
1305 $account->fetch($bankaccount_id, $id, -1, '');
1306
1307 if ($account->socid != $id) {
1308 throw new RestException(401);
1309 }
1310
1311
1312 foreach ($request_data as $field => $value) {
1313 $account->$field = $value;
1314 }
1315
1316 if (empty($account->rum)) {
1317 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1318 $prelevement = new BonPrelevement($this->db);
1319 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1320 $account->date_rum = dol_now();
1321 }
1322
1323 if ($account->update(DolibarrApiAccess::$user) < 0) {
1324 throw new RestException(500, 'Error updating values');
1325 }
1326
1327 return $this->_cleanObjectDatas($account);
1328 }
1329
1340 public function deleteCompanyBankAccount($id, $bankaccount_id)
1341 {
1342 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1343 throw new RestException(401);
1344 }
1345
1346 $account = new CompanyBankAccount($this->db);
1347
1348 $account->fetch($bankaccount_id);
1349
1350 if (!$account->socid == $id) {
1351 throw new RestException(401);
1352 }
1353
1354 return $account->delete(DolibarrApiAccess::$user);
1355 }
1356
1367 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1368 {
1369 global $conf, $langs;
1370
1371 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1372
1373 if ($this->company->fetch($id) <= 0) {
1374 throw new RestException(404, 'Thirdparty not found');
1375 }
1376
1377 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1378 throw new RestException(401);
1379 }
1380
1381 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1382
1383 $this->company->fk_bank = $this->company->fk_account;
1384 $this->company->fk_account = $this->company->fk_account;
1385
1386 $outputlangs = $langs;
1387 $newlang = '';
1388
1389 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1390 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1391 if (isset($this->company->thirdparty->default_lang)) {
1392 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1393 } elseif (isset($this->company->default_lang)) {
1394 $newlang = $this->company->default_lang; // for thirdparty
1395 }
1396 }
1397 if (!empty($newlang)) {
1398 $outputlangs = new Translate("", $conf);
1399 $outputlangs->setDefaultLang($newlang);
1400 }
1401
1402 $sql = "SELECT rowid";
1403 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1404 if ($id) {
1405 $sql .= " WHERE fk_soc = ".((int) $id);
1406 }
1407 if ($companybankid) {
1408 $sql .= " AND rowid = ".((int) $companybankid);
1409 }
1410
1411 $i = 0;
1412 $accounts = array();
1413
1414 $result = $this->db->query($sql);
1415 if ($result) {
1416 if ($this->db->num_rows($result) == 0) {
1417 throw new RestException(404, 'Bank account not found');
1418 }
1419
1420 $num = $this->db->num_rows($result);
1421 while ($i < $num) {
1422 $obj = $this->db->fetch_object($result);
1423
1424 $account = new CompanyBankAccount($this->db);
1425 if ($account->fetch($obj->rowid)) {
1426 $accounts[] = $account;
1427 }
1428 $i++;
1429 }
1430 } else {
1431 throw new RestException(500, 'Sql error '.$this->db->lasterror());
1432 }
1433
1434 $moreparams = array(
1435 'use_companybankid' => $accounts[0]->id,
1436 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1437 );
1438
1439 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1440
1441 if ($result > 0) {
1442 return array("success" => $result);
1443 } else {
1444 throw new RestException(500, 'Error generating the document '.$this->company->error);
1445 }
1446 }
1447
1460 public function getSocieteAccounts($id, $site = null)
1461 {
1462 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1463 throw new RestException(401);
1464 }
1465
1466 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1467 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1468 }
1469
1473 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1474 $sql .= " WHERE fk_soc = ".((int) $id);
1475 if ($site) {
1476 $sql .= " AND site ='".$this->db->escape($site)."'";
1477 }
1478
1479 $result = $this->db->query($sql);
1480
1481 if ($result && $this->db->num_rows($result) == 0) {
1482 throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1483 }
1484
1485 $i = 0;
1486
1487 $accounts = array();
1488
1489 $num = $this->db->num_rows($result);
1490 while ($i < $num) {
1491 $obj = $this->db->fetch_object($result);
1492 $account = new SocieteAccount($this->db);
1493
1494 if ($account->fetch($obj->rowid)) {
1495 $accounts[] = $account;
1496 }
1497 $i++;
1498 }
1499
1500 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1501
1502 $returnAccounts = array();
1503
1504 foreach ($accounts as $account) {
1505 $object = array();
1506 foreach ($account as $key => $value) {
1507 if (in_array($key, $fields)) {
1508 $object[$key] = $value;
1509 }
1510 }
1511 $returnAccounts[] = $object;
1512 }
1513
1514 return $returnAccounts;
1515 }
1516
1536 public function createSocieteAccount($id, $request_data = null)
1537 {
1538 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1539 throw new RestException(401);
1540 }
1541
1542 if (!isset($request_data['site'])) {
1543 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1544 }
1545
1546 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1547 $result = $this->db->query($sql);
1548
1549 if ($result && $this->db->num_rows($result) == 0) {
1550 $account = new SocieteAccount($this->db);
1551 if (!isset($request_data['login'])) {
1552 $account->login = "";
1553 }
1554 $account->fk_soc = $id;
1555
1556 foreach ($request_data as $field => $value) {
1557 $account->$field = $value;
1558 }
1559
1560 if ($account->create(DolibarrApiAccess::$user) < 0) {
1561 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1562 }
1563
1564 $this->_cleanObjectDatas($account);
1565
1566 return $account;
1567 } else {
1568 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1569 }
1570 }
1571
1594 public function putSocieteAccount($id, $site, $request_data = null)
1595 {
1596 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1597 throw new RestException(401);
1598 }
1599
1600 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1601 $result = $this->db->query($sql);
1602
1603 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1604 if ($result && $this->db->num_rows == 0) {
1605 if (!isset($request_data['key_account'])) {
1606 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1607 }
1608 $account = new SocieteAccount($this->db);
1609 if (!isset($request_data['login'])) {
1610 $account->login = "";
1611 }
1612
1613 foreach ($request_data as $field => $value) {
1614 $account->$field = $value;
1615 }
1616
1617 $account->fk_soc = $id;
1618 $account->site = $site;
1619
1620 if ($account->create(DolibarrApiAccess::$user) < 0) {
1621 throw new RestException(500, 'Error creating SocieteAccount entity.');
1622 }
1623 // We found an existing SocieteAccount entity, we are replacing it
1624 } else {
1625 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1626 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1627 $result = $this->db->query($sql);
1628
1629 if ($result && $this->db->num_rows($result) !== 0) {
1630 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.");
1631 }
1632 }
1633
1634 $obj = $this->db->fetch_object($result);
1635
1636 $account = new SocieteAccount($this->db);
1637 $account->id = $obj->rowid;
1638 $account->fk_soc = $id;
1639 $account->site = $site;
1640 if (!isset($request_data['login'])) {
1641 $account->login = "";
1642 }
1643 $account->fk_user_creat = $obj->fk_user_creat;
1644 $account->date_creation = $obj->date_creation;
1645
1646 foreach ($request_data as $field => $value) {
1647 $account->$field = $value;
1648 }
1649
1650 if ($account->update(DolibarrApiAccess::$user) < 0) {
1651 throw new RestException(500, 'Error updating SocieteAccount entity.');
1652 }
1653 }
1654
1655 $this->_cleanObjectDatas($account);
1656
1657 return $account;
1658 }
1659
1676 public function patchSocieteAccount($id, $site, $request_data = null)
1677 {
1678 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1679 throw new RestException(401);
1680 }
1681
1682 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1683 $result = $this->db->query($sql);
1684
1685 if ($result && $this->db->num_rows($result) == 0) {
1686 throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1687 } else {
1688 // If the user tries to edit the site member, we check first if
1689 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1690 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1691 $result = $this->db->query($sql);
1692
1693 if ($result && $this->db->num_rows($result) !== 0) {
1694 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.");
1695 }
1696 }
1697
1698 $obj = $this->db->fetch_object($result);
1699 $account = new SocieteAccount($this->db);
1700 $account->fetch($obj->rowid);
1701
1702 foreach ($request_data as $field => $value) {
1703 $account->$field = $value;
1704 }
1705
1706 if ($account->update(DolibarrApiAccess::$user) < 0) {
1707 throw new RestException(500, 'Error updating SocieteAccount account');
1708 }
1709
1710 $this->_cleanObjectDatas($account);
1711
1712 return $account;
1713 }
1714 }
1715
1729 public function deleteSocieteAccount($id, $site)
1730 {
1731 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1732 throw new RestException(401);
1733 }
1734
1735 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1736 $result = $this->db->query($sql);
1737
1738 if ($result && $this->db->num_rows($result) == 0) {
1739 throw new RestException(404);
1740 } else {
1741 $obj = $this->db->fetch_object($result);
1742 $account = new SocieteAccount($this->db);
1743 $account->fetch($obj->rowid);
1744
1745 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1746 throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1747 }
1748 }
1749 }
1750
1763 public function deleteSocieteAccounts($id)
1764 {
1765 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1766 throw new RestException(401);
1767 }
1768
1773 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1774 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1775
1776 $result = $this->db->query($sql);
1777
1778 if ($result && $this->db->num_rows($result) == 0) {
1779 throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1780 } else {
1781 $i = 0;
1782
1783 $num = $this->db->num_rows($result);
1784 while ($i < $num) {
1785 $obj = $this->db->fetch_object($result);
1786 $account = new SocieteAccount($this->db);
1787 $account->fetch($obj->rowid);
1788
1789 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1790 throw new RestException(500, 'Error while deleting gateways attached to this third party');
1791 }
1792 $i++;
1793 }
1794 }
1795 }
1796
1797 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1804 protected function _cleanObjectDatas($object)
1805 {
1806 // phpcs:enable
1807 $object = parent::_cleanObjectDatas($object);
1808
1809 unset($object->nom); // ->name already defined and nom deprecated
1810 unset($object->name_bis); // ->name_alias already defined
1811 unset($object->note); // ->note_private and note_public already defined
1812 unset($object->departement);
1813 unset($object->departement_code);
1814 unset($object->pays);
1815 unset($object->particulier);
1816 unset($object->prefix_comm);
1817
1818 unset($object->siren);
1819 unset($object->siret);
1820 unset($object->ape);
1821
1822 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.
1823
1824 unset($object->total_ht);
1825 unset($object->total_tva);
1826 unset($object->total_localtax1);
1827 unset($object->total_localtax2);
1828 unset($object->total_ttc);
1829
1830 unset($object->lines);
1831 unset($object->thirdparty);
1832
1833 unset($object->fk_delivery_address); // deprecated feature
1834
1835 return $object;
1836 }
1837
1846 private function _validate($data)
1847 {
1848 $thirdparty = array();
1849 foreach (Thirdparties::$FIELDS as $field) {
1850 if (!isset($data[$field])) {
1851 throw new RestException(400, "$field field missing");
1852 }
1853 $thirdparty[$field] = $data[$field];
1854 }
1855 return $thirdparty;
1856 }
1857
1879 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1880 {
1881 global $conf;
1882
1883 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1884 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
1885 }
1886
1887 if ($rowid === 0) {
1888 $result = $this->company->initAsSpecimen();
1889 } else {
1890 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1891 }
1892 if (!$result) {
1893 throw new RestException(404, 'Thirdparty not found');
1894 }
1895
1896 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1897 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
1898 }
1899 if (isModEnabled('mailing')) {
1900 $this->company->getNoEmail();
1901 }
1902
1903 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1904 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1905 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1906 } else {
1907 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1908 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1909 }
1910
1911 $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1912 $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1913 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1914 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1915
1916 return $this->_cleanObjectDatas($this->company);
1917 }
1918}
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
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='')
List thirdparties.
__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)
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 dolibarr global constant int value.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.