api_thirdparties.class.php 60 KB

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