CSV-Importscripte für Adressen, Kontakte, Lieferanschriften
authorHolger Lindemann <hli@lx-system.de>
Wed, 13 Dec 2006 09:57:47 +0000 (09:57 +0000)
committerHolger Lindemann <hli@lx-system.de>
Wed, 13 Dec 2006 09:57:47 +0000 (09:57 +0000)
lxo-import/addressB.php [new file with mode: 0644]
lxo-import/contactB.php [new file with mode: 0644]
lxo-import/import_lib.php [new file with mode: 0644]
lxo-import/shiptoB.php [new file with mode: 0644]

diff --git a/lxo-import/addressB.php b/lxo-import/addressB.php
new file mode 100644 (file)
index 0000000..5e19f7b
--- /dev/null
@@ -0,0 +1,205 @@
+<html>
+<LINK REL="stylesheet" HREF="../css/lx-office-erp.css" TYPE="text/css" TITLE="Lx-Office stylesheet">
+<body>
+<?
+/*
+Kunden- bzw. Lieferantenimport mit Browser nach Lx-Office ERP
+
+Copyright (C) 2005
+Author: Holger Lindemann
+Email: hli@lx-system.de
+Web: http://lx-system.de
+
+*/
+
+if ($_GET["login"]) {
+       $login=$_GET["login"];
+} else {
+       $login=$_POST["login"];
+};
+if ($_POST["ok"]) {
+
+$nun=time();
+
+require ("import_lib.php");
+$db=new myDB($login);
+$crm=checkCRM();
+
+function ende($nr) {
+       echo "Abbruch: $nr<br>";
+       echo "Fehlende oder falsche Daten.";
+       exit(1);
+}
+
+if ($_POST["ok"]=="Hilfe") {
+       echo "Importfelder:<br>";
+       echo "Feldname => Bedeutung<br>";
+       foreach($address as $key=>$val) {
+               echo "$key => $val<br>";
+       }
+       exit(0);
+};
+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"].")<br>";
+       } 
+} else if (is_file($_POST["ziel"].".csv")) {
+       $file=$_POST["ziel"];
+} else {
+       $file=false;
+} 
+
+if (!$file) ende (2);
+
+$trenner=($_POST["trenner"])?$_POST["trenner"]:",";
+//echo "../users/$login.conf";
+if (!file_exists("../users/$login.conf")) ende(3);
+
+if (!file_exists("$file.csv")) ende(5);
+
+$db=new myDB($login);
+
+if (!$db->chkcol($file)) ende(6);
+
+$employee=chkUsr($login);
+if (!$employee) ende(4);
+
+$kunde_fld = array_keys($address);
+
+$f=fopen("$file.csv","r");
+$zeile=fgets($f,1200);
+$infld=split($trenner,strtolower($zeile));
+//echo "$zeile<br>";
+//print_r($infld); echo "<br>";
+$first=true;
+$ok=true;
+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;
+}
+//print_r($in_fld); echo "<br>";
+$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=htmlentities($data);
+               $data=addslashes($data);
+               if (trim($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]=="language") {
+                       $data=strtolower(substr($data,0,2));
+                       if (!in_array($data,array("de","en","fr"))) $data=false;
+               }
+               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) {
+                       if (in_array($in_fld[$i],array("name"))) {
+                               $keys="(";
+                               break;
+                       }
+                       $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 "<table border='1'>\n";
+                               echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
+                               $first=false;
+                       };
+                       $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
+                       echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
+                        //echo "Import $j<br>\n";
+                       flush();
+               } else {
+                       $sql.=$keys."import)";
+                       $sql.=$vals."$nun)";            
+                       $rc=$db->query($sql);
+                       if (!$rc) echo "Fehler: ".$vals."<br>";
+               }
+               $j++;
+       } else {
+          $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
+          echo "<tr><td style=\"color:red\">".str_replace("null,","null</td><td style=\"color:red\">",$vals)."</td></tr>\n";
+          flush();
+        }
+       $zeile=fgetcsv($f,1200,$trenner);
+}
+fclose($f);
+if ($test) echo "</table>\n ##### = Neue Kunden-/Lieferantennummer\n<br>";
+echo $j." $file importiert.\n";
+} else {
+?>
+
+<p class="listtop">Adressimport f&uuml;r die ERP<p>
+<br>
+<form name="import" method="post" enctype="multipart/form-data" action="addressB.php">
+<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
+<input type="hidden" name="login" value="<?= $login ?>">
+<table>
+<tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
+<tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
+<tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
+<tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
+<tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
+<tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
+</table>
+</form>
+<? }; ?>
diff --git a/lxo-import/contactB.php b/lxo-import/contactB.php
new file mode 100644 (file)
index 0000000..8c8e53a
--- /dev/null
@@ -0,0 +1,187 @@
+<html>
+<LINK REL="stylesheet" HREF="../css/lx-office-erp.css" TYPE="text/css" TITLE="Lx-Office stylesheet">
+<body>
+<?
+/*
+Kontaktimport mit Browser nach Lx-Office ERP
+
+Copyright (C) 2005
+Author: Holger Lindemann
+Email: hli@lx-system.de
+Web: http://lx-system.de
+
+*/
+if ($_GET["login"]) {
+       $login=$_GET["login"];
+} else {
+       $login=$_POST["login"];
+};
+
+require ("import_lib.php");
+$db=new myDB($login);
+$crm=checkCRM();
+
+if ($_POST["ok"]) {
+       $login=$_POST["login"];
+       $test=$_POST["test"];
+
+       if ($crm) {
+               $kunde_fld = array_keys($contactscrm);
+               $contact=$contactscrm;
+       } else {
+               $kunde_fld = array_keys($contacts);
+               $contact=$contacts;
+       }
+       $nun=time();
+
+       function ende($nr) {
+               echo "Abbruch: $nr\n";
+               echo "Aufruf: addressS.php [login customer|vendor] [test] | [felder]\n";
+               exit($nr);
+       }
+       if ($_POST["ok"]=="Hilfe") {
+               echo "Importfelder:<br>";
+               echo "Feldname => Bedeutung<br>";
+               foreach($contact as $key=>$val) {
+                       echo "$key => $val<br>";
+               }
+               exit(0);
+       };
+
+       clearstatcache ();
+
+       $trenner=($_POST["trenner"])?$_POST["trenner"]:",";
+
+if (!empty($_FILES["Datei"]["name"])) { 
+       $file=$_POST["ziel"];
+       if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file."_contact.csv")) {
+               $file=false;
+               echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")<br>";
+       } 
+} else if (is_file($_POST["ziel"]."_contact.csv")) {
+       $file=$_POST["ziel"];
+} else {
+       $file=false;
+} 
+
+if (!$file) ende (2);
+
+if (!file_exists($file."_contact.csv")) ende(5);
+
+if (!file_exists("../users/$login.conf")) ende(3);
+
+
+$employee=chkUsr($login);
+if (!$employee) ende(4);
+
+if (!$db->chkcol($file)) ende(6);
+
+$f=fopen($file."_contact.csv","r");
+$zeile=fgets($f,1200);
+$infld=split($trenner,strtolower($zeile));
+$first=true;
+
+
+foreach ($infld as $fld) {
+       $fld = trim(strtr($fld,array("\""=>"","'"=>"")));
+       $in_fld[]=$fld;
+}
+$j=0;
+$zeile=fgetcsv($f,1200,$trenner);
+while (!feof($f)){
+       $i=-1;
+       $firma="";
+       $name=false;
+       $id=false;
+       $sql="insert into contacts ";
+       $keys="(";
+       $vals=" values (";
+       foreach($zeile as $data) {
+               $i++;
+               if ($in_fld[$i]=="firma") { 
+                       $firma=addslashes(trim($data)); 
+                       continue;
+               };
+               if (!in_array($in_fld[$i],$kunde_fld)) {
+                       continue;
+               }
+               $data=addslashes(trim($data));
+               if ($in_fld[$i]=="cp_cv_id" && $data) {
+                       $data=chkKdId($data);
+                       if ($data) $firma="";
+                       if (!$id) $id = $data;
+                       continue;
+               } 
+               if ($in_fld[$i]==$file."number" && $data) {
+                       $tmp=getFirma($data,$file);
+                       if ($tmp) $firma="";
+                       if ($id<>$tmp) $id=$tmp;
+                       continue;
+               } 
+               $keys.=$in_fld[$i].",";
+               
+               if ($data==false or empty($data) or !$data) {
+                        $vals.="null,";
+                } else {
+                       if (in_array($in_fld[$i],array("cp_fax","cp_phone1","cp_phone2"))) {
+                               $data="0".$data;
+                       } else if ($in_fld[$i]=="cp_country" && $data) {
+                               $data=mkland($data);
+                       }
+                       if ($in_fld[$i]=="cp_name") $name=true;
+                        $vals.="'".$data."',";
+                        // bei jedem gefuellten Datenfeld erhoehen
+                        $val_count++;
+                }
+       }
+       if (!$name) {
+               $zeile=fgetcsv($f,1200,$trenner);
+               continue;
+       }
+       if ($firma) {
+               $data=suchFirma($file,$firma);
+               if ($data) {
+                       $vals.=$data["cp_cv_id"].",";
+                       $keys.="cp_cv_id,";
+               }
+       } else if ($id) {
+                       $vals.=$id.",";
+                       $keys.="cp_cv_id,";
+       }
+       if ($keys<>"(" && $val_count>2) {
+               if ($test) {
+                       if ($first) {
+                               echo "<table border='1'>\n";
+                               echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
+                               $first=false;
+                       };
+                       $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
+                       echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
+                       flush();
+               } else {
+                       $sql.=substr($keys,0,-1).")";
+                       $sql.=substr($vals,0,-1).")";
+                       $rc=$db->query($sql);
+                       if (!$rc) echo "Fehler: ".$sql."\n";
+               }
+               $j++;
+       };
+       $zeile=fgetcsv($f,1200,$trenner);
+}
+fclose($f);
+echo $j." $file importiert.\n";} else {
+?>
+<p class="listtop">Kontakt-Adressimport f&uuml;r die ERP</p>
+<form name="import" method="post" enctype="multipart/form-data" action="contactB.php">
+<input type="hidden" name="MAX_FILE_SIZE" value="300000">
+<input type="hidden" name="login" value="<?= $login ?>">
+<table>
+<tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
+<tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
+<tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
+<tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
+<tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
+<tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
+</table>
+</form>
+<? }; ?>
diff --git a/lxo-import/import_lib.php b/lxo-import/import_lib.php
new file mode 100644 (file)
index 0000000..89ca76f
--- /dev/null
@@ -0,0 +1,371 @@
+<?
+/*
+Funktionsbibliothek für den Datenimport in Lx-Office ERP
+
+Copyright (C) 2005
+Author: Holger Lindemann
+Email: hli@lx-system.de
+Web: http://lx-system.de
+
+*/
+
+require_once "DB.php";
+
+$address = array(
+       "name" => "Firmenname",
+       "department_1" => "Abteilung",
+       "department_2" => "Abteilung",
+       "street" => "Strasse + Nr",
+       "zipcode" => "Plz",
+       "city" => "Ort",
+       "country" => "Land",
+       "contact" => "Ansprechpartner",
+       "phone" => "Telefon",
+       "fax" => "Fax",
+       "homepage" => "Homepage",
+       "email" => "eMail",
+       "notes" => "Bemerkungen",
+       "discount" => "Rabatt (nn.nn)",
+       "taxincluded" => "incl. Steuer? (t/f)",
+       "terms" => "Zahlungsziel (Tage)",
+       "customernumber" => "Kundennummer",
+       "vendornumber" => "Lieferantennummer",
+       "taxnumber" => "Steuernummer",
+       "account_number" => "Kontonummer",
+       "bank_code" => "Bankleitzahl",
+       "bank" => "Bankname",
+       "branche" => "Branche",
+       "language" => "Sprache (de,en,fr)",
+       "sw" => "Stichwort",
+       "creditlimit" => "Kreditlimit (nnnnnn.nn)",
+       "hierarchie" => "Hierarchie",
+       "potenzial" => "Potenzial",
+        "ar" => "Debitorenkonto",
+        "ap" => "Kreditorenkonto",
+        "matchcode" => "Matchcode",
+       "customernumber2" => "Kundennummer 2");
+        
+$shiptos = array(
+       "shiptoname" => "Firmenname",
+       "shiptodepartment_1" => "Abteilung",
+       "shiptodepartment_2" => "Abteilung",
+       "shiptostreet" => "Strasse + Nr",
+       "shiptozipcode" => "Plz",
+       "shiptocity" => "Ort",
+       "shiptocountry" => "Land",
+       "shiptocontact" => "Ansprechpartner",
+       "shiptophone" => "Telefon",
+       "shiptofax" => "Fax",
+       "shiptoemail" => "eMail",
+       "customernumber" => "Kundennummer",
+       "vendornumber" => "Lieferantennummer");
+
+$parts = array( 
+       "partnumber" => "Artikelnummer",
+       "description" => "Artikeltext",
+       "unit" => "Einheit",
+       "weight" => "Gewicht (kg)",
+       "onhand" => "Lagerbestand",
+       "notes" => "Beschreibung",
+       "makemodel" => "Hersteller",
+       "model" => "Modellbezeichnung",
+       "income_accno_id" => "Erlöskonto",
+       "expense_accno_id" => "Konto Umsatzkosten",
+       "bin" => "Lagerort",
+       "image" => "Pfad/Dateiname",
+       "drawing" => "Pfad/Dateiname",
+       "microfiche" => "Pfad/Dateiname",
+       "partsgroup_id" => "Name Warengruppe",
+       "listprice" => "Listenpreis",
+       "sellprice" => "Verkaufspreis",
+       "lastcost" => "letzter EK",
+       "art" => "Ware/Dienstleistung (*/d)");
+       
+$contactscrm = array(
+       "customernumber" => "Kundennummer",
+       "vendornumber" => "Lieferantennummer",
+       "cp_cv_id" => "FirmenID in der db",
+       "firma" => "Firmenname",
+       "cp_greeting" => "Anrede",
+       "cp_title" => "Titel",
+       "cp_givenname" => "Vorname",
+       "cp_greeting" => "Anrede",
+       "cp_name" => "Nachname",
+       "cp_email" => "eMail",
+       "cp_phone1" => "Telefon 1",
+       "cp_phone2" => "Telefon 2",
+       "cp_street" => "Strasse",
+       "cp_zipcode" => "PLZ",
+       "cp_city" => "Ort",
+       "cp_notes" => "Bemerkungen",
+       "cp_country" => "Land",
+       "cp_stichwort1" => "Stichwort(e)",
+       "katalog" => "Katalog",
+       "inhaber" => "Inhaber",
+       "contact_id" => "Kontakt ID"
+       );
+
+$contacts = array(
+       "customernumber" => "Kundennummer",
+       "vendornumber" => "Lieferantennummer",
+       "cp_cv_id" => "FirmenID in der db",
+       "firma" => "Firmenname",
+       "cp_greeting" => "Anrede",
+       "cp_title" => "Titel",
+       "cp_givenname" => "Vorname",
+       "cp_greeting" => "Anrede",
+       "cp_name" => "Nachname",
+       "cp_email" => "eMail",
+       "cp_phone1" => "Telefon 1",
+       "cp_phone2" => "Telefon 2",
+       "katalog" => "Katalog",
+       "inhaber" => "Inhaber",
+       "contact_id" => "Kontakt ID"
+       );
+
+function checkCRM() {
+global $db;
+       $sql="select * from crm";
+       $rs=$db->getAll($sql);
+       if ($rs) {
+               return true;
+       } else {
+               return false;
+       }
+}
+
+function chkUsr($usr) {
+// ist es ein gültiger ERP-Benutzer? Er muß mindestens 1 x angemeldet gewesen sein.
+global $db;
+       $sql="select * from employee where login = '$usr'";
+       $rs=$db->getAll($sql);
+       if ($rs[0]["id"]) { return $rs[0]["id"]; } 
+       else { return false; };
+}
+
+function getKdId() {
+// die nächste freie Kunden-/Lieferantennummer holen
+global $db,$file,$test;
+       if ($test) { return "#####"; }
+       $sql1="select * from defaults";
+       $sql2="update defaults set ".$file."number = '%s'";
+       $db->lock();
+       $rs=$db->getAll($sql1);
+       $nr=$rs[0][$file."number"];
+       preg_match("/^([^0-9]*)([0-9]+)/",$nr,$hits);
+       if ($hits[2]) { $nr=$hits[2]+1; $nnr=$hits[1].$nr; }
+       else { $nr=$hits[1]+1; $nnr=$nr; };
+       $rc=$db->query(sprintf($sql2,$nnr));
+       if ($rc) { 
+               $db->commit(); 
+               return $nnr;
+       } else { 
+               $db->rollback(); 
+               return false;
+       };
+}
+
+function chkKdId($data) {
+// gibt es die Nummer schon?
+global $db,$file,$test;
+       $sql="select * from $file where ".$file."number = '$data'";
+       $rs=$db->getAll($sql);
+       if ($rs[0][$file."number"]==$data) {
+               // ja, eine neue holen
+               return getKdId();
+       } else {
+               return $data;
+       }
+}
+
+function getKdRefId($data) {
+// gibt es die Nummer schon?
+global $db,$file,$test;
+       if (empty($data) or !$data) {   
+               return false; 
+       } 
+       $sql="select * from $file where ".$file."number = '$data'";
+       $rs=$db->getAll($sql);
+       return $rs[0]["id"];
+}
+
+function suchFirma($tab,$data) {
+// gibt die Firma ?
+global $db;
+       if (empty($data) or !$data) {   
+               return false; 
+       }
+       $data=strtoupper($data);
+       $sql="select * from $tab where upper(name) like '%$data%'";
+       $rs=$db->getAll($sql);
+       if (!$rs) {
+               $org=$data;
+               while(strpos($data,"  ")>0) {
+                       $data=ereg_replace("  "," ",$data);
+               }
+               $data=preg_replace("/[^A-Z0-9]/ ",".*",trim($data));
+               $sql="select * from $tab where upper(name) ~ '$data'"; 
+               $rs=$db->getAll($sql);
+               if (count($rs)==1) {
+                       return array("cp_cv_id"=>$rs[0]["id"],"Firma"=>$rs[0]["name"]);
+               }
+               return false;
+       } else {
+               return array("cp_cv_id"=>$rs[0]["id"],"Firma"=>$rs[0]["name"]);
+       }
+}
+
+$land=array("DEUTSC"=>"D","FRANKR"=>"F","SPANIE"=>"ES","ITALIE"=>"I","HOLLAN"=>"NL","NIEDER"=>"NL",
+       "BELGIE"=>"B","LUXEMB"=>"L","NORWEG"=>"N","FINNLA"=>"","GRIECH"=>"GR","OESTER"=>"A",
+       "SCHWEI"=>"CH","SCHWED"=>"S","AUSTRI"=>"A");
+
+function mkland($data) {
+global $land;
+       $data=strtr($data,array("Ö"=>"OE","Ä"=>"AE","Ü"=>"UE","ö"=>"OE","ä"=>"AE","ü"=>"UE","ß"=>"SS"));
+       $data=strtoupper(substr($data,0,6));
+       $cntr=$land[$data];
+       return (strlen($cntr)>0)?$cntr:substr($data,0,3);
+}
+
+//Suche Nach Kunden-/Lieferantenummer
+function getFirma($nummer,$tabelle) {
+global $db;
+       $nummer=strtoupper($nummer);
+       $sql="select id from $tabelle where upper(".$tabelle."number) = '$nummer'";
+       $rs=$db->getAll($sql);
+       if (!$rs) {
+               $nr=ereg_replace(" ","%",$nummer);
+               $sql="select id,".$tabelle."number from $tabelle where upper(".$tabelle."number) like '$nr'";
+               $rs=$db->getAll($sql);
+               if ($rs) {
+                       $nr=ereg_replace(" ","",$nummer);
+                       foreach ($rs as $row) {
+                               $tmp=ereg_replace(" ","",$row[$tabelle."number"]);
+                               if ($tmp==$nr) return $row["id"];
+                       }
+               } else { 
+                       return false;
+               }
+       } else {
+               return $rs[0]["id"];
+       }
+}
+
+class myDB extends DB {
+// Datenbankklasse
+
+ var $rc = false;
+ var $showErr = false;
+ var $db = false;
+ var $debug = false;
+
+/****************************************************
+* uudecode
+* in: string
+* out: string
+* dekodiert Perl-UU-kodierte Passwort-Strings
+* http://de3.php.net/base64_decode (bug #171)
+*****************************************************/
+       function uudecode($encode) {
+         $b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+         $encode = preg_replace("/^./m","",$encode);
+         $encode = preg_replace("/\n/m","",$encode);
+         for($i=0; $i<strlen($encode); $i++) {
+           if ($encode[$i] == '')
+             $encode[$i] = ' ';
+           $encode[$i] = $b64chars[ord($encode[$i])-32];
+         }
+   
+         while(strlen($encode) % 4)
+           $encode .= "=";
+
+         return base64_decode($encode);
+       }
+
+       function dbFehler($sql,$err) {
+               if ($this->showErr)
+                       echo "$sql : $err\n";
+       }
+
+       function showDebug($sql) {
+               echo $sql."\n";
+               if ($this->debug==2) {
+                       print_r($this->rc);
+               };
+       }
+
+       function myDB($usr) {
+               // Datenbankparameter des ERP-Users benutzen.
+               $tmp = file_get_contents("../users/$usr.conf");
+               preg_match("/dbname => '(.+)'/",$tmp,$hits);
+               $dbname=$hits[1];
+               preg_match("/dbpasswd => '(.+)'/",$tmp,$hits);
+               if ($hits[1]) {
+                       $dbpasswd=$this->uudecode($hits[1]);
+               } else {
+                       $dbpasswd="";
+               };
+               preg_match("/dbuser => '(.+)'/",$tmp,$hits);
+               $dbuser=$hits[1];
+               preg_match("/dbhost => '(.+)'/",$tmp,$hits);
+               $dbhost=$hits[1];
+               if (!$dbhost) $dbhost="localhost";
+               if ($dbpasswd) {
+                       $dns=$dbuser.":".$dbpasswd."@".$dbhost."/".$dbname;
+               } else {
+                       $dns=$dbuser."@".$dbhost."/".$dbname;
+               };
+               $dns="pgsql://".$dns;
+               $this->db=DB::connect($dns);
+               if (!$this->db) DB::dbFehler("oh oh oh",$this->db->getDebugInfo());
+               if (DB::isError($this->db)) {
+                       $this->dbFehler("Connect",$this->db->getDebugInfo());
+                       die ($this->db->getDebugInfo());
+               }
+               return $this->db;
+       }
+
+       function query($sql) {
+               $this->rc=@$this->db->query($sql);
+               if ($this->debug) $this->showDebug($sql);
+               if(DB::isError($this->rc)) {
+                       $this->dbFehler($sql,$this->rc->getMessage());
+                       return false;
+               } else {
+                       return $this->rc;
+               }
+       }
+       function getAll($sql) {
+               $this->rc=@$this->db->getAll($sql,DB_FETCHMODE_ASSOC);
+               if ($this->debug) $this->showDebug($sql);
+               if(DB::isError($this->rc)) {
+                       $this->dbFehler($sql,$this->rc->getMessage());
+                       return false;
+               } else {
+                       return $this->rc;
+               }
+       }       
+
+       function lock() {
+               $this->query("BEGIN");
+       }
+       function commit() {
+               $this->query("COMMIT");
+       }
+       function rollback() {
+               $this->query("ROLLBACK");
+       }
+       function chkcol($tbl) {
+       // gibt es die Spalte import schon?
+               $rc=$this->db->query("select import from $tbl limit 1");
+               if(DB::isError($rc)) {
+                       $rc=$this->db->query("alter table $tbl add column import int4");
+                       if(DB::isError($rc)) { return false; }
+                       else { return true; }
+               
+               } else { return true; };
+       }
+}
+
+?>
diff --git a/lxo-import/shiptoB.php b/lxo-import/shiptoB.php
new file mode 100644 (file)
index 0000000..946b172
--- /dev/null
@@ -0,0 +1,184 @@
+<html>
+<LINK REL="stylesheet" HREF="../css/lx-office-erp.css" TYPE="text/css" TITLE="Lx-Office stylesheet">
+<body>
+<?
+/*
+Lieferanschriftimport mit Browser nach Lx-Office ERP
+
+Copyright (C) 2005
+Author: Philip Reetz
+Email: p.reetz@linet-services.de
+Web: http://www.linet-services.de
+
+*/
+if ($_GET["login"]) {
+       $login=$_GET["login"];
+} else {
+       $login=$_POST["login"];
+};
+
+require ("import_lib.php");
+$db=new myDB($login);
+$crm=checkCRM();
+
+if ($_POST["ok"]) {
+       $login=$_POST["login"];
+       $test=$_POST["test"];
+
+       
+       $shipto_fld = array_keys($shiptos);
+       $shipto=$shiptos;
+       
+       $nun=time();
+
+       function ende($nr) {
+               echo "Abbruch: $nr\n";
+               echo "Aufruf: shiptoB.php [login customer|vendor] [test] | [felder]\n";
+               exit($nr);
+       }
+       if ($_POST["ok"]=="Hilfe") {
+               echo "Importfelder:<br>";
+               echo "Feldname => Bedeutung<br>";
+               foreach($contact as $key=>$val) {
+                       echo "$key => $val<br>";
+               }
+               exit(0);
+       };
+
+       clearstatcache ();
+
+       $trenner=($_POST["trenner"])?$_POST["trenner"]:",";
+
+if (!empty($_FILES["Datei"]["name"])) { 
+       $file=$_POST["ziel"];
+       if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file."_shipto.csv")) {
+               $file=false;
+               echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")<br>";
+       } 
+} else if (is_file($_POST["ziel"]."_shipto.csv")) {
+       $file=$_POST["ziel"];
+} else {
+       $file=false;
+} 
+
+if (!$file) ende (2);
+
+if (!file_exists($file."_shipto.csv")) ende(5);
+
+if (!file_exists("../users/$login.conf")) ende(3);
+
+
+$employee=chkUsr($login);
+if (!$employee) ende(4);
+
+if (!$db->chkcol($file)) ende(6);
+
+$f=fopen($file."_shipto.csv","r");
+$zeile=fgets($f,1000);
+$infld=split($trenner,strtolower($zeile));
+$first=true;
+
+
+foreach ($infld as $fld) {
+       $fld = trim(strtr($fld,array("\""=>"","'"=>"")));
+       $in_fld[]=$fld;
+}
+$j=0;
+$zeile=fgetcsv($f,1000,$trenner);
+while (!feof($f)){
+       $i=-1;
+       $firma="";
+       $name=false;
+       $id=false;
+       $sql="insert into shipto ";
+       $keys="(";
+       $vals=" values (";
+       foreach($zeile as $data) {
+               $i++;
+               if ($in_fld[$i]=="firma") { 
+                       $firma=addslashes(trim($data)); 
+                       continue;
+               };
+               if (!in_array($in_fld[$i],$shipto_fld)) {
+                       continue;
+               }
+               $data=addslashes(trim($data));
+               if ($in_fld[$i]=="trans_id" && $data) {
+                       $data=chkKdId($data);
+                       if ($data) $firma="";
+                       if (!$id) $id = $data;
+                       continue;
+               } 
+               if ($in_fld[$i]==$file."number" && $data) {
+                       $tmp=getFirma($data,$file);
+                       if ($tmp) $firma="";
+                       if ($id<>$tmp) $id=$tmp;
+                       continue;
+               } 
+               $keys.=$in_fld[$i].",";
+               
+               if ($data==false or empty($data) or !$data) {
+                        $vals.="null,";
+                } else {
+                       if (in_array($in_fld[$i],array("cp_fax","cp_phone1","cp_phone2"))) {
+                               $data="0".$data;
+                       } else if ($in_fld[$i]=="cp_country" && $data) {
+                               $data=mkland($data);
+                       }
+                       if ($in_fld[$i]=="cp_name") $name=true;
+                        $vals.="'".$data."',";
+                        // bei jedem gefuellten Datenfeld erhoehen
+                        $val_count++;
+                }
+       }
+//     if (!$name) {
+//             $zeile=fgetcsv($f,1000,$trenner);
+//             continue;
+//     }
+       if ($firma) {
+               $data=suchFirma($file,$firma);
+               if ($data) {
+                       $vals.=$data["trans_id"].",";
+                       $keys.="trans_id,";
+               }
+       } else if ($id) {
+                       $vals.=$id.",";
+                       $keys.="trans_id,";
+       }
+       if ($keys<>"(") {
+               if ($test) {
+                       if ($first) {
+                               echo "<table border='1'>\n";
+                               echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
+                               $first=false;
+                       };
+                       $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
+                       echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
+                       flush();
+               } else {
+                       $sql.=substr($keys,0,-1).")";
+                       $sql.=substr($vals,0,-1).")";
+                       $rc=$db->query($sql);
+                       if (!$rc) echo "Fehler: ".$sql."\n";
+               }
+               $j++;
+       };
+       $zeile=fgetcsv($f,1000,$trenner);
+}
+fclose($f);
+echo $j." $file importiert.\n";} else {
+?>
+<p class="listtop">Lieferanschriftimport f&uuml;r die ERP</p>
+<form name="import" method="post" enctype="multipart/form-data" action="shiptoB.php">
+<input type="hidden" name="MAX_FILE_SIZE" value="300000">
+<input type="hidden" name="login" value="<?= $login ?>">
+<table>
+<tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
+<tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
+<tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
+<tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
+<tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
+<tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
+</table>
+</form>
+<? }; ?>