api_proposals.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. if (!count($obj_ret)) {
  221. throw new RestException(404, 'No proposal found');
  222. }
  223. return $obj_ret;
  224. }
  225. /**
  226. * Create commercial proposal object
  227. *
  228. * @param array $request_data Request data
  229. * @return int ID of proposal
  230. */
  231. public function post($request_data = null)
  232. {
  233. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  234. throw new RestException(401, "Insuffisant rights");
  235. }
  236. // Check mandatory fields
  237. $result = $this->_validate($request_data);
  238. foreach ($request_data as $field => $value) {
  239. $this->propal->$field = $value;
  240. }
  241. /*if (isset($request_data["lines"])) {
  242. $lines = array();
  243. foreach ($request_data["lines"] as $line) {
  244. array_push($lines, (object) $line);
  245. }
  246. $this->propal->lines = $lines;
  247. }*/
  248. if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
  249. throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
  250. }
  251. return $this->propal->id;
  252. }
  253. /**
  254. * Get lines of a commercial proposal
  255. *
  256. * @param int $id Id of commercial proposal
  257. * @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')"
  258. *
  259. * @url GET {id}/lines
  260. *
  261. * @return array
  262. */
  263. public function getLines($id, $sqlfilters = '')
  264. {
  265. if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
  266. throw new RestException(401);
  267. }
  268. $result = $this->propal->fetch($id);
  269. if (!$result) {
  270. throw new RestException(404, 'Commercial Proposal not found');
  271. }
  272. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  273. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  274. }
  275. $sql = '';
  276. if (!empty($sqlfilters)) {
  277. $errormessage = '';
  278. $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  279. if ($errormessage) {
  280. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  281. }
  282. }
  283. $this->propal->getLinesArray($sql);
  284. $result = array();
  285. foreach ($this->propal->lines as $line) {
  286. array_push($result, $this->_cleanObjectDatas($line));
  287. }
  288. return $result;
  289. }
  290. /**
  291. * Add a line to given commercial proposal
  292. *
  293. * @param int $id Id of commercial proposal to update
  294. * @param array $request_data Commercial proposal line data
  295. *
  296. * @url POST {id}/line
  297. *
  298. * @return int
  299. */
  300. public function postLine($id, $request_data = null)
  301. {
  302. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  303. throw new RestException(401);
  304. }
  305. $result = $this->propal->fetch($id);
  306. if (!$result) {
  307. throw new RestException(404, 'Commercial Proposal not found');
  308. }
  309. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  310. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  311. }
  312. $request_data = (object) $request_data;
  313. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  314. $request_data->label = sanitizeVal($request_data->label);
  315. $updateRes = $this->propal->addline(
  316. $request_data->desc,
  317. $request_data->subprice,
  318. $request_data->qty,
  319. $request_data->tva_tx,
  320. $request_data->localtax1_tx,
  321. $request_data->localtax2_tx,
  322. $request_data->fk_product,
  323. $request_data->remise_percent,
  324. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  325. $request_data->subprice,
  326. $request_data->info_bits,
  327. $request_data->product_type,
  328. $request_data->rang,
  329. $request_data->special_code,
  330. $request_data->fk_parent_line,
  331. $request_data->fk_fournprice,
  332. $request_data->pa_ht,
  333. $request_data->label,
  334. $request_data->date_start,
  335. $request_data->date_end,
  336. $request_data->array_options,
  337. $request_data->fk_unit,
  338. $request_data->origin,
  339. $request_data->origin_id,
  340. $request_data->multicurrency_subprice,
  341. $request_data->fk_remise_except
  342. );
  343. if ($updateRes > 0) {
  344. return $updateRes;
  345. } else {
  346. throw new RestException(400, $this->propal->error);
  347. }
  348. }
  349. /**
  350. * Add lines to given commercial proposal
  351. *
  352. * @param int $id Id of commercial proposal to update
  353. * @param array $request_data Commercial proposal line data
  354. *
  355. * @url POST {id}/lines
  356. *
  357. * @return int
  358. */
  359. public function postLines($id, $request_data = null)
  360. {
  361. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  362. throw new RestException(401);
  363. }
  364. $result = $this->propal->fetch($id);
  365. if (!$result) {
  366. throw new RestException(404, 'Commercial Proposal not found');
  367. }
  368. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  369. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  370. }
  371. $errors = [];
  372. $this->db->begin();
  373. foreach ($request_data as $TData) {
  374. if (empty($TData[0])) {
  375. $TData = array($TData);
  376. }
  377. foreach ($TData as $lineData) {
  378. $line = (object) $lineData;
  379. $updateRes = $this->propal->addline(
  380. $line->desc,
  381. $line->subprice,
  382. $line->qty,
  383. $line->tva_tx,
  384. $line->localtax1_tx,
  385. $line->localtax2_tx,
  386. $line->fk_product,
  387. $line->remise_percent,
  388. 'HT',
  389. 0,
  390. $line->info_bits,
  391. $line->product_type,
  392. $line->rang,
  393. $line->special_code,
  394. $line->fk_parent_line,
  395. $line->fk_fournprice,
  396. $line->pa_ht,
  397. $line->label,
  398. $line->date_start,
  399. $line->date_end,
  400. $line->array_options,
  401. $line->fk_unit,
  402. $line->origin,
  403. $line->origin_id,
  404. $line->multicurrency_subprice,
  405. $line->fk_remise_except
  406. );
  407. if ($updateRes < 0) {
  408. $errors['lineLabel'] = $line->label;
  409. $errors['msg'] = $this->propal->errors;
  410. }
  411. }
  412. }
  413. if (empty($errors)) {
  414. $this->db->commit();
  415. return $updateRes;
  416. } else {
  417. $this->db->rollback();
  418. throw new RestException(400, implode(", ", $errors));
  419. }
  420. }
  421. /**
  422. * Update a line of given commercial proposal
  423. *
  424. * @param int $id Id of commercial proposal to update
  425. * @param int $lineid Id of line to update
  426. * @param array $request_data Commercial proposal line data
  427. * @return Object|false Object with cleaned properties
  428. *
  429. * @url PUT {id}/lines/{lineid}
  430. */
  431. public function putLine($id, $lineid, $request_data = null)
  432. {
  433. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  434. throw new RestException(401);
  435. }
  436. $result = $this->propal->fetch($id);
  437. if ($result <= 0) {
  438. throw new RestException(404, 'Proposal not found');
  439. }
  440. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  441. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  442. }
  443. $request_data = (object) $request_data;
  444. if (isset($request_data->desc)) {
  445. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  446. }
  447. if (isset($request_data->label)) {
  448. $request_data->label = sanitizeVal($request_data->label);
  449. }
  450. $propalline = new PropaleLigne($this->db);
  451. $result = $propalline->fetch($lineid);
  452. if ($result <= 0) {
  453. throw new RestException(404, 'Proposal line not found');
  454. }
  455. $updateRes = $this->propal->updateline(
  456. $lineid,
  457. isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
  458. isset($request_data->qty) ? $request_data->qty : $propalline->qty,
  459. isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
  460. isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
  461. isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
  462. isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
  463. isset($request_data->desc) ? $request_data->desc : $propalline->desc,
  464. isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
  465. isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
  466. isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
  467. isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
  468. 0,
  469. isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
  470. isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
  471. isset($request_data->label) ? $request_data->label : $propalline->label,
  472. isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
  473. isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
  474. isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
  475. isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
  476. isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
  477. isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
  478. 0,
  479. isset($request_data->rang) ? $request_data->rang : $propalline->rang
  480. );
  481. if ($updateRes > 0) {
  482. $result = $this->get($id);
  483. unset($result->line);
  484. return $this->_cleanObjectDatas($result);
  485. }
  486. return false;
  487. }
  488. /**
  489. * Delete a line of given commercial proposal
  490. *
  491. *
  492. * @param int $id Id of commercial proposal to update
  493. * @param int $lineid Id of line to delete
  494. * @return Object|false Object with cleaned properties
  495. *
  496. * @url DELETE {id}/lines/{lineid}
  497. *
  498. * @throws RestException 401
  499. * @throws RestException 404
  500. */
  501. public function deleteLine($id, $lineid)
  502. {
  503. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  504. throw new RestException(401);
  505. }
  506. $result = $this->propal->fetch($id);
  507. if (!$result) {
  508. throw new RestException(404, 'Proposal not found');
  509. }
  510. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  511. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  512. }
  513. $updateRes = $this->propal->deleteline($lineid, $id);
  514. if ($updateRes > 0) {
  515. return $this->get($id);
  516. } else {
  517. throw new RestException(405, $this->propal->error);
  518. }
  519. }
  520. /**
  521. * Add a contact type of given commercial proposal
  522. *
  523. * @param int $id Id of commercial proposal to update
  524. * @param int $contactid Id of contact to add
  525. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER)
  526. * @return array
  527. *
  528. * @url POST {id}/contact/{contactid}/{type}
  529. *
  530. * @throws RestException 401
  531. * @throws RestException 404
  532. */
  533. public function postContact($id, $contactid, $type)
  534. {
  535. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  536. throw new RestException(401);
  537. }
  538. $result = $this->propal->fetch($id);
  539. if (!$result) {
  540. throw new RestException(404, 'Proposal not found');
  541. }
  542. if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
  543. throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
  544. }
  545. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  546. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  547. }
  548. $result = $this->propal->add_contact($contactid, $type, 'external');
  549. if (!$result) {
  550. throw new RestException(500, 'Error when added the contact');
  551. }
  552. return array(
  553. 'success' => array(
  554. 'code' => 200,
  555. 'message' => 'Contact linked to the proposal'
  556. )
  557. );
  558. }
  559. /**
  560. * Delete a contact type of given commercial proposal
  561. *
  562. * @param int $id Id of commercial proposal to update
  563. * @param int $contactid Row key of the contact in the array contact_ids.
  564. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER).
  565. * @return Object Object with cleaned properties
  566. *
  567. * @url DELETE {id}/contact/{contactid}/{type}
  568. *
  569. * @throws RestException 401
  570. * @throws RestException 404
  571. * @throws RestException 500 System error
  572. */
  573. public function deleteContact($id, $contactid, $type)
  574. {
  575. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  576. throw new RestException(401);
  577. }
  578. $result = $this->propal->fetch($id);
  579. if (!$result) {
  580. throw new RestException(404, 'Proposal not found');
  581. }
  582. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  583. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  584. }
  585. $contacts = $this->propal->liste_contact();
  586. foreach ($contacts as $contact) {
  587. if ($contact['id'] == $contactid && $contact['code'] == $type) {
  588. $result = $this->propal->delete_contact($contact['rowid']);
  589. if (!$result) {
  590. throw new RestException(500, 'Error when deleted the contact');
  591. }
  592. }
  593. }
  594. return $this->_cleanObjectDatas($this->propal);
  595. }
  596. /**
  597. * Update commercial proposal general fields (won't touch lines of commercial proposal)
  598. *
  599. * @param int $id Id of commercial proposal to update
  600. * @param array $request_data Datas
  601. * @return Object Object with cleaned properties
  602. */
  603. public function put($id, $request_data = null)
  604. {
  605. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  606. throw new RestException(401);
  607. }
  608. $result = $this->propal->fetch($id);
  609. if (!$result) {
  610. throw new RestException(404, 'Proposal not found');
  611. }
  612. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  613. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  614. }
  615. foreach ($request_data as $field => $value) {
  616. if ($field == 'id') {
  617. continue;
  618. }
  619. $this->propal->$field = $value;
  620. }
  621. // update end of validity date
  622. if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
  623. $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
  624. }
  625. if (!empty($this->propal->fin_validite)) {
  626. if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
  627. throw new RestException(500, $this->propal->error);
  628. }
  629. }
  630. if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
  631. return $this->get($id);
  632. } else {
  633. throw new RestException(500, $this->propal->error);
  634. }
  635. }
  636. /**
  637. * Delete commercial proposal
  638. *
  639. * @param int $id Commercial proposal ID
  640. * @return array
  641. */
  642. public function delete($id)
  643. {
  644. if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
  645. throw new RestException(401);
  646. }
  647. $result = $this->propal->fetch($id);
  648. if (!$result) {
  649. throw new RestException(404, 'Commercial Proposal not found');
  650. }
  651. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  652. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  653. }
  654. if (!$this->propal->delete(DolibarrApiAccess::$user)) {
  655. throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
  656. }
  657. return array(
  658. 'success' => array(
  659. 'code' => 200,
  660. 'message' => 'Commercial Proposal deleted'
  661. )
  662. );
  663. }
  664. /**
  665. * Set a proposal to draft
  666. *
  667. * @param int $id Order ID
  668. * @return Object Object with cleaned properties
  669. *
  670. * @url POST {id}/settodraft
  671. */
  672. public function settodraft($id)
  673. {
  674. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  675. throw new RestException(401);
  676. }
  677. $result = $this->propal->fetch($id);
  678. if (!$result) {
  679. throw new RestException(404, 'Proposal not found');
  680. }
  681. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  682. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  683. }
  684. $result = $this->propal->setDraft(DolibarrApiAccess::$user);
  685. if ($result == 0) {
  686. throw new RestException(304, 'Nothing done. May be object is already draft');
  687. }
  688. if ($result < 0) {
  689. throw new RestException(500, 'Error : '.$this->propal->error);
  690. }
  691. $result = $this->propal->fetch($id);
  692. if (!$result) {
  693. throw new RestException(404, 'Proposal not found');
  694. }
  695. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  696. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  697. }
  698. $this->propal->fetchObjectLinked();
  699. return $this->_cleanObjectDatas($this->propal);
  700. }
  701. /**
  702. * Validate a commercial proposal
  703. *
  704. * If you get a bad value for param notrigger check that ou provide this in body
  705. * {
  706. * "notrigger": 0
  707. * }
  708. *
  709. * @param int $id Commercial proposal ID
  710. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  711. * @return Object Object with cleaned properties
  712. *
  713. * @url POST {id}/validate
  714. *
  715. * @throws RestException 304
  716. * @throws RestException 401
  717. * @throws RestException 404
  718. * @throws RestException 500 System error
  719. */
  720. public function validate($id, $notrigger = 0)
  721. {
  722. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  723. throw new RestException(401);
  724. }
  725. $result = $this->propal->fetch($id);
  726. if (!$result) {
  727. throw new RestException(404, 'Commercial Proposal not found');
  728. }
  729. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  730. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  731. }
  732. $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
  733. if ($result == 0) {
  734. throw new RestException(304, 'Error nothing done. May be object is already validated');
  735. }
  736. if ($result < 0) {
  737. throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
  738. }
  739. $result = $this->propal->fetch($id);
  740. if (!$result) {
  741. throw new RestException(404, 'Commercial Proposal not found');
  742. }
  743. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  744. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  745. }
  746. $this->propal->fetchObjectLinked();
  747. return $this->_cleanObjectDatas($this->propal);
  748. }
  749. /**
  750. * Close (Accept or refuse) a quote / commercial proposal
  751. *
  752. * @param int $id Commercial proposal ID
  753. * @param int $status Must be 2 (accepted) or 3 (refused) {@min 2}{@max 3}
  754. * @param string $note_private Add this mention at end of private note
  755. * @param int $notrigger Disabled triggers
  756. * @return Object Object with cleaned properties
  757. *
  758. * @url POST {id}/close
  759. */
  760. public function close($id, $status, $note_private = '', $notrigger = 0)
  761. {
  762. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  763. throw new RestException(401);
  764. }
  765. $result = $this->propal->fetch($id);
  766. if (!$result) {
  767. throw new RestException(404, 'Commercial Proposal not found');
  768. }
  769. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  770. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  771. }
  772. $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
  773. if ($result == 0) {
  774. throw new RestException(304, 'Error nothing done. May be object is already closed');
  775. }
  776. if ($result < 0) {
  777. throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
  778. }
  779. $result = $this->propal->fetch($id);
  780. if (!$result) {
  781. throw new RestException(404, 'Proposal not found');
  782. }
  783. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  784. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  785. }
  786. $this->propal->fetchObjectLinked();
  787. return $this->_cleanObjectDatas($this->propal);
  788. }
  789. /**
  790. * Set a commercial proposal billed. Could be also called setbilled
  791. *
  792. * @param int $id Commercial proposal ID
  793. * @return Object Object with cleaned properties
  794. *
  795. * @url POST {id}/setinvoiced
  796. */
  797. public function setinvoiced($id)
  798. {
  799. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  800. throw new RestException(401);
  801. }
  802. $result = $this->propal->fetch($id);
  803. if (!$result) {
  804. throw new RestException(404, 'Commercial Proposal not found');
  805. }
  806. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  807. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  808. }
  809. $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
  810. if ($result < 0) {
  811. throw new RestException(500, 'Error : '.$this->propal->error);
  812. }
  813. $result = $this->propal->fetch($id);
  814. if (!$result) {
  815. throw new RestException(404, 'Proposal not found');
  816. }
  817. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  818. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  819. }
  820. $this->propal->fetchObjectLinked();
  821. return $this->_cleanObjectDatas($this->propal);
  822. }
  823. /**
  824. * Validate fields before create or update object
  825. *
  826. * @param array $data Array with data to verify
  827. * @return array
  828. *
  829. * @throws RestException
  830. */
  831. private function _validate($data)
  832. {
  833. $propal = array();
  834. foreach (Proposals::$FIELDS as $field) {
  835. if (!isset($data[$field])) {
  836. throw new RestException(400, "$field field missing");
  837. }
  838. $propal[$field] = $data[$field];
  839. }
  840. return $propal;
  841. }
  842. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  843. /**
  844. * Clean sensible object datas
  845. *
  846. * @param Object $object Object to clean
  847. * @return Object Object with cleaned properties
  848. */
  849. protected function _cleanObjectDatas($object)
  850. {
  851. // phpcs:enable
  852. $object = parent::_cleanObjectDatas($object);
  853. unset($object->note);
  854. unset($object->name);
  855. unset($object->lastname);
  856. unset($object->firstname);
  857. unset($object->civility_id);
  858. unset($object->address);
  859. return $object;
  860. }
  861. }