api_supplier_proposals.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, $conf;
  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 array|mixed data without useless information
  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->propal->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. * @return array Array of order objects
  84. */
  85. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
  86. {
  87. global $db, $conf;
  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";
  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. if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
  125. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  126. }
  127. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  128. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  129. }
  130. $sql .= $this->db->order($sortfield, $sortorder);
  131. if ($limit) {
  132. if ($page < 0) {
  133. $page = 0;
  134. }
  135. $offset = $limit * $page;
  136. $sql .= $this->db->plimit($limit + 1, $offset);
  137. }
  138. $result = $this->db->query($sql);
  139. if ($result) {
  140. $num = $this->db->num_rows($result);
  141. $min = min($num, ($limit <= 0 ? $num : $limit));
  142. $i = 0;
  143. while ($i < $min) {
  144. $obj = $this->db->fetch_object($result);
  145. $propal_static = new SupplierProposal($this->db);
  146. if ($propal_static->fetch($obj->rowid)) {
  147. $obj_ret[] = $this->_cleanObjectDatas($propal_static);
  148. }
  149. $i++;
  150. }
  151. } else {
  152. throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
  153. }
  154. if (!count($obj_ret)) {
  155. throw new RestException(404, 'No supplier proposal found');
  156. }
  157. return $obj_ret;
  158. }
  159. /**
  160. * Validate fields before create or update object
  161. *
  162. * @param array $data Array with data to verify
  163. * @return array
  164. * @throws RestException
  165. */
  166. private function _validate($data)
  167. {
  168. $propal = array();
  169. foreach (SupplierProposals::$FIELDS as $field) {
  170. if (!isset($data[$field])) {
  171. throw new RestException(400, "$field field missing");
  172. }
  173. $propal[$field] = $data[$field];
  174. }
  175. return $propal;
  176. }
  177. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  178. /**
  179. * Clean sensible object datas
  180. *
  181. * @param Object $object Object to clean
  182. * @return Object Object with cleaned properties
  183. */
  184. protected function _cleanObjectDatas($object)
  185. {
  186. // phpcs:enable
  187. $object = parent::_cleanObjectDatas($object);
  188. unset($object->name);
  189. unset($object->lastname);
  190. unset($object->firstname);
  191. unset($object->civility_id);
  192. unset($object->address);
  193. unset($object->datec);
  194. unset($object->datev);
  195. return $object;
  196. }
  197. }