dolibarr  16.0.1
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  return $this->company->delete($id);
532  }
533 
549  public function setThirdpartyPriceLevel($id, $priceLevel)
550  {
551  global $conf;
552 
553  if (empty($conf->societe->enabled)) {
554  throw new RestException(501, 'Module "Thirdparties" needed for this request');
555  }
556 
557  if (empty($conf->product->enabled)) {
558  throw new RestException(501, 'Module "Products" needed for this request');
559  }
560 
561  if (empty($conf->global->PRODUIT_MULTIPRICES)) {
562  throw new RestException(501, 'Multiprices features activation needed for this request');
563  }
564 
565  if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
566  throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
567  }
568 
569  if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
570  throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
571  }
572 
573  $result = $this->company->fetch($id);
574  if ($result < 0) {
575  throw new RestException(404, 'Thirdparty '.$id.' not found');
576  }
577 
578  if (empty($result)) {
579  throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
580  }
581 
582  if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
583  throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
584  }
585 
586  $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
587  if ($result <= 0) {
588  throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
589  }
590 
591  return $this->_cleanObjectDatas($this->company);
592  }
593 
607  public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
608  {
609  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
610  throw new RestException(401);
611  }
612 
613  $result = $this->company->fetch($id);
614  if (!$result) {
615  throw new RestException(404, 'Thirdparty not found');
616  }
617 
618  $categories = new Categorie($this->db);
619 
620  $result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
621 
622  if (is_numeric($result) && $result < 0) {
623  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
624  }
625 
626  if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
627  return array();
628  }
629 
630  return $result;
631  }
632 
643  public function addCategory($id, $category_id)
644  {
645  if (!DolibarrApiAccess::$user->rights->societe->creer) {
646  throw new RestException(401);
647  }
648 
649  $result = $this->company->fetch($id);
650  if (!$result) {
651  throw new RestException(404, 'Thirdparty not found');
652  }
653  $category = new Categorie($this->db);
654  $result = $category->fetch($category_id);
655  if (!$result) {
656  throw new RestException(404, 'category not found');
657  }
658 
659  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
660  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
661  }
662  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
663  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
664  }
665 
666  $category->add_type($this->company, 'customer');
667 
668  return $this->_cleanObjectDatas($this->company);
669  }
670 
681  public function deleteCategory($id, $category_id)
682  {
683  if (!DolibarrApiAccess::$user->rights->societe->creer) {
684  throw new RestException(401);
685  }
686 
687  $result = $this->company->fetch($id);
688  if (!$result) {
689  throw new RestException(404, 'Thirdparty not found');
690  }
691  $category = new Categorie($this->db);
692  $result = $category->fetch($category_id);
693  if (!$result) {
694  throw new RestException(404, 'category not found');
695  }
696 
697  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
698  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
699  }
700  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
701  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
702  }
703 
704  $category->del_type($this->company, 'customer');
705 
706  return $this->_cleanObjectDatas($this->company);
707  }
708 
722  public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
723  {
724  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
725  throw new RestException(401);
726  }
727 
728  $result = $this->company->fetch($id);
729  if (!$result) {
730  throw new RestException(404, 'Thirdparty not found');
731  }
732 
733  $categories = new Categorie($this->db);
734 
735  $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
736 
737  if (is_numeric($result) && $result < 0) {
738  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
739  }
740 
741  if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
742  return array();
743  }
744 
745  return $result;
746  }
747 
758  public function addSupplierCategory($id, $category_id)
759  {
760  if (!DolibarrApiAccess::$user->rights->societe->creer) {
761  throw new RestException(401);
762  }
763 
764  $result = $this->company->fetch($id);
765  if (!$result) {
766  throw new RestException(404, 'Thirdparty not found');
767  }
768  $category = new Categorie($this->db);
769  $result = $category->fetch($category_id);
770  if (!$result) {
771  throw new RestException(404, 'category not found');
772  }
773 
774  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
775  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
776  }
777  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
778  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
779  }
780 
781  $category->add_type($this->company, 'supplier');
782 
783  return $this->_cleanObjectDatas($this->company);
784  }
785 
796  public function deleteSupplierCategory($id, $category_id)
797  {
798  if (!DolibarrApiAccess::$user->rights->societe->creer) {
799  throw new RestException(401);
800  }
801 
802  $result = $this->company->fetch($id);
803  if (!$result) {
804  throw new RestException(404, 'Thirdparty not found');
805  }
806  $category = new Categorie($this->db);
807  $result = $category->fetch($category_id);
808  if (!$result) {
809  throw new RestException(404, 'category not found');
810  }
811 
812  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
813  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
814  }
815  if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
816  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
817  }
818 
819  $category->del_type($this->company, 'supplier');
820 
821  return $this->_cleanObjectDatas($this->company);
822  }
823 
824 
839  public function getOutStandingProposals($id, $mode = 'customer')
840  {
841  if (!DolibarrApiAccess::$user->rights->societe->lire) {
842  throw new RestException(401);
843  }
844 
845  if (empty($id)) {
846  throw new RestException(400, 'Thirdparty ID is mandatory');
847  }
848 
849  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
850  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
851  }
852 
853  $result = $this->company->fetch($id);
854  if (!$result) {
855  throw new RestException(404, 'Thirdparty not found');
856  }
857 
858  $result = $this->company->getOutstandingProposals($mode);
859 
860  unset($result['total_ht']);
861  unset($result['total_ttc']);
862 
863  return $result;
864  }
865 
866 
881  public function getOutStandingOrder($id, $mode = 'customer')
882  {
883  if (!DolibarrApiAccess::$user->rights->societe->lire) {
884  throw new RestException(401);
885  }
886 
887  if (empty($id)) {
888  throw new RestException(400, 'Thirdparty ID is mandatory');
889  }
890 
891  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
892  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
893  }
894 
895  $result = $this->company->fetch($id);
896  if (!$result) {
897  throw new RestException(404, 'Thirdparty not found');
898  }
899 
900  $result = $this->company->getOutstandingOrders($mode);
901 
902  unset($result['total_ht']);
903  unset($result['total_ttc']);
904 
905  return $result;
906  }
907 
922  public function getOutStandingInvoices($id, $mode = 'customer')
923  {
924  if (!DolibarrApiAccess::$user->rights->societe->lire) {
925  throw new RestException(401);
926  }
927 
928  if (empty($id)) {
929  throw new RestException(400, 'Thirdparty ID is mandatory');
930  }
931 
932  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
933  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
934  }
935 
936  $result = $this->company->fetch($id);
937  if (!$result) {
938  throw new RestException(404, 'Thirdparty not found');
939  }
940 
941  $result = $this->company->getOutstandingBills($mode);
942 
943  unset($result['total_ht']);
944  unset($result['total_ttc']);
945 
946  return $result;
947  }
948 
963  public function getSalesRepresentatives($id, $mode = 0)
964  {
965  if (!DolibarrApiAccess::$user->rights->societe->lire) {
966  throw new RestException(401);
967  }
968 
969  if (empty($id)) {
970  throw new RestException(400, 'Thirdparty ID is mandatory');
971  }
972 
973  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
974  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
975  }
976 
977  $result = $this->company->fetch($id);
978  if (!$result) {
979  throw new RestException(404, 'Thirdparty not found');
980  }
981 
982  $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
983 
984  return $result;
985  }
986 
1004  public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
1005  {
1006  $obj_ret = array();
1007 
1008  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1009  throw new RestException(401);
1010  }
1011 
1012  if (empty($id)) {
1013  throw new RestException(400, 'Thirdparty ID is mandatory');
1014  }
1015 
1016  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1017  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1018  }
1019 
1020  $result = $this->company->fetch($id);
1021  if (!$result) {
1022  throw new RestException(404, 'Thirdparty not found');
1023  }
1024 
1025 
1026  $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";
1027  $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
1028  $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1029  if ($filter == "available") {
1030  $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1031  }
1032  if ($filter == "used") {
1033  $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1034  }
1035 
1036  $sql .= $this->db->order($sortfield, $sortorder);
1037 
1038  $result = $this->db->query($sql);
1039  if (!$result) {
1040  throw new RestException(503, $this->db->lasterror());
1041  } else {
1042  $num = $this->db->num_rows($result);
1043  while ($obj = $this->db->fetch_object($result)) {
1044  $obj_ret[] = $obj;
1045  }
1046  }
1047 
1048  return $obj_ret;
1049  }
1050 
1051 
1052 
1067  {
1068  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1069  throw new RestException(401);
1070  }
1071  if (empty($id)) {
1072  throw new RestException(400, 'Thirdparty ID is mandatory');
1073  }
1074 
1075  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1076  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1077  }
1078 
1079  /*$result = $this->thirdparty->fetch($id);
1080  if( ! $result ) {
1081  throw new RestException(404, 'Thirdparty not found');
1082  }*/
1083 
1084  $invoice = new Facture($this->db);
1085  $result = $invoice->list_replacable_invoices($id);
1086  if ($result < 0) {
1087  throw new RestException(405, $this->thirdparty->error);
1088  }
1089 
1090  return $result;
1091  }
1092 
1110  {
1111  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1112  throw new RestException(401);
1113  }
1114  if (empty($id)) {
1115  throw new RestException(400, 'Thirdparty ID is mandatory');
1116  }
1117 
1118  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1119  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1120  }
1121 
1122  /*$result = $this->thirdparty->fetch($id);
1123  if( ! $result ) {
1124  throw new RestException(404, 'Thirdparty not found');
1125  }*/
1126 
1127  $invoice = new Facture($this->db);
1128  $result = $invoice->list_qualified_avoir_invoices($id);
1129  if ($result < 0) {
1130  throw new RestException(405, $this->thirdparty->error);
1131  }
1132 
1133  return $result;
1134  }
1135 
1145  public function getCompanyBankAccount($id)
1146  {
1147  if (!DolibarrApiAccess::$user->rights->facture->lire) {
1148  throw new RestException(401);
1149  }
1150  if (empty($id)) {
1151  throw new RestException(400, 'Thirdparty ID is mandatory');
1152  }
1153 
1154  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1155  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1156  }
1157 
1162  $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1163  $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1164  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1165  if ($id) {
1166  $sql .= " WHERE fk_soc = ".((int) $id);
1167  }
1168 
1169 
1170  $result = $this->db->query($sql);
1171 
1172  if ($result->num_rows == 0) {
1173  throw new RestException(404, 'Account not found');
1174  }
1175 
1176  $i = 0;
1177 
1178  $accounts = array();
1179 
1180  if ($result) {
1181  $num = $this->db->num_rows($result);
1182  while ($i < $num) {
1183  $obj = $this->db->fetch_object($result);
1184  $account = new CompanyBankAccount($this->db);
1185  if ($account->fetch($obj->rowid)) {
1186  $accounts[] = $account;
1187  }
1188  $i++;
1189  }
1190  } else {
1191  throw new RestException(404, 'Account not found');
1192  }
1193 
1194 
1195  $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
1196 
1197  $returnAccounts = array();
1198 
1199  foreach ($accounts as $account) {
1200  $object = array();
1201  foreach ($account as $key => $value) {
1202  if (in_array($key, $fields)) {
1203  $object[$key] = $value;
1204  }
1205  }
1206  $returnAccounts[] = $object;
1207  }
1208 
1209  return $returnAccounts;
1210  }
1211 
1221  public function createCompanyBankAccount($id, $request_data = null)
1222  {
1223  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1224  throw new RestException(401);
1225  }
1226  if ($this->company->fetch($id) <= 0) {
1227  throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1228  }
1229  $account = new CompanyBankAccount($this->db);
1230 
1231  $account->socid = $id;
1232 
1233  foreach ($request_data as $field => $value) {
1234  $account->$field = $value;
1235  }
1236 
1237  if ($account->create(DolibarrApiAccess::$user) < 0) {
1238  throw new RestException(500, 'Error creating Company Bank account');
1239  }
1240 
1241  if (empty($account->rum)) {
1242  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1243  $prelevement = new BonPrelevement($this->db);
1244  $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1245  $account->date_rum = dol_now();
1246  }
1247 
1248  if ($account->update(DolibarrApiAccess::$user) < 0) {
1249  throw new RestException(500, 'Error updating values');
1250  }
1251 
1252  return $this->_cleanObjectDatas($account);
1253  }
1254 
1266  public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
1267  {
1268  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1269  throw new RestException(401);
1270  }
1271  if ($this->company->fetch($id) <= 0) {
1272  throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
1273  }
1274  $account = new CompanyBankAccount($this->db);
1275 
1276  $account->fetch($bankaccount_id, $id, -1, '');
1277 
1278  if ($account->socid != $id) {
1279  throw new RestException(401);
1280  }
1281 
1282 
1283  foreach ($request_data as $field => $value) {
1284  $account->$field = $value;
1285  }
1286 
1287  if (empty($account->rum)) {
1288  require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
1289  $prelevement = new BonPrelevement($this->db);
1290  $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1291  $account->date_rum = dol_now();
1292  }
1293 
1294  if ($account->update(DolibarrApiAccess::$user) < 0) {
1295  throw new RestException(500, 'Error updating values');
1296  }
1297 
1298  return $this->_cleanObjectDatas($account);
1299  }
1300 
1311  public function deleteCompanyBankAccount($id, $bankaccount_id)
1312  {
1313  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1314  throw new RestException(401);
1315  }
1316 
1317  $account = new CompanyBankAccount($this->db);
1318 
1319  $account->fetch($bankaccount_id);
1320 
1321  if (!$account->socid == $id) {
1322  throw new RestException(401);
1323  }
1324 
1325  return $account->delete(DolibarrApiAccess::$user);
1326  }
1327 
1338  public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
1339  {
1340  global $conf, $langs;
1341 
1342  $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
1343 
1344  if ($this->company->fetch($id) <= 0) {
1345  throw new RestException(404, 'Thirdparty not found');
1346  }
1347 
1348  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1349  throw new RestException(401);
1350  }
1351 
1352  $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1353 
1354  $this->company->fk_bank = $this->company->fk_account;
1355 
1356  $outputlangs = $langs;
1357  $newlang = '';
1358 
1359  //if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
1360  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1361  if (isset($this->company->thirdparty->default_lang)) {
1362  $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
1363  } elseif (isset($this->company->default_lang)) {
1364  $newlang = $this->company->default_lang; // for thirdparty
1365  }
1366  }
1367  if (!empty($newlang)) {
1368  $outputlangs = new Translate("", $conf);
1369  $outputlangs->setDefaultLang($newlang);
1370  }
1371 
1372  $sql = "SELECT rowid";
1373  $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
1374  if ($id) {
1375  $sql .= " WHERE fk_soc = ".((int) $id);
1376  }
1377  if ($companybankid) {
1378  $sql .= " AND rowid = ".((int) $companybankid);
1379  }
1380 
1381  $i = 0;
1382  $accounts = array();
1383 
1384  $result = $this->db->query($sql);
1385  if ($result) {
1386  if ($this->db->num_rows($result) == 0) {
1387  throw new RestException(404, 'Bank account not found');
1388  }
1389 
1390  $num = $this->db->num_rows($result);
1391  while ($i < $num) {
1392  $obj = $this->db->fetch_object($result);
1393 
1394  $account = new CompanyBankAccount($this->db);
1395  if ($account->fetch($obj->rowid)) {
1396  $accounts[] = $account;
1397  }
1398  $i++;
1399  }
1400  } else {
1401  throw new RestException(500, 'Sql error '.$this->db->lasterror());
1402  }
1403 
1404  $moreparams = array(
1405  'use_companybankid' => $accounts[0]->id,
1406  'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
1407  );
1408 
1409  $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1410 
1411  if ($result > 0) {
1412  return array("success" => $result);
1413  } else {
1414  throw new RestException(500, 'Error generating the document '.$this->error);
1415  }
1416  }
1417 
1430  public function getSocieteAccounts($id, $site = null)
1431  {
1432  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1433  throw new RestException(401);
1434  }
1435 
1436  if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
1437  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1438  }
1439 
1443  $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
1444  $sql .= " WHERE fk_soc = ".((int) $id);
1445  if ($site) {
1446  $sql .= " AND site ='".$this->db->escape($site)."'";
1447  }
1448 
1449  $result = $this->db->query($sql);
1450 
1451  if ($result && $this->db->num_rows($result) == 0) {
1452  throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
1453  }
1454 
1455  $i = 0;
1456 
1457  $accounts = array();
1458 
1459  $num = $this->db->num_rows($result);
1460  while ($i < $num) {
1461  $obj = $this->db->fetch_object($result);
1462  $account = new SocieteAccount($this->db);
1463 
1464  if ($account->fetch($obj->rowid)) {
1465  $accounts[] = $account;
1466  }
1467  $i++;
1468  }
1469 
1470  $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
1471 
1472  $returnAccounts = array();
1473 
1474  foreach ($accounts as $account) {
1475  $object = array();
1476  foreach ($account as $key => $value) {
1477  if (in_array($key, $fields)) {
1478  $object[$key] = $value;
1479  }
1480  }
1481  $returnAccounts[] = $object;
1482  }
1483 
1484  return $returnAccounts;
1485  }
1486 
1506  public function createSocieteAccount($id, $request_data = null)
1507  {
1508  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1509  throw new RestException(401);
1510  }
1511 
1512  if (!isset($request_data['site'])) {
1513  throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
1514  }
1515 
1516  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
1517  $result = $this->db->query($sql);
1518 
1519  if ($result && $this->db->num_rows($result) == 0) {
1520  $account = new SocieteAccount($this->db);
1521  if (!isset($request_data['login'])) {
1522  $account->login = "";
1523  }
1524  $account->fk_soc = $id;
1525 
1526  foreach ($request_data as $field => $value) {
1527  $account->$field = $value;
1528  }
1529 
1530  if ($account->create(DolibarrApiAccess::$user) < 0) {
1531  throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1532  }
1533 
1534  $this->_cleanObjectDatas($account);
1535 
1536  return $account;
1537  } else {
1538  throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
1539  }
1540  }
1541 
1564  public function putSocieteAccount($id, $site, $request_data = null)
1565  {
1566  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1567  throw new RestException(401);
1568  }
1569 
1570  $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1571  $result = $this->db->query($sql);
1572 
1573  // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
1574  if ($result && $this->db->num_rows == 0) {
1575  if (!isset($request_data['key_account'])) {
1576  throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1577  }
1578  $account = new SocieteAccount($this->db);
1579  if (!isset($request_data['login'])) {
1580  $account->login = "";
1581  }
1582 
1583  foreach ($request_data as $field => $value) {
1584  $account->$field = $value;
1585  }
1586 
1587  $account->fk_soc = $id;
1588  $account->site = $site;
1589 
1590  if ($account->create(DolibarrApiAccess::$user) < 0) {
1591  throw new RestException(500, 'Error creating SocieteAccount entity.');
1592  }
1593  // We found an existing SocieteAccount entity, we are replacing it
1594  } else {
1595  if (isset($request_data['site']) && $request_data['site'] !== $site) {
1596  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1597  $result = $this->db->query($sql);
1598 
1599  if ($result && $this->db->num_rows($result) !== 0) {
1600  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.");
1601  }
1602  }
1603 
1604  $obj = $this->db->fetch_object($result);
1605 
1606  $account = new SocieteAccount($this->db);
1607  $account->id = $obj->rowid;
1608  $account->fk_soc = $id;
1609  $account->site = $site;
1610  if (!isset($request_data['login'])) {
1611  $account->login = "";
1612  }
1613  $account->fk_user_creat = $obj->fk_user_creat;
1614  $account->date_creation = $obj->date_creation;
1615 
1616  foreach ($request_data as $field => $value) {
1617  $account->$field = $value;
1618  }
1619 
1620  if ($account->update(DolibarrApiAccess::$user) < 0) {
1621  throw new RestException(500, 'Error updating SocieteAccount entity.');
1622  }
1623  }
1624 
1625  $this->_cleanObjectDatas($account);
1626 
1627  return $account;
1628  }
1629 
1646  public function patchSocieteAccount($id, $site, $request_data = null)
1647  {
1648  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1649  throw new RestException(401);
1650  }
1651 
1652  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
1653  $result = $this->db->query($sql);
1654 
1655  if ($result && $this->db->num_rows($result) == 0) {
1656  throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
1657  } else {
1658  // If the user tries to edit the site member, we check first if
1659  if (isset($request_data['site']) && $request_data['site'] !== $site) {
1660  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
1661  $result = $this->db->query($sql);
1662 
1663  if ($result && $this->db->num_rows($result) !== 0) {
1664  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.");
1665  }
1666  }
1667 
1668  $obj = $this->db->fetch_object($result);
1669  $account = new SocieteAccount($this->db);
1670  $account->fetch($obj->rowid);
1671 
1672  foreach ($request_data as $field => $value) {
1673  $account->$field = $value;
1674  }
1675 
1676  if ($account->update(DolibarrApiAccess::$user) < 0) {
1677  throw new RestException(500, 'Error updating SocieteAccount account');
1678  }
1679 
1680  $this->_cleanObjectDatas($account);
1681 
1682  return $account;
1683  }
1684  }
1685 
1699  public function deleteSocieteAccount($id, $site)
1700  {
1701  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1702  throw new RestException(401);
1703  }
1704 
1705  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
1706  $result = $this->db->query($sql);
1707 
1708  if ($result && $this->db->num_rows($result) == 0) {
1709  throw new RestException(404);
1710  } else {
1711  $obj = $this->db->fetch_object($result);
1712  $account = new SocieteAccount($this->db);
1713  $account->fetch($obj->rowid);
1714 
1715  if ($account->delete(DolibarrApiAccess::$user) < 0) {
1716  throw new RestException(500, "Error while deleting $site gateway attached to this third party");
1717  }
1718  }
1719  }
1720 
1733  public function deleteSocieteAccounts($id)
1734  {
1735  if (!DolibarrApiAccess::$user->rights->societe->creer) {
1736  throw new RestException(401);
1737  }
1738 
1743  $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1744  $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
1745 
1746  $result = $this->db->query($sql);
1747 
1748  if ($result && $this->db->num_rows($result) == 0) {
1749  throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
1750  } else {
1751  $i = 0;
1752 
1753  $num = $this->db->num_rows($result);
1754  while ($i < $num) {
1755  $obj = $this->db->fetch_object($result);
1756  $account = new SocieteAccount($this->db);
1757  $account->fetch($obj->rowid);
1758 
1759  if ($account->delete(DolibarrApiAccess::$user) < 0) {
1760  throw new RestException(500, 'Error while deleting gateways attached to this third party');
1761  }
1762  $i++;
1763  }
1764  }
1765  }
1766 
1767  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1774  protected function _cleanObjectDatas($object)
1775  {
1776  // phpcs:enable
1777  $object = parent::_cleanObjectDatas($object);
1778 
1779  unset($object->nom); // ->name already defined and nom deprecated
1780  unset($object->name_bis); // ->name_alias already defined
1781  unset($object->note); // ->note_private and note_public already defined
1782  unset($object->departement);
1783  unset($object->departement_code);
1784  unset($object->pays);
1785  unset($object->particulier);
1786  unset($object->prefix_comm);
1787 
1788  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.
1789 
1790  unset($object->total_ht);
1791  unset($object->total_tva);
1792  unset($object->total_localtax1);
1793  unset($object->total_localtax2);
1794  unset($object->total_ttc);
1795 
1796  unset($object->lines);
1797  unset($object->thirdparty);
1798 
1799  unset($object->fk_delivery_address); // deprecated feature
1800 
1801  unset($object->skype);
1802  unset($object->twitter);
1803  unset($object->facebook);
1804  unset($object->linkedin);
1805  unset($object->instagram);
1806  unset($object->snapchat);
1807  unset($object->googleplus);
1808  unset($object->youtube);
1809  unset($object->whatsapp);
1810 
1811  return $object;
1812  }
1813 
1822  private function _validate($data)
1823  {
1824  $thirdparty = array();
1825  foreach (Thirdparties::$FIELDS as $field) {
1826  if (!isset($data[$field])) {
1827  throw new RestException(400, "$field field missing");
1828  }
1829  $thirdparty[$field] = $data[$field];
1830  }
1831  return $thirdparty;
1832  }
1833 
1855  private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
1856  {
1857  global $conf;
1858 
1859  if (!DolibarrApiAccess::$user->rights->societe->lire) {
1860  throw new RestException(401);
1861  }
1862 
1863  if ($rowid === 0) {
1864  $result = $this->company->initAsSpecimen();
1865  } else {
1866  $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1867  }
1868  if (!$result) {
1869  throw new RestException(404, 'Thirdparty not found');
1870  }
1871 
1872  if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
1873  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1874  }
1875 
1876  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1877  $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1878  $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
1879  } else {
1880  $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1881  $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1882  }
1883 
1884  $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
1885  $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
1886  $this->company->absolute_discount = price2num($absolute_discount, 'MT');
1887  $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
1888 
1889  return $this->_cleanObjectDatas($this->company);
1890  }
1891 }
__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.
$conf db
API class for accounts.
Definition: inc.php:41
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.
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, &$error= '')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
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 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 access by user to a given resource.
Definition: api.class.php:283
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)