api_partnership.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2022 Alice Adminson <aadminson@example.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. dol_include_once('/partnership/class/partnership.class.php');
  20. /**
  21. * \file partnership/class/api_partnership.class.php
  22. * \ingroup partnership
  23. * \brief File for API management of partnership.
  24. */
  25. /**
  26. * API class for partnership partnership
  27. *
  28. * @access protected
  29. * @class DolibarrApiAccess {@requires user,external}
  30. */
  31. class PartnershipApi extends DolibarrApi
  32. {
  33. /**
  34. * @var Partnership $partnership {@type Partnership}
  35. */
  36. public $partnership;
  37. /**
  38. * Constructor
  39. *
  40. * @url GET /
  41. *
  42. */
  43. public function __construct()
  44. {
  45. global $db;
  46. $this->db = $db;
  47. $this->partnership = new Partnership($this->db);
  48. }
  49. /**
  50. * Get properties of a partnership object
  51. *
  52. * Return an array with partnership informations
  53. *
  54. * @param int $id ID of partnership
  55. * @return Object Object with cleaned properties
  56. *
  57. * @url GET partnerships/{id}
  58. *
  59. * @throws RestException 401 Not allowed
  60. * @throws RestException 404 Not found
  61. */
  62. public function get($id)
  63. {
  64. if (!DolibarrApiAccess::$user->rights->partnership->read) {
  65. throw new RestException(401);
  66. }
  67. $result = $this->partnership->fetch($id);
  68. if (!$result) {
  69. throw new RestException(404, 'Partnership not found');
  70. }
  71. if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
  72. throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  73. }
  74. return $this->_cleanObjectDatas($this->partnership);
  75. }
  76. /**
  77. * List partnerships
  78. *
  79. * Get a list of partnerships
  80. *
  81. * @param string $sortfield Sort field
  82. * @param string $sortorder Sort order
  83. * @param int $limit Limit for list
  84. * @param int $page Page number
  85. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  86. * @return array Array of order objects
  87. *
  88. * @throws RestException
  89. *
  90. * @url GET /partnerships/
  91. */
  92. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
  93. {
  94. global $db, $conf;
  95. $obj_ret = array();
  96. $tmpobject = new Partnership($this->db);
  97. if (!DolibarrApiAccess::$user->rights->partnership->read) {
  98. throw new RestException(401);
  99. }
  100. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  101. $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
  102. // If the internal user must only see his customers, force searching by him
  103. $search_sale = 0;
  104. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  105. $search_sale = DolibarrApiAccess::$user->id;
  106. }
  107. $sql = "SELECT t.rowid";
  108. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  109. $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)
  110. }
  111. $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  112. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  113. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  114. }
  115. $sql .= " WHERE 1 = 1";
  116. // Example of use $mode
  117. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  118. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  119. if ($tmpobject->ismultientitymanaged) {
  120. $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
  121. }
  122. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  123. $sql .= " AND t.fk_soc = sc.fk_soc";
  124. }
  125. if ($restrictonsocid && $socid) {
  126. $sql .= " AND t.fk_soc = ".((int) $socid);
  127. }
  128. if ($restrictonsocid && $search_sale > 0) {
  129. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  130. }
  131. // Insert sale filter
  132. if ($restrictonsocid && $search_sale > 0) {
  133. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  134. }
  135. if ($sqlfilters) {
  136. $errormessage = '';
  137. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  138. if ($errormessage) {
  139. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  140. }
  141. }
  142. $sql .= $this->db->order($sortfield, $sortorder);
  143. if ($limit) {
  144. if ($page < 0) {
  145. $page = 0;
  146. }
  147. $offset = $limit * $page;
  148. $sql .= $this->db->plimit($limit + 1, $offset);
  149. }
  150. $result = $this->db->query($sql);
  151. $i = 0;
  152. if ($result) {
  153. $num = $this->db->num_rows($result);
  154. while ($i < $num) {
  155. $obj = $this->db->fetch_object($result);
  156. $tmp_object = new Partnership($this->db);
  157. if ($tmp_object->fetch($obj->rowid)) {
  158. $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
  159. }
  160. $i++;
  161. }
  162. } else {
  163. throw new RestException(503, 'Error when retrieving partnership list: '.$this->db->lasterror());
  164. }
  165. if (!count($obj_ret)) {
  166. throw new RestException(404, 'No partnership found');
  167. }
  168. return $obj_ret;
  169. }
  170. /**
  171. * Create partnership object
  172. *
  173. * @param array $request_data Request datas
  174. * @return int ID of partnership
  175. *
  176. * @throws RestException
  177. *
  178. * @url POST partnerships/
  179. */
  180. public function post($request_data = null)
  181. {
  182. if (!DolibarrApiAccess::$user->rights->partnership->write) {
  183. throw new RestException(401);
  184. }
  185. // Check mandatory fields
  186. $result = $this->_validate($request_data);
  187. foreach ($request_data as $field => $value) {
  188. $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
  189. }
  190. // Clean data
  191. // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
  192. if ($this->partnership->create(DolibarrApiAccess::$user)<0) {
  193. throw new RestException(500, "Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
  194. }
  195. return $this->partnership->id;
  196. }
  197. /**
  198. * Update partnership
  199. *
  200. * @param int $id Id of partnership to update
  201. * @param array $request_data Datas
  202. * @return int
  203. *
  204. * @throws RestException
  205. *
  206. * @url PUT partnerships/{id}
  207. */
  208. public function put($id, $request_data = null)
  209. {
  210. if (!DolibarrApiAccess::$user->rights->partnership->write) {
  211. throw new RestException(401);
  212. }
  213. $result = $this->partnership->fetch($id);
  214. if (!$result) {
  215. throw new RestException(404, 'Partnership not found');
  216. }
  217. if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
  218. throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  219. }
  220. foreach ($request_data as $field => $value) {
  221. if ($field == 'id') {
  222. continue;
  223. }
  224. $this->partnership->$field = $this->_checkValForAPI($field, $value, $this->partnership);
  225. }
  226. // Clean data
  227. // $this->partnership->abc = sanitizeVal($this->partnership->abc, 'alphanohtml');
  228. if ($this->partnership->update(DolibarrApiAccess::$user, false) > 0) {
  229. return $this->get($id);
  230. } else {
  231. throw new RestException(500, $this->partnership->error);
  232. }
  233. }
  234. /**
  235. * Delete partnership
  236. *
  237. * @param int $id Partnership ID
  238. * @return array
  239. *
  240. * @throws RestException
  241. *
  242. * @url DELETE partnerships/{id}
  243. */
  244. public function delete($id)
  245. {
  246. if (!DolibarrApiAccess::$user->rights->partnership->delete) {
  247. throw new RestException(401);
  248. }
  249. $result = $this->partnership->fetch($id);
  250. if (!$result) {
  251. throw new RestException(404, 'Partnership not found');
  252. }
  253. if (!DolibarrApi::_checkAccessToResource('partnership', $this->partnership->id, 'partnership')) {
  254. throw new RestException(401, 'Access to instance id='.$this->partnership->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  255. }
  256. if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
  257. throw new RestException(500, 'Error when deleting Partnership : '.$this->partnership->error);
  258. }
  259. return array(
  260. 'success' => array(
  261. 'code' => 200,
  262. 'message' => 'Partnership deleted'
  263. )
  264. );
  265. }
  266. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  267. /**
  268. * Clean sensible object datas
  269. *
  270. * @param Object $object Object to clean
  271. * @return Object Object with cleaned properties
  272. */
  273. protected function _cleanObjectDatas($object)
  274. {
  275. // phpcs:enable
  276. $object = parent::_cleanObjectDatas($object);
  277. unset($object->rowid);
  278. unset($object->canvas);
  279. /*unset($object->name);
  280. unset($object->lastname);
  281. unset($object->firstname);
  282. unset($object->civility_id);
  283. unset($object->statut);
  284. unset($object->state);
  285. unset($object->state_id);
  286. unset($object->state_code);
  287. unset($object->region);
  288. unset($object->region_code);
  289. unset($object->country);
  290. unset($object->country_id);
  291. unset($object->country_code);
  292. unset($object->barcode_type);
  293. unset($object->barcode_type_code);
  294. unset($object->barcode_type_label);
  295. unset($object->barcode_type_coder);
  296. unset($object->total_ht);
  297. unset($object->total_tva);
  298. unset($object->total_localtax1);
  299. unset($object->total_localtax2);
  300. unset($object->total_ttc);
  301. unset($object->fk_account);
  302. unset($object->comments);
  303. unset($object->note);
  304. unset($object->mode_reglement_id);
  305. unset($object->cond_reglement_id);
  306. unset($object->cond_reglement);
  307. unset($object->shipping_method_id);
  308. unset($object->fk_incoterms);
  309. unset($object->label_incoterms);
  310. unset($object->location_incoterms);
  311. */
  312. // If object has lines, remove $db property
  313. if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
  314. $nboflines = count($object->lines);
  315. for ($i = 0; $i < $nboflines; $i++) {
  316. $this->_cleanObjectDatas($object->lines[$i]);
  317. unset($object->lines[$i]->lines);
  318. unset($object->lines[$i]->note);
  319. }
  320. }
  321. return $object;
  322. }
  323. /**
  324. * Validate fields before create or update object
  325. *
  326. * @param array $data Array of data to validate
  327. * @return array
  328. *
  329. * @throws RestException
  330. */
  331. private function _validate($data)
  332. {
  333. $partnership = array();
  334. foreach ($this->partnership->fields as $field => $propfield) {
  335. if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
  336. continue; // Not a mandatory field
  337. }
  338. if (!isset($data[$field])) {
  339. throw new RestException(400, "$field field missing");
  340. }
  341. $partnership[$field] = $data[$field];
  342. }
  343. return $partnership;
  344. }
  345. }