diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index c7ffdc63753..acf708005d3 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -63,13 +63,24 @@ class Categorie * Load category into memory from database * * @param int $id Id of category + * @param strin $label Label of category * @return int <0 if KO, >0 if OK */ - function fetch($id) + function fetch($id,$label='') { + global $conf; + $sql = "SELECT rowid, fk_parent, entity, label, description, fk_soc, visible, type"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie"; - $sql.= " WHERE rowid = ".$id; + if ($id) + { + $sql.= " WHERE rowid = '".$id."'"; + } + + else + { + if ($label) $sql.= " WHERE label = '".$this->db->escape($label)."' AND entity=".$conf->entity;; + } dol_syslog(get_class($this)."::fetch sql=".$sql); $resql = $this->db->query($sql); diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 1861e974576..da2493264be 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -2,6 +2,7 @@ /* Copyright (C) 2006-2012 Laurent Destailleur * Copyright (C) 2009-2012 Regis Houssin * Copyright (C) 2012 Christophe Battarel + * Copyright (C) 2012 Juanjo Menent * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -583,10 +584,19 @@ class ImportCsv extends ModeleImports //var_dump($objimport->array_import_convertvalue); exit; // Build SQL request - $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key'; - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; - $sql.=') VALUES('.$listvalues.", '".$importid."'"; - if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$user->id; + if (! tablewithentity($tablename)) + { + $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key'; + if (! empty($objimport->array_import_tables_creator[0][$alias])) $sql.=', '.$objimport->array_import_tables_creator[0][$alias]; + $sql.=') VALUES('.$listvalues.", '".$importid."'"; + } + else + { + $sql ='INSERT INTO '.$tablename.'('.$listfields.', import_key, entity'; + 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.=')'; dol_syslog("import_csv.modules sql=".$sql); @@ -637,4 +647,34 @@ function cleansep($value) return str_replace(',','/',$value); }; +/** + * Returns if a table contains entity column + * + * @param string $table Table name + * @return int 1 if table contains entity, 0 if not and -1 if error + */ +function tablewithentity($table) +{ + global $db; + $sql = "SHOW COLUMNS FROM ".$table." LIKE 'entity'"; + + $resql=$db->query($sql); + if ($resql) + { + $numrows=$db->num_rows($resql); + if ($numrows) + { + return 1; + } + else + { + return 0; + } + } + else + { + return -1; + } +} + ?> diff --git a/htdocs/core/modules/modCategorie.class.php b/htdocs/core/modules/modCategorie.class.php index cb5fe07ee5f..4f4ecba0714 100644 --- a/htdocs/core/modules/modCategorie.class.php +++ b/htdocs/core/modules/modCategorie.class.php @@ -178,6 +178,63 @@ class modCategorie extends DolibarrModules $this->import_regex_array[$r]=array('ca.type'=>'^[0|1|2|3]'); $this->import_examplevalues_array[$r]=array('ca.label'=>"Supplier Category",'ca.type'=>"1",'ca.description'=>"Imported category"); + + if (! empty($conf->product->enabled)) + { + //Products + $r++; + $this->import_code[$r]=$this->rights_class.'_'.$r; + $this->import_label[$r]="CatProdList"; // Translation key + $this->import_icon[$r]=$this->picto; + $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon + $this->import_tables_array[$r]=array('cp'=>MAIN_DB_PREFIX.'categorie_product'); + $this->import_fields_array[$r]=array('cp.fk_categorie'=>"Category*",'cp.fk_product'=>"Product*" + ); + + $this->import_convertvalue_array[$r]=array( + 'cp.fk_categorie'=>array('rule'=>'fetchidfromref','classfile'=>'/categories/class/categorie.class.php','class'=>'Categorie','method'=>'fetch','element'=>'category'), + 'cp.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'product') + ); + $this->import_examplevalues_array[$r]=array('cp.fk_categorie'=>"Imported category",'cp.fk_product'=>"PREF123456"); + } + + if (! empty($conf->societe->enabled)) + { + //Customers + $r++; + $this->import_code[$r]=$this->rights_class.'_'.$r; + $this->import_label[$r]="CatCusList"; // Translation key + $this->import_icon[$r]=$this->picto; + $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon + $this->import_tables_array[$r]=array('cs'=>MAIN_DB_PREFIX.'categorie_societe'); + $this->import_fields_array[$r]=array('cs.fk_categorie'=>"Category*",'cs.fk_societe'=>"ThirdParty*" + ); + + $this->import_convertvalue_array[$r]=array( + 'cs.fk_categorie'=>array('rule'=>'fetchidfromref','classfile'=>'/categories/class/categorie.class.php','class'=>'Categorie','method'=>'fetch','element'=>'category'), + 'cs.fk_societe'=>array('rule'=>'fetchidfromref','classfile'=>'/societe/class/societe.class.php','class'=>'Societe','method'=>'fetch','element'=>'ThirdParty') + ); + $this->import_examplevalues_array[$r]=array('cs.fk_categorie'=>"Imported category",'cs.fk_societe'=>"MyBigCompany"); + } + + if (! empty($conf->fournisseur->enabled)) + { + // Suppliers + $r++; + $this->import_code[$r]=$this->rights_class.'_'.$r; + $this->import_label[$r]="CatSupList"; // Translation key + $this->import_icon[$r]=$this->picto; + $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon + $this->import_tables_array[$r]=array('cs'=>MAIN_DB_PREFIX.'categorie_fournisseur'); + $this->import_fields_array[$r]=array('cs.fk_categorie'=>"Category*",'cs.fk_societe'=>"Supplier*" + ); + + $this->import_convertvalue_array[$r]=array( + 'cs.fk_categorie'=>array('rule'=>'fetchidfromref','classfile'=>'/categories/class/categorie.class.php','class'=>'Categorie','method'=>'fetch','element'=>'category'), + 'cs.fk_societe'=>array('rule'=>'fetchidfromref','classfile'=>'/societe/class/societe.class.php','class'=>'Societe','method'=>'fetch','element'=>'ThirdParty') + ); + $this->import_examplevalues_array[$r]=array('cs.fk_categorie'=>"Imported category",'cs.fk_societe'=>"MyBigCompany"); + } } diff --git a/htdocs/core/modules/modProduct.class.php b/htdocs/core/modules/modProduct.class.php index ec6971689b7..87114427243 100644 --- a/htdocs/core/modules/modProduct.class.php +++ b/htdocs/core/modules/modProduct.class.php @@ -223,6 +223,28 @@ class modProduct extends DolibarrModules 'sp.remise_percent'=>'0' ); } + + if (! empty($conf->stock->enabled)) + { + // Import stocks + $r++; + $this->import_code[$r]=$this->rights_class.'_'.$r; + $this->import_label[$r]="Stocks"; // Translation key + $this->import_icon[$r]='stock'; + $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon + $this->import_tables_array[$r]=array('ps'=>MAIN_DB_PREFIX.'product_stock'); + $this->import_fields_array[$r]=array('ps.fk_product'=>"Product*",'ps.fk_entrepot'=>"Warehouse*", + 'ps.reel'=>"Stock*",'ps.pmp'=>"PMP" + ); + + $this->import_convertvalue_array[$r]=array( + 'ps.fk_product'=>array('rule'=>'fetchidfromref','classfile'=>'/product/class/product.class.php','class'=>'Product','method'=>'fetch','element'=>'product'), + 'ps.fk_entrepot'=>array('rule'=>'fetchidfromref','classfile'=>'/product/stock/class/entrepot.class.php','class'=>'Entrepot','method'=>'fetch','element'=>'label') + ); + $this->import_examplevalues_array[$r]=array('ps.fk_product'=>"PREF123456",'ps.fk_entrepot'=>"ALM001", + 'ps.reel'=>"10",'ps.pmp'=>"25" + ); + } } diff --git a/htdocs/install/mysql/migration/3.2.0-3.3.0.sql b/htdocs/install/mysql/migration/3.2.0-3.3.0.sql index 259726d354c..16b325b5376 100755 --- a/htdocs/install/mysql/migration/3.2.0-3.3.0.sql +++ b/htdocs/install/mysql/migration/3.2.0-3.3.0.sql @@ -772,6 +772,9 @@ ALTER TABLE llx_entrepot ADD COLUMN import_key varchar(14) AFTER fk_user_author; ALTER TABLE llx_product_fournisseur_price ADD COLUMN import_key varchar(14) AFTER fk_user; ALTER TABLE llx_product_stock ADD COLUMN import_key varchar(14) AFTER pmp; ALTER TABLE llx_societe_rib ADD COLUMN import_key varchar(14) AFTER adresse_proprio; +ALTER TABLE llx_categorie_product ADD COLUMN import_key varchar(14) AFTER fk_product; +ALTER TABLE llx_categorie_societe ADD COLUMN import_key varchar(14) AFTER fk_societe; +ALTER TABLE llx_categorie_fournisseur ADD COLUMN import_key varchar(14) AFTER fk_societe; -- Export filter ALTER TABLE llx_export_model ADD COLUMN filter text AFTER field; diff --git a/htdocs/install/mysql/tables/llx_categorie_fournisseur.sql b/htdocs/install/mysql/tables/llx_categorie_fournisseur.sql index 535972686c2..36b1e7ab37b 100644 --- a/htdocs/install/mysql/tables/llx_categorie_fournisseur.sql +++ b/htdocs/install/mysql/tables/llx_categorie_fournisseur.sql @@ -2,6 +2,7 @@ -- Copyright (C) 2006 Rodolphe Quiedeville -- Copyright (C) 2012 Laurent Destailleur -- Copyright (C) 2012 Regis Houssin +-- Copyright (C) 2012 Juanjo Menent -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -21,5 +22,6 @@ create table llx_categorie_fournisseur ( fk_categorie integer NOT NULL, - fk_societe integer NOT NULL + fk_societe integer NOT NUL, + import_key varchar(14) )ENGINE=innodb; diff --git a/htdocs/install/mysql/tables/llx_categorie_product.sql b/htdocs/install/mysql/tables/llx_categorie_product.sql index f10a5e32c61..2cdcf6802e4 100644 --- a/htdocs/install/mysql/tables/llx_categorie_product.sql +++ b/htdocs/install/mysql/tables/llx_categorie_product.sql @@ -1,6 +1,7 @@ -- ============================================================================ -- Copyright (C) 2005 Brice Davoleau --- Copyright (C) 2005 Matthieu Valleton +-- Copyright (C) 2005 Matthieu Valleton +-- Copyright (C) 2012 Juanjo Menent -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -20,5 +21,6 @@ create table llx_categorie_product ( fk_categorie integer NOT NULL, - fk_product integer NOT NULL + fk_product integer NOT NULL, + import_key varchar(14) )ENGINE=innodb; diff --git a/htdocs/install/mysql/tables/llx_categorie_societe.sql b/htdocs/install/mysql/tables/llx_categorie_societe.sql index 1e9da3f158e..46d5a51ea06 100644 --- a/htdocs/install/mysql/tables/llx_categorie_societe.sql +++ b/htdocs/install/mysql/tables/llx_categorie_societe.sql @@ -1,5 +1,6 @@ -- ============================================================================ -- Copyright (C) 2007 Patrick Raguin +-- Copyright (C) 2012 Juanjo Menent -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -19,5 +20,6 @@ create table llx_categorie_societe ( fk_categorie integer NOT NULL, - fk_societe integer NOT NULL + fk_societe integer NOT NULL, + import_key varchar(14) )ENGINE=innodb; diff --git a/htdocs/product/stock/class/entrepot.class.php b/htdocs/product/stock/class/entrepot.class.php index 30a590016e5..eec0b8b22cf 100644 --- a/htdocs/product/stock/class/entrepot.class.php +++ b/htdocs/product/stock/class/entrepot.class.php @@ -240,13 +240,26 @@ class Entrepot extends CommonObject * Load warehouse data * * @param int $id Warehouse id + * @param string $ref Warehouse label * @return int >0 if OK, <0 if KO */ - function fetch($id) + function fetch($id, $ref='') { + global $conf; + $sql = "SELECT rowid, label, description, statut, lieu, address, cp as zip, ville as town, fk_pays as country_id"; $sql .= " FROM ".MAIN_DB_PREFIX."entrepot"; - $sql .= " WHERE rowid = ".$id; + + if ($id) + { + $sql.= " WHERE rowid = '".$id."'"; + } + + else + { + $sql.= " WHERE entity = " .$conf->entity; + if ($ref) $sql.= " AND label = '".$this->db->escape($ref)."'"; + } dol_syslog(get_class($this)."::fetch sql=".$sql); $result = $this->db->query($sql);