Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / olcom / shopimport_csv.php
1 <?php
2 /***************************************************************
3 * $Id: shopimport_csv.php,v 1.2 2004/06/30 08:31:35 hli Exp $
4 *Author: Holger Lindemann
5 *Copyright: (c) 2004 Lx-System
6 *License: non free
7 *eMail: info@lx-system.de
8 *Version: 1.0.0
9 *Shop: olCommerce 1.2.2
10 ***************************************************************/
11
12 require_once "DB.php";
13 require_once "conf.php";
14 if ($dbprefix<>"") { define("PREFIX",$dbprefix."_"); } else { define("PREFIX",""); }
15
16 $LAND=array("Germany"=>"D");
17 $db=DB::connect($SHOPdns);
18 if (!$db) dbFehler("",$db->getDebugInfo());
19 if (DB::isError($db)) {
20         dbFehler("",$db->getDebugInfo());
21         die ($db->getDebugInfo());
22 };
23
24 function createCategory($name,$maingroup) {
25 global $db,$langs;
26         $newID=uniqid(rand());
27         $sql="insert into ".PREFIX."categories (categories_image,parent_id,date_added) values ('$newID',$maingroup,now())";
28         $rc=$db->query($sql);
29         $sql="select * from ".PREFIX."categories where categories_image = '$newID'";
30         $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
31         $id=$rs[0]["categories_id"];
32         $sql="update ".PREFIX."categories set categories_image = null where categories_id=$id";
33         $rc=$db->query($sql);
34         echo "($name) ";
35         foreach ($langs as $LANG) {
36                 $sql="insert into ".PREFIX."categories_description (categories_id,language_id,categories_name) values ($id,$LANG,'$name')";
37                 $rc=$db->query($sql);
38                 if (!$rc) break;
39         }
40         return ($rc)?$id:false;
41 }
42 function getCategory($name) {
43 global $db;
44         if (empty($name)) $name="Default";
45         $tmp=split("!",$name);
46         $maingroup=0;
47         $found=true;
48         $i=0;
49         do {
50                 $sql="select D.*,C.parent_id from ".PREFIX."categories C left join ".PREFIX."categories_description D on C.categories_id=D.categories_id where categories_name like '".$tmp[$i]."' and C.parent_id=$maingroup";
51                 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
52                 if ($rs[0]["categories_id"]) {
53                         $maingroup=$rs[0]["categories_id"];
54                         echo $maingroup.":".$rs[0]["categories_name"]." ";
55                         $i++;
56                 } else {
57                         $found=false;
58                 }
59         } while ($rs and $found and $i<count($tmp));
60         for (;$i<count($tmp); $i++) {
61                 $maingroup=createCategory($tmp[$i],$maingroup);
62         }
63         return $maingroup;
64 }
65 function insartikel($data) {
66 global $db,$header,$tax,$defLang;
67         $newID=uniqid(rand());
68         $sql="insert into ".PREFIX."products (products_model,products_image) values ('".$data[array_search("products_model")]."','$newID')";
69         $rc=$db->query($sql);
70         $sql="select * from ".PREFIX."products where products_image='$newID'";
71         $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
72         $sql="update ".PREFIX."products set products_image=null,products_status=1 where products_id=".$rs[0]["products_id"];
73         $rc=$db->query($sql);
74         $sql="insert into ".PREFIX."products_description (products_id,language_id,products_name) values (".$rs[0]["products_id"].",$defLang,' ')";
75         $rc=$db->query($sql);
76         $sql="insert into ".PREFIX."products_to_categories (products_id,categories_id) values (".$rs[0]["products_id"].",".$data["categories_id"].")";
77         $rc=$db->query($sql);
78         echo " <b>insert</b> ";
79         updartikel($data,$rs[0]["products_id"]);
80 }
81 function updartikel($data,$id) {
82 global $db,$header,$tax,$defLang;
83         $sql="update ".PREFIX."products set products_price=%01.2f,products_weight=%01.2f,products_tax_class_id=%d,products_last_modified=now()  where products_id=%d";
84         $sql=sprintf($sql,$data[array_search("products_price",$header)],$data[array_search("products_weight",$header)],$tax[$data[array_search("products_tax",$header)]],$id);
85         $rc=$db->query($sql);
86         $sql="update ".PREFIX."products_description set products_name='%s',products_description='%s' where products_id=%d and language_id=$defLang";
87         $sql=sprintf($sql,$data[array_search("products_name",$header)],$data[array_search("products_description",$header)],$id);
88         $rc=$db->query($sql);
89         $sql="update ".PREFIX."products_to_categories set categories_id=".$data[array_search("categories_id",$header)]." where products_id=$id";
90         $rc=$db->query($sql);
91         echo "(".$id." ".$data[array_search("products_name",$header)].")+++<br>";
92 }
93 function chkartikel($data) {
94 global $db,$header,$tax;
95         $sql="select * from ".PREFIX."products P left join ".PREFIX."products_description D on P.products_id=D.products_id left join ".PREFIX."products_to_categories C on P.products_id=C.products_id where  products_model like '".$data[array_search("products_model",$header)]."' and language_id=2";
96         $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
97         if ($rs) {
98                          if ($rs[0]["products_price"]<>$data[array_search("products_price",$header)])   { updartikel($data,$rs[0]["products_id"]); }
99                 else if ($rs[0]["products_weight"]<>$data[array_search("products_weight",$header)])     { updartikel($data,$rs[0]["products_id"]); }
100                 else if ($rs[0]["products_name"]<>$data[array_search("products_name",$header)])         { updartikel($data,$rs[0]["products_id"]); }
101                 else if ($rs[0]["products_description"]<>$data[array_search("products_description",$header)])   { updartikel($data,$rs[0]["products_id"]); }
102                 else if ($rs[0]["products_tax_class_id"]<>$tax[$data[array_search("products_tax",$header)]])    { updartikel($data,$rs[0]["products_id"]); }
103                 else if ($rs[0]["categories_id"]<>$data[array_search("categories_id",$header)])         { updartikel($data,$rs[0]["products_id"]); }
104                 else { echo "(".$rs[0]["products_id"]." ".$rs[0]["products_name"].")...<br>"; };
105         } else {
106                 insartikel($data);
107         }
108 }
109
110 $sql="select languages_id from languages";
111 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
112 if ($rs) {
113         foreach ($rs as $zeile) {
114                 $langs[]=$zeile["languages_id"];
115         }
116 } else {
117         $langs[]=1;
118 }
119 $sql="select * from ".PREFIX."languages L left join ".PREFIX."configuration C on L.code=C.configuration_value where  configuration_key = 'DEFAULT_LANGUAGE'";
120 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
121 if ($rs) {
122         $defLang=$rs[0]["languages_id"];
123 } else {
124         $defLang=$SHOPlang;
125 }
126 $sql="select * from ".PREFIX."tax_rates";
127 $rs=$db->getAll($sql,DB_FETCHMODE_ASSOC);
128 if ($rs) {
129         foreach ($rs as $zeile) {
130                 $tax[$zeile["tax_rate"]]=$zeile["tax_class_id"];
131         }
132 } else {
133         $tax[0]="";
134 }
135
136 if ($_FILES["csv"]["name"] || ($_POST["nofile"] && file_exists($SHOPdir)) ) {
137         if ($_FILES["csv"]["tmp_name"]) {
138                 move_uploaded_file($_FILES["csv"]["tmp_name"],$SHOPdir);
139         }
140         $f=fopen($SHOPdir,"r");
141         $header=fgetcsv($f,1000,";");
142         $header[]="categories_id";
143         $data=fgetcsv($f,1000,";");
144         while (!feof($f)) {
145                 $catId=getCategory($data[array_search("categories_name",$header)]);
146                 $data[]=$catId;
147                 chkartikel($data);
148                 $data=fgetcsv($f,1000,";");
149         }
150         fclose($f);
151         echo "<a href='trans.php'>zur&uuml;ck</a>";
152 } else {
153 ?>
154 <html>
155         <head>
156                 <title>Datenaustausch ERP-xt:Commerce</title>
157         </head>
158 <body>
159 <center>
160 <br>
161 <h1>Artikelimport aus csv-Datei in xt:Commerce</h1><br>
162 <form name="csv" action="shopimport_csv.php" enctype="multipart/form-data" method="post">
163         <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="500000">
164         <input type="checkbox" name="nofile" value="1">Auf dem Server vorhandene Daten importieren<br>
165         Datenfile f&uuml;r Import <input type="file" name="csv"><br>
166         <input type="submit" name="ok" value="ok">
167 </form>
168 </center>
169 <a href="trans.php">zur&uuml;ck</a>
170 </body>
171 </html>
172 <?
173 }
174 ?>