api_proposals.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. 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, $conf;
  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 array|mixed data without useless information
  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 array|mixed data without useless information
  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 array|mixed data without useless information
  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 array|mixed data without useless information
  109. *
  110. * @throws RestException
  111. */
  112. private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
  113. {
  114. if (!DolibarrApiAccess::$user->rights->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. * @return array Array of order objects
  144. */
  145. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
  146. {
  147. global $db, $conf;
  148. if (!DolibarrApiAccess::$user->rights->propal->lire) {
  149. throw new RestException(401);
  150. }
  151. $obj_ret = array();
  152. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  153. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  154. // If the internal user must only see his customers, force searching by him
  155. $search_sale = 0;
  156. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  157. $search_sale = DolibarrApiAccess::$user->id;
  158. }
  159. $sql = "SELECT t.rowid";
  160. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  161. $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)
  162. }
  163. $sql .= " FROM ".MAIN_DB_PREFIX."propal as t";
  164. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  165. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  166. }
  167. $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
  168. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  169. $sql .= " AND t.fk_soc = sc.fk_soc";
  170. }
  171. if ($socids) {
  172. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  173. }
  174. if ($search_sale > 0) {
  175. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  176. }
  177. // Insert sale filter
  178. if ($search_sale > 0) {
  179. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  180. }
  181. // Add sql filters
  182. if ($sqlfilters) {
  183. $errormessage = '';
  184. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  185. if ($errormessage) {
  186. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  187. }
  188. }
  189. $sql .= $this->db->order($sortfield, $sortorder);
  190. if ($limit) {
  191. if ($page < 0) {
  192. $page = 0;
  193. }
  194. $offset = $limit * $page;
  195. $sql .= $this->db->plimit($limit + 1, $offset);
  196. }
  197. dol_syslog("API Rest request");
  198. $result = $this->db->query($sql);
  199. if ($result) {
  200. $num = $this->db->num_rows($result);
  201. $min = min($num, ($limit <= 0 ? $num : $limit));
  202. $i = 0;
  203. while ($i < $min) {
  204. $obj = $this->db->fetch_object($result);
  205. $proposal_static = new Propal($this->db);
  206. if ($proposal_static->fetch($obj->rowid)) {
  207. // Add external contacts ids
  208. $tmparray = $proposal_static->liste_contact(-1, 'external', 1);
  209. if (is_array($tmparray)) {
  210. $proposal_static->contacts_ids = $tmparray;
  211. }
  212. $obj_ret[] = $this->_cleanObjectDatas($proposal_static);
  213. }
  214. $i++;
  215. }
  216. } else {
  217. throw new RestException(503, 'Error when retrieve propal list : '.$this->db->lasterror());
  218. }
  219. if (!count($obj_ret)) {
  220. throw new RestException(404, 'No proposal found');
  221. }
  222. return $obj_ret;
  223. }
  224. /**
  225. * Create commercial proposal object
  226. *
  227. * @param array $request_data Request data
  228. * @return int ID of proposal
  229. */
  230. public function post($request_data = null)
  231. {
  232. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  233. throw new RestException(401, "Insuffisant rights");
  234. }
  235. // Check mandatory fields
  236. $result = $this->_validate($request_data);
  237. foreach ($request_data as $field => $value) {
  238. $this->propal->$field = $value;
  239. }
  240. /*if (isset($request_data["lines"])) {
  241. $lines = array();
  242. foreach ($request_data["lines"] as $line) {
  243. array_push($lines, (object) $line);
  244. }
  245. $this->propal->lines = $lines;
  246. }*/
  247. if ($this->propal->create(DolibarrApiAccess::$user) < 0) {
  248. throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors));
  249. }
  250. return $this->propal->id;
  251. }
  252. /**
  253. * Get lines of a commercial proposal
  254. *
  255. * @param int $id Id of commercial proposal
  256. * @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')"
  257. *
  258. * @url GET {id}/lines
  259. *
  260. * @return int
  261. */
  262. public function getLines($id, $sqlfilters = '')
  263. {
  264. $filters = "";
  265. if (!DolibarrApiAccess::$user->rights->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. if (!empty($sqlfilters)) {
  276. $errormessage = '';
  277. $sql = forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  278. if ($errormessage) {
  279. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  280. }
  281. }
  282. $this->propal->getLinesArray($sql);
  283. $result = array();
  284. foreach ($this->propal->lines as $line) {
  285. array_push($result, $this->_cleanObjectDatas($line));
  286. }
  287. return $result;
  288. }
  289. /**
  290. * Add a line to given commercial proposal
  291. *
  292. * @param int $id Id of commercial proposal to update
  293. * @param array $request_data Commercial proposal line data
  294. *
  295. * @url POST {id}/line
  296. *
  297. * @return int
  298. */
  299. public function postLine($id, $request_data = null)
  300. {
  301. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  302. throw new RestException(401);
  303. }
  304. $result = $this->propal->fetch($id);
  305. if (!$result) {
  306. throw new RestException(404, 'Commercial Proposal not found');
  307. }
  308. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  309. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  310. }
  311. $request_data = (object) $request_data;
  312. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  313. $request_data->label = sanitizeVal($request_data->label);
  314. $updateRes = $this->propal->addline(
  315. $request_data->desc,
  316. $request_data->subprice,
  317. $request_data->qty,
  318. $request_data->tva_tx,
  319. $request_data->localtax1_tx,
  320. $request_data->localtax2_tx,
  321. $request_data->fk_product,
  322. $request_data->remise_percent,
  323. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  324. $request_data->subprice,
  325. $request_data->info_bits,
  326. $request_data->product_type,
  327. $request_data->rang,
  328. $request_data->special_code,
  329. $request_data->fk_parent_line,
  330. $request_data->fk_fournprice,
  331. $request_data->pa_ht,
  332. $request_data->label,
  333. $request_data->date_start,
  334. $request_data->date_end,
  335. $request_data->array_options,
  336. $request_data->fk_unit,
  337. $request_data->origin,
  338. $request_data->origin_id,
  339. $request_data->multicurrency_subprice,
  340. $request_data->fk_remise_except
  341. );
  342. if ($updateRes > 0) {
  343. return $updateRes;
  344. } else {
  345. throw new RestException(400, $this->propal->error);
  346. }
  347. }
  348. /**
  349. * Add lines to given commercial proposal
  350. *
  351. * @param int $id Id of commercial proposal to update
  352. * @param array $request_data Commercial proposal line data
  353. *
  354. * @url POST {id}/lines
  355. *
  356. * @return int
  357. */
  358. public function postLines($id, $request_data = null)
  359. {
  360. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  361. throw new RestException(401);
  362. }
  363. $result = $this->propal->fetch($id);
  364. if (!$result) {
  365. throw new RestException(404, 'Commercial Proposal not found');
  366. }
  367. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  368. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  369. }
  370. $errors = [];
  371. $this->db->begin();
  372. foreach ($request_data as $TData) {
  373. if (empty($TData[0])) $TData = array($TData);
  374. foreach ($TData as $lineData) {
  375. $line = (object) $lineData;
  376. $updateRes = $this->propal->addline(
  377. $line->desc,
  378. $line->subprice,
  379. $line->qty,
  380. $line->tva_tx,
  381. $line->localtax1_tx,
  382. $line->localtax2_tx,
  383. $line->fk_product,
  384. $line->remise_percent,
  385. 'HT',
  386. 0,
  387. $line->info_bits,
  388. $line->product_type,
  389. $line->rang,
  390. $line->special_code,
  391. $line->fk_parent_line,
  392. $line->fk_fournprice,
  393. $line->pa_ht,
  394. $line->label,
  395. $line->date_start,
  396. $line->date_end,
  397. $line->array_options,
  398. $line->fk_unit,
  399. $line->origin,
  400. $line->origin_id,
  401. $line->multicurrency_subprice,
  402. $line->fk_remise_except
  403. );
  404. if ($updateRes < 0) {
  405. $errors['lineLabel'] = $line->label;
  406. $errors['msg'] = $this->propal->errors;
  407. }
  408. }
  409. }
  410. if (empty($errors)) {
  411. $this->db->commit();
  412. return count($request_data);
  413. } else {
  414. $this->db->rollback();
  415. throw new RestException(400, implode(", ", $errors));
  416. }
  417. }
  418. /**
  419. * Update a line of given commercial proposal
  420. *
  421. * @param int $id Id of commercial proposal to update
  422. * @param int $lineid Id of line to update
  423. * @param array $request_data Commercial proposal line data
  424. *
  425. * @url PUT {id}/lines/{lineid}
  426. *
  427. * @return object
  428. */
  429. public function putLine($id, $lineid, $request_data = null)
  430. {
  431. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  432. throw new RestException(401);
  433. }
  434. $result = $this->propal->fetch($id);
  435. if ($result <= 0) {
  436. throw new RestException(404, 'Proposal not found');
  437. }
  438. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  439. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  440. }
  441. $request_data = (object) $request_data;
  442. if (isset($request_data->desc)) {
  443. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  444. }
  445. if (isset($request_data->label)) {
  446. $request_data->label = sanitizeVal($request_data->label);
  447. }
  448. $propalline = new PropaleLigne($this->db);
  449. $result = $propalline->fetch($lineid);
  450. if ($result <= 0) {
  451. throw new RestException(404, 'Proposal line not found');
  452. }
  453. $updateRes = $this->propal->updateline(
  454. $lineid,
  455. isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice,
  456. isset($request_data->qty) ? $request_data->qty : $propalline->qty,
  457. isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent,
  458. isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx,
  459. isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx,
  460. isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx,
  461. isset($request_data->desc) ? $request_data->desc : $propalline->desc,
  462. isset($request_data->price_base_type) ? $request_data->price_base_type : 'HT',
  463. isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits,
  464. isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code,
  465. isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line,
  466. 0,
  467. isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice,
  468. isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht,
  469. isset($request_data->label) ? $request_data->label : $propalline->label,
  470. isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type,
  471. isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start,
  472. isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end,
  473. isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options,
  474. isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit,
  475. isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice,
  476. 0,
  477. isset($request_data->rang) ? $request_data->rang : $propalline->rang
  478. );
  479. if ($updateRes > 0) {
  480. $result = $this->get($id);
  481. unset($result->line);
  482. return $this->_cleanObjectDatas($result);
  483. }
  484. return false;
  485. }
  486. /**
  487. * Delete a line of given commercial proposal
  488. *
  489. *
  490. * @param int $id Id of commercial proposal to update
  491. * @param int $lineid Id of line to delete
  492. *
  493. * @url DELETE {id}/lines/{lineid}
  494. *
  495. * @return int
  496. *
  497. * @throws RestException 401
  498. * @throws RestException 404
  499. */
  500. public function deleteLine($id, $lineid)
  501. {
  502. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  503. throw new RestException(401);
  504. }
  505. $result = $this->propal->fetch($id);
  506. if (!$result) {
  507. throw new RestException(404, 'Proposal not found');
  508. }
  509. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  510. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  511. }
  512. // TODO Check the lineid $lineid is a line of ojbect
  513. $updateRes = $this->propal->deleteline($lineid);
  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. *
  527. * @url POST {id}/contact/{contactid}/{type}
  528. *
  529. * @return int
  530. *
  531. * @throws RestException 401
  532. * @throws RestException 404
  533. */
  534. public function postContact($id, $contactid, $type)
  535. {
  536. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  537. throw new RestException(401);
  538. }
  539. $result = $this->propal->fetch($id);
  540. if (!$result) {
  541. throw new RestException(404, 'Proposal not found');
  542. }
  543. if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) {
  544. throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER');
  545. }
  546. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  547. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  548. }
  549. $result = $this->propal->add_contact($contactid, $type, 'external');
  550. if (!$result) {
  551. throw new RestException(500, 'Error when added the contact');
  552. }
  553. return $this->propal;
  554. }
  555. /**
  556. * Delete a contact type of given commercial proposal
  557. *
  558. * @param int $id Id of commercial proposal to update
  559. * @param int $contactid Row key of the contact in the array contact_ids.
  560. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER).
  561. *
  562. * @url DELETE {id}/contact/{contactid}/{type}
  563. *
  564. * @return int
  565. *
  566. * @throws RestException 401
  567. * @throws RestException 404
  568. * @throws RestException 500 System error
  569. */
  570. public function deleteContact($id, $contactid, $type)
  571. {
  572. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  573. throw new RestException(401);
  574. }
  575. $result = $this->propal->fetch($id);
  576. if (!$result) {
  577. throw new RestException(404, 'Proposal not found');
  578. }
  579. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  580. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  581. }
  582. $contacts = $this->propal->liste_contact();
  583. foreach ($contacts as $contact) {
  584. if ($contact['id'] == $contactid && $contact['code'] == $type) {
  585. $result = $this->propal->delete_contact($contact['rowid']);
  586. if (!$result) {
  587. throw new RestException(500, 'Error when deleted the contact');
  588. }
  589. }
  590. }
  591. return $this->_cleanObjectDatas($this->propal);
  592. }
  593. /**
  594. * Update commercial proposal general fields (won't touch lines of commercial proposal)
  595. *
  596. * @param int $id Id of commercial proposal to update
  597. * @param array $request_data Datas
  598. *
  599. * @return int
  600. */
  601. public function put($id, $request_data = null)
  602. {
  603. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  604. throw new RestException(401);
  605. }
  606. $result = $this->propal->fetch($id);
  607. if (!$result) {
  608. throw new RestException(404, 'Proposal not found');
  609. }
  610. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  611. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  612. }
  613. foreach ($request_data as $field => $value) {
  614. if ($field == 'id') {
  615. continue;
  616. }
  617. $this->propal->$field = $value;
  618. }
  619. // update end of validity date
  620. if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) {
  621. $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600);
  622. }
  623. if (!empty($this->propal->fin_validite)) {
  624. if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) {
  625. throw new RestException(500, $this->propal->error);
  626. }
  627. }
  628. if ($this->propal->update(DolibarrApiAccess::$user) > 0) {
  629. return $this->get($id);
  630. } else {
  631. throw new RestException(500, $this->propal->error);
  632. }
  633. }
  634. /**
  635. * Delete commercial proposal
  636. *
  637. * @param int $id Commercial proposal ID
  638. *
  639. * @return array
  640. */
  641. public function delete($id)
  642. {
  643. if (!DolibarrApiAccess::$user->rights->propal->supprimer) {
  644. throw new RestException(401);
  645. }
  646. $result = $this->propal->fetch($id);
  647. if (!$result) {
  648. throw new RestException(404, 'Commercial Proposal not found');
  649. }
  650. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  651. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  652. }
  653. if (!$this->propal->delete(DolibarrApiAccess::$user)) {
  654. throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error);
  655. }
  656. return array(
  657. 'success' => array(
  658. 'code' => 200,
  659. 'message' => 'Commercial Proposal deleted'
  660. )
  661. );
  662. }
  663. /**
  664. * Set a proposal to draft
  665. *
  666. * @param int $id Order ID
  667. *
  668. * @url POST {id}/settodraft
  669. *
  670. * @return array
  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. *
  712. * @url POST {id}/validate
  713. *
  714. * @throws RestException 304
  715. * @throws RestException 401
  716. * @throws RestException 404
  717. * @throws RestException 500 System error
  718. *
  719. * @return array
  720. */
  721. public function validate($id, $notrigger = 0)
  722. {
  723. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  724. throw new RestException(401);
  725. }
  726. $result = $this->propal->fetch($id);
  727. if (!$result) {
  728. throw new RestException(404, 'Commercial Proposal not found');
  729. }
  730. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  731. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  732. }
  733. $result = $this->propal->valid(DolibarrApiAccess::$user, $notrigger);
  734. if ($result == 0) {
  735. throw new RestException(304, 'Error nothing done. May be object is already validated');
  736. }
  737. if ($result < 0) {
  738. throw new RestException(500, 'Error when validating Commercial Proposal: '.$this->propal->error);
  739. }
  740. $result = $this->propal->fetch($id);
  741. if (!$result) {
  742. throw new RestException(404, 'Commercial Proposal not found');
  743. }
  744. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  745. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  746. }
  747. $this->propal->fetchObjectLinked();
  748. return $this->_cleanObjectDatas($this->propal);
  749. }
  750. /**
  751. * Close (Accept or refuse) a quote / commercial proposal
  752. *
  753. * @param int $id Commercial proposal ID
  754. * @param int $status Must be 2 (accepted) or 3 (refused) {@min 2}{@max 3}
  755. * @param string $note_private Add this mention at end of private note
  756. * @param int $notrigger Disabled triggers
  757. *
  758. * @url POST {id}/close
  759. *
  760. * @return array
  761. */
  762. public function close($id, $status, $note_private = '', $notrigger = 0)
  763. {
  764. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  765. throw new RestException(401);
  766. }
  767. $result = $this->propal->fetch($id);
  768. if (!$result) {
  769. throw new RestException(404, 'Commercial Proposal not found');
  770. }
  771. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  772. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  773. }
  774. $result = $this->propal->closeProposal(DolibarrApiAccess::$user, $status, $note_private, $notrigger);
  775. if ($result == 0) {
  776. throw new RestException(304, 'Error nothing done. May be object is already closed');
  777. }
  778. if ($result < 0) {
  779. throw new RestException(500, 'Error when closing Commercial Proposal: '.$this->propal->error);
  780. }
  781. $result = $this->propal->fetch($id);
  782. if (!$result) {
  783. throw new RestException(404, 'Proposal not found');
  784. }
  785. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  786. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  787. }
  788. $this->propal->fetchObjectLinked();
  789. return $this->_cleanObjectDatas($this->propal);
  790. }
  791. /**
  792. * Set a commercial proposal billed. Could be also called setbilled
  793. *
  794. * @param int $id Commercial proposal ID
  795. *
  796. * @url POST {id}/setinvoiced
  797. *
  798. * @return array
  799. */
  800. public function setinvoiced($id)
  801. {
  802. if (!DolibarrApiAccess::$user->rights->propal->creer) {
  803. throw new RestException(401);
  804. }
  805. $result = $this->propal->fetch($id);
  806. if (!$result) {
  807. throw new RestException(404, 'Commercial Proposal not found');
  808. }
  809. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  810. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  811. }
  812. $result = $this->propal->classifyBilled(DolibarrApiAccess::$user);
  813. if ($result < 0) {
  814. throw new RestException(500, 'Error : '.$this->propal->error);
  815. }
  816. $result = $this->propal->fetch($id);
  817. if (!$result) {
  818. throw new RestException(404, 'Proposal not found');
  819. }
  820. if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
  821. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  822. }
  823. $this->propal->fetchObjectLinked();
  824. return $this->_cleanObjectDatas($this->propal);
  825. }
  826. /**
  827. * Validate fields before create or update object
  828. *
  829. * @param array $data Array with data to verify
  830. * @return array
  831. * @throws RestException
  832. */
  833. private function _validate($data)
  834. {
  835. $propal = array();
  836. foreach (Proposals::$FIELDS as $field) {
  837. if (!isset($data[$field])) {
  838. throw new RestException(400, "$field field missing");
  839. }
  840. $propal[$field] = $data[$field];
  841. }
  842. return $propal;
  843. }
  844. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  845. /**
  846. * Clean sensible object datas
  847. *
  848. * @param Object $object Object to clean
  849. * @return Object Object with cleaned properties
  850. */
  851. protected function _cleanObjectDatas($object)
  852. {
  853. // phpcs:enable
  854. $object = parent::_cleanObjectDatas($object);
  855. unset($object->note);
  856. unset($object->name);
  857. unset($object->lastname);
  858. unset($object->firstname);
  859. unset($object->civility_id);
  860. unset($object->address);
  861. return $object;
  862. }
  863. }