2 //Henry Margies <h.margies@maxina.de>
3 //Holger Lindemann <hli@lx-system.de>
6 * Returns ID of a partgroup (or adds a new partgroup entry)
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
12 function getPartsgroupId($db, $value, $add) {
13 $sql="select id from partsgroup where partsgroup = '$value'";
14 $rs=$db->getAll($sql);
15 if (empty($rs[0]["id"]) && $add) {
16 $sql="insert into partsgroup (partsgroup) values ('$value')";
20 return getPartsgroupId($db, $value, 0);
24 function insertParts($db,$insert,$show,$data) {
27 show($data["partnumber"]); show($data["lastcost"]); show($data["sellprice"]);
28 show($data["description"]); show(substr($data["notes"],0,25));show($data["ean"]);
29 show($data["weight"]); show($data["image"]); show($data["partsgroup_id"]);
30 show($data["buchungsgruppen_id"]);show($data["income_accno"]); show($data["expense_accno"]);
31 show($data["inventory_accno"]); show($data["microfiche"]); show($data["drawing"]);
32 show($data["rop"]); show($data["assembly"]); show($data["makemodel"]);
35 /*foreach ($data as $key=>$val) {
36 echo $key.":".gettype($val).":".gettype($data[$key]).":".$val."<br>";
39 $sqlIa = 'INSERT INTO parts (';
40 $sqlIa .= 'partnumber,description,notes,ean,unit,';
41 $sqlIa .= 'weight,image,sellprice,lastcost,partsgroup_id,';
42 $sqlIa .= 'buchungsgruppen_id,income_accno_id,expense_accno_id,inventory_accno_id,';
43 $sqlIa .= 'microfiche,drawing,rop,assembly,shop,makemodel,import) ';
44 $sqlIa .= 'VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
45 $data["import"]=time();
46 $rc=$db->execute($sqlIa,$data);
54 show('<font color="red">error</font>');
55 show('</tr>'."\n",false);
59 function updPrice($db,$insert,$show,$partnumber,$lastcost,$sellprice,$shop) {
62 show($partnumber); show($lastcost); show($sellprice);
65 $sqlPr = 'UPDATE PARTS SET ';
66 $sqlPr .= 'sellprice = ?, lastcost = ?, shop = ? ';
67 $sqlPr .= 'WHERE partnumber = ?';
68 $rc=$db->execute($sqlPr,array("sellprice"=>$sellprice,"lastcost"=>$lastcost,"shop"=>$shop,"partnumber"=>$partnumber));
76 show('<font color="red">error</font>');
77 show('</tr>'."\n",false);
81 function updParts($db,$insert,$show,$partnumber,$lastcost,$sellprice,
82 $description,$notes,$ean,$weight,$image,
83 $partsgroup_id, $shop) {
86 show($partnumber); show($lastcost); show($sellprice);
87 show($description); show(substr($notes,0,25));show($ean);
88 show($weight); show($image); show($partsgroup_id);
91 $sqlUa = 'UPDATE PARTS SET ';
92 $sqlUa .= 'description = ?, notes = ?, ean = ?, weight = ?, image = ?, ';
93 $sqlUa .= 'sellprice = ?, lastcost = ?, partsgroup_id = ?, shop = ? ';
94 $sqlUa .= 'WHERE partnumber = ?';
95 $rc=$db->execute($sqlUa,array($description,$notes,$ean,$weight,$image,
96 $sellprice,$lastcost,$partsgroup_id,$shop,$partnumber));
104 show('<font color="red">error</font>');
105 show('</tr>'."\n",false);
110 function getMakemodel($db,$check,$hersteller,$model,$partsid,$add=true) {
111 $sql="select * from makemodel where make = $hersteller and model = '$model' and parts_id = $partsid";
112 $rs=$db->getAll($sql);
113 if (empty($rs[0]["id"]) && $add) {
114 $sql="insert into makemodel (parts_id,make,model) values ($partsid,'$hersteller','$model')";
115 $rc=$db->query($sql);
119 function getAccnoId($db, $accno) {
120 $sql = "select id from chart where accno='$accno'";
121 $rs=$db->getAll($sql);
125 function getPartsid($db,$number) {
126 $sql = "select id from parts where partnumber = '$number'";
127 $rs=$db->getAll($sql);
128 if ($rs[0]["id"]>0) {
135 function newPartNumber($db,$check) {
136 if ($check) return "check";
137 $rc=$db->query("BEGIN");
138 $sql = "select articlenumber from defaults";
139 $rs=$db->getAll($sql);
140 if ($rs[0]["articlenumber"]) {
141 preg_match("/([^0-9]+)?([0-9]+)([^0-9]+)?/", $rs[0]["articlenumber"] , $regs);
143 $number=$regs[1].($regs[2]+1).$regs[3];
145 $sql = "update defaults set articlenumber = '$number'";
146 $rc=$db->query($sql);
147 $rc=$db->query("COMMIT");
148 //Prüfen ob die Nummer nicht doch schon vergeben ist.
149 $sql = "select * from parts where partnumber = '$number'";
150 $rs=$db->getAll($sql);
151 if ($rs[0]["id"]>0) return "";
155 function getBuchungsgruppe($db, $income, $expense) {
156 $income_id = getAccnoId($db, $income);
157 $expense_id = getAccnoId($db, $expense);
158 $sql = "select id from buchungsgruppen where ";
159 $sql .= "income_accno_id_0 = $income and ";
160 $sql .= "expense_accno_id_0 = $expense ";
161 $sql .= "order by sortkey";
162 $rs=$db->getAll($sql);
166 function getFromBG($db, $bg_id, $name) {
167 $sql = "select $name from buchungsgruppen where id='$bg_id'";
168 $rs=$db->getAll($sql);
169 return 1*$rs[0][$name];
172 function existUnit($db, $value) {
173 $sql="select name from units where name = '$value'";
174 $rs=$db->getAll($sql);
175 if (empty($rs[0]["name"]))
180 function show($things,$td=true) {
182 echo '<td>'.$things.'</td>';
187 function getStdUnit($db,$type) {
188 $sql="select * from units where type='$type' order by sortkey limit 1";
189 $rs=$db->getAll($sql);
190 return $rs[0]["name"];
193 function import_parts($db, $file, $trenner, $trennzeichen, $fields, $check, $insert, $show ,$maske) {
194 $precision=$maske["precision"];
195 $quotation=$maske["quotation"];
196 $quottype=$maske["quottype"];
197 $shop=$maske["shop"];
198 $wgtrenner=$maske["wgtrenner"];
199 $Update=($maske["update"]=="U")?true:false;
200 $UpdText=($maske["TextUpd"]=="1")?true:false;
202 $stdunitW=getStdUnit($db,"dimension");
203 $stdunitD=getStdUnit($db,"service");
204 if ($quottype=="P") $quotation=($quotation+100)/100;
206 if ($show && !$insert) show("<b>Testimport</b>",false);
207 if ($show) show("<table border='1'>\n",false);
209 /* field description */
210 $parts_fld = array_keys($fields);
213 $f=fopen($file.'.csv',"r");
216 * read first line with table descriptions
220 show("partnumber"); show("lastcost"); show("sellprice");
221 show("description");show("notes"); show("ean");
222 show("weight"); show("image"); show("partsgroup_id");
223 show("bg"); show("income_accno"); show("expense_accno");
224 show("inventory_accno"); show("microfiche");show("drawing");show("rop");
225 show("assembly");show("makemodel"); show("shop"); show("");
226 show("</tr>\n",false);
229 if ($trenner=="other") $trenner=trim($trennzeichen);
230 if (substr($trenner,0,1)=="#") if (strlen($trenner)>1) $trenner=chr(substr($trenner,1));
232 // Erst einmal die erste Zeile mit den Feldbezeichnungen einlesen.
233 $infld=fgetcsv($f,1200,$trenner);
235 foreach ($infld as $fld) {
236 $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
237 if (in_array($fld,$parts_fld)) {
245 $errors=0; /* number of errors detected */
250 while ( ($zeile=fgetcsv($f,120000,$trenner)) != FALSE) {
251 $m++; /* increase line */
254 unset($partsgroup_id);
258 unset($inventory_accno);
259 unset($income_accno);
260 unset($expense_accno);
265 /* VK-Preis bilden */
266 $sellprice = str_replace(",", ".", $zeile[$fldpos["sellprice"]]);
267 $lastcost = str_replace(",", ".", $zeile[$fldpos["lastcost"]]);
269 if ($quottype=="A") { $sellprice += $quotation; }
270 else { $sellprice = $sellprice * $quotation; }
272 if ($lastcost=="") unset($lastcost);
273 if ($sellprice=="") unset($sellprice);
275 /* Langtext zusammenbauen */
276 if ($zeile[$fldpos["notes"]]) {
278 //$notes = preg_replace('/""[^ ]/','"',$zeile[$fldpos["notes"]]);
279 $notes = addslashes($zeile[$fldpos["notes"]]);
280 if (Translate) translate($notes);
282 if ($zeile[$fldpos["notes1"]]) {
284 //$notes1 = preg_replace('/""[^ ]/','"',$zeile[$fldpos["notes1"]]);
285 $notes1 = addslashes($zeile[$fldpos["notes1"]]);
286 echo "!".$notes1."!<br>";
287 if (Translate) translate($notes1);
288 echo "!".$notes1."!<br>";
290 $notes .= "\n".$notes1;
296 /* Warengruppe bilden */
297 if ($fldpos["partsgroup"]>0 and $zeile[$fldpos["partsgroup"]]) $pgroup[]=$zeile[$fldpos["partsgroup"]];
298 if ($fldpos["partsgroup1"]>0 and $zeile[$fldpos["partsgroup1"]]) $pgroup[]=$zeile[$fldpos["partsgroup1"]];
299 if ($fldpos["partsgroup2"]>0 and $zeile[$fldpos["partsgroup2"]]) $pgroup[]=$zeile[$fldpos["partsgroup2"]];
300 if ($fldpos["partsgroup3"]>0 and $zeile[$fldpos["partsgroup3"]]) $pgroup[]=$zeile[$fldpos["partsgroup3"]];
301 if ($fldpos["partsgroup4"]>0 and $zeile[$fldpos["partsgroup4"]]) $pgroup[]=$zeile[$fldpos["partsgroup4"]];
302 if (count($pgroup)>0) {
303 $pgname = implode($wgtrenner,$pgroup);
304 if (Translate) translate($pgname);
305 $partsgroup_id = getPartsgroupId($db, $pgname, $insert);
308 /* sind Hersteller und Modelnummer hinterlegt
309 wenn ja, erfolgt er insert später */
310 if (!empty($zeile[$fldpos["makemodel"]]) and !$artikel) {
311 $mm = $zeile[$fldpos["makemodel"]];
312 if (Translate) translate($mm);
313 $hersteller=suchFirma("vendor",$mm);
314 $hersteller=$hersteller["cp_cv_id"];
315 if (!empty($zeile[$fldpos["model"]])) {
316 $mo = $zeile[$fldpos["model"]];
317 if (Translate) translate($mo);
326 /* Ware oder Dienstleistung */
327 if (($maske["ware"]=="G" and strtoupper($zeile[$fldpos["art"]])=="D") or $maske["ware"]=="D") {
329 } else if (($maske["ware"]=="G" and strtoupper($zeile[$fldpos["art"]])=="W") or $maske["ware"]=="W") {
333 /* Einheit ermitteln */
334 if ($zeile[$fldpos["unit"]]=="") {
335 //Keine Einheit mitgegeben
336 if ($maske["ware"]=="G") {
342 } else if ($maske["ware"]=="D") { $unit = $stdunitD; }
343 else { $unit = $stdunitW; };
345 if (existUnit($db,$zeile[$fldpos["unit"]])) {
346 $unit = $zeile[$fldpos["unit"]];
348 $unit = ($artikel)?$stdunitD:$stdunitW;
352 /* Buchungsgruppe ermitteln */
353 if ($maske["bugrufix"]==1) {
354 $bg = $maske["bugru"];
356 if ($zeile[$fldpos["income_accno"]]<>"" and $zeile[$fldpos["expense_accno"]]<>"") {
357 /* search for buchungsgruppe */
358 $bg = getBuchungsgruppe($db, $zeile[$fldpos["income_accno"]],$zeile[$fldpos["expense_accno"]]);
359 if ($bg == "" and $maske["bugrufix"]==2 and $maske["bugru"]<>"") {
360 $bg = $maske["bugru"];
362 } else if ($maske["bugru"]<>"" and $maske["bugrufix"]==2) {
363 $bg = $maske["bugru"];
365 /* nothing found? user must create one */
366 echo "Error in line $m: ";
367 echo "Keine Buchungsgruppe gefunden für <br>";
368 echo "Erlöse Inland: ".$zeile[$fldpos["income_accno"]]."<br>";
369 echo "Aufwand Inland: ".$zeile[$fldpos["expense_accno"]]."<br>";
370 echo "Bitte legen Sie eine an oder geben Sie eine vor.<br>";
376 /* found one, add income_accno_id etc from buchungsgr. */
377 /* XXX nur bei artikel!!! */
379 $inventory_accno = getFromBG($db, $bg, "inventory_accno_id");
381 $income_accno = getFromBG($db, $bg, "income_accno_id_0");
382 $expense_accno = getFromBG($db, $bg, "expense_accno_id_0");
385 echo "Error in line $m: ";
386 echo "Keine Buchungsgruppe angegeben/gefunden<br>";
391 $description = preg_replace('/""[^ ]/','"',$zeile[$fldpos["description"]]);
392 $description = addslashes($description);
393 if (Translate) translate($description);
395 // rop und weight müssen null oder Zahl sein
396 if ($zeile[$fldpos["rop"]]) $rop = 1 * str_replace(",", ".",$zeile[$fldpos["rop"]]);
397 if ($zeile[$fldpos["weight"]]) $weight = 1 * str_replace(",", ".", $zeile[$fldpos["weight"]]);
400 if ($zeile[$fldpos["shop"]]) {
401 $shop = (strtolower($zeile[$fldpos["shop"]]=='t'))?'t':'f';
403 $shop = $maske["shop"];
408 if (getPartsid($db,trim($zeile[$fldpos["partnumber"]]))) {
409 /* es gibt die Artikelnummer */
411 /* Updates durchführen */
413 $u += updParts($db,$insert,$show,$zeile[$fldpos["partnumber"]],$lastcost,$sellprice,
414 $description,$notes,$zeile[$fldpos["ean"]],$weight,
415 $zeile[$fldpos["image"]],$partsgroup_id, $shop);
417 $u += updPrice($db,$insert,$show,$zeile[$fldpos["partnumber"]],$lastcost,$sellprice,$shop);
424 // Neuen Artikel einfügen
426 if ($zeile[$fldpos["partnumber"]] == "") {
427 $zeile[$fldpos["partnumber"]] = newPartNumber($db,$check);
428 //Keine Artikelnummer bekommen
429 if ($zeile[$fldpos["partnumber"]] == "") {
433 $i += insertParts($db,$insert,$show,array(
434 "partnumber"=>$zeile[$fldpos["partnumber"]],
435 "description"=>$description,"notes"=>$notes,
436 "ean"=>$zeile[$fldpos["ean"]],"unit"=>$unit,
437 "weight"=>$weight,"image"=>$zeile[$fldpos["image"]],
438 "sellprice"=>$sellprice,"lastcost"=>$lastcost,
439 "partsgroup_id"=>$partsgroup_id,
440 "buchungsgruppen_id"=>$bg,"income_accno"=>$income_accno,
441 "expense_accno"=>$expense_accno,"inventory_accno"=>$inventory_accno,
442 "microfiche"=>$zeile[$fldpos["microfiche"]],"drawing"=>$zeile[$fldpos["drawing"]],
443 "rop"=>$rop,"assembly"=>$assembly,
444 "shop"=>$shop,"makemodel"=>$makemodel)
446 if ($hersteller>0 && $model) {
447 $partsid=getPartsid($db,$zeile[$fldpos["partnumber"]]);
449 getMakemodel($db,$check,$hersteller,$model,$partsid,true);
455 if ($show) show("</table>",false);
457 echo "$m Zeilen bearbeitet. Importiert: $i Update: $u (".($m-$u-$i+$errors)." : Fehler) ";