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