Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision bd10a269

Von Holger Lindemann vor etwa 18 Jahren hinzugefügt

  • ID bd10a269df8a3de2653d02b8771459a4e34165a9
  • Vorgänger 68efe1a8
  • Nachfolger 98255c88

Artikelimport, neu

Unterschiede anzeigen:

lxo-import/partsB.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
Warenimport mit Browser nach Lx-Office ERP
7
Henry Margies <h.margies@maxina.de>
8
Holger Lindemann <hli@lx-system.de>
9
*/
10

  
11
/* get login via GET or POST */
12
if ($_GET["login"]) {
13
	$login=$_GET["login"];
14
} else {
15
	$login=$_POST["login"];
16
};
17

  
18
require ("import_lib.php");
19
/* get DB instance */
20
$db=new myDB($login);
21

  
22

  
23
/* just display page or do real import? */
24
if ($_POST["ok"]) {
25

  
26

  
27
require ("parts_import.php");
28

  
29
function ende($nr) {
30
	echo "Abbruch: $nr<br>";
31
	echo "Fehlende oder falsche Daten.";
32
	exit(1);
33
}
34

  
35
/* display help */
36
if ($_POST["ok"]=="Hilfe") {
37
	echo "Importfelder:<br>";
38
	echo "Feldname => Bedeutung<br>";
39
	foreach($parts as $key=>$val) {
40
		echo "$key => $val<br>";
41
	}
42
	echo "Jeder Artikel mu&szlig; einer Buchungsgruppe zugeordnet werden. ";
43
	echo "Dazu mu&szlig; entweder in der Maske eine Standardbuchungsgruppe gew&auml;hlt werden <br>";
44
	echo "oder es wird ein g&uuml;ltiges Konto in 'income_accno_id' und 'expense_accno_id' eingegeben. ";
45
	echo "Das Programm versucht dann eine passende Buchungsgruppe zu finden.";
46
	exit(0);
47
};
48

  
49
clearstatcache ();
50

  
51
$test    = $_POST["test"];
52
$trenner = ($_POST["trenner"])?$_POST["trenner"]:",";
53
$file    = "parts";
54

  
55
/* no data? */
56
if (empty($_FILES["Datei"]["name"]))
57
	ende (2);
58

  
59
/* copy file */
60
if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file.".csv")) {
61
	echo "Upload von Datei fehlerhaft.";
62
	echo $_FILES["Datei"]["error"], "<br>";
63
	ende (2);
64
} 
65

  
66
/* ??? */
67
if (!file_exists("../users/$login.conf")) 
68
	ende(3);
69

  
70
/* check if file is really there */
71
if (!file_exists("$file.csv")) 
72
	ende(5);
73

  
74
/* ??? */
75
if (!$db->chkcol($file)) 
76
	ende(6);
77

  
78
/* ??? */
79
if (!chkUsr($login))
80
	ende(4);
81

  
82
/* first check all elements */
83
echo "Checking data:<br>";
84
$err = import_parts($db, $file, $trenner, $parts, TRUE, FALSE, FALSE,$_POST);
85
echo "$err Errors found\n";
86

  
87

  
88
if ($err!=0)
89
	exit(0);
