Fix: The way we tested if a ref/id already exists was wrong. Introduce a

common static method for this. Removed verifNumRef method.
Fix: Add missing logs
This commit is contained in:
Laurent Destailleur 2014-05-13 10:12:45 +02:00
parent 3a29d7dac1
commit 69eaefc459
16 changed files with 114 additions and 254 deletions

View File

@ -673,16 +673,26 @@ class Propal extends CommonObject
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -3;
}
// Check parameters
if (! empty($this->ref)) // We check that ref is not already used
{
$result=self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
if ($result > 0)
{
$this->error='ErrorRefAlreadyExists';
dol_syslog(get_class($this)."::create ".$this->error,LOG_WARNING);
$this->db->rollback();
return -1;
}
}
if (empty($this->date))
{
$this->error="Date of proposal is required";
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -4;
}
if (! empty($this->ref))
{
$result=$this->verifyNumRef(); // Check ref is not yet used
}
$this->db->begin();

View File

@ -612,6 +612,18 @@ class Commande extends CommonOrder
dol_syslog(get_class($this)."::create user=".$user->id);
// Check parameters
if (! empty($this->ref)) // We check that ref is not already used
{
$result=self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
if ($result > 0)
{
$this->error='ErrorRefAlreadyExists';
dol_syslog(get_class($this)."::create ".$this->error,LOG_WARNING);
$this->db->rollback();
return -1;
}
}
$soc = new Societe($this->db);
$result=$soc->fetch($this->socid);
if ($result < 0)
@ -1039,7 +1051,7 @@ class Commande extends CommonOrder
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $info_bits=0, $fk_remise_except=0, $price_base_type='HT', $pu_ttc=0, $date_start='', $date_end='', $type=0, $rang=-1, $special_code=0, $fk_parent_line=0, $fk_fournprice=null, $pa_ht=0, $label='',$array_option=0)
{
global $mysoc, $conf, $langs;
$commandeid=$this->id;
dol_syslog(get_class($this)."::addline commandeid=$commandeid, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start, date_end=$date_end, type=$type", LOG_DEBUG);
@ -1086,9 +1098,9 @@ class Commande extends CommonOrder
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc);
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type,'', $localtaxes_type);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];
@ -1103,14 +1115,14 @@ class Commande extends CommonOrder
$rangmax = $this->line_max($fk_parent_line);
$rangtouse = $rangmax + 1;
}
$product_type=$type;
if (!empty($fk_product))
{
$product=new Product($this->db);
$result=$product->fetch($fk_product);
$product_type=$product->type;
if($conf->global->STOCK_MUST_BE_ENOUGH_FOR_ORDER && $product_type == 0 && $product->stock_reel < $qty) {
$this->error=$langs->trans('ErrorStockIsNotEnough');
$this->db->rollback();
@ -2303,9 +2315,9 @@ class Commande extends CommonOrder
// qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc);
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, '', $localtaxes_type);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];

View File

