api_proposals.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
  5. * Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
  6. * Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. use Luracast\Restler\RestException;
  22. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  23. /**
  24. * API class for orders
  25. *
  26. * @access protected
  27. * @class DolibarrApiAccess {@requires user,external}
  28. */
  29. class Proposals extends DolibarrApi
  30. {
  31. /**
  32. * @var array $FIELDS Mandatory fields, checked when create and update object
  33. */
  34. public static $FIELDS = array(
  35. 'socid'
  36. );
  37. /**
  38. * @var Propal $propal {@type Propal}
  39. */
  40. public $propal;
  41. /**
  42. * Constructor
  43. */
  44. public function __construct()
  45. {
  46. global $db;
  47. $this->db = $db;
  48. $this->propal = new Propal($this->db);
  49. }
  50. /**
  51. * Get properties of a commercial proposal object
  52. *
  53. * Return an array with commercial proposal informations
  54. *
  55. * @param int $id ID of commercial proposal
  56. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  57. * @return Object Object with cleaned properties
  58. *
  59. * @throws RestException
  60. */
  61. public function get($id, $contact_list = 1)
  62. {
  63. return $this->_fetch($id, '', '', $contact_list);
  64. }
  65. /**
  66. * Get properties of an proposal object by ref
  67. *
  68. * Return an array with proposal informations
  69. *
  70. * @param string $ref Ref of object
  71. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  72. * @return Object Object with cleaned properties
  73. *
  74. * @url GET ref/{ref}
  75. *
  76. * @throws RestException
  77. */
  78. public function getByRef($ref, $contact_list = 1)
  79. {
  80. return $this->_fetch('', $ref, '', $contact_list);
  81. }
  82. /**
  83. * Get properties of an proposal object by ref_ext
  84. *
  85. * Return an array with proposal informations
  86. *
  87. * @param string $ref_ext External reference of object
  88. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  89. * @return Object Object with cleaned properties
  90. *
  91. * @url GET ref_ext/{ref_ext}
  92. *
  93. * @throws RestException
  94. */
  95. public function getByRefExt($ref_ext, $contact_list = 1)
  96. {
  97. return $this->_fetch('', '', $ref_ext, $contact_list);
  98. }
  99. /**
  100. * Get properties of an proposal object
  101. *
  102. * Return an array with proposal informations
  103. *
  104. * @param int $id ID of order
  105. * @param string $ref Ref of object
  106. * @param string $ref_ext External reference of object
  107. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  108. * @return Object Object with cleaned properties
  109. *
  110. * @throws RestException
  111. */
  112. private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
  113. {
  114. if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
  115. throw new RestException(401);
  116. }
  117. $result = $this->propal->fetch($id, $ref, $ref_ext);
  118. if (!$result) {
  119. throw new RestException(404, 'Commercial Proposal not found');
  120. }
  121. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  122. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  123. }
  124. // Add external contacts ids.
  125. $tmparray = $this->propal->liste_contact(-1, 'external', $contact_list);
  126. if (is_array($tmparray)) {
  127. $this->propal->contacts_ids = $tmparray;
  128. }
  129. $this->propal->fetchObjectLinked();
  130. return $this->_cleanObjectDatas($this->propal);
  131. }
  132. /**
  133. * List commercial proposals
  134. *
  135. * Get a list of commercial proposals
  136. *
  137. * @param string $sortfield Sort field
  138. * @param string $sortorder Sort order
  139. * @param int $limit Limit for list
  140. * @param int $page Page number
  141. * @param string $thirdparty_ids Thirdparty ids to filter commercial proposals (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  142. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.datec:<:'20160101')"
  143. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  144. * @return array Array of order objects
  145. */
  146. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
  147. {
  148. global $db, $conf;
  149. if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
  150. throw new RestException(401);
  151. }
  152. $obj_ret = array();
  153. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  154. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  155. // If the internal user must only see his customers, force searching by him
  156. $search_sale = 0;
  157. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  158. $search_sale = DolibarrApiAccess::$user->id;
  159. }
  160. $sql = "SELECT t.rowid";
  161. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  162. $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)
  163. }
  164. $sql .= " FROM ".MAIN_DB_PREFIX."propal AS t LEFT JOIN ".MAIN_DB_PREFIX."propal_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
  165. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  166. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  167. }
  168. $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
  169. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  170. $sql .= " AND t.fk_soc = sc.fk_soc";
  171. }
  172. if ($socids) {
  173. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  174. }
  175. if ($search_sale > 0) {
  176. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  177. }
  178. // Insert sale filter
  179. if ($search_sale > 0) {
  180. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  181. }
  182. // Add sql filters
  183. if ($sqlfilters) {
  184. $errormessage = '';
  185. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  186. if ($errormessage) {
  187. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  188. }
  189. }
  190. $sql .= $this->db->order($sortfield, $sortorder);
  191. if ($limit) {
  192. if ($page < 0) {
  193. $page = 0;
  194. }
  195. $offset = $limit * $page;
  196. $sql .= $this->db->plimit($limit + 1, $offset);
  197. }
  198. dol_syslog("API Rest request");
  199. $result = $this->db->query($sql);
  200. if ($result) {
  201. $num = $this->db->num_rows($result);
  202. $min = min($num, ($limit <= 0 ? $num : $limit));
  203. $i = 0;
  204. while ($i < $min) {
  205. $obj = $this->db->fetch_object($result);
  206. $proposal_static = new Propal($this->db);
  207. if ($proposal_static->fetch($obj->rowid)) {
  208. // Add external contacts ids
  209. $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
  210. if (is_array($tmparray)) {
  211. $proposal_static->contacts_ids = $tmparray;
  212. }
  213. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($proposal_static), $properties);
  214. }
  215. $i++;
  216. }
  217. } else {
  218. throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
  219. }
  220. return $obj_ret;
  221. }
  222. /**
  223. * Create commercial proposal object
  224. *
  225. * @param array $request_data Request data
  226. * @return int ID of proposal
  227. */
  228. public function post($request_data = null)
  229. {
  230. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  231. throw new RestException(401, "Insuffisant rights");
  232. }
  233. // Check mandatory fields
  234. $result = $this->_validate($request_data);
  235. foreach ($request_data as $field => $value) {
  236. if ($field === 'caller') {
  237. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  238. $this->propal->context['caller'] = $request_data['caller'];
  239. continue;
  240. }
  241. $this->propal->$field = $value;
  242. }
  243. /*if (isset($request_data["lines"])) {
  244. $lines = array();
  245. foreach ($request_data["lines"] as $line) {
  246. array_push($lines, (object) $line);
  247. }
  248. $this->propal->lines = $lines;
  249. }*/
  250. if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
  251. throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
  252. }
  253. return $this->propal->id;
  254. }
  255. /**
  256. * Get lines of a commercial proposal
  257. *
  258. * @param int $id Id of commercial proposal
  259. * @param string $sqlfilters Other criteria to filter answers separated by a comma. d is the alias for proposal lines table, p is the alias for product table. "Syntax example "(p.ref:like:'SO-%') AND (d.date_start:<:'20220101')"
  260. *
  261. * @url GET {id}/lines
  262. *
  263. * @return array
  264. */
  265. public function getLines($id, $sqlfilters = '')
  266. {
  267. if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
  268. throw new RestException(401);
  269. }
  270. $result = $this->propal->fetch($id);
  271. if (!$result) {
  272. throw new RestException(404, 'Commercial Proposal not found');
  273. }
  274. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  275. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  276. }
  277. $sql = '';
  278. if (!empty($sqlfilters)) {
  279. $errormessage = '';
  280. $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  281. if ($errormessage) {
  282. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  283. }
  284. }
  285. $this->propal->getLinesArray($sql);
  286. $result = array();
  287. foreach ($this->propal->lines as $line) {
  288. array_push($result, $this->_cleanObjectDatas($line));
  289. }
  290. return $result;
  291. }
  292. /**
  293. * Add a line to given commercial proposal
  294. *
  295. * @param int $id Id of commercial proposal to update
  296. * @param array $request_data Commercial proposal line data
  297. *
  298. * @url POST {id}/line
  299. *
  300. * @return int
  301. */
  302. public function postLine($id, $request_data = null)
  303. {
  304. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  305. throw new RestException(401);
  306. }
  307. $result = $this->propal->fetch($id);
  308. if (!$result) {
  309. throw new RestException(404, 'Commercial Proposal not found');
  310. }
  311. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  312. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  313. }
  314. $request_data = (object) $request_data;
  315. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  316. $request_data->label = sanitizeVal($request_data->label);
  317. $updateRes = $this->propal->addline(
  318. $request_data->desc,
  319. $request_data->subprice,
  320. $request_data->qty,
  321. $request_data->tva_tx,
  322. $request_data->localtax1_tx,
  323. $request_data->localtax2_tx,
  324. $request_data->fk_product,
  325. $request_data->remise_percent,
  326. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  327. $request_data->subprice,
  328. $request_data->info_bits,
  329. $request_data->product_type,
  330. $request_data->rang,
  331. $request_data->special_code,
  332. $request_data->fk_parent_line,
  333. $request_data->fk_fournprice,
  334. $request_data->pa_ht,
  335. $request_data->label,
  336. $request_data->date_start,
  337. $request_data->date_end,
  338. $request_data->array_options,
  339. $request_data->fk_unit,
  340. $request_data->origin,
  341. $request_data->origin_id,
  342. $request_data->multicurrency_subprice,
  343. $request_data->fk_remise_except
  344. );
  345. if ($updateRes > 0) {
  346. return $updateRes;
  347. } else {
  348. throw new RestException(400, $this->propal->error);
  349. }
  350. }
  351. /**
  352. * Add lines to given commercial proposal
  353. *
  354. * @param int $id Id of commercial proposal to update
  355. * @param array $request_data Commercial proposal line data
  356. *
  357. * @url POST {id}/lines
  358. *
  359. * @return int
  360. */
  361. public function postLines($id, $request_data = null)
  362. {
  363. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  364. throw new RestException(401);
  365. }
  366. $result = $this->propal->fetch($id);
  367. if (!$result) {
  368. throw new RestException(404, 'Commercial Proposal not found');
  369. }
  370. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  371. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  372. }
  373. $errors = [];
  374. $this->db->begin();
  375. foreach ($request_data as $TData) {
  376. if (empty($TData[0])) {
  377. $TData = array($TData);
  378. }
  379. foreach ($TData as $lineData) {
  380. $line = (object) $lineData;
  381. $updateRes = $this->propal->addline(
  382. $line->desc,
  383. $line->subprice,
  384. $line->qty,
  385. $line->tva_tx,
  386. $line->localtax1_tx,
  387. $line->localtax2_tx,
  388. $line->fk_product,
  389. $line->remise_percent,
  390. 'HT',
  391. 0,
  392. $line->info_bits,
  393. $line->product_type,
  394. $line->rang,
  395. $line->special_code,
  396. $line->fk_parent_line,
  397. $line->fk_fournprice,
  398. $line->pa_ht,
  399. $line->label,
  400. $line->date_start,
  401. $line->date_end,
  402. $line->array_options,
  403. $line->fk_unit,
  404. $line->origin,
  405. $line->origin_id,
  406. $line->multicurrency_subprice,
  407. $line->fk_remise_except
  408. );
  409. if ($updateRes < 0) {
  410. $errors['lineLabel'] = $line->label;
  411. $errors['msg'] = $this->propal->errors;
  412. }
  413. }
  414. }
  415. if (empty($errors)) {
  416. $this->db->commit();
  417. return $updateRes;
  418. } else {
  419. $this->db->rollback();
  420. throw new RestException(400, implode(", ", $errors));
  421. }
  422. }
  423. /**
  424. * Update a line of given commercial proposal
  425. *
  426. * @param int $id Id of commercial proposal to update
  427. * @param int $lineid Id of line to update
  428. * @param array $request_data Commercial proposal line data
  429. * @return Object|false Object with cleaned properties
  430. *
  431. * @url PUT {id}/lines/{lineid}
  432. */
  433. public function putLine($id, $lineid, $request_data = null)
  434. {
  435. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  436. throw new RestException(401);
  437. }
  438. $result = $this->propal->fetch($id);
  439. if ($result <= 0) {
  440. throw new RestException(404, 'Proposal not found');
  441. }
  442. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  443. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  444. }
  445. $request_data = (object) $request_data;
  446. if (isset($request_data->desc)) {
  447. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  448. }
  449. if (isset($request_data->label)) {
  450. $request_data->label = sanitizeVal($request_data->label);
  451. }
  452. $propalline = new PropaleLigne($this->db);
  453. $result = $propalline->fetch($lineid);
  454. if ($result <= 0) {
  455. throw new RestException(404, 'Proposal line not found');
  456. }
  457. $updateRes = $this->propal->updateline(
  458. $lineid,
  459. isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
  460. isset($request_data->qty) ? $request_data->qty : $propalline->qty,
  461. isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
  462. isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
  463. isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
  464. isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
  465. isset($request_data->desc) ? $request_data->desc : $propalline->desc,
  466. isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
  467. isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
  468. isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
  469. isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
  470. 0,
  471. isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
  472. isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
  473. isset($request_data->label) ? $request_data->label : $propalline->label,
  474. isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
  475. isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
  476. isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
  477. isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
  478. isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
  479. isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
  480. 0,
  481. isset($request_data->rang) ? $request_data->rang : $propalline->rang
  482. );
  483. if ($updateRes > 0) {
  484. $result = $this->get($id);
  485. unset($result->line);
  486. return $this->_cleanObjectDatas($result);
  487. }
  488. return false;
  489. }
  490. /**
  491. * Delete a line of given commercial proposal
  492. *
  493. *
  494. * @param int $id Id of commercial proposal to update
  495. * @param int $lineid Id of line to delete
  496. * @return Object|false Object with cleaned properties
  497. *
  498. * @url DELETE {id}/lines/{lineid}
  499. *
  500. * @throws RestException 401
  501. * @throws RestException 404
  502. */
  503. public function deleteLine($id, $lineid)
  504. {
  505. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  506. throw new RestException(401);
  507. }
  508. $result = $this->propal->fetch($id);
  509. if (!$result) {
  510. throw new RestException(404, 'Proposal not found');
  511. }
  512. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  513. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  514. }
  515. $updateRes = $this->propal->deleteline($lineid, $id);
  516. if ($updateRes > 0) {
  517. return $this->get($id);
  518. } else {
  519. throw new RestException(405, $this->propal->error);
  520. }
  521. }
  522. /**
  523. * Add a contact type of given commercial proposal
  524. *
  525. * @param int $id Id of commercial proposal to update
  526. * @param int $contactid Id of contact to add
  527. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER)
  528. * @return array
  529. *
  530. * @url POST {id}/contact/{contactid}/{type}
  531. *
  532. * @throws RestException 401
  533. * @throws RestException 404
  534. */
  535. public function postContact($id, $contactid, $type)
  536. {
  537. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  538. throw new RestException(401);
  539. }
  540. $result = $this->propal->fetch($id);
  541. if (!$result) {
  542. throw new RestException(404, 'Proposal not found');
  543. }
  544. if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
  545. throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
  546. }
  547. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  548. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  549. }
  550. $result = $this->propal->add_contact($contactid, $type, 'external');
  551. if (!$result) {
  552. throw new RestException(500, 'Error when added the contact');
  553. }
  554. return array(
  555. 'success' => array(
  556. 'code' => 200,
  557. 'message' => 'Contact linked to the proposal'
  558. )
  559. );
  560. }
  561. /**
  562. * Delete a contact type of given commercial proposal
  563. *
  564. * @param int $id Id of commercial proposal to update
  565. * @param int $contactid Row key of the contact in the array contact_ids.
  566. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER).
  567. * @return Object Object with cleaned properties
  568. *
  569. * @url DELETE {id}/contact/{contactid}/{type}
  570. *
  571. * @throws RestException 401
  572. * @throws RestException 404
  573. * @throws RestException 500 System error
  574. */
  575. public function deleteContact($id, $contactid, $type)
  576. {
  577. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  578. throw new RestException(401);
  579. }
  580. $result = $this->propal->fetch($id);
  581. if (!$result) {
  582. throw new RestException(404, 'Proposal not found');
  583. }
  584. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  585. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  586. }
  587. $contacts = $this->propal->liste_contact();
  588. foreach ($contacts as $contact) {
  589. if ($contact['id'] == $contactid && $contact['code'] == $type) {
  590. $result = $this->propal->delete_contact($contact['rowid']);
  591. if (!$result) {
  592. throw new RestException(500, 'Error when deleted the contact');
  593. }
  594. }
  595. }
  596. return $this->_cleanObjectDatas($this->propal);
  597. }
  598. /**
  599. * Update commercial proposal general fields (won't touch lines of commercial proposal)
  600. *
  601. * @param int $id Id of commercial proposal to update
  602. * @param array $request_data Datas
  603. * @return Object Object with cleaned properties
  604. */
  605. public function put($id, $request_data = null)
  606. {
  607. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  608. throw new RestException(401);
  609. }
  610. $result = $this->propal->fetch($id);
  611. if (!$result) {
  612. throw new RestException(404, 'Proposal not found');
  613. }
  614. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  615. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  616. }
  617. foreach ($request_data as $field => $value) {
  618. if ($field == 'id') {
  619. continue;
  620. }
  621. if ($field === 'caller') {
  622. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  623. $this->propal->context['caller'] = $request_data['caller'];
  624. continue;
  625. }
  626. $this->propal->$field = $value;
  627. }
  628. // update end of validity date
  629. if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
  630. $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
  631. }
  632. if (!empty($this->propal->fin_validite)) {
  633. if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
  634. throw new RestException(500, $this->propal->error);
  635. }
  636. }
  637. if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
  638. return $this->get($id);
  639. } else {
  640. throw new RestException(500, $this->propal->error);
  641. }
  642. }
  643. /**
  644. * Delete commercial proposal
  645. *
  646. * @param int $id Commercial proposal ID
  647. * @return array
  648. */
  649. public function delete($id)
  650. {
  651. if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
  652. throw new RestException(401);
  653. }
  654. $result = $this->propal->fetch($id);
  655. if (!$result) {
  656. throw new RestException(404, 'Commercial Proposal not found');
  657. }
  658. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  659. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  660. }
  661. if (!$this->propal->delete(DolibarrApiAccess::$user)) {
  662. throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
  663. }
  664. return array(
  665. 'success' => array(
  666. 'code' => 200,
  667. 'message' => 'Commercial Proposal deleted'
  668. )
  669. );
  670. }
  671. /**
  672. * Set a proposal to draft
  673. *
  674. * @param int $id Order ID
  675. * @return Object Object with cleaned properties
  676. *
  677. * @url POST {id}/settodraft
  678. */
  679. public function settodraft($id)
  680. {
  681. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  682. throw new RestException(401);
  683. }
  684. $result = $this->propal->fetch($id);
  685. if (!$result) {
  686. throw new RestException(404, 'Proposal not found');
  687. }
  688. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  689. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  690. }
  691. $result = $this->propal->setDraft(DolibarrApiAccess::$user);
  692. if ($result == 0) {
  693. throw new RestException(304, 'Nothing done. May be object is already draft');
  694. }
  695. if ($result < 0) {
  696. throw new RestException(500, 'Error : '.$this->propal->error);
  697. }
  698. $result = $this->propal->fetch($id);
  699. if (!$result) {
  700. throw new RestException(404, 'Proposal not found');
  701. }
  702. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  703. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  704. }
  705. $this->propal->fetchObjectLinked();
  706. return $this->_cleanObjectDatas($this->propal);
  707. }
  708. /**
  709. * Validate a commercial proposal
  710. *
  711. * If you get a bad value for param notrigger check that ou provide this in body
  712. * {
  713. * "notrigger": 0
  714. * }
  715. *
  716. * @param int $id Commercial proposal ID
  717. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  718. * @return Object Object with cleaned properties
  719. *
  720. * @url POST {id}/validate
  721. *
  722. * @throws RestException 304
  723. * @throws RestException 401
  724. * @throws RestException 404
  725. * @throws RestException 500 System error
  726. */
  727. public function validate($id, $notrigger = 0)
  728. {
  729. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  730. throw new RestException(401);
  731. }
  732. $result = $this->propal->fetch($id);
  733. if (!$result) {
  734. throw new RestException(404, 'Commercial Proposal not found');
  735. }
  736. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  737. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  738. }
  739. $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
  740. if ($result == 0) {
  741. throw new RestException(304, 'Error nothing done. May be object is already validated');
  742. }
  743. if ($result < 0) {
  744. throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
  745. }
  746. $result = $this->propal->fetch($id);
  747. if (!$result) {
  748. throw new RestException(404, 'Commercial Proposal not found');
  749. }
  750. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  751. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  752. }
  753. $this->propal->fetchObjectLinked();
  754. return $this->_cleanObjectDatas($this->propal);
  755. }
  756. /**
  757. * Close (Accept or refuse) a quote / commercial proposal
  758. *
  759. * @param int $id Commercial proposal ID
  760. * @param int $status Must be 2 (accepted) or 3 (refused) {@min 2}{@max 3}
  761. * @param string $note_private Add this mention at end of private note
  762. * @param int $notrigger Disabled triggers
  763. * @return Object Object with cleaned properties
  764. *
  765. * @url POST {id}/close
  766. */
  767. public function close($id, $status, $note_private = '', $notrigger = 0)
  768. {
  769. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  770. throw new RestException(401);
  771. }
  772. $result = $this->propal->fetch($id);
  773. if (!$result) {
  774. throw new RestException(404, 'Commercial Proposal not found');
  775. }
  776. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  777. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  778. }
  779. $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
  780. if ($result == 0) {
  781. throw new RestException(304, 'Error nothing done. May be object is already closed');
  782. }
  783. if ($result < 0) {
  784. throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
  785. }
  786. $result = $this->propal->fetch($id);
  787. if (!$result) {
  788. throw new RestException(404, 'Proposal not found');
  789. }
  790. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  791. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  792. }
  793. $this->propal->fetchObjectLinked();
  794. return $this->_cleanObjectDatas($this->propal);
  795. }
  796. /**
  797. * Set a commercial proposal billed. Could be also called setbilled
  798. *
  799. * @param int $id Commercial proposal ID
  800. * @return Object Object with cleaned properties
  801. *
  802. * @url POST {id}/setinvoiced
  803. */
  804. public function setinvoiced($id)
  805. {
  806. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  807. throw new RestException(401);
  808. }
  809. $result = $this->propal->fetch($id);
  810. if (!$result) {
  811. throw new RestException(404, 'Commercial Proposal not found');
  812. }
  813. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  814. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  815. }
  816. $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
  817. if ($result < 0) {
  818. throw new RestException(500, 'Error : '.$this->propal->error);
  819. }
  820. $result = $this->propal->fetch($id);
  821. if (!$result) {
  822. throw new RestException(404, 'Proposal not found');
  823. }
  824. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  825. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  826. }
  827. $this->propal->fetchObjectLinked();
  828. return $this->_cleanObjectDatas($this->propal);
  829. }
  830. /**
  831. * Validate fields before create or update object
  832. *
  833. * @param array $data Array with data to verify
  834. * @return array
  835. *
  836. * @throws RestException
  837. */
  838. private function _validate($data)
  839. {
  840. $propal = array();
  841. foreach (Proposals::$FIELDS as $field) {
  842. if (!isset($data[$field])) {
  843. throw new RestException(400, "$field field missing");
  844. }
  845. $propal[$field] = $data[$field];
  846. }
  847. return $propal;
  848. }
  849. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  850. /**
  851. * Clean sensible object datas
  852. *
  853. * @param Object $object Object to clean
  854. * @return Object Object with cleaned properties
  855. */
  856. protected function _cleanObjectDatas($object)
  857. {
  858. // phpcs:enable
  859. $object = parent::_cleanObjectDatas($object);
  860. unset($object->note);
  861. unset($object->name);
  862. unset($object->lastname);
  863. unset($object->firstname);
  864. unset($object->civility_id);
  865. unset($object->address);
  866. return $object;
  867. }
  868. }