dolibarr 23.0.4
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 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use Luracast\Restler\RestException;
22
23require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
24
25
33{
37 public static $FIELDS = array(
38 'socid'
39 );
40
44 public $supplier_proposal;
45
49 public function __construct()
50 {
51 global $db;
52 $this->db = $db;
53 $this->supplier_proposal = new SupplierProposal($this->db);
54 }
55
64 public function delete($id)
65 {
66 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'supprimer')) {
67 throw new RestException(403);
68 }
69 $result = $this->supplier_proposal->fetch($id);
70 if (!$result) {
71 throw new RestException(404, 'Supplier Proposal not found');
72 }
73
74 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
75 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
76 }
77
78 if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
79 throw new RestException(500, 'Error when delete Supplier Proposal : '.$this->supplier_proposal->error);
80 }
81
82 return array(
83 'success' => array(
84 'code' => 200,
85 'message' => 'Supplier Proposal deleted'
86 )
87 );
88 }
89
100 public function get($id)
101 {
102 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'lire')) {
103 throw new RestException(403);
104 }
105
106 $result = $this->supplier_proposal->fetch($id);
107 if (!$result) {
108 throw new RestException(404, 'Supplier Proposal not found');
109 }
110
111 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
112 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
113 }
114
115 $this->supplier_proposal->fetchObjectLinked();
116 return $this->_cleanObjectDatas($this->supplier_proposal);
117 }
118
127 public function post($request_data = null)
128 {
129 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'creer')) {
130 throw new RestException(403, "Insufficiant rights");
131 }
132 // Check mandatory fields
133 $result = $this->_validate($request_data);
134
135 foreach ($request_data as $field => $value) {
136 if ($field === 'caller') {
137 // 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
138 $this->supplier_proposal->context['caller'] = $request_data['caller'];
139 continue;
140 }
141
142 $this->supplier_proposal->$field = $value;
143 }
144 /*if (isset($request_data["lines"])) {
145 $lines = array();
146 foreach ($request_data["lines"] as $line) {
147 array_push($lines, (object) $line);
148 }
149 $this->propal->lines = $lines;
150 }*/
151 if ($this->supplier_proposal->create(DolibarrApiAccess::$user) < 0) {
152 throw new RestException(500, "Error creating supplier proposal", array_merge(array($this->supplier_proposal->error), $this->supplier_proposal->errors));
153 }
154
155 return $this->supplier_proposal->id;
156 }
157
167 public function put($id, $request_data = null)
168 {
169 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'creer')) {
170 throw new RestException(403);
171 }
172
173 $result = $this->supplier_proposal->fetch($id);
174 if (!$result) {
175 throw new RestException(404, 'Supplier proposal not found');
176 }
177
178 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
179 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
180 }
181 foreach ($request_data as $field => $value) {
182 if ($field == 'id') {
183 continue;
184 }
185 if ($field === 'caller') {
186 // 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
187 $this->supplier_proposal->context['caller'] = $request_data['caller'];
188 continue;
189 }
190 if ($field == 'array_options' && is_array($value)) {
191 foreach ($value as $index => $val) {
192 $this->supplier_proposal->array_options[$index] = $val;
193 }
194 continue;
195 }
196 $this->supplier_proposal->$field = $value;
197 }
198
199 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
200 return $this->get($id);
201 } else {
202 throw new RestException(500, $this->supplier_proposal->error);
203 }
204 }
205
223 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
224 {
225 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'lire')) {
226 throw new RestException(403);
227 }
228
229 $obj_ret = array();
230
231 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
232 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
233
234 // If the internal user must only see his customers, force searching by him
235 $search_sale = 0;
236 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
237 $search_sale = DolibarrApiAccess::$user->id;
238 }
239
240 $sql = "SELECT t.rowid";
241 $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal AS t";
242 $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
243 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
244 if ($socids) {
245 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
246 }
247 // Search on sale representative
248 if ($search_sale && $search_sale != '-1') {
249 if ($search_sale == -2) {
250 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
251 } elseif ($search_sale > 0) {
252 $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).")";
253 }
254 }
255 // Add sql filters
256 if ($sqlfilters) {
257 $errormessage = '';
258 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
259 if ($errormessage) {
260 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
261 }
262 }
263
264 //this query will return total supplier proposals with the filters given
265 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
266
267 $sql .= $this->db->order($sortfield, $sortorder);
268 if ($limit) {
269 if ($page < 0) {
270 $page = 0;
271 }
272 $offset = $limit * $page;
273
274 $sql .= $this->db->plimit($limit + 1, $offset);
275 }
276
277 $result = $this->db->query($sql);
278
279 if ($result) {
280 $num = $this->db->num_rows($result);
281 $min = min($num, ($limit <= 0 ? $num : $limit));
282 $i = 0;
283 while ($i < $min) {
284 $obj = $this->db->fetch_object($result);
285 $propal_static = new SupplierProposal($this->db);
286 if ($propal_static->fetch($obj->rowid)) {
287 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($propal_static), $properties);
288 }
289 $i++;
290 }
291 } else {
292 throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
293 }
294
295 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
296 if ($pagination_data) {
297 $totalsResult = $this->db->query($sqlTotals);
298 $total = $this->db->fetch_object($totalsResult)->total;
299
300 $tmp = $obj_ret;
301 $obj_ret = [];
302
303 $obj_ret['data'] = $tmp;
304 $obj_ret['pagination'] = [
305 'total' => (int) $total,
306 'page' => $page, //count starts from 0
307 'page_count' => ceil((int) $total / $limit),
308 'limit' => $limit
309 ];
310 }
311
312 return $obj_ret;
313 }
314
315
323 private function _validate($data)
324 {
325 if ($data === null) {
326 $data = array();
327 }
328 $propal = array();
329 foreach (SupplierProposals::$FIELDS as $field) {
330 if (!isset($data[$field])) {
331 throw new RestException(400, "$field field missing");
332 }
333 $propal[$field] = $data[$field];
334 }
335 return $propal;
336 }
337
338
339 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
349 protected function _cleanObjectDatas($object)
350 {
351 // phpcs:enable
352 $object = parent::_cleanObjectDatas($object);
353
354 unset($object->name);
355 unset($object->lastname);
356 unset($object->firstname);
357 unset($object->civility_id);
358 unset($object->address);
359 unset($object->datec);
360 unset($object->datev);
361
362 return $object;
363 }
364}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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 @phpstan-template T.
_validate($data)
Validate fields before create or update object.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria