fix mos api update

This commit is contained in:
Frédéric FRANCE 2020-12-11 19:02:13 +01:00
parent 91ddcf6446
commit 5ac677e2ab
No known key found for this signature in database
GPG Key ID: 06809324E4B2ABC1

View File

@ -107,32 +107,43 @@ class Mos extends DolibarrApi
// If the internal user must only see his customers, force searching by him // If the internal user must only see his customers, force searching by him
$search_sale = 0; $search_sale = 0;
if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
$search_sale = DolibarrApiAccess::$user->id;
}
$sql = "SELECT t.rowid"; $sql = "SELECT t.rowid";
if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
$sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
}
$sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t"; $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
}
$sql .= " WHERE 1 = 1"; $sql .= " WHERE 1 = 1";
// Example of use $mode // Example of use $mode
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)"; //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)"; //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
if ($tmpobject->ismultientitymanaged) $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')'; if ($tmpobject->ismultientitymanaged) {
if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc"; $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
if ($restrictonsocid && $socid) $sql .= " AND t.fk_soc = ".$socid; }
if ($restrictonsocid && $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
$sql .= " AND t.fk_soc = sc.fk_soc";
}
if ($restrictonsocid && $socid) {
$sql .= " AND t.fk_soc = ".$socid;
}
if ($restrictonsocid && $search_sale > 0) {
$sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
}
// Insert sale filter // Insert sale filter
if ($restrictonsocid && $search_sale > 0) if ($restrictonsocid && $search_sale > 0) {
{
$sql .= " AND sc.fk_user = ".$search_sale; $sql .= " AND sc.fk_user = ".$search_sale;
} }
if ($sqlfilters) if ($sqlfilters) {
{ if (!DolibarrApi::_checkFilters($sqlfilters)) {
if (!DolibarrApi::_checkFilters($sqlfilters))
{
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
} }
$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
@ -141,8 +152,7 @@ class Mos extends DolibarrApi
$sql .= $this->db->order($sortfield, $sortorder); $sql .= $this->db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
if ($page < 0) if ($page < 0) {
{
$page = 0; $page = 0;
} }
$offset = $limit * $page; $offset = $limit * $page;
@ -151,12 +161,10 @@ class Mos extends DolibarrApi
} }
$result = $this->db->query($sql); $result = $this->db->query($sql);
if ($result) if ($result) {
{
$num = $this->db->num_rows($result); $num = $this->db->num_rows($result);
$i = 0; $i = 0;
while ($i < $num) while ($i < $num) {
{
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
$tmp_object = new Mo($this->db); $tmp_object = new Mo($this->db);
if ($tmp_object->fetch($obj->rowid)) { if ($tmp_object->fetch($obj->rowid)) {
@ -164,8 +172,7 @@ class Mos extends DolibarrApi
} }
$i++; $i++;
} }
} } else {
else {
throw new RestException(503, 'Error when retrieve MO list'); throw new RestException(503, 'Error when retrieve MO list');
} }
if (!count($obj_ret)) { if (!count($obj_ret)) {
@ -221,11 +228,13 @@ class Mos extends DolibarrApi
} }
foreach ($request_data as $field => $value) { foreach ($request_data as $field => $value) {
if ($field == 'id') continue; if ($field == 'id') {
continue;
}
$this->mo->$field = $value; $this->mo->$field = $value;
} }
if ($this->mo->update($id, DolibarrApiAccess::$user) > 0) { if ($this->mo->update(DolibarrApiAccess::$user) > 0) {
return $this->get($id); return $this->get($id);
} else { } else {
throw new RestException(500, $this->mo->error); throw new RestException(500, $this->mo->error);
@ -252,8 +261,7 @@ class Mos extends DolibarrApi
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
if (!$this->mo->delete(DolibarrApiAccess::$user)) if (!$this->mo->delete(DolibarrApiAccess::$user)) {
{
throw new RestException(500, 'Error when deleting MO : '.$this->mo->error); throw new RestException(500, 'Error when deleting MO : '.$this->mo->error);
} }
@ -317,8 +325,7 @@ class Mos extends DolibarrApi
// If object has lines, remove $db property // If object has lines, remove $db property
if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) { if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
$nboflines = count($object->lines); $nboflines = count($object->lines);
for ($i = 0; $i < $nboflines; $i++) for ($i = 0; $i < $nboflines; $i++) {
{
$this->_cleanObjectDatas($object->lines[$i]); $this->_cleanObjectDatas($object->lines[$i]);
unset($object->lines[$i]->lines); unset($object->lines[$i]->lines);
@ -341,9 +348,12 @@ class Mos extends DolibarrApi
{ {
$myobject = array(); $myobject = array();
foreach ($this->mo->fields as $field => $propfield) { foreach ($this->mo->fields as $field => $propfield) {
if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) continue; // Not a mandatory field if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
if (!isset($data[$field])) continue; // Not a mandatory field
}
if (!isset($data[$field])) {
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
}
$myobject[$field] = $data[$field]; $myobject[$field] = $data[$field];
} }
return $myobject; return $myobject;