21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
38 public static $FIELDS = array(
45 public $supplier_proposal;
65 public function delete(
$id)
67 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'supprimer')) {
68 throw new RestException(403);
70 $result = $this->supplier_proposal->fetch(
$id);
72 throw new RestException(404,
'Supplier Proposal not found');
76 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
80 throw new RestException(500,
'Error when delete Supplier Proposal : '.$this->supplier_proposal->errorsToString());
86 'message' =>
'Supplier Proposal deleted'
103 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
104 throw new RestException(403);
107 $result = $this->supplier_proposal->fetch(
$id);
109 throw new RestException(404,
'Supplier Proposal not found');
113 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
116 $this->supplier_proposal->fetchObjectLinked();
128 public function post($request_data =
null)
130 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
131 throw new RestException(403,
"Insufficiant rights");
134 $result = $this->
_validate($request_data);
136 foreach ($request_data as $field => $value) {
137 if ($field ===
'caller') {
139 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
143 if ($field ==
'array_options' && is_array($value)) {
144 $this->supplier_proposal->fetch_optionals();
146 foreach ($value as $index => $val) {
151 $this->supplier_proposal->$field = $this->
_checkValForAPI($field, $value, $this->supplier_proposal);
162 if ($this->supplier_proposal->create(DolibarrApiAccess::$user) < 0) {
163 throw new RestException(500,
"Error creating supplier proposal", array_merge(array($this->supplier_proposal->error), $this->supplier_proposal->errors));
166 return $this->supplier_proposal->id;
178 public function put(
$id, $request_data =
null)
180 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
181 throw new RestException(403);
184 $result = $this->supplier_proposal->fetch(
$id);
186 throw new RestException(404,
'Supplier proposal not found');
190 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
192 foreach ($request_data as $field => $value) {
193 if ($field ==
'id') {
196 if ($field ===
'caller') {
198 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
201 if ($field ==
'array_options' && is_array($value)) {
202 foreach ($value as $index => $val) {
207 $this->supplier_proposal->$field = $this->
_checkValForAPI($field, $value, $this->supplier_proposal);
210 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
211 return $this->
get(
$id);
213 throw new RestException(500, $this->supplier_proposal->errorsToString());
235 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
236 throw new RestException(403);
239 $result = $this->supplier_proposal->fetch(
$id);
241 throw new RestException(404,
'Supplier proposal not found');
245 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
248 $request_data = (object) $request_data;
250 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
251 $request_data->label =
sanitizeVal($request_data->label);
253 $updateRes = $this->supplier_proposal->addline(
255 $request_data->subprice,
257 $request_data->tva_tx,
258 $request_data->localtax1_tx,
259 $request_data->localtax2_tx,
260 $request_data->fk_product,
261 $request_data->remise_percent,
262 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
263 $request_data->subprice,
264 $request_data->info_bits,
265 $request_data->product_type,
267 $request_data->special_code,
268 $request_data->fk_parent_line,
269 $request_data->fk_fournprice,
270 $request_data->pa_ht,
271 $request_data->label,
272 $request_data->array_options,
273 $request_data->ref_fourn,
274 $request_data->fk_unit,
275 $request_data->origin,
276 $request_data->origin_id,
277 $request_data->multicurrency_subprice,
278 $request_data->date_start,
279 $request_data->date_end
282 if ($updateRes > 0) {
285 throw new RestException(400, $this->supplier_proposal->errorsToString());
306 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
308 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
309 throw new RestException(403);
315 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
319 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
320 $search_sale = DolibarrApiAccess::$user->id;
323 $sql =
"SELECT t.rowid";
324 $sql .=
" FROM ".MAIN_DB_PREFIX.
"supplier_proposal AS t";
325 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"supplier_proposal_extrafields AS ef ON (ef.fk_object = t.rowid)";
326 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
328 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
331 if ($search_sale && $search_sale !=
'-1') {
332 if ($search_sale == -2) {
333 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
334 } elseif ($search_sale > 0) {
335 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
343 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
348 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
350 $sql .= $this->db->order($sortfield, $sortorder);
355 $offset = $limit * $page;
357 $sql .= $this->db->plimit($limit + 1, $offset);
360 $result = $this->db->query($sql);
363 $num = $this->db->num_rows($result);
364 $min = min($num, ($limit <= 0 ? $num : $limit));
367 $obj = $this->db->fetch_object($result);
369 if ($propal_static->fetch($obj->rowid)) {
375 throw new RestException(503,
'Error when retrieving supplier proposal list : '.$this->db->lasterror());
379 if ($pagination_data) {
380 $totalsResult = $this->db->query($sqlTotals);
381 $total = $this->db->fetch_object($totalsResult)->total;
386 $obj_ret[
'data'] = $tmp;
387 $obj_ret[
'pagination'] = [
388 'total' => (int) $total,
390 'page_count' => ceil((
int) $total / $limit),
408 if ($data ===
null) {
412 foreach (SupplierProposals::$FIELDS as $field) {
413 if (!isset($data[$field])) {
414 throw new RestException(400,
"$field field missing");
416 $propal[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
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)
postLine($id, $request_data=null)
Add a line to a supplier proposal (price request)
__construct()
Constructor.
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, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.