dolibarr  21.0.0-alpha
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 {
35  public static $FIELDS = array(
36  'socid'
37  );
38 
42  public $supplier_proposal;
43 
47  public function __construct()
48  {
49  global $db;
50  $this->db = $db;
51  $this->supplier_proposal = new SupplierProposal($this->db);
52  }
53 
60  public function delete($id)
61  {
62  if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'supprimer')) {
63  throw new RestException(403);
64  }
65  $result = $this->supplier_proposal->fetch($id);
66  if (!$result) {
67  throw new RestException(404, 'Supplier Proposal not found');
68  }
69 
70  if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
71  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
72  }
73 
74  if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
75  throw new RestException(500, 'Error when delete Supplier Proposal : '.$this->supplier_proposal->error);
76  }
77 
78  return array(
79  'success' => array(
80  'code' => 200,
81  'message' => 'Supplier Proposal deleted'
82  )
83  );
84  }
85 
96  public function get($id)
97  {
98  if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'lire')) {
99  throw new RestException(403);
100  }
101 
102  $result = $this->supplier_proposal->fetch($id);
103  if (!$result) {
104  throw new RestException(404, 'Supplier Proposal not found');
105  }
106 
107  if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
108  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
109  }
110 
111  $this->supplier_proposal->fetchObjectLinked();
112  return $this->_cleanObjectDatas($this->supplier_proposal);
113  }
114 
121  public function post($request_data = null)
122  {
123  if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'creer')) {
124  throw new RestException(403, "Insuffisant rights");
125  }
126  // Check mandatory fields
127  $result = $this->_validate($request_data);
128 
129  foreach ($request_data as $field => $value) {
130  if ($field === 'caller') {
131  // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
132  $this->supplier_proposal->context['caller'] = $request_data['caller'];
133  continue;
134  }
135 
136  $this->supplier_proposal->$field = $value;
137  }
138  /*if (isset($request_data["lines"])) {
139  $lines = array();
140  foreach ($request_data["lines"] as $line) {
141  array_push($lines, (object) $line);
142  }
143  $this->propal->lines = $lines;
144  }*/
145  if ($this->supplier_proposal->create(DolibarrApiAccess::$user) < 0) {
146  throw new RestException(500, "Error creating supplier proposal", array_merge(array($this->supplier_proposal->error), $this->supplier_proposal->errors));
147  }
148 
149  return $this->supplier_proposal->id;
150  }
151 
159  public function put($id, $request_data = null)
160  {
161  if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'creer')) {
162  throw new RestException(403);
163  }
164 
165  $result = $this->supplier_proposal->fetch($id);
166  if (!$result) {
167  throw new RestException(404, 'Supplier proposal not found');
168  }
169 
170  if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
171  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
172  }
173  foreach ($request_data as $field => $value) {
174  if ($field == 'id') {
175  continue;
176  }
177  if ($field === 'caller') {
178  // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
179  $this->supplier_proposal->context['caller'] = $request_data['caller'];
180  continue;
181  }
182  if ($field == 'array_options' && is_array($value)) {
183  foreach ($value as $index => $val) {
184  $this->supplier_proposal->array_options[$index] = $val;
185  }
186  continue;
187  }
188  $this->supplier_proposal->$field = $value;
189  }
190 
191  // update end of validity date
192  if (empty($this->supplier_proposal->fin_validite) && !empty($this->supplier_proposal->duree_validite) && !empty($this->supplier_proposal->date_creation)) {
193  $this->supplier_proposal->fin_validite = $this->supplier_proposal->date_creation + ($this->supplier_proposal->duree_validite * 24 * 3600);
194  }
195  if (!empty($this->supplier_proposal->fin_validite)) {
196  if ($this->supplier_proposal->set_echeance(DolibarrApiAccess::$user, $this->supplier_proposal->fin_validite) < 0) {
197  throw new RestException(500, $this->supplier_proposal->error);
198  }
199  }
200 
201  if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
202  return $this->get($id);
203  } else {
204  throw new RestException(500, $this->supplier_proposal->error);
205  }
206  }
207 
222  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
223  {
224  if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'lire')) {
225  throw new RestException(403);
226  }
227 
228  $obj_ret = array();
229 
230  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
231  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
232 
233  // If the internal user must only see his customers, force searching by him
234  $search_sale = 0;
235  if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
236  $search_sale = DolibarrApiAccess::$user->id;
237  }
238 
239  $sql = "SELECT t.rowid";
240  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal AS t";
241  $sql .= " 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
242  $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
243  if ($socids) {
244  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
245  }
246  // Search on sale representative
247  if ($search_sale && $search_sale != '-1') {
248  if ($search_sale == -2) {
249  $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
250  } elseif ($search_sale > 0) {
251  $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
252  }
253  }
254  // Add sql filters
255  if ($sqlfilters) {
256  $errormessage = '';
257  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
258  if ($errormessage) {
259  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
260  }
261  }
262 
263  $sql .= $this->db->order($sortfield, $sortorder);
264  if ($limit) {
265  if ($page < 0) {
266  $page = 0;
267  }
268  $offset = $limit * $page;
269 
270  $sql .= $this->db->plimit($limit + 1, $offset);
271  }
272 
273  $result = $this->db->query($sql);
274 
275  if ($result) {
276  $num = $this->db->num_rows($result);
277  $min = min($num, ($limit <= 0 ? $num : $limit));
278  $i = 0;
279  while ($i < $min) {
280  $obj = $this->db->fetch_object($result);
281  $propal_static = new SupplierProposal($this->db);
282  if ($propal_static->fetch($obj->rowid)) {
283  $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($propal_static), $properties);
284  }
285  $i++;
286  }
287  } else {
288  throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
289  }
290 
291  return $obj_ret;
292  }
293 
294 
302  private function _validate($data)
303  {
304  $propal = array();
305  foreach (SupplierProposals::$FIELDS as $field) {
306  if (!isset($data[$field])) {
307  throw new RestException(400, "$field field missing");
308  }
309  $propal[$field] = $data[$field];
310  }
311  return $propal;
312  }
313 
314 
315  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
322  protected function _cleanObjectDatas($object)
323  {
324  // phpcs:enable
325  $object = parent::_cleanObjectDatas($object);
326 
327  unset($object->name);
328  unset($object->lastname);
329  unset($object->firstname);
330  unset($object->civility_id);
331  unset($object->address);
332  unset($object->datec);
333  unset($object->datev);
334 
335  return $object;
336  }
337 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Class for API REST v1.
Definition: api.class.php:30
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
Definition: api.class.php:136
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:369
Class to manage price ask supplier.
put($id, $request_data=null)
Update supplier proposal general fields (won't touch lines of supplier proposal)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List supplier proposals.
post($request_data=null)
Create supplier proposal (price request) object.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria