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 if ($field == 'array_options' && is_array($value)) {
306 foreach ($value as $index => $val) {
307 $this->company->array_options[$index] = $this->_checkValForAPI($field, $val, $this->company);
308 }
309 continue;
310 }
311 $this->company->$field = $value;
312 }
313
314 if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
315 $this->company->setNoEmail($this->company->no_email);
316 }
317
318 if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update', 1) > 0) {
319 return $this->get($id);
320 } else {
321 throw new RestException(500, $this->company->error);
322 }
323 }
324
339 public function merge($id, $idtodelete)
340 {
341 global $hookmanager;
342
343 $error = 0;
344
345 if ($id == $idtodelete) {
346 throw new RestException(400, 'Try to merge a thirdparty into itself');
347 }
348
349 if (!DolibarrApiAccess::$user->rights->societe->creer) {
350 throw new RestException(401);
351 }
352
353 $result = $this->company->fetch($id); // include the fetch of extra fields
354 if (!$result) {
355 throw new RestException(404, 'Thirdparty not found');
356 }
357
358 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
359 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
360 }
361
362 $this->companytoremove = new Societe($this->db);
363
364 $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
365 if (!$result) {
366 throw new RestException(404, 'Thirdparty not found');
367 }
368
369 if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
370 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
371 }
372
373 $soc_origin = $this->companytoremove;
374 $object = $this->company;
375 $user = DolibarrApiAccess::$user;
376
377
378 // Call same code than into action 'confirm_merge'
379
380
381 $this->db->begin();
382
383 // Recopy some data
384 $object->client = $object->client | $soc_origin->client;
385 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
386 $listofproperties = array(
387 'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
388 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
389 'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
390 'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
391 'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
392 'model_pdf', 'fk_projet'
393 );
394 foreach ($listofproperties as $property) {
395 if (empty($object->$property)) {
396 $object->$property = $soc_origin->$property;
397 }
398 }
399
400 // Concat some data
401 $listofproperties = array(
402 'note_public', 'note_private'
403 );
404 foreach ($listofproperties as $property) {
405 $object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
406 }
407
408 // Merge extrafields
409 if (is_array($soc_origin->array_options)) {
410 foreach ($soc_origin->array_options as $key => $val) {
411 if (empty($object->array_options[$key])) {
412 $object->array_options[$key] = $val;
413 }
414 }
415 }
416
417 // Merge categories
418 $static_cat = new Categorie($this->db);
419 $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
420 $object->setCategories($custcats, 'customer');
421 $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
422 $object->setCategories($suppcats, 'supplier');
423
424 // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
425 if ($soc_origin->code_client == $object->code_client
426 || $soc_origin->code_fournisseur == $object->code_fournisseur
427 || $soc_origin->barcode == $object->barcode) {
428 dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
429 $soc_origin->code_client = '';
430 $soc_origin->code_fournisseur = '';
431 $soc_origin->barcode = '';
432 $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
433 }
434
435 // Update
436 $result = $object->update($object->id, $user, 0, 1, 1, 'merge');
437 if ($result < 0) {
438 $error++;
439 }
440
441 // Move links
442 if (!$error) {
443 // This list is also into the societe/card.php file
444 // TODO Mutualise the list into object societe.class.php
445 $objects = array(
446 'Adherent' => '/adherents/class/adherent.class.php',
447 'Don' => '/don/class/don.class.php',
448 'Societe' => '/societe/class/societe.class.php',
449 //'Categorie' => '/categories/class/categorie.class.php',
450 'ActionComm' => '/comm/action/class/actioncomm.class.php',
451 'Propal' => '/comm/propal/class/propal.class.php',
452 'Commande' => '/commande/class/commande.class.php',
453 'Facture' => '/compta/facture/class/facture.class.php',
454 'FactureRec' => '/compta/facture/class/facture-rec.class.php',
455 'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
456 'Mo' => '/mrp/class/mo.class.php',
457 'Contact' => '/contact/class/contact.class.php',
458 'Contrat' => '/contrat/class/contrat.class.php',
459 'Expedition' => '/expedition/class/expedition.class.php',
460 'Fichinter' => '/fichinter/class/fichinter.class.php',
461 'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
462 'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
463 'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
464 'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
465 'Delivery' => '/delivery/class/delivery.class.php',
466 'Product' => '/product/class/product.class.php',
467 'Project' => '/projet/class/project.class.php',
468 'Ticket' => '/ticket/class/ticket.class.php',
469 'User' => '/user/class/user.class.php',
470 'Account' => '/compta/bank/class/account.class.php',
471 'ConferenceOrBoothAttendee' => '/eventorganization/class/conferenceorboothattendee.class.php'
472 );
473
474 //First, all core objects must update their tables
475 foreach ($objects as $object_name => $object_file) {
476 require_once DOL_DOCUMENT_ROOT.$object_file;
477
478 if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
479 $error++;
480 //setEventMessages($this->db->lasterror(), null, 'errors');
481 }
482 }
483 }
484
485 // External modules should update their ones too
486 if (!$error) {
487 $parameters = array('soc_origin' => $soc_origin->id, 'soc_dest' => $object->id);
488 $reshook = $hookmanager->executeHooks('replaceThirdparty', $parameters, $soc_dest, $action);
489
490 if ($reshook < 0) {
491 //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
492 $error++;
493 }
494 }
495
496
497 if (!$error) {
498 $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
499
500 // Call trigger
501 $result = $object->call_trigger('COMPANY_MODIFY', $user);
502 if ($result < 0) {
503 //setEventMessages($object->error, $object->errors, 'errors');
504 $error++;
505 }
506 // End call triggers
507 }
508
509 if (!$error) {
510 //We finally remove the old thirdparty
511 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
512 $error++;
513 }
514 }
515
516 // End of merge
517
518 if ($error) {
519 $this->db->rollback();
520
521 throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
522 } else {
523 $this->db->commit();
524 }
525
526 return $this->get($id);
527 }
528
535 public function delete($id)
536 {
537 if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
538 throw new RestException(401);
539 }
540 $result = $this->company->fetch($id);
541 if (!$result) {
542 throw new RestException(404, 'Thirdparty not found');
543 }
544 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
545 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
546 }
547 $this->company->oldcopy = clone $this->company;
548
549 $res = $this->company->delete($id);
550 if ($res < 0) {
551 throw new RestException(500, "Can't delete, error occurs");
552 } elseif ($res == 0) {
553 throw new RestException(409, "Can't delete, that product is probably used");
554 }
555
556 return array(
557 'success' => array(
558 'code' => 200,
559 'message' => 'Object deleted'
560 )
561 );
562 }
563
579 public function setThirdpartyPriceLevel($id, $priceLevel)
580 {
581 global $conf;
582
583 if (!isModEnabled('societe')) {
584 throw new RestException(501, 'Module "Thirdparties" needed for this request');
585 }
586
587 if (!isModEnabled("product")) {
588 throw new RestException(501, 'Module "Products" needed for this request');
589 }
590
591 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
592 throw new RestException(501, 'Multiprices features activation needed for this request');
593 }
594
595 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
596 throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
597 }
598
599 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
600 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
601 }
602
603 $result = $this->company->fetch($id);
604 if ($result < 0) {
605 throw new RestException(404, 'Thirdparty '.$id.' not found');
606 }
607
608 if (empty($result)) {
609 throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
610 }
611
612 if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
613 throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
614 }
615
616 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
617 if ($result <= 0) {
618 throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
619 }
620
621 return $this->_cleanObjectDatas($this->company);
622 }
623
636 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
637 {
638 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
639 throw new RestException(401);
640 }
641
642 $result = $this->company->fetch($id);
643 if (!$result) {
644 throw new RestException(404, 'Thirdparty not found');
645 }
646
647 $categories = new Categorie($this->db);
648
649 $arrayofcateg = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
650
651 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
652 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
653 }
654
655 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) { // To fix a return of 0 instead of empty array of method getListForItem
656 return array();
657 }
658
659 return $arrayofcateg;
660 }
661
671 public function addCategory($id, $category_id)
672 {
673 if (!DolibarrApiAccess::$user->rights->societe->creer) {
674 throw new RestException(401);
675 }
676
677 $result = $this->company->fetch($id);
678 if (!$result) {
679 throw new RestException(404, 'Thirdparty not found');
680 }
681 $category = new Categorie($this->db);
682 $result = $category->fetch($category_id);
683 if (!$result) {
684 throw new RestException(404, 'category not found');
685 }
686
687 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
688 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
689 }
690 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
691 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
692 }
693
694 $category->add_type($this->company, 'customer');
695
696 return $this->_cleanObjectDatas($this->company);
697 }
698
709 public function deleteCategory($id, $category_id)
710 {
711 if (!DolibarrApiAccess::$user->rights->societe->creer) {
712 throw new RestException(401);
713 }
714
715 $result = $this->company->fetch($id);
716 if (!$result) {
717 throw new RestException(404, 'Thirdparty not found');
718 }
719 $category = new Categorie($this->db);
720 $result = $category->fetch($category_id);
721 if (!$result) {
722 throw new RestException(404, 'category not found');
723 }
724
725 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
726 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
727 }
728 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
729 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 }
731
732 $category->del_type($this->company, 'customer');
733
734 return $this->_cleanObjectDatas($this->company);
735 }
736
750 public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
751 {
752 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
753 throw new RestException(401);
754 }
755
756 $result = $this->company->fetch($id);
757 if (!$result) {
758 throw new RestException(404, 'Thirdparty not found');
759 }
760
761 $categories = new Categorie($this->db);
762
763 $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
764
765 if (is_numeric($result) && $result < 0) {
766 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
767 }
768
769 if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
770 return array();
771 }
772
773 return $result;
774 }
775
786 public function addSupplierCategory($id, $category_id)
787 {
788 if (!DolibarrApiAccess::$user->rights->societe->creer) {
789 throw new RestException(401);
790 }
791
792 $result = $this->company->fetch($id);
793 if (!$result) {
794 throw new RestException(404, 'Thirdparty not found');
795 }
796 $category = new Categorie($this->db);
797 $result = $category->fetch($category_id);
798 if (!$result) {
799 throw new RestException(404, 'category not found');
800 }
801
802 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
803 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
804 }
805 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
806 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
807 }
808
809 $category->add_type($this->company, 'supplier');
810
811 return $this->_cleanObjectDatas($this->company);
812 }
813
824 public function deleteSupplierCategory($id, $category_id)
825 {
826 if (!DolibarrApiAccess::$user->rights->societe->creer) {
827 throw new RestException(401);
828 }
829
830 $result = $this->company->fetch($id);
831 if (!$result) {
832 throw new RestException(404, 'Thirdparty not found');
833 }
834 $category = new Categorie($this->db);
835 $result = $category->fetch($category_id);
836 if (!$result) {
837 throw new RestException(404, 'category not found');
838 }
839
840 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
841 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
842 }
843 if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
844 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 }
846
847 $category->del_type($this->company, 'supplier');
848
849 return $this->_cleanObjectDatas($this->company);
850 }
851
852
867 public function getOutStandingProposals($id, $mode = 'customer')
868 {
869 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
870 throw new RestException(401);
871 }
872
873 if (empty($id)) {
874 throw new RestException(400, 'Thirdparty ID is mandatory');
875 }
876
877 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
878 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
879 }
880
881 $result = $this->company->fetch($id);
882 if (!$result) {
883 throw new RestException(404, 'Thirdparty not found');
884 }
885
886 $result = $this->company->getOutstandingProposals($mode);
887
888 unset($result['total_ht']);
889 unset($result['total_ttc']);
890
891 return $result;
892 }
893
894
909 public function getOutStandingOrder($id, $mode = 'customer')
910 {
911 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
912 throw new RestException(401);
913 }
914
915 if (empty($id)) {
916 throw new RestException(400, 'Thirdparty ID is mandatory');
917 }
918
919 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
920 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 }
922
923 $result = $this->company->fetch($id);
924 if (!$result) {
925 throw new RestException(404, 'Thirdparty not found');
926 }
927
928 $result = $this->company->getOutstandingOrders($mode);
929
930 unset($result['total_ht']);
931 unset($result['total_ttc']);
932
933 return $result;
934 }
935
950 public function getOutStandingInvoices($id, $mode = 'customer')
951 {
952 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
953 throw new RestException(401);
954 }
955
956 if (empty($id)) {
957 throw new RestException(400, 'Thirdparty ID is mandatory');
958 }
959
960 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
961 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
962 }
963
964 $result = $this->company->fetch($id);
965 if (!$result) {
966 throw new RestException(404, 'Thirdparty not found');
967 }
968
969 $result = $this->company->getOutstandingBills($mode);
970
971 unset($result['total_ht']);
972 unset($result['total_ttc']);
973
974 return $result;
975 }
976
991 public function getSalesRepresentatives($id, $mode = 0)
992 {
993 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
994 throw new RestException(401);
995 }
996
997 if (empty($id)) {
998 throw new RestException(400, 'Thirdparty ID is mandatory');
999 }
1000
1001 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1002 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1003 }
1004
1005 $result = $this->company->fetch($id);
1006 if (!$result) {
1007 throw new RestException(404, 'Thirdparty not found');
1008 }
1009
1010 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1011
1012 return $result;
1013 }
1014
1032 public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1033 {
1034 $obj_ret = array();
1035
1036 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1037 throw new RestException(401);
1038 }
1039
1040 if (empty($id)) {
1041 throw new RestException(400, 'Thirdparty ID is mandatory');
1042 }
1043
1044 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1045 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1046 }
1047
1048 $result = $this->company->fetch($id);
1049 if (!$result) {
1050 throw new RestException(404, 'Thirdparty not found');
1051 }
1052
1053
1054 $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";
1055 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1056 $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1057 if ($filter == "available") {
1058 $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1059 }
1060 if ($filter == "used") {
1061 $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1062 }
1063
1064 $sql .= $this->db->order($sortfield, $sortorder);
1065
1066 $result = $this->db->query($sql);
1067 if (!$result) {
1068 throw new RestException(503, $this->db->lasterror());
1069 } else {
1070 $num = $this->db->num_rows($result);
1071 while ($obj = $this->db->fetch_object($result)) {
1072 $obj_ret[] = $obj;
1073 }
1074 }
1075
1076 return $obj_ret;
1077 }
1078
1079
1080
1095 {
1096 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1097 throw new RestException(401);
1098 }
1099 if (empty($id)) {
1100 throw new RestException(400, 'Thirdparty ID is mandatory');
1101 }
1102
1103 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1104 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1105 }
1106
1107 /*$result = $this->thirdparty->fetch($id);
1108 if( ! $result ) {
1109 throw new RestException(404, 'Thirdparty not found');
1110 }*/
1111
1112 $invoice = new Facture($this->db);
1113 $result = $invoice->list_replacable_invoices($id);
1114 if ($result < 0) {
1115 throw new RestException(405, $invoice->error);
1116 }
1117
1118 return $result;
1119 }
1120
1138 {
1139 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
1140 throw new RestException(401);
1141 }
1142 if (empty($id)) {
1143 throw new RestException(400, 'Thirdparty ID is mandatory');
1144 }
1145
1146 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1147 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1148 }
1149
1150 /*$result = $this->thirdparty->fetch($id);
1151 if( ! $result ) {
1152 throw new RestException(404, 'Thirdparty not found');
1153 }*/
1154
1155 $invoice = new Facture($this->db);
1156 $result = $invoice->list_qualified_avoir_invoices($id);
1157 if ($result < 0) {
1158 throw new RestException(405, $invoice->error);
1159 }
1160
1161 return $result;
1162 }
1163
1173 public function getCompanyBankAccount($id)
1174 {
1175 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1176 throw new RestException(401);
1177 }
1178 if (empty($id)) {
1179 throw new RestException(400, 'Thirdparty ID is mandatory');
1180 }
1181
1182 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1183 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1184 }
1185
1190 $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1191 $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1192 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1193 if ($id) {
1194 $sql .= " WHERE fk_soc = ".((int) $id);
1195 }
1196
1197 $result = $this->db->query($sql);
1198
1199 if ($this->db->num_rows($result) == 0) {
1200 throw new RestException(404, 'Account not found');
1201 }
1202
1203 $i = 0;
1204
1205 $accounts = array();
1206
1207 if ($result) {
1208 $num = $this->db->num_rows($result);
1209 while ($i < $num) {
1210 $obj = $this->db->fetch_object($result);
1211 $account = new CompanyBankAccount($this->db);
1212 if ($account->fetch($obj->rowid)) {
1213 $accounts[] = $account;
1214 }
1215 $i++;
1216 }
1217 } else {
1218 throw new RestException(404, 'Account not found');
1219 }
1220
1221
1222 $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1223
1224 $returnAccounts = array();
1225
1226 foreach ($accounts as $account) {
1227 $object = array();
1228 foreach ($account as $key => $value) {
1229 if (in_array($key, $fields)) {
1230 $object[$key] = $value;
1231 }
1232 }
1233 $returnAccounts[] = $object;
1234 }
1235
1236 return $returnAccounts;
1237 }
1238
1248 public function createCompanyBankAccount($id, $request_data = null)
1249 {
1250 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1251 throw new RestException(401);
1252 }
1253 if ($this->company->fetch($id) <= 0) {
1254 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1255 }
1256 $account = new CompanyBankAccount($this->db);
1257
1258 $account->socid = $id;
1259
1260 foreach ($request_data as $field => $value) {
1261 $account->$field = $value;
1262 }
1263
1264 if ($account->create(DolibarrApiAccess::$user) < 0) {
1265 throw new RestException(500, 'Error creating Company Bank account');
1266 }
1267
1268 if (empty($account->rum)) {
1269 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1270 $prelevement = new BonPrelevement($this->db);
1271 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1272 $account->date_rum = dol_now();
1273 }
1274
1275 if ($account->update(DolibarrApiAccess::$user) < 0) {
1276 throw new RestException(500, 'Error updating values');
1277 }
1278
1279 return $this->_cleanObjectDatas($account);
1280 }
1281
1293 public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1294 {
1295 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1296 throw new RestException(401);
1297 }
1298 if ($this->company->fetch($id) <= 0) {
1299 throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1300 }
1301 $account = new CompanyBankAccount($this->db);
1302
1303 $account->fetch($bankaccount_id, $id, -1, '');
1304
1305 if ($account->socid != $id) {
1306 throw new RestException(401);
1307 }
1308
1309
1310 foreach ($request_data as $field => $value) {
1311 $account->$field = $value;
1312 }
1313
1314 if (empty($account->rum)) {
1315 require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1316 $prelevement = new BonPrelevement($this->db);
1317 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1318 $account->date_rum = dol_now();
1319 }
1320
1321 if ($account->update(DolibarrApiAccess::$user) < 0) {
1322 throw new RestException(500, 'Error updating values');
1323 }
1324
1325 return $this->_cleanObjectDatas($account);
1326 }
1327
1338 public function deleteCompanyBankAccount($id, $bankaccount_id)
1339 {
1340 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1341 throw new RestException(401);
1342 }
1343
1344 $account = new CompanyBankAccount($this->db);
1345
1346 $account->fetch($bankaccount_id);
1347
1348 if (!$account->socid == $id) {
1349 throw new RestException(401);
1350 }
1351
1352 return $account->delete(DolibarrApiAccess::$user);
1353 }
1354
1365 public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1366 {
1367 global $conf, $langs;
1368
1369 $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1370
1371 if ($this->company->fetch($id) <= 0) {
1372 throw new RestException(404, 'Thirdparty not found');
1373 }
1374
1375 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1376 throw new RestException(401);
1377 }
1378
1379 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1380
1381 $this->company->fk_bank = $this->company->fk_account;
1382 $this->company->fk_account = $this->company->fk_account;
1383
1384 $outputlangs = $langs;
1385 $newlang = '';
1386
1387 //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1388 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1389 if (isset($this->company->thirdparty->default_lang)) {
1390 $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1391 } elseif (isset($this->company->default_lang)) {
1392 $newlang = $this->company->default_lang; // for thirdparty
1393 }
1394 }
1395 if (!empty($newlang)) {
1396 $outputlangs = new Translate("", $conf);
1397 $outputlangs->setDefaultLang($newlang);
1398 }
1399
1400 $sql = "SELECT rowid";
1401 $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1402 if ($id) {
1403 $sql .= " WHERE fk_soc = ".((int) $id);
1404 }
1405 if ($companybankid) {
1406 $sql .= " AND rowid = ".((int) $companybankid);
1407 }
1408
1409 $i = 0;
1410 $accounts = array();
1411
1412 $result = $this->db->query($sql);
1413 if ($result) {
1414 if ($this->db->num_rows($result) == 0) {
1415 throw new RestException(404, 'Bank account not found');
1416 }
1417
1418 $num = $this->db->num_rows($result);
1419 while ($i < $num) {
1420 $obj = $this->db->fetch_object($result);
1421
1422 $account = new CompanyBankAccount($this->db);
1423 if ($account->fetch($obj->rowid)) {
1424 $accounts[] = $account;
1425 }
1426 $i++;
1427 }
1428 } else {
1429 throw new RestException(500, 'Sql error '.$this->db->lasterror());
1430 }
1431
1432 $moreparams = array(
1433 'use_companybankid' => $accounts[0]->id,
1434 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1435 );
1436
1437 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1438
1439 if ($result > 0) {
1440 return array("success" => $result);
1441 } else {
1442 throw new RestException(500, 'Error generating the document '.$this->company->error);
1443 }
1444 }
1445
1458 public function getSocieteAccounts($id, $site = null)
1459 {
1460 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1461 throw new RestException(401);
1462 }
1463
1464 if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1465 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1466 }
1467
1471 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1472 $sql .= " WHERE fk_soc = ".((int) $id);
1473 if ($site) {
1474 $sql .= " AND site ='".$this->db->escape($site)."'";
1475 }
1476
1477 $result = $this->db->query($sql);
1478
1479 if ($result && $this->db->num_rows($result) == 0) {
1480 throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1481 }
1482
1483 $i = 0;
1484
1485 $accounts = array();
1486
1487 $num = $this->db->num_rows($result);
1488 while ($i < $num) {
1489 $obj = $this->db->fetch_object($result);
1490 $account = new SocieteAccount($this->db);
1491
1492 if ($account->fetch($obj->rowid)) {
1493 $accounts[] = $account;
1494 }
1495 $i++;
1496 }
1497
1498 $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1499
1500 $returnAccounts = array();
1501
1502 foreach ($accounts as $account) {
1503 $object = array();
1504 foreach ($account as $key => $value) {
1505 if (in_array($key, $fields)) {
1506 $object[$key] = $value;
1507 }
1508 }
1509 $returnAccounts[] = $object;
1510 }
1511
1512 return $returnAccounts;
1513 }
1514
1534 public function createSocieteAccount($id, $request_data = null)
1535 {
1536 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1537 throw new RestException(401);
1538 }
1539
1540 if (!isset($request_data['site'])) {
1541 throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1542 }
1543
1544 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1545 $result = $this->db->query($sql);
1546
1547 if ($result && $this->db->num_rows($result) == 0) {
1548 $account = new SocieteAccount($this->db);
1549 if (!isset($request_data['login'])) {
1550 $account->login = "";
1551 }
1552 $account->fk_soc = $id;
1553
1554 foreach ($request_data as $field => $value) {
1555 $account->$field = $value;
1556 }
1557
1558 if ($account->create(DolibarrApiAccess::$user) < 0) {
1559 throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1560 }
1561
1562 $this->_cleanObjectDatas($account);
1563
1564 return $account;
1565 } else {
1566 throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1567 }
1568 }
1569
1592 public function putSocieteAccount($id, $site, $request_data = null)
1593 {
1594 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1595 throw new RestException(401);
1596 }
1597
1598 $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1599 $result = $this->db->query($sql);
1600
1601 // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1602 if ($result && $this->db->num_rows == 0) {
1603 if (!isset($request_data['key_account'])) {
1604 throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1605 }
1606 $account = new SocieteAccount($this->db);
1607 if (!isset($request_data['login'])) {
1608 $account->login = "";
1609 }
1610
1611 foreach ($request_data as $field => $value) {
1612 $account->$field = $value;
1613 }
1614
1615 $account->fk_soc = $id;
1616 $account->site = $site;
1617
1618 if ($account->create(DolibarrApiAccess::$user) < 0) {
1619 throw new RestException(500, 'Error creating SocieteAccount entity.');
1620 }
1621 // We found an existing SocieteAccount entity, we are replacing it
1622 } else {
1623 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1624 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1625 $result = $this->db->query($sql);
1626
1627 if ($result && $this->db->num_rows($result) !== 0) {
1628 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.");
1629 }
1630 }
1631
1632 $obj = $this->db->fetch_object($result);
1633
1634 $account = new SocieteAccount($this->db);
1635 $account->id = $obj->rowid;
1636 $account->fk_soc = $id;
1637 $account->site = $site;
1638 if (!isset($request_data['login'])) {
1639 $account->login = "";
1640 }
1641 $account->fk_user_creat = $obj->fk_user_creat;
1642 $account->date_creation = $obj->date_creation;
1643
1644 foreach ($request_data as $field => $value) {
1645 $account->$field = $value;
1646 }
1647
1648 if ($account->update(DolibarrApiAccess::$user) < 0) {
1649 throw new RestException(500, 'Error updating SocieteAccount entity.');
1650 }
1651 }
1652
1653 $this->_cleanObjectDatas($account);
1654
1655 return $account;
1656 }
1657
1674 public function patchSocieteAccount($id, $site, $request_data = null)
1675 {
1676 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1677 throw new RestException(401);
1678 }
1679
1680 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1681 $result = $this->db->query($sql);
1682
1683 if ($result && $this->db->num_rows($result) == 0) {
1684 throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1685 } else {
1686 // If the user tries to edit the site member, we check first if
1687 if (isset($request_data['site']) && $request_data['site'] !== $site) {
1688 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1689 $result = $this->db->query($sql);
1690
1691 if ($result && $this->db->num_rows($result) !== 0) {
1692 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.");
1693 }
1694 }
1695
1696 $obj = $this->db->fetch_object($result);
1697 $account = new SocieteAccount($this->db);
1698 $account->fetch($obj->rowid);
1699
1700 foreach ($request_data as $field => $value) {
1701 $account->$field = $value;
1702 }
1703
1704 if ($account->update(DolibarrApiAccess::$user) < 0) {
1705 throw new RestException(500, 'Error updating SocieteAccount account');
1706 }
1707
1708 $this->_cleanObjectDatas($account);
1709
1710 return $account;
1711 }
1712 }
1713
1727 public function deleteSocieteAccount($id, $site)
1728 {
1729 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1730 throw new RestException(401);
1731 }
1732
1733 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1734 $result = $this->db->query($sql);
1735
1736 if ($result && $this->db->num_rows($result) == 0) {
1737 throw new RestException(404);
1738 } else {
1739 $obj = $this->db->fetch_object($result);
1740 $account = new SocieteAccount($this->db);
1741 $account->fetch($obj->rowid);
1742
1743 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1744 throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1745 }
1746 }
1747 }
1748
1761 public function deleteSocieteAccounts($id)
1762 {
1763 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1764 throw new RestException(401);
1765 }
1766
1771 $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1772 $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1773
1774 $result = $this->db->query($sql);
1775
1776 if ($result && $this->db->num_rows($result) == 0) {
1777 throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1778 } else {
1779 $i = 0;
1780
1781 $num = $this->db->num_rows($result);
1782 while ($i < $num) {
1783 $obj = $this->db->fetch_object($result);
1784 $account = new SocieteAccount($this->db);
1785 $account->fetch($obj->rowid);
1786
1787 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1788 throw new RestException(500, 'Error while deleting gateways attached to this third party');
1789 }
1790 $i++;
1791 }
1792 }
1793 }
1794
1795 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1802 protected function _cleanObjectDatas($object)
1803 {
1804 // phpcs:enable
1805 $object = parent::_cleanObjectDatas($object);
1806
1807 unset($object->nom); // ->name already defined and nom deprecated
1808 unset($object->name_bis); // ->name_alias already defined
1809 unset($object->note); // ->note_private and note_public already defined
1810 unset($object->departement);
1811 unset($object->departement_code);
1812 unset($object->pays);
1813 unset($object->particulier);
1814 unset($object->prefix_comm);
1815
1816 unset($object->siren);
1817 unset($object->siret);
1818 unset($object->ape);
1819
1820 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.
1821
1822 unset($object->total_ht);
1823 unset($object->total_tva);
1824 unset($object->total_localtax1);
1825 unset($object->total_localtax2);
1826 unset($object->total_ttc);
1827
1828 unset($object->lines);
1829 unset($object->thirdparty);
1830
1831 unset($object->fk_delivery_address); // deprecated feature
1832
1833 return $object;
1834 }
1835
1844 private function _validate($data)
1845 {
1846 $thirdparty = array();
1847 foreach (Thirdparties::$FIELDS as $field) {
1848 if (!isset($data[$field])) {
1849 throw new RestException(400, "$field field missing");
1850 }
1851 $thirdparty[$field] = $data[$field];
1852 }
1853 return $thirdparty;
1854 }
1855
1877 private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1878 {
1879 global $conf;
1880
1881 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
1882 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
1883 }
1884
1885 if ($rowid === 0) {
1886 $result = $this->company->initAsSpecimen();
1887 } else {
1888 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1889 }
1890 if (!$result) {
1891 throw new RestException(404, 'Thirdparty not found');
1892 }
1893
1894 if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1895 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
1896 }
1897 if (isModEnabled('mailing')) {
1898 $this->company->getNoEmail();
1899 }
1900
1901 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1902 $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1903 $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1904 } else {
1905 $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1906 $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1907 }
1908
1909 $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1910 $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1911 $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1912 $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1913
1914 return $this->_cleanObjectDatas($this->company);
1915 }
1916}
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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
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.