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