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->error);
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);
211 if (empty($this->supplier_proposal->fin_validite) && !empty($this->supplier_proposal->duree_validite) && !empty($this->supplier_proposal->date_creation)) {
212 $this->supplier_proposal->fin_validite = $this->supplier_proposal->date_creation + ($this->supplier_proposal->duree_validite * 24 * 3600);
214 if (!empty($this->supplier_proposal->fin_validite)) {
215 if ($this->supplier_proposal->set_echeance(DolibarrApiAccess::$user, $this->supplier_proposal->fin_validite) < 0) {
216 throw new RestException(500, $this->supplier_proposal->error);
220 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
221 return $this->
get(
$id);
223 throw new RestException(500, $this->supplier_proposal->error);
244 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
246 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
247 throw new RestException(403);
253 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
257 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
258 $search_sale = DolibarrApiAccess::$user->id;
261 $sql =
"SELECT t.rowid";
262 $sql .=
" FROM ".MAIN_DB_PREFIX.
"supplier_proposal AS t";
263 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"supplier_proposal_extrafields AS ef ON (ef.fk_object = t.rowid)";
264 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
266 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
269 if ($search_sale && $search_sale !=
'-1') {
270 if ($search_sale == -2) {
271 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
272 } elseif ($search_sale > 0) {
273 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
281 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
286 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
288 $sql .= $this->db->order($sortfield, $sortorder);
293 $offset = $limit * $page;
295 $sql .= $this->db->plimit($limit + 1, $offset);
298 $result = $this->db->query($sql);
301 $num = $this->db->num_rows($result);
302 $min = min($num, ($limit <= 0 ? $num : $limit));
305 $obj = $this->db->fetch_object($result);
307 if ($propal_static->fetch($obj->rowid)) {
313 throw new RestException(503,
'Error when retrieving supplier proposal list : '.$this->db->lasterror());
317 if ($pagination_data) {
318 $totalsResult = $this->db->query($sqlTotals);
319 $total = $this->db->fetch_object($totalsResult)->total;
324 $obj_ret[
'data'] = $tmp;
325 $obj_ret[
'pagination'] = [
326 'total' => (int) $total,
328 'page_count' => ceil((
int) $total / $limit),
346 if ($data ===
null) {
350 foreach (SupplierProposals::$FIELDS as $field) {
351 if (!isset($data[$field])) {
352 throw new RestException(400,
"$field field missing");
354 $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)
__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