X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=lxo-import%2FaddressB.php;h=df0cfc50e7c84939be67271854479dd846ccb68a;hb=b354d72972e377a3007f8f03ad9b719488c4b3bf;hp=dcb5adb7f974c22cf37c90beccec14c4ac6d2150;hpb=3e63947841c49112212cdeef5bd178fe961bf002;p=kivitendo-erp.git
diff --git a/lxo-import/addressB.php b/lxo-import/addressB.php
index dcb5adb7f..df0cfc50e 100644
--- a/lxo-import/addressB.php
+++ b/lxo-import/addressB.php
@@ -1,7 +1,7 @@
-
+";
+ echo "Feldname => Bedeutung
";
+ foreach($address as $key=>$val) {
+ echo "$key => $val
";
+ }
+ $header=implode(";",array_keys($address));
+ echo $header;
+ exit(0);
};
+
if ($_POST["ok"]) {
$nun=time();
-require ("import_lib.php");
-$db=new myDB($login);
+
+/* get DB instance */
+$db=$_SESSION["db"]; //new myDB($login);
+
$crm=checkCRM();
-function ende($nr) {
- echo "Abbruch: $nr
";
- echo "Fehlende oder falsche Daten.";
- exit(1);
+function ende($txt) {
+ echo "Abbruch: $txt
";
+ exit(1);
}
-
-if ($_POST["ok"]=="Hilfe") {
- echo "Importfelder:
";
- echo "Feldname => Bedeutung
";
- foreach($address as $key=>$val) {
- echo "$key => $val
";
- }
- exit(0);
-};
+$dir = "../users/";
clearstatcache ();
//print_r($_FILES);
$test=$_POST["test"];
if (!empty($_FILES["Datei"]["name"])) {
- $file=$_POST["ziel"];
- if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file.".csv")) {
- $file=false;
- echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")
";
- }
-} else if (is_file($_POST["ziel"].".csv")) {
- $file=$_POST["ziel"];
+ $file=$_POST["ziel"];
+ if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$dir.$file.".csv")) {
+ $file=false;
+ echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")
";
+ }
+} else if (is_file($dir.$_POST["ziel"].".csv")) {
+ $file=$_POST["ziel"];
} else {
- $file=false;
+ $file=false;
}
-if (!$file) ende (2);
+if (!$file) ende ("Kein Datenfile");
$trenner=($_POST["trenner"])?$_POST["trenner"]:",";
-//echo "../users/$login.conf";
-if (!file_exists("../users/$login.conf")) ende(3);
+if ($trenner=="other") {
+ $trenner=trim($trennzeichen);
+}
+if (substr($trenner,0,1)=="#") if (strlen($trenner)>1) $trenner=chr(substr($trenner,1));
-if (!file_exists("$file.csv")) ende(5);
+if (!file_exists($dir.$file.".csv")) ende("$file.csv nicht im Ordner oder leer");
-$db=new myDB($login);
+if (!$db->chkcol($file)) ende("Importspalte kann nicht angelegt werden");
-if (!$db->chkcol($file)) ende(6);
-
-$employee=chkUsr($login);
-if (!$employee) ende(4);
+$employee=chkUsr($_SESSION["employee"]);
+if (!$employee) ende("Ungültiger User");
$kunde_fld = array_keys($address);
-$f=fopen("$file.csv","r");
+ //Zeichencodierung des Servers
+ $tmpcode = $db->getServerCode();
+ //Leider sind die Benennungen vom Server anders als von mb_detect_encoding
+ if ($tmpcode == "UTF8") {
+ define("ServerCode","UTF-8");
+ } else if ($tmpcode == "LATIN9") {
+ define("ServerCode","ISO-8859-15");
+ } else if ($tmpcode == "LATIN1") {
+ define("ServerCode","ISO-8859-1");
+ } else {
+ define("ServerCode",$tmpcode);
+ }
+ //Zeichensatz sollte gleich sein, sonst ist die Datenkonvertierung nutzlos
+ //DB und LxO müssen ja nicht auf der gleichen Maschiene sein.
+ if($tmpcode<>$db->getClientCode()) {
+ $rc = $db->setClientCode($tmpcode);
+ }
+
+ // Zeichenkodierung File
+ if ($_POST["encoding"] == "auto") {
+ define("Auto",true);
+ define("Translate",true);
+ } else {
+ define("Auto",false);
+ if ($_POST["encoding"] == ServerCode) {
+ define("Translate",false);
+ } else {
+ define("Translate",true);
+ define("FileCode",$_POST["encoding"]);
+ }
+ }
+
+function chkBusiness($data,$id=true) {
+global $db;
+ if ($id) {
+ $rs = $db->getAll("select id from business where id =$data");
+ } else {
+ $rs = $db->getAll("select id from business where decription ilike '$data'");
+ }
+ if ($rs[0]["id"]) {
+ return $rs[0]["id"];
+ } else {
+ return "null";
+ }
+}
+
+function chkSalesman($data,$id=true) {
+global $db;
+ if ($id) {
+ $rs = $db->getAll("select id from employee where id =$data");
+ } else {
+ $rs = $db->getAll("select id from employee where login ilike '$data'");
+ }
+ if ($rs[0]["id"]) {
+ return $rs[0]["id"];
+ } else {
+ return "null";
+ }
+}
+
+$f=fopen($dir.$file.".csv","r");
$zeile=fgets($f,1200);
-$infld=split($trenner,strtolower($zeile));
+$infld=explode($trenner,strtolower($zeile));
$first=true;
$ok=true;
+$p=0;
foreach ($infld as $fld) {
- $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
- if ($fld=="branche" && !$crm) { $in_fld[]=""; continue; };
- if ($fld=="sw" && !$crm) { $in_fld[]=""; continue; };
- $in_fld[]=$fld;
+ $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
+ if (in_array($fld,$kunde_fld)) {
+ if ($fld=="branche" && !$crm) { continue; };
+ if ($fld=="sw" && !$crm) { continue; };
+ $in_fld[$fld]=$p;
+ //$fldpos[$fld]=$p;
+ //$in_fld[]=$fld;
+ }
+ $p++;
}
-//print_r($in_fld); echo "
";
+$infld = array_keys($in_fld);
+$infld[] = "import";
+$infld = implode(",",$infld);
$j=0;
$m=0;
$zeile=fgetcsv($f,1200,$trenner);
if ($ok) while (!feof($f)){
- $i=0;
- //echo "Arbeite an $m ";
- $m++;
- $anrede="";
- $Matchcode="";
- $sql="insert into $file ";
- $keys="(";
- $vals=" values (";
- $number=false;
- foreach($zeile as $data) {
- if (!in_array(trim($in_fld[$i]),$kunde_fld)) {
- if ($in_fld[$i]=="anrede") { $anrede=addslashes(trim($data)); }
- $i++;
- continue;
- };
- $data=trim($data);
- $data=mb_convert_encoding($data,"ISO-8859-15","auto");
- //$data=htmlentities($data);
- $data=addslashes($data);
- if ($in_fld[$i]==$file."number") { // customernumber || vendornumber
- if (empty($data) or !$data) {
- $data=getKdId();
- $number=true;
- } else {
- $data=chkKdId($data);
- $number=true;
- }
- } else if ($in_fld[$i]=="taxincluded"){
- $data=strtolower(substr($data,0,1));
- if ($data!="f" && $data!="t") $data="f";
- } else if ($in_fld[$i]=="ustid"){
- $data=strtr(" ","",$data);
- } /*else if ($in_fld[$i]=="matchcode") {
- $matchcode=$data;
- $i++;
- continue;
- if ($data==false or empty($data) or !$data) {
- if (in_array($in_fld[$i],array("name"))) {
- $data=$matchcode;
- }
- }
- }*/
-
- $keys.=$in_fld[$i].",";
- if ($data==false or empty($data) or !$data) {
- $vals.="null,";
- } else {
- if ($in_fld[$i]=="contact"){
- if ($anrede) {
- $vals.="'$anrede $data',";
- } else {
- $vals.="'$data',";
- }
- } else {
- $vals.="'".$data."',";
- }
- }
- $i++;
- }
- if (!$number) {
- $keys.=$file."number,";
- $vals.="'".getKdId()."',";
- }
- if ($keys<>"(") {
- if ($test) {
- if ($first) {
- echo "\n";
- echo "".str_replace(","," | ",substr($keys,1,-1))." |
\n";
- $first=false;
- };
- $vals=str_replace("',","'",substr($vals,9,-1));
- echo " | ".str_replace("null,","null | ",$vals)." |
\n";
- //echo "Import $j
\n";
- flush();
- } else {
- $sql.=$keys."taxzone_id,import)";
- $sql.=$vals."0,$nun)";
- $rc=$db->query($sql);
- if (!$rc) echo "Fehler: ".$sql."
";
- }
- $j++;
- } else {
- $vals=str_replace("',","'",substr($vals,9,-1));
- echo " | ".str_replace("null,","null | ",$vals)." |
\n";
- flush();
+ $i=0;
+ $m++;
+ $anrede="";
+ $Matchcode="";
+ $sql="insert into $file ";
+ $keys=array();
+ $vals=array();
+ $number=false;
+ //foreach($zeile as $data) {
+
+ foreach($in_fld as $fld => $pos) {
+ switch ($fld) {
+ case "name" :
+ case "department_1" :
+ case "department_2" :
+ case "matchcode" :
+ case "street" :
+ case "city" :
+ case "notes" :
+ case "sw" :
+ case "branche" :
+ case "country" :
+ case "contact" :
+ case "homepage" :
+ case "email" :
+ case "bank" : $data = addslashes(trim($zeile[$pos]));
+ if (Translate) translate($data);
+ case "ustid" : $data = strtr(trim($zeile[$pos])," ","");
+ case "bank_code" : $data = trim($zeile[$pos]);
+ case "account_number":
+ case "greeting" :
+ case "taxnumber" :
+ case "zipcode" :
+ case "phone" :
+ case "fax" : $data = trim($zeile[$pos]);
+ $data = "'$data'";
+ if ($data=="''") {
+ $vals[] = "null";
+ } else {
+ $vals[] = $data;
+ }
+ break;
+ case "business_id" : $vals[] = chkBusiness(trim($zeile[$pos]));
+ break;
+ case "salesman_id" : $vals[] = chkSalesman(trim($zeile[$pos]));
+ break;
+ case "taxincluded" : $data = strtolower(substr($zeile[$pos],0,1));
+ if ($data!="f" && $data!="t") { $vals[] = "'f'"; }
+ else { $vals[] = "'".$data."'";}
+ break;
+ case "taxzone_id" : $data = trim($zeile[$pos])*1;
+ if ($data>3 && $data<0) $data = 0;
+ $vals[] = $data;
+ break;
+ case "creditlimit" :
+ case "discount" :
+ case "terms" : $vals[] = trim($zeile[$pos])*1;
+ break;
+ case "customernumber":
+ case "vendornumber" : $data = trim($zeile[$pos]);
+ if (empty($data) or !$data) {
+ $vals[] = getKdId();
+ $number = true;
+ } else {
+ $vals[] = chkKdId($data);
+ $number = true;
+ }
+ break;
}
- $zeile=fgetcsv($f,1200,$trenner);
+ };
+ if (!in_array("taxzone_id",$in_fld)) {
+ $in_fld[] = "taxzone_id";
+ $vals[] = 0;
+ }
+ // seit 2.6 ist die DB-Kodierung UTF-8 @holger Ansonsten einmal vorher die DB-Encoding auslesen
+ // Falls die Daten ISO-kodiert kommen entsprechend wandeln
+ // done!
+ // UTF-8 MUSS als erstes stehen, da ansonsten die Prüfung bei ISO-8859-1 aufhört ...
+ // die blöde mb_detect... tut leider nicht immer, daher die Möglichkeit der Auswahl
+ // TODO Umlaute am Anfang wurden bei meinem Test nicht übernommen (Ãsterreich). S.a.:
+ // http://forum.de.selfhtml.org/archiv/2007/1/t143904/
+ if ($test) {
+ if ($first) {
+ echo "\n";
+ echo implode(' | ',array_keys($in_fld));
+ echo " |
\n";
+ $first=false;
+ };
+ echo "";
+ echo implode(' | ',$vals);
+ echo " |
\n";
+ //echo "Import $j
\n";
+ flush();
+ } else {
+ $vals[] = $nun;
+ $sql = "INSERT INTO $file (".$infld.") values (".implode(",",$vals).")";
+ $rc=$db->query($sql);
+ if ($j % 10 == 0) { echo "."; flush(); };
+ if (!$rc) { echo "
Fehler: ".$sql."
"; flush(); };
+ }
+ $j++;
+ $zeile=fgetcsv($f,1200,$trenner);
}
fclose($f);
if ($test) echo "
\n ##### = Neue Kunden-/Lieferantennummer\n
";
@@ -184,15 +277,33 @@ echo $j." $file importiert.\n";
Adressimport für die ERP
- }; ?>
+