@ -3493,7 +3493,7 @@ class FactureLigne extends CommonInvoiceLine
}
/**
* Insert line in database
* Insert line into database
*
* @param int $notrigger 1 no triggers
* @return int <0 if KO, >0 if OK
@ -3531,7 +3531,21 @@ class FactureLigne extends CommonInvoiceLine
}
// Check parameters
if ($this->product_type < 0) return -1;
if ($this->product_type < 0)
{
$this->error='ErrorProductTypeMustBe0orMore';
return -1;
}
if (! empty($this->fk_product))
{
// Check product exists
$result=Product::isExistingObject('product', $this->fk_product);
if ($result <= 0)
{
$this->error='ErrorProductIdDoesNotExists';
return -1;
}
}
$this->db->begin();

View File

@ -53,6 +53,42 @@ abstract class CommonObject
// No constructor as it is an abstract class
/**
* Check an object id/ref exists
* If you don't need/want to instantiate object and just need to know if object exists, use this method instead of fetch
*
* @param string $element String of element ('product', 'facture', ...)
* @param int $id Id of object
* @param string $ref Ref of object to check
* @param string $ref_ext Ref ext of object to check
* @return int <0 if KO, 0 if OK but not found, >0 if OK and exists
*/
static function isExistingObject($element, $id, $ref='', $ref_ext='')
{
global $db;
$sql = "SELECT rowid, ref, ref_ext";
$sql.= " FROM ".MAIN_DB_PREFIX.$element;
if ($id > 0) $sql.= " WHERE rowid = ".$db->escape($id);
else if ($ref) $sql.= " WHERE ref = '".$db->escape($ref)."'";
else if ($ref_ext) $sql.= " WHERE ref_ext = '".$db->escape($ref_ext)."'";
else {
$error='ErrorWrongParameters';
dol_print_error(get_class()."::isExistingObject ".$error, LOG_ERR);
return -1;
}
dol_syslog(get_class()."::isExistingObject sql=".$sql);
$resql = $db->query($sql);
if ($resql)
{
$num=$db->num_rows($resql);
if ($num > 0) return 1;
else return 0;
}
return -1;
}
/**
* Method to output saved errors
*
@ -114,33 +150,6 @@ abstract class CommonObject
return dol_format_address($this, $withcountry, $sep);
}
/**
* Check if ref is used.
*
* @return int <0 if KO, 0 if not found, >0 if found
*/
function verifyNumRef()
{
global $conf;
$sql = "SELECT rowid";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE ref = '".$this->ref."'";
$sql.= " AND entity = ".$conf->entity;
dol_syslog(get_class($this)."::verifyNumRef sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
return $num;
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::verifyNumRef ".$this->error, LOG_ERR);
return -1;
}
}
/**
* Add a link between element $this->element and a contact
@ -2052,7 +2061,7 @@ abstract class CommonObject
/**
* Get special code of line
* Get special code of a line
*
* @param int $lineid Id of line
* @return int Special code
@ -3088,7 +3097,8 @@ abstract class CommonObject
* @param string $force_price True of not
* @return mixed Array with info
*/
function getMarginInfos($force_price=false) {
function getMarginInfos($force_price=false)
{
global $conf;
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';

View File

@ -26,7 +26,7 @@
*
* @param string $url URL to call.
* @param string $postorget 'POST', 'GET', 'HEAD'
* @param string $param Paraemeters of URL (x=value1&y=value2)
* @param string $param Parameters of URL (x=value1&y=value2)
* @param string $followlocation 1=Follow location, 0=Do not follow
* @param array $addheaders Array of string to add into header. Example: ('Accept: application/xrds+xml', ....)
* @return array Returns an associative array containing the response from the server array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)

View File

@ -95,36 +95,28 @@ class Fichinter extends CommonObject
dol_syslog(get_class($this)."::create ref=".$this->ref);
// Check parameters
if (! is_numeric($this->duree)) {
$this->duree = 0;
if (! empty($this->ref)) // We check that ref is not already used
{
$result=self::isExistingObject($this->element, 0, $this->ref); // Check ref is not yet used
if ($result > 0)
{
$this->error='ErrorRefAlreadyExists';
dol_syslog(get_class($this)."::create ".$this->error,LOG_WARNING);
$this->db->rollback();
return -1;
}
}
if (! is_numeric($this->duree)) $this->duree = 0;
if ($this->socid <= 0)
{
$this->error='ErrorBadParameterForFunc';
dol_syslog(get_class($this)."::create ".$this->error,LOG_ERR);
return -1;
}
// on verifie si la ref n'est pas utilisee
$soc = new Societe($this->db);
$result=$soc->fetch($this->socid);
if (! empty($this->ref))
{
$result=$this->verifyNumRef(); // Check ref is not yet used
if ($result > 0)
{
$this->error='ErrorRefAlreadyExists';
dol_syslog(get_class($this)."::create ".$this->error,LOG_WARNING);
$this->db->rollback();
return -3;
}
else if ($result < 0)
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::create ".$this->error,LOG_ERR);
$this->db->rollback();
return -2;
}
}
$now=dol_now();

View File

@ -2276,6 +2276,7 @@ class Product extends CommonObject
$sql.= " FROM ".MAIN_DB_PREFIX."product_price ";
$sql.= " WHERE fk_product = ". $fromId;
dol_syslog(get_class($this).'::clone_price sql='.$sql);
if (! $this->db->query($sql))
{
$this->db->rollback();
@ -2285,6 +2286,13 @@ class Product extends CommonObject
return 1;
}
/**
* Clone links between products
*
* @param int $fromId Product id
* @param int $toId Product id
* @return number
*/
function clone_associations($fromId, $toId)
{
$this->db->begin();
@ -2293,6 +2301,7 @@ class Product extends CommonObject
$sql.= " SELECT null, $toId, fk_product_fils, qty FROM ".MAIN_DB_PREFIX."product_association";
$sql.= " WHERE fk_product_pere = '".$fromId."'";
dol_syslog(get_class($this).'::clone_association sql='.$sql);
if (! $this->db->query($sql))
{
$this->db->rollback();
@ -2336,6 +2345,7 @@ class Product extends CommonObject
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE fk_product = ".$fromId;
dol_syslog(get_class($this).'::clone_fournisseurs sql='.$sql);
$resql=$this->db->query($sql);
if (! $resql)
{
@ -2894,42 +2904,6 @@ class Product extends CommonObject
}
}
/**
* Deplace fichier recupere sur internet (utilise pour interface avec OSC)
*
* @param string $sdir Repertoire destination finale
* @param string $file url de l'image
* @return void
*/
function add_photo_web($sdir, $file)
{
$dir = $sdir .'/'. get_exdir($this->id,2) . $this->id ."/";
$dir .= "photos/";
$dir_osencoded=dol_osencode($dir);
if (! file_exists($dir_osencoded))
{
dol_syslog("Product Create ".$dir);
dol_mkdir($dir);
}
if (file_exists($dir_osencoded))
{
// Cree fichier en taille vignette
// TODO A faire
// Cree fichier en taille origine
$content = @file_get_contents($file);
if( $content)
{
$nom = basename($file);
$im = fopen(dol_osencode($dir.$nom),'wb');
fwrite($im, $content);
fclose($im);
}
}
}
/**
* Affiche la premiere photo du produit
*

View File

@ -68,7 +68,6 @@ $limit = $conf->liste_limit;
// Get object canvas (By default, this is not defined, so standard usage of dolibarr)
//$object->getCanvas($id);
$canvas=GETPOST("canvas");
$objcanvas='';
if (! empty($canvas))

View File

@ -62,7 +62,6 @@ $search_sale = GETPOST("search_sale");
$search_categ = GETPOST("search_categ");
// Get object canvas (By default, this is not defined, so standard usage of dolibarr)
//$object->getCanvas($id);
$canvas=GETPOST("canvas");
$objcanvas='';
if (! empty($canvas))

View File

@ -136,7 +136,7 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase
$product=new ProductFournisseur($db);
$product->fetch(0,'PIDRESS');
if ($product->id <= 0) { print "\n".__METHOD__." A product with ref PIDRESS must exists into database"; die(); }
$quantity=10;
$ref_fourn='SUPPLIER_REF_PHPUNIT';
$tva_tx=19.6;
@ -337,26 +337,5 @@ class CommandeFournisseurTest extends PHPUnit_Framework_TestCase
return $result;
}
/**
* testVerifyNumRef
*
* @return void
*/
public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new CommandeFournisseur($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}
}
?>

View File

@ -264,26 +264,5 @@ class CommandeTest extends PHPUnit_Framework_TestCase
return $result;
}
/**
* testVerifyNumRef
*
* @return void
*/
public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Commande($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}
}
?>

