dolibarr  13.0.2
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 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  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  // case of external user, we force socids
136  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
137 
138  // If the internal user must only see his customers, force searching by him
139  $search_sale = 0;
140  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
141 
142  $sql = "SELECT t.rowid";
143  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $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)
144  $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
145  if ($category > 0) {
146  if ($mode != 4) $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
147  if (!in_array($mode, array(1, 2, 3))) $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
148  }
149  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
150  $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
151  $sql .= " WHERE t.entity IN (".getEntity('societe').")";
152  $sql .= " AND t.fk_stcomm = st.id";
153 
154  if ($mode == 1) $sql .= " AND t.client IN (1, 3)";
155  elseif ($mode == 2) $sql .= " AND t.client IN (2, 3)";
156  elseif ($mode == 3) $sql .= " AND t.client IN (0)";
157  elseif ($mode == 4) $sql .= " AND t.fournisseur IN (1)";
158 
159  // Select thirdparties of given category
160  if ($category > 0) {
161  if (!empty($mode) && $mode != 4) { $sql .= " AND c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid"; }
162  elseif (!empty($mode) && $mode == 4) { $sql .= " AND cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid"; }
163  else { $sql .= " AND ((c.fk_categorie = ".$this->db->escape($category)." AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".$this->db->escape($category)." AND cc.fk_soc = t.rowid))"; }
164  }
165 
166  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc";
167  //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
168  if ($socids) $sql .= " AND t.rowid IN (".$socids.")";
169  if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
170  // Insert sale filter
171  if ($search_sale > 0)
172  {
173  $sql .= " AND sc.fk_user = ".$search_sale;
174  }
175  // Add sql filters
176  if ($sqlfilters)
177  {
178  if (!DolibarrApi::_checkFilters($sqlfilters))
179  {
180  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
181  }
182  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
183  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
184  }
185 
186  $sql .= $this->db->order($sortfield, $sortorder);
187 
188  if ($limit) {
189  if ($page < 0) {
190  $page = 0;
191  }
192  $offset = $limit * $page;
193 
194  $sql .= $this->db->plimit($limit + 1, $offset);
195  }
196 
197  $result = $this->db->query($sql);
198  if ($result)
199  {
200  $num = $this->db->num_rows($result);
201  $min = min($num, ($limit <= 0 ? $num : $limit));
202  $i = 0;
203  while ($i < $min)
204  {
205  $obj = $this->db->fetch_object($result);
206  $soc_static = new Societe($this->db);
207  if ($soc_static->fetch($obj->rowid)) {
208  $obj_ret[] = $this->_cleanObjectDatas($soc_static);
209  }
210  $i++;
211  }
212  } else {
213  throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
214  }
215  if (!count($obj_ret)) {
216  throw new RestException(404, 'Thirdparties not found');
217  }
218  return $obj_ret;
219  }
220 
227  public function post($request_data = null)
228  {
229  if (!DolibarrApiAccess::$user->rights->societe->creer) {
230  throw new RestException(401);
231  }
232  // Check mandatory fields
233  $result = $this->_validate($request_data);
234 
235  foreach ($request_data as $field => $value) {
236  $this->company->$field = $value;
237  }
238  if ($this->company->create(DolibarrApiAccess::$user) < 0)
239  throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
240 
241  return $this->company->id;
242  }
243 
251  public function put($id, $request_data = null)
252  {
253  if (!DolibarrApiAccess::$user->rights->societe->creer) {
254  throw new RestException(401);
255  }
256 
257  $result = $this->company->fetch($id);
258  if (!$result) {
259  throw new RestException(404, 'Thirdparty not found');
260  }
261 
262  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
263  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
264  }
265 
266  foreach ($request_data as $field => $value) {
267  if ($field == 'id') continue;
268  $this->company->$field = $value;
269  }
270 
271  if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) {
272  return $this->get($id);
273  }
274 
275  return false;
276  }
277 
292  public function merge($id, $idtodelete)
293  {
294  global $hookmanager;
295 
296  $error = 0;
297 
298  if ($id == $idtodelete)
299  {
300  throw new RestException(400, 'Try to merge a thirdparty into itself');
301  }
302 
303  if (!DolibarrApiAccess::$user->rights->societe->creer) {
304  throw new RestException(401);
305  }
306 
307  $result = $this->company->fetch($id); // include the fetch of extra fields
308  if (!$result) {
309  throw new RestException(404, 'Thirdparty not found');
310  }
311 
312  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
313  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
314  }
315 
316  $this->companytoremove = new Societe($this->db);
317 
318  $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
319  if (!$result) {
320  throw new RestException(404, 'Thirdparty not found');
321  }
322 
323  if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
324  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
325  }
326 
327  $soc_origin = $this->companytoremove;
328  $object = $this->company;
329  $user = DolibarrApiAccess::$user;
330 
331 
332  // Call same code than into action 'confirm_merge'
333 
334 
335  $this->db->begin();
336 
337  // Recopy some data
338  $object->client = $object->client | $soc_origin->client;
339  $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
340  $listofproperties = array(
341  'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
342  'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
343  'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
344  'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
345  'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
346  'model_pdf', 'fk_projet'
347  );
348  foreach ($listofproperties as $property)
349  {
350  if (empty($object->$property)) $object->$property = $soc_origin->$property;
351  }
352 
353  // Concat some data
354  $listofproperties = array(
355  'note_public', 'note_private'
356  );
357  foreach ($listofproperties as $property)
358  {
359  $object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
360  }
361 
362  // Merge extrafields
363  if (is_array($soc_origin->array_options))
364  {
365  foreach ($soc_origin->array_options as $key => $val)
366  {
367  if (empty($object->array_options[$key])) $object->array_options[$key] = $val;
368  }
369  }
370 
371  // Merge categories
372  $static_cat = new Categorie($this->db);
373  $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
374  $object->setCategories($custcats, 'customer');
375  $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
376  $object->setCategories($suppcats, 'supplier');
377 
378  // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
379  if ($soc_origin->code_client == $object->code_client
380  || $soc_origin->code_fournisseur == $object->code_fournisseur
381  || $soc_origin->barcode == $object->barcode)
382  {
383  dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
384  $soc_origin->code_client = '';
385  $soc_origin->code_fournisseur = '';
386  $soc_origin->barcode = '';
387  $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
388  }
389 
390  // Update
391  $result = $object->update($object->id, $user, 0, 1, 1, 'merge');
392  if ($result < 0)
393  {
394  $error++;
395  }
396 
397  // Move links
398  if (!$error) {
399  // This list is also into the societe/card.php file
400  // TODO Mutualise the list into object societe.class.php
401  $objects = array(
402  'Adherent' => '/adherents/class/adherent.class.php',
403  'Societe' => '/societe/class/societe.class.php',
404  'Categorie' => '/categories/class/categorie.class.php',
405  'ActionComm' => '/comm/action/class/actioncomm.class.php',
406  'Propal' => '/comm/propal/class/propal.class.php',
407  'Commande' => '/commande/class/commande.class.php',
408  'Facture' => '/compta/facture/class/facture.class.php',
409  'FactureRec' => '/compta/facture/class/facture-rec.class.php',
410  'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
411  'Mo' => '/mrp/class/mo.class.php',
412  'Contact' => '/contact/class/contact.class.php',
413  'Contrat' => '/contrat/class/contrat.class.php',
414  'Expedition' => '/expedition/class/expedition.class.php',
415  'Fichinter' => '/fichinter/class/fichinter.class.php',
416  'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
417  'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
418  'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
419  'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
420  'Livraison' => '/delivery/class/delivery.class.php',
421  'Product' => '/product/class/product.class.php',
422  'Project' => '/projet/class/project.class.php',
423  'Ticket' => '/ticket/class/ticket.class.php',
424  'User' => '/user/class/user.class.php'
425  );
426 
427  //First, all core objects must update their tables
428  foreach ($objects as $object_name => $object_file)
429  {
430  require_once DOL_DOCUMENT_ROOT.$object_file;
431 
432  if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
433  $error++;
434  //setEventMessages($this->db->lasterror(), null, 'errors');
435  }
436  }
437  }
438 
439  // External modules should update their ones too
440  if (!$error) {
441  $reshook = $hookmanager->executeHooks('replaceThirdparty', array(
442  'soc_origin' => $soc_origin->id,
443  'soc_dest' => $object->id
444  ), $soc_dest, $action);
445 
446  if ($reshook < 0) {
447  //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
448  $error++;
449  }
450  }
451 
452 
453  if (!$error) {
454  $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
455 
456  // Call trigger
457  $result = $object->call_trigger('COMPANY_MODIFY', $user);
458  if ($result < 0) {
459  //setEventMessages($object->error, $object->errors, 'errors');
460  $error++;
461  }
462  // End call triggers
463  }
464 
465  if (!$error) {
466  //We finally remove the old thirdparty
467  if ($soc_origin->delete($soc_origin->id, $user) < 1) {
468  $error++;
469  }
470  }
471 
472  // End of merge
473 
474  if ($error) {
475  $this->db->rollback();
476 
477  throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
478  } else {
479  $this->db->commit();
480  }
481 
482  return $this->get($id);
483  }
484 
491  public function delete($id)
492  {
493  if (!DolibarrApiAccess::$user->rights->societe->supprimer) {
494  throw new RestException(401);
495  }
496  $result = $this->company->fetch($id);
497  if (!$result) {
498  throw new RestException(404, 'Thirdparty not found');
499  }
500  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
501  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
502  }
503  $this->company->oldcopy = clone $this->company;
504  return $this->company->delete($id);
505  }
506 
522  public function setThirdpartyPriceLevel($id, $priceLevel)
523  {
524  global $conf;
525 
526  if (empty($conf->societe->enabled)) {
527  throw new RestException(501, 'Module "Thirdparties" needed for this request');
528  }
529 
530  if (empty($conf->product->enabled)) {
531  throw new RestException(501, 'Module "Products" needed for this request');
532  }
533 
534  if (empty($conf->global->PRODUIT_MULTIPRICES)) {
535  throw new RestException(501, 'Multiprices features activation needed for this request');
536  }
537 
538  if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
539  throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
540  }
541 
542  if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
543  throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
544  }
545 
546  $result = $this->company->fetch($id);
547  if ($result < 0) {
548  throw new RestException(404, 'Thirdparty '.$id.' not found');
549  }
550 
551  if (empty($result)) {
552  throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
553  }
554 
555  if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
556  throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
557  }
558 
559  $result = $this->company->set_price_level($priceLevel, DolibarrApiAccess::$user);
560  if ($result <= 0) {
561  throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
562  }
563 
564  return $this->_cleanObjectDatas($this->company);
565  }
566 
580  public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
581  {
582  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
583  throw new RestException(401);
584  }
585 
586  $result = $this->company->fetch($id);
587  if (!$result)
588  {
589  throw new RestException(404, 'Thirdparty not found');
590  }
591 
592  $categories = new Categorie($this->db);
593 
594  $result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
595 
596  if (is_numeric($result) && $result < 0)
597  {
598  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
599  }
600 
601  if (is_numeric($result) && $result == 0) // To fix a return of 0 instead of empty array of method getListForItem
602  {
603  return array();
604  }
605 
606  return $result;
607  }
608 
619  public function addCategory($id, $category_id)
620  {
621  if (!DolibarrApiAccess::$user->rights->societe->creer) {
622  throw new RestException(401);
623  }
624 
625  $result = $this->company->fetch($id);
626  if (!$result) {
627  throw new RestException(404, 'Thirdparty not found');
628  }
629  $category = new Categorie($this->db);
630  $result = $category->fetch($category_id);
631  if (!$result) {
632  throw new RestException(404, 'category not found');
633  }
634 
635  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
636  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
637  }
638  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
639  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
640  }
641 
642  $category->add_type($this->company, 'customer');
643 
644  return $this->_cleanObjectDatas($this->company);
645  }
646 
657  public function deleteCategory($id, $category_id)
658  {
659  if (!DolibarrApiAccess::$user->rights->societe->creer) {
660  throw new RestException(401);
661  }
662 
663  $result = $this->company->fetch($id);
664  if (!$result) {
665  throw new RestException(404, 'Thirdparty not found');
666  }
667  $category = new Categorie($this->db);
668  $result = $category->fetch($category_id);
669  if (!$result) {
670  throw new RestException(404, 'category not found');
671  }
672 
673  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
674  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
675  }
676  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
677  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
678  }
679 
680  $category->del_type($this->company, 'customer');
681 
682  return $this->_cleanObjectDatas($this->company);
683  }
684 
698  public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
699  {
700  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
701  throw new RestException(401);
702  }
703 
704  $result = $this->company->fetch($id);
705  if (!$result)
706  {
707  throw new RestException(404, 'Thirdparty not found');
708  }
709 
710  $categories = new Categorie($this->db);
711 
712  $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
713 
714  if (is_numeric($result) && $result < 0)
715  {
716  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
717  }
718 
719  if (is_numeric($result) && $result == 0) // To fix a return of 0 instead of empty array of method getListForItem
720  {
721  return array();
722  }
723 
724  return $result;
725  }
726 
737  public function addSupplierCategory($id, $category_id)
738  {
739  if (!DolibarrApiAccess::$user->rights->societe->creer) {
740  throw new RestException(401);
741  }
742 
743  $result = $this->company->fetch($id);
744  if (!$result) {
745  throw new RestException(404, 'Thirdparty not found');
746  }
747  $category = new Categorie($this->db);
748  $result = $category->fetch($category_id);
749  if (!$result) {
750  throw new RestException(404, 'category not found');
751  }
752 
753  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
754  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
755  }
756  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
757  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
758  }
759 
760  $category->add_type($this->company, 'supplier');
761 
762  return $this->_cleanObjectDatas($this->company);
763  }
764 
775  public function deleteSupplierCategory($id, $category_id)
776  {
777  if (!DolibarrApiAccess::$user->rights->societe->creer) {
778  throw new RestException(401);
779  }
780 
781  $result = $this->company->fetch($id);
782  if (!$result) {
783  throw new RestException(404, 'Thirdparty not found');
784  }
785  $category = new Categorie($this->db);
786  $result = $category->fetch($category_id);
787  if (!$result) {
788  throw new RestException(404, 'category not found');
789  }
790 
791  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
792  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
793  }
794  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
795  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
796  }
797 
798  $category->del_type($this->company, 'supplier');
799 
800  return $this->_cleanObjectDatas($this->company);
801  }
802 
803 
818  public function getOutStandingProposals($id, $mode = 'customer')
819  {
820  if (!DolibarrApiAccess::$user->rights->societe->lire) {
821  throw new RestException(401);
822  }
823 
824  if (empty($id)) {
825  throw new RestException(400, 'Thirdparty ID is mandatory');
826  }
827 
828  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
829  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
830  }
831 
832  $result = $this->company->fetch($id);
833  if (!$result) {
834  throw new RestException(404, 'Thirdparty not found');
835  }
836 
837  $result = $this->company->getOutstandingProposals($mode);
838 
839  unset($result['total_ht']);
840  unset($result['total_ttc']);
841 
842  return $result;
843  }
844 
845 
860  public function getOutStandingOrder($id, $mode = 'customer')
861  {
862  if (!DolibarrApiAccess::$user->rights->societe->lire) {
863  throw new RestException(401);
864  }
865 
866  if (empty($id)) {
867  throw new RestException(400, 'Thirdparty ID is mandatory');
868  }
869 
870  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
871  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
872  }
873 
874  $result = $this->company->fetch($id);
875  if (!$result) {
876  throw new RestException(404, 'Thirdparty not found');
877  }
878 
879  $result = $this->company->getOutstandingOrders($mode);
880 
881  unset($result['total_ht']);
882  unset($result['total_ttc']);
883 
884  return $result;
885  }
886 
901  public function getOutStandingInvoices($id, $mode = 'customer')
902  {
903  if (!DolibarrApiAccess::$user->rights->societe->lire) {
904  throw new RestException(401);
905  }
906 
907  if (empty($id)) {
908  throw new RestException(400, 'Thirdparty ID is mandatory');
909  }
910 
911  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
912  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
913  }
914 
915  $result = $this->company->fetch($id);
916  if (!$result) {
917  throw new RestException(404, 'Thirdparty not found');
918  }
919 
920  $result = $this->company->getOutstandingBills($mode);
921 
922  unset($result['total_ht']);
923  unset($result['total_ttc']);
924 
925  return $result;
926  }
927 
942  public function getSalesRepresentatives($id, $mode = 0)
943  {
944  if (!DolibarrApiAccess::$user->rights->societe->lire) {
945  throw new RestException(401);
946  }
947 
948  if (empty($id)) {
949  throw new RestException(400, 'Thirdparty ID is mandatory');
950  }
951 
952  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
953  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
954  }
955 
956  $result = $this->company->fetch($id);
957  if (!$result) {
958  throw new RestException(404, 'Thirdparty not found');
959  }
960 
961  $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
962 
963  return $result;
964  }
965 
983  public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
984  {
985  $obj_ret = array();
986 
987  if (!DolibarrApiAccess::$user->rights->societe->lire) {
988  throw new RestException(401);
989  }
990 
991  if (empty($id)) {
992  throw new RestException(400, 'Thirdparty ID is mandatory');
993  }
994 
995  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
996  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
997  }
998 
999  $result = $this->company->fetch($id);
1000  if (!$result) {
1001  throw new RestException(404, 'Thirdparty not found');
1002  }
1003 
1004 
1005  $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";
1006  $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1007  $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".$id;
1008  if ($filter == "available") $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1009  if ($filter == "used") $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1010 
1011  $sql .= $this->db->order($sortfield, $sortorder);
1012 
1013  $result = $this->db->query($sql);
1014  if (!$result) {
1015  throw new RestException(503, $this->db->lasterror());
1016  } else {
1017  $num = $this->db->num_rows($result);
1018  while ($obj = $this->db->fetch_object($result)) {
1019  $obj_ret[] = $obj;
1020  }
1021  }
1022 
1023  return $obj_ret;
1024  }
1025 
1026 
1027 
1042  {
1043  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1044  throw new RestException(401);
1045  }
1046  if (empty($id)) {
1047  throw new RestException(400, 'Thirdparty ID is mandatory');
1048  }
1049 
1050  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1051  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1052  }
1053 
1054  /*$result = $this->thirdparty->fetch($id);
1055  if( ! $result ) {
1056  throw new RestException(404, 'Thirdparty not found');
1057  }*/
1058 
1059  $invoice = new Facture($this->db);
1060  $result = $invoice->list_replacable_invoices($id);
1061  if ($result < 0) {
1062  throw new RestException(405, $this->thirdparty->error);
1063  }
1064 
1065  return $result;
1066  }
1067 
1085  {
1086  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1087  throw new RestException(401);
1088  }
1089  if (empty($id)) {
1090  throw new RestException(400, 'Thirdparty ID is mandatory');
1091  }
1092 
1093  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1094  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1095  }
1096 
1097  /*$result = $this->thirdparty->fetch($id);
1098  if( ! $result ) {
1099  throw new RestException(404, 'Thirdparty not found');
1100  }*/
1101 
1102  $invoice = new Facture($this->db);
1103  $result = $invoice->list_qualified_avoir_invoices($id);
1104  if ($result < 0) {
1105  throw new RestException(405, $this->thirdparty->error);
1106  }
1107 
1108  return $result;
1109  }
1110 
1120  public function getCompanyBankAccount($id)
1121  {
1122  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1123  throw new RestException(401);
1124  }
1125  if (empty($id)) {
1126  throw new RestException(400, 'Thirdparty ID is mandatory');
1127  }
1128 
1129  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1130  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1131  }
1132 
1137  $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1138  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1139  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1140  if ($id) $sql .= " WHERE fk_soc = ".$id." ";
1141 
1142 
1143  $result = $this->db->query($sql);
1144 
1145  if ($result->num_rows == 0) {
1146  throw new RestException(404, 'Account not found');
1147  }
1148 
1149  $i = 0;
1150 
1151  $accounts = array();
1152 
1153  if ($result)
1154  {
1155  $num = $this->db->num_rows($result);
1156  while ($i < $num)
1157  {
1158  $obj = $this->db->fetch_object($result);
1159  $account = new CompanyBankAccount($this->db);
1160  if ($account->fetch($obj->rowid)) {
1161  $accounts[] = $account;
1162  }
1163  $i++;
1164  }
1165  } else {
1166  throw new RestException(404, 'Account not found');
1167  }
1168 
1169 
1170  $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1171 
1172  $returnAccounts = array();
1173 
1174  foreach ($accounts as $account) {
1175  $object = array();
1176  foreach ($account as $key => $value) {
1177  if (in_array($key, $fields)) {
1178  $object[$key] = $value;
1179  }
1180  }
1181  $returnAccounts[] = $object;
1182  }
1183 
1184  return $returnAccounts;
1185  }
1186 
1196  public function createCompanyBankAccount($id, $request_data = null)
1197  {
1198  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1199  throw new RestException(401);
1200  }
1201  if ($this->company->fetch($id) <= 0) {
1202  throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1203  }
1204  $account = new CompanyBankAccount($this->db);
1205 
1206  $account->socid = $id;
1207 
1208  foreach ($request_data as $field => $value) {
1209  $account->$field = $value;
1210  }
1211 
1212  if ($account->create(DolibarrApiAccess::$user) < 0)
1213  throw new RestException(500, 'Error creating Company Bank account');
1214 
1215  if (empty($account->rum)) {
1216  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1217  $prelevement = new BonPrelevement($this->db);
1218  $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1219  $account->date_rum = dol_now();
1220  }
1221 
1222  if ($account->update(DolibarrApiAccess::$user) < 0)
1223  throw new RestException(500, 'Error updating values');
1224 
1225  return $this->_cleanObjectDatas($account);
1226  }
1227 
1239  public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1240  {
1241  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1242  throw new RestException(401);
1243  }
1244  if ($this->company->fetch($id) <= 0) {
1245  throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1246  }
1247  $account = new CompanyBankAccount($this->db);
1248 
1249  $account->fetch($bankaccount_id, $id, -1, '');
1250 
1251  if ($account->socid != $id) {
1252  throw new RestException(401);
1253  }
1254 
1255 
1256  foreach ($request_data as $field => $value) {
1257  $account->$field = $value;
1258  }
1259 
1260  if (empty($account->rum)) {
1261  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1262  $prelevement = new BonPrelevement($this->db);
1263  $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1264  $account->date_rum = dol_now();
1265  }
1266 
1267  if ($account->update(DolibarrApiAccess::$user) < 0)
1268  throw new RestException(500, 'Error updating values');
1269 
1270  return $this->_cleanObjectDatas($account);
1271  }
1272 
1283  public function deleteCompanyBankAccount($id, $bankaccount_id)
1284  {
1285  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1286  throw new RestException(401);
1287  }
1288 
1289  $account = new CompanyBankAccount($this->db);
1290 
1291  $account->fetch($bankaccount_id);
1292 
1293  if (!$account->socid == $id)
1294  throw new RestException(401);
1295 
1296  return $account->delete(DolibarrApiAccess::$user);
1297  }
1298 
1309  public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1310  {
1311  global $conf, $langs;
1312 
1313  $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1314 
1315  if ($this->company->fetch($id) <= 0) {
1316  throw new RestException(404, 'Thirdparty not found');
1317  }
1318 
1319  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1320  throw new RestException(401);
1321  }
1322 
1323  $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1324 
1325  $this->company->fk_bank = $this->company->fk_account;
1326 
1327  $outputlangs = $langs;
1328  $newlang = '';
1329 
1330  //if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1331  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1332  if (isset($this->company->thirdparty->default_lang)) {
1333  $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1334  } elseif (isset($this->company->default_lang)) {
1335  $newlang = $this->company->default_lang; // for thirdparty
1336  }
1337  }
1338  if (!empty($newlang)) {
1339  $outputlangs = new Translate("", $conf);
1340  $outputlangs->setDefaultLang($newlang);
1341  }
1342 
1343  $sql = "SELECT rowid";
1344  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1345  if ($id) $sql .= " WHERE fk_soc = ".$id." ";
1346  if ($companybankid) $sql .= " AND rowid = ".$companybankid."";
1347 
1348  $i = 0;
1349  $accounts = array();
1350 
1351  $result = $this->db->query($sql);
1352  if ($result) {
1353  if ($this->db->num_rows($result) == 0) {
1354  throw new RestException(404, 'Bank account not found');
1355  }
1356 
1357  $num = $this->db->num_rows($result);
1358  while ($i < $num) {
1359  $obj = $this->db->fetch_object($result);
1360 
1361  $account = new CompanyBankAccount($this->db);
1362  if ($account->fetch($obj->rowid)) {
1363  $accounts[] = $account;
1364  }
1365  $i++;
1366  }
1367  } else {
1368  throw new RestException(500, 'Sql error '.$this->db->lasterror());
1369  }
1370 
1371  $moreparams = array(
1372  'use_companybankid' => $accounts[0]->id,
1373  'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1374  );
1375 
1376  $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1377 
1378  if ($result > 0) {
1379  return array("success" => $result);
1380  } else {
1381  throw new RestException(500);
1382  }
1383  }
1384 
1397  public function getSocieteAccounts($id, $site = null)
1398  {
1399  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1400  throw new RestException(401);
1401  }
1402 
1403  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1404  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1405  }
1406 
1410  $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1411  $sql .= " WHERE fk_soc = $id";
1412  if ($site) $sql .= " AND site ='$site'";
1413 
1414  $result = $this->db->query($sql);
1415 
1416  if ($result && $this->db->num_rows($result) == 0) {
1417  throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1418  }
1419 
1420  $i = 0;
1421 
1422  $accounts = array();
1423 
1424  $num = $this->db->num_rows($result);
1425  while ($i < $num)
1426  {
1427  $obj = $this->db->fetch_object($result);
1428  $account = new SocieteAccount($this->db);
1429 
1430  if ($account->fetch($obj->rowid)) {
1431  $accounts[] = $account;
1432  }
1433  $i++;
1434  }
1435 
1436  $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1437 
1438  $returnAccounts = array();
1439 
1440  foreach ($accounts as $account) {
1441  $object = array();
1442  foreach ($account as $key => $value) {
1443  if (in_array($key, $fields)) {
1444  $object[$key] = $value;
1445  }
1446  }
1447  $returnAccounts[] = $object;
1448  }
1449 
1450  return $returnAccounts;
1451  }
1452 
1472  public function createSocieteAccount($id, $request_data = null)
1473  {
1474  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1475  throw new RestException(401);
1476  }
1477 
1478  if (!isset($request_data['site'])) {
1479  throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1480  }
1481 
1482  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '".$this->db->escape($request_data['site'])."'";
1483  $result = $this->db->query($sql);
1484 
1485  if ($result && $this->db->num_rows($result) == 0) {
1486  $account = new SocieteAccount($this->db);
1487  if (!isset($request_data['login'])) {
1488  $account->login = "";
1489  }
1490  $account->fk_soc = $id;
1491 
1492  foreach ($request_data as $field => $value) {
1493  $account->$field = $value;
1494  }
1495 
1496  if ($account->create(DolibarrApiAccess::$user) < 0)
1497  throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1498 
1499  $this->_cleanObjectDatas($account);
1500 
1501  return $account;
1502  } else {
1503  throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1504  }
1505  }
1506 
1529  public function putSocieteAccount($id, $site, $request_data = null)
1530  {
1531  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1532  throw new RestException(401);
1533  }
1534 
1535  $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1536  $result = $this->db->query($sql);
1537 
1538  // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1539  if ($result && $this->db->num_rows == 0) {
1540  if (!isset($request_data['key_account'])) {
1541  throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1542  }
1543  $account = new SocieteAccount($this->db);
1544  if (!isset($request_data['login'])) {
1545  $account->login = "";
1546  }
1547 
1548  foreach ($request_data as $field => $value) {
1549  $account->$field = $value;
1550  }
1551 
1552  $account->fk_soc = $id;
1553  $account->site = $site;
1554 
1555  if ($account->create(DolibarrApiAccess::$user) < 0) {
1556  throw new RestException(500, 'Error creating SocieteAccount entity.');
1557  }
1558  // We found an existing SocieteAccount entity, we are replacing it
1559  } else {
1560  if (isset($request_data['site']) && $request_data['site'] !== $site) {
1561  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '".$this->db->escape($request_data['site'])."' ";
1562  $result = $this->db->query($sql);
1563 
1564  if ($result && $this->db->num_rows($result) !== 0) {
1565  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.");
1566  }
1567  }
1568 
1569  $obj = $this->db->fetch_object($result);
1570 
1571  $account = new SocieteAccount($this->db);
1572  $account->id = $obj->rowid;
1573  $account->fk_soc = $id;
1574  $account->site = $site;
1575  if (!isset($request_data['login'])) {
1576  $account->login = "";
1577  }
1578  $account->fk_user_creat = $obj->fk_user_creat;
1579  $account->date_creation = $obj->date_creation;
1580 
1581  foreach ($request_data as $field => $value) {
1582  $account->$field = $value;
1583  }
1584 
1585  if ($account->update(DolibarrApiAccess::$user) < 0)
1586  throw new RestException(500, 'Error updating SocieteAccount entity.');
1587  }
1588 
1589  $this->_cleanObjectDatas($account);
1590 
1591  return $account;
1592  }
1593 
1610  public function patchSocieteAccount($id, $site, $request_data = null)
1611  {
1612  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1613  throw new RestException(401);
1614  }
1615 
1616  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' ";
1617  $result = $this->db->query($sql);
1618 
1619  if ($result && $this->db->num_rows($result) == 0) {
1620  throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1621  } else {
1622  // If the user tries to edit the site member, we check first if
1623  if (isset($request_data['site']) && $request_data['site'] !== $site) {
1624  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$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) site member from $site to ".$request_data['site']." but another SocieteAccount entity already exists for this thirdparty with this site key.");
1629  }
1630 
1631  $obj = $this->db->fetch_object($result);
1632  $account = new SocieteAccount($this->db);
1633  $account->fetch($obj->rowid);
1634 
1635  foreach ($request_data as $field => $value) {
1636  $account->$field = $value;
1637  }
1638 
1639  if ($account->update(DolibarrApiAccess::$user) < 0)
1640  throw new RestException(500, 'Error updating SocieteAccount account');
1641 
1642  $this->_cleanObjectDatas($account);
1643 
1644  return $account;
1645  }
1646  }
1647 
1661  public function deleteSocieteAccount($id, $site)
1662  {
1663  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1664  throw new RestException(401);
1665  }
1666 
1667  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1668  $result = $this->db->query($sql);
1669 
1670  if ($result && $this->db->num_rows($result) == 0) {
1671  throw new RestException(404);
1672  } else {
1673  $obj = $this->db->fetch_object($result);
1674  $account = new SocieteAccount($this->db);
1675  $account->fetch($obj->rowid);
1676 
1677  if ($account->delete(DolibarrApiAccess::$user) < 0) {
1678  throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1679  }
1680  }
1681  }
1682 
1695  public function deleteSocieteAccounts($id)
1696  {
1697  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1698  throw new RestException(401);
1699  }
1700 
1705  $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1706  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id;
1707 
1708  $result = $this->db->query($sql);
1709 
1710  if ($result && $this->db->num_rows($result) == 0) {
1711  throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1712  } else {
1713  $i = 0;
1714 
1715  $num = $this->db->num_rows($result);
1716  while ($i < $num)
1717  {
1718  $obj = $this->db->fetch_object($result);
1719  $account = new SocieteAccount($this->db);
1720  $account->fetch($obj->rowid);
1721 
1722  if ($account->delete(DolibarrApiAccess::$user) < 0) {
1723  throw new RestException(500, 'Error while deleting gateways attached to this third party');
1724  }
1725  $i++;
1726  }
1727  }
1728  }
1729 
1730  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1737  protected function _cleanObjectDatas($object)
1738  {
1739  // phpcs:enable
1740  $object = parent::_cleanObjectDatas($object);
1741 
1742  unset($object->nom); // ->name already defined and nom deprecated
1743  unset($object->name_bis); // ->name_alias already defined
1744  unset($object->note); // ->note_private and note_public already defined
1745  unset($object->departement);
1746  unset($object->departement_code);
1747  unset($object->pays);
1748  unset($object->particulier);
1749  unset($object->prefix_comm);
1750 
1751  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.
1752 
1753  unset($object->total_ht);
1754  unset($object->total_tva);
1755  unset($object->total_localtax1);
1756  unset($object->total_localtax2);
1757  unset($object->total_ttc);
1758 
1759  unset($object->lines);
1760  unset($object->thirdparty);
1761 
1762  unset($object->fk_delivery_address); // deprecated feature
1763 
1764  unset($object->skype);
1765  unset($object->twitter);
1766  unset($object->facebook);
1767  unset($object->linkedin);
1768 
1769  return $object;
1770  }
1771 
1780  private function _validate($data)
1781  {
1782  $thirdparty = array();
1783  foreach (Thirdparties::$FIELDS as $field) {
1784  if (!isset($data[$field]))
1785  throw new RestException(400, "$field field missing");
1786  $thirdparty[$field] = $data[$field];
1787  }
1788  return $thirdparty;
1789  }
1790 
1812  private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1813  {
1814  global $conf;
1815  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1816  throw new RestException(401);
1817  }
1818  if ($rowid === 0) {
1819  $result = $this->company->initAsSpecimen();
1820  } else {
1821  $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1822  }
1823  if (!$result) {
1824  throw new RestException(404, 'Thirdparty not found');
1825  }
1826 
1827  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1828  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1829  }
1830 
1831  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1832  $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1833  $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1834  } else {
1835  $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1836  $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1837  }
1838 
1839  $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1840  $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1841  $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1842  $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1843 
1844  return $this->_cleanObjectDatas($this->company);
1845  }
1846 }
__construct()
Constructor.
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
_cleanObjectDatas($object)
Clean sensible object datas.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder= 'ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
_fetch($rowid, $ref= '', $ref_ext= '', $barcode= '', $idprof1= '', $idprof2= '', $idprof3= '', $idprof4= '', $idprof5= '', $idprof6= '', $email= '', $ref_alias= '')
Fetch properties of a thirdparty object.
addCategory($id, $category_id)
Add a customer category to a thirdparty.
getSalesRepresentatives($id, $mode=0)
Get representatives of thirdparty.
dol_now($mode= 'auto')
Return date for now.
getOutStandingOrder($id, $mode= 'customer')
Get outstanding orders of thirdparty.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
Class to manage bank accounts description of third parties.
_validate($data)
Validate fields before create or update object.
getByEmail($email)
Get properties of a thirdparty object by email.
post($request_data=null)
Create thirdparty object.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters= '')
List thirdparties.
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
Class for SocieteAccount.
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
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...
getOutStandingInvoices($id, $mode= 'customer')
Get outstanding invoices of thirdparty.
$conf db
API class for accounts.
Definition: inc.php:54
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder= 'ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers...
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage categories.
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific site gateway attached to a thirdparty.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
Class to manage withdrawal receipts.
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
createSocieteAccount($id, $request_data=null)
Create and attach a new gateway to an existing thirdparty.
Class to manage translations.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
getOutStandingProposals($id, $mode= 'customer')
Get outstanding proposals of thirdparty.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
merge($id, $idtodelete)
Merge a thirdparty into another one.
Class to manage invoices.
generateBankAccountDocument($id, $companybankid=null, $model= 'sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
getCategories($id, $sortfield="s.rowid", $sortorder= 'ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
put($id, $request_data=null)
Update thirdparty.
deleteSocieteAccounts($id)
Delete all gateways attached to a thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway id)