5e19f7b4e5e0715926b50d7e2f1901b65da13ca1
[kivitendo-erp.git] / lxo-import / addressB.php
1 <html>
2 <LINK REL="stylesheet" HREF="../css/lx-office-erp.css" TYPE="text/css" TITLE="Lx-Office stylesheet">
3 <body>
4 <?
5 /*
6 Kunden- bzw. Lieferantenimport mit Browser nach Lx-Office ERP
7
8 Copyright (C) 2005
9 Author: Holger Lindemann
10 Email: hli@lx-system.de
11 Web: http://lx-system.de
12
13 */
14
15 if ($_GET["login"]) {
16         $login=$_GET["login"];
17 } else {
18         $login=$_POST["login"];
19 };
20 if ($_POST["ok"]) {
21
22 $nun=time();
23
24 require ("import_lib.php");
25 $db=new myDB($login);
26 $crm=checkCRM();
27
28 function ende($nr) {
29         echo "Abbruch: $nr<br>";
30         echo "Fehlende oder falsche Daten.";
31         exit(1);
32 }
33
34 if ($_POST["ok"]=="Hilfe") {
35         echo "Importfelder:<br>";
36         echo "Feldname => Bedeutung<br>";
37         foreach($address as $key=>$val) {
38                 echo "$key => $val<br>";
39         }
40         exit(0);
41 };
42 clearstatcache ();
43 //print_r($_FILES);
44 $test=$_POST["test"];
45 if (!empty($_FILES["Datei"]["name"])) { 
46         $file=$_POST["ziel"];
47         if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file.".csv")) {
48                 $file=false;
49                 echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")<br>";
50         } 
51 } else if (is_file($_POST["ziel"].".csv")) {
52         $file=$_POST["ziel"];
53 } else {
54         $file=false;
55
56
57 if (!$file) ende (2);
58
59 $trenner=($_POST["trenner"])?$_POST["trenner"]:",";
60 //echo "../users/$login.conf";
61 if (!file_exists("../users/$login.conf")) ende(3);
62
63 if (!file_exists("$file.csv")) ende(5);
64
65 $db=new myDB($login);
66
67 if (!$db->chkcol($file)) ende(6);
68
69 $employee=chkUsr($login);
70 if (!$employee) ende(4);
71
72 $kunde_fld = array_keys($address);
73
74 $f=fopen("$file.csv","r");
75 $zeile=fgets($f,1200);
76 $infld=split($trenner,strtolower($zeile));
77 //echo "$zeile<br>";
78 //print_r($infld); echo "<br>";
79 $first=true;
80 $ok=true;
81 foreach ($infld as $fld) {
82         $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
83         if ($fld=="branche" && !$crm) { $in_fld[]=""; continue; };
84         if ($fld=="sw" && !$crm) { $in_fld[]=""; continue; };
85         $in_fld[]=$fld;
86 }
87 //print_r($in_fld); echo "<br>";
88 $j=0;
89 $m=0;
90 $zeile=fgetcsv($f,1200,$trenner);
91 if ($ok) while (!feof($f)){
92         $i=0;
93         //echo "Arbeite an $m    ";
94         $m++;
95         $anrede="";
96         $Matchcode="";
97         $sql="insert into $file ";
98         $keys="(";
99         $vals=" values (";
100         $number=false;
101         foreach($zeile as $data) {
102                 if (!in_array(trim($in_fld[$i]),$kunde_fld)) {
103                         if ($in_fld[$i]=="anrede") {  $anrede=addslashes(trim($data)); }
104                         $i++;
105                         continue;
106                 };
107                 $data=trim($data);
108                 //$data=htmlentities($data);
109                 $data=addslashes($data);
110                 if (trim($in_fld[$i])==$file."number") {  // customernumber || vendornumber
111                         if (empty($data) or !$data) {
112                                 $data=getKdId(); 
113                                 $number=true;
114                         } else {
115                                 $data=chkKdId($data);
116                                 $number=true;
117                         }
118                 } else if ($in_fld[$i]=="taxincluded"){
119                         $data=strtolower(substr($data,0,1));
120                         if ($data!="f" && $data!="t") $data="f";
121                 } else if ($in_fld[$i]=="language") {
122                         $data=strtolower(substr($data,0,2));
123                         if (!in_array($data,array("de","en","fr"))) $data=false;
124                 }
125                 if ($in_fld[$i]=="matchcode") {  
126                   $matchcode=$data;
127                   $i++;
128                   continue;
129                 }
130
131                 if ($data==false or empty($data) or !$data) {
132                         if (in_array($in_fld[$i],array("name"))) {
133                                 $data=$matchcode;
134                         }                
135                 }
136                 $keys.=$in_fld[$i].",";
137                 if ($data==false or empty($data) or !$data) {
138                         if (in_array($in_fld[$i],array("name"))) {
139                                 $keys="(";
140                                 break;
141                         }
142                         $vals.="null,";
143                 } else {
144                         if ($in_fld[$i]=="contact"){
145                                 if ($anrede) {
146                                         $vals.="'$anrede $data',";
147                                 } else {
148                                         $vals.="'$data',";
149                                 }
150                         } else {
151                                 $vals.="'".$data."',";
152                         }
153                 }
154                 $i++;
155         }
156         if (!$number) {
157                 $keys.=$file."number,";
158                 $vals.="'".getKdId()."',";
159         }
160         if ($keys<>"(") {
161                 if ($test) {
162                         if ($first) {
163                                 echo "<table border='1'>\n";
164                                 echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
165                                 $first=false;
166                         };
167                         $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
168                         echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
169                         //echo "Import $j<br>\n";
170                         flush();
171                 } else {
172                         $sql.=$keys."import)";
173                         $sql.=$vals."$nun)";            
174                         $rc=$db->query($sql);
175                         if (!$rc) echo "Fehler: ".$vals."<br>";
176                 }
177                 $j++;
178         } else {
179           $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
180           echo "<tr><td style=\"color:red\">".str_replace("null,","null</td><td style=\"color:red\">",$vals)."</td></tr>\n";
181           flush();
182         }
183         $zeile=fgetcsv($f,1200,$trenner);
184 }
185 fclose($f);
186 if ($test) echo "</table>\n ##### = Neue Kunden-/Lieferantennummer\n<br>";
187 echo $j." $file importiert.\n";
188 } else {
189 ?>
190
191 <p class="listtop">Adressimport f&uuml;r die ERP<p>
192 <br>
193 <form name="import" method="post" enctype="multipart/form-data" action="addressB.php">
194 <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
195 <input type="hidden" name="login" value="<?= $login ?>">
196 <table>
197 <tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
198 <tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
199 <tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
200 <tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
201 <tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
202 <tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
203 </table>
204 </form>
205 <? }; ?>