diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index 529bb20a89b..835ad698a1b 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -398,7 +398,7 @@ abstract class CommonObject
$lastname=$this->lastname;
$firstname=$this->firstname;
if (empty($lastname)) $lastname=(isset($this->lastname)?$this->lastname:(isset($this->name)?$this->name:(isset($this->nom)?$this->nom:(isset($this->societe)?$this->societe:(isset($this->company)?$this->company:'')))));
-
+
$ret='';
if ($option && $this->civility_id)
{
@@ -597,11 +597,11 @@ abstract class CommonObject
dol_syslog("CODE_NOT_VALID_FOR_THIS_ELEMENT");
return -3;
}
-
+
$datecreate = dol_now();
$this->db->begin();
-
+
// Insertion dans la base
$sql = "INSERT INTO ".MAIN_DB_PREFIX."element_contact";
$sql.= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) ";
@@ -1230,19 +1230,19 @@ abstract class CommonObject
function setValueFrom($field, $value, $table='', $id=null, $format='', $id_field='', $fuser=null, $trigkey='')
{
global $user,$langs,$conf;
-
+
if (empty($table)) $table=$this->table_element;
if (empty($id)) $id=$this->id;
if (empty($format)) $format='text';
if (empty($id_field)) $id_field='rowid';
$error=0;
-
+
$this->db->begin();
// Special case
if ($table == 'product' && $field == 'note_private') $field='note';
-
+
$sql = "UPDATE ".MAIN_DB_PREFIX.$table." SET ";
if ($format == 'text') $sql.= $field." = '".$this->db->escape($value)."'";
else if ($format == 'int') $sql.= $field." = ".$this->db->escape($value);
@@ -1284,7 +1284,7 @@ abstract class CommonObject
/**
* Load properties id_previous and id_next
*
- * @param string $filter Optional filter
+ * @param string $filter Optional filter. Example: " AND (t.field1 = 'aa' OR t.field2 = 'bb')"
* @param int $fieldid Name of field to use for the select MAX and MIN
* @param int $nodbprefix Do not include DB prefix to forge table name
* @return int <0 if KO, >0 if OK
@@ -1310,11 +1310,15 @@ abstract class CommonObject
if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON ".$alias.".rowid = sc.fk_soc";
$sql.= " WHERE te.".$fieldid." < '".$this->db->escape($this->ref)."'"; // ->ref must always be defined (set to id if field does not exists)
if (empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir) $sql.= " AND sc.fk_user = " .$user->id;
- if (! empty($filter)) $sql.=" AND ".$filter;
+ if (! empty($filter))
+ {
+ if (! preg_match('/^\s*AND/i', $filter)) $sql.=" AND "; // For backward compatibility
+ $sql.=$filter;
+ }
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 2 || ($this->element != 'societe' && empty($this->isnolinkedbythird) && !$user->rights->societe->client->voir)) $sql.= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to entity
if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql.= ' AND te.entity IN ('.getEntity($this->element, 1).')';
- //print $sql."
";
+ //print $filter.' '.$sql."
";
$result = $this->db->query($sql);
if (! $result)
{
@@ -1518,7 +1522,7 @@ abstract class CommonObject
if($mode == 1) {
$line->subprice = 0;
}
-
+
switch ($this->element) {
case 'propal':
$this->updateline($line->id, $line->subprice, $line->qty, $line->remise_percent, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->desc, 'HT', $line->info_bits, $line->special_code, $line->fk_parent_line, $line->skip_update_total, $line->fk_fournprice, $line->pa_ht, $line->label, $line->product_type, $line->date_start, $line->date_end, $line->array_options, $line->fk_unit, $line->multicurrency_subprice);
@@ -2154,7 +2158,7 @@ abstract class CommonObject
// Special cas
//var_dump($this->table_element);exit;
if ($this->table_element == 'product') $suffix='';
-
+
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET note".$suffix." = ".(!empty($note)?("'".$this->db->escape($note)."'"):"NULL");
$sql.= " WHERE rowid =". $this->id;
@@ -2164,7 +2168,7 @@ abstract class CommonObject
{
if ($suffix == '_public') $this->note_public = $note;
else if ($suffix == '_private') $this->note_private = $note;
- else
+ else
{
$this->note = $note; // deprecated
$this->note_private = $note;
@@ -2413,7 +2417,7 @@ abstract class CommonObject
// Special case
if ($origin == 'order') $origin='commande';
if ($origin == 'invoice') $origin='facture';
-
+
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."element_element (";
@@ -2446,10 +2450,10 @@ abstract class CommonObject
* Fetch array of objects linked to current object. Links are loaded into this->linkedObjects array and this->linkedObjectsIds
* Possible usage for parameters:
* - all parameters empty -> we look all link to current object (current object can be source or target)
- * - source id+type -> will get target list linked to source
- * - target id+type -> will get source list linked to target
- * - source id+type + target type -> will get target list of the type
- * - target id+type + target source -> will get source list of the type
+ * - source id+type -> will get target list linked to source
+ * - target id+type -> will get source list linked to target
+ * - source id+type + target type -> will get target list of the type
+ * - target id+type + target source -> will get source list of the type
*
* @param int $sourceid Object source id (if not defined, id of object)
* @param string $sourcetype Object source type (if not defined, element name of object)
@@ -2902,7 +2906,7 @@ abstract class CommonObject
function isObjectUsed($id=0)
{
if (empty($id)) $id=$this->id;
-
+
// Check parameters
if (! isset($this->childtables) || ! is_array($this->childtables) || count($this->childtables) == 0)
{
@@ -3019,12 +3023,12 @@ abstract class CommonObject
foreach ($this->lines as $line)
{
- if (isset($line->qty_asked))
+ if (isset($line->qty_asked))
{
if (empty($totalOrdered)) $totalOrdered=0; // Avoid warning because $totalOrdered is ''
$totalOrdered+=$line->qty_asked; // defined for shipment only
}
- if (isset($line->qty_shipped))
+ if (isset($line->qty_shipped))
{
if (empty($totalToShip)) $totalToShip=0; // Avoid warning because $totalToShip is ''
$totalToShip+=$line->qty_shipped; // defined for shipment only
@@ -3052,7 +3056,7 @@ abstract class CommonObject
if (empty($totalWeight)) $totalWeight=0; // Avoid warning because $totalWeight is ''
if (empty($totalVolume)) $totalVolume=0; // Avoid warning because $totalVolume is ''
-
+
//var_dump($line->volume_units);
if ($weight_units < 50) // >50 means a standard unit (power of 10 of official unit), > 50 means an exotic unit (like inch)
{
@@ -3369,9 +3373,9 @@ abstract class CommonObject
if ($conf->global->MARGIN_TYPE == "1")
print '