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