dolibarr  16.0.5
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 
21 use 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->rights->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  if ($category > 0) {
154  if ($mode != 4) {
155  $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
156  }
157  if (!in_array($mode, array(1, 2, 3))) {
158  $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
159  }
160  }
161  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
162  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
163  }
164  $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
165  $sql .= " WHERE t.entity IN (".getEntity('societe').")";
166  $sql .= " AND t.fk_stcomm = st.id";
167 
168  if ($mode == 1) {
169  $sql .= " AND t.client IN (1, 3)";
170  } elseif ($mode == 2) {
171  $sql .= " AND t.client IN (2, 3)";
172  } elseif ($mode == 3) {
173  $sql .= " AND t.client IN (0)";
174  } elseif ($mode == 4) {
175  $sql .= " AND t.fournisseur IN (1)";
176  }
177 
178  // Select thirdparties of given category
179  if ($category > 0) {
180  if (!empty($mode) && $mode != 4) {
181  $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
182  } elseif (!empty($mode) && $mode == 4) {
183  $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
184  } else {
185  $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))";
186  }
187  }
188 
189  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
190  $sql .= " AND t.rowid = sc.fk_soc";
191  }
192  //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
193  if ($socids) {
194  $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
195  }
196  if ($search_sale > 0) {
197  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
198  }
199  // Insert sale filter
200  if ($search_sale > 0) {
201  $sql .= " AND sc.fk_user = ".((int) $search_sale);
202  }
203  // Add sql filters
204  if ($sqlfilters) {
205  $errormessage = '';
206  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
207  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
208  }
209  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
210  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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  $obj_ret[] = $this->_cleanObjectDatas($soc_static);
234  }
235  $i++;
236  }
237  } else {
238  throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
239  }
240  if (!count($obj_ret)) {
241  throw new RestException(404, 'Thirdparties not found');
242  }
243  return $obj_ret;
244  }
245 
252  public function post($request_data = null)
253  {
254  if (!DolibarrApiAccess::$user->rights->societe->creer) {
255  throw new RestException(401);
256  }
257  // Check mandatory fields
258  $result = $this->_validate($request_data);
259 
260  foreach ($request_data as $field => $value) {
261  $this->company->$field = $value;
262  }
263  if ($this->company->create(DolibarrApiAccess::$user) < 0) {
264  throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
265  }
266 
267  return $this->company->id;
268  }
269 
277  public function put($id, $request_data = null)
278  {
279  if (!DolibarrApiAccess::$user->rights->societe->creer) {
280  throw new RestException(401);
281  }
282 
283  $result = $this->company->fetch($id);
284  if (!$result) {
285  throw new RestException(404, 'Thirdparty not found');
286  }
287 
288  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
289  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
290  }
291 
292  foreach ($request_data as $field => $value) {
293  if ($field == 'id') {
294  continue;
295  }
296  $this->company->$field = $value;
297  }
298 
299  if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) {
300  return $this->get($id);
301  }
302 
303  return false;
304  }
305 
320  public function merge($id, $idtodelete)
321  {
322  global $hookmanager;
323 
324  $error = 0;
325 
326  if ($id == $idtodelete) {
327  throw new RestException(400, 'Try to merge a thirdparty into itself');
328  }
329 
330  if (!DolibarrApiAccess::$user->rights->societe->creer) {
331  throw new RestException(401);
332  }
333 
334  $result = $this->company->fetch($id); // include the fetch of extra fields
335  if (!$result) {
336  throw new RestException(404, 'Thirdparty not found');
337  }
338 
339  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
340  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
341  }
342 
343  $this->companytoremove = new Societe($this->db);
344 
345  $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
346  if (!$result) {
347  throw new RestException(404, 'Thirdparty not found');
348  }
349 
350  if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
351  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
352  }
353 
354  $soc_origin = $this->companytoremove;
355  $object = $this->company;
356  $user = DolibarrApiAccess::$user;
357 
358 
359  // Call same code than into action 'confirm_merge'
360 
361 
362  $this->db->begin();
363 
364  // Recopy some data
365  $object->client = $object->client | $soc_origin->client;
366  $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
367  $listofproperties = array(
368  'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
369  'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
370  'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
371  'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
372  'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
373  'model_pdf', 'fk_projet'
374  );
375  foreach ($listofproperties as $property) {
376  if (empty($object->$property)) {
377  $object->$property = $soc_origin->$property;
378  }
379  }
380 
381  // Concat some data
382  $listofproperties = array(
383  'note_public', 'note_private'
384  );
385  foreach ($listofproperties as $property) {
386  $object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
387  }
388 
389  // Merge extrafields
390  if (is_array($soc_origin->array_options)) {
391  foreach ($soc_origin->array_options as $key => $val) {
392  if (empty($object->array_options[$key])) {
393  $object->array_options[$key] = $val;
394  }
395  }
396  }
397 
398  // Merge categories
399  $static_cat = new Categorie($this->db);
400  $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
401  $object->setCategories($custcats, 'customer');
402  $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
403  $object->setCategories($suppcats, 'supplier');
404 
405  // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
406  if ($soc_origin->code_client == $object->code_client
407  || $soc_origin->code_fournisseur == $object->code_fournisseur
408  || $soc_origin->barcode == $object->barcode) {
409  dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
410  $soc_origin->code_client = '';
411  $soc_origin->code_fournisseur = '';
412  $soc_origin->barcode = '';
413  $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
414  }
415 
416  // Update
417  $result = $object->update($object->id, $user, 0, 1, 1, 'merge');
418  if ($result < 0) {
419  $error++;
420  }
421 
422  // Move links
423  if (!$error) {
424  // This list is also into the societe/card.php file
425  // TODO Mutualise the list into object societe.class.php
426  $objects = array(
427  'Adherent' => '/adherents/class/adherent.class.php',
428  'Don' => '/don/class/don.class.php',
429  'Societe' => '/societe/class/societe.class.php',
430  //'Categorie' => '/categories/class/categorie.class.php',
431  'ActionComm' => '/comm/action/class/actioncomm.class.php',
432  'Propal' => '/comm/propal/class/propal.class.php',
433  'Commande' => '/commande/class/commande.class.php',
434  'Facture' => '/compta/facture/class/facture.class.php',
435  'FactureRec' => '/compta/facture/class/facture-rec.class.php',
436  'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
437  'Mo' => '/mrp/class/mo.class.php',
438  'Contact' => '/contact/class/contact.class.php',
439  'Contrat' => '/contrat/class/contrat.class.php',
440  'Expedition' => '/expedition/class/expedition.class.php',
441  'Fichinter' => '/fichinter/class/fichinter.class.php',
442  'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
443  'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
444  'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
445  'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
446  'Delivery' => '/delivery/class/delivery.class.php',
447  'Product' => '/product/class/product.class.php',
448  'Project' => '/projet/class/project.class.php',
449  'Ticket' => '/ticket/class/ticket.class.php',
450  'User' => '/user/class/user.class.php',
451  'Account' => '/compta/bank/class/account.class.php',
452  'ConferenceOrBoothAttendee' => '/eventorganization/class/conferenceorboothattendee.class.php'
453  );
454 
455  //First, all core objects must update their tables
456  foreach ($objects as $object_name => $object_file) {
457  require_once DOL_DOCUMENT_ROOT.$object_file;
458 
459  if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
460  $error++;
461  //setEventMessages($this->db->lasterror(), null, 'errors');
462  }
463  }
464  }
465 
466  // External modules should update their ones too
467  if (!$error) {
468  $reshook = $hookmanager->executeHooks('replaceThirdparty', array(
469  'soc_origin' => $soc_origin->id,
470  'soc_dest' => $object->id
471  ), $soc_dest, $action);
472 
473  if ($reshook < 0) {
474  //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
475  $error++;
476  }
477  }
478 
479 
480  if (!$error) {
481  $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
482 
483  // Call trigger
484  $result = $object->call_trigger('COMPANY_MODIFY', $user);
485  if ($result < 0) {
486  //setEventMessages($object->error, $object->errors, 'errors');
487  $error++;
488  }
489  // End call triggers
490  }
491 
492  if (!$error) {
493  //We finally remove the old thirdparty
494  if ($soc_origin->delete($soc_origin->id, $user) < 1) {
495  $error++;
496  }
497  }
498 
499  // End of merge
500 
501  if ($error) {
502  $this->db->rollback();
503 
504  throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
505  } else {
506  $this->db->commit();
507  }
508 
509  return $this->get($id);
510  }
511 
518  public function delete($id)
519  {
520  if (!DolibarrApiAccess::$user->rights->societe->supprimer) {
521  throw new RestException(401);
522  }
523  $result = $this->company->fetch($id);
524  if (!$result) {
525  throw new RestException(404, 'Thirdparty not found');
526  }
527  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
528  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
529  }
530  $this->company->oldcopy = clone $this->company;
531 
532  $res = $this->company->delete($id);
533  if ($res < 0) {
534  throw new RestException(500, "Can't delete, error occurs");
535  } elseif ($res == 0) {
536  throw new RestException(409, "Can't delete, that product is probably used");
537  }
538 
539  return array(
540  'success' => array(
541  'code' => 200,
542  'message' => 'Object deleted'
543  )
544  );
545  }
546 
562  public function setThirdpartyPriceLevel($id, $priceLevel)
563  {
564  global $conf;
565 
566  if (empty($conf->societe->enabled)) {
567  throw new RestException(501, 'Module "Thirdparties" needed for this request');
568  }
569 
570  if (empty($conf->product->enabled)) {
571  throw new RestException(501, 'Module "Products" needed for this request');
572  }
573 
574  if (empty($conf->global->PRODUIT_MULTIPRICES)) {
575  throw new RestException(501, 'Multiprices features activation needed for this request');
576  }
577 
578  if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
579  throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
580  }
581 
582  if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
583  throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
584  }
585 
586  $result = $this->company->fetch($id);
587  if ($result < 0) {
588  throw new RestException(404, 'Thirdparty '.$id.' not found');
589  }
590 
591  if (empty($result)) {
592  throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
593  }
594 
595  if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
596  throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
597  }
598 
599  $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
600  if ($result <= 0) {
601  throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
602  }
603 
604  return $this->_cleanObjectDatas($this->company);
605  }
606 
620  public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
621  {
622  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
623  throw new RestException(401);
624  }
625 
626  $result = $this->company->fetch($id);
627  if (!$result) {
628  throw new RestException(404, 'Thirdparty not found');
629  }
630 
631  $categories = new Categorie($this->db);
632 
633  $result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
634 
635  if (is_numeric($result) && $result < 0) {
636  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
637  }
638 
639  if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
640  return array();
641  }
642 
643  return $result;
644  }
645 
656  public function addCategory($id, $category_id)
657  {
658  if (!DolibarrApiAccess::$user->rights->societe->creer) {
659  throw new RestException(401);
660  }
661 
662  $result = $this->company->fetch($id);
663  if (!$result) {
664  throw new RestException(404, 'Thirdparty not found');
665  }
666  $category = new Categorie($this->db);
667  $result = $category->fetch($category_id);
668  if (!$result) {
669  throw new RestException(404, 'category not found');
670  }
671 
672  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
673  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
674  }
675  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
676  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
677  }
678 
679  $category->add_type($this->company, 'customer');
680 
681  return $this->_cleanObjectDatas($this->company);
682  }
683 
694  public function deleteCategory($id, $category_id)
695  {
696  if (!DolibarrApiAccess::$user->rights->societe->creer) {
697  throw new RestException(401);
698  }
699 
700  $result = $this->company->fetch($id);
701  if (!$result) {
702  throw new RestException(404, 'Thirdparty not found');
703  }
704  $category = new Categorie($this->db);
705  $result = $category->fetch($category_id);
706  if (!$result) {
707  throw new RestException(404, 'category not found');
708  }
709 
710  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
711  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
712  }
713  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
714  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
715  }
716 
717  $category->del_type($this->company, 'customer');
718 
719  return $this->_cleanObjectDatas($this->company);
720  }
721 
735  public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
736  {
737  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
738  throw new RestException(401);
739  }
740 
741  $result = $this->company->fetch($id);
742  if (!$result) {
743  throw new RestException(404, 'Thirdparty not found');
744  }
745 
746  $categories = new Categorie($this->db);
747 
748  $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
749 
750  if (is_numeric($result) && $result < 0) {
751  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
752  }
753 
754  if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
755  return array();
756  }
757 
758  return $result;
759  }
760 
771  public function addSupplierCategory($id, $category_id)
772  {
773  if (!DolibarrApiAccess::$user->rights->societe->creer) {
774  throw new RestException(401);
775  }
776 
777  $result = $this->company->fetch($id);
778  if (!$result) {
779  throw new RestException(404, 'Thirdparty not found');
780  }
781  $category = new Categorie($this->db);
782  $result = $category->fetch($category_id);
783  if (!$result) {
784  throw new RestException(404, 'category not found');
785  }
786 
787  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
788  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
789  }
790  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
791  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
792  }
793 
794  $category->add_type($this->company, 'supplier');
795 
796  return $this->_cleanObjectDatas($this->company);
797  }
798 
809  public function deleteSupplierCategory($id, $category_id)
810  {
811  if (!DolibarrApiAccess::$user->rights->societe->creer) {
812  throw new RestException(401);
813  }
814 
815  $result = $this->company->fetch($id);
816  if (!$result) {
817  throw new RestException(404, 'Thirdparty not found');
818  }
819  $category = new Categorie($this->db);
820  $result = $category->fetch($category_id);
821  if (!$result) {
822  throw new RestException(404, 'category not found');
823  }
824 
825  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
826  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
827  }
828  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
829  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
830  }
831 
832  $category->del_type($this->company, 'supplier');
833 
834  return $this->_cleanObjectDatas($this->company);
835  }
836 
837 
852  public function getOutStandingProposals($id, $mode = 'customer')
853  {
854  if (!DolibarrApiAccess::$user->rights->societe->lire) {
855  throw new RestException(401);
856  }
857 
858  if (empty($id)) {
859  throw new RestException(400, 'Thirdparty ID is mandatory');
860  }
861 
862  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
863  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
864  }
865 
866  $result = $this->company->fetch($id);
867  if (!$result) {
868  throw new RestException(404, 'Thirdparty not found');
869  }
870 
871  $result = $this->company->getOutstandingProposals($mode);
872 
873  unset($result['total_ht']);
874  unset($result['total_ttc']);
875 
876  return $result;
877  }
878 
879 
894  public function getOutStandingOrder($id, $mode = 'customer')
895  {
896  if (!DolibarrApiAccess::$user->rights->societe->lire) {
897  throw new RestException(401);
898  }
899 
900  if (empty($id)) {
901  throw new RestException(400, 'Thirdparty ID is mandatory');
902  }
903 
904  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
905  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
906  }
907 
908  $result = $this->company->fetch($id);
909  if (!$result) {
910  throw new RestException(404, 'Thirdparty not found');
911  }
912 
913  $result = $this->company->getOutstandingOrders($mode);
914 
915  unset($result['total_ht']);
916  unset($result['total_ttc']);
917 
918  return $result;
919  }
920 
935  public function getOutStandingInvoices($id, $mode = 'customer')
936  {
937  if (!DolibarrApiAccess::$user->rights->societe->lire) {
938  throw new RestException(401);
939  }
940 
941  if (empty($id)) {
942  throw new RestException(400, 'Thirdparty ID is mandatory');
943  }
944 
945  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
946  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
947  }
948 
949  $result = $this->company->fetch($id);
950  if (!$result) {
951  throw new RestException(404, 'Thirdparty not found');
952  }
953 
954  $result = $this->company->getOutstandingBills($mode);
955 
956  unset($result['total_ht']);
957  unset($result['total_ttc']);
958 
959  return $result;
960  }
961 
976  public function getSalesRepresentatives($id, $mode = 0)
977  {
978  if (!DolibarrApiAccess::$user->rights->societe->lire) {
979  throw new RestException(401);
980  }
981 
982  if (empty($id)) {
983  throw new RestException(400, 'Thirdparty ID is mandatory');
984  }
985 
986  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
987  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
988  }
989 
990  $result = $this->company->fetch($id);
991  if (!$result) {
992  throw new RestException(404, 'Thirdparty not found');
993  }
994 
995  $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
996 
997  return $result;
998  }
999 
1017  public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1018  {
1019  $obj_ret = array();
1020 
1021  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1022  throw new RestException(401);
1023  }
1024 
1025  if (empty($id)) {
1026  throw new RestException(400, 'Thirdparty ID is mandatory');
1027  }
1028 
1029  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1030  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1031  }
1032 
1033  $result = $this->company->fetch($id);
1034  if (!$result) {
1035  throw new RestException(404, 'Thirdparty not found');
1036  }
1037 
1038 
1039  $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";
1040  $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1041  $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1042  if ($filter == "available") {
1043  $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1044  }
1045  if ($filter == "used") {
1046  $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1047  }
1048 
1049  $sql .= $this->db->order($sortfield, $sortorder);
1050 
1051  $result = $this->db->query($sql);
1052  if (!$result) {
1053  throw new RestException(503, $this->db->lasterror());
1054  } else {
1055  $num = $this->db->num_rows($result);
1056  while ($obj = $this->db->fetch_object($result)) {
1057  $obj_ret[] = $obj;
1058  }
1059  }
1060 
1061  return $obj_ret;
1062  }
1063 
1064 
1065 
1080  {
1081  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1082  throw new RestException(401);
1083  }
1084  if (empty($id)) {
1085  throw new RestException(400, 'Thirdparty ID is mandatory');
1086  }
1087 
1088  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1089  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1090  }
1091 
1092  /*$result = $this->thirdparty->fetch($id);
1093  if( ! $result ) {
1094  throw new RestException(404, 'Thirdparty not found');
1095  }*/
1096 
1097  $invoice = new Facture($this->db);
1098  $result = $invoice->list_replacable_invoices($id);
1099  if ($result < 0) {
1100  throw new RestException(405, $invoice->error);
1101  }
1102 
1103  return $result;
1104  }
1105 
1123  {
1124  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1125  throw new RestException(401);
1126  }
1127  if (empty($id)) {
1128  throw new RestException(400, 'Thirdparty ID is mandatory');
1129  }
1130 
1131  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1132  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1133  }
1134 
1135  /*$result = $this->thirdparty->fetch($id);
1136  if( ! $result ) {
1137  throw new RestException(404, 'Thirdparty not found');
1138  }*/
1139 
1140  $invoice = new Facture($this->db);
1141  $result = $invoice->list_qualified_avoir_invoices($id);
1142  if ($result < 0) {
1143  throw new RestException(405, $invoice->error);
1144  }
1145 
1146  return $result;
1147  }
1148 
1158  public function getCompanyBankAccount($id)
1159  {
1160  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1161  throw new RestException(401);
1162  }
1163  if (empty($id)) {
1164  throw new RestException(400, 'Thirdparty ID is mandatory');
1165  }
1166 
1167  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1168  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1169  }
1170 
1175  $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1176  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1177  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1178  if ($id) {
1179  $sql .= " WHERE fk_soc = ".((int) $id);
1180  }
1181 
1182  $result = $this->db->query($sql);
1183 
1184  if ($this->db->num_rows($result) == 0) {
1185  throw new RestException(404, 'Account not found');
1186  }
1187 
1188  $i = 0;
1189 
1190  $accounts = array();
1191 
1192  if ($result) {
1193  $num = $this->db->num_rows($result);
1194  while ($i < $num) {
1195  $obj = $this->db->fetch_object($result);
1196  $account = new CompanyBankAccount($this->db);
1197  if ($account->fetch($obj->rowid)) {
1198  $accounts[] = $account;
1199  }
1200  $i++;
1201  }
1202  } else {
1203  throw new RestException(404, 'Account not found');
1204  }
1205 
1206 
1207  $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1208 
1209  $returnAccounts = array();
1210 
1211  foreach ($accounts as $account) {
1212  $object = array();
1213  foreach ($account as $key => $value) {
1214  if (in_array($key, $fields)) {
1215  $object[$key] = $value;
1216  }
1217  }
1218  $returnAccounts[] = $object;
1219  }
1220 
1221  return $returnAccounts;
1222  }
1223 
1233  public function createCompanyBankAccount($id, $request_data = null)
1234  {
1235  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1236  throw new RestException(401);
1237  }
1238  if ($this->company->fetch($id) <= 0) {
1239  throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1240  }
1241  $account = new CompanyBankAccount($this->db);
1242 
1243  $account->socid = $id;
1244 
1245  foreach ($request_data as $field => $value) {
1246  $account->$field = $value;
1247  }
1248 
1249  if ($account->create(DolibarrApiAccess::$user) < 0) {
1250  throw new RestException(500, 'Error creating Company Bank account');
1251  }
1252 
1253  if (empty($account->rum)) {
1254  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1255  $prelevement = new BonPrelevement($this->db);
1256  $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1257  $account->date_rum = dol_now();
1258  }
1259 
1260  if ($account->update(DolibarrApiAccess::$user) < 0) {
1261  throw new RestException(500, 'Error updating values');
1262  }
1263 
1264  return $this->_cleanObjectDatas($account);
1265  }
1266 
1278  public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1279  {
1280  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1281  throw new RestException(401);
1282  }
1283  if ($this->company->fetch($id) <= 0) {
1284  throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1285  }
1286  $account = new CompanyBankAccount($this->db);
1287 
1288  $account->fetch($bankaccount_id, $id, -1, '');
1289 
1290  if ($account->socid != $id) {
1291  throw new RestException(401);
1292  }
1293 
1294 
1295  foreach ($request_data as $field => $value) {
1296  $account->$field = $value;
1297  }
1298 
1299  if (empty($account->rum)) {
1300  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1301  $prelevement = new BonPrelevement($this->db);
1302  $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1303  $account->date_rum = dol_now();
1304  }
1305 
1306  if ($account->update(DolibarrApiAccess::$user) < 0) {
1307  throw new RestException(500, 'Error updating values');
1308  }
1309 
1310  return $this->_cleanObjectDatas($account);
1311  }
1312 
1323  public function deleteCompanyBankAccount($id, $bankaccount_id)
1324  {
1325  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1326  throw new RestException(401);
1327  }
1328 
1329  $account = new CompanyBankAccount($this->db);
1330 
1331  $account->fetch($bankaccount_id);
1332 
1333  if (!$account->socid == $id) {
1334  throw new RestException(401);
1335  }
1336 
1337  return $account->delete(DolibarrApiAccess::$user);
1338  }
1339 
1350  public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1351  {
1352  global $conf, $langs;
1353 
1354  $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1355 
1356  if ($this->company->fetch($id) <= 0) {
1357  throw new RestException(404, 'Thirdparty not found');
1358  }
1359 
1360  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1361  throw new RestException(401);
1362  }
1363 
1364  $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1365 
1366  $this->company->fk_bank = $this->company->fk_account;
1367 
1368  $outputlangs = $langs;
1369  $newlang = '';
1370 
1371  //if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1372  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1373  if (isset($this->company->thirdparty->default_lang)) {
1374  $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1375  } elseif (isset($this->company->default_lang)) {
1376  $newlang = $this->company->default_lang; // for thirdparty
1377  }
1378  }
1379  if (!empty($newlang)) {
1380  $outputlangs = new Translate("", $conf);
1381  $outputlangs->setDefaultLang($newlang);
1382  }
1383 
1384  $sql = "SELECT rowid";
1385  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1386  if ($id) {
1387  $sql .= " WHERE fk_soc = ".((int) $id);
1388  }
1389  if ($companybankid) {
1390  $sql .= " AND rowid = ".((int) $companybankid);
1391  }
1392 
1393  $i = 0;
1394  $accounts = array();
1395 
1396  $result = $this->db->query($sql);
1397  if ($result) {
1398  if ($this->db->num_rows($result) == 0) {
1399  throw new RestException(404, 'Bank account not found');
1400  }
1401 
1402  $num = $this->db->num_rows($result);
1403  while ($i < $num) {
1404  $obj = $this->db->fetch_object($result);
1405 
1406  $account = new CompanyBankAccount($this->db);
1407  if ($account->fetch($obj->rowid)) {
1408  $accounts[] = $account;
1409  }
1410  $i++;
1411  }
1412  } else {
1413  throw new RestException(500, 'Sql error '.$this->db->lasterror());
1414  }
1415 
1416  $moreparams = array(
1417  'use_companybankid' => $accounts[0]->id,
1418  'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1419  );
1420 
1421  $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1422 
1423  if ($result > 0) {
1424  return array("success" => $result);
1425  } else {
1426  throw new RestException(500, 'Error generating the document '.$this->company->error);
1427  }
1428  }
1429 
1442  public function getSocieteAccounts($id, $site = null)
1443  {
1444  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1445  throw new RestException(401);
1446  }
1447 
1448  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1449  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1450  }
1451 
1455  $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1456  $sql .= " WHERE fk_soc = ".((int) $id);
1457  if ($site) {
1458  $sql .= " AND site ='".$this->db->escape($site)."'";
1459  }
1460 
1461  $result = $this->db->query($sql);
1462 
1463  if ($result && $this->db->num_rows($result) == 0) {
1464  throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1465  }
1466 
1467  $i = 0;
1468 
1469  $accounts = array();
1470 
1471  $num = $this->db->num_rows($result);
1472  while ($i < $num) {
1473  $obj = $this->db->fetch_object($result);
1474  $account = new SocieteAccount($this->db);
1475 
1476  if ($account->fetch($obj->rowid)) {
1477  $accounts[] = $account;
1478  }
1479  $i++;
1480  }
1481 
1482  $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1483 
1484  $returnAccounts = array();
1485 
1486  foreach ($accounts as $account) {
1487  $object = array();
1488  foreach ($account as $key => $value) {
1489  if (in_array($key, $fields)) {
1490  $object[$key] = $value;
1491  }
1492  }
1493  $returnAccounts[] = $object;
1494  }
1495 
1496  return $returnAccounts;
1497  }
1498 
1518  public function createSocieteAccount($id, $request_data = null)
1519  {
1520  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1521  throw new RestException(401);
1522  }
1523 
1524  if (!isset($request_data['site'])) {
1525  throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1526  }
1527 
1528  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1529  $result = $this->db->query($sql);
1530 
1531  if ($result && $this->db->num_rows($result) == 0) {
1532  $account = new SocieteAccount($this->db);
1533  if (!isset($request_data['login'])) {
1534  $account->login = "";
1535  }
1536  $account->fk_soc = $id;
1537 
1538  foreach ($request_data as $field => $value) {
1539  $account->$field = $value;
1540  }
1541 
1542  if ($account->create(DolibarrApiAccess::$user) < 0) {
1543  throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1544  }
1545 
1546  $this->_cleanObjectDatas($account);
1547 
1548  return $account;
1549  } else {
1550  throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1551  }
1552  }
1553 
1576  public function putSocieteAccount($id, $site, $request_data = null)
1577  {
1578  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1579  throw new RestException(401);
1580  }
1581 
1582  $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1583  $result = $this->db->query($sql);
1584 
1585  // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1586  if ($result && $this->db->num_rows == 0) {
1587  if (!isset($request_data['key_account'])) {
1588  throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1589  }
1590  $account = new SocieteAccount($this->db);
1591  if (!isset($request_data['login'])) {
1592  $account->login = "";
1593  }
1594 
1595  foreach ($request_data as $field => $value) {
1596  $account->$field = $value;
1597  }
1598 
1599  $account->fk_soc = $id;
1600  $account->site = $site;
1601 
1602  if ($account->create(DolibarrApiAccess::$user) < 0) {
1603  throw new RestException(500, 'Error creating SocieteAccount entity.');
1604  }
1605  // We found an existing SocieteAccount entity, we are replacing it
1606  } else {
1607  if (isset($request_data['site']) && $request_data['site'] !== $site) {
1608  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1609  $result = $this->db->query($sql);
1610 
1611  if ($result && $this->db->num_rows($result) !== 0) {
1612  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.");
1613  }
1614  }
1615 
1616  $obj = $this->db->fetch_object($result);
1617 
1618  $account = new SocieteAccount($this->db);
1619  $account->id = $obj->rowid;
1620  $account->fk_soc = $id;
1621  $account->site = $site;
1622  if (!isset($request_data['login'])) {
1623  $account->login = "";
1624  }
1625  $account->fk_user_creat = $obj->fk_user_creat;
1626  $account->date_creation = $obj->date_creation;
1627 
1628  foreach ($request_data as $field => $value) {
1629  $account->$field = $value;
1630  }
1631 
1632  if ($account->update(DolibarrApiAccess::$user) < 0) {
1633  throw new RestException(500, 'Error updating SocieteAccount entity.');
1634  }
1635  }
1636 
1637  $this->_cleanObjectDatas($account);
1638 
1639  return $account;
1640  }
1641 
1658  public function patchSocieteAccount($id, $site, $request_data = null)
1659  {
1660  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1661  throw new RestException(401);
1662  }
1663 
1664  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1665  $result = $this->db->query($sql);
1666 
1667  if ($result && $this->db->num_rows($result) == 0) {
1668  throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1669  } else {
1670  // If the user tries to edit the site member, we check first if
1671  if (isset($request_data['site']) && $request_data['site'] !== $site) {
1672  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1673  $result = $this->db->query($sql);
1674 
1675  if ($result && $this->db->num_rows($result) !== 0) {
1676  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.");
1677  }
1678  }
1679 
1680  $obj = $this->db->fetch_object($result);
1681  $account = new SocieteAccount($this->db);
1682  $account->fetch($obj->rowid);
1683 
1684  foreach ($request_data as $field => $value) {
1685  $account->$field = $value;
1686  }
1687 
1688  if ($account->update(DolibarrApiAccess::$user) < 0) {
1689  throw new RestException(500, 'Error updating SocieteAccount account');
1690  }
1691 
1692  $this->_cleanObjectDatas($account);
1693 
1694  return $account;
1695  }
1696  }
1697 
1711  public function deleteSocieteAccount($id, $site)
1712  {
1713  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1714  throw new RestException(401);
1715  }
1716 
1717  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1718  $result = $this->db->query($sql);
1719 
1720  if ($result && $this->db->num_rows($result) == 0) {
1721  throw new RestException(404);
1722  } else {
1723  $obj = $this->db->fetch_object($result);
1724  $account = new SocieteAccount($this->db);
1725  $account->fetch($obj->rowid);
1726 
1727  if ($account->delete(DolibarrApiAccess::$user) < 0) {
1728  throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1729  }
1730  }
1731  }
1732 
1745  public function deleteSocieteAccounts($id)
1746  {
1747  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1748  throw new RestException(401);
1749  }
1750 
1755  $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1756  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1757 
1758  $result = $this->db->query($sql);
1759 
1760  if ($result && $this->db->num_rows($result) == 0) {
1761  throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1762  } else {
1763  $i = 0;
1764 
1765  $num = $this->db->num_rows($result);
1766  while ($i < $num) {
1767  $obj = $this->db->fetch_object($result);
1768  $account = new SocieteAccount($this->db);
1769  $account->fetch($obj->rowid);
1770 
1771  if ($account->delete(DolibarrApiAccess::$user) < 0) {
1772  throw new RestException(500, 'Error while deleting gateways attached to this third party');
1773  }
1774  $i++;
1775  }
1776  }
1777  }
1778 
1779  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1786  protected function _cleanObjectDatas($object)
1787  {
1788  // phpcs:enable
1789  $object = parent::_cleanObjectDatas($object);
1790 
1791  unset($object->nom); // ->name already defined and nom deprecated
1792  unset($object->name_bis); // ->name_alias already defined
1793  unset($object->note); // ->note_private and note_public already defined
1794  unset($object->departement);
1795  unset($object->departement_code);
1796  unset($object->pays);
1797  unset($object->particulier);
1798  unset($object->prefix_comm);
1799 
1800  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.
1801 
1802  unset($object->total_ht);
1803  unset($object->total_tva);
1804  unset($object->total_localtax1);
1805  unset($object->total_localtax2);
1806  unset($object->total_ttc);
1807 
1808  unset($object->lines);
1809  unset($object->thirdparty);
1810 
1811  unset($object->fk_delivery_address); // deprecated feature
1812 
1813  unset($object->skype);
1814  unset($object->twitter);
1815  unset($object->facebook);
1816  unset($object->linkedin);
1817  unset($object->instagram);
1818  unset($object->snapchat);
1819  unset($object->googleplus);
1820  unset($object->youtube);
1821  unset($object->whatsapp);
1822 
1823  return $object;
1824  }
1825 
1834  private function _validate($data)
1835  {
1836  $thirdparty = array();
1837  foreach (Thirdparties::$FIELDS as $field) {
1838  if (!isset($data[$field])) {
1839  throw new RestException(400, "$field field missing");
1840  }
1841  $thirdparty[$field] = $data[$field];
1842  }
1843  return $thirdparty;
1844  }
1845 
1867  private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1868  {
1869  global $conf;
1870 
1871  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1872  throw new RestException(401);
1873  }
1874 
1875  if ($rowid === 0) {
1876  $result = $this->company->initAsSpecimen();
1877  } else {
1878  $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1879  }
1880  if (!$result) {
1881  throw new RestException(404, 'Thirdparty not found');
1882  }
1883 
1884  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1885  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1886  }
1887 
1888  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1889  $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1890  $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1891  } else {
1892  $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1893  $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1894  }
1895 
1896  $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1897  $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1898  $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1899  $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1900 
1901  return $this->_cleanObjectDatas($this->company);
1902  }
1903 }
Thirdparties\getCompanyBankAccount
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
Definition: api_thirdparties.class.php:1158
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
db
$conf db
API class for accounts.
Definition: inc.php:41
Thirdparties\deleteSocieteAccounts
deleteSocieteAccounts($id)
Delete all gateways attached to a thirdparty.
Definition: api_thirdparties.class.php:1745
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
SocieteAccount
Class for SocieteAccount.
Definition: societeaccount.class.php:35
Thirdparties\createSocieteAccount
createSocieteAccount($id, $request_data=null)
Create and attach a new gateway to an existing thirdparty.
Definition: api_thirdparties.class.php:1518
Translate
Class to manage translations.
Definition: translate.class.php:30
Thirdparties\addSupplierCategory
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
Definition: api_thirdparties.class.php:771
Thirdparties\put
put($id, $request_data=null)
Update thirdparty.
Definition: api_thirdparties.class.php:277
Thirdparties\deleteCategory
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
Definition: api_thirdparties.class.php:694
Thirdparties\_fetch
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
Definition: api_thirdparties.class.php:1867
Thirdparties\getByEmail
getByEmail($email)
Get properties of a thirdparty object by email.
Definition: api_thirdparties.class.php:92
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Categorie
Class to manage categories.
Definition: categorie.class.php:47
Facture
Class to manage invoices.
Definition: facture.class.php:60
Thirdparties\getOutStandingProposals
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals of thirdparty.
Definition: api_thirdparties.class.php:852
CompanyBankAccount
Class to manage bank accounts description of third parties.
Definition: companybankaccount.class.php:34
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
dol_concatdesc
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...
Definition: functions.lib.php:7248
Thirdparties\__construct
__construct()
Constructor.
Definition: api_thirdparties.class.php:48
Thirdparties\getCategories
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
Definition: api_thirdparties.class.php:620
BonPrelevement
Class to manage withdrawal receipts.
Definition: bonprelevement.class.php:43
Thirdparties\getFixedAmountDiscounts
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers....
Definition: api_thirdparties.class.php:1017
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Thirdparties\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='')
List thirdparties.
Definition: api_thirdparties.class.php:131
Thirdparties\getOutStandingInvoices
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices of thirdparty.
Definition: api_thirdparties.class.php:935
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Thirdparties\deleteSocieteAccount
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway id)
Definition: api_thirdparties.class.php:1711
Thirdparties\setThirdpartyPriceLevel
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
Definition: api_thirdparties.class.php:562
Thirdparties\getSupplierCategories
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
Definition: api_thirdparties.class.php:735
Thirdparties\getOutStandingOrder
getOutStandingOrder($id, $mode='customer')
Get outstanding orders of thirdparty.
Definition: api_thirdparties.class.php:894
Thirdparties\getByBarcode
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
Definition: api_thirdparties.class.php:109
Thirdparties\getInvoicesQualifiedForCreditNote
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
Definition: api_thirdparties.class.php:1122
Thirdparties\merge
merge($id, $idtodelete)
Merge a thirdparty into another one.
Definition: api_thirdparties.class.php:320
Thirdparties\generateBankAccountDocument
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
Definition: api_thirdparties.class.php:1350
Thirdparties\updateCompanyBankAccount
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
Definition: api_thirdparties.class.php:1278
Thirdparties\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_thirdparties.class.php:1786
Thirdparties\getInvoicesQualifiedForReplacement
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
Definition: api_thirdparties.class.php:1079
Thirdparties\deleteSupplierCategory
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
Definition: api_thirdparties.class.php:809
Thirdparties\putSocieteAccount
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
Definition: api_thirdparties.class.php:1576
Thirdparties
Definition: api_thirdparties.class.php:30
Thirdparties\getSalesRepresentatives
getSalesRepresentatives($id, $mode=0)
Get representatives of thirdparty.
Definition: api_thirdparties.class.php:976
Thirdparties\addCategory
addCategory($id, $category_id)
Add a customer category to a thirdparty.
Definition: api_thirdparties.class.php:656
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
Thirdparties\post
post($request_data=null)
Create thirdparty object.
Definition: api_thirdparties.class.php:252
Thirdparties\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_thirdparties.class.php:1834
Thirdparties\getSocieteAccounts
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
Definition: api_thirdparties.class.php:1442
Thirdparties\patchSocieteAccount
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific gateway attached to a thirdparty.
Definition: api_thirdparties.class.php:1658
Thirdparties\deleteCompanyBankAccount
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
Definition: api_thirdparties.class.php:1323
Thirdparties\createCompanyBankAccount
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
Definition: api_thirdparties.class.php:1233