6 #require_once "DB.php";
7 require_once "MDB2.php";
9 if (file_exists ("conf$login.php")) {
10 require "conf$login.php";
15 $landarray=array("DEUTSCHLAND"=>"D","STEREICH"=>"A","OESTEREICH"=>"A","SCHWEIZ"=>"CH");
16 $taxarray=array("D"=>0,"A"=>1,"CH"=>2);
23 //$PGdns = "user='$ERPuser' password='$ERPpass' host='$ERPhost' dbname='$ERPdbname' port='$ERPport'";
25 $ERPdns= array('phptype' => 'pgsql',
26 'username' => $ERPuser,
27 'password' => $ERPpass,
28 'hostspec' => $ERPhost,
29 'database' => $ERPdbname,
32 $SHOPdns=array('phptype' => 'mysql',
33 'username' => $SHOPuser,
34 'password' => $SHOPpass,
35 'hostspec' => $SHOPhost,
36 'database' => $SHOPdbname,
39 /****************************************************
40 * Debugmeldungen in File schreiben
41 ****************************************************/
42 if ($debug) { $log=fopen("tmp/shop.log","a"); } // zum Debuggen
47 /****************************************************
48 * Shopverbindung aufbauen
49 ****************************************************/
50 /*$shop=DB::connect($SHOPdns);
51 if (!$shop) shopFehler("",$shop->getDebugInfo());
52 if (DB::isError($shop)) {
53 $nun=date("Y-m-d H:i:s");
54 if ($log) fputs($log,$nun.": Shop-Connect\n");
55 shopFehler("",$shop->getDebugInfo());
56 die ($shop->getDebugInfo());
60 $shop=MDB2::factory($SHOPdns,$options);
61 //echo "<pre>"; print_r($shop); echo "</pre>";
62 if (!$shop) shopFehler("",$shop->getMessage());
63 if (PEAR::isError($shop)) {
64 $nun=date("Y-m-d H:i:s");
65 if ($log) fputs($log,$nun.": Shop-Connect\n");
66 shopFehler("",$shop->getMessage());
67 die ($shop->getMessage());
69 if (ExportMode == "1") $shop->setCharset('utf8');
70 $shop->setFetchMode(MDB2_FETCHMODE_ASSOC);
73 /****************************************************
74 * ERPverbindung aufbauen
75 ****************************************************/
76 /*$erp=DB::connect($ERPdns);
77 if (!$erp) shopFehler("",$erp->getDebugInfo());
78 if (DB::isError($erp)) {
79 $nun=date("Y-m-d H:i:s");
80 if ($log) fputs($log,$nun.": ERP-Connect\n");
81 shopFehler("",$erp->getDebugInfo());
82 die ($erp->getDebugInfo());
84 $erp->autoCommit(true);
86 $options = array('result_buffering' => false,);
87 $erp = MDB2::factory($ERPdns,$options);
88 //echo "<pre>"; print_r($erp); echo "</pre>";
89 if (!$erp) shopFehler("",$erp->getMessage());
90 if (PEAR::isError($erp)) {
91 $nun=date("Y-m-d H:i:s");
92 if ($log) fputs($log,$nun.": ERP-Connect\n");
93 shopFehler("",$erp->getMessage());
94 die ($erp->getMessage());
96 if ($erp->autocommit) $erp->autocommit();
99 if ($SHOPchar and ExportMode != "1") {
100 $erp->setCharset($SHOPchar);
102 $erp->setFetchMode(MDB2_FETCHMODE_ASSOC);
106 /****************************************************
107 * SQL-Befehle absetzen
108 ****************************************************/
109 function query($db,$sql,$function="--") {
110 $nun=date("d.m.y H:i:s");
111 //if ($db<>"shop") { echo "$sql!$db!<br>"; flush(); };
112 if ($GLOBALS["log"]) fputs($GLOBALS["log"],$nun.": ".$function."\n".$sql."\n");
113 $rc=$GLOBALS[$db]->query($sql);
114 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rc,true)."\n");
115 if(PEAR::isError($rc)) {
123 /****************************************************
125 ****************************************************/
126 function getAll($db,$sql,$function="--") {
127 $nun=date("d.m.y H:i:s");
128 if ($GLOBALS["log"]) fputs($GLOBALS["log"],$nun.": ".$function."\n".$sql."\n");
129 //$rs=$GLOBALS[$db]->getAll($sql,DB_FETCHMODE_ASSOC);
130 $rs=$GLOBALS[$db]->queryAll($sql);
131 //if ($rs["message"]<>"") {
132 if ($rs->message<>"") {
133 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rs,true)."\n");
140 /****************************************************
142 * in: sql,err = string
144 * Fehlermeldungen ausgeben
145 *****************************************************/
146 function shopFehler($sql,$err) {
149 echo "</td></tr></table><font color='red'>$sql : $err</font><br>";
152 /****************************************************
153 * Nächste Auftragsnummer (ERP) holen
154 ****************************************************/
155 function getNextAnr() {
156 $sql="select * from defaults";
157 $sql1="update defaults set sonumber=";
158 $rs2=getAll("erp",$sql,"getNextAnr");
159 if ($rs2[0]["sonumber"]) {
160 $auftrag=$rs2[0]["sonumber"]+1;
161 $rc=query("erp",$sql1.$auftrag,"getNextAnr");
163 echo "Kann keine Auftragsnummer erzeugen - Abbruch";
172 /****************************************************
173 * Nächste Kundennummer (ERP) holen
174 ****************************************************/
175 function getNextKnr() {
176 $sql="select * from defaults";
177 $sql1="update defaults set customernumber='";
178 $rs2=getAll("erp",$sql,"getNextKnr");
179 if ($rs2[0]["customernumber"]) {
180 $kdnr=$rs2[0]["customernumber"]+1;
181 $rc=query("erp",$sql1.$kdnr."'","getNextKnr");
183 echo "Kann keine Kundennummer erzeugen - Abbruch";
193 //$shopdata=array("firma"=>"","abteilung"=>"","vorname"=>"","nachname"=>"","strasse"=>"","plz"=>"","ort"=>"","telefon"=>"","email"=>"","land"=>"","fax"=>"","notiz"=>"","postfach"=>"")
194 $shopdata=array( "id"=>"customers_id","kdnr"=>"customers_cid","bid"=>"orders_id", "anrede"=>" customers_gender",
195 "firma"=>"customers_company", "nachname"=>"customers_lastname", "vorname"=>"customers_firstname",
196 "strasse"=>"customers_street_address","plz"=>"customers_postcode","ort"=>"customers_city","land"=>"customers_country",
197 "telefon"=>"customers_phone","email"=>"customers_email_address","fax"=>"Fax","notiz"=>"comments",
199 "netto"=>"ot_subtotal","steuer"=>"ot_tax","datum"=>" date_purchased","bemerkung"=>"comments",
200 "artnr"=>"products_id","preis"=>"final_price","artikeltxt"=>" products_name","menge"=>" products_quantity");
202 $shopartikel=array( "id"=>"Artikel_ID","artnr"=>"Artikel_Nr","arttxt"=>"Name","artbeschr"=>"Beschreibung","gruppe"=>"Kategorie_ID",
203 "preis"=>"Preis","preis2"=>"Haendlerpreis","preis3"=>"Aktionspreis","gewicht"=>"Gewicht",
204 "bild"=>"Bild_gross","bestand"=>"Lagerbestand","minbestand"=>"Mindestlagermenge","steuer"=>"MwSt_Satz");
207 /****************************************************
208 * Ab hier Artikelexport aus ERP
209 ****************************************************/
210 // Ab hier Artikelexport aus ERP nur eine Sprache
211 function shopartikellang($lang,$alle) {
212 $sql="SELECT P.partnumber,L.translation,P.description,L.longdescription,P.notes,PG.partsgroup ";
213 $sql.="FROM parts P left join translation L on L.parts_id=P.id left join partsgroup PG on PG.id=P.partsgroup_id ";
214 $sql.="WHERE P.shop='t' and (L.language_id = $lang";
216 $sql.=" or L.language_id is Null)";
217 } else { $sql.=")"; };
218 $rs=getAll("erp",$sql,"shopartikellang");
220 if ($rs) foreach ($rs as $row) {
221 if (!$data[$row["partnumber"]]) $data[$row["partnumber"]]=$row;
225 // Ab hier alle Artikelexport aus ERP Defaultsprache
226 function shopartikel() {
227 global $stdprice,$altprice;
229 $sql="SELECT P.partnumber,P.description,P.weight,(t.rate * 100) as rate,G.price as sellprice,P.sellprice as stdprice, ";
230 $sql.="PG.partsgroup,P.notes,P.image,P.onhand,G.pricegroup_id,P.buchungsgruppen_id as bugru FROM ";
231 $sql.="chart c left join tax t on c.taxkey_id=t.taxkey, parts P left join partsgroup PG on ";
232 $sql.="PG.id=P.partsgroup_id left join prices G on G.parts_id=P.id ";
233 $sql.="left join buchungsgruppen B on P.buchungsgruppen_id = B.id ";
234 $sql.="where P.shop='t' and c.id=B.income_accno_id_0 and ";
235 $sql.="(G.pricegroup_id=$stdprice or G.pricegroup_id=$altprice or G.pricegroup_id is null) ";
236 $sql.="order by P.partnumber";
238 $sql="SELECT P.partnumber,P.description,P.weight,(t.rate * 100) as rate,P.sellprice,PG.partsgroup,";
239 $sql.="P.notes,P.image,P.onhand,P.buchungsgruppen_id as bugru FROM ";
240 $sql.="chart c left join tax t on c.taxkey_id=t.taxkey, parts P left join partsgroup PG on ";
241 $sql.="PG.id=P.partsgroup_id left join buchungsgruppen B on P.buchungsgruppen_id = B.id ";
242 $sql.="WHERE P.shop='t' and c.id=B.income_accno_id_0";
244 $rs=getAll("erp",$sql,"shopartikel");
247 if ($rs) foreach ($rs as $row) {
248 if (!$data[$row["partnumber"]]) $data[$row["partnumber"]]=$row;
249 if ($row["pricegroup_id"]==$altprice) {
250 $data[$row["partnumber"]]["altprice"]=($row["sellprice"])?$row["sellprice"]:$row["stdprice"];
252 $data[$row["partnumber"]]["sellprice"]=($row["sellprice"])?$row["sellprice"]:$row["stdprice"];
260 /****************************************************
261 * Artikelexport in ERP importieren
262 ****************************************************/
263 function insertArtikel($data) {
265 foreach ($data as $row) {
266 $pg=$GLOBALS["warengruppen"][$row[$shopartikel["gruppe"]]]["partsgroup"];
267 $bg=$GLOBALS["buchungsgruppen"][sprintf("%0.2f",$row[$shopartikel["steuer"]])];
268 $artnr=($row[$shopartikel["artnr"]])?$row[$shopartikel["artnr"]]:getArtnr();
269 $sqltmp="insert into parts (partnumber,description,notes,weight,onhand,rop,image,sellprice,unit,partsgroup_id,buchungsgruppen_id) ";
270 $sqltmp.="values ('%s','%s','%s',%0.5f,%0.5f,%0.5f,'%s',%0.5f,'%s',%d,%d)";
271 $sql=sprintf($sqltmp,$artnr,$row[$shopartikel["arttxt"]],$row[$shopartikel["artbeschr"]],
272 $row[$shopartikel["gewicht"]],$row[$shopartikel["bestand"]],$row[$shopartikel["minbestand"]],
273 $row[$shopartikel["bild"]],$row[$shopartikel["preis"]],$row[$shopartikel["einheit"]],$pg,$bg);
274 $rc=query("erp",$sql,"insertArtikel");
276 echo $row[$shopartikel["id"]]." ".$row[$shopartikel["arttxt"]]." nicht importiert<br>";
284 /****************************************************
285 * Nächste Artikelnummer (ERP) holen
286 ****************************************************/
287 function getArtnr() {
288 $sql="select * from defaults";
289 $sql1="update defaults set articlenumber='";
291 echo "Kann keine Artikelnummer erzeugen - Abbruch";
294 $rs2=getAll("erp",$sql,"getArtnr");
295 $artnr=$rs2[0]["articelnumber"]+1;
296 $rc=query("erp",$sql1.$artnr."'","getArtnr");
298 echo "Kann keine Artikelnummer erzeugen - Abbruch";
299 $rc=query("erp","ROLLBACK","getArtnr");
305 $buchungsgruppen=array();
306 $warengruppen=array();
308 function getBugru() {
309 $sql ="select B.id,tax.rate from buchungsgruppen B left join chart on income_accno_id_0=chart.id left join taxkeys T on ";
310 $sql.="T.chart_id=income_accno_id_0 left join tax on tax.id=T.tax_id where T.startdate<=now()";
311 $rs=getAll("erp",$sql,"getBugru");
312 if ($rs) foreach ($rs as $row) {
313 $steuer=sprintf("%0.2f",$row["rate"]*100);
314 $GLOBALS["buchungsgruppen"][$steuer]=$row["id"];
320 function insPartgroup($kat) {
321 $sql="insert into partsgroup () value ()";
323 //$rc=query("erp",$sql,"insPartgroup");
324 if ($rc === -99) { return false; }
325 else { return $GLOBALS["wg"]; }