diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 8a4ff80f7134403991a70759b254bcabef8c7a54..2744c3eec4d77d6571da5d8a36ee8ac7a9605776 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -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(); diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 5c4d1d0e131c011cd1c92803e34c4786e74e80aa..dd5bb59c678f689ffd21d908d0c8ac0d4b4a8a45 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -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]; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 8a2907c3522c051b5b58e13f061eb944eaff69a5..65736e060974dec0e86f690a580b0d499c757c0e 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -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(); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index f6e5e2e815e566f82dbc9960d56e58bff803405f..ec8a9be586b8afecf69c17793b685edc7a170ccb 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -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'; diff --git a/htdocs/core/lib/geturl.lib.php b/htdocs/core/lib/geturl.lib.php index 15604c0ce5d20580570e52d317e355d676ff631a..8a9ac1385bea57136b91c8e6700b67c2ab9204a9 100644 --- a/htdocs/core/lib/geturl.lib.php +++ b/htdocs/core/lib/geturl.lib.php @@ -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...) diff --git a/htdocs/fichinter/class/fichinter.class.php b/htdocs/fichinter/class/fichinter.class.php index bae211525eaa981b56109fedb67544f146482ec2..d7bb50682fe1e92d4f0da1e83e04bc232f3e3078 100644 --- a/htdocs/fichinter/class/fichinter.class.php +++ b/htdocs/fichinter/class/fichinter.class.php @@ -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(); diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 50ca2d1b6c45f7328075ff7a492c0e2e87defe62..ae1c0f2f43cec108c80c1199924b9620f9076988 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -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 * diff --git a/htdocs/product/liste.php b/htdocs/product/liste.php index 36904eb03787179408fda1b7c36358e4ff7da463..821686b026cc9b63ac2120d34e762ccef44130e7 100644 --- a/htdocs/product/liste.php +++ b/htdocs/product/liste.php @@ -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)) diff --git a/htdocs/product/reassort.php b/htdocs/product/reassort.php index c185eeaf38947dff81c8e1520229097c1775b117..4522304b8870f58850f48e4ed7f9cad9b3dadc2c 100644 --- a/htdocs/product/reassort.php +++ b/htdocs/product/reassort.php @@ -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)) diff --git a/test/phpunit/CommandeFournisseurTest.php b/test/phpunit/CommandeFournisseurTest.php index 74f048dece834bb139011b2bbc7f13fb626e2c82..2c83b4fdfc37eee419d0d07b5e4c07b2aea459a1 100644 --- a/test/phpunit/CommandeFournisseurTest.php +++ b/test/phpunit/CommandeFournisseurTest.php @@ -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; - } } ?> \ No newline at end of file diff --git a/test/phpunit/CommandeTest.php b/test/phpunit/CommandeTest.php index c77dd39d0c21a59e8b6df08002a8f23d57abd199..451e886a7614a5b19176a745307047a364273eae 100644 --- a/test/phpunit/CommandeTest.php +++ b/test/phpunit/CommandeTest.php @@ -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; - } } ?> \ No newline at end of file diff --git a/test/phpunit/CommonObjectTest.php b/test/phpunit/CommonObjectTest.php index a01982508e32547e7c376a68cf5ab21d67738364..cbdde31e14d9c56ecb82553198681330d4550b6b 100644 --- a/test/phpunit/CommonObjectTest.php +++ b/test/phpunit/CommonObjectTest.php @@ -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 * diff --git a/test/phpunit/ContratTest.php b/test/phpunit/ContratTest.php index 2c9eaf6dc9d2a3234c82796182c19f858d771a56..c42d62cd3b162cea257f696303e4720eadbc31ce 100644 --- a/test/phpunit/ContratTest.php +++ b/test/phpunit/ContratTest.php @@ -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; - } - } diff --git a/test/phpunit/FactureFournisseurTest.php b/test/phpunit/FactureFournisseurTest.php index 7fe3dbd32146c52a38f4d94e0ccddf64a84ff7e9..2cacb073b60d2011d54e6f5c7299238d17b825a7 100644 --- a/test/phpunit/FactureFournisseurTest.php +++ b/test/phpunit/FactureFournisseurTest.php @@ -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; - }*/ } ?> \ No newline at end of file diff --git a/test/phpunit/ProjectTest.php b/test/phpunit/ProjectTest.php index d9a5d8d09b4283051e3db9d050ea890d6dbda9b3..9ed50eb247541b8bcb0ef420101cc8431aa99c49 100644 --- a/test/phpunit/ProjectTest.php +++ b/test/phpunit/ProjectTest.php @@ -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; - } - } ?> \ No newline at end of file diff --git a/test/phpunit/PropalTest.php b/test/phpunit/PropalTest.php index 772a792e367615509b6a8d3ede7223151b26ed51..805ee7f908a550737203c7634693dd8491f57298 100644 --- a/test/phpunit/PropalTest.php +++ b/test/phpunit/PropalTest.php @@ -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; - } - } ?> \ No newline at end of file