Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop

This commit is contained in:
Laurent Destailleur 2017-07-07 20:16:04 +02:00
commit 0d8f382ca4
10 changed files with 175 additions and 81 deletions

View File

@ -46,7 +46,7 @@ if ($cat_id == 0) {
} }
// Security check // Security check
if (! empty($user->rights->accountancy->chartofaccount)) if (empty($user->rights->accounting->chartofaccount))
{ {
accessforbidden(); accessforbidden();
} }

View File

@ -57,7 +57,7 @@ $rowid=GETPOST('rowid','alpha');
$code=GETPOST('code','alpha'); $code=GETPOST('code','alpha');
// Security access // Security access
if (! empty($user->rights->accountancy->chartofaccount)) if (empty($user->rights->accounting->chartofaccount))
{ {
accessforbidden(); accessforbidden();
} }

View File

@ -42,7 +42,7 @@ $langs->load("salaries");
$langs->load("loan"); $langs->load("loan");
// Security check // Security check
if (! empty($user->rights->accountancy->chartofaccount)) if (empty($user->rights->accounting->chartofaccount))
{ {
accessforbidden(); accessforbidden();
} }

View File

@ -38,7 +38,7 @@ $langs->load("admin");
$langs->load("accountancy"); $langs->load("accountancy");
// Security check // Security check
if (empty($user->admin) || ! empty($user->rights->accountancy->chartofaccount)) if (empty($user->admin) && empty($user->rights->accounting->chartofaccount))
{ {
accessforbidden(); accessforbidden();
} }

View File

@ -266,7 +266,7 @@ if (! empty($user->admin))
} }
// Param a user $user->rights->accountancy->chartofaccount can access // Param a user $user->rights->accounting->chartofaccount can access
foreach ($list as $key) foreach ($list as $key)
{ {
print '<tr class="oddeven value">'; print '<tr class="oddeven value">';

View File

@ -41,7 +41,7 @@ $rowid=GETPOST('rowid','alpha');
$code=GETPOST('code','alpha'); $code=GETPOST('code','alpha');
// Security access // Security access
if (! empty($user->rights->accountancy->chartofaccount)) if (empty($user->rights->accounting->chartofaccount))
{ {
accessforbidden(); accessforbidden();
} }

View File

@ -1752,8 +1752,9 @@ function fieldList($fieldlist, $obj='', $tabname='', $context='')
} }
elseif ($fieldlist[$field] == 'type' && $tabname == MAIN_DB_PREFIX."c_actioncomm") elseif ($fieldlist[$field] == 'type' && $tabname == MAIN_DB_PREFIX."c_actioncomm")
{ {
$type = (! empty($obj->type)?$obj->type:'user'); // Check if type is different of 'user' (external module)
print '<td>'; print '<td>';
print 'user<input type="hidden" name="type" value="user">'; print $type.'<input type="hidden" name="type" value="'.$type.'">';
print '</td>'; print '</td>';
} }
elseif ($fieldlist[$field] == 'recuperableonly' || $fieldlist[$field] == 'type_cdr' || $fieldlist[$field] == 'deductible' || $fieldlist[$field] == 'category_type') { elseif ($fieldlist[$field] == 'recuperableonly' || $fieldlist[$field] == 'type_cdr' || $fieldlist[$field] == 'deductible' || $fieldlist[$field] == 'category_type') {

View File

@ -292,10 +292,11 @@ class ImportXlsx extends ModeleImports
* @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...) * @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...)
* @param int $maxfields Max number of fields to use * @param int $maxfields Max number of fields to use
* @param string $importid Import key * @param string $importid Import key
* @param array $updatekeys Array of keys to use to try to do update
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
// What is this doing here ? it is common to all imports, is should be in the parent class // What is this doing here ? it is common to all imports, is should be in the parent class
function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid) function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid,$updatekeys)
{ {
global $langs,$conf,$user; global $langs,$conf,$user;
global $thirdparty_static; // Specific to thirdparty import global $thirdparty_static; // Specific to thirdparty import
@ -326,13 +327,15 @@ class ImportXlsx extends ModeleImports
else else
{ {
$last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id) $last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id)
$updatedone = false;
$insertdone = false;
// For each table to insert, me make a separate insert // For each table to insert, me make a separate insert
foreach($objimport->array_import_tables[0] as $alias => $tablename) foreach($objimport->array_import_tables[0] as $alias => $tablename)
{ {
// Build sql request // Build sql request
$sql=''; $sql='';
$listfields=''; $listfields=array();
$listvalues=''; $listvalues=array();
$i=0; $i=0;
$errorforthistable=0; $errorforthistable=0;
@ -372,7 +375,7 @@ class ImportXlsx extends ModeleImports
// Make some tests on $newval // Make some tests on $newval
// Is it a required field ? // Is it a required field ?
if (preg_match('/\*/',$objimport->array_import_fields[0][$val]) && ((string) $newval == '')) if (preg_match('/\*/',$objimport->array_import_fields[0][$val]) && ((string) $newval==''))
{ {
$this->errors[$error]['lib']=$langs->trans('ErrorMissingMandatoryValue',$key); $this->errors[$error]['lib']=$langs->trans('ErrorMissingMandatoryValue',$key);
$this->errors[$error]['type']='NOTNULL'; $this->errors[$error]['type']='NOTNULL';
@ -399,7 +402,7 @@ class ImportXlsx extends ModeleImports
if ($isidorref == 'ref') // If value into input import file is a ref, we apply the function defined into descriptor if ($isidorref == 'ref') // If value into input import file is a ref, we apply the function defined into descriptor
{ {
$file=$objimport->array_import_convertvalue[0][$val]['classfile']; $file=(empty($objimport->array_import_convertvalue[0][$val]['classfile'])?$objimport->array_import_convertvalue[0][$val]['file']:$objimport->array_import_convertvalue[0][$val]['classfile']);
$class=$objimport->array_import_convertvalue[0][$val]['class']; $class=$objimport->array_import_convertvalue[0][$val]['class'];
$method=$objimport->array_import_convertvalue[0][$val]['method']; $method=$objimport->array_import_convertvalue[0][$val]['method'];
if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] != '') if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] != '')
@ -408,7 +411,12 @@ class ImportXlsx extends ModeleImports
} }
else else
{ {
dol_include_once($file); $resultload = dol_include_once($file);
if (empty($resultload))
{
dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
break;
}
$classinstance=new $class($this->db); $classinstance=new $class($this->db);
// Try the fetch from code or ref // Try the fetch from code or ref
call_user_func_array(array($classinstance, $method),array('', $newval)); call_user_func_array(array($classinstance, $method),array('', $newval));
@ -481,6 +489,26 @@ class ImportXlsx extends ModeleImports
} }
if (empty($newval)) $arrayrecord[($key-1)]['type']=-1; // If we get empty value, we will use "null" if (empty($newval)) $arrayrecord[($key-1)]['type']=-1; // If we get empty value, we will use "null"
} }
elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='getrefifauto')
{
$defaultref='';
// TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
$obj = empty($conf->global->PROJECT_TASK_ADDON)?'mod_task_simple':$conf->global->PROJECT_TASK_ADDON;
if (! empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT ."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php"))
{
require_once DOL_DOCUMENT_ROOT ."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
$modTask = new $obj;
$defaultref = $modTask->getNextValue(null,null);
}
if (is_numeric($defaultref) && $defaultref <= 0) $defaultref='';
$newval=$defaultref;
}
elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='numeric')
{
$newval = price2num($newval);
}
//print 'Val to use as insert is '.$newval.'<br>'; //print 'Val to use as insert is '.$newval.'<br>';
} }
@ -541,42 +569,37 @@ class ImportXlsx extends ModeleImports
} }
// Define $listfields and $listvalues to build SQL request // Define $listfields and $listvalues to build SQL request
if ($listfields) { $listfields.=', '; $listvalues.=', '; } $listfields[] = $fieldname;
$listfields.=$fieldname;
// Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert // Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert
if (empty($newval) && $arrayrecord[($key-1)]['type'] < 0) $listvalues.=($newval=='0'?$newval:"null"); if (empty($newval) && $arrayrecord[($key-1)]['type'] < 0) $listvalues[] = ($newval=='0'?$newval:"null");
elseif (empty($newval) && $arrayrecord[($key-1)]['type'] == 0) $listvalues.="''"; elseif (empty($newval) && $arrayrecord[($key-1)]['type'] == 0) $listvalues[] = "''";
else $listvalues.="'".$this->db->escape($newval)."'"; else $listvalues[] = "'".$this->db->escape($newval)."'";
} }
$i++; $i++;
} }
// We add hidden fields (but only if there is at least one field to add into table) // We add hidden fields (but only if there is at least one field to add into table)
if ($listfields && is_array($objimport->array_import_fieldshidden[0])) if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0]))
{ {
// Loop on each hidden fields to add them into listfields/listvalues // Loop on each hidden fields to add them into listfields/listvalues
foreach($objimport->array_import_fieldshidden[0] as $key => $val) foreach($objimport->array_import_fieldshidden[0] as $key => $val)
{ {
if (! preg_match('/^'.preg_quote($alias).'\./', $key)) continue; // Not a field of current table if (! preg_match('/^'.preg_quote($alias).'\./', $key)) continue; // Not a field of current table
if ($listfields) { $listfields.=', '; $listvalues.=', '; }
if ($val == 'user->id') if ($val == 'user->id')
{ {
$listfields.=preg_replace('/^'.preg_quote($alias).'\./','',$key); $listfields[] = preg_replace('/^'.preg_quote($alias).'\./','',$key);
$listvalues.=$user->id; $listvalues[] = $user->id;
} }
elseif (preg_match('/^lastrowid-/',$val)) elseif (preg_match('/^lastrowid-/',$val))
{ {
$tmp=explode('-',$val); $tmp=explode('-',$val);
$lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0; $lastinsertid=(isset($last_insert_id_array[$tmp[1]]))?$last_insert_id_array[$tmp[1]]:0;
$listfields.=preg_replace('/^'.preg_quote($alias).'\./','',$key); $keyfield = preg_replace('/^'.preg_quote($alias).'\./','',$key);
$listvalues.=$lastinsertid; $listfields[] = $keyfield;
$listvalues[] = $lastinsertid;
//print $key."-".$val."-".$listfields."-".$listvalues."<br>";exit; //print $key."-".$val."-".$listfields."-".$listvalues."<br>";exit;
} else }
{
$listfields.=preg_replace('/^'.preg_quote($alias).'\./','',$key);
$listvalues.=$val;
}
} }
} }
//print 'listfields='.$listfields.'<br>listvalues='.$listvalues.'<br>'; //print 'listfields='.$listfields.'<br>listvalues='.$listvalues.'<br>';
@ -584,49 +607,114 @@ class ImportXlsx extends ModeleImports
// If no error for this $alias/$tablename, we have a complete $listfields and $listvalues that are defined // If no error for this $alias/$tablename, we have a complete $listfields and $listvalues that are defined
if (! $errorforthistable) if (! $errorforthistable)
{ {
//print "$alias/$tablename/$listfields/$listvalues<br>"; //print "$alias/$tablename/$listfields/$listvalues<br>";
if ($listfields) if (!empty($listfields))
{ {
//var_dump($objimport->array_import_convertvalue); exit; $updatedone = false;
$insertdone = false;
if (!empty($updatekeys)) {
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
if (empty($lastinsertid)) {
$sqlSelect = 'SELECT rowid FROM '.$tablename;
// Build SQL request $data = array_combine($listfields, $listvalues);
if (empty($tablewithentity_cache[$tablename])) $where = array();
{ $filters = array();
$sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key'; foreach ($updatekeys as $key) {
if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; $col = $objimport->array_import_updatekeys[0][$key];
$sql.=') VALUES('.$listvalues.", '".$importid."'"; $key=preg_replace('/^.*\./i','',$key);
} $where[] = $key.' = '.$data[$key];
else $filters[] = $col.' = '.$data[$key];
{ }
$sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key, entity'; $sqlSelect.= ' WHERE '.implode(' AND ', $where);
if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias];
$sql.=') VALUES('.$listvalues.", '".$importid."', ".$conf->entity ;
}
if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id;
$sql.=')';
//print($sql).'<br>'; $resql=$this->db->query($sqlSelect);
dol_syslog("import_csv.modules", LOG_DEBUG); if($resql) {
$res = $this->db->fetch_object($resql);
//print '> '.join(',',$arrayrecord); if($resql->num_rows == 1) {
//print 'sql='.$sql; $lastinsertid = $res->rowid;
//print '<br>'."\n"; $last_insert_id_array[$tablename] = $lastinsertid;
} else if($resql->num_rows > 1) {
// Run insert request $this->errors[$error]['lib']=$langs->trans('MultipleRecordFoundWithTheseFilters', implode($filters, ', '));
if ($sql) $this->errors[$error]['type']='SQL';
{ $error++;
$resql=$this->db->query($sql); } else {
$last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr). // No record found with filters, insert will be tried below
if ($resql) }
{ }
//print '.'; else
{
//print 'E';
$this->errors[$error]['lib']=$this->db->lasterror();
$this->errors[$error]['type']='SQL';
$error++;
}
} }
else
if (!empty($lastinsertid)) {
// Build SQL UPDATE request
$sqlstart = 'UPDATE '.$tablename;
$data = array_combine($listfields, $listvalues);
$set = array();
foreach ($data as $key => $val) {
$set[] = $key.' = '.$val;
}
$sqlstart.= ' SET '.implode(', ', $set);
if(empty($keyfield)) $keyfield = 'rowid';
$sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
$sql = $sqlstart.$sqlend;
// Run update request
$resql=$this->db->query($sql);
if($resql) {
// No error, update has been done. $this->db->db->affected_rows can be 0 if data hasn't changed
$updatedone = true;
}
else
{
//print 'E';
$this->errors[$error]['lib']=$this->db->lasterror();
$this->errors[$error]['type']='SQL';
$error++;
}
}
}
// Update not done, we do insert
if (!$error && !$updatedone) {
// Build SQL INSERT request
$sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
$sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
if (! empty($tablewithentity_cache[$tablename])) {
$sqlstart.= ', entity';
$sqlend.= ', '.$conf->entity;
}
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
$sqlend.=', '.$user->id;
}
$sql = $sqlstart.$sqlend.')';
dol_syslog("import_xlsx.modules", LOG_DEBUG);
// Run insert request
if ($sql)
{ {
//print 'E'; $resql=$this->db->query($sql);
$this->errors[$error]['lib']=$this->db->lasterror(); if ($resql)
$this->errors[$error]['type']='SQL'; {
$error++; $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that child tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
$insertdone = true;
}
else
{
//print 'E';
$this->errors[$error]['lib']=$this->db->lasterror();
$this->errors[$error]['type']='SQL';
$error++;
}
} }
} }
} }
@ -638,6 +726,9 @@ class ImportXlsx extends ModeleImports
if ($error) break; if ($error) break;
} }
if($updatedone) $this->nbupdate++;
if($insertdone) $this->nbinsert++;
} }
return 1; return 1;

View File

@ -716,6 +716,7 @@ class Expedition extends CommonObject
if ($result < 0) { if ($result < 0) {
$error++; $error++;
$this->errors[]=$mouvS->error; $this->errors[]=$mouvS->error;
$this->errors = array_merge($this->errors, $mouvS->errors);
break; break;
} }
} }
@ -729,6 +730,7 @@ class Expedition extends CommonObject
if ($result < 0) { if ($result < 0) {
$error++; $error++;
$this->errors[]=$mouvS->error; $this->errors[]=$mouvS->error;
$this->errors = array_merge($this->errors, $mouvS->errors);
break; break;
} }
} }