90

  
91
/* just print data or insert it, if test is false */
92
import_parts($db, $file, $trenner, $parts, FALSE, !$test, TRUE,$_POST);
93

  
94
} else {
95
	$bugrus=getAllBG($db);
96
?>
97

  
98
<p class="listtop">Artikelimport f&uuml;r die ERP<p>
99
<br>
100
<form name="import" method="post" enctype="multipart/form-data" action="partsB.php">
101
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
102
<input type="hidden" name="login" value="<?= $login ?>">
103
<table>
104
<tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
105
<tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
106
<tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
107
<tr><td>Art</td><td><input type="Radio" name="ware" value="W">Ware &nbsp; 
108
		    <input type="Radio" name="ware" value="D">Dienstleistung
109
		    <input type="Radio" name="ware" value="G" checked>gemischt (Spalte 'art' vorhanden)</td></tr>
110
<tr><td>Default Bugru<br></td><td><select name="bugru">
111
<?	if ($bugrus) foreach ($bugrus as $bg) { ?>
112
			<option value="<?= $bg["id"] ?>"><?= $bg["description"] ?>
113
<?	} ?>
114
	</select>
115
	<input type="radio" name="bugrufix" value="0" checked>nie<br>
116
	<input type="radio" name="bugrufix" value="1">f&uuml;r alle Artikel verwenden
117
	<input type="radio" name="bugrufix" value="2">f&uuml;r Artikel ohne passende Bugru
118
	</td></tr>
119
<tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
120
<tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
121
</table>
122
</form>
123
<? }; ?>
lxo-import/parts_import.php
1
<?
2
//Henry Margies <h.margies@maxina.de>
3
//Holger Lindemann <hli@lx-system.de>
4

  
5
/**
6
 * Returns ID of a partgroup (or adds a new partgroup entry)
7
 * \db is the database
8
 * \value is the partgroup name
9
 * \add if true and partgroup does not exist yet, we will add it automatically
10
 * \returns partgroup id or "" in case of an error
11
 */
