Kosmetik
[kivitendo-erp.git] / lxo-import / contactB.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 Kontaktimport 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 if ($_GET["login"]) {
15         $login=$_GET["login"];
16 } else {
17         $login=$_POST["login"];
18 };
19
20 require ("import_lib.php");
21 $db=new myDB($login);
22 $crm=checkCRM();
23
24 if ($_POST["ok"]) {
25         $login=$_POST["login"];
26         $test=$_POST["test"];
27
28         if ($crm) {
29                 $kunde_fld = array_keys($contactscrm);
30                 $contact=$contactscrm;
31         } else {
32                 $kunde_fld = array_keys($contacts);
33                 $contact=$contacts;
34         }
35         $nun=time();
36
37         function ende($nr) {
38                 echo "Abbruch: $nr\n";
39                 echo "Aufruf: addressS.php [login customer|vendor] [test] | [felder]\n";
40                 exit($nr);
41         }
42         if ($_POST["ok"]=="Hilfe") {
43                 echo "Importfelder:<br>";
44                 echo "Feldname => Bedeutung<br>";
45                 foreach($contact as $key=>$val) {
46                         echo "$key => $val<br>";
47                 }
48                 exit(0);
49         };
50
51         clearstatcache ();
52
53         $trenner=($_POST["trenner"])?$_POST["trenner"]:",";
54
55 if (!empty($_FILES["Datei"]["name"])) { 
56         $file=$_POST["ziel"];
57         if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file."_contact.csv")) {
58                 $file=false;
59                 echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")<br>";
60         } 
61 } else if (is_file($_POST["ziel"]."_contact.csv")) {
62         $file=$_POST["ziel"];
63 } else {
64         $file=false;
65
66
67 if (!$file) ende (2);
68
69 if (!file_exists($file."_contact.csv")) ende(5);
70
71 if (!file_exists("../users/$login.conf")) ende(3);
72
73
74 $employee=chkUsr($login);
75 if (!$employee) ende(4);
76
77 if (!$db->chkcol($file)) ende(6);
78
79 $f=fopen($file."_contact.csv","r");
80 $zeile=fgets($f,1200);
81 $infld=split($trenner,strtolower($zeile));
82 $first=true;
83
84
85 foreach ($infld as $fld) {
86         $fld = trim(strtr($fld,array("\""=>"","'"=>"")));
87         $in_fld[]=$fld;
88 }
89 $j=0;
90 $zeile=fgetcsv($f,1200,$trenner);
91 while (!feof($f)){
92         $i=-1;
93         $firma="";
94         $name=false;
95         $id=false;
96         $sql="insert into contacts ";
97         $keys="(";
98         $vals=" values (";
99         foreach($zeile as $data) {
100                 $i++;
101                 if ($in_fld[$i]=="firma") { 
102                         $firma=addslashes(trim($data)); 
103                         continue;
104                 };
105                 if (!in_array($in_fld[$i],$kunde_fld)) {
106                         continue;
107                 }
108                 $data=addslashes(trim($data));
109                 if ($in_fld[$i]=="cp_cv_id" && $data) {
110                         $data=chkKdId($data);
111                         if ($data) $firma="";
112                         if (!$id) $id = $data;
113                         continue;
114                 } 
115                 if ($in_fld[$i]==$file."number" && $data) {
116                         $tmp=getFirma($data,$file);
117                         if ($tmp) $firma="";
118                         if ($id<>$tmp) $id=$tmp;
119                         continue;
120                 } 
121                 $keys.=$in_fld[$i].",";
122                 
123                 if ($data==false or empty($data) or !$data) {
124                         $vals.="null,";
125                 } else {
126                         if (in_array($in_fld[$i],array("cp_fax","cp_phone1","cp_phone2"))) {
127                                 $data="0".$data;
128                         } else if ($in_fld[$i]=="cp_country" && $data) {
129                                 $data=mkland($data);
130                         }
131                         if ($in_fld[$i]=="cp_name") $name=true;
132                         $vals.="'".$data."',";
133                         // bei jedem gefuellten Datenfeld erhoehen
134                         $val_count++;
135                 }
136         }
137         if (!$name) {
138                 $zeile=fgetcsv($f,1200,$trenner);
139                 continue;
140         }
141         if ($firma) {
142                 $data=suchFirma($file,$firma);
143                 if ($data) {
144                         $vals.=$data["cp_cv_id"].",";
145                         $keys.="cp_cv_id,";
146                 }
147         } else if ($id) {
148                         $vals.=$id.",";
149                         $keys.="cp_cv_id,";
150         }
151         if ($keys<>"(" && $val_count>2) {
152                 if ($test) {
153                         if ($first) {
154                                 echo "<table border='1'>\n";
155                                 echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
156                                 $first=false;
157                         };
158                         $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
159                         echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
160                         flush();
161                 } else {
162                         $sql.=substr($keys,0,-1).")";
163                         $sql.=substr($vals,0,-1).")";
164                         $rc=$db->query($sql);
165                         if (!$rc) echo "Fehler: ".$sql."\n";
166                 }
167                 $j++;
168         };
169         $zeile=fgetcsv($f,1200,$trenner);
170 }
171 fclose($f);
172 echo $j." $file importiert.\n";} else {
173 ?>
174 <p class="listtop">Kontakt-Adressimport f&uuml;r die ERP</p>
175 <form name="import" method="post" enctype="multipart/form-data" action="contactB.php">
176 <input type="hidden" name="MAX_FILE_SIZE" value="300000">
177 <input type="hidden" name="login" value="<?= $login ?>">
178 <table>
179 <tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
180 <tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
181 <tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
182 <tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
183 <tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
184 <tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
185 </table>
186 </form>
187 <? }; ?>