api_supplier_proposals.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
  20. /**
  21. * API class for orders
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class SupplierProposals extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. 'socid'
  33. );
  34. /**
  35. * @var SupplierProposal $supplier_proposal {@type SupplierProposal}
  36. */
  37. public $supplier_proposal;
  38. /**
  39. * Constructor
  40. */
  41. public function __construct()
  42. {
  43. global $db;
  44. $this->db = $db;
  45. $this->supplier_proposal = new SupplierProposal($this->db);
  46. }
  47. /**
  48. * Get properties of a supplier proposal (price request) object
  49. *
  50. * Return an array with supplier proposal informations
  51. *
  52. * @param int $id ID of supplier proposal
  53. * @return Object Object with cleaned properties
  54. *
  55. * @throws RestException
  56. */
  57. public function get($id)
  58. {
  59. if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
  60. throw new RestException(401);
  61. }
  62. $result = $this->supplier_proposal->fetch($id);
  63. if (!$result) {
  64. throw new RestException(404, 'Supplier Proposal not found');
  65. }
  66. if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
  67. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  68. }
  69. $this->supplier_proposal->fetchObjectLinked();
  70. return $this->_cleanObjectDatas($this->supplier_proposal);
  71. }
  72. /**
  73. * List supplier proposals
  74. *
  75. * Get a list of supplier proposals
  76. *
  77. * @param string $sortfield Sort field
  78. * @param string $sortorder Sort order
  79. * @param int $limit Limit for list
  80. * @param int $page Page number
  81. * @param string $thirdparty_ids Thirdparty ids to filter supplier proposals (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  82. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.datec:<:'20160101')"
  83. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  84. * @return array Array of order objects
  85. */
  86. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
  87. {
  88. if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
  89. throw new RestException(401);
  90. }
  91. $obj_ret = array();
  92. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  93. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  94. // If the internal user must only see his customers, force searching by him
  95. $search_sale = 0;
  96. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  97. $search_sale = DolibarrApiAccess::$user->id;
  98. }
  99. $sql = "SELECT t.rowid";
  100. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  101. $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)
  102. }
  103. $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal AS t LEFT JOIN ".MAIN_DB_PREFIX."supplier_proposal_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
  104. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  105. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  106. }
  107. $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
  108. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  109. $sql .= " AND t.fk_soc = sc.fk_soc";
  110. }
  111. if ($socids) {
  112. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  113. }
  114. if ($search_sale > 0) {
  115. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  116. }
  117. // Insert sale filter
  118. if ($search_sale > 0) {
  119. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  120. }
  121. // Add sql filters
  122. if ($sqlfilters) {
  123. $errormessage = '';
  124. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  125. if ($errormessage) {
  126. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  127. }
  128. }
  129. $sql .= $this->db->order($sortfield, $sortorder);
  130. if ($limit) {
  131. if ($page < 0) {
  132. $page = 0;
  133. }
  134. $offset = $limit * $page;
  135. $sql .= $this->db->plimit($limit + 1, $offset);
  136. }
  137. $result = $this->db->query($sql);
  138. if ($result) {
  139. $num = $this->db->num_rows($result);
  140. $min = min($num, ($limit <= 0 ? $num : $limit));
  141. $i = 0;
  142. while ($i < $min) {
  143. $obj = $this->db->fetch_object($result);
  144. $propal_static = new SupplierProposal($this->db);
  145. if ($propal_static->fetch($obj->rowid)) {
  146. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($propal_static), $properties);
  147. }
  148. $i++;
  149. }
  150. } else {
  151. throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
  152. }
  153. return $obj_ret;
  154. }
  155. /**
  156. * Validate fields before create or update object
  157. *
  158. * @param array $data Array with data to verify
  159. * @return array
  160. * @throws RestException
  161. */
  162. private function _validate($data)
  163. {
  164. $propal = array();
  165. foreach (SupplierProposals::$FIELDS as $field) {
  166. if (!isset($data[$field])) {
  167. throw new RestException(400, "$field field missing");
  168. }
  169. $propal[$field] = $data[$field];
  170. }
  171. return $propal;
  172. }
  173. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  174. /**
  175. * Clean sensible object datas
  176. *
  177. * @param Object $object Object to clean
  178. * @return Object Object with cleaned properties
  179. */
  180. protected function _cleanObjectDatas($object)
  181. {
  182. // phpcs:enable
  183. $object = parent::_cleanObjectDatas($object);
  184. unset($object->name);
  185. unset($object->lastname);
  186. unset($object->firstname);
  187. unset($object->civility_id);
  188. unset($object->address);
  189. unset($object->datec);
  190. unset($object->datev);
  191. return $object;
  192. }
  193. }