Filterkriterium "Kunden-/Lieferantenname" in Berichten auch beim Umsortieren beibehal...
[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 $first=true;
78 $ok=true;
79 foreach ($infld as $fld) {
80         $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
81         if ($fld=="branche" && !$crm) { $in_fld[]=""; continue; };
82         if ($fld=="sw" && !$crm) { $in_fld[]=""; continue; };
83         $in_fld[]=$fld;
84 }
85 //print_r($in_fld); echo "<br>";
86 $j=0;
87 $m=0;
88 $zeile=fgetcsv($f,1200,$trenner);
89 if ($ok) while (!feof($f)){
90         $i=0;
91         //echo "Arbeite an $m    ";
92         $m++;
93         $anrede="";
94         $Matchcode="";
95         $sql="insert into $file ";
96         $keys="(";
97         $vals=" values (";
98         $number=false;
99         foreach($zeile as $data) {
100                 if (!in_array(trim($in_fld[$i]),$kunde_fld)) {
101                         if ($in_fld[$i]=="anrede") {  $anrede=addslashes(trim($data)); }
102                         $i++;
103                         continue;
104                 };
105                 $data=trim($data);
106                 $data=mb_convert_encoding($data,"ISO-8859-15","auto");
107                 //$data=htmlentities($data);
108                 $data=addslashes($data);
109                 if ($in_fld[$i]==$file."number") {  // customernumber || vendornumber
110                         if (empty($data) or !$data) {
111                                 $data=getKdId();
112                                 $number=true;
113                         } else {
114                                 $data=chkKdId($data);
115                                 $number=true;
116                         }
117                 } else if ($in_fld[$i]=="taxincluded"){
118                         $data=strtolower(substr($data,0,1));
119                         if ($data!="f" && $data!="t") $data="f";
120                 } else if ($in_fld[$i]=="ustid"){
121                         $data=strtr(" ","",$data);
122                 } /*else if ($in_fld[$i]=="matchcode") {
123                   $matchcode=$data;
124                   $i++;
125                   continue;
126                 if ($data==false or empty($data) or !$data) {
127                         if (in_array($in_fld[$i],array("name"))) {
128                                 $data=$matchcode;
129                         }
130                 }
131                 }*/
132
133                 $keys.=$in_fld[$i].",";
134                 if ($data==false or empty($data) or !$data) {
135                         $vals.="null,";
136                 } else {
137                         if ($in_fld[$i]=="contact"){
138                                 if ($anrede) {
139                                         $vals.="'$anrede $data',";
140                                 } else {
141                                         $vals.="'$data',";
142                                 }
143                         } else {
144                                 $vals.="'".$data."',";
145                         }
146                 }
147                 $i++;
148         }
149         if (!$number) {
150                 $keys.=$file."number,";
151                 $vals.="'".getKdId()."',";
152         }
153         if ($keys<>"(") {
154                 if ($test) {
155                         if ($first) {
156                                 echo "<table border='1'>\n";
157                                 echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
158                                 $first=false;
159                         };
160                         $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
161                         echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
162                         //echo "Import $j<br>\n";
163                         flush();
164                 } else {
165                         $sql.=$keys."taxzone_id,import)";
166                         $sql.=$vals."0,$nun)";
167                         $rc=$db->query($sql);
168                         if (!$rc) echo "Fehler: ".$sql."<br>";
169                 }
170                 $j++;
171         } else {
172           $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
173           echo "<tr><td style=\"color:red\">".str_replace("null,","null</td><td style=\"color:red\">",$vals)."</td></tr>\n";
174           flush();
175         }
176         $zeile=fgetcsv($f,1200,$trenner);
177 }
178 fclose($f);
179 if ($test) echo "</table>\n ##### = Neue Kunden-/Lieferantennummer\n<br>";
180 echo $j." $file importiert.\n";
181 } else {
182 ?>
183
184 <p class="listtop">Adressimport f&uuml;r die ERP<p>
185 <br>
186 <form name="import" method="post" enctype="multipart/form-data" action="addressB.php">
187 <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
188 <input type="hidden" name="login" value="<?= $login ?>">
189 <table>
190 <tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
191 <tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
192 <tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
193 <tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
194 <tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
195 <tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
196 </table>
197 </form>
198 <? }; ?>