dolibarr 19.0.3
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
19use Luracast\Restler\RestException;
20
21require_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
64 public function get($id)
65 {
66 if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
67 throw new RestException(401);
68 }
69
70 $result = $this->supplier_proposal->fetch($id);
71 if (!$result) {
72 throw new RestException(404, 'Supplier Proposal not found');
73 }
74
75 if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->supplier_proposal->id)) {
76 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77 }
78
79 $this->supplier_proposal->fetchObjectLinked();
80 return $this->_cleanObjectDatas($this->supplier_proposal);
81 }
82
97 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
98 {
99 if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
100 throw new RestException(401);
101 }
102
103 $obj_ret = array();
104
105 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
106 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
107
108 // If the internal user must only see his customers, force searching by him
109 $search_sale = 0;
110 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
111 $search_sale = DolibarrApiAccess::$user->id;
112 }
113
114 $sql = "SELECT t.rowid";
115 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
116 $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)
117 }
118 $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal AS t 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
119
120 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
121 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
122 }
123
124 $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
125 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
126 $sql .= " AND t.fk_soc = sc.fk_soc";
127 }
128 if ($socids) {
129 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
130 }
131 if ($search_sale > 0) {
132 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
133 }
134 // Insert sale filter
135 if ($search_sale > 0) {
136 $sql .= " AND sc.fk_user = ".((int) $search_sale);
137 }
138 // Add sql filters
139 if ($sqlfilters) {
140 $errormessage = '';
141 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
142 if ($errormessage) {
143 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
144 }
145 }
146
147 $sql .= $this->db->order($sortfield, $sortorder);
148 if ($limit) {
149 if ($page < 0) {
150 $page = 0;
151 }
152 $offset = $limit * $page;
153
154 $sql .= $this->db->plimit($limit + 1, $offset);
155 }
156
157 $result = $this->db->query($sql);
158
159 if ($result) {
160 $num = $this->db->num_rows($result);
161 $min = min($num, ($limit <= 0 ? $num : $limit));
162 $i = 0;
163 while ($i < $min) {
164 $obj = $this->db->fetch_object($result);
165 $propal_static = new SupplierProposal($this->db);
166 if ($propal_static->fetch($obj->rowid)) {
167 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($propal_static), $properties);
168 }
169 $i++;
170 }
171 } else {
172 throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
173 }
174
175 return $obj_ret;
176 }
177
178
186 private function _validate($data)
187 {
188 $propal = array();
189 foreach (SupplierProposals::$FIELDS as $field) {
190 if (!isset($data[$field])) {
191 throw new RestException(400, "$field field missing");
192 }
193 $propal[$field] = $data[$field];
194 }
195 return $propal;
196 }
197
198
199 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
206 protected function _cleanObjectDatas($object)
207 {
208 // phpcs:enable
209 $object = parent::_cleanObjectDatas($object);
210
211 unset($object->name);
212 unset($object->lastname);
213 unset($object->firstname);
214 unset($object->civility_id);
215 unset($object->address);
216 unset($object->datec);
217 unset($object->datev);
218
219 return $object;
220 }
221}
Class for API REST v1.
Definition api.class.php:31
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
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