forked from kivitendo/kivitendo-crm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebayImporter.php
345 lines (297 loc) · 10.6 KB
/
ebayImporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/***************************************************************************************************
***** EbayImporter: Importiert Kundendaten und Rechnungsdaten der EbayCSV-Exporte in Kivitendo *****
***************************************************************************************************/
//Script im Alpha-Stadium
//ToDo0: Nur Mitglieder der Gruppe Admin erlauben, naxFilesize beschränken
//ToDo1: 3 Phasen realisieren: Upload+Einstellung, Darstellung+Korrektur, Datenimport
//ToDo2: Rechnungen importieren
// ID tax_zones für Inland holen und ersetzen
require_once("inc/stdLib.php");
require_once("inc/crmLib.php");
include("inc/template.inc");
require_once("inc/FirmenLib.php");
$menu = $_SESSION['menu'];
$head = mkHeader();
echo '<html>
<head><title></title>';
echo $menu['stylesheets'];
echo $menu['javascripts'];
echo $head['CRMCSS'];
echo $head['JQUERY'];
echo $head['JQUERYUI'];
echo $head['THEME'];
echo $head['JQTABLE'];
?>
<style type="text/css">
tr.bgcol1 {background-color:#efd; }
td.bgcol2 {background-color:#666; }
table.bgcol1 th {
background-color: #e3e3e3;
}
</style>
<script>
$(function() {
$('#treffer')
.tablesorter( {widthFixed: true, widgets: ['zebra']})
.tablesorterPager({container: $("#pager"), size: 120, positionFixed: false})
});
</script>
<?php
//String aufspalten
function mehrfachexplode ($trenner,$string) {
$temp = str_replace($trenner, $trenner[0], $string);
$getrennt = explode($trenner[0], $temp);
return $getrennt;
}
//Straßennamen korrigieren
function korrektur ($street){
// Status gibt die Formatierung der Straße an, ob mit Trennzeichen oder ohne etc
$tempstreet = "";
$korrekt = "";
$sfeld = array();
//Hausnummern separieren
$streetarray = str_split($street);
for($i = 0; $i < count($streetarray); $i++) {
//Ist Arrayelement eine Zahl
if(ctype_digit($streetarray[$i])){
//Ist vorherige Arrayelement eine Zahl
if(ctype_digit($streetarray[$i-1])){
//Ist das Arrayelement dannach eine Zahl
if(ctype_digit($streetarray[$i+1])){
$tempstreet.=$streetarray[$i];
}
//Das Arrayelement dannach ist keine Zahl
else{
$tempstreet.=$streetarray[$i]." ";
}
}
//Ist das Arrayelement dannach eine Zahl
elseif(ctype_digit($streetarray[$i+1])) {
$tempstreet.=" ".$streetarray[$i];
}
//Das Arrayelement dananch ist keine Zahl
else{
$tempstreet.=" ".$streetarray[$i]." ";
}
}
//Arrayelement ist keine Zahl
else{
$tempstreet.=$streetarray[$i];
}
}
//Straßenstring aufspalten
//echo $tempstreet;
$sfeld = mehrfachexplode(array("-"," "), $tempstreet);
$last = count($sfeld)-1;
//Leerzeichen am Ende entfernen
if(empty($sfeld[$last])){
array_pop($sfeld);
}
//print_r($sfeld);
// - Trenner erkennen.. Straßennamen mit - Trenner
if(strpos($tempstreet,'-')){
//Straßenstring zusammensetzen
for($i = 0; $i < count($sfeld); $i++) {
//Haunummer + mögliches Leerzeichen für Buchstabe
if(ctype_digit($sfeld[$i])){
$korrekt.=$sfeld[$i]." ";
}
elseif(empty($sfeld[$i+1]) || ctype_digit($sfeld[$i+1])){
$korrekt.=ucwords(strtolower($sfeld[$i]))." ";
}
elseif(!(empty($sfeld[$i]))){
$korrekt.=ucwords(strtolower($sfeld[$i]))."-";
}
}
}
// Straßennamen ohne "-"
else{
$zaehler = 0;
$lastword;
$hausnummerpos;
$letzteszeichen = (count($sfeld)-1);
//Position der Hausnummer bestimmen
foreach($sfeld as $adressteil){
if(ctype_digit($adressteil)){
$hausnummerpos = $zaehler;
}
$zaehler++;
}
//Letztes Wort bestimmten
for($i = 0; $i < $hausnummerpos; $i++) {
if(!empty($sfeld[$i])){
$lastword = $i;
}
}
//echo "LetztesZeichen: ".$letzteszeichen."</br>";
//echo "Hausnummerposition: ".$hausnummerpos."</br>";
//echo "LEtzteswortposition: ".$lastword."</br>";
//print_r($sfeld);
//Straßenstring zusammensetzen
for($i = 0; $i < count($sfeld); $i++) {
//erstes Adresswort
if($i == 0){
$korrekt.=ucwords(strtolower($sfeld[$i]))." ";
}
//letzes Adresswort / möglicher Hausnummerbuchstabe
elseif($i == $letzteszeichen){
$korrekt.=ucwords(strtolower($sfeld[$i]));
}
//Test ob Adressteil vor der Hausnummer erreicht wurde
elseif($i == $lastword){
$korrekt.=ucwords(strtolower($sfeld[$i]))." ";
}
else{
$korrekt.=strtolower($sfeld[$i])." ";
}
}
}
return $korrekt;
}
$form_select_file = '
</head>
<body>'.
$menu['pre_content'].
$menu['start_content'].
'<div class="ui-widget-content" style="height:600px">
<p class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0.6em;">EbayImporter</p>
<table width="600">
<form action="ebayImporter.php" method="post" enctype="multipart/form-data">
<tr>
<td width="20%">.:Select file:.</td>
<td width="80%"><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td>.:Submit:.</td>
<td><input type="submit" name="select_file" /></td>
</tr>
</form>
</table>
</div>'.
$menu['end_content'];
;
if ( !$_POST['select_file'] ) echo $form_select_file; // in Phase 3 $_POST['select_file'] = true;
if ($_FILES['file']['type'] != 'text/csv') exit();
move_uploaded_file($_FILES['file']['tmp_name'], "tmp/import-src.csv");
//Datei nach UTF8 konvertieren
system("file tmp/import-src.csv | grep 'UTF-8'; echo $?>test_utf8");
$dateihandle = fopen("test_utf8","r");
$zeichen = fgetc($dateihandle);
fclose($dateihandle);
//echo "*****************************************";
//echo $zeichen;
//readfile("test_utf8");
//echo "*****************************************";
// Uploaded file is UTF-8 encoded? i know, it's ugly, but it's working.
if("$zeichen"!="0"){
//echo "NOT UTF-8";
// die daten sind anscheinend ISO-8859-2 (laut chardet)
$command = "iconv -f ISO-8859-15 -t UTF8 -c -o tmp/import-utf8.csv tmp/import-src.csv";
#$command = "iconv -f ISO-8859-15 -t UTF8 -c -o upload/import-src.csv upload/import-utf8.csv";
system($command);
}
else {
echo "UTF-8";
system("cp tmp/import-src.csv tmp/import-utf8.csv");
}
//Testkommentar
//Ausführen:
system($command);
//$data = array();
$row = 1;
if (($handle = fopen("tmp/import-utf8.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
//$num = count($data);
//echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
//for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
//}
$csvArray[$row] = $data;
}
fclose($handle);
}
//BusinessIds ermitteln
$end = "Endverbraucher"; //ANPASSEN
$sql = "SELECT id FROM business WHERE description ILIKE '$end'";
$rs = $GLOBALS['dbh']->getOne($sql);
$end_id = $rs['id'];
$sql = "SELECT id FROM leads WHERE lead ILIKE 'ebay'";
$rs = $GLOBALS['dbh']->getOne($sql);
$ebayLeadId = $rs['id'];
//SELECT id FROM payment_terms WHERE description ILIKE 'paypal' OR description_long ILIKE '%paypal%';
$sql = "SELECT id FROM payment_terms WHERE description ILIKE 'paypal' OR description_long ILIKE '%paypal%'";
$rs = $GLOBALS['dbh']->getOne($sql);
$paypalId = $rs['id'];
$sql = "SELECT id FROM payment_terms WHERE description ILIKE 'vorkasse' OR description_long ILIKE '%vorkasse%'";
$rs = $GLOBALS['dbh']->getOne($sql);
$payotherId = $rs['id'];
array_shift($csvArray);//Erste Zeile löschen
//print_r($csvArray);
echo "</head><body>".$menu['pre_content'].$menu['start_content'];
echo "<table id='treffer' class='tablesorter'>\n";
echo "<thead><tr ><th>Ebayname</th><th>Name</th><th>Anschrift</th><th>Email</th><th>1.Artikel</th></tr></thead>\n<tbody>\n";
$i = 0;
/*
echo "<pre>";
print_r ($csvArray);
echo "</pre>";
*/
if ($csvArray) foreach($csvArray as $key => $row) {
$ok = true;
if( $row['4'] == $row['2'] ){
$row['4'] = $row['5'];
$row['5'] = '';
$row['attention'] = "!!!changed by Importer!!!";
}
if ($row['5'] != '') {
$row['6'] .= " ".$row['5'];
$row['5'] = '';
$row['attention'] = "!!!changed by Importer!!!";
}
$row['2'] = ucwords($row['2']);
//Straßennamen formatieren
//Am Berg 8, Wilhelm-Leuchner-Straße, An der kleinen Gasse usw..
$row['4'] = korrektur($row['4']);
$row['6'] = ucwords($row['6']);
$row['9'] = $row['9'][0]; //Deutschland to D
$del = false;
//Dublikate entsorgen
if (!$row['1'] && !$row['2']) {
unset($csvArray[$row]);
//break -1;
$ok = false;
}
echo "row: ".$csvArray[$key - 1]['1'];
if ( $csvArray[$key ]['1'] == $csvArray[$key + 1]['1'] ){
unset($csvArray[$key]);
$ok = false;
}
if ($ok) {
$i++;
$sql = "SELECT * FROM customer WHERE name ILIKE '".$row["2"]."' AND zipcode = '".$row["8"]."' OR department_1 ILIKE '".$row["1"]."' OR email ILIKE '".$row["3"]."'";
$rs=$GLOBALS['dbh']->getAll($sql);
echo "Vorhanden??: ".$rs[0]['name']."<br />";
echo "<tr class='bgcol2'>".
"<td class=\"liste\">".$row["1"]."</td><td class=\"liste\">".$row["2"]."</td>".
"<td class=\"liste\">".$row["4"].$row["5"].", ".$row["8"]." ".$row["6"]."</td><td class=\"liste\">".$row["3"]."</td><td class=\"liste\">".$row["13"]."</td>
<td class=\"liste\">".$row['attention']."</td><td class=\"liste\">".$row['9']."</td><td class=\"liste\">".$row['20']."</td></tr>\n";
$i++;
if (!$rs['0']) {
$CustNb = newnr('customer',$end_id);
echo "Nummer: ".$CustNb;
$payment = $row['20']=='PayPal'?$paypalId:$payotherId;
$sql = "INSERT INTO customer (email, name, department_1, street, zipcode, city, country, business_id, customernumber, lead, payment_id, currency_id, taxzone_id ) VALUES ";
$sql.= "('".$row['3']."', '".$row["2"]."', '".$row["1"]."', '".$row["4"].$row["5"]."', '".$row["8"]."','".$row["6"]."', '".$row['9']."', ".$end_id.", '".$CustNb."', ".$ebayLeadId.", ".$paypalId." , 1 , 4)";
echo "SQL: ".$sql;
$rcc = $GLOBALS['dbh']->query($sql);
}
}
//$row['3']
}
echo "</tbody></table>\n";
echo $menu['end_content'];
echo '</body>';
echo '</html>';
?>