- $m=0; /* line */
- $errors=0; /* number of errors detected */
- $income_accno = "";
- $expense_accno = "";
-
- /*if ($insert) {
- $sql="update parts set shop = 'n' where partnumber like '______'";
- $rc=$db->query($sql);
- //echo $sql; print_r($rc); echo "<br>";
- if (!$rc) {
- echo "Fehler: Artikel nicht aus dem Shop genommen";
- }
- };*/
- if ($quottype=="P") $quotation=($quotation+100)/100;
- while ( ($zeile=fgetcsv($f,120000,$trenner)) != FALSE) {
- $i=0; /* column */
- $m++; /* increase line */
-
- if ($Update) {
- $sellprice=$zeile[$posprice];
- $partnumber=$zeile[$posnumber];
- $sellprice = str_replace(",", ".", $sellprice);
- if ($quotation<>0) {
- if ($quottype=="A") { $sellprice += $quotation; }
- else { $sellprice = $sellprice * $quotation; }
- };
- if ($precision>=0) $sellprice = round($sellprice,$precision);
- if ($UpdText) {
- $description=$zeile[$posdescript];
- $note=$zeile[$posnotes];
- $note1=$zeile[$posnotes1];
- $note=mb_convert_encoding($note,"ISO-8859-15","ISO-8859-15");
- $note=preg_replace('/""[^ ]/','"',$note);
- $note=" \n".addslashes($note);
- $note1=mb_convert_encoding($note1,"ISO-8859-15","ISO-8859-15");
- $note1=preg_replace('/""[^ ]/','"',$note1);
- $note.=" \n".addslashes($note1);
- $rc=chkPartNumberUpd($db,$sellprice,$partnumber,$description,$note,$check);
- } else {
- $rc=chkPartNumberUpd($db,$sellprice,$partnumber,false,false,$check);
- }
- if ($rc==-1) {
- show($show,"<tr><td>Update </td><td>$partnumber:$sellprice</td></tr>\n");
- continue;
- } else if ($rc==-99) {
- show($show,"<tr><td>Fehler Zeile $m</td></tr>\n");
- continue;
- } else {
- $zeile[$posnumber]=$rc;
- }
- };
-
- $sql="insert into $file ";
- $keys="(";
- $vals=" values (";
- show( $show, "<tr><td>$m</td>\n");
-
- /* for each column */
- $dienstleistung=false;
- $artikel=-1;
- $partNr=false;
- $pg_name_val=array();
- $note_val="";
- $model="";
- $hersteller="";
- foreach($zeile as $data) {
- /* check if column will be imported */
- if (!in_array(trim($in_fld[$i]),$parts_fld)) {
- $i++;
- continue;
- };
- $data=trim($data);
- $key=$in_fld[$i];
- /* add key and data */
-
- /* special case partsgroup1 */
- if (substr($key,0,10) == "partsgroup") {
- if (strlen($key)==10) { $pgnr=0; }
- else { $pgnr=substr($key,-1); }
- $pg_name_val[$pgnr]=$data;
- $i++;
- continue;
- } else if ($key == "lastcost" ||
- $key == "sellprice") {
-
- /* convert 0,0 numeric into 0.0 */
- $data = str_replace(",", ".", $data);
- if ($key == "sellprice") {
- if ($quotation<>0) {
- if ($quottype=="A") { $data += $quotation; }
- else { $data = $data * $quotation; }
- };
- if ($precision>=0) $data = round($data,$precision);
- }
- } else if ($key == "partnumber") {
- $partNr=true;
- $partnumber=chkPartNumber($db,$data,$check);
- if ($partnumber=="") {
- show( $show, "<td>NULL</td>\n");
- $i++;
- continue;
- } else {
- //$keys.="partnumber, ";
- $data=$partnumber;
- //show( $show, "<td>$partnumber</td>\n");
- }
- } else if ($key == "description") {
- $data=mb_convert_encoding($data,"ISO-8859-15","ISO-8859-15");
- $data=preg_replace('/""[^ ]/','"',$data);
- $data=addslashes($data);
- } else if ($key == "notes") {
- $data=mb_convert_encoding($data,"ISO-8859-15","ISO-8859-15");
- $data=preg_replace('/""[^ ]/','"',$data);
- $notesval=addslashes($data);
- $i++;
- continue;
- } else if ($key == "notes1") {
- $data=mb_convert_encoding($data,"ISO-8859-15","ISO-8859-15");
- $data=preg_replace('/""[^ ]/','"',$data);
- $notesval.=" \n".addslashes($data);
- $i++;
- continue;
- //$key="notes";
- /*} else if ($key == "makemodel") {
- $data=mb_convert_encoding($data,"ISO-8859-15","ISO-8859-15");
- $hersteller=addslashes($data);
- $i++;
- continue;
- } else if ($key == "model") {
- $data=mb_convert_encoding($data,"ISO-8859-15","ISO-8859-15");
- $model=addslashes($data);
- $i++;
- continue;*/
- } else if ($key == "unit") {
- if ($data=="") {
- if ($maske["ware"]=="W") { $data=$stdunitW; }
- else if ($maske["ware"]=="D") { $data=$stdunitD; }
- //else if ($maske["ware"]=="G") { $data=$stdunitD; //Noch machen!}
- else { $data=$stdunitW; };
- }
- /* convert stück and Stunde */
- if (preg_match("/^st..?ck$/i", $data))
- $data = "Stck";
- else if ($data == "Stunde")
- $data = "Std";
- /* check if unit exists */
- //echo "!$data!";
- if (!existUnit($db, $data)) {
- echo "Error in line $m: ";
- echo "Einheit <b>$data</b> existiert nicht ";
- echo "Bitte legen Sie diese Einheit an<br>";
- $errors++;
- }
- } else if ($key == "art") {
- if ($maske["ware"]=="G" and strtoupper($data)=="D") { $artikel=false; }
- else if ($maske["ware"]=="G") { $artikel=true; };
- $i++;
- continue;
- } else if ($key == "income_accno") {
- $income_accno = $data;
- $i++;
- show( $show, "<td>$data</td>\n");
- continue;
- } else if ($key == "expense_accno") {
- $expense_accno = $data;
- $i++;
- show( $show, "<td>$data</td>\n");
- continue;
- }
- if ($data==false or empty($data) or !$data) {
- show( $show, "<td>NULL</td>\n");
- $i++;
- continue;
- }
- /* convert JA to Yes */
- if ($data === "J" || $data === "j") $data = "Y";
- $vals.="'".$data."',";
- show( $show, "<td>".htmlentities($data)."</td>\n");
- $keys.=$key.",";
- $i++;
- }
- if ($unit) {
- if ($maske["ware"]=="D") { $einh=$stdunitD; }
- else { $einh=$stdunitW; }
- $keys.="unit,";
- $vals.="'$einh',";
- show( $show,"<td>$einh</td>\n");
- }
- /* special case partsgroup */
- //Mehrere Warengruppennamen werden mit einem Trennzeichen zu einen Namen zusammengefürt.
- if ($pgshow) {
- foreach($pg_name_val as $pg) {
- if ($pg>"") $pgname.=$pg.$wgtrenner;
- };
- unset($pg_name_val);
- if ($pgname and $pgname <> "!") {
- $pgname=substr($pgname,0,-1);
- /* get ID of partsgroup or add new
- * partsgroup_id */
- $ID = getPartsgroupId($db, $pgname, $insert);
- $keys.= "partsgroup_id,";
- $vals.="'".$ID."',";
- show( $show, "<td>".htmlentities($pgname).":$ID</td>\n");
- } else {
- show( $show,"<td>NULL</td>\n");
- }
- }
- if ($note2show) {
- if ($notesval) {
- $keys.="notes,";
- $vals.="'$notesval',";
- if (strlen($notesval)>255) {
- show( $show, "<td>".substr($notesval,0,25)." . . . ".htmlentities(substr($notesval,-25))."</td>\n");
- } else {
- show( $show, "<td>$notesval</td>\n");
- }
- } else {
- show( $show,"<td>NULL</td>\n");
- }
- }
- if ($artikel==-1) {
- if ($maske["ware"]=="D") { $artikel=false; }
- else { $artikel=true; };
- }
- if ($maske["bugrufix"]==1) {
- $bg = $maske["bugru"];
- } else {
- if ($income_accno<>"" and $expense_accno<>"") {
- /* search for buchungsgruppe */
- $bg = getBuchungsgruppe($db, $income_accno, $expense_accno);
- if ($bg == "" and $maske["bugrufix"]==2 and $maske["bugru"]<>"") {
- $bg = $maske["bugru"];
- }
- } else if ($maske["bugru"]<>"" and $maske["bugrufix"]==2) {
- $bg = $maske["bugru"];
- } else {
- /* nothing found? user must create one */
- echo "Error in line $m: ";
- echo "Keine Buchungsgruppe gefunden für <br>";
- echo "Erlöse Inland: $income_accno<br>";
- echo "Bitte legen Sie eine an oder geben Sie eine vor.<br>";
- echo "<br>";
- $errors++;
- }
- }
- if ($bg > 0) {
- /* found one, add income_accno_id etc from buchungsgr.
- */
- $keys.="buchungsgruppen_id, ";
- $vals.="'$bg', ";
- /* XXX nur bei artikel!!! */
- if ($artikel) {
- $keys.="inventory_accno_id, ";
- $vals.=getFromBG($db, $bg, "inventory_accno_id")." ,";
- };
- $keys.="income_accno_id, ";
- $vals.=getFromBG($db, $bg, "income_accno_id_0")." ,";
- $keys.="expense_accno_id,";
- $vals.=getFromBG($db, $bg, "expense_accno_id_0")." ,";
- }
- if ($partNr==false) {
- $partnumber=chkPartNumber($db,"",$check);
- if ($partnumber=="") {
- show( $show, "<td>NULL</td>\n");
- $errors++;
- } else {
- $keys.="partnumber, ";
- $vals.="'$partnumber',";
- show( $show, "<td>$partnumber</td>\n");
- }
- }
-
- $sql.=$keys."shop,import)";
- $sql.="$vals'$shop',".time().")";
-
- if ($insert) {
- show( $show, "<td>");
- $db->showErr = TRUE;
- $rc=$db->query($sql);
- if (!$rc) {
- echo "Fehler";
- $fehler++;
- }
- show( $show, "</td>\n");
- }
- $pgname="";
- show( $show, "</tr>\n");
+ }
+ $db->query("COMMIT");
+ return true;
+}
+
+/**
+ * TODO: short description.
+ *
+ * @param double $db
+ * @param mixed $file
+ * @param mixed $fields
+ * @param mixed $check
+ * @param int $insert
+ * @param string $show
+ * @param mixed $maske
+ *
+ * @return TODO
+ */
+function import_parts($db, $file, $fields, $check, $maske) {
+ $insert = !$maske["test"];
+ $show = $maske["show"];
+ $trennzeichen = ($maske["trennzeichen"])?$maske["trennzeichen"]:"";
+ $trenner = ($maske["trenner"])?$maske["trenner"]:",";
+ $precision=$maske["precision"];
+ $quotation=$maske["quotation"];
+ $quottype=$maske["quottype"];
+ $shop=$maske["shop"];
+ $wgtrenner=$maske["wgtrenner"];
+ $Update=($maske["update"]=="U")?true:false;
+ $UpdText=($maske["TextUpd"]=="1")?true:false;
+
+ //$stdunitW=getStdUnit($db,"dimension");
+ //$stdunitD=getStdUnit($db,"service");
+ $stdunitW=$maske["dimensionunit"];
+ $stdunitD=$maske["serviceunit"];
+ if ($quottype=="P") $quotation=($quotation+100)/100;
+
+ if ($show && !$insert) show("<b>Testimport</b>",false);
+ if ($show) show("<table border='1'>\n",false);
+
+ /* field description */
+ $prices = getPricegroup($db);
+ if ($prices) {
+ $priceskey = array_keys($prices);
+ $parts_fld = array_merge(array_keys($fields),$priceskey);
+ } else {
+ $parts_fld = array_keys($fields);
+ }
+
+ if ($trenner=="other") $trenner=trim($trennzeichen);
+ if (substr($trenner,0,1)=="#") if (strlen($trenner)>1) $trenner=chr(substr($trenner,1));
+
+ /* open csv file */
+ if (file_exists($file."head.csv")) {
+ $fh=fopen($file.'head.csv',"r");
+ // Erst einmal die erste Zeile mit den richtigen Feldbezeichnungen einlesen.
+ $infld=fgetcsv($fh,1200,$trenner);
+ fclose($fh);
+ $f=fopen($file.'.csv',"r");
+ // Erst einmal die erste Zeile mit den falschen Feldbezeichnungen einlesen.
+ $tmp=fgetcsv($f,1200,$trenner);
+ } else {
+ $f=fopen($file.'.csv',"r");
+ // Erst einmal die erste Zeile mit den Feldbezeichnungen einlesen.
+ $infld=fgetcsv($f,1200,$trenner);
+ }
+
+ /*
+ * read first line with table descriptions
+ */
+ if ($show) {
+ show('<tr>',false);
+ show("partnumber"); show("lastcost"); show("sellprice"); show("listprice");
+ show("description");show("notes"); show("ean");
+ show("weight"); show("image"); show("partsgroup_id");
+ show("bg"); show("income_accno"); show("expense_accno");
+ show("inventory_accno"); show("microfiche");show("drawing");show("rop");
+ show("assembly");show("makemodel"); show("shop"); show("");
+ show("</tr>\n",false);
+ }
+
+
+ $p=0;
+ foreach ($infld as $fld) {
+ $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
+ if (in_array($fld,$parts_fld)) {
+ $fldpos[$fld]=$p;
+ }
+ $p++;
+ }
+ $i=0;
+ $u=0;
+ $m=0; /* line */
+ $errors=0; /* number of errors detected */
+ $income_accno = "";
+ $expense_accno = "";
+ $assembly = 'f';
+
+ while ( ($zeile=fgetcsv($f,120000,$trenner)) != FALSE) {
+ $m++; /* increase line */
+ $unit=false;
+ unset($pgroup);
+ unset($partsgroup_id);
+ unset($notes);
+ unset($rop);
+ unset($weight);
+ unset($inventory_accno);
+ unset($income_accno);
+ unset($expense_accno);
+ unset($model);
+ unset($makemodel);
+ unset($hersteller);
+
+ /* VK-Preis bilden */
+ $sellprice = str_replace(",", ".", $zeile[$fldpos["sellprice"]]);
+ $listprice = str_replace(",", ".", $zeile[$fldpos["listprice"]]);
+ $lastcost = str_replace(",", ".", $zeile[$fldpos["lastcost"]]);
+ if ($prices) {
+ foreach ($prices as $pkey=>$val) {
+ if (array_key_exists($pkey,$fldpos))
+ $pricegroup[$val] = str_replace(",", ".", $zeile[$fldpos[$pkey]]);
+ }