api_thirdparties.class.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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. use Luracast\Restler\RestException;
  21. /**
  22. * API class for thirdparties
  23. *
  24. * @access protected
  25. * @class DolibarrApiAccess {@requires user,external}
  26. *
  27. */
  28. class Thirdparties extends DolibarrApi
  29. {
  30. /**
  31. *
  32. * @var array $FIELDS Mandatory fields, checked when create and update object
  33. */
  34. public static $FIELDS = array(
  35. 'name'
  36. );
  37. /**
  38. * @var Societe $company {@type Societe}
  39. */
  40. public $company;
  41. /**
  42. * Constructor
  43. */
  44. public function __construct()
  45. {
  46. global $db, $conf;
  47. $this->db = $db;
  48. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  49. require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
  50. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  51. require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
  52. $this->company = new Societe($this->db);
  53. if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
  54. static::$FIELDS[] = 'email';
  55. }
  56. }
  57. /**
  58. * Get properties of a thirdparty object
  59. *
  60. * Return an array with thirdparty informations
  61. *
  62. * @param int $id Id of third party to load
  63. * @return array|mixed Cleaned Societe object
  64. *
  65. * @throws RestException
  66. */
  67. public function get($id)
  68. {
  69. return $this->_fetch($id);
  70. }
  71. /**
  72. * Get properties of a thirdparty object by email.
  73. *
  74. * Return an array with thirdparty informations
  75. *
  76. * @param string $email Email of third party to load
  77. * @return array|mixed Cleaned Societe object
  78. *
  79. * @url GET email/{email}
  80. *
  81. * @throws RestException
  82. */
  83. public function getByEmail($email)
  84. {
  85. return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email);
  86. }
  87. /**
  88. * Get properties of a thirdparty object by barcode.
  89. *
  90. * Return an array with thirdparty informations
  91. *
  92. * @param string $barcode Barcode of third party to load
  93. * @return array|mixed Cleaned Societe object
  94. *
  95. * @url GET barcode/{barcode}
  96. *
  97. * @throws RestException
  98. */
  99. public function getByBarcode($barcode)
  100. {
  101. return $this->_fetch('', '', '', $barcode);
  102. }
  103. /**
  104. * List thirdparties
  105. *
  106. * Get a list of thirdparties
  107. *
  108. * @param string $sortfield Sort field
  109. * @param string $sortorder Sort order
  110. * @param int $limit Limit for list
  111. * @param int $page Page number
  112. * @param int $mode Set to 1 to show only customers
  113. * Set to 2 to show only prospects
  114. * Set to 3 to show only those are not customer neither prospect
  115. * Set to 4 to show only suppliers
  116. * @param int $category Use this param to filter list by category
  117. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "((t.nom:like:'TheCompany%') or (t.name_alias:like:'TheCompany%')) and (t.datec:<:'20160101')"
  118. * @return array Array of thirdparty objects
  119. */
  120. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '')
  121. {
  122. $obj_ret = array();
  123. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  124. throw new RestException(401);
  125. }
  126. // case of external user, we force socids
  127. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  128. // If the internal user must only see his customers, force searching by him
  129. $search_sale = 0;
  130. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  131. $search_sale = DolibarrApiAccess::$user->id;
  132. }
  133. $sql = "SELECT t.rowid";
  134. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  135. $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)
  136. }
  137. $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
  138. if ($category > 0) {
  139. if ($mode != 4) {
  140. $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c";
  141. }
  142. if (!in_array($mode, array(1, 2, 3))) {
  143. $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc";
  144. }
  145. }
  146. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  147. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  148. }
  149. $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st";
  150. $sql .= " WHERE t.entity IN (".getEntity('societe').")";
  151. $sql .= " AND t.fk_stcomm = st.id";
  152. if ($mode == 1) {
  153. $sql .= " AND t.client IN (1, 3)";
  154. } elseif ($mode == 2) {
  155. $sql .= " AND t.client IN (2, 3)";
  156. } elseif ($mode == 3) {
  157. $sql .= " AND t.client IN (0)";
  158. } elseif ($mode == 4) {
  159. $sql .= " AND t.fournisseur IN (1)";
  160. }
  161. // Select thirdparties of given category
  162. if ($category > 0) {
  163. if (!empty($mode) && $mode != 4) {
  164. $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_soc = t.rowid";
  165. } elseif (!empty($mode) && $mode == 4) {
  166. $sql .= " AND cc.fk_categorie = ".((int) $category)." AND cc.fk_soc = t.rowid";
  167. } else {
  168. $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))";
  169. }
  170. }
  171. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  172. $sql .= " AND t.rowid = sc.fk_soc";
  173. }
  174. //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
  175. if ($socids) {
  176. $sql .= " AND t.rowid IN (".$this->db->sanitize($socids).")";
  177. }
  178. if ($search_sale > 0) {
  179. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  180. }
  181. // Insert sale filter
  182. if ($search_sale > 0) {
  183. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  184. }
  185. // Add sql filters
  186. if ($sqlfilters) {
  187. $errormessage = '';
  188. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  189. if ($errormessage) {
  190. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  191. }
  192. }
  193. $sql .= $this->db->order($sortfield, $sortorder);
  194. if ($limit) {
  195. if ($page < 0) {
  196. $page = 0;
  197. }
  198. $offset = $limit * $page;
  199. $sql .= $this->db->plimit($limit + 1, $offset);
  200. }
  201. $result = $this->db->query($sql);
  202. if ($result) {
  203. $num = $this->db->num_rows($result);
  204. $min = min($num, ($limit <= 0 ? $num : $limit));
  205. $i = 0;
  206. while ($i < $min) {
  207. $obj = $this->db->fetch_object($result);
  208. $soc_static = new Societe($this->db);
  209. if ($soc_static->fetch($obj->rowid)) {
  210. if (isModEnabled('mailing')) {
  211. $soc_static->getNoEmail();
  212. }
  213. $obj_ret[] = $this->_cleanObjectDatas($soc_static);
  214. }
  215. $i++;
  216. }
  217. } else {
  218. throw new RestException(503, 'Error when retrieve thirdparties : '.$this->db->lasterror());
  219. }
  220. if (!count($obj_ret)) {
  221. throw new RestException(404, 'Thirdparties not found');
  222. }
  223. return $obj_ret;
  224. }
  225. /**
  226. * Create thirdparty object
  227. *
  228. * @param array $request_data Request datas
  229. * @return int ID of thirdparty
  230. */
  231. public function post($request_data = null)
  232. {
  233. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  234. throw new RestException(401);
  235. }
  236. // Check mandatory fields
  237. $result = $this->_validate($request_data);
  238. foreach ($request_data as $field => $value) {
  239. $this->company->$field = $value;
  240. }
  241. if ($this->company->create(DolibarrApiAccess::$user) < 0) {
  242. throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
  243. }
  244. if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
  245. $this->company->setNoEmail($this->company->no_email);
  246. }
  247. return $this->company->id;
  248. }
  249. /**
  250. * Update thirdparty
  251. *
  252. * @param int $id Id of thirdparty to update
  253. * @param array $request_data Datas
  254. * @return array|mixed|boolean
  255. */
  256. public function put($id, $request_data = null)
  257. {
  258. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  259. throw new RestException(401);
  260. }
  261. $result = $this->company->fetch($id);
  262. if (!$result) {
  263. throw new RestException(404, 'Thirdparty not found');
  264. }
  265. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  266. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  267. }
  268. foreach ($request_data as $field => $value) {
  269. if ($field == 'id') {
  270. continue;
  271. }
  272. $this->company->$field = $value;
  273. }
  274. if (isModEnabled('mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
  275. $this->company->setNoEmail($this->company->no_email);
  276. }
  277. if ($this->company->update($id, DolibarrApiAccess::$user, 1, '', '', 'update', 1)) {
  278. return $this->get($id);
  279. }
  280. return false;
  281. }
  282. /**
  283. * Merge a thirdparty into another one.
  284. *
  285. * Merge content (properties, notes) and objects (like invoices, events, orders, proposals, ...) of a thirdparty into a target thirdparty,
  286. * then delete the merged thirdparty.
  287. * If a property has a defined value both in thirdparty to delete and thirdparty to keep, the value into the thirdparty to
  288. * delete will be ignored, the value of target thirdparty will remain, except for notes (content is concatenated).
  289. *
  290. * @param int $id ID of thirdparty to keep (the target thirdparty)
  291. * @param int $idtodelete ID of thirdparty to remove (the thirdparty to delete), once data has been merged into the target thirdparty.
  292. * @return int
  293. *
  294. * @url PUT {id}/merge/{idtodelete}
  295. */
  296. public function merge($id, $idtodelete)
  297. {
  298. global $hookmanager;
  299. $error = 0;
  300. if ($id == $idtodelete) {
  301. throw new RestException(400, 'Try to merge a thirdparty into itself');
  302. }
  303. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  304. throw new RestException(401);
  305. }
  306. $result = $this->company->fetch($id); // include the fetch of extra fields
  307. if (!$result) {
  308. throw new RestException(404, 'Thirdparty not found');
  309. }
  310. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  311. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  312. }
  313. $this->companytoremove = new Societe($this->db);
  314. $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields
  315. if (!$result) {
  316. throw new RestException(404, 'Thirdparty not found');
  317. }
  318. if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) {
  319. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  320. }
  321. $soc_origin = $this->companytoremove;
  322. $object = $this->company;
  323. $user = DolibarrApiAccess::$user;
  324. // Call same code than into action 'confirm_merge'
  325. $this->db->begin();
  326. // Recopy some data
  327. $object->client = $object->client | $soc_origin->client;
  328. $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
  329. $listofproperties = array(
  330. 'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode',
  331. 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6',
  332. 'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis',
  333. 'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency',
  334. 'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur',
  335. 'model_pdf', 'fk_projet'
  336. );
  337. foreach ($listofproperties as $property) {
  338. if (empty($object->$property)) {
  339. $object->$property = $soc_origin->$property;
  340. }
  341. }
  342. // Concat some data
  343. $listofproperties = array(
  344. 'note_public', 'note_private'
  345. );
  346. foreach ($listofproperties as $property) {
  347. $object->$property = dol_concatdesc($object->$property, $soc_origin->$property);
  348. }
  349. // Merge extrafields
  350. if (is_array($soc_origin->array_options)) {
  351. foreach ($soc_origin->array_options as $key => $val) {
  352. if (empty($object->array_options[$key])) {
  353. $object->array_options[$key] = $val;
  354. }
  355. }
  356. }
  357. // Merge categories
  358. $static_cat = new Categorie($this->db);
  359. $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id');
  360. $object->setCategories($custcats, 'customer');
  361. $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id');
  362. $object->setCategories($suppcats, 'supplier');
  363. // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys.
  364. if ($soc_origin->code_client == $object->code_client
  365. || $soc_origin->code_fournisseur == $object->code_fournisseur
  366. || $soc_origin->barcode == $object->barcode) {
  367. dol_syslog("We clean customer and supplier code so we will be able to make the update of target");
  368. $soc_origin->code_client = '';
  369. $soc_origin->code_fournisseur = '';
  370. $soc_origin->barcode = '';
  371. $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge');
  372. }
  373. // Update
  374. $result = $object->update($object->id, $user, 0, 1, 1, 'merge');
  375. if ($result < 0) {
  376. $error++;
  377. }
  378. // Move links
  379. if (!$error) {
  380. // This list is also into the societe/card.php file
  381. // TODO Mutualise the list into object societe.class.php
  382. $objects = array(
  383. 'Adherent' => '/adherents/class/adherent.class.php',
  384. 'Don' => '/don/class/don.class.php',
  385. 'Societe' => '/societe/class/societe.class.php',
  386. //'Categorie' => '/categories/class/categorie.class.php',
  387. 'ActionComm' => '/comm/action/class/actioncomm.class.php',
  388. 'Propal' => '/comm/propal/class/propal.class.php',
  389. 'Commande' => '/commande/class/commande.class.php',
  390. 'Facture' => '/compta/facture/class/facture.class.php',
  391. 'FactureRec' => '/compta/facture/class/facture-rec.class.php',
  392. 'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php',
  393. 'Mo' => '/mrp/class/mo.class.php',
  394. 'Contact' => '/contact/class/contact.class.php',
  395. 'Contrat' => '/contrat/class/contrat.class.php',
  396. 'Expedition' => '/expedition/class/expedition.class.php',
  397. 'Fichinter' => '/fichinter/class/fichinter.class.php',
  398. 'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php',
  399. 'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php',
  400. 'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php',
  401. 'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php',
  402. 'Delivery' => '/delivery/class/delivery.class.php',
  403. 'Product' => '/product/class/product.class.php',
  404. 'Project' => '/projet/class/project.class.php',
  405. 'Ticket' => '/ticket/class/ticket.class.php',
  406. 'User' => '/user/class/user.class.php',
  407. 'Account' => '/compta/bank/class/account.class.php',
  408. 'ConferenceOrBoothAttendee' => '/eventorganization/class/conferenceorboothattendee.class.php'
  409. );
  410. //First, all core objects must update their tables
  411. foreach ($objects as $object_name => $object_file) {
  412. require_once DOL_DOCUMENT_ROOT.$object_file;
  413. if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
  414. $error++;
  415. //setEventMessages($this->db->lasterror(), null, 'errors');
  416. }
  417. }
  418. }
  419. // External modules should update their ones too
  420. if (!$error) {
  421. $reshook = $hookmanager->executeHooks('replaceThirdparty', array(
  422. 'soc_origin' => $soc_origin->id,
  423. 'soc_dest' => $object->id
  424. ), $soc_dest, $action);
  425. if ($reshook < 0) {
  426. //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  427. $error++;
  428. }
  429. }
  430. if (!$error) {
  431. $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id);
  432. // Call trigger
  433. $result = $object->call_trigger('COMPANY_MODIFY', $user);
  434. if ($result < 0) {
  435. //setEventMessages($object->error, $object->errors, 'errors');
  436. $error++;
  437. }
  438. // End call triggers
  439. }
  440. if (!$error) {
  441. //We finally remove the old thirdparty
  442. if ($soc_origin->delete($soc_origin->id, $user) < 1) {
  443. $error++;
  444. }
  445. }
  446. // End of merge
  447. if ($error) {
  448. $this->db->rollback();
  449. throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.');
  450. } else {
  451. $this->db->commit();
  452. }
  453. return $this->get($id);
  454. }
  455. /**
  456. * Delete thirdparty
  457. *
  458. * @param int $id Thirdparty ID
  459. * @return integer
  460. */
  461. public function delete($id)
  462. {
  463. if (!DolibarrApiAccess::$user->hasRight('societe', 'supprimer')) {
  464. throw new RestException(401);
  465. }
  466. $result = $this->company->fetch($id);
  467. if (!$result) {
  468. throw new RestException(404, 'Thirdparty not found');
  469. }
  470. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  471. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  472. }
  473. $this->company->oldcopy = clone $this->company;
  474. $res = $this->company->delete($id);
  475. if ($res < 0) {
  476. throw new RestException(500, "Can't delete, error occurs");
  477. } elseif ($res == 0) {
  478. throw new RestException(409, "Can't delete, that product is probably used");
  479. }
  480. return array(
  481. 'success' => array(
  482. 'code' => 200,
  483. 'message' => 'Object deleted'
  484. )
  485. );
  486. }
  487. /**
  488. * Set new price level for the given thirdparty
  489. *
  490. * @param int $id ID of thirdparty
  491. * @param int $priceLevel Price level to apply to thirdparty
  492. * @return object Thirdparty data without useless information
  493. *
  494. * @url PUT {id}/setpricelevel
  495. *
  496. * @throws RestException 400 Price level out of bounds
  497. * @throws RestException 401 Access not allowed for your login
  498. * @throws RestException 404 Thirdparty not found
  499. * @throws RestException 500 Error fetching/setting price level
  500. * @throws RestException 501 Request needs modules "Thirdparties" and "Products" and setting Multiprices activated
  501. */
  502. public function setThirdpartyPriceLevel($id, $priceLevel)
  503. {
  504. global $conf;
  505. if (!isModEnabled('societe')) {
  506. throw new RestException(501, 'Module "Thirdparties" needed for this request');
  507. }
  508. if (empty($conf->product->enabled)) {
  509. throw new RestException(501, 'Module "Products" needed for this request');
  510. }
  511. if (empty($conf->global->PRODUIT_MULTIPRICES)) {
  512. throw new RestException(501, 'Multiprices features activation needed for this request');
  513. }
  514. if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
  515. throw new RestException(400, 'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
  516. }
  517. if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
  518. throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
  519. }
  520. $result = $this->company->fetch($id);
  521. if ($result < 0) {
  522. throw new RestException(404, 'Thirdparty '.$id.' not found');
  523. }
  524. if (empty($result)) {
  525. throw new RestException(500, 'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
  526. }
  527. if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) {
  528. throw new RestException(401, 'Access to thirdparty '.$id.' not allowed for login '.DolibarrApiAccess::$user->login);
  529. }
  530. $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
  531. if ($result <= 0) {
  532. throw new RestException(500, 'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
  533. }
  534. return $this->_cleanObjectDatas($this->company);
  535. }
  536. /**
  537. * Get customer categories for a thirdparty
  538. *
  539. * @param int $id ID of thirdparty
  540. * @param string $sortfield Sort field
  541. * @param string $sortorder Sort order
  542. * @param int $limit Limit for list
  543. * @param int $page Page number
  544. *
  545. * @return mixed
  546. *
  547. * @url GET {id}/categories
  548. */
  549. public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  550. {
  551. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  552. throw new RestException(401);
  553. }
  554. $result = $this->company->fetch($id);
  555. if (!$result) {
  556. throw new RestException(404, 'Thirdparty not found');
  557. }
  558. $categories = new Categorie($this->db);
  559. $result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page);
  560. if (is_numeric($result) && $result < 0) {
  561. throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
  562. }
  563. if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
  564. return array();
  565. }
  566. return $result;
  567. }
  568. /**
  569. * Add a customer category to a thirdparty
  570. *
  571. * @param int $id Id of thirdparty
  572. * @param int $category_id Id of category
  573. *
  574. * @return mixed
  575. *
  576. * @url POST {id}/categories/{category_id}
  577. */
  578. public function addCategory($id, $category_id)
  579. {
  580. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  581. throw new RestException(401);
  582. }
  583. $result = $this->company->fetch($id);
  584. if (!$result) {
  585. throw new RestException(404, 'Thirdparty not found');
  586. }
  587. $category = new Categorie($this->db);
  588. $result = $category->fetch($category_id);
  589. if (!$result) {
  590. throw new RestException(404, 'category not found');
  591. }
  592. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  593. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  594. }
  595. if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
  596. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  597. }
  598. $category->add_type($this->company, 'customer');
  599. return $this->_cleanObjectDatas($this->company);
  600. }
  601. /**
  602. * Remove the link between a customer category and the thirdparty
  603. *
  604. * @param int $id Id of thirdparty
  605. * @param int $category_id Id of category
  606. *
  607. * @return mixed
  608. *
  609. * @url DELETE {id}/categories/{category_id}
  610. */
  611. public function deleteCategory($id, $category_id)
  612. {
  613. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  614. throw new RestException(401);
  615. }
  616. $result = $this->company->fetch($id);
  617. if (!$result) {
  618. throw new RestException(404, 'Thirdparty not found');
  619. }
  620. $category = new Categorie($this->db);
  621. $result = $category->fetch($category_id);
  622. if (!$result) {
  623. throw new RestException(404, 'category not found');
  624. }
  625. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  626. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  627. }
  628. if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
  629. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  630. }
  631. $category->del_type($this->company, 'customer');
  632. return $this->_cleanObjectDatas($this->company);
  633. }
  634. /**
  635. * Get supplier categories for a thirdparty
  636. *
  637. * @param int $id ID of thirdparty
  638. * @param string $sortfield Sort field
  639. * @param string $sortorder Sort order
  640. * @param int $limit Limit for list
  641. * @param int $page Page number
  642. *
  643. * @return mixed
  644. *
  645. * @url GET {id}/supplier_categories
  646. */
  647. public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
  648. {
  649. if (!DolibarrApiAccess::$user->rights->categorie->lire) {
  650. throw new RestException(401);
  651. }
  652. $result = $this->company->fetch($id);
  653. if (!$result) {
  654. throw new RestException(404, 'Thirdparty not found');
  655. }
  656. $categories = new Categorie($this->db);
  657. $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page);
  658. if (is_numeric($result) && $result < 0) {
  659. throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
  660. }
  661. if (is_numeric($result) && $result == 0) { // To fix a return of 0 instead of empty array of method getListForItem
  662. return array();
  663. }
  664. return $result;
  665. }
  666. /**
  667. * Add a supplier category to a thirdparty
  668. *
  669. * @param int $id Id of thirdparty
  670. * @param int $category_id Id of category
  671. *
  672. * @return mixed
  673. *
  674. * @url POST {id}/supplier_categories/{category_id}
  675. */
  676. public function addSupplierCategory($id, $category_id)
  677. {
  678. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  679. throw new RestException(401);
  680. }
  681. $result = $this->company->fetch($id);
  682. if (!$result) {
  683. throw new RestException(404, 'Thirdparty not found');
  684. }
  685. $category = new Categorie($this->db);
  686. $result = $category->fetch($category_id);
  687. if (!$result) {
  688. throw new RestException(404, 'category not found');
  689. }
  690. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  691. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  692. }
  693. if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
  694. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  695. }
  696. $category->add_type($this->company, 'supplier');
  697. return $this->_cleanObjectDatas($this->company);
  698. }
  699. /**
  700. * Remove the link between a category and the thirdparty
  701. *
  702. * @param int $id Id of thirdparty
  703. * @param int $category_id Id of category
  704. *
  705. * @return mixed
  706. *
  707. * @url DELETE {id}/supplier_categories/{category_id}
  708. */
  709. public function deleteSupplierCategory($id, $category_id)
  710. {
  711. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  712. throw new RestException(401);
  713. }
  714. $result = $this->company->fetch($id);
  715. if (!$result) {
  716. throw new RestException(404, 'Thirdparty not found');
  717. }
  718. $category = new Categorie($this->db);
  719. $result = $category->fetch($category_id);
  720. if (!$result) {
  721. throw new RestException(404, 'category not found');
  722. }
  723. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  724. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  725. }
  726. if (!DolibarrApi::_checkAccessToResource('category', $category->id)) {
  727. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  728. }
  729. $category->del_type($this->company, 'supplier');
  730. return $this->_cleanObjectDatas($this->company);
  731. }
  732. /**
  733. * Get outstanding proposals of thirdparty
  734. *
  735. * @param int $id ID of the thirdparty
  736. * @param string $mode 'customer' or 'supplier'
  737. *
  738. * @url GET {id}/outstandingproposals
  739. *
  740. * @return array List of outstandings proposals of thirdparty
  741. *
  742. * @throws RestException 400
  743. * @throws RestException 401
  744. * @throws RestException 404
  745. */
  746. public function getOutStandingProposals($id, $mode = 'customer')
  747. {
  748. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  749. throw new RestException(401);
  750. }
  751. if (empty($id)) {
  752. throw new RestException(400, 'Thirdparty ID is mandatory');
  753. }
  754. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  755. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  756. }
  757. $result = $this->company->fetch($id);
  758. if (!$result) {
  759. throw new RestException(404, 'Thirdparty not found');
  760. }
  761. $result = $this->company->getOutstandingProposals($mode);
  762. unset($result['total_ht']);
  763. unset($result['total_ttc']);
  764. return $result;
  765. }
  766. /**
  767. * Get outstanding orders of thirdparty
  768. *
  769. * @param int $id ID of the thirdparty
  770. * @param string $mode 'customer' or 'supplier'
  771. *
  772. * @url GET {id}/outstandingorders
  773. *
  774. * @return array List of outstandings orders of thirdparty
  775. *
  776. * @throws RestException 400
  777. * @throws RestException 401
  778. * @throws RestException 404
  779. */
  780. public function getOutStandingOrder($id, $mode = 'customer')
  781. {
  782. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  783. throw new RestException(401);
  784. }
  785. if (empty($id)) {
  786. throw new RestException(400, 'Thirdparty ID is mandatory');
  787. }
  788. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  789. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  790. }
  791. $result = $this->company->fetch($id);
  792. if (!$result) {
  793. throw new RestException(404, 'Thirdparty not found');
  794. }
  795. $result = $this->company->getOutstandingOrders($mode);
  796. unset($result['total_ht']);
  797. unset($result['total_ttc']);
  798. return $result;
  799. }
  800. /**
  801. * Get outstanding invoices of thirdparty
  802. *
  803. * @param int $id ID of the thirdparty
  804. * @param string $mode 'customer' or 'supplier'
  805. *
  806. * @url GET {id}/outstandinginvoices
  807. *
  808. * @return array List of outstandings invoices of thirdparty
  809. *
  810. * @throws RestException 400
  811. * @throws RestException 401
  812. * @throws RestException 404
  813. */
  814. public function getOutStandingInvoices($id, $mode = 'customer')
  815. {
  816. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  817. throw new RestException(401);
  818. }
  819. if (empty($id)) {
  820. throw new RestException(400, 'Thirdparty ID is mandatory');
  821. }
  822. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  823. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  824. }
  825. $result = $this->company->fetch($id);
  826. if (!$result) {
  827. throw new RestException(404, 'Thirdparty not found');
  828. }
  829. $result = $this->company->getOutstandingBills($mode);
  830. unset($result['total_ht']);
  831. unset($result['total_ttc']);
  832. return $result;
  833. }
  834. /**
  835. * Get representatives of thirdparty
  836. *
  837. * @param int $id ID of the thirdparty
  838. * @param string $mode 0=Array with properties, 1=Array of id.
  839. *
  840. * @url GET {id}/representatives
  841. *
  842. * @return array List of representatives of thirdparty
  843. *
  844. * @throws RestException 400
  845. * @throws RestException 401
  846. * @throws RestException 404
  847. */
  848. public function getSalesRepresentatives($id, $mode = 0)
  849. {
  850. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  851. throw new RestException(401);
  852. }
  853. if (empty($id)) {
  854. throw new RestException(400, 'Thirdparty ID is mandatory');
  855. }
  856. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  857. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  858. }
  859. $result = $this->company->fetch($id);
  860. if (!$result) {
  861. throw new RestException(404, 'Thirdparty not found');
  862. }
  863. $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
  864. return $result;
  865. }
  866. /**
  867. * Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers...)
  868. *
  869. * @param int $id ID of the thirdparty
  870. * @param string $filter Filter exceptional discount. "none" will return every discount, "available" returns unapplied discounts, "used" returns applied discounts {@choice none,available,used}
  871. * @param string $sortfield Sort field
  872. * @param string $sortorder Sort order
  873. *
  874. * @url GET {id}/fixedamountdiscounts
  875. *
  876. * @return array List of fixed discount of thirdparty
  877. *
  878. * @throws RestException 400
  879. * @throws RestException 401
  880. * @throws RestException 404
  881. * @throws RestException 503
  882. */
  883. public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC')
  884. {
  885. $obj_ret = array();
  886. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  887. throw new RestException(401);
  888. }
  889. if (empty($id)) {
  890. throw new RestException(400, 'Thirdparty ID is mandatory');
  891. }
  892. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  893. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  894. }
  895. $result = $this->company->fetch($id);
  896. if (!$result) {
  897. throw new RestException(404, 'Thirdparty not found');
  898. }
  899. $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";
  900. $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f";
  901. $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
  902. if ($filter == "available") {
  903. $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
  904. }
  905. if ($filter == "used") {
  906. $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
  907. }
  908. $sql .= $this->db->order($sortfield, $sortorder);
  909. $result = $this->db->query($sql);
  910. if (!$result) {
  911. throw new RestException(503, $this->db->lasterror());
  912. } else {
  913. $num = $this->db->num_rows($result);
  914. while ($obj = $this->db->fetch_object($result)) {
  915. $obj_ret[] = $obj;
  916. }
  917. }
  918. return $obj_ret;
  919. }
  920. /**
  921. * Return list of invoices qualified to be replaced by another invoice.
  922. *
  923. * @param int $id Id of thirdparty
  924. *
  925. * @url GET {id}/getinvoicesqualifiedforreplacement
  926. *
  927. * @return array
  928. * @throws RestException 400
  929. * @throws RestException 401
  930. * @throws RestException 404
  931. * @throws RestException 405
  932. */
  933. public function getInvoicesQualifiedForReplacement($id)
  934. {
  935. if (!DolibarrApiAccess::$user->rights->facture->lire) {
  936. throw new RestException(401);
  937. }
  938. if (empty($id)) {
  939. throw new RestException(400, 'Thirdparty ID is mandatory');
  940. }
  941. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  942. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  943. }
  944. /*$result = $this->thirdparty->fetch($id);
  945. if( ! $result ) {
  946. throw new RestException(404, 'Thirdparty not found');
  947. }*/
  948. $invoice = new Facture($this->db);
  949. $result = $invoice->list_replacable_invoices($id);
  950. if ($result < 0) {
  951. throw new RestException(405, $invoice->error);
  952. }
  953. return $result;
  954. }
  955. /**
  956. * Return list of invoices qualified to be corrected by a credit note.
  957. * Invoices matching the following rules are returned
  958. * (validated + payment on process) or classified (paid completely or paid partialy) + not already replaced + not already a credit note
  959. *
  960. * @param int $id Id of thirdparty
  961. *
  962. * @url GET {id}/getinvoicesqualifiedforcreditnote
  963. *
  964. * @return array
  965. *
  966. * @throws RestException 400
  967. * @throws RestException 401
  968. * @throws RestException 404
  969. * @throws RestException 405
  970. */
  971. public function getInvoicesQualifiedForCreditNote($id)
  972. {
  973. if (!DolibarrApiAccess::$user->rights->facture->lire) {
  974. throw new RestException(401);
  975. }
  976. if (empty($id)) {
  977. throw new RestException(400, 'Thirdparty ID is mandatory');
  978. }
  979. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  980. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  981. }
  982. /*$result = $this->thirdparty->fetch($id);
  983. if( ! $result ) {
  984. throw new RestException(404, 'Thirdparty not found');
  985. }*/
  986. $invoice = new Facture($this->db);
  987. $result = $invoice->list_qualified_avoir_invoices($id);
  988. if ($result < 0) {
  989. throw new RestException(405, $invoice->error);
  990. }
  991. return $result;
  992. }
  993. /**
  994. * Get CompanyBankAccount objects for thirdparty
  995. *
  996. * @param int $id ID of thirdparty
  997. *
  998. * @return array
  999. *
  1000. * @url GET {id}/bankaccounts
  1001. */
  1002. public function getCompanyBankAccount($id)
  1003. {
  1004. if (!DolibarrApiAccess::$user->rights->societe->lire) {
  1005. throw new RestException(401);
  1006. }
  1007. if (empty($id)) {
  1008. throw new RestException(400, 'Thirdparty ID is mandatory');
  1009. }
  1010. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  1011. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  1012. }
  1013. /**
  1014. * We select all the records that match the socid
  1015. */
  1016. $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
  1017. $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
  1018. $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
  1019. if ($id) {
  1020. $sql .= " WHERE fk_soc = ".((int) $id);
  1021. }
  1022. $result = $this->db->query($sql);
  1023. if ($this->db->num_rows($result) == 0) {
  1024. throw new RestException(404, 'Account not found');
  1025. }
  1026. $i = 0;
  1027. $accounts = array();
  1028. if ($result) {
  1029. $num = $this->db->num_rows($result);
  1030. while ($i < $num) {
  1031. $obj = $this->db->fetch_object($result);
  1032. $account = new CompanyBankAccount($this->db);
  1033. if ($account->fetch($obj->rowid)) {
  1034. $accounts[] = $account;
  1035. }
  1036. $i++;
  1037. }
  1038. } else {
  1039. throw new RestException(404, 'Account not found');
  1040. }
  1041. $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum');
  1042. $returnAccounts = array();
  1043. foreach ($accounts as $account) {
  1044. $object = array();
  1045. foreach ($account as $key => $value) {
  1046. if (in_array($key, $fields)) {
  1047. $object[$key] = $value;
  1048. }
  1049. }
  1050. $returnAccounts[] = $object;
  1051. }
  1052. return $returnAccounts;
  1053. }
  1054. /**
  1055. * Create CompanyBankAccount object for thirdparty
  1056. * @param int $id ID of thirdparty
  1057. * @param array $request_data Request data
  1058. *
  1059. * @return array|mixed BankAccount of thirdparty
  1060. *
  1061. * @url POST {id}/bankaccounts
  1062. */
  1063. public function createCompanyBankAccount($id, $request_data = null)
  1064. {
  1065. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1066. throw new RestException(401);
  1067. }
  1068. if ($this->company->fetch($id) <= 0) {
  1069. throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
  1070. }
  1071. $account = new CompanyBankAccount($this->db);
  1072. $account->socid = $id;
  1073. foreach ($request_data as $field => $value) {
  1074. $account->$field = $value;
  1075. }
  1076. if ($account->create(DolibarrApiAccess::$user) < 0) {
  1077. throw new RestException(500, 'Error creating Company Bank account');
  1078. }
  1079. if (empty($account->rum)) {
  1080. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  1081. $prelevement = new BonPrelevement($this->db);
  1082. $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
  1083. $account->date_rum = dol_now();
  1084. }
  1085. if ($account->update(DolibarrApiAccess::$user) < 0) {
  1086. throw new RestException(500, 'Error updating values');
  1087. }
  1088. return $this->_cleanObjectDatas($account);
  1089. }
  1090. /**
  1091. * Update CompanyBankAccount object for thirdparty
  1092. *
  1093. * @param int $id ID of thirdparty
  1094. * @param int $bankaccount_id ID of CompanyBankAccount
  1095. * @param array $request_data Request data
  1096. *
  1097. * @return array|mixed BankAccount of thirdparty
  1098. *
  1099. * @url PUT {id}/bankaccounts/{bankaccount_id}
  1100. */
  1101. public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null)
  1102. {
  1103. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1104. throw new RestException(401);
  1105. }
  1106. if ($this->company->fetch($id) <= 0) {
  1107. throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists');
  1108. }
  1109. $account = new CompanyBankAccount($this->db);
  1110. $account->fetch($bankaccount_id, $id, -1, '');
  1111. if ($account->socid != $id) {
  1112. throw new RestException(401);
  1113. }
  1114. foreach ($request_data as $field => $value) {
  1115. $account->$field = $value;
  1116. }
  1117. if (empty($account->rum)) {
  1118. require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
  1119. $prelevement = new BonPrelevement($this->db);
  1120. $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
  1121. $account->date_rum = dol_now();
  1122. }
  1123. if ($account->update(DolibarrApiAccess::$user) < 0) {
  1124. throw new RestException(500, 'Error updating values');
  1125. }
  1126. return $this->_cleanObjectDatas($account);
  1127. }
  1128. /**
  1129. * Delete a bank account attached to a thirdparty
  1130. *
  1131. * @param int $id ID of thirdparty
  1132. * @param int $bankaccount_id ID of CompanyBankAccount
  1133. *
  1134. * @return int -1 if error 1 if correct deletion
  1135. *
  1136. * @url DELETE {id}/bankaccounts/{bankaccount_id}
  1137. */
  1138. public function deleteCompanyBankAccount($id, $bankaccount_id)
  1139. {
  1140. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1141. throw new RestException(401);
  1142. }
  1143. $account = new CompanyBankAccount($this->db);
  1144. $account->fetch($bankaccount_id);
  1145. if (!$account->socid == $id) {
  1146. throw new RestException(401);
  1147. }
  1148. return $account->delete(DolibarrApiAccess::$user);
  1149. }
  1150. /**
  1151. * Generate a Document from a bank account record (like SEPA mandate)
  1152. *
  1153. * @param int $id Thirdparty id
  1154. * @param int $companybankid Companybank id
  1155. * @param string $model Model of document to generate
  1156. * @return void
  1157. *
  1158. * @url GET {id}/generateBankAccountDocument/{companybankid}/{model}
  1159. */
  1160. public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate')
  1161. {
  1162. global $conf, $langs;
  1163. $langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals"));
  1164. if ($this->company->fetch($id) <= 0) {
  1165. throw new RestException(404, 'Thirdparty not found');
  1166. }
  1167. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1168. throw new RestException(401);
  1169. }
  1170. $this->company->setDocModel(DolibarrApiAccess::$user, $model);
  1171. $this->company->fk_bank = $this->company->fk_account;
  1172. $outputlangs = $langs;
  1173. $newlang = '';
  1174. //if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
  1175. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  1176. if (isset($this->company->thirdparty->default_lang)) {
  1177. $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ...
  1178. } elseif (isset($this->company->default_lang)) {
  1179. $newlang = $this->company->default_lang; // for thirdparty
  1180. }
  1181. }
  1182. if (!empty($newlang)) {
  1183. $outputlangs = new Translate("", $conf);
  1184. $outputlangs->setDefaultLang($newlang);
  1185. }
  1186. $sql = "SELECT rowid";
  1187. $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib";
  1188. if ($id) {
  1189. $sql .= " WHERE fk_soc = ".((int) $id);
  1190. }
  1191. if ($companybankid) {
  1192. $sql .= " AND rowid = ".((int) $companybankid);
  1193. }
  1194. $i = 0;
  1195. $accounts = array();
  1196. $result = $this->db->query($sql);
  1197. if ($result) {
  1198. if ($this->db->num_rows($result) == 0) {
  1199. throw new RestException(404, 'Bank account not found');
  1200. }
  1201. $num = $this->db->num_rows($result);
  1202. while ($i < $num) {
  1203. $obj = $this->db->fetch_object($result);
  1204. $account = new CompanyBankAccount($this->db);
  1205. if ($account->fetch($obj->rowid)) {
  1206. $accounts[] = $account;
  1207. }
  1208. $i++;
  1209. }
  1210. } else {
  1211. throw new RestException(500, 'Sql error '.$this->db->lasterror());
  1212. }
  1213. $moreparams = array(
  1214. 'use_companybankid' => $accounts[0]->id,
  1215. 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id)
  1216. );
  1217. $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
  1218. if ($result > 0) {
  1219. return array("success" => $result);
  1220. } else {
  1221. throw new RestException(500, 'Error generating the document '.$this->company->error);
  1222. }
  1223. }
  1224. /**
  1225. * Get a specific gateway attached to a thirdparty (by specifying the site key)
  1226. *
  1227. * @param int $id ID of thirdparty
  1228. * @param string $site Site key
  1229. *
  1230. * @return array|mixed
  1231. * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
  1232. * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
  1233. *
  1234. * @url GET {id}/gateways/
  1235. */
  1236. public function getSocieteAccounts($id, $site = null)
  1237. {
  1238. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  1239. throw new RestException(401);
  1240. }
  1241. if (!DolibarrApi::_checkAccessToResource('societe', $id)) {
  1242. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  1243. }
  1244. /**
  1245. * We select all the records that match the socid
  1246. */
  1247. $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
  1248. $sql .= " WHERE fk_soc = ".((int) $id);
  1249. if ($site) {
  1250. $sql .= " AND site ='".$this->db->escape($site)."'";
  1251. }
  1252. $result = $this->db->query($sql);
  1253. if ($result && $this->db->num_rows($result) == 0) {
  1254. throw new RestException(404, 'This thirdparty does not have any gateway attached or does not exist.');
  1255. }
  1256. $i = 0;
  1257. $accounts = array();
  1258. $num = $this->db->num_rows($result);
  1259. while ($i < $num) {
  1260. $obj = $this->db->fetch_object($result);
  1261. $account = new SocieteAccount($this->db);
  1262. if ($account->fetch($obj->rowid)) {
  1263. $accounts[] = $account;
  1264. }
  1265. $i++;
  1266. }
  1267. $fields = array('id', 'fk_soc', 'key_account', 'site', 'date_creation', 'tms');
  1268. $returnAccounts = array();
  1269. foreach ($accounts as $account) {
  1270. $object = array();
  1271. foreach ($account as $key => $value) {
  1272. if (in_array($key, $fields)) {
  1273. $object[$key] = $value;
  1274. }
  1275. }
  1276. $returnAccounts[] = $object;
  1277. }
  1278. return $returnAccounts;
  1279. }
  1280. /**
  1281. * Create and attach a new gateway to an existing thirdparty
  1282. *
  1283. * Possible fields for request_data (request body) are specified in <code>llx_societe_account</code> table.<br>
  1284. * See <a href="https://wiki.dolibarr.org/index.php/Table_llx_societe_account">Table llx_societe_account</a> wiki page for more information<br><br>
  1285. * <u>Example body payload :</u> <pre>{"key_account": "cus_DAVkLSs1LYyYI", "site": "stripe"}</pre>
  1286. *
  1287. * @param int $id ID of thirdparty
  1288. * @param array $request_data Request data
  1289. *
  1290. * @return array|mixed
  1291. *
  1292. * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
  1293. * @throws RestException 409 Conflict: A SocieteAccount entity (gateway) already exists for this company and site.
  1294. * @throws RestException 422 Unprocessable Entity: You must pass the site attribute in your request data !
  1295. * @throws RestException 500 Internal Server Error: Error creating SocieteAccount account
  1296. *
  1297. * @url POST {id}/gateways
  1298. */
  1299. public function createSocieteAccount($id, $request_data = null)
  1300. {
  1301. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1302. throw new RestException(401);
  1303. }
  1304. if (!isset($request_data['site'])) {
  1305. throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !');
  1306. }
  1307. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."'";
  1308. $result = $this->db->query($sql);
  1309. if ($result && $this->db->num_rows($result) == 0) {
  1310. $account = new SocieteAccount($this->db);
  1311. if (!isset($request_data['login'])) {
  1312. $account->login = "";
  1313. }
  1314. $account->fk_soc = $id;
  1315. foreach ($request_data as $field => $value) {
  1316. $account->$field = $value;
  1317. }
  1318. if ($account->create(DolibarrApiAccess::$user) < 0) {
  1319. throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
  1320. }
  1321. $this->_cleanObjectDatas($account);
  1322. return $account;
  1323. } else {
  1324. throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.');
  1325. }
  1326. }
  1327. /**
  1328. * Create and attach a new (or replace an existing) specific site gateway to a thirdparty
  1329. *
  1330. * You <strong>MUST</strong> pass all values to keep (otherwise, they will be deleted) !<br>
  1331. * If you just need to update specific fields prefer <code>PATCH /thirdparties/{id}/gateways/{site}</code> endpoint.<br><br>
  1332. * When a <strong>SocieteAccount</strong> entity does not exist for the <code>id</code> and <code>site</code>
  1333. * supplied, a new one will be created. In that case <code>fk_soc</code> and <code>site</code> members form
  1334. * request body payload will be ignored and <code>id</code> and <code>site</code> query strings parameters
  1335. * will be used instead.
  1336. *
  1337. * @param int $id ID of thirdparty
  1338. * @param string $site Site key
  1339. * @param array $request_data Request data
  1340. *
  1341. * @return array|mixed
  1342. *
  1343. * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
  1344. * @throws RestException 422 Unprocessable Entity: You must pass the site attribute in your request data !
  1345. * @throws RestException 500 Internal Server Error: Error updating SocieteAccount entity
  1346. *
  1347. * @url PUT {id}/gateways/{site}
  1348. */
  1349. public function putSocieteAccount($id, $site, $request_data = null)
  1350. {
  1351. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1352. throw new RestException(401);
  1353. }
  1354. $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
  1355. $result = $this->db->query($sql);
  1356. // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one.
  1357. if ($result && $this->db->num_rows == 0) {
  1358. if (!isset($request_data['key_account'])) {
  1359. throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !');
  1360. }
  1361. $account = new SocieteAccount($this->db);
  1362. if (!isset($request_data['login'])) {
  1363. $account->login = "";
  1364. }
  1365. foreach ($request_data as $field => $value) {
  1366. $account->$field = $value;
  1367. }
  1368. $account->fk_soc = $id;
  1369. $account->site = $site;
  1370. if ($account->create(DolibarrApiAccess::$user) < 0) {
  1371. throw new RestException(500, 'Error creating SocieteAccount entity.');
  1372. }
  1373. // We found an existing SocieteAccount entity, we are replacing it
  1374. } else {
  1375. if (isset($request_data['site']) && $request_data['site'] !== $site) {
  1376. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
  1377. $result = $this->db->query($sql);
  1378. if ($result && $this->db->num_rows($result) !== 0) {
  1379. 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.");
  1380. }
  1381. }
  1382. $obj = $this->db->fetch_object($result);
  1383. $account = new SocieteAccount($this->db);
  1384. $account->id = $obj->rowid;
  1385. $account->fk_soc = $id;
  1386. $account->site = $site;
  1387. if (!isset($request_data['login'])) {
  1388. $account->login = "";
  1389. }
  1390. $account->fk_user_creat = $obj->fk_user_creat;
  1391. $account->date_creation = $obj->date_creation;
  1392. foreach ($request_data as $field => $value) {
  1393. $account->$field = $value;
  1394. }
  1395. if ($account->update(DolibarrApiAccess::$user) < 0) {
  1396. throw new RestException(500, 'Error updating SocieteAccount entity.');
  1397. }
  1398. }
  1399. $this->_cleanObjectDatas($account);
  1400. return $account;
  1401. }
  1402. /**
  1403. * Update specified values of a specific gateway attached to a thirdparty
  1404. *
  1405. * @param int $id Id of thirdparty
  1406. * @param string $site Site key
  1407. * @param array $request_data Request data
  1408. *
  1409. * @return array|mixed
  1410. *
  1411. * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties
  1412. * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
  1413. * @throws RestException 409 Conflict: Another SocieteAccount entity already exists for this thirdparty with this site key.
  1414. * @throws RestException 500 Internal Server Error: Error updating SocieteAccount entity
  1415. *
  1416. * @url PATCH {id}/gateways/{site}
  1417. */
  1418. public function patchSocieteAccount($id, $site, $request_data = null)
  1419. {
  1420. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1421. throw new RestException(401);
  1422. }
  1423. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($site)."'";
  1424. $result = $this->db->query($sql);
  1425. if ($result && $this->db->num_rows($result) == 0) {
  1426. throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist.");
  1427. } else {
  1428. // If the user tries to edit the site member, we check first if
  1429. if (isset($request_data['site']) && $request_data['site'] !== $site) {
  1430. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id)." AND site = '".$this->db->escape($request_data['site'])."' ";
  1431. $result = $this->db->query($sql);
  1432. if ($result && $this->db->num_rows($result) !== 0) {
  1433. 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.");
  1434. }
  1435. }
  1436. $obj = $this->db->fetch_object($result);
  1437. $account = new SocieteAccount($this->db);
  1438. $account->fetch($obj->rowid);
  1439. foreach ($request_data as $field => $value) {
  1440. $account->$field = $value;
  1441. }
  1442. if ($account->update(DolibarrApiAccess::$user) < 0) {
  1443. throw new RestException(500, 'Error updating SocieteAccount account');
  1444. }
  1445. $this->_cleanObjectDatas($account);
  1446. return $account;
  1447. }
  1448. }
  1449. /**
  1450. * Delete a specific site gateway attached to a thirdparty (by gateway id)
  1451. *
  1452. * @param int $id ID of thirdparty
  1453. * @param int $site Site key
  1454. *
  1455. * @return void
  1456. * @throws RestException 401 Unauthorized: User does not have permission to delete thirdparties gateways
  1457. * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
  1458. * @throws RestException 500 Internal Server Error: Error deleting SocieteAccount entity
  1459. *
  1460. * @url DELETE {id}/gateways/{site}
  1461. */
  1462. public function deleteSocieteAccount($id, $site)
  1463. {
  1464. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1465. throw new RestException(401);
  1466. }
  1467. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site)."'";
  1468. $result = $this->db->query($sql);
  1469. if ($result && $this->db->num_rows($result) == 0) {
  1470. throw new RestException(404);
  1471. } else {
  1472. $obj = $this->db->fetch_object($result);
  1473. $account = new SocieteAccount($this->db);
  1474. $account->fetch($obj->rowid);
  1475. if ($account->delete(DolibarrApiAccess::$user) < 0) {
  1476. throw new RestException(500, "Error while deleting $site gateway attached to this third party");
  1477. }
  1478. }
  1479. }
  1480. /**
  1481. * Delete all gateways attached to a thirdparty
  1482. *
  1483. * @param int $id ID of thirdparty
  1484. *
  1485. * @return void
  1486. * @throws RestException 401 Unauthorized: User does not have permission to delete thirdparties gateways
  1487. * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty
  1488. * @throws RestException 500 Internal Server Error: Error deleting SocieteAccount entity
  1489. *
  1490. * @url DELETE {id}/gateways
  1491. */
  1492. public function deleteSocieteAccounts($id)
  1493. {
  1494. if (!DolibarrApiAccess::$user->rights->societe->creer) {
  1495. throw new RestException(401);
  1496. }
  1497. /**
  1498. * We select all the records that match the socid
  1499. */
  1500. $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms";
  1501. $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".((int) $id);
  1502. $result = $this->db->query($sql);
  1503. if ($result && $this->db->num_rows($result) == 0) {
  1504. throw new RestException(404, 'This third party does not have any gateway attached or does not exist.');
  1505. } else {
  1506. $i = 0;
  1507. $num = $this->db->num_rows($result);
  1508. while ($i < $num) {
  1509. $obj = $this->db->fetch_object($result);
  1510. $account = new SocieteAccount($this->db);
  1511. $account->fetch($obj->rowid);
  1512. if ($account->delete(DolibarrApiAccess::$user) < 0) {
  1513. throw new RestException(500, 'Error while deleting gateways attached to this third party');
  1514. }
  1515. $i++;
  1516. }
  1517. }
  1518. }
  1519. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  1520. /**
  1521. * Clean sensible object datas
  1522. *
  1523. * @param Object $object Object to clean
  1524. * @return array|mixed Object with cleaned properties
  1525. */
  1526. protected function _cleanObjectDatas($object)
  1527. {
  1528. // phpcs:enable
  1529. $object = parent::_cleanObjectDatas($object);
  1530. unset($object->nom); // ->name already defined and nom deprecated
  1531. unset($object->name_bis); // ->name_alias already defined
  1532. unset($object->note); // ->note_private and note_public already defined
  1533. unset($object->departement);
  1534. unset($object->departement_code);
  1535. unset($object->pays);
  1536. unset($object->particulier);
  1537. unset($object->prefix_comm);
  1538. unset($object->siren);
  1539. unset($object->siret);
  1540. unset($object->ape);
  1541. 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.
  1542. unset($object->total_ht);
  1543. unset($object->total_tva);
  1544. unset($object->total_localtax1);
  1545. unset($object->total_localtax2);
  1546. unset($object->total_ttc);
  1547. unset($object->lines);
  1548. unset($object->thirdparty);
  1549. unset($object->fk_delivery_address); // deprecated feature
  1550. unset($object->skype);
  1551. unset($object->twitter);
  1552. unset($object->facebook);
  1553. unset($object->linkedin);
  1554. unset($object->instagram);
  1555. unset($object->snapchat);
  1556. unset($object->googleplus);
  1557. unset($object->youtube);
  1558. unset($object->whatsapp);
  1559. return $object;
  1560. }
  1561. /**
  1562. * Validate fields before create or update object
  1563. *
  1564. * @param array $data Datas to validate
  1565. * @return array
  1566. *
  1567. * @throws RestException
  1568. */
  1569. private function _validate($data)
  1570. {
  1571. $thirdparty = array();
  1572. foreach (Thirdparties::$FIELDS as $field) {
  1573. if (!isset($data[$field])) {
  1574. throw new RestException(400, "$field field missing");
  1575. }
  1576. $thirdparty[$field] = $data[$field];
  1577. }
  1578. return $thirdparty;
  1579. }
  1580. /**
  1581. * Fetch properties of a thirdparty object.
  1582. *
  1583. * Return an array with thirdparty informations
  1584. *
  1585. * @param int $rowid Id of third party to load (Use 0 to get a specimen record, use null to use other search criterias)
  1586. * @param string $ref Reference of third party, name (Warning, this can return several records)
  1587. * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr)
  1588. * @param string $barcode Barcode of third party to load
  1589. * @param string $idprof1 Prof id 1 of third party (Warning, this can return several records)
  1590. * @param string $idprof2 Prof id 2 of third party (Warning, this can return several records)
  1591. * @param string $idprof3 Prof id 3 of third party (Warning, this can return several records)
  1592. * @param string $idprof4 Prof id 4 of third party (Warning, this can return several records)
  1593. * @param string $idprof5 Prof id 5 of third party (Warning, this can return several records)
  1594. * @param string $idprof6 Prof id 6 of third party (Warning, this can return several records)
  1595. * @param string $email Email of third party (Warning, this can return several records)
  1596. * @param string $ref_alias Name_alias of third party (Warning, this can return several records)
  1597. * @return array|mixed cleaned Societe object
  1598. *
  1599. * @throws RestException
  1600. */
  1601. private function _fetch($rowid, $ref = '', $ref_ext = '', $barcode = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '')
  1602. {
  1603. global $conf;
  1604. if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
  1605. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.'. No read permission on thirdparties.');
  1606. }
  1607. if ($rowid === 0) {
  1608. $result = $this->company->initAsSpecimen();
  1609. } else {
  1610. $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
  1611. }
  1612. if (!$result) {
  1613. throw new RestException(404, 'Thirdparty not found');
  1614. }
  1615. if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) {
  1616. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login.' on this thirdparty');
  1617. }
  1618. if (isModEnabled('mailing')) {
  1619. $this->company->getNoEmail();
  1620. }
  1621. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  1622. $filterabsolutediscount = "fk_facture_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  1623. $filtercreditnote = "fk_facture_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
  1624. } else {
  1625. $filterabsolutediscount = "fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
  1626. $filtercreditnote = "fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
  1627. }
  1628. $absolute_discount = $this->company->getAvailableDiscounts('', $filterabsolutediscount);
  1629. $absolute_creditnote = $this->company->getAvailableDiscounts('', $filtercreditnote);
  1630. $this->company->absolute_discount = price2num($absolute_discount, 'MT');
  1631. $this->company->absolute_creditnote = price2num($absolute_creditnote, 'MT');
  1632. return $this->_cleanObjectDatas($this->company);
  1633. }
  1634. }