Beispiel #1
0
function import_parts($db, $file, $trenner, $trennzeichen, $fields, $check, $insert, $show, $maske)
{
    $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");
    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 */
    $parts_fld = array_keys($fields);
    /* open csv file */
    $f = fopen("{$file}.csv", "r");
    /*
     * read first line with table descriptions
     */
    if ($show) {
        show('<tr>', false);
        show("partnumber");
        show("lastcost");
        show("sellprice");
        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("");
        show("</tr>\n", false);
    }
    if ($trenner == "other") {
        $trenner = trim($trennzeichen);
    }
    if (substr($trenner, 0, 1) == "#") {
        if (strlen($trenner) > 1) {
            $trenner = chr(substr($trenner, 1));
        }
    }
    // Erst einmal die erste Zeile mit den Feldbezeichnungen einlesen.
    $infld = fgetcsv($f, 1200, $trenner);
    $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($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"]]);
        $lastcost = str_replace(",", ".", $zeile[$fldpos["lastcost"]]);
        if ($quotation != 0) {
            if ($quottype == "A") {
                $sellprice += $quotation;
            } else {
                $sellprice = $sellprice * $quotation;
            }
        }
        if ($lastcost == "") {
            unset($lastcost);
        }
        if ($sellprice == "") {
            unset($sellprice);
        }
        /* Langtext zusammenbauen */
        if ($zeile[$fldpos["notes"]]) {
            $notes = preg_replace('/""[^ ]/', '"', $zeile[$fldpos["notes"]]);
            $notes = addslashes($notes);
        }
        if ($zeile[$fldpos["notes1"]]) {
            $notes1 = preg_replace('/""[^ ]/', '"', $zeile[$fldpos["notes1"]]);
            if ($notes) {
                $notes = "\n" . addslashes($notes1);
            } else {
                $notes = addslashes($notes1);
            }
        }
        /* Warengruppe bilden */
        if ($fldpos["partsgroup"] > 0 and $zeile[$fldpos["partsgroup"]]) {
            $pgroup[] = $zeile[$fldpos["partsgroup"]];
        }
        if ($fldpos["partsgroup1"] > 0 and $zeile[$fldpos["partsgroup1"]]) {
            $pgroup[] = $zeile[$fldpos["partsgroup1"]];
        }
        if ($fldpos["partsgroup2"] > 0 and $zeile[$fldpos["partsgroup2"]]) {
            $pgroup[] = $zeile[$fldpos["partsgroup2"]];
        }
        if ($fldpos["partsgroup3"] > 0 and $zeile[$fldpos["partsgroup3"]]) {
            $pgroup[] = $zeile[$fldpos["partsgroup3"]];
        }
        if ($fldpos["partsgroup4"] > 0 and $zeile[$fldpos["partsgroup4"]]) {
            $pgroup[] = $zeile[$fldpos["partsgroup4"]];
        }
        if (count($pgroup) > 0) {
            $pgname = implode($wgtrenner, $pgroup);
            $partsgroup_id = getPartsgroupId($db, $pgname, $insert);
        }
        /* sind Hersteller und Modelnummer hinterlegt 
           wenn ja, erfolgt er insert später */
        if (!empty($zeile[$fldpos["makemodel"]]) and !$artikel) {
            $hersteller = suchFirma("vendor", $zeile[$fldpos["makemodel"]]);
            $hersteller = $hersteller["cp_cv_id"];
            if (!empty($zeile[$fldpos["model"]])) {
                $model = $zeile[$fldpos["model"]];
                $makemodel = 't';
            } else {
                unset($hersteller);
                $makemodel = 'f';
            }
        }
        /* Ware oder Dienstleistung */
        if ($maske["ware"] == "G" and strtoupper($zeile[$fldpos["art"]]) == "D" or $maske["ware"] == "D") {
            $artikel = false;
        } else {
            if ($maske["ware"] == "G" and strtoupper($zeile[$fldpos["art"]]) == "W" or $maske["ware"] == "W") {
                $artikel = true;
            }
        }
        /* Einheit ermitteln */
        if ($zeile[$fldpos["unit"]] == "") {
            //Keine Einheit mitgegeben
            if ($maske["ware"] == "G") {
                if ($artikel) {
                    $unit = $stdunitD;
                } else {
                    $unit = $stdunitW;
                }
            } else {
                if ($maske["ware"] == "D") {
                    $unit = $stdunitD;
                } else {
                    $unit = $stdunitW;
                }
            }
        } else {
            if (existUnit($db, $zeile[$fldpos["unit"]])) {
                $unit = $zeile[$fldpos["unit"]];
            } else {
                $unit = $artikel ? $stdunitD : $stdunitW;
            }
        }
        /* Buchungsgruppe ermitteln */
        if ($maske["bugrufix"] == 1) {
            $bg = $maske["bugru"];
        } else {
            if ($zeile[$fldpos["income_accno"]] != "" and $zeile[$fldpos["expense_accno"]] != "") {
                /* search for buchungsgruppe */
                $bg = getBuchungsgruppe($db, $zeile[$fldpos["income_accno"]], $zeile[$fldpos["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&uuml;r <br>";
                    echo "Erl&ouml;se Inland: " . $zeile[$fldpos["income_accno"]] . "<br>";
                    echo "Aufwand Inland: " . $zeile[$fldpos["expense_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. */
            /* XXX nur bei artikel!!! */
            if ($artikel) {
                $inventory_accno = getFromBG($db, $bg, "inventory_accno_id");
            }
            $income_accno = getFromBG($db, $bg, "income_accno_id_0");
            $expense_accno = getFromBG($db, $bg, "expense_accno_id_0");
            $bg = $bg * 1;
        } else {
            echo "Error in line {$m}: ";
            echo "Keine Buchungsgruppe angegeben/gefunden<br>";
            $errors++;
            continue;
        }
        $description = preg_replace('/""[^ ]/', '"', $zeile[$fldpos["description"]]);
        $description = addslashes($description);
        // rop und weight müssen null oder Zahl sein
        if ($zeile[$fldpos["rop"]]) {
            $rop = 1 * $zeile[$fldpos["rop"]];
        }
        if ($zeile[$fldpos["weight"]]) {
            $weight = 1 * $zeile[$fldpos["weight"]];
        }
        if (getPartsid($db, trim($zeile[$fldpos["partnumber"]]))) {
            /* es gibt die Artikelnummer */
            if ($Update) {
                /* Updates durchführen */
                if ($UpdText == '1') {
                    $u += updParts($db, $insert, $show, $zeile[$fldpos["partnumber"]], $lastcost, $sellprice, $description, $notes, $zeile[$fldpos["ean"]], $weight, $zeile[$fldpos["image"]], $partsgroup_id, $shop);
                } else {
                    $u += updPrice($db, $insert, $show, $zeile[$fldpos["partnumber"]], $lastcost, $sellprice, $shop);
                }
                continue;
                // nächste Zeile
            }
        }
        // Neuen Artikel einfügen
        if ($zeile[$fldpos["partnumber"]] == "") {
            $zeile[$fldpos["partnumber"]] = newPartNumber($db, $check);
            //Keine Artikelnummer bekommen
            if ($zeile[$fldpos["partnumber"]] == "") {
                continue;
            }
        }
        $i += insertParts($db, $insert, $show, array("partnumber" => $zeile[$fldpos["partnumber"]], "description" => $description, "notes" => $notes, "ean" => $zeile[$fldpos["ean"]], "unit" => $unit, "weight" => $weight, "image" => $zeile[$fldpos["image"]], "sellprice" => $sellprice, "lastcost" => $lastcost, "partsgroup_id" => $partsgroup_id, "buchungsgruppen_id" => $bg, "income_accno" => $income_accno, "expense_accno" => $expense_accno, "inventory_accno" => $inventory_accno, "microfiche" => $zeile[$fldpos["microfiche"]], "drawing" => $zeile[$fldpos["drawing"]], "rop" => $rop, "assembly" => $assembly, "shop" => $shop, "makemodel" => $makemodel));
        if ($hersteller > 0 && $model) {
            $partsid = getPartsid($db, $zeile[$fldpos["partnumber"]]);
            if ($partsid) {
                getMakemodel($db, $check, $hersteller, $model, $partsid, true);
            }
        }
        unset($zeile);
    }
    if ($show) {
        show("</table>", false);
    }
    fclose($f);
    echo "{$m} Zeilen bearbeitet. Importiert: {$i} Update: {$u} (" . ($m - $u - $i + $errors) . " : Fehler) ";
}
if (isset($_POST["felder"]) && $_POST["felder"] != '') {
    $rc = doReport($_POST, $Q);
    $t->set_file(array("fa1" => "companies1.tpl"));
    if ($rc) {
        $tmp = "<div style='width:300px'>[<a href='tmp/report_" . $_SESSION["login"] . ".csv'>download Report</a>]</div>";
    } else {
        $tmp = "Sorry, not found";
    }
    $t->set_var(array('report' => $tmp));
    leertpl($t, 1, $Q_Array, "", true, true);
} else {
    if ((isset($_POST["suche"]) and $_POST["suche"] != "") || isset($_POST["first"])) {
        if (isset($_POST["first"])) {
            $daten = getAllFirmen(array(1, $_POST["first"]), false, $Q);
        } else {
            $daten = suchFirma($_POST, $Q);
        }
        if (count($daten) == 1 && $daten != false) {
            echo '<script> showD("' . $Q . '","' . $daten[0]["id"] . '");</script>';
        } else {
            if (count($daten) > 1) {
                $t->set_file(array("fa1" => "companies1Result.tpl"));
                $t->set_block("fa1", "Liste", "Block");
                $t->set_var(array('FAART' => $Q == "C" ? "Customer" : "Vendor"));
                $i = 0;
                $rc = clearCSVData();
                $header = array('ANREDE', 'NAME1', 'NAME2', 'LAND', 'PLZ', 'ORT', 'STRASSE', 'TEL', 'FAX', 'EMAIL', 'KONTAKT', 'ID', 'KDNR', 'USTID', 'STEUERNR', 'KTONR', 'BANK', 'BLZ', 'LANG', 'KDTYP');
                if (isset($_POST['umsatz']) and $_POST['umsatz'] != '') {
                    $header[] = "UMSATZ";
                }
                $sql = "select name from custom_variable_configs where module = 'CT'";
Beispiel #3
0
 while (!feof($f)) {
     $i = -1;
     $firma = "";
     $name = false;
     $id = false;
     $sql = "insert into contacts ";
     $keys = "(";
     $vals = " values (";
     foreach ($zeile as $data) {
         $i++;
         if (!in_array($in_fld[$i], $kunde_fld)) {
             continue;
         }
         $data = addslashes(trim($data));
         if ($in_fld[$i] == "firma" && $data) {
             $data = suchFirma($file, $data);
             if ($data) {
                 $id = $data["cp_cv_id"];
             }
             continue;
         } else {
             if ($in_fld[$i] == "firma") {
                 continue;
             }
         }
         if ($in_fld[$i] == "cp_cv_id" && $data) {
             $data = chkKdId($data);
             if ($data) {
                 $id = $data;
             }
             continue;