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"]);
34 /*foreach ($data as $key=>$val) {
35 echo $key.":".gettype($val).":".gettype($data[$key]).":".$val."<br>";
38 $sqlIa = 'INSERT INTO parts (';
39 $sqlIa .= 'partnumber,description,notes,ean,unit,';
40 $sqlIa .= 'weight,image,sellprice,lastcost,partsgroup_id,';
41 $sqlIa .= 'buchungsgruppen_id,income_accno_id,expense_accno_id,inventory_accno_id,';
42 $sqlIa .= 'microfiche,drawing,rop,assembly,shop,makemodel,import) ';
43 $sqlIa .= 'VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
44 $data["import"]=time();
45 $rc=$db->execute($sqlIa,$data);
53 show('<font color="red">error</font>');
54 show('</tr>'."\n",false);
58 function updPrice($db,$insert,$show,$partnumber,$lastcost,$sellprice,$shop) {
61 show($partnumber); show($lastcost); show($sellprice);
64 $sqlPr = 'UPDATE PARTS SET ';
65 $sqlPr .= 'sellprice = ?, lastcost = ?, shop = ? ';
66 $sqlPr .= 'WHERE partnumber = ?';
67 $rc=$db->execute($sqlPr,array("sellprice"=>$sellprice,"lastcost"=>$lastcost,"shop"=>$shop,"partnumber"=>$partnumber));
75 show('<font color="red">error</font>');
76 show('</tr>'."\n",false);
80 function updParts($db,$insert,$show,$partnumber,$lastcost,$sellprice,
81 $description,$notes,$ean,$weight,$image,
82 $partsgroup_id, $shop) {
85 show($partnumber); show($lastcost); show($sellprice);
86 show($description); show(substr($notes,0,25));show($ean);
87 show($weight); show($image); show($partsgroup_id);
90 $sqlUa = 'UPDATE PARTS SET ';
91 $sqlUa .= 'description = ?, notes = ?, ean = ?, weight = ?, image = ?, ';
92 $sqlUa .= 'sellprice = ?, lastcost = ?, partsgroup_id = ?, shop = ? ';
93 $sqlUa .= 'WHERE partnumber = ?';
94 $rc=$db->execute($sqlUa,array($description,$notes,$ean,$weight,$image,
95 $sellprice,$lastcost,$partsgroup_id,$shop,$partnumber));
103 show('<font color="red">error</font>');
104 show('</tr>'."\n",false);
109 function getMakemodel($db,$check,$hersteller,$model,$partsid,$add=true) {
110 $sql="select * from makemodel where make = $hersteller and model = '$model' and parts_id = $partsid";
111 $rs=$db->getAll($sql);
112 if (empty($rs[0]["id"]) && $add) {
113 $sql="insert into makemodel (parts_id,make,model) values ($partsid,'$hersteller','$model')";
114 $rc=$db->query($sql);
118 function getAccnoId($db, $accno) {
119 $sql = "select id from chart where accno='$accno'";
120 $rs=$db->getAll($sql);
124 function getPartsid($db,$number) {
125 $sql = "select id from parts where partnumber = '$number'";
126 $rs=$db->getAll($sql);
127 if ($rs[0]["id"]>0) {
134 function newPartNumber($db,$check) {
135 if ($check) return "check";
136 $rc=$db->query("BEGIN");
137 $sql = "select articlenumber from defaults";
138 $rs=$db->getAll($sql);
139 if ($rs[0]["articlenumber"]) {
140 preg_match("/([^0-9]+)?([0-9]+)([^0-9]+)?/", $rs[0]["articlenumber"] , $regs);
142 $number=$regs[1].($regs[2]+1).$regs[3];
144 $sql = "update defaults set articlenumber = '$number'";
145 $rc=$db->query($sql);
146 $rc=$db->query("COMMIT");
147 //Prüfen ob die Nummer nicht doch schon vergeben ist.
148 $sql = "select * from parts where partnumber = '$number'";
149 $rs=$db->getAll($sql);
150 if ($rs[0]["id"]>0) return "";
154 function getBuchungsgruppe($db, $income, $expense) {
155 $income_id = getAccnoId($db, $income);
156 $expense_id = getAccnoId($db, $expense);
157 $sql = "select id from buchungsgruppen where ";
158 $sql .= "income_accno_id_0 = $income and ";
159 $sql .= "expense_accno_id_0 = $expense ";
160 $sql .= "order by sortkey";
161 $rs=$db->getAll($sql);
165 function getFromBG($db, $bg_id, $name) {
166 $sql = "select $name from buchungsgruppen where id='$bg_id'";
167 $rs=$db->getAll($sql);
168 return 1*$rs[0][$name];
171 function existUnit($db, $value) {
172 $sql="select name from units where name = '$value'";
173 $rs=$db->getAll($sql);
174 if (empty($rs[0]["name"]))
179 function show($things,$td=true) {
181 echo '<td>'.$things.'</td>';
186 function getStdUnit($db,$type) {
187 $sql="select * from units where type='$type' order by sortkey limit 1";
188 $rs=$db->getAll($sql);
189 return $rs[0]["name"];
192 function import_parts($db, $file, $trenner, $trennzeichen, $fields, $check, $insert, $show ,$maske) {
193 $precision=$maske["precision"];
194 $quotation=$maske["quotation"];
195 $quottype=$maske["quottype"];
196 $shop=$maske["shop"];
197 $wgtrenner=$maske["wgtrenner"];
198 $Update=($maske["update"]=="U")?true:false;
199 $UpdText=($maske["TextUpd"]=="1")?true:false;
201 $stdunitW=getStdUnit($db,"dimension");
202 $stdunitD=getStdUnit($db,"service");
203 if ($quottype=="P") $quotation=($quotation+100)/100;
205 if ($show && !$insert) show("<b>Testimport</b>",false);
206 if ($show) show("<table border='1'>\n",false);
208 /* field description */
209 $parts_fld = array_keys($fields);
212 $f=fopen("$file.csv","r");
215 * read first line with table descriptions
219 show("partnumber"); show("lastcost"); show("sellprice");
220 show("description");show("notes"); show("ean");
221 show("weight"); show("image"); show("partsgroup_id");
222 show("bg"); show("income_accno"); show("expense_accno");
223 show("inventory_accno"); show("microfiche");show("drawing");show("rop");
224 show("assembly");show("makemodel"); show("");
225 show("</tr>\n",false);
228 if ($trenner=="other") $trenner=trim($trennzeichen);
229 if (substr($trenner,0,1)=="#") if (strlen($trenner)>1) $trenner=chr(substr($trenner,1));
231 // Erst einmal die erste Zeile mit den Feldbezeichnungen einlesen.
232 $infld=fgetcsv($f,1200,$trenner);
234 foreach ($infld as $fld) {
235 $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
236 if (in_array($fld,$parts_fld)) {
244 $errors=0; /* number of errors detected */
249 while ( ($zeile=fgetcsv($f,120000,$trenner)) != FALSE) {
250 $m++; /* increase line */
257 unset($inventory_accno);
258 unset($income_accno);
259 unset($expense_accno);
264 /* VK-Preis bilden */
265 $sellprice = str_replace(",", ".", $zeile[$fldpos["sellprice"]]);
266 $lastcost = str_replace(",", ".", $zeile[$fldpos["lastcost"]]);
268 if ($quottype=="A") { $sellprice += $quotation; }
269 else { $sellprice = $sellprice * $quotation; }
271 if ($lastcost=="") unset($lastcost);
272 if ($sellprice=="") unset($sellprice);
274 /* Langtext zusammenbauen */
275 if ($zeile[$fldpos["notes"]]) {
276 $notes = preg_replace('/""[^ ]/','"',$zeile[$fldpos["notes"]]);
277 $notes = addslashes($notes);
279 if ($zeile[$fldpos["notes1"]]) {
280 $notes1 = preg_replace('/""[^ ]/','"',$zeile[$fldpos["notes1"]]);
282 $notes = "\n".addslashes($notes1);
284 $notes = addslashes($notes1);
288 /* Warengruppe bilden */
289 if ($fldpos["partsgroup"]>0 and $zeile[$fldpos["partsgroup"]]) $pgroup[]=$zeile[$fldpos["partsgroup"]];
290 if ($fldpos["partsgroup1"]>0 and $zeile[$fldpos["partsgroup1"]]) $pgroup[]=$zeile[$fldpos["partsgroup1"]];
291 if ($fldpos["partsgroup2"]>0 and $zeile[$fldpos["partsgroup2"]]) $pgroup[]=$zeile[$fldpos["partsgroup2"]];
292 if ($fldpos["partsgroup3"]>0 and $zeile[$fldpos["partsgroup3"]]) $pgroup[]=$zeile[$fldpos["partsgroup3"]];
293 if ($fldpos["partsgroup4"]>0 and $zeile[$fldpos["partsgroup4"]]) $pgroup[]=$zeile[$fldpos["partsgroup4"]];
294 if (count($pgroup)>0) {
295 $pgname = implode($wgtrenner,$pgroup);
296 $partsgroup_id = getPartsgroupId($db, $pgname, $insert);
299 /* sind Hersteller und Modelnummer hinterlegt
300 wenn ja, erfolgt er insert später */
301 if (!empty($zeile[$fldpos["makemodel"]]) and !$artikel) {
302 $hersteller=suchFirma("vendor",$zeile[$fldpos["makemodel"]]);
303 $hersteller=$hersteller["cp_cv_id"];
304 if (!empty($zeile[$fldpos["model"]])) {
305 $model = $zeile[$fldpos["model"]];
313 /* Ware oder Dienstleistung */
314 if (($maske["ware"]=="G" and strtoupper($zeile[$fldpos["art"]])=="D") or $maske["ware"]=="D") {
316 } else if (($maske["ware"]=="G" and strtoupper($zeile[$fldpos["art"]])=="W") or $maske["ware"]=="W") {
320 /* Einheit ermitteln */
321 if ($zeile[$fldpos["unit"]]=="") {
322 //Keine Einheit mitgegeben
323 if ($maske["ware"]=="G") {
329 } else if ($maske["ware"]=="D") { $unit = $stdunitD; }
330 else { $unit = $stdunitW; };
332 if (existUnit($db,$zeile[$fldpos["unit"]])) {
333 $unit = $zeile[$fldpos["unit"]];
335 $unit = ($artikel)?$stdunitD:$stdunitW;
339 /* Buchungsgruppe ermitteln */
340 if ($maske["bugrufix"]==1) {
341 $bg = $maske["bugru"];
343 if ($zeile[$fldpos["income_accno"]]<>"" and $zeile[$fldpos["expense_accno"]]<>"") {
344 /* search for buchungsgruppe */
345 $bg = getBuchungsgruppe($db, $zeile[$fldpos["income_accno"]],$zeile[$fldpos["expense_accno"]]);
346 if ($bg == "" and $maske["bugrufix"]==2 and $maske["bugru"]<>"") {
347 $bg = $maske["bugru"];
349 } else if ($maske["bugru"]<>"" and $maske["bugrufix"]==2) {
350 $bg = $maske["bugru"];
352 /* nothing found? user must create one */
353 echo "Error in line $m: ";
354 echo "Keine Buchungsgruppe gefunden für <br>";
355 echo "Erlöse Inland: ".$zeile[$fldpos["income_accno"]]."<br>";
356 echo "Aufwand Inland: ".$zeile[$fldpos["expense_accno"]]."<br>";
357 echo "Bitte legen Sie eine an oder geben Sie eine vor.<br>";
363 /* found one, add income_accno_id etc from buchungsgr. */
364 /* XXX nur bei artikel!!! */
366 $inventory_accno = getFromBG($db, $bg, "inventory_accno_id");
368 $income_accno = getFromBG($db, $bg, "income_accno_id_0");
369 $expense_accno = getFromBG($db, $bg, "expense_accno_id_0");
372 echo "Error in line $m: ";
373 echo "Keine Buchungsgruppe angegeben/gefunden<br>";
378 $description = preg_replace('/""[^ ]/','"',$zeile[$fldpos["description"]]);
379 $description = addslashes($description);
381 // rop und weight müssen null oder Zahl sein
382 if ($zeile[$fldpos["rop"]]) $rop = 1 * $zeile[$fldpos["rop"]];
383 if ($zeile[$fldpos["weight"]]) $weight = 1 * $zeile[$fldpos["weight"]];
385 if (getPartsid($db,trim($zeile[$fldpos["partnumber"]]))) {
386 /* es gibt die Artikelnummer */
388 /* Updates durchführen */
390 $u += updParts($db,$insert,$show,$zeile[$fldpos["partnumber"]],$lastcost,$sellprice,
391 $description,$notes,$zeile[$fldpos["ean"]],$weight,
392 $zeile[$fldpos["image"]],$partsgroup_id, $shop);
394 $u += updPrice($db,$insert,$show,$zeile[$fldpos["partnumber"]],$lastcost,$sellprice,$shop);
401 // Neuen Artikel einfügen
403 if ($zeile[$fldpos["partnumber"]] == "") {
404 $zeile[$fldpos["partnumber"]] = newPartNumber($db,$check);
405 //Keine Artikelnummer bekommen
406 if ($zeile[$fldpos["partnumber"]] == "") {
410 $i += insertParts($db,$insert,$show,array(
411 "partnumber"=>$zeile[$fldpos["partnumber"]],
412 "description"=>$description,"notes"=>$notes,
413 "ean"=>$zeile[$fldpos["ean"]],"unit"=>$unit,
414 "weight"=>$weight,"image"=>$zeile[$fldpos["image"]],
415 "sellprice"=>$sellprice,"lastcost"=>$lastcost,
416 "partsgroup_id"=>$partsgroup_id,
417 "buchungsgruppen_id"=>$bg,"income_accno"=>$income_accno,
418 "expense_accno"=>$expense_accno,"inventory_accno"=>$inventory_accno,
419 "microfiche"=>$zeile[$fldpos["microfiche"]],"drawing"=>$zeile[$fldpos["drawing"]],
420 "rop"=>$rop,"assembly"=>$assembly,
421 "shop"=>$shop,"makemodel"=>$makemodel)
423 if ($hersteller>0 && $model) {
424 $partsid=getPartsid($db,$zeile[$fldpos["partnumber"]]);
426 getMakemodel($db,$check,$hersteller,$model,$partsid,true);
432 if ($show) show("</table>",false);
434 echo "$m Zeilen bearbeitet. Importiert: $i Update: $u (".($m-$u-$i+$errors)." : Fehler) ";