19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/supplier_proposal/class/supplier_proposal.class.php';
35 public static $FIELDS = array(
42 public $supplier_proposal;
60 public function delete($id)
62 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'supprimer')) {
63 throw new RestException(403);
65 $result = $this->supplier_proposal->fetch($id);
67 throw new RestException(404,
'Supplier Proposal not found');
71 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
74 if (!$this->supplier_proposal->delete(DolibarrApiAccess::$user)) {
75 throw new RestException(500,
'Error when delete Supplier Proposal : '.$this->supplier_proposal->error);
81 'message' =>
'Supplier Proposal deleted'
96 public function get($id)
98 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
99 throw new RestException(403);
102 $result = $this->supplier_proposal->fetch($id);
104 throw new RestException(404,
'Supplier Proposal not found');
108 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
111 $this->supplier_proposal->fetchObjectLinked();
121 public function post($request_data =
null)
123 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
124 throw new RestException(403,
"Insuffisant rights");
127 $result = $this->
_validate($request_data);
129 foreach ($request_data as $field => $value) {
130 if ($field ===
'caller') {
132 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
136 $this->supplier_proposal->$field = $value;
145 if ($this->supplier_proposal->create(DolibarrApiAccess::$user) < 0) {
146 throw new RestException(500,
"Error creating supplier proposal", array_merge(array($this->supplier_proposal->error), $this->supplier_proposal->errors));
149 return $this->supplier_proposal->id;
159 public function put($id, $request_data =
null)
161 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'creer')) {
162 throw new RestException(403);
165 $result = $this->supplier_proposal->fetch($id);
167 throw new RestException(404,
'Supplier proposal not found');
171 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
173 foreach ($request_data as $field => $value) {
174 if ($field ==
'id') {
177 if ($field ===
'caller') {
179 $this->supplier_proposal->context[
'caller'] = $request_data[
'caller'];
182 if ($field ==
'array_options' && is_array($value)) {
183 foreach ($value as $index => $val) {
184 $this->supplier_proposal->array_options[$index] = $val;
188 $this->supplier_proposal->$field = $value;
192 if (empty($this->supplier_proposal->fin_validite) && !empty($this->supplier_proposal->duree_validite) && !empty($this->supplier_proposal->date_creation)) {
193 $this->supplier_proposal->fin_validite = $this->supplier_proposal->date_creation + ($this->supplier_proposal->duree_validite * 24 * 3600);
195 if (!empty($this->supplier_proposal->fin_validite)) {
196 if ($this->supplier_proposal->set_echeance(DolibarrApiAccess::$user, $this->supplier_proposal->fin_validite) < 0) {
197 throw new RestException(500, $this->supplier_proposal->error);
201 if ($this->supplier_proposal->update(DolibarrApiAccess::$user) > 0) {
202 return $this->
get($id);
204 throw new RestException(500, $this->supplier_proposal->error);
222 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
224 if (!DolibarrApiAccess::$user->hasRight(
'supplier_proposal',
'lire')) {
225 throw new RestException(403);
231 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
235 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
236 $search_sale = DolibarrApiAccess::$user->id;
239 $sql =
"SELECT t.rowid";
240 $sql .=
" FROM ".MAIN_DB_PREFIX.
"supplier_proposal AS t";
241 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"supplier_proposal_extrafields AS ef ON (ef.fk_object = t.rowid)";
242 $sql .=
' WHERE t.entity IN ('.getEntity(
'propal').
')';
244 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
247 if ($search_sale && $search_sale !=
'-1') {
248 if ($search_sale == -2) {
249 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
250 } elseif ($search_sale > 0) {
251 $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).
")";
259 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
263 $sql .= $this->db->order($sortfield, $sortorder);
268 $offset = $limit * $page;
270 $sql .= $this->db->plimit($limit + 1, $offset);
273 $result = $this->db->query($sql);
276 $num = $this->db->num_rows($result);
277 $min = min($num, ($limit <= 0 ? $num : $limit));
280 $obj = $this->db->fetch_object($result);
282 if ($propal_static->fetch($obj->rowid)) {
288 throw new RestException(503,
'Error when retrieving supplier proposal list : '.$this->db->lasterror());
305 foreach (SupplierProposals::$FIELDS as $field) {
306 if (!isset($data[$field])) {
307 throw new RestException(400,
"$field field missing");
309 $propal[$field] = $data[$field];
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)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List supplier proposals.
__construct()
Constructor.
post($request_data=null)
Create supplier proposal (price request) object.
_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