View File

@ -115,28 +115,6 @@ class CommonObjectTest extends PHPUnit_Framework_TestCase
}
/**
* testVerifyNumRef
*
* @return void
*/
public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Commande($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}
/**
* testFetchUser
*

View File

@ -241,27 +241,4 @@ class ContratTest extends PHPUnit_Framework_TestCase
return $result;
}
/**
* testVerifyNumRef
*
* @return int
*/
public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Contrat($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}
}

View File

@ -264,24 +264,5 @@ class FactureFournisseurTest extends PHPUnit_Framework_TestCase
return $result;
}
/**
*
*/
/*public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Facture($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}*/
}
?>

View File

@ -185,7 +185,7 @@ class ProjectTest extends PHPUnit_Framework_TestCase
$this->assertLessThan($result, 0);
return $localobject;
}
/**
* testProjectOther
*
@ -232,27 +232,5 @@ class ProjectTest extends PHPUnit_Framework_TestCase
return $result;
}
/**
* testVerifyNumRef
*
* @return void
*/
public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Project($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}
}
?>

View File

@ -264,27 +264,5 @@ class PropalTest extends PHPUnit_Framework_TestCase
return $result;
}
/**
* testVerifyNumRef
*
* @return void
*/
public function testVerifyNumRef()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Propal($this->savdb);
$result=$localobject->ref='refthatdoesnotexists';
$result=$localobject->VerifyNumRef();
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, 0);
return $result;
}
}
?>