12
function getPartsgroupId($db, $value, $add) {
13
	
14
	$sql="select id from partsgroup where partsgroup = '$value'";
15
	$rs=$db->getAll($sql);
16
	if (empty($rs[0]["id"]) && $add) {
17
		$sql="insert into partsgroup (partsgroup) values ('$value')";
18
		$rc=$db->query($sql);
19
		if (!$rc)
20
			return "";
21
		return getPartsgroupId($db, $value, 0);
22
	}
23
	return $rs[0]["id"];
24
}
25

  
26
function getAccnoId($db, $accno) {
27
	$sql = "select id from chart where accno='$accno'";
28
	$rs=$db->getAll($sql);
29
	return $rs[0]["id"];
30
}
31

  
32
function chkPartNumber($db,$number,$check) {
33
	if ($number<>"") {
34
		$sql = "select * from parts where partnumber = '$number'";
35
		$rs=$db->getAll($sql);
36
	}
37
	//echo $sql; print_r($rs);
38
	if ($rs[0]["id"]>0 or $number=="") {
39
		if ($check) return "check";
40
		$rc=$db->query("BEGIN");
41
		$sql = "select  articlenumber from defaults";
42
		$rs=$db->getAll($sql);
43
		$number=$rs[0]["articlenumber"]+1;
44
		$sql = "update defaults set articlenumber = '$number'";
45
		$rc=$db->query($sql);
46
		$rc=$db->query("COMMIT");
47
		$sql = "select * from parts where partnumber = '$number'";
48
		$rs=$db->getAll($sql);
49
		if ($rs[0]["id"]>0) return "";
50
	}
51
	return $number;
52
}
53

  
54
function getBuchungsgruppe($db, $income, $expense) {
55
	
56
	$income_id = getAccnoId($db, $income);
57
	$expense_id = getAccnoId($db, $expense);
58
	//$accno0_id = getAccnoId($db, $accno0);
59
	//$accno1_id = getAccnoId($db, $accno1);
60
	//$accno3_id = getAccnoId($db, $accno3);
61

  
62
	$sql  = "select id from buchungsgruppen where ";
63
	$sql .= "income_accno_id_0 = $income_id and ";
64
	$sql .= "expense_accno_id_0 = $expense_id ";
65
	//$sql .= "income_accno_id_0 = '$accno0_id' ";
66
	//$sql .= "and income_accno_id_1 = '$accno1_id' ";
67
	//$sql .= "and income_accno_id_3 = '$accno3_id'";
68
	$rs=$db->getAll($sql);
69
	return $rs[0]["id"];
70
}
71

  
72

  
73
function getFromBG($db, $bg_id, $name) {
74
	
75
	$sql  = "select $name from buchungsgruppen where id='$bg_id'";
76
	$rs=$db->getAll($sql);
77
	return $rs[0][$name];
78
}
79

  
80
function existUnit($db, $value) {
81
	$sql="select name from units where name = '$value'";
82
	$rs=$db->getAll($sql);
83
	if (empty($rs[0]["name"]))
84
		return FALSE;
85
	return TRUE;
86
}
87

  
88
function show($show, $things) {
89
	if ($show)
90
		echo $things;
91
}
92

  
93
function import_parts($db, $file, $trenner, $fields, $check, $insert, $show,$maske) {
94

  
95
	/* field description */
96
	$parts_fld = array_keys($fields);
97

  
98
	/* open csv file */
99
	$f=fopen("$file.csv","r");
100
	
101
	/*
102
	 * read first line with table descriptions
103
	 */
104
	show( $show, "<table border='1'><tr>\n");
105
	$infld=fgetcsv($f,1200,$trenner);
106
	foreach ($infld as $fld) {
107
		$fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
108
		$in_fld[]=$fld;
109
		if (in_array(trim($fld),$parts_fld)) {
110
			show( $show, "<td>$fld</td>\n");
111
		}
112
	}
113

  
114
	$m=0;		/* line */
115
	$errors=0;	/* number of errors detected */
116
	$income_accno = "";
117
	$expense_accno = "";
118
	while ( ($zeile=fgetcsv($f,1200,$trenner)) != FALSE) {
119

  
120
		$i=0;	/* column */
121
	        $m++;	/* increase line */
122

  
123
		$sql="insert into $file ";
124
		$keys="(";
125
		$vals=" values (";
126

  
127
		show( $show, "<tr>\n");
128

  
129
		/* for each column */
130
		$dienstleistung=false;
131
		$artikel=-1;
132
		$partNr=false;
133
		foreach($zeile as $data) {
134
			/* check if column will be imported */
135
			if (!in_array(trim($in_fld[$i]),$parts_fld)) {
136
				$i++;
137
				continue;
138
			};
139
			$data=trim($data);
140
			$data=addslashes($data);
141
			$key=$in_fld[$i];
142
			/* add key and data */
143
			if ($data==false or empty($data) or !$data) {
144
				show( $show, "<td>NULL</td>\n");
145
				$i++;
146
				continue;
147
			}
148

  
149
			/* special case partsgroup */
150
			if ($key == "partsgroup") {
151

  
152
				/* get ID of partsgroup or add new 
153
				 * partsgroup_id */
154
				$data = getPartsgroupId($db, $data, $insert);
155
				$key  = "partsgroup_id";
156

  
157
				/* TODO error handling */
158

  
159
			} else if ($key == "lastcost" || 
160
				   $key == "sellprice") {
161
				
162
				/* convert 0,0 numeric into 0.0 */
163
				$data = str_replace(",", ".", $data);
164

  
165
			} else if ($key == "partnumber") {
166
				$partNr=true;
167
				$partnumber=chkPartNumber($db,$data,$check);
168
				if ($partnumber=="") {
169
					show( $show, "<td>NULL</td>\n");
170
					$i++;
171
					continue;
172
				} else {
173
					//$keys.="partnumber, ";
174
					$data=$partnumber;
175
					//show( $show, "<td>$partnumber</td>\n");
176
				}
177
			} else if ($key == "description") {
178
				$data=addslashes($data);
179
			} else if ($key == "notes") {
180
				$data=addslashes($data);
181
			} else if ($key == "unit") {
182
				/* convert st?ck and Stunde */
183
				if (preg_match("/^st..?ck$/i", $data))
184
					$data = "Stck";
185
				else if ($data == "Stunde")
186
					$data = "Std";
187
				/* check if unit exists */
188
				if (!existUnit($db, $data)) {
189
					echo "Error in line $m: ";
190
					echo "Einheit <b>$data</b> existiert nicht ";
191
					echo "Bitte legen Sie diese Einheit an<br>";
192
					$errors++;
193
				}
194
			} else if ($key == "art") {
195
				if ($maske["ware"]=="G" and strtoupper($data)=="D") { $artikel=false; }
196
				else if ($maske["ware"]=="G") { $artikel=true; };
197
				$i++;
198
				continue;
199
			} else if ($key == "income_accno") {
200
				$income_accno = $data;
201
				$i++;
202
				show( $show, "<td>$data</td>\n");
203
				continue;
204
			} else if ($key == "expense_accno") {
205
				$expense_accno = $data;
206
				$i++;
207
				show( $show, "<td>$data</td>\n");
208
				continue;
209
			}
210
			/* convert JA to Yes */
211
			if ($data == "J" )
212
				$data = "Y";
213

  
214
			$vals.="'".$data."',";
215
			show( $show, "<td>$data</td>\n");
216
			$keys.=$key.",";
217
	
218
			$i++;
219
		}
220
		if ($artikel==-1) {
221
			if ($maske["ware"]=="D") {  $artikel=false; }
222
			else { $artikel=true; };			
223
		}		
224
		if ($maske["bugrufix"]==1) {
225
			$bg = $maske["bugru"];
226
		} else {
227
			/* search for buchungsgruppe */
228
			$bg = getBuchungsgruppe($db, $income_accno, $expense_accno);
229
		}
230
		/* nothing found? user must create one */
231
		if ($bg == "") {
232
			if ($maske["bugrufix"]==2) {
233
				$bg = $maske["bugru"];
234
			} else {
235
				echo "Error in line $m: ";
236
				echo "Keine Buchungsgruppe gefunden f?r <br>";
237
				echo "Erl?se Inland: $income_accno<br>";
238
				//echo "Erl?se EU: $income_accno_1<br>";
239
				//echo "Erl?se Ausland: $income_accno_3<br>";
240
				echo "Bitte legen Sie eine an<br>";
241
				echo "<br>";
242
				$errors++;
243
			}
244
		} 
245
		if ($bg > 0) {
246
			/* found one, add income_accno_id etc from buchungsgr.
247
			 */
248
			$keys.="buchungsgruppen_id, ";
249
			$vals.="'$bg', ";
250
			/* XXX nur bei artikel!!! */
251
			if ($artikel) {
252
				$keys.="inventory_accno_id, ";
253
				$vals.=getFromBG($db, $bg, "inventory_accno_id")." ,";
254
			};
255
			$keys.="income_accno_id, ";
256
			$vals.=getFromBG($db, $bg, "income_accno_id_0")." ,";
257
			$keys.="expense_accno_id,";
258
			$vals.=getFromBG($db, $bg, "expense_accno_id_0")." ,";
259
		}
260
		if ($partNr==false) {
261
			$partnumber=chkPartNumber($db,"",$check);
262
			if ($partnumber=="") {
263
				show( $show, "<td>NULL</td>\n");
264
				$errors++;
265
			} else {
266
				$keys.="partnumber, ";
267
				$vals.="'$partnumber',";
268
				show( $show, "<td>$partnumber</td>\n");
269
			}
270
		} 
271
		$sql.=$keys."import)";
272
		$sql.=$vals.time().")";		
273
		//show( $show, "<td> $sql </td>\n");
274

  
275
		if ($insert) {
276
			show( $show, "<td>");
277
			$db->showErr = TRUE;
278
			$rc=$db->query($sql);
279
			if (!$rc)
280
				echo "Fehler";
281
			show( $show, "</td>\n");
282
		}
283

  
284
		show( $show, "</tr>\n");
285
	}
286

  
287
	show( $show, "</table>\n");
288
	fclose($f);
289
	return $errors;
290
}
291

  
292
?>
293

  

Auch abrufbar als: Unified diff