dolibarr 18.0.6
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use Luracast\Restler\RestException;
22
31{
36 public static $FIELDS = array(
37 'name'
38 );
39
43 public $company;
44
48 public function __construct()
49 {
50 global $db, $conf;
51 $this->db = $db;
52
53 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
54 require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
55 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
56 require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
57
58 $this->company = new Societe($this->db);
59
60 if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
61 static::$FIELDS[] = 'email';
62 }
63 }
64
75 public function get($id)
76 {
77 return $this->_fetch($id);
78 }
79
92 public function getByEmail($email)
93 {
94 return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
95 }
96
109 public function getByBarcode($barcode)
110 {
111 return $this->_fetch('', '', '', $barcode);
112 }
113
131 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '')
132 {
133 $obj_ret = array();
134
135 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
136 throw new RestException(401);
137 }
138
139 // case of external user, we force socids
140 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
141
142 // If the internal user must only see his customers, force searching by him
143 $search_sale = 0;
144 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
145 $search_sale = DolibarrApiAccess::$user->id;
146 }
147
148 $sql = "SELECT t.rowid";
149 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
150 $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)
151 }
152 $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
153 $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
154 if ($category > 0) {
155 if ($mode != 4) {
156 $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
157 }
158 if (!in_array($mode, array(1, 2, 3))) {
159 $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
160 }
161 }
162 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
163 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
164 }
165 $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
166 $sql .= " WHERE t.entity IN (".getEntity('societe').")";
167 $sql .= " AND t.fk_stcomm = st.id";
168
169 if ($mode == 1) {
170 $sql .= " AND t.client IN (1, 3)";
171 } elseif ($mode == 2) {
172 $sql .= " AND t.client IN (2, 3)";
173 } elseif ($mode == 3) {
174 $sql .= " AND t.client IN (0)";
175 } elseif ($mode == 4) {
176 $sql .= " AND t.fournisseur IN (1)";
177 }
178
179 // Select thirdparties of given category
180 if ($category > 0) {
181 if (!empty($mode) && $mode != 4) {
182 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
183 } elseif (!empty($mode) && $mode == 4) {
184 $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
185 } else {
186 $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))";
187 }
188 }
189
190 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
191 $sql .= " AND t.rowid = sc.fk_soc";
192 }
193 //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
194 if ($socids) {
195 $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
196 }
197 if ($search_sale > 0) {
198 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
199 }
200 // Insert sale filter
201 if ($search_sale > 0) {
202 $sql .= " AND sc.fk_user = ".((int) $search_sale);
203 }
204 // Add sql filters
205 if ($sqlfilters) {
206 $errormessage = '';
207 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
208 if ($errormessage) {
209 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
210 }
211 }
212
213 $sql .= $this->db->order($sortfield, $sortorder);
214
215 if ($limit) {
216 if ($page < 0) {
217 $page = 0;
218 }
219 $offset = $limit * $page;
220
221 $sql .= $this->db->plimit($limit + 1, $offset);
222 }
223
224 $result = $this->db->query($sql);
225 if ($result) {
226 $num = $this->db->num_rows($result);
227 $min = min($num, ($limit <= 0 ? $num : $limit));
228 $i = 0;
229 while ($i < $min) {
230 $obj = $this->db->fetch_object($result);
231 $soc_static = new Societe($this->db);
232 if ($soc_static->fetch($obj->rowid)) {
233 if (isModEnabled('mailing')) {
234 $soc_static->getNoEmail();
235 }
236 $obj_ret[] = $this->_cleanObjectDatas($soc_static);
237 }
238 $i++;
239 }
240 } else {
241 throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
242 }
243 if (!count($obj_ret)) {
244 throw new RestException(404, 'Thirdparties not found');
245 }
246 return $obj_ret;
247 }
248
255 public function post($request_data = null)
256 {
257 if (!DolibarrApiAccess::$user->rights->societe->creer) {
258 throw new RestException(401);
259 }
260 // Check mandatory fields
261 $result = $this->_validate($request_data);
262
263 foreach ($request_data as $field => $value) {
264 $this->company->$field = $value;
265 }
266 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
267 throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
268 }
269 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
270 $this->company->setNoEmail($this->company->no_email);
271 }
272
273 return $this->company->id;
274 }
275
286 public function put($id, $request_data = null)
287 {
288 if (!DolibarrApiAccess::$user->rights->societe->creer) {
289 throw new RestException(401);
290 }
291
292 $result = $this->company->fetch($id);
293 if (!$result) {
294 throw new RestException(404, 'Thirdparty not found');
295 }
296
297 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
298 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
299 }
300
301 foreach ($request_data as $field => $value) {
302 if ($field == 'id') {
303 continue;
304 }
305 $this->company->$field = $value;
306 }
307
308 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
309 $this->company->setNoEmail($this->company->no_email);
310 }
311
312 if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update', 1) > 0) {
313 return $this->get($id);
314 } else {
315 throw new RestException(500, $this->company->error);
316 }
317 }
318
333 public function merge($id, $idtodelete)
334 {
335 global $hookmanager;
336
337 $error = 0;
338
339 if ($id == $idtodelete) {
340 throw new RestException(400, 'Try to merge a thirdparty into itself');
341 }
342
343 if (!DolibarrApiAccess::$user->rights->societe->creer) {
344 throw new RestException(401);
345 }
346
347 $result = $this->company->fetch($id); // include the fetch of extra fields
348 if (!$result) {
349 throw new RestException(404, 'Thirdparty not found');
350 }
351
352 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
353 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
354 }
355
356 $this->companytoremove = new Societe($this->db);
357
358 $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
359 if (!$result) {
360 throw new RestException(404, 'Thirdparty not found');
361 }
362
363 if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
364 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
365 }
366
367 $soc_origin = $this->companytoremove;
368 $object = $this->company;
369 $user = DolibarrApiAccess::$user;
370
371
372 // Call same code than into action 'confirm_merge'
373
374
375 $this->db->begin();
376
377 // Recopy some data
378 $object->client = $object->client | $soc_origin->client;
379 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
380 $listofproperties = array(
381 'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
382 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
383 'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
384 'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
385 'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
386 'model_pdf', 'fk_projet'
387 );
388 foreach ($listofproperties as $property) {
389 if (empty($object->$property)) {
390 $object->$property = $soc_origin->$property;
391 }
392 }
393
394 // Concat some data
395 $listofproperties = array(
396 'note_public', 'note_private'
397 );
398 foreach ($listofproperties as $property) {
399 $object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
400 }
401
402 // Merge extrafields
403 if (is_array($soc_origin->array_options)) {
404 foreach ($soc_origin->array_options as $key => $val) {
405 if (empty($object->array_options[$key])) {
406 $object->array_options[$key] = $val;
407 }
408 }
409 }
410
411 // Merge categories
412 $static_cat = new Categorie($this->db);
413 $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
414 $object->setCategories($custcats, 'customer');
415 $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
416 $object->setCategories($suppcats, 'supplier');
417
418 // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
419 if ($soc_origin->code_client == $object->code_client
420 || $soc_origin->code_fournisseur == $object->code_fournisseur
421 || $soc_origin->barcode == $object->barcode) {
422 dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
423 $soc_origin->code_client = '';
424 $soc_origin->code_fournisseur = '';
425 $soc_origin->barcode = '';
426 $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
427 }
428
429 // Update
430 $result = $object->update($object->id, $user, 0, 1, 1, 'merge');
431 if ($result < 0) {
432 $error++;
433 }
434
435 // Move links
436 if (!$error) {
437 // This list is also into the societe/card.php file
438 // TODO Mutualise the list into object societe.class.php
439 $objects = array(
440 'Adherent' => '/adherents/class/adherent.class.php',
441 'Don' => '/don/class/don.class.php',
442 'Societe' => '/societe/class/societe.class.php',
443 //'Categorie' => '/categories/class/categorie.class.php',
444 'ActionComm' => '/comm/action/class/actioncomm.class.php',
445 'Propal' => '/comm/propal/class/propal.class.php',
446 'Commande' => '/commande/class/commande.class.php',
447 'Facture' => '/compta/facture/class/facture.class.php',
448 'FactureRec' => '/compta/facture/class/facture-rec.class.php',
449 'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
450 'Mo' => '/mrp/class/mo.class.php',
451 'Contact' => '/contact/class/contact.class.php',
452 'Contrat' => '/contrat/class/contrat.class.php',
453 'Expedition' => '/expedition/class/expedition.class.php',
454 'Fichinter' => '/fichinter/class/fichinter.class.php',
455 'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
456 'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
457 'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
458 'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
459 'Delivery' => '/delivery/class/delivery.class.php',
460 'Product' => '/product/class/product.class.php',
461 'Project' => '/projet/class/project.class.php',
462 'Ticket' => '/ticket/class/ticket.class.php',
463 'User' => '/user/class/user.class.php',
464 'Account' => '/compta/bank/class/account.class.php',
465 'ConferenceOrBoothAttendee' => '/eventorganization/class/conferenceorboothattendee.class.php'
466 );
467
468 //First, all core objects must update their tables
469 foreach ($objects as $object_name => $object_file) {
470 require_once DOL_DOCUMENT_ROOT.$object_file;
471
472 if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
473 $error++;
474 //setEventMessages($this->db->lasterror(), null, 'errors');
475 }
476 }
477 }
478
479 // External modules should update their ones too
480 if (!$error) {
481 $parameters = array('soc_origin' => $soc_origin->id, 'soc_dest' => $object->id);
482 $reshook = $hookmanager->executeHooks('replaceThirdparty', $parameters, $soc_dest, $action);
483
484 if ($reshook < 0) {
485 //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
486 $error++;
487 }
488 }
489
490
491 if (!$error) {
492 $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
493
494 // Call trigger
495 $result = $object->call_trigger('COMPANY_MODIFY', $user);
496 if ($result < 0) {
497 //setEventMessages($object->error, $object->errors, 'errors');
498 $error++;
499 }
500 // End call triggers
501 }
502
503 if (!$error) {
504 //We finally remove the old thirdparty
505 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
506 $error++;
507 }
508 }
509
510 // End of merge
511
512 if ($error) {
513 $this->db->rollback();
514
515 throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
516 } else {
517 $this->db->commit();
518 }
519
520 return $this->get($id);
521 }
522
529 public function delete($id)
530 {
531 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
532 throw new RestException(401);
533 }
534 $result = $this->company->fetch($id);
535 if (!$result) {
536 throw new RestException(404, 'Thirdparty not found');
537 }
538 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
539 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 }
541 $this->company->oldcopy = clone $this->company;
542
543 $res = $this->company->delete($id);
544 if ($res < 0) {
545 throw new RestException(500, "Can't delete, error occurs");
546 } elseif ($res == 0) {
547 throw new RestException(409, "Can't delete, that product is probably used");
548 }
549
550 return array(
551 'success' => array(
552 'code' => 200,
553 'message' => 'Object deleted'
554 )
555 );
556 }
557
573 public function setThirdpartyPriceLevel($id, $priceLevel)
574 {
575 global $conf;
576
577 if (!isModEnabled('societe')) {
578 throw new RestException(501, 'Module "Thirdparties" needed for this request');
579 }
580
581 if (!isModEnabled("product")) {
582 throw new RestException(501, 'Module "Products" needed for this request');
583 }
584
585 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
586 throw new RestException(501, 'Multiprices features activation needed for this request');
587 }
588
589 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
590 throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
591 }
592
593 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
594 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
595 }
596
597 $result = $this->company->fetch($id);
598 if ($result < 0) {
599 throw new RestException(404, 'Thirdparty '.$id.' not found');
600 }
601
602 if (empty($result)) {
603 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
604 }
605
606 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
607 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
608 }
609
610 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
611 if ($result <= 0) {
612 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
613 }
614
615 return $this->_cleanObjectDatas($this->company);
616 }
617
630 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
631 {
632 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
633 throw new RestException(401);
634 }
635
636 $result = $this->company->fetch($id);
637 if (!$result) {
638 throw new RestException(404, 'Thirdparty not found');
639 }
640
641 $categories = new Categorie($this->db);
642
643 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
644
645 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
646 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
647 }
648
649 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
650 return array();
651 }
652
653 return $arrayofcateg;
654 }
655
665 public function addCategory($id, $category_id)
666 {
667 if (!DolibarrApiAccess::$user->rights->societe->creer) {
668 throw new RestException(401);
669 }
670
671 $result = $this->company->fetch($id);
672 if (!$result) {
673 throw new RestException(404, 'Thirdparty not found');
674 }
675 $category = new Categorie($this->db);
676 $result = $category->fetch($category_id);
677 if (!$result) {
678 throw new RestException(404, 'category not found');
679 }
680
681 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
682 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
683 }
684 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
685 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
686 }
687
688 $category->add_type($this->company, 'customer');
689
690 return $this->_cleanObjectDatas($this->company);
691 }
692
703 public function deleteCategory($id, $category_id)
704 {
705 if (!DolibarrApiAccess::$user->rights->societe->creer) {
706 throw new RestException(401);
707 }
708
709 $result = $this->company->fetch($id);
710 if (!$result) {
711 throw new RestException(404, 'Thirdparty not found');
712 }
713 $category = new Categorie($this->db);
714 $result = $category->fetch($category_id);
715 if (!$result) {
716 throw new RestException(404, 'category not found');
717 }
718
719 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
720 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
721 }
722 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
723 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
724 }
725
726 $category->del_type($this->company, 'customer');
727
728 return $this->_cleanObjectDatas($this->company);
729 }
730
744 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
745 {
746 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
747 throw new RestException(401);
748 }
749
750 $result = $this->company->fetch($id);
751 if (!$result) {
752 throw new RestException(404, 'Thirdparty not found');
753 }
754
755 $categories = new Categorie($this->db);
756
757 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
758
759 if (is_numeric($result) && $result < 0) {
760 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
761 }
762
763 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
764 return array();
765 }
766
767 return $result;
768 }
769
780 public function addSupplierCategory($id, $category_id)
781 {
782 if (!DolibarrApiAccess::$user->rights->societe->creer) {
783 throw new RestException(401);
784 }
785
786 $result = $this->company->fetch($id);
787 if (!$result) {
788 throw new RestException(404, 'Thirdparty not found');
789 }
790 $category = new Categorie($this->db);
791 $result = $category->fetch($category_id);
792 if (!$result) {
793 throw new RestException(404, 'category not found');
794 }
795
796 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
797 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
798 }
799 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
800 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
801 }
802
803 $category->add_type($this->company, 'supplier');
804
805 return $this->_cleanObjectDatas($this->company);
806 }
807
818 public function deleteSupplierCategory($id, $category_id)
819 {
820 if (!DolibarrApiAccess::$user->rights->societe->creer) {
821 throw new RestException(401);
822 }
823
824 $result = $this->company->fetch($id);
825 if (!$result) {
826 throw new RestException(404, 'Thirdparty not found');
827 }
828 $category = new Categorie($this->db);
829 $result = $category->fetch($category_id);
830 if (!$result) {
831 throw new RestException(404, 'category not found');
832 }
833
834 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
835 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
836 }
837 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
838 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
839 }
840
841 $category->del_type($this->company, 'supplier');
842
843 return $this->_cleanObjectDatas($this->company);
844 }
845
846
861 public function getOutStandingProposals($id, $mode = 'customer')
862 {
863 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
864 throw new RestException(401);
865 }
866
867 if (empty($id)) {
868 throw new RestException(400, 'Thirdparty ID is mandatory');
869 }
870
871 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
872 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
873 }
874
875 $result = $this->company->fetch($id);
876 if (!$result) {
877 throw new RestException(404, 'Thirdparty not found');
878 }
879
880 $result = $this->company->getOutstandingProposals($mode);
881
882 unset($result['total_ht']);
883 unset($result['total_ttc']);
884
885 return $result;
886 }
887
888
903 public function getOutStandingOrder($id, $mode = 'customer')
904 {
905 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
906 throw new RestException(401);
907 }
908
909 if (empty($id)) {
910 throw new RestException(400, 'Thirdparty ID is mandatory');
911 }
912
913 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
914 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
915 }
916
917 $result = $this->company->fetch($id);
918 if (!$result) {
919 throw new RestException(404, 'Thirdparty not found');
920 }
921
922 $result = $this->company->getOutstandingOrders($mode);
923
924 unset($result['total_ht']);
925 unset($result['total_ttc']);
926
927 return $result;
928 }
929
944 public function getOutStandingInvoices($id, $mode = 'customer')
945 {
946 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
947 throw new RestException(401);
948 }
949
950 if (empty($id)) {
951 throw new RestException(400, 'Thirdparty ID is mandatory');
952 }
953
954 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
955 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
956 }
957
958 $result = $this->company->fetch($id);
959 if (!$result) {
960 throw new RestException(404, 'Thirdparty not found');
961 }
962
963 $result = $this->company->getOutstandingBills($mode);
964
965 unset($result['total_ht']);
966 unset($result['total_ttc']);
967
968 return $result;
969 }
970
985 public function getSalesRepresentatives($id, $mode = 0)
986 {
987 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
988 throw new RestException(401);
989 }
990
991 if (empty($id)) {
992 throw new RestException(400, 'Thirdparty ID is mandatory');
993 }
994
995 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
996 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
997 }
998
999 $result = $this->company->fetch($id);
1000 if (!$result) {
1001 throw new RestException(404, 'Thirdparty not found');
1002 }
1003
1004 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1005
1006 return $result;
1007 }
1008
1026 public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1027 {
1028 $obj_ret = array();
1029
1030 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1031 throw new RestException(401);
1032 }
1033
1034 if (empty($id)) {
1035 throw new RestException(400, 'Thirdparty ID is mandatory');
1036 }
1037
1038 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1039 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1040 }
1041
1042 $result = $this->company->fetch($id);
1043 if (!$result) {
1044 throw new RestException(404, 'Thirdparty not found');
1045 }
1046
1047
1048 $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";
1049 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1050 $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1051 if ($filter == "available") {
1052 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1053 }
1054 if ($filter == "used") {
1055 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1056 }
1057
1058 $sql .= $this->db->order($sortfield, $sortorder);
1059
1060 $result = $this->db->query($sql);
1061 if (!$result) {
1062 throw new RestException(503, $this->db->lasterror());
1063 } else {
1064 $num = $this->db->num_rows($result);
1065 while ($obj = $this->db->fetch_object($result)) {
1066 $obj_ret[] = $obj;
1067 }
1068 }
1069
1070 return $obj_ret;
1071 }
1072
1073
1074
1089 {
1090 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1091 throw new RestException(401);
1092 }
1093 if (empty($id)) {
1094 throw new RestException(400, 'Thirdparty ID is mandatory');
1095 }
1096
1097 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1098 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1099 }
1100
1101 /*$result = $this->thirdparty->fetch($id);
1102 if( ! $result ) {
1103 throw new RestException(404, 'Thirdparty not found');
1104 }*/
1105
1106 $invoice = new Facture($this->db);
1107 $result = $invoice->list_replacable_invoices($id);
1108 if ($result < 0) {
1109 throw new RestException(405, $invoice->error);
1110 }
1111
1112 return $result;
1113 }
1114
1132 {
1133 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1134 throw new RestException(401);
1135 }
1136 if (empty($id)) {
1137 throw new RestException(400, 'Thirdparty ID is mandatory');
1138 }
1139
1140 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1141 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1142 }
1143
1144 /*$result = $this->thirdparty->fetch($id);
1145 if( ! $result ) {
1146 throw new RestException(404, 'Thirdparty not found');
1147 }*/
1148
1149 $invoice = new Facture($this->db);
1150 $result = $invoice->list_qualified_avoir_invoices($id);
1151 if ($result < 0) {
1152 throw new RestException(405, $invoice->error);
1153 }
1154
1155 return $result;
1156 }
1157
1167 public function getCompanyBankAccount($id)
1168 {
1169 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1170 throw new RestException(401);
1171 }
1172 if (empty($id)) {
1173 throw new RestException(400, 'Thirdparty ID is mandatory');
1174 }
1175
1176 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1177 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1178 }
1179
1184 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1185 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1186 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1187 if ($id) {
1188 $sql .= " WHERE fk_soc = ".((int) $id);
1189 }
1190
1191 $result = $this->db->query($sql);
1192
1193 if ($this->db->num_rows($result) == 0) {
1194 throw new RestException(404, 'Account not found');
1195 }
1196
1197 $i = 0;
1198
1199 $accounts = array();
1200
1201 if ($result) {
1202 $num = $this->db->num_rows($result);
1203 while ($i < $num) {
1204 $obj = $this->db->fetch_object($result);
1205 $account = new CompanyBankAccount($this->db);
1206 if ($account->fetch($obj->rowid)) {
1207 $accounts[] = $account;
1208 }
1209 $i++;
1210 }
1211 } else {
1212 throw new RestException(404, 'Account not found');
1213 }
1214
1215
1216 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1217
1218 $returnAccounts = array();
1219
1220 foreach ($accounts as $account) {
1221 $object = array();
1222 foreach ($account as $key => $value) {
1223 if (in_array($key, $fields)) {
1224 $object[$key] = $value;
1225 }
1226 }
1227 $returnAccounts[] = $object;
1228 }
1229
1230 return $returnAccounts;
1231 }
1232
1242 public function createCompanyBankAccount($id, $request_data = null)
1243 {
1244 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1245 throw new RestException(401);
1246 }
1247 if ($this->company->fetch($id) <= 0) {
1248 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1249 }
1250 $account = new CompanyBankAccount($this->db);
1251
1252 $account->socid = $id;
1253
1254 foreach ($request_data as $field => $value) {
1255 $account->$field = $value;
1256 }
1257
1258 if ($account->create(DolibarrApiAccess::$user) < 0) {
1259 throw new RestException(500, 'Error creating Company Bank account');
1260 }
1261
1262 if (empty($account->rum)) {
1263 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1264 $prelevement = new BonPrelevement($this->db);
1265 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1266 $account->date_rum = dol_now();
1267 }
1268
1269 if ($account->update(DolibarrApiAccess::$user) < 0) {
1270 throw new RestException(500, 'Error updating values');
1271 }
1272
1273 return $this->_cleanObjectDatas($account);
1274 }
1275
1287 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1288 {
1289 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1290 throw new RestException(401);
1291 }
1292 if ($this->company->fetch($id) <= 0) {
1293 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1294 }
1295 $account = new CompanyBankAccount($this->db);
1296
1297 $account->fetch($bankaccount_id, $id, -1, '');
1298
1299 if ($account->socid != $id) {
1300 throw new RestException(401);
1301 }
1302
1303
1304 foreach ($request_data as $field => $value) {
1305 $account->$field = $value;
1306 }
1307
1308 if (empty($account->rum)) {
1309 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1310 $prelevement = new BonPrelevement($this->db);
1311 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1312 $account->date_rum = dol_now();
1313 }
1314
1315 if ($account->update(DolibarrApiAccess::$user) < 0) {
1316 throw new RestException(500, 'Error updating values');
1317 }
1318
1319 return $this->_cleanObjectDatas($account);
1320 }
1321
1332 public function deleteCompanyBankAccount($id, $bankaccount_id)
1333 {
1334 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1335 throw new RestException(401);
1336 }
1337
1338 $account = new CompanyBankAccount($this->db);
1339
1340 $account->fetch($bankaccount_id);
1341
1342 if (!$account->socid == $id) {
1343 throw new RestException(401);
1344 }
1345
1346 return $account->delete(DolibarrApiAccess::$user);
1347 }
1348
1359 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1360 {
1361 global $conf, $langs;
1362
1363 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1364
1365 if ($this->company->fetch($id) <= 0) {
1366 throw new RestException(404, 'Thirdparty not found');
1367 }
1368
1369 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1370 throw new RestException(401);
1371 }
1372
1373 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1374
1375 $this->company->fk_bank = $this->company->fk_account;
1376 $this->company->fk_account = $this->company->fk_account;
1377
1378 $outputlangs = $langs;
1379 $newlang = '';
1380
1381 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1382 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1383 if (isset($this->company->thirdparty->default_lang)) {
1384 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1385 } elseif (isset($this->company->default_lang)) {
1386 $newlang = $this->company->default_lang; // for thirdparty
1387 }
1388 }
1389 if (!empty($newlang)) {
1390 $outputlangs = new Translate("", $conf);
1391 $outputlangs->setDefaultLang($newlang);
1392 }
1393
1394 $sql = "SELECT rowid";
1395 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1396 if ($id) {
1397 $sql .= " WHERE fk_soc = ".((int) $id);
1398 }
1399 if ($companybankid) {
1400 $sql .= " AND rowid = ".((int) $companybankid);
1401 }
1402
1403 $i = 0;
1404 $accounts = array();
1405
1406 $result = $this->db->query($sql);
1407 if ($result) {
1408 if ($this->db->num_rows($result) == 0) {
1409 throw new RestException(404, 'Bank account not found');
1410 }
1411
1412 $num = $this->db->num_rows($result);
1413 while ($i < $num) {
1414 $obj = $this->db->fetch_object($result);
1415
1416 $account = new CompanyBankAccount($this->db);
1417 if ($account->fetch($obj->rowid)) {
1418 $accounts[] = $account;
1419 }
1420 $i++;
1421 }
1422 } else {
1423 throw new RestException(500, 'Sql error '.$this->db->lasterror());
1424 }
1425
1426 $moreparams = array(
1427 'use_companybankid' => $accounts[0]->id,
1428 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1429 );
1430
1431 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1432
1433 if ($result > 0) {
1434 return array("success" => $result);
1435 } else {
1436 throw new RestException(500, 'Error generating the document '.$this->company->error);
1437 }
1438 }
1439
1452 public function getSocieteAccounts($id, $site = null)
1453 {
1454 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1455 throw new RestException(401);
1456 }
1457
1458 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1459 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1460 }
1461
1465 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1466 $sql .= " WHERE fk_soc = ".((int) $id);
1467 if ($site) {
1468 $sql .= " AND site ='".$this->db->escape($site)."'";
1469 }
1470
1471 $result = $this->db->query($sql);
1472
1473 if ($result && $this->db->num_rows($result) == 0) {
1474 throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1475 }
1476
1477 $i = 0;
1478
1479 $accounts = array();
1480
1481 $num = $this->db->num_rows($result);
1482 while ($i < $num) {
1483 $obj = $this->db->fetch_object($result);
1484 $account = new SocieteAccount($this->db);
1485
1486 if ($account->fetch($obj->rowid)) {
1487 $accounts[] = $account;
1488 }
1489 $i++;
1490 }
1491
1492 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1493
1494 $returnAccounts = array();
1495
1496 foreach ($accounts as $account) {
1497 $object = array();
1498 foreach ($account as $key => $value) {
1499 if (in_array($key, $fields)) {
1500 $object[$key] = $value;
1501 }
1502 }
1503 $returnAccounts[] = $object;
1504 }
1505
1506 return $returnAccounts;
1507 }
1508
1528 public function createSocieteAccount($id, $request_data = null)
1529 {
1530 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1531 throw new RestException(401);
1532 }
1533
1534 if (!isset($request_data['site'])) {
1535 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1536 }
1537
1538 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1539 $result = $this->db->query($sql);
1540
1541 if ($result && $this->db->num_rows($result) == 0) {
1542 $account = new SocieteAccount($this->db);
1543 if (!isset($request_data['login'])) {
1544 $account->login = "";
1545 }
1546 $account->fk_soc = $id;
1547
1548 foreach ($request_data as $field => $value) {
1549 $account->$field = $value;
1550 }
1551
1552 if ($account->create(DolibarrApiAccess::$user) < 0) {
1553 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1554 }
1555
1556 $this->_cleanObjectDatas($account);
1557
1558 return $account;
1559 } else {
1560 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1561 }
1562 }
1563
1586 public function putSocieteAccount($id, $site, $request_data = null)
1587 {
1588 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1589 throw new RestException(401);
1590 }
1591
1592 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1593 $result = $this->db->query($sql);
1594
1595 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1596 if ($result && $this->db->num_rows == 0) {
1597 if (!isset($request_data['key_account'])) {
1598 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1599 }
1600 $account = new SocieteAccount($this->db);
1601 if (!isset($request_data['login'])) {
1602 $account->login = "";
1603 }
1604
1605 foreach ($request_data as $field => $value) {
1606 $account->$field = $value;
1607 }
1608
1609 $account->fk_soc = $id;
1610 $account->site = $site;
1611
1612 if ($account->create(DolibarrApiAccess::$user) < 0) {
1613 throw new RestException(500, 'Error creating SocieteAccount entity.');
1614 }
1615 // We found an existing SocieteAccount entity, we are replacing it
1616 } else {
1617 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1618 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1619 $result = $this->db->query($sql);
1620
1621 if ($result && $this->db->num_rows($result) !== 0) {
1622 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.");
1623 }
1624 }
1625
1626 $obj = $this->db->fetch_object($result);
1627
1628 $account = new SocieteAccount($this->db);
1629 $account->id = $obj->rowid;
1630 $account->fk_soc = $id;
1631 $account->site = $site;
1632 if (!isset($request_data['login'])) {
1633 $account->login = "";
1634 }
1635 $account->fk_user_creat = $obj->fk_user_creat;
1636 $account->date_creation = $obj->date_creation;
1637
1638 foreach ($request_data as $field => $value) {
1639 $account->$field = $value;
1640 }
1641
1642 if ($account->update(DolibarrApiAccess::$user) < 0) {
1643 throw new RestException(500, 'Error updating SocieteAccount entity.');
1644 }
1645 }
1646
1647 $this->_cleanObjectDatas($account);
1648
1649 return $account;
1650 }
1651
1668 public function patchSocieteAccount($id, $site, $request_data = null)
1669 {
1670 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1671 throw new RestException(401);
1672 }
1673
1674 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1675 $result = $this->db->query($sql);
1676
1677 if ($result && $this->db->num_rows($result) == 0) {
1678 throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1679 } else {
1680 // If the user tries to edit the site member, we check first if
1681 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1682 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1683 $result = $this->db->query($sql);
1684
1685 if ($result && $this->db->num_rows($result) !== 0) {
1686 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.");
1687 }
1688 }
1689
1690 $obj = $this->db->fetch_object($result);
1691 $account = new SocieteAccount($this->db);
1692 $account->fetch($obj->rowid);
1693
1694 foreach ($request_data as $field => $value) {
1695 $account->$field = $value;
1696 }
1697
1698 if ($account->update(DolibarrApiAccess::$user) < 0) {
1699 throw new RestException(500, 'Error updating SocieteAccount account');
1700 }
1701
1702 $this->_cleanObjectDatas($account);
1703
1704 return $account;
1705 }
1706 }
1707
1721 public function deleteSocieteAccount($id, $site)
1722 {
1723 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1724 throw new RestException(401);
1725 }
1726
1727 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1728 $result = $this->db->query($sql);
1729
1730 if ($result && $this->db->num_rows($result) == 0) {
1731 throw new RestException(404);
1732 } else {
1733 $obj = $this->db->fetch_object($result);
1734 $account = new SocieteAccount($this->db);
1735 $account->fetch($obj->rowid);
1736
1737 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1738 throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1739 }
1740 }
1741 }
1742
1755 public function deleteSocieteAccounts($id)
1756 {
1757 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1758 throw new RestException(401);
1759 }
1760
1765 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1766 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1767
1768 $result = $this->db->query($sql);
1769
1770 if ($result && $this->db->num_rows($result) == 0) {
1771 throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1772 } else {
1773 $i = 0;
1774
1775 $num = $this->db->num_rows($result);
1776 while ($i < $num) {
1777 $obj = $this->db->fetch_object($result);
1778 $account = new SocieteAccount($this->db);
1779 $account->fetch($obj->rowid);
1780
1781 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1782 throw new RestException(500, 'Error while deleting gateways attached to this third party');
1783 }
1784 $i++;
1785 }
1786 }
1787 }
1788
1789 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1796 protected function _cleanObjectDatas($object)
1797 {
1798 // phpcs:enable
1799 $object = parent::_cleanObjectDatas($object);
1800
1801 unset($object->nom); // ->name already defined and nom deprecated
1802 unset($object->name_bis); // ->name_alias already defined
1803 unset($object->note); // ->note_private and note_public already defined
1804 unset($object->departement);
1805 unset($object->departement_code);
1806 unset($object->pays);
1807 unset($object->particulier);
1808 unset($object->prefix_comm);
1809
1810 unset($object->siren);
1811 unset($object->siret);
1812 unset($object->ape);
1813
1814 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.
1815
1816 unset($object->total_ht);
1817 unset($object->total_tva);
1818 unset($object->total_localtax1);
1819 unset($object->total_localtax2);
1820 unset($object->total_ttc);
1821
1822 unset($object->lines);
1823 unset($object->thirdparty);
1824
1825 unset($object->fk_delivery_address); // deprecated feature
1826
1827 return $object;
1828 }
1829
1838 private function _validate($data)
1839 {
1840 $thirdparty = array();
1841 foreach (Thirdparties::$FIELDS as $field) {
1842 if (!isset($data[$field])) {
1843 throw new RestException(400, "$field field missing");
1844 }
1845 $thirdparty[$field] = $data[$field];
1846 }
1847 return $thirdparty;
1848 }
1849
1871 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1872 {
1873 global $conf;
1874
1875 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1876 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
1877 }
1878
1879 if ($rowid === 0) {
1880 $result = $this->company->initAsSpecimen();
1881 } else {
1882 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1883 }
1884 if (!$result) {
1885 throw new RestException(404, 'Thirdparty not found');
1886 }
1887
1888 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1889 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
1890 }
1891 if (isModEnabled('mailing')) {
1892 $this->company->getNoEmail();
1893 }
1894
1895 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1896 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1897 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1898 } else {
1899 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1900 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1901 }
1902
1903 $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1904 $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1905 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1906 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1907
1908 return $this->_cleanObjectDatas($this->company);
1909 }
1910}
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
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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.