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) {
14 $sql="select id from partsgroup where partsgroup = '$value'";
15 $rs=$db->getAll($sql);
16 if (empty($rs[0]["id"]) && $add) {
17 $sql="insert into partsgroup (partsgroup) values ('$value')";
21 return getPartsgroupId($db, $value, 0);
26 function getAccnoId($db, $accno) {
27 $sql = "select id from chart where accno='$accno'";
28 $rs=$db->getAll($sql);
32 function chkPartNumber($db,$number,$check) {
34 $sql = "select * from parts where partnumber = '$number'";
35 $rs=$db->getAll($sql);
37 //echo $sql; print_r($rs);
38 if ($rs[0]["id"]>0 or $number=="") {
39 if ($check) return "check";
40 $rc=$db->query("BEGIN");
41 $sql = "select articlenumber from defaults";
42 $rs=$db->getAll($sql);
43 if ($rs[0]["articlenumber"]) {
44 preg_match("/([^0-9]+)?([0-9]+)([^0-9]+)?/", $rs[0]["articlenumber"] , $regs);
45 $number=$regs[1].($regs[2]+1).$regs[3];
47 $sql = "update defaults set articlenumber = '$number'";
49 $rc=$db->query("COMMIT");
50 $sql = "select * from parts where partnumber = '$number'";
51 $rs=$db->getAll($sql);
52 if ($rs[0]["id"]>0) return "";
57 function getBuchungsgruppe($db, $income, $expense) {
59 $income_id = getAccnoId($db, $income);
60 $expense_id = getAccnoId($db, $expense);
61 //$accno0_id = getAccnoId($db, $accno0);
62 //$accno1_id = getAccnoId($db, $accno1);
63 //$accno3_id = getAccnoId($db, $accno3);
65 $sql = "select id from buchungsgruppen where ";
66 $sql .= "income_accno_id_0 = $income_id and ";
67 $sql .= "expense_accno_id_0 = $expense_id ";
68 //$sql .= "income_accno_id_0 = '$accno0_id' ";
69 //$sql .= "and income_accno_id_1 = '$accno1_id' ";
70 //$sql .= "and income_accno_id_3 = '$accno3_id'";
71 $rs=$db->getAll($sql);
76 function getFromBG($db, $bg_id, $name) {
78 $sql = "select $name from buchungsgruppen where id='$bg_id'";
79 $rs=$db->getAll($sql);
83 function existUnit($db, $value) {
84 $sql="select name from units where name = '$value'";
85 $rs=$db->getAll($sql);
86 if (empty($rs[0]["name"]))
91 function show($show, $things) {
96 function import_parts($db, $file, $trenner, $fields, $check, $insert, $show,$maske) {
98 /* field description */
99 $parts_fld = array_keys($fields);
102 $f=fopen("$file.csv","r");
105 * read first line with table descriptions
107 show( $show, "<table border='1'><tr><td>#</td>\n");
108 $infld=fgetcsv($f,1200,$trenner);
109 foreach ($infld as $fld) {
110 $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
112 if (in_array(trim($fld),$parts_fld)) {
113 show( $show, "<td>$fld</td>\n");
118 $errors=0; /* number of errors detected */
121 while ( ($zeile=fgetcsv($f,1200,$trenner)) != FALSE) {
123 $m++; /* increase line */
125 $sql="insert into $file ";
129 show( $show, "<tr><td>$m</td>\n");
131 /* for each column */
132 $dienstleistung=false;
135 foreach($zeile as $data) {
136 /* check if column will be imported */
137 if (!in_array(trim($in_fld[$i]),$parts_fld)) {
142 //$data=addslashes($data);
144 /* add key and data */
145 if ($data==false or empty($data) or !$data) {
146 show( $show, "<td>NULL</td>\n");
151 /* special case partsgroup */
152 if ($key == "partsgroup") {
154 /* get ID of partsgroup or add new
156 $data = getPartsgroupId($db, $data, $insert);
157 $key = "partsgroup_id";
159 /* TODO error handling */
161 } else if ($key == "lastcost" ||
162 $key == "sellprice") {
164 /* convert 0,0 numeric into 0.0 */
165 $data = str_replace(",", ".", $data);
167 } else if ($key == "partnumber") {
169 $partnumber=chkPartNumber($db,$data,$check);
170 if ($partnumber=="") {
171 show( $show, "<td>NULL</td>\n");
175 //$keys.="partnumber, ";
177 //show( $show, "<td>$partnumber</td>\n");
179 } else if ($key == "description") {
180 $data=mb_convert_encoding($data,"ISO-8859-15","auto");
181 $data=addslashes($data);
182 } else if ($key == "notes") {
183 $data=mb_convert_encoding($data,"ISO-8859-15","auto");
184 $data=addslashes($data);
185 } else if ($key == "unit") {
186 /* convert stück and Stunde */
187 if (preg_match("/^st..?ck$/i", $data))
189 else if ($data == "Stunde")
191 /* check if unit exists */
192 if (!existUnit($db, $data)) {
193 echo "Error in line $m: ";
194 echo "Einheit <b>$data</b> existiert nicht ";
195 echo "Bitte legen Sie diese Einheit an<br>";
198 } else if ($key == "art") {
199 if ($maske["ware"]=="G" and strtoupper($data)=="D") { $artikel=false; }
200 else if ($maske["ware"]=="G") { $artikel=true; };
203 } else if ($key == "income_accno") {
204 $income_accno = $data;
206 show( $show, "<td>$data</td>\n");
208 } else if ($key == "expense_accno") {
209 $expense_accno = $data;
211 show( $show, "<td>$data</td>\n");
214 /* convert JA to Yes */
218 $vals.="'".$data."',";
219 show( $show, "<td>$data</td>\n");
225 if ($maske["ware"]=="D") { $artikel=false; }
226 else { $artikel=true; };
228 if ($maske["bugrufix"]==1) {
229 $bg = $maske["bugru"];
231 if ($income_accno<>"" and $expense_accno<>"") {
232 /* search for buchungsgruppe */
233 $bg = getBuchungsgruppe($db, $income_accno, $expense_accno);
234 if ($bg == "" and $maske["bugrufix"]==2 and $maske["bugru"]<>"") {
235 $bg = $maske["bugru"];
237 } else if ($maske["bugru"]<>"" and $maske["bugrufix"]==2) {
238 $bg = $maske["bugru"];
240 /* nothing found? user must create one */
241 echo "Error in line $m: ";
242 echo "Keine Buchungsgruppe gefunden für <br>";
243 echo "Erlöse Inland: $income_accno<br>";
244 echo "Bitte legen Sie eine an oder geben Sie eine vor.<br>";
250 /* found one, add income_accno_id etc from buchungsgr.
252 $keys.="buchungsgruppen_id, ";
254 /* XXX nur bei artikel!!! */
256 $keys.="inventory_accno_id, ";
257 $vals.=getFromBG($db, $bg, "inventory_accno_id")." ,";
259 $keys.="income_accno_id, ";
260 $vals.=getFromBG($db, $bg, "income_accno_id_0")." ,";
261 $keys.="expense_accno_id,";
262 $vals.=getFromBG($db, $bg, "expense_accno_id_0")." ,";
264 if ($partNr==false) {
265 $partnumber=chkPartNumber($db,"",$check);
266 if ($partnumber=="") {
267 show( $show, "<td>NULL</td>\n");
270 $keys.="partnumber, ";
271 $vals.="'$partnumber',";
272 show( $show, "<td>$partnumber</td>\n");
275 $sql.=$keys."import)";
276 $sql.=$vals.time().")";
277 //show( $show, "<td> $sql </td>\n");
280 show( $show, "<td>");
282 $rc=$db->query($sql);
287 show( $show, "</td>\n");
290 show( $show, "</tr>\n");
293 show( $show, "</table>\n");
295 echo "$m Zeilen bearbeitet. ($fehler : Fehler) ";