20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
36 public static $FIELDS = array(
43 public $supplier_proposal;
63 public function delete(
$id)
65 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'supprimer')) {
66 throw new RestException(403);
68 $result = $this->supplier_proposal->fetch(
$id);
70 throw new RestException(404,
'Supplier Proposal not found');
74 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
77 if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
78 throw new RestException(500,
'Error when delete Supplier Proposal : '.$this->supplier_proposal->error);
84 'message' =>
'Supplier Proposal deleted'
99 public function get(
$id)
101 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
102 throw new RestException(403);
105 $result = $this->supplier_proposal->fetch(
$id);
107 throw new RestException(404,
'Supplier Proposal not found');
111 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
114 $this->supplier_proposal->fetchObjectLinked();
126 public function post($request_data =
null)
128 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
129 throw new RestException(403,
"Insuffisant rights");
132 $result = $this->
_validate($request_data);
134 foreach ($request_data as $field => $value) {
135 if ($field ===
'caller') {
137 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
141 $this->supplier_proposal->$field = $value;
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));
154 return $this->supplier_proposal->id;
166 public function put(
$id, $request_data =
null)
168 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
169 throw new RestException(403);
172 $result = $this->supplier_proposal->fetch(
$id);
174 throw new RestException(404,
'Supplier proposal not found');
178 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
180 foreach ($request_data as $field => $value) {
181 if ($field ==
'id') {
184 if ($field ===
'caller') {
186 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
189 if ($field ==
'array_options' && is_array($value)) {
190 foreach ($value as $index => $val) {
191 $this->supplier_proposal->array_options[$index] = $val;
195 $this->supplier_proposal->$field = $value;
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);
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);
208 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
209 return $this->
get(
$id);
211 throw new RestException(500, $this->supplier_proposal->error);
232 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
234 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
235 throw new RestException(403);
241 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
245 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
246 $search_sale = DolibarrApiAccess::$user->id;
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)";
252 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
254 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
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).
")";
269 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
274 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
276 $sql .= $this->db->order($sortfield, $sortorder);
281 $offset = $limit * $page;
283 $sql .= $this->db->plimit($limit + 1, $offset);
286 $result = $this->db->query($sql);
289 $num = $this->db->num_rows($result);
290 $min = min($num, ($limit <= 0 ? $num : $limit));
293 $obj = $this->db->fetch_object($result);
295 if ($propal_static->fetch($obj->rowid)) {
301 throw new RestException(503,
'Error when retrieving supplier proposal list : '.$this->db->lasterror());
305 if ($pagination_data) {
306 $totalsResult = $this->db->query($sqlTotals);
307 $total = $this->db->fetch_object($totalsResult)->total;
312 $obj_ret[
'data'] = $tmp;
313 $obj_ret[
'pagination'] = [
314 'total' => (int) $total,
316 'page_count' => ceil((
int) $total / $limit),
334 if ($data ===
null) {
338 foreach (SupplierProposals::$FIELDS as $field) {
339 if (!isset($data[$field])) {
340 throw new RestException(400,
"$field field missing");
342 $propal[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_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)
__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.
_validate($data)
Validate fields before create or update object.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria