-function import_parts($db, $file, $trenner, $trennzeichen, $fields, $check, $insert, $show,$maske) {
-
- $pgshow=false;
- $note2show=false;
- $fehler=0;
- $precision=$maske["precision"];
- $quotation=$maske["quotation"];
- $quottype=$maske["quottype"];
-
- $Update=($maske["update"]=="U")?true:false;
- /* field description */
- $parts_fld = array_keys($fields);
-
- /* open csv file */
- $f=fopen("$file.csv","r");
-
- /*
- * read first line with table descriptions
- */
- show( $show, "<table border='1'><tr><td>#</td>\n");
- if ($trenner=="other") $trenner=trim($trennzeichen);
- if (substr($trenner,0,1)=="#") if (strlen($trenner)>1) $trenner=chr(substr($trenner,1));
- $infld=fgetcsv($f,1200,$trenner);
- foreach ($infld as $fld) {
- $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
- $in_fld[]=$fld;
- if (in_array(trim($fld),$parts_fld)) {
- if ($fld=="partsgroup" || $fld=="partsgroup1" ) {
- $pgshow=true;
- } else {
- show( $show, "<td>$fld</td>\n");
- }
- }
- }
- if (!in_array("unit",$infld)) {
- $stdunitW=getStdUnit($db,"dimension");
- $stdunitD=getStdUnit($db,"service");
- $unit=true;
- show( $show, "<td>unit</td>\n");
- };
- if ($pgshow) show( $show, "<td>partsgroup</td>\n");
- $posprice=0;
- $posnumber=0;
- $j=0;
- foreach ($infld as $value) {
- if ($infld[$j]=="sellprice") $posprice=$j;
- if ($infld[$j]=="partnumber") $posnumber=$j;
- $j++;
- }
- $m=0; /* line */
- $errors=0; /* number of errors detected */
- $income_accno = "";
- $expense_accno = "";
- if ($quottype=="P") $quotation=($quotation+100)/100;
- while ( ($zeile=fgetcsv($f,15000,$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);
- $rc=chkPartNumberUpd($db,$sellprice,$partnumber,!$insert);
- 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="";
- 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 ($key == "partsgroup1") {
- if ($pg_name_val<>"") {
- if ($data<>"") $pg_name_val.="!".$data;
- } else {
- $pg_name_val=$data;
- }
- $i++;
- continue;
- } else if ($key == "partsgroup") {
- /* special case partsgroup */
- $pg_name_val=$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","auto");
- $data=addslashes($data);
- } else if ($key == "notes") {
- $data=mb_convert_encoding($data,"ISO-8859-15","auto");
- $data=addslashes($data);
- } 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 */
- 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 */
- if ($pgshow) {
- if ($pg_name_val) {
- /* get ID of partsgroup or add new
- * partsgroup_id */
- $ID = getPartsgroupId($db, $pg_name_val, $insert);
- $keys.= "partsgroup_id,";
- $vals.="'".$ID."',";
- show( $show, "<td>".htmlentities($pg_name_val).":$ID</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."import)";
- $sql.=$vals.time().")";
- //show( $show, "<td> $sql </td>\n");
-
- if ($insert) {
- show( $show, "<td>");
- $db->showErr = TRUE;
- $rc=$db->query($sql);
- if (!$rc) {
- echo "Fehler";
- $fehler++;
- }
- show( $show, "</td>\n");
- }
-
- show( $show, "</tr>\n");
- }
-
- show( $show, "</table>\n");
- fclose($f);
- echo "$m Zeilen bearbeitet. ($fehler : Fehler) ";
- return $errors;
-}