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 // update end of validity date
199 if (empty($this->supplier_proposal->fin_validite) && !empty($this->supplier_proposal->duree_validite) && !empty($this->supplier_proposal->date_creation)) {
200 $this->supplier_proposal->fin_validite = $this->supplier_proposal->date_creation + ($this->supplier_proposal->duree_validite * 24 * 3600);
201 }
202 if (!empty($this->supplier_proposal->fin_validite)) {
203 if ($this->supplier_proposal->set_echeance(DolibarrApiAccess::$user, $this->supplier_proposal->fin_validite) < 0) {
204 throw new RestException(500, $this->supplier_proposal->error);
205 }
206 }
207
208 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
209 return $this->get($id);
210 } else {
211 throw new RestException(500, $this->supplier_proposal->error);
212 }
213 }
214
232 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
233 {
234 if (!DolibarrApiAccess::$user->hasRight('supplier_proposal', 'lire')) {
235 throw new RestException(403);
236 }
237
238 $obj_ret = array();
239
240 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
241 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
242
243 // If the internal user must only see his customers, force searching by him
244 $search_sale = 0;
245 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
246 $search_sale = DolibarrApiAccess::$user->id;
247 }
248
249 $sql = "SELECT t.rowid";
250 $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal AS t";
251 $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
252 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
253 if ($socids) {
254 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
255 }
256 // Search on sale representative
257 if ($search_sale && $search_sale != '-1') {
258 if ($search_sale == -2) {
259 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
260 } elseif ($search_sale > 0) {
261 $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).")";
262 }
263 }
264 // Add sql filters
265 if ($sqlfilters) {
266 $errormessage = '';
267 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
268 if ($errormessage) {
269 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
270 }
271 }
272
273 //this query will return total supplier proposals with the filters given
274 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
275
276 $sql .= $this->db->order($sortfield, $sortorder);
277 if ($limit) {
278 if ($page < 0) {
279 $page = 0;
280 }
281 $offset = $limit * $page;
282
283 $sql .= $this->db->plimit($limit + 1, $offset);
284 }
285
286 $result = $this->db->query($sql);
287
288 if ($result) {
289 $num = $this->db->num_rows($result);
290 $min = min($num, ($limit <= 0 ? $num : $limit));
291 $i = 0;
292 while ($i < $min) {
293 $obj = $this->db->fetch_object($result);
294 $propal_static = new SupplierProposal($this->db);
295 if ($propal_static->fetch($obj->rowid)) {
296 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($propal_static), $properties);
297 }
298 $i++;
299 }
300 } else {
301 throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
302 }
303
304 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
305 if ($pagination_data) {
306 $totalsResult = $this->db->query($sqlTotals);
307 $total = $this->db->fetch_object($totalsResult)->total;
308
309 $tmp = $obj_ret;
310 $obj_ret = [];
311
312 $obj_ret['data'] = $tmp;
313 $obj_ret['pagination'] = [
314 'total' => (int) $total,
315 'page' => $page, //count starts from 0
316 'page_count' => ceil((int) $total / $limit),
317 'limit' => $limit
318 ];
319 }
320
321 return $obj_ret;
322 }
323
324
332 private function _validate($data)
333 {
334 if ($data === null) {
335 $data = array();
336 }
337 $propal = array();
338 foreach (SupplierProposals::$FIELDS as $field) {
339 if (!isset($data[$field])) {
340 throw new RestException(400, "$field field missing");
341 }
342 $propal[$field] = $data[$field];
343 }
344 return $propal;
345 }
346
347
348 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
355 protected function _cleanObjectDatas($object)
356 {
357 // phpcs:enable
358 $object = parent::_cleanObjectDatas($object);
359
360 unset($object->name);
361 unset($object->lastname);
362 unset($object->firstname);
363 unset($object->civility_id);
364 unset($object->address);
365 unset($object->datec);
366 unset($object->datev);
367
368 return $object;
369 }
370}
$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