7 if (file_exists ("conf$login.php")) {
8 require "conf$login.php";
13 $landarray=array("DEUTSCHLAND"=>"D","STEREICH"=>"A","OESTEREICH"=>"A","SCHWEIZ"=>"CH");
14 $taxarray=array("D"=>0,"A"=>1,"CH"=>2);
21 //$PGdns = "user='$ERPuser' password='$ERPpass' host='$ERPhost' dbname='$ERPdbname' port='$ERPport'";
23 $ERPdns= array('phptype' => 'pgsql',
24 'username' => $ERPuser,
25 'password' => $ERPpass,
26 'hostspec' => $ERPhost,
27 'database' => $ERPdbname,
30 $SHOPdns=array('phptype' => 'mysql',
31 'username' => $SHOPuser,
32 'password' => $SHOPpass,
33 'hostspec' => $SHOPhost,
34 'database' => $SHOPdbname,
37 /****************************************************
38 * Debugmeldungen in File schreiben
39 ****************************************************/
40 if ($debug) { $log=fopen("tmp/shop.log","a"); } // zum Debuggen
45 /****************************************************
46 * Shopverbindung aufbauen
47 ****************************************************/
48 $shop=DB::connect($SHOPdns);
49 if (!$shop) shopFehler("",$shop->getDebugInfo());
50 if (DB::isError($shop)) {
51 $nun=date("Y-m-d H:i:s");
52 if ($log) fputs($log,$nun.": Shop-Connect\n");
53 shopFehler("",$shop->getDebugInfo());
54 die ($shop->getDebugInfo());
57 /****************************************************
58 * ERPverbindung aufbauen
59 ****************************************************/
60 $erp=DB::connect($ERPdns);
61 if (!$erp) shopFehler("",$erp->getDebugInfo());
62 if (DB::isError($erp)) {
63 $nun=date("Y-m-d H:i:s");
64 if ($log) fputs($log,$nun.": ERP-Connect\n");
65 shopFehler("",$erp->getDebugInfo());
66 die ($erp->getDebugInfo());
68 $erp->autoCommit(true);
72 /****************************************************
73 * SQL-Befehle absetzen
74 ****************************************************/
75 function query($db,$sql,$function="--") {
76 $nun=date("d.m.y H:i:s");
77 //if ($db<>"shop") { echo "$sql!$db!<br>"; flush(); };
78 if ($GLOBALS["log"]) fputs($GLOBALS["log"],$nun.": ".$function."\n".$sql."\n");
79 $rc=$GLOBALS[$db]->query($sql);
80 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rc,true)."\n");
88 /****************************************************
90 ****************************************************/
91 function getAll($db,$sql,$function="--") {
92 $nun=date("d.m.y H:i:s");
93 if ($GLOBALS["log"]) fputs($GLOBALS["log"],$nun.": ".$function."\n".$sql."\n");
94 $rs=$GLOBALS[$db]->getAll($sql,DB_FETCHMODE_ASSOC);
95 if ($rs["message"]<>"") {
96 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rs,true)."\n");
103 /****************************************************
105 * in: sql,err = string
107 * Fehlermeldungen ausgeben
108 *****************************************************/
109 function shopFehler($sql,$err) {
112 echo "</td></tr></table><font color='red'>$sql : $err</font><br>";
115 /****************************************************
116 * Nächste Auftragsnummer (ERP) holen
117 ****************************************************/
118 function getNextAnr() {
119 $sql="select * from defaults";
120 $sql1="update defaults set sonumber=";
121 $rs2=getAll("erp",$sql,"getNextAnr");
122 if ($rs2[0]["sonumber"]) {
123 $auftrag=$rs2[0]["sonumber"]+1;
124 $rc=query("erp",$sql1.$auftrag,"getNextAnr");
126 echo "Kann keine Auftragsnummer erzeugen - Abbruch";
135 /****************************************************
136 * Nächste Kundennummer (ERP) holen
137 ****************************************************/
138 function getNextKnr() {
139 $sql="select * from defaults";
140 $sql1="update defaults set customernumber='";
141 $rs2=getAll("erp",$sql,"getNextKnr");
142 if ($rs2[0]["customernumber"]) {
143 $kdnr=$rs2[0]["customernumber"]+1;
144 $rc=query("erp",$sql1.$kdnr."'","getNextKnr");
146 echo "Kann keine Kundennummer erzeugen - Abbruch";
156 //$shopdata=array("firma"=>"","abteilung"=>"","vorname"=>"","nachname"=>"","strasse"=>"","plz"=>"","ort"=>"","telefon"=>"","email"=>"","land"=>"","fax"=>"","notiz"=>"","postfach"=>"")
157 $shopdata=array( "id"=>"customers_id","kdnr"=>"customers_cid","bid"=>"orders_id", "anrede"=>" customers_gender",
158 "firma"=>"customers_company", "nachname"=>"customers_lastname", "vorname"=>"customers_firstname",
159 "strasse"=>"customers_street_address","plz"=>"customers_postcode","ort"=>"customers_city","land"=>"customers_country",
160 "telefon"=>"customers_phone","email"=>"customers_email_address","fax"=>"Fax","notiz"=>"comments",
162 "netto"=>"ot_subtotal","steuer"=>"ot_tax","datum"=>" date_purchased","bemerkung"=>"comments",
163 "artnr"=>"products_id","preis"=>"final_price","artikeltxt"=>" products_name","menge"=>" products_quantity");
165 $shopartikel=array( "id"=>"Artikel_ID","artnr"=>"Artikel_Nr","arttxt"=>"Name","artbeschr"=>"Beschreibung","gruppe"=>"Kategorie_ID",
166 "preis"=>"Preis","preis2"=>"Haendlerpreis","preis3"=>"Aktionspreis","gewicht"=>"Gewicht",
167 "bild"=>"Bild_gross","bestand"=>"Lagerbestand","minbestand"=>"Mindestlagermenge","steuer"=>"MwSt_Satz");
170 /****************************************************
171 * Ab hier Artikelexport aus ERP
172 ****************************************************/
173 // Ab hier Artikelexport aus ERP nur eine Sprache
174 function shopartikellang($lang,$alle) {
175 $sql="SELECT P.partnumber,L.translation,P.description,L.longdescription,P.notes,PG.partsgroup ";
176 $sql.="FROM parts P left join translation L on L.parts_id=P.id left join partsgroup PG on PG.id=P.partsgroup_id ";
177 $sql.="WHERE P.shop='t' and (L.language_id = $lang";
179 $sql.=" or L.language_id is Null)";
180 } else { $sql.=")"; };
181 $rs=getAll("erp",$sql,"shopartikellang");
183 if ($rs) foreach ($rs as $row) {
184 if (!$data[$row["partnumber"]]) $data[$row["partnumber"]]=$row;
188 // Ab hier alle Artikelexport aus ERP Defaultsprache
189 function shopartikel() {
190 global $stdprice,$altprice;
192 $sql="SELECT P.partnumber,P.description,P.weight,(t.rate * 100) as rate,G.price as sellprice,P.sellprice as stdprice, ";
193 $sql.="PG.partsgroup,P.notes,P.image,P.onhand,G.pricegroup_id,P.buchungsgruppen_id as bugru FROM ";
194 $sql.="chart c left join tax t on c.taxkey_id=t.taxkey, parts P left join partsgroup PG on ";
195 $sql.="PG.id=P.partsgroup_id left join prices G on G.parts_id=P.id ";
196 $sql.="left join buchungsgruppen B on P.buchungsgruppen_id = B.id ";
197 $sql.="where P.shop='t' and c.id=B.income_accno_id_0 and ";
198 $sql.="(G.pricegroup_id=$stdprice or G.pricegroup_id=$altprice or G.pricegroup_id is null) ";
199 $sql.="order by P.partnumber";
201 $sql="SELECT P.partnumber,P.description,P.weight,(t.rate * 100) as rate,P.sellprice,PG.partsgroup,";
202 $sql.="P.notes,P.image,P.onhand,P.buchungsgruppen_id as bugru FROM ";
203 $sql.="chart c left join tax t on c.taxkey_id=t.taxkey, parts P left join partsgroup PG on ";
204 $sql.="PG.id=P.partsgroup_id left join buchungsgruppen B on P.buchungsgruppen_id = B.id ";
205 $sql.="WHERE P.shop='t' and c.id=B.income_accno_id_0";
207 $rs=getAll("erp",$sql,"shopartikel");
210 if ($rs) foreach ($rs as $row) {
211 if (!$data[$row["partnumber"]]) $data[$row["partnumber"]]=$row;
212 if ($row["pricegroup_id"]==$altprice) {
213 $data[$row["partnumber"]]["altprice"]=($row["sellprice"])?$row["sellprice"]:$row["stdprice"];
215 $data[$row["partnumber"]]["sellprice"]=($row["sellprice"])?$row["sellprice"]:$row["stdprice"];
223 /****************************************************
224 * Artikelexport in ERP importieren
225 ****************************************************/
226 function insertArtikel($data) {
228 foreach ($data as $row) {
229 $pg=$GLOBALS["warengruppen"][$row[$shopartikel["gruppe"]]]["partsgroup"];
230 $bg=$GLOBALS["buchungsgruppen"][sprintf("%0.2f",$row[$shopartikel["steuer"]])];
231 $artnr=($row[$shopartikel["artnr"]])?$row[$shopartikel["artnr"]]:getArtnr();
232 $sqltmp="insert into parts (partnumber,description,notes,weight,onhand,rop,image,sellprice,unit,partsgroup_id,buchungsgruppen_id) ";
233 $sqltmp.="values ('%s','%s','%s',%0.5f,%0.5f,%0.5f,'%s',%0.5f,'%s',%d,%d)";
234 $sql=sprintf($sqltmp,$artnr,$row[$shopartikel["arttxt"]],$row[$shopartikel["artbeschr"]],
235 $row[$shopartikel["gewicht"]],$row[$shopartikel["bestand"]],$row[$shopartikel["minbestand"]],
236 $row[$shopartikel["bild"]],$row[$shopartikel["preis"]],$row[$shopartikel["einheit"]],$pg,$bg);
237 $rc=query("erp",$sql,"insertArtikel");
239 echo $row[$shopartikel["id"]]." ".$row[$shopartikel["arttxt"]]." nicht importiert<br>";
247 /****************************************************
248 * Nächste Artikelnummer (ERP) holen
249 ****************************************************/
250 function getArtnr() {
251 $sql="select * from defaults";
252 $sql1="update defaults set articlenumber='";
254 echo "Kann keine Artikelnummer erzeugen - Abbruch";
257 $rs2=getAll("erp",$sql,"getArtnr");
258 $artnr=$rs2[0]["articelnumber"]+1;
259 $rc=query("erp",$sql1.$artnr."'","getArtnr");
261 echo "Kann keine Artikelnummer erzeugen - Abbruch";
262 $rc=query("erp","ROLLBACK","getArtnr");
268 $buchungsgruppen=array();
269 $warengruppen=array();
271 function getBugru() {
272 $sql ="select B.id,tax.rate from buchungsgruppen B left join chart on income_accno_id_0=chart.id left join taxkeys T on ";
273 $sql.="T.chart_id=income_accno_id_0 left join tax on tax.id=T.tax_id where T.startdate<=now()";
274 $rs=getAll("erp",$sql,"getBugru");
275 if ($rs) foreach ($rs as $row) {
276 $steuer=sprintf("%0.2f",$row["rate"]*100);
277 $GLOBALS["buchungsgruppen"][$steuer]=$row["id"];
283 function insPartgroup($kat) {
284 $sql="insert into partsgroup () value ()";
286 //$rc=query("erp",$sql,"insPartgroup");
287 if ($rc === -99) { return false; }
288 else { return $GLOBALS["wg"]; }