dolibarr 22.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 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
23
24
32{
36 public static $FIELDS = array(
37 'socid'
38 );
39
43 public $supplier_proposal;
44
48 public function __construct()
49 {
50 global $db;
51 $this->db = $db;
52 $this->supplier_proposal = new SupplierProposal($this->db);
53 }
54
63 public function delete($id)
64 {
65 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'supprimer')) {
66 throw new RestException(403);
67 }
68 $result = $this->supplier_proposal->fetch($id);
69 if (!$result) {
70 throw new RestException(404, 'Supplier Proposal not found');
71 }
72
73 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
74 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
75 }
76
77 if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
78 throw new RestException(500, 'Error when delete Supplier Proposal : '.$this->supplier_proposal->error);
79 }
80
81 return array(
82 'success' => array(
83 'code' => 200,
84 'message' => 'Supplier Proposal deleted'
85 )
86 );
87 }
88
99 public function get($id)
100 {
101 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'lire')) {
102 throw new RestException(403);
103 }
104
105 $result = $this->supplier_proposal->fetch($id);
106 if (!$result) {
107 throw new RestException(404, 'Supplier Proposal not found');
108 }
109
110 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
111 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
112 }
113
114 $this->supplier_proposal->fetchObjectLinked();
115 return $this->_cleanObjectDatas($this->supplier_proposal);
116 }
117
126 public function post($request_data = null)
127 {
128 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'creer')) {
129 throw new RestException(403, "Insuffisant rights");
130 }
131 // Check mandatory fields
132 $result = $this->_validate($request_data);
133
134 foreach ($request_data as $field => $value) {
135 if ($field === 'caller') {
136 // 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
137 $this->supplier_proposal->context['caller'] = $request_data['caller'];
138 continue;
139 }
140
141 $this->supplier_proposal->$field = $value;
142 }
143 /*if (isset($request_data["lines"])) {
144 $lines = array();
145 foreach ($request_data["lines"] as $line) {
146 array_push($lines, (object) $line);
147 }
148 $this->propal->lines = $lines;
149 }*/
150 if ($this->supplier_proposal->create(DolibarrApiAccess::$user) < 0) {
151 throw new RestException(500, "Error creating supplier proposal", array_merge(array($this->supplier_proposal->error), $this->supplier_proposal->errors));
152 }
153
154 return $this->supplier_proposal->id;
155 }
156
166 public function put($id, $request_data = null)
167 {
168 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'creer')) {
169 throw new RestException(403);
170 }
171
172 $result = $this->supplier_proposal->fetch($id);
173 if (!$result) {
174 throw new RestException(404, 'Supplier proposal not found');
175 }
176
177 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
178 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
179 }
180 foreach ($request_data as $field => $value) {
181 if ($field == 'id') {
182 continue;
183 }
184 if ($field === 'caller') {
185 // 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
186 $this->supplier_proposal->context['caller'] = $request_data['caller'];
187 continue;
188 }
189 if ($field == 'array_options' && is_array($value)) {
190 foreach ($value as $index => $val) {
191 $this->supplier_proposal->array_options[$index] = $val;
192 }
193 continue;
194 }
195 $this->supplier_proposal->$field = $value;
196 }
197
198 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
199 return $this->get($id);
200 } else {
201 throw new RestException(500, $this->supplier_proposal->error);
202 }
203 }
204
222 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
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 ?: $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 //this query will return total supplier proposals with the filters given
264 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
265
266 $sql .= $this->db->order($sortfield, $sortorder);
267 if ($limit) {
268 if ($page < 0) {
269 $page = 0;
270 }
271 $offset = $limit * $page;
272
273 $sql .= $this->db->plimit($limit + 1, $offset);
274 }
275
276 $result = $this->db->query($sql);
277
278 if ($result) {
279 $num = $this->db->num_rows($result);
280 $min = min($num, ($limit <= 0 ? $num : $limit));
281 $i = 0;
282 while ($i < $min) {
283 $obj = $this->db->fetch_object($result);
284 $propal_static = new SupplierProposal($this->db);
285 if ($propal_static->fetch($obj->rowid)) {
286 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($propal_static), $properties);
287 }
288 $i++;
289 }
290 } else {
291 throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
292 }
293
294 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
295 if ($pagination_data) {
296 $totalsResult = $this->db->query($sqlTotals);
297 $total = $this->db->fetch_object($totalsResult)->total;
298
299 $tmp = $obj_ret;
300 $obj_ret = [];
301
302 $obj_ret['data'] = $tmp;
303 $obj_ret['pagination'] = [
304 'total' => (int) $total,
305 'page' => $page, //count starts from 0
306 'page_count' => ceil((int) $total / $limit),
307 'limit' => $limit
308 ];
309 }
310
311 return $obj_ret;
312 }
313
314
322 private function _validate($data)
323 {
324 if ($data === null) {
325 $data = array();
326 }
327 $propal = array();
328 foreach (SupplierProposals::$FIELDS as $field) {
329 if (!isset($data[$field])) {
330 throw new RestException(400, "$field field missing");
331 }
332 $propal[$field] = $data[$field];
333 }
334 return $propal;
335 }
336
337
338 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
345 protected function _cleanObjectDatas($object)
346 {
347 // phpcs:enable
348 $object = parent::_cleanObjectDatas($object);
349
350 unset($object->name);
351 unset($object->lastname);
352 unset($object->firstname);
353 unset($object->civility_id);
354 unset($object->address);
355 unset($object->datec);
356 unset($object->datev);
357
358 return $object;
359 }
360}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
Class for API REST v1.
Definition api.class.php:33
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage price ask supplier.
put($id, $request_data=null)
Update supplier proposal general fields (won't touch lines of supplier proposal)
post($request_data=null)
Create supplier proposal (price request) object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List supplier proposals.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria