api_thirdparties.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. /**
  19. * API class for thirdparties
  20. *
  21. * @access protected
  22. * @class DolibarrApiAccess {@requires user,external}
  23. *
  24. */
  25. class Thirdparties extends DolibarrApi
  26. {
  27. /**
  28. *
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. static $FIELDS = array(
  32. 'name'
  33. );
  34. /**
  35. * @var Societe $company {@type Societe}
  36. */
  37. public $company;
  38. /**
  39. * Constructor
  40. */
  41. function __construct()
  42. {
  43. global $db, $conf;
  44. $this->db = $db;
  45. $this->company = new Societe($this->db);
  46. if (! empty($conf->global->SOCIETE_MAIL_REQUIRED)) {
  47. static::$FIELDS[] = 'email';
  48. }
  49. }
  50. /**
  51. * Get properties of a thirdparty object
  52. *
  53. * Return an array with thirdparty informations
  54. *
  55. * @param int $id ID of thirdparty
  56. * @return array|mixed data without useless information
  57. *
  58. * @throws RestException
  59. */
  60. function get($id)
  61. {
  62. if(! DolibarrApiAccess::$user->rights->societe->lire) {
  63. throw new RestException(401);
  64. }
  65. $result = $this->company->fetch($id);
  66. if( ! $result ) {
  67. throw new RestException(404, 'Thirdparty not found');
  68. }
  69. if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
  70. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  71. }
  72. return $this->_cleanObjectDatas($this->company);
  73. }
  74. /**
  75. * List thirdparties
  76. *
  77. * Get a list of thirdparties
  78. *
  79. * @param string $sortfield Sort field
  80. * @param string $sortorder Sort order
  81. * @param int $limit Limit for list
  82. * @param int $page Page number
  83. * @param int $mode Set to 1 to show only customers
  84. * Set to 2 to show only prospects
  85. * Set to 3 to show only those are not customer neither prospect
  86. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  87. * @return array Array of thirdparty objects
  88. */
  89. function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $mode=0, $sqlfilters = '') {
  90. global $db, $conf;
  91. $obj_ret = array();
  92. // case of external user, we force socids
  93. $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
  94. // If the internal user must only see his customers, force searching by him
  95. $search_sale = 0;
  96. if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
  97. $sql = "SELECT t.rowid";
  98. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  99. $sql.= " FROM ".MAIN_DB_PREFIX."societe as t";
  100. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  101. $sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st";
  102. $sql.= " WHERE t.fk_stcomm = st.id";
  103. if ($mode == 1) $sql.= " AND t.client IN (1, 3)";
  104. if ($mode == 2) $sql.= " AND t.client IN (2, 3)";
  105. if ($mode == 3) $sql.= " AND t.client IN (0)";
  106. $sql.= ' AND t.entity IN ('.getEntity('societe', 1).')';
  107. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc";
  108. //if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
  109. if ($socid) $sql.= " AND t.rowid IN (".$socids.")";
  110. if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  111. // Insert sale filter
  112. if ($search_sale > 0)
  113. {
  114. $sql .= " AND sc.fk_user = ".$search_sale;
  115. }
  116. // Add sql filters
  117. if ($sqlfilters)
  118. {
  119. if (! DolibarrApi::_checkFilters($sqlfilters))
  120. {
  121. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  122. }
  123. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  124. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  125. }
  126. $sql.= $db->order($sortfield, $sortorder);
  127. if ($limit) {
  128. if ($page < 0)
  129. {
  130. $page = 0;
  131. }
  132. $offset = $limit * $page;
  133. $sql.= $db->plimit($limit + 1, $offset);
  134. }
  135. $result = $db->query($sql);
  136. if ($result)
  137. {
  138. $num = $db->num_rows($result);
  139. $min = min($num, ($limit <= 0 ? $num : $limit));
  140. while ($i < $min)
  141. {
  142. $obj = $db->fetch_object($result);
  143. $soc_static = new Societe($db);
  144. if($soc_static->fetch($obj->rowid)) {
  145. $obj_ret[] = $this->_cleanObjectDatas($soc_static);
  146. }
  147. $i++;
  148. }
  149. }
  150. else {
  151. throw new RestException(503, 'Error when retrieve thirdparties : '.$db->lasterror());
  152. }
  153. if( ! count($obj_ret)) {
  154. throw new RestException(404, 'Thirdparties not found');
  155. }
  156. return $obj_ret;
  157. }
  158. /**
  159. * Create thirdparty object
  160. *
  161. * @param array $request_data Request datas
  162. * @return int ID of thirdparty
  163. */
  164. function post($request_data = NULL)
  165. {
  166. if(! DolibarrApiAccess::$user->rights->societe->creer) {
  167. throw new RestException(401);
  168. }
  169. // Check mandatory fields
  170. $result = $this->_validate($request_data);
  171. foreach($request_data as $field => $value) {
  172. $this->company->$field = $value;
  173. }
  174. if ($this->company->create(DolibarrApiAccess::$user) < 0)
  175. throw new RestException(500, 'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
  176. return $this->company->id;
  177. }
  178. /**
  179. * Update thirdparty
  180. *
  181. * @param int $id Id of thirdparty to update
  182. * @param array $request_data Datas
  183. * @return int
  184. */
  185. function put($id, $request_data = NULL)
  186. {
  187. if(! DolibarrApiAccess::$user->rights->societe->creer) {
  188. throw new RestException(401);
  189. }
  190. $result = $this->company->fetch($id);
  191. if( ! $result ) {
  192. throw new RestException(404, 'Thirdparty not found');
  193. }
  194. if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
  195. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  196. }
  197. foreach($request_data as $field => $value) {
  198. if ($field == 'id') continue;
  199. $this->company->$field = $value;
  200. }
  201. if($this->company->update($id, DolibarrApiAccess::$user,1,'','','update'))
  202. return $this->get ($id);
  203. return false;
  204. }
  205. /**
  206. * Delete thirdparty
  207. *
  208. * @param int $id Thirparty ID
  209. * @return integer
  210. */
  211. function delete($id)
  212. {
  213. if(! DolibarrApiAccess::$user->rights->societe->supprimer) {
  214. throw new RestException(401);
  215. }
  216. $result = $this->company->fetch($id);
  217. if( ! $result ) {
  218. throw new RestException(404, 'Thirdparty not found');
  219. }
  220. if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
  221. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  222. }
  223. return $this->company->delete($id);
  224. }
  225. /**
  226. * Get categories for a thirdparty
  227. *
  228. * @param int $id ID of thirdparty
  229. * @param string $sortfield Sort field
  230. * @param string $sortorder Sort order
  231. * @param int $limit Limit for list
  232. * @param int $page Page number
  233. *
  234. * @return mixed
  235. *
  236. * @url GET {id}/categories
  237. */
  238. function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
  239. $categories = new Categories();
  240. return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'customer', $id);
  241. }
  242. /**
  243. * Add category to a thirdparty
  244. *
  245. * @param int $id Id of thirdparty
  246. * @param array $request_data Request datas
  247. *
  248. * @return mixed
  249. *
  250. * @url POST {id}/addCategory
  251. */
  252. function addCategory($id, $request_data = NULL) {
  253. if (!isset($request_data["category_id"]))
  254. throw new RestException(400, "category_id field missing");
  255. $category_id = $request_data["category_id"];
  256. if(! DolibarrApiAccess::$user->rights->societe->creer) {
  257. throw new RestException(401);
  258. }
  259. $result = $this->company->fetch($id);
  260. if( ! $result ) {
  261. throw new RestException(404, 'Thirdparty not found');
  262. }
  263. $category = new Categorie($this->db);
  264. $result = $category->fetch($category_id);
  265. if( ! $result ) {
  266. throw new RestException(404, 'category not found');
  267. }
  268. if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
  269. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  270. }
  271. if( ! DolibarrApi::_checkAccessToResource('category',$category->id)) {
  272. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  273. }
  274. $category->add_type($this->company,'customer');
  275. return $this->company;
  276. }
  277. /**
  278. * Clean sensible object datas
  279. *
  280. * @param object $object Object to clean
  281. * @return array Array of cleaned object properties
  282. */
  283. function _cleanObjectDatas($object) {
  284. $object = parent::_cleanObjectDatas($object);
  285. unset($object->total_ht);
  286. unset($object->total_tva);
  287. unset($object->total_localtax1);
  288. unset($object->total_localtax2);
  289. unset($object->total_ttc);
  290. return $object;
  291. }
  292. /**
  293. * Validate fields before create or update object
  294. *
  295. * @param array $data Datas to validate
  296. * @return array
  297. *
  298. * @throws RestException
  299. */
  300. function _validate($data)
  301. {
  302. $thirdparty = array();
  303. foreach (Thirdparties::$FIELDS as $field) {
  304. if (!isset($data[$field]))
  305. throw new RestException(400, "$field field missing");
  306. $thirdparty[$field] = $data[$field];
  307. }
  308. return $thirdparty;
  309. }
  310. }