api_donations.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /* Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
  3. * Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
  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. require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
  20. /**
  21. * API class for donations
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Donations extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. 'amount'
  33. );
  34. /**
  35. * @var Don $don {@type Don}
  36. */
  37. public $don;
  38. /**
  39. * Constructor
  40. */
  41. public function __construct()
  42. {
  43. global $db, $conf;
  44. $this->db = $db;
  45. $this->don = new Don($this->db);
  46. }
  47. /**
  48. * Get properties of an donation object
  49. *
  50. * Return an array with donation informations
  51. *
  52. * @param int $id ID of order
  53. * @return Object Object with cleaned properties
  54. *
  55. * @throws RestException
  56. */
  57. public function get($id)
  58. {
  59. if (!DolibarrApiAccess::$user->rights->don->lire) {
  60. throw new RestException(401);
  61. }
  62. $result = $this->don->fetch($id);
  63. if (!$result) {
  64. throw new RestException(404, 'Donation not found');
  65. }
  66. if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
  67. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  68. }
  69. // Add external contacts ids
  70. //$this->don->contacts_ids = $this->don->liste_contact(-1,'external',1);
  71. //$this->don->fetchObjectLinked();
  72. return $this->_cleanObjectDatas($this->don);
  73. }
  74. /**
  75. * List donations
  76. *
  77. * Get a list of donations
  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 string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  84. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  85. * @return array Array of order objects
  86. *
  87. * @throws RestException
  88. */
  89. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
  90. {
  91. global $db, $conf;
  92. if (!DolibarrApiAccess::$user->rights->don->lire) {
  93. throw new RestException(401);
  94. }
  95. $obj_ret = array();
  96. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  97. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  98. $sql = "SELECT t.rowid";
  99. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) {
  100. $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)
  101. }
  102. $sql .= " FROM ".MAIN_DB_PREFIX."don AS t LEFT JOIN ".MAIN_DB_PREFIX."don_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
  103. $sql .= ' WHERE t.entity IN ('.getEntity('don').')';
  104. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) {
  105. $sql .= " AND t.fk_soc = sc.fk_soc";
  106. }
  107. if ($thirdparty_ids) {
  108. $sql .= " AND t.fk_soc = ".((int) $thirdparty_ids)." ";
  109. }
  110. // Add sql filters
  111. if ($sqlfilters) {
  112. $errormessage = '';
  113. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  114. if ($errormessage) {
  115. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  116. }
  117. }
  118. $sql .= $this->db->order($sortfield, $sortorder);
  119. if ($limit) {
  120. if ($page < 0) {
  121. $page = 0;
  122. }
  123. $offset = $limit * $page;
  124. $sql .= $this->db->plimit($limit + 1, $offset);
  125. }
  126. dol_syslog("API Rest request");
  127. $result = $this->db->query($sql);
  128. if ($result) {
  129. $num = $this->db->num_rows($result);
  130. $min = min($num, ($limit <= 0 ? $num : $limit));
  131. $i = 0;
  132. while ($i < $min) {
  133. $obj = $this->db->fetch_object($result);
  134. $don_static = new Don($this->db);
  135. if ($don_static->fetch($obj->rowid)) {
  136. // Add external contacts ids
  137. //$don_static->contacts_ids = $don_static->liste_contact(-1, 'external', 1);
  138. $obj_ret[] = $this->_cleanObjectDatas($don_static);
  139. }
  140. $i++;
  141. }
  142. } else {
  143. throw new RestException(503, 'Error when retrieve donation list : '.$this->db->lasterror());
  144. }
  145. if (!count($obj_ret)) {
  146. throw new RestException(404, 'No donation found');
  147. }
  148. return $obj_ret;
  149. }
  150. /**
  151. * Create donation object
  152. *
  153. * @param array $request_data Request data
  154. * @return int ID of order
  155. */
  156. public function post($request_data = null)
  157. {
  158. if (!DolibarrApiAccess::$user->rights->don->creer) {
  159. throw new RestException(401, "Insuffisant rights");
  160. }
  161. // Check mandatory fields
  162. $result = $this->_validate($request_data);
  163. foreach ($request_data as $field => $value) {
  164. $this->don->$field = $value;
  165. }
  166. /*if (isset($request_data["lines"])) {
  167. $lines = array();
  168. foreach ($request_data["lines"] as $line) {
  169. array_push($lines, (object) $line);
  170. }
  171. $this->don->lines = $lines;
  172. }*/
  173. if ($this->don->create(DolibarrApiAccess::$user) < 0) {
  174. throw new RestException(500, "Error creating donation", array_merge(array($this->don->error), $this->don->errors));
  175. }
  176. return $this->don->id;
  177. }
  178. /**
  179. * Update order general fields (won't touch lines of order)
  180. *
  181. * @param int $id Id of order to update
  182. * @param array $request_data Datas
  183. *
  184. * @return int
  185. */
  186. public function put($id, $request_data = null)
  187. {
  188. if (!DolibarrApiAccess::$user->rights->don->creer) {
  189. throw new RestException(401);
  190. }
  191. $result = $this->don->fetch($id);
  192. if (!$result) {
  193. throw new RestException(404, 'Donation not found');
  194. }
  195. if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
  196. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  197. }
  198. foreach ($request_data as $field => $value) {
  199. if ($field == 'id') {
  200. continue;
  201. }
  202. $this->don->$field = $value;
  203. }
  204. if ($this->don->update(DolibarrApiAccess::$user) > 0) {
  205. return $this->get($id);
  206. } else {
  207. throw new RestException(500, $this->don->error);
  208. }
  209. }
  210. /**
  211. * Delete donation
  212. *
  213. * @param int $id Order ID
  214. * @return array
  215. */
  216. public function delete($id)
  217. {
  218. if (!DolibarrApiAccess::$user->rights->don->supprimer) {
  219. throw new RestException(401);
  220. }
  221. $result = $this->don->fetch($id);
  222. if (!$result) {
  223. throw new RestException(404, 'Donation not found');
  224. }
  225. if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
  226. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  227. }
  228. if (!$this->don->delete(DolibarrApiAccess::$user)) {
  229. throw new RestException(500, 'Error when delete donation : '.$this->don->error);
  230. }
  231. return array(
  232. 'success' => array(
  233. 'code' => 200,
  234. 'message' => 'Donation deleted'
  235. )
  236. );
  237. }
  238. /**
  239. * Validate an donation
  240. *
  241. * If you get a bad value for param notrigger check, provide this in body
  242. * {
  243. * "idwarehouse": 0,
  244. * "notrigger": 0
  245. * }
  246. *
  247. * @param int $id Order ID
  248. * @param int $idwarehouse Warehouse ID
  249. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  250. *
  251. * @url POST {id}/validate
  252. *
  253. * @throws RestException 304
  254. * @throws RestException 401
  255. * @throws RestException 404
  256. * @throws RestException 500 System error
  257. *
  258. * @return object
  259. */
  260. public function validate($id, $idwarehouse = 0, $notrigger = 0)
  261. {
  262. if (!DolibarrApiAccess::$user->rights->don->creer) {
  263. throw new RestException(401);
  264. }
  265. $result = $this->don->fetch($id);
  266. if (!$result) {
  267. throw new RestException(404, 'Donation not found');
  268. }
  269. if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
  270. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  271. }
  272. $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->id, $notrigger);
  273. if ($result == 0) {
  274. throw new RestException(304, 'Error nothing done. May be object is already validated');
  275. }
  276. if ($result < 0) {
  277. throw new RestException(500, 'Error when validating Order: '.$this->don->error);
  278. }
  279. $result = $this->don->fetch($id);
  280. if (!$result) {
  281. throw new RestException(404, 'Order not found');
  282. }
  283. if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
  284. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  285. }
  286. $this->don->fetchObjectLinked();
  287. return $this->_cleanObjectDatas($this->don);
  288. }
  289. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  290. /**
  291. * Clean sensible object datas
  292. *
  293. * @param Object $object Object to clean
  294. * @return Object Object with cleaned properties
  295. */
  296. protected function _cleanObjectDatas($object)
  297. {
  298. // phpcs:enable
  299. $object = parent::_cleanObjectDatas($object);
  300. unset($object->note);
  301. unset($object->address);
  302. unset($object->barcode_type);
  303. unset($object->barcode_type_code);
  304. unset($object->barcode_type_label);
  305. unset($object->barcode_type_coder);
  306. return $object;
  307. }
  308. /**
  309. * Validate fields before create or update object
  310. *
  311. * @param array $data Array with data to verify
  312. * @return array
  313. * @throws RestException
  314. */
  315. private function _validate($data)
  316. {
  317. $don = array();
  318. foreach (Donations::$FIELDS as $field) {
  319. if (!isset($data[$field])) {
  320. throw new RestException(400, $field." field missing");
  321. }
  322. $don[$field] = $data[$field];
  323. }
  324. return $don;
  325. }
  326. }