21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
37 public static $FIELDS = array(
44 public $supplier_proposal;
64 public function delete(
$id)
66 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'supprimer')) {
67 throw new RestException(403);
69 $result = $this->supplier_proposal->fetch(
$id);
71 throw new RestException(404,
'Supplier Proposal not found');
75 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
78 if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
79 throw new RestException(500,
'Error when delete Supplier Proposal : '.$this->supplier_proposal->error);
85 'message' =>
'Supplier Proposal deleted'
102 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
103 throw new RestException(403);
106 $result = $this->supplier_proposal->fetch(
$id);
108 throw new RestException(404,
'Supplier Proposal not found');
112 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
115 $this->supplier_proposal->fetchObjectLinked();
127 public function post($request_data =
null)
129 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
130 throw new RestException(403,
"Insufficiant rights");
133 $result = $this->
_validate($request_data);
135 foreach ($request_data as $field => $value) {
136 if ($field ===
'caller') {
138 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
142 if ($field ==
'array_options' && is_array($value)) {
143 $this->supplier_proposal->fetch_optionals();
145 foreach ($value as $index => $val) {
150 $this->supplier_proposal->$field = $this->
_checkValForAPI($field, $value, $this->supplier_proposal);
161 if ($this->supplier_proposal->create(DolibarrApiAccess::$user) < 0) {
162 throw new RestException(500,
"Error creating supplier proposal", array_merge(array($this->supplier_proposal->error), $this->supplier_proposal->errors));
165 return $this->supplier_proposal->id;
177 public function put(
$id, $request_data =
null)
179 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
180 throw new RestException(403);
183 $result = $this->supplier_proposal->fetch(
$id);
185 throw new RestException(404,
'Supplier proposal not found');
189 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
191 foreach ($request_data as $field => $value) {
192 if ($field ==
'id') {
195 if ($field ===
'caller') {
197 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
200 if ($field ==
'array_options' && is_array($value)) {
201 foreach ($value as $index => $val) {
206 $this->supplier_proposal->$field = $this->
_checkValForAPI($field, $value, $this->supplier_proposal);
210 if (empty($this->supplier_proposal->fin_validite) && !empty($this->supplier_proposal->duree_validite) && !empty($this->supplier_proposal->date_creation)) {
211 $this->supplier_proposal->fin_validite = $this->supplier_proposal->date_creation + ($this->supplier_proposal->duree_validite * 24 * 3600);
213 if (!empty($this->supplier_proposal->fin_validite)) {
214 if ($this->supplier_proposal->set_echeance(DolibarrApiAccess::$user, $this->supplier_proposal->fin_validite) < 0) {
215 throw new RestException(500, $this->supplier_proposal->error);
219 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
220 return $this->
get(
$id);
222 throw new RestException(500, $this->supplier_proposal->error);
243 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
245 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
246 throw new RestException(403);
252 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
256 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
257 $search_sale = DolibarrApiAccess::$user->id;
260 $sql =
"SELECT t.rowid";
261 $sql .=
" FROM ".MAIN_DB_PREFIX.
"supplier_proposal AS t";
262 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"supplier_proposal_extrafields AS ef ON (ef.fk_object = t.rowid)";
263 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
265 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
268 if ($search_sale && $search_sale !=
'-1') {
269 if ($search_sale == -2) {
270 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
271 } elseif ($search_sale > 0) {
272 $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).
")";
278 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
280 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
285 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
287 $sql .= $this->db->order($sortfield, $sortorder);
292 $offset = $limit * $page;
294 $sql .= $this->db->plimit($limit + 1, $offset);
297 $result = $this->db->query($sql);
300 $num = $this->db->num_rows($result);
301 $min = min($num, ($limit <= 0 ? $num : $limit));
304 $obj = $this->db->fetch_object($result);
306 if ($propal_static->fetch($obj->rowid)) {
312 throw new RestException(503,
'Error when retrieving supplier proposal list : '.$this->db->lasterror());
316 if ($pagination_data) {
317 $totalsResult = $this->db->query($sqlTotals);
318 $total = $this->db->fetch_object($totalsResult)->total;
323 $obj_ret[
'data'] = $tmp;
324 $obj_ret[
'pagination'] = [
325 'total' => (int) $total,
327 'page_count' => ceil((
int) $total / $limit),
345 if ($data ===
null) {
349 foreach (SupplierProposals::$FIELDS as $field) {
350 if (!isset($data[$field])) {
351 throw new RestException(400,
"$field field missing");
353 $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.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage price ask supplier.
put($id, $request_data=null)
Update supplier proposal general fields (won't touch lines of supplier proposal)
__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.