2 /***************************************************************
 
   3 * $Id: shopimport_csv.php,v 1.2 2004/06/30 08:31:35 hli Exp $
 
   4 *Author: Holger Lindemann
 
   5 *Copyright: (c) 2004 Lx-System
 
   7 *eMail: info@lx-system.de
 
   9 *Shop: olCommerce 1.2.2
 
  10 ***************************************************************/
 
  12 require_once "DB.php";
 
  13 require_once "conf.php";
 
  14 if ($dbprefix<>"") { define("PREFIX",$dbprefix."_"); } else { define("PREFIX",""); }
 
  16 $LAND=array("Germany"=>"D");
 
  17 $db=DB::connect($SHOPdns);
 
  18 if (!$db) dbFehler("",$db->getDebugInfo());
 
  19 if (DB::isError($db)) {
 
  20         dbFehler("",$db->getDebugInfo());
 
  21         die ($db->getDebugInfo());
 
  24 function createCategory($name,$maingroup) {
 
  26         $newID=uniqid(rand());
 
  27         $sql="insert into ".PREFIX."categories (categories_image,parent_id,date_added) values ('$newID',$maingroup,now())";
 
  29         $sql="select * from ".PREFIX."categories where categories_image = '$newID'";
 
  30         $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
  31         $id=$rs[0]["categories_id"];
 
  32         $sql="update ".PREFIX."categories set categories_image = null where categories_id=$id";
 
  35         foreach ($langs as $LANG) {
 
  36                 $sql="insert into ".PREFIX."categories_description (categories_id,language_id,categories_name) values ($id,$LANG,'$name')";
 
  40         return ($rc)?$id:false;
 
  42 function getCategory($name) {
 
  44         if (empty($name)) $name="Default";
 
  45         $tmp=split("!",$name);
 
  50                 $sql="select D.*,C.parent_id from ".PREFIX."categories C left join ".PREFIX."categories_description D on C.categories_id=D.categories_id where categories_name like '".$tmp[$i]."' and C.parent_id=$maingroup";
 
  51                 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
  52                 if ($rs[0]["categories_id"]) {
 
  53                         $maingroup=$rs[0]["categories_id"];
 
  54                         echo $maingroup.":".$rs[0]["categories_name"]." ";
 
  59         } while ($rs and $found and $i<count($tmp));
 
  60         for (;$i<count($tmp); $i++) {
 
  61                 $maingroup=createCategory($tmp[$i],$maingroup);
 
  65 function insartikel($data) {
 
  66 global $db,$header,$tax,$defLang;
 
  67         $newID=uniqid(rand());
 
  68         $sql="insert into ".PREFIX."products (products_model,products_image) values ('".$data[array_search("products_model")]."','$newID')";
 
  70         $sql="select * from ".PREFIX."products where products_image='$newID'";
 
  71         $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
  72         $sql="update ".PREFIX."products set products_image=null,products_status=1 where products_id=".$rs[0]["products_id"];
 
  74         $sql="insert into ".PREFIX."products_description (products_id,language_id,products_name) values (".$rs[0]["products_id"].",$defLang,' ')";
 
  76         $sql="insert into ".PREFIX."products_to_categories (products_id,categories_id) values (".$rs[0]["products_id"].",".$data["categories_id"].")";
 
  78         echo " <b>insert</b> ";
 
  79         updartikel($data,$rs[0]["products_id"]);
 
  81 function updartikel($data,$id) {
 
  82 global $db,$header,$tax,$defLang;
 
  83         $sql="update ".PREFIX."products set products_price=%01.2f,products_weight=%01.2f,products_tax_class_id=%d,products_last_modified=now()  where products_id=%d";
 
  84         $sql=sprintf($sql,$data[array_search("products_price",$header)],$data[array_search("products_weight",$header)],$tax[$data[array_search("products_tax",$header)]],$id);
 
  86         $sql="update ".PREFIX."products_description set products_name='%s',products_description='%s' where products_id=%d and language_id=$defLang";
 
  87         $sql=sprintf($sql,$data[array_search("products_name",$header)],$data[array_search("products_description",$header)],$id);
 
  89         $sql="update ".PREFIX."products_to_categories set categories_id=".$data[array_search("categories_id",$header)]." where products_id=$id";
 
  91         echo "(".$id." ".$data[array_search("products_name",$header)].")+++<br>";
 
  93 function chkartikel($data) {
 
  94 global $db,$header,$tax;
 
  95         $sql="select * from ".PREFIX."products P left join ".PREFIX."products_description D on P.products_id=D.products_id left join ".PREFIX."products_to_categories C on P.products_id=C.products_id where  products_model like '".$data[array_search("products_model",$header)]."' and language_id=2";
 
  96         $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
  98                          if ($rs[0]["products_price"]<>$data[array_search("products_price",$header)])   { updartikel($data,$rs[0]["products_id"]); }
 
  99                 else if ($rs[0]["products_weight"]<>$data[array_search("products_weight",$header)])     { updartikel($data,$rs[0]["products_id"]); }
 
 100                 else if ($rs[0]["products_name"]<>$data[array_search("products_name",$header)])         { updartikel($data,$rs[0]["products_id"]); }
 
 101                 else if ($rs[0]["products_description"]<>$data[array_search("products_description",$header)])   { updartikel($data,$rs[0]["products_id"]); }
 
 102                 else if ($rs[0]["products_tax_class_id"]<>$tax[$data[array_search("products_tax",$header)]])    { updartikel($data,$rs[0]["products_id"]); }
 
 103                 else if ($rs[0]["categories_id"]<>$data[array_search("categories_id",$header)])         { updartikel($data,$rs[0]["products_id"]); }
 
 104                 else { echo "(".$rs[0]["products_id"]." ".$rs[0]["products_name"].")...<br>"; };
 
 110 $sql="select languages_id from languages";
 
 111 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
 113         foreach ($rs as $zeile) {
 
 114                 $langs[]=$zeile["languages_id"];
 
 119 $sql="select * from ".PREFIX."languages L left join ".PREFIX."configuration C on L.code=C.configuration_value where  configuration_key = 'DEFAULT_LANGUAGE'";
 
 120 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
 122         $defLang=$rs[0]["languages_id"];
 
 126 $sql="select * from ".PREFIX."tax_rates";
 
 127 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
 
 129         foreach ($rs as $zeile) {
 
 130                 $tax[$zeile["tax_rate"]]=$zeile["tax_class_id"];
 
 136 if ($_FILES["csv"]["name"] || ($_POST["nofile"] && file_exists($SHOPdir)) ) {
 
 137         if ($_FILES["csv"]["tmp_name"]) {
 
 138                 move_uploaded_file($_FILES["csv"]["tmp_name"],$SHOPdir);
 
 140         $f=fopen($SHOPdir,"r");
 
 141         $header=fgetcsv($f,1000,";");
 
 142         $header[]="categories_id";
 
 143         $data=fgetcsv($f,1000,";");
 
 145                 $catId=getCategory($data[array_search("categories_name",$header)]);
 
 148                 $data=fgetcsv($f,1000,";");
 
 151         echo "<a href='trans.php'>zurück</a>";
 
 156                 <title>Datenaustausch ERP-xt:Commerce</title>
 
 161 <h1>Artikelimport aus csv-Datei in xt:Commerce</h1><br>
 
 162 <form name="csv" action="shopimport_csv.php" enctype="multipart/form-data" method="post">
 
 163         <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="500000">
 
 164         <input type="checkbox" name="nofile" value="1">Auf dem Server vorhandene Daten importieren<br>
 
 165         Datenfile für Import <input type="file" name="csv"><br>
 
 166         <input type="submit" name="ok" value="ok">
 
 169 <a href="trans.php">zurück</a>