dolibarr  16.0.5
api_supplier_proposals.class.php
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 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
22 
23 
31 {
32 
36  public static $FIELDS = array(
37  'socid'
38  );
39 
43  public $supplier_proposal;
44 
48  public function __construct()
49  {
50  global $db, $conf;
51  $this->db = $db;
52  $this->supplier_proposal = new SupplierProposal($this->db);
53  }
54 
65  public function get($id)
66  {
67  if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
68  throw new RestException(401);
69  }
70 
71  $result = $this->supplier_proposal->fetch($id);
72  if (!$result) {
73  throw new RestException(404, 'Supplier Proposal not found');
74  }
75 
76  if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->propal->id)) {
77  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
78  }
79 
80  $this->supplier_proposal->fetchObjectLinked();
81  return $this->_cleanObjectDatas($this->supplier_proposal);
82  }
83 
97  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
98  {
99  global $db, $conf;
100 
101  if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
102  throw new RestException(401);
103  }
104 
105  $obj_ret = array();
106 
107  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
108  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
109 
110  // If the internal user must only see his customers, force searching by him
111  $search_sale = 0;
112  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
113  $search_sale = DolibarrApiAccess::$user->id;
114  }
115 
116  $sql = "SELECT t.rowid";
117  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
118  $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)
119  }
120  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as t";
121 
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
123  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
124  }
125 
126  $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
127  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128  $sql .= " AND t.fk_soc = sc.fk_soc";
129  }
130  if ($socids) {
131  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
132  }
133  if ($search_sale > 0) {
134  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
135  }
136  // Insert sale filter
137  if ($search_sale > 0) {
138  $sql .= " AND sc.fk_user = ".((int) $search_sale);
139  }
140  // Add sql filters
141  if ($sqlfilters) {
142  $errormessage = '';
143  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
144  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
145  }
146  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
147  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
148  }
149 
150  $sql .= $this->db->order($sortfield, $sortorder);
151  if ($limit) {
152  if ($page < 0) {
153  $page = 0;
154  }
155  $offset = $limit * $page;
156 
157  $sql .= $this->db->plimit($limit + 1, $offset);
158  }
159 
160  $result = $this->db->query($sql);
161 
162  if ($result) {
163  $num = $this->db->num_rows($result);
164  $min = min($num, ($limit <= 0 ? $num : $limit));
165  $i = 0;
166  while ($i < $min) {
167  $obj = $this->db->fetch_object($result);
168  $propal_static = new SupplierProposal($this->db);
169  if ($propal_static->fetch($obj->rowid)) {
170  $obj_ret[] = $this->_cleanObjectDatas($propal_static);
171  }
172  $i++;
173  }
174  } else {
175  throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
176  }
177  if (!count($obj_ret)) {
178  throw new RestException(404, 'No supplier proposal found');
179  }
180  return $obj_ret;
181  }
182 
183 
191  private function _validate($data)
192  {
193  $propal = array();
194  foreach (SupplierProposals::$FIELDS as $field) {
195  if (!isset($data[$field])) {
196  throw new RestException(400, "$field field missing");
197  }
198  $propal[$field] = $data[$field];
199  }
200  return $propal;
201  }
202 
203 
204  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
211  protected function _cleanObjectDatas($object)
212  {
213  // phpcs:enable
214  $object = parent::_cleanObjectDatas($object);
215 
216  unset($object->name);
217  unset($object->lastname);
218  unset($object->firstname);
219  unset($object->civility_id);
220  unset($object->address);
221  unset($object->datec);
222  unset($object->datev);
223 
224  return $object;
225  }
226 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Supplierproposals\__construct
__construct()
Constructor.
Definition: api_supplier_proposals.class.php:48
Supplierproposals\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_supplier_proposals.class.php:191
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Supplierproposals\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List supplier proposals.
Definition: api_supplier_proposals.class.php:97
Supplierproposals
Definition: api_supplier_proposals.class.php:30
SupplierProposal
Class to manage price ask supplier.
Definition: supplier_proposal.class.php:50
Supplierproposals\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_supplier_proposals.class.php:211