2 /***************************************************************
3 *Author: Holger Lindemann
4 *Copyright: (c) 2004 Lx-System
6 *eMail: info@lx-system.de
8 *Shop: osCommerce 2.2 ms2
9 *ERP: Lx-Office ERP 2.4.x
10 ***************************************************************/
16 <head><title>Lx-ERP Export der Shopartikel</title>
17 <!--link type="text/css" REL="stylesheet" HREF="../css/main.css"></link-->
21 require_once "shoplib.php";
23 function sonderkosten($transID,$data,$id,$f) {
24 global $versand,$nachn,$minder,$treuh,$paypal;
25 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
26 $sql.=$transID.",".${$id}["ID"].",'".${$id}["TXT"]."',1,".$data.",'Stck',0,0)";
27 fputs($f,"$transID,".${$id}["ID"].",'".${$id}["TXT"]."',1,$data\n");
28 if (!query("erp",$sql,"sonderkosten")) { return false; }
29 else { return true; };
32 function getBestellKunde($KID,$BID,$OID) {
34 $sql="select kdnr from customers_number where customers_id=$KID";
35 $rs=getAll("shop",$sql,"getBestellKunde");
36 if (!$rs or $rs[0]["kdnr"]<1) {
37 $kdnr=insKdData($BID);
39 $sql="insert into customers_number (customers_id,kdnr) values(".$KID.",".$kdnr.")";
40 $rc=query("shop",$sql,"getBestellKunde");
42 echo "Kundennummer nicht im Shop gespeichert: $KID<br>";
50 chkKdData($kdnr,$BID,$OID);
54 /**********************************************
55 * getAttribut($oid,$pid)
57 **********************************************/
58 function getAttribut($oid,$pid) {
59 $sql="select * from orders_products_attributes where orders_id=$oid and orders_products_id=$pid";
60 $rs=getAll("shop",$sql,"getAttribut");
62 foreach ($rs as $zeile) {
63 $txt.="\n - ".$zeile["products_options"].":".$zeile["products_options_values"];
67 function getTaxRate() {
68 $sql="select tax_rate from tax_rates R left join configuration C on C.configuration_value=R.tax_class_id ";
69 $sql.="where C.configuration_key like 'MODULE_SHIPPING%TAX%' limit 1";
70 $rs=getAll("shop",$sql,"getTaxRate");
71 if ($rs[0]["tax_rate"]) {
72 return (100+$rs[0]["tax_rate"])/100 ;
74 echo "Bitte mind. eine Versandart mit Steuer anlegen.";
78 function checkTaxIncl() {
79 $sql="select * from configuration where configuration_key = 'DISPLAY_PRICE_WITH_TAX'";
80 $rs=getAll("shop",$sql,"checkTaxIncl");
81 if ($rs[0]["configuration_value"]=="false") { return false; }
84 function getNetto($id) {
85 $sql="select * from orders_total where orders_id=$id and class='ot_subtotal'";
86 $rs=getAll("shop",$sql,"getNetto");
87 return $rs[0]["value"];
89 /**********************************************
92 **********************************************/
93 function getBrutto($id) {
94 $sql="select * from orders_total where orders_id=$id and class='ot_total'";
95 $rs=getAll("shop",$sql,"getBrutto");
96 return $rs[0]["value"];
99 /**********************************************
102 **********************************************/
103 function getMwSt($id) {
106 $sql="select * from orders_total where orders_id=$id and class='ot_tax'";
107 $rs=getAll("shop",$sql,"getMwSt");
109 foreach ($rs as $zeile) {
110 $mwst+=$zeile["value"];
114 //return $rs[0]["value"]/$TaxFactor;
116 /**********************************************
117 * getSonderkosten($id,$art)
119 **********************************************/
120 function getSonderkosten($id,$art) {
121 $sql="select * from orders_total where orders_id=$id and class='".$GLOBALS["skosten"][$art]."'";
122 $rs=getAll("shop",$sql,"getSonderkosten");
123 if ($rs[0]["value"]) {
124 $kosten=round($rs[0]["value"]/(100+$GLOBALS["versand"]["TAX"])*100,2);
130 /**********************************************
131 * insBestArtikel($zeile,$transID)
133 **********************************************/
134 function insBestArtikel($ordersID,$transID) {
135 global $div07,$div16;
136 $sql="select * from orders_products where orders_id=$ordersID";
137 $rs=getAll("shop",$sql,"insBestArtikel");
139 if ($rs) foreach ($rs as $zeile) {
140 $sql="select * from parts where partnumber='".$zeile["products_model"]."'";
141 $rs2=getAll("erp",$sql,"insBestArtikel");
142 if ( $rs2[0]["id"]) {
143 $artID=$rs2[0]["id"];
144 $artNr=$rs2[0]["partnumber"];
146 if ($zeile["products_tax"]=="19.0000") {
154 $preis=$zeile["final_price"];
155 $text=getAttribut($ordersID,$zeile["orders_products_id"]);
156 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) ";
158 $sql.=$transID.",".$artID.",'".$zeile["products_name"].$text."',".$zeile["products_quantity"].",";
159 $sql.=$preis.",'Stck',0,0)";
160 echo " - Artikel:[ BuNr.:$artID ArtNr:<b>$artNr</b> ".$zeile["products_name"]." ]<br>";
161 $rc=query("erp",$sql,"insBestArtikel");
162 if ($rc === -99) { $ok=false; break; };
166 function insAuftrag($data) {
167 global $ERPusr,$versand,$nachn,$minder,$paypal,$auftrnr;
168 $Zahlmethode=array("authorizenet"=>"Authorize.net","banktransfer"=>"Lastschriftverfahren","cc"=>"Kreditkarte",
169 "cod"=>"Nachnahme","eustandardtransfer"=>"EU-Standard Bank Transfer","iclear"=>"iclear Rechnungskauf",
170 "invoice"=>"Rechnung","ipayment"=>"iPayment","liberecobanktransfer"=>"Lastschriftverfahren",
171 "liberecocc"=>"Kreditkarte","moneybookers"=>"Moneybookers.com","moneyorder"=>"Scheck/Vorkasse",
172 "nochex"=>"NOCHEX","paypal"=>"PayPal","pm2checkout"=>"2CheckOut","psigate"=>"PSiGate",
173 "qenta"=>"qenta.at","secpay"=>"SECPay");
174 $brutto=getBrutto($data["orders_id"]);
175 $mwst=getMwSt($data["orders_id"]);
176 if ($GLOBALS["inclTax"]) {
177 $netto=$brutto-$mwst;
179 $netto=getNetto($data["orders_id"]);
181 $versandK=getSonderkosten($data["orders_id"],"Versand");
182 $nachnK =getSonderkosten($data["orders_id"],"NachName");
183 $mindermK=getSonderkosten($data["orders_id"],"Minder");
184 $paypalK =getSonderkosten($data["orders_id"],"Paypal");
189 // Hier beginnt die Transaktion
190 $rc=query("erp","BEGIN WORK","insAuftrag");
191 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
193 $auftrag=$GLOBALS["preA"].getNextAnr();
195 $auftrag=$GLOBALS["preA"].$data["orders_id"];
197 $sql="select count(*) as cnt from oe where ordnumber = '$auftrag'";
198 $rs=getAll("erp",$sql,"insAuftrag 1");
199 if ($rs[0]["cnt"]>0) {
200 $auftrag=$GLOBALS["preA"].getNextAnr();
202 $newID=uniqid (rand());
203 $sql="insert into oe (notes,ordnumber,cusordnumber) values ('$newID','$auftrag','".$data["kdnr"]."')";
204 $rc=query("erp",$sql,"insAuftrag 2");
206 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
207 $rc=query("erp","ROLLBACK WORK","chkKunde");
208 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
211 $sql="select * from oe where notes = '$newID'";
212 $rs2=getAll("erp",$sql,"insAuftrag 3");
214 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
215 $rc=query("erp","ROLLBACK WORK","chkKunde");
216 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
219 $zahlart=$data["payment_method"]."\n";
220 if ($data["cc_type"]) $zahlart.=$data["cc_type"]."\n".$data["cc_owner"]."\n".$data["cc_number"]."\n".$data["cc_expires"]."\n";
221 $sql="update oe set cusordnumber=".$data["orders_id"].", transdate='".$data["date_purchased"]."', customer_id=".$data["kdnr"].", ";
222 $sql.="amount=".$brutto.", netamount=".$netto.", reqdate='".$data["date_purchased"]."', taxincluded='f', ";
223 if ($data["shipto"]>0) $sql.="shipto_id=".$data["shipto"].", ";
224 $sql.="intnotes='".$data["comments"]."',notes='".$zahlart."', curr='EUR',employee_id=".$ERPusr["ID"].", vendor_id=0 ";
225 $sql.="where id=".$rs2[0]["id"];
226 $rc=query("erp",$sql,"insAuftrag 4");
228 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
229 $rc=query("erp","ROLLBACK WORK","chkKunde");
230 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
233 echo "Auftrag:[ Buchungsnummer:".$rs2[0]["id"]." AuftrNr:<b>".$auftrag."</b> ]<br>";
234 if (!insBestArtikel($data["orders_id"],$rs2[0]["id"])) {
235 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
236 $rc=query("erp","ROLLBACK WORK","chkKunde");
237 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
241 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
242 $sql.=$rs2[0]["id"].",".$versand["ID"].",'".$versand["TXT"]."',1,".$versandK.",'Stck',0,0)";
243 $rc=query("erp",$sql,"insAuftrag 8");
244 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den Versandkosten<br>";
247 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
248 $sql.=$rs2[0]["id"].",".$nachn["ID"].",'".$nachn["TXT"]."',1,".$nachnK.",'Stck',0,0)";
249 $rc=query("erp",$sql,"insAuftrag 9");
250 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den Nachnamekosten<br>";
253 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
254 $sql.=$rs2[0]["id"].",".$minder["ID"].",'".$minder["TXT"]."',1,".$mindermK.",'Stck',0,0)";
255 $rc=query("erp",$sql,"insAuftrag 10");
256 if ($rc === -99) echo "Auftrag $auftrag : Fehler beim Mindermengenzuschlag<br>";
259 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
260 $sql.=$rs2[0]["id"].",".$paypal["ID"].",'".$paypal["TXT"]."',1,".$paypalK.",'Stck',0,0)";
261 $rc=query("erp",$sql,"insAuftrag 11");
262 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den PayPal-Kosten<br>";
264 $sql="update orders set orders_status ='3' WHERE orders_id =".$data["orders_id"];
265 $rc=query("shop",$sql,"insBestArtikel 12");
266 if ($rc === -99) echo "Bestellung im Shop nicht geschlossen";
267 $rc=query("erp","COMMIT WORK","chkKunde");
268 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
272 /**********************************************
275 **********************************************/
276 function getBestellung() {
277 $sql="select o.*,h.comments,cn.kdnr from orders o left join orders_status_history h on h.orders_id=o.orders_id ";
278 $sql.="left join customers_number cn on ";
279 $sql.="cn.customers_id=o.customers_id where o.orders_status=1 order by o.orders_id";
280 $rs=getAll("shop",$sql,"getBestellung");
284 /**********************************************
287 **********************************************/
288 function chkKunden() {
289 $felder=array("firstname","lastname","company","street_address","city","postcode","country");
290 foreach ($GLOBALS["bestellungen"] as $bestellung) {
291 $rc=query("erp","BEGIN WORK","chkKunden");
292 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
293 if ($bestellung["kdnr"]>0) { // Bestandskunde; kdnr == ID in customers
295 $kdnr=chkOldKd($bestellung);
296 if ($kdnr == -1) { //Kunde nicht gefunden, neu anlegen.
298 $kdnr=insNewKd($bestellung);
301 echo $msg." ".$bestellung["customers_name"]." fehlgeschlagen!<br>";
307 $kdnr=insNewKd($bestellung);
310 echo $bestellung["customers_company"]." ".$bestellung["customers_name"]." $kdnr<br>";
311 $GLOBALS["bestellungen"][$GLOBALS["gesKd"]]["kdnr"]=$kdnr;
312 $sql="delete from customers_number where customers_id=".$bestellung["customers_id"];
313 $rc=query("shop",$sql,"chkKunde");
314 $sql="insert into customers_number (customers_id,kdnr) values(".$bestellung["customers_id"].",".$kdnr.")";
315 $rc=query("shop",$sql,"chkKunde");
317 foreach($felder as $feld) {
318 if ($bestellung["delivery_$feld"]<>$bestellung["customers_$feld"]) {
319 $rc=insShData($bestellung,$kdnr);
320 if ($rc>0) $GLOBALS["bestellungen"][$GLOBALS["gesKd"]]["shipto"]=$rc;
325 if (!$kdnr || $rc === -99) {
326 echo $msg." ".$bestellung["customers_name"]." fehlgeschlagen! ($kdnr,$rc)<br>";
327 $rc=query("erp","ROLLBACK WORK","chkKunde");
328 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
330 $rc=query("erp","COMMIT WORK","chkKunde");
331 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
338 function chkOldKd($data) {
339 $sql="select * from customer where id = ".$data["kdnr"];
340 $rs=getAll("erp",$sql,"chkKdData");
341 if (!$rs or count($rs)==0) { return -1; }; // Kunde nicht gefunden
342 if ($rs[0]["zipcode"]<>$data["customers_postcode"]) $set.="zipcode='".$data["customers_postcode"]."',";
343 if ($rs[0]["city"]<>$data["customers_city"]) $set.="city='".$data["customers_city"]."',";
344 if ($rs[0]["country"]<>$GLOBALS["taxarray"][$data["billing_country"]]["code"]) $set.="country='".$GLOBALS["taxarray"][$data["billing_country"]]["code"]."',";
345 if ($rs[0]["phone"]<>$data["customers_phone"])$set.="phone='".$data["customers_phone"]."',";
346 if ($rs[0]["email"]<>$data["customers_email_address"])$set.="email='".$data["customers_email_address"]."',";
347 if ($data["customers_company"]) {
348 if ($rs[0]["name"]<>$data["customers_company"]) $set.="name='".$data["customers_company"]."',";
349 if ($rs[0]["contact"]<>$data["customers_name"]) $set.="contact='".$data["customers_name"]."',";
351 if ($rs[0]["name"]<>$data["customers_name"]) $set.="name='".$data["customers_name"]."',";
353 if ($rs[0]["street"]<>$data["customers_street_address"]) $set.="street='".$data["customers_street_address"]."',";
355 $set=substr($set,0,-1);
356 $sql="update customer set $set where id=".$data["kdnr"];
357 $rc=query("erp",$sql,"chkKdData");
361 return $data["kdnr"];
364 return $data["kdnr"];
368 /**********************************************
369 * insShData($data,$id)
371 **********************************************/
372 function insShData($data,$id) {
374 if ($data["delivery_company"]) { $set.=",'".$data["delivery_company"]."','".$data["delivery_name"]."',"; }
375 else { $set.=",'".$data["delivery_name"]."','',"; }
376 $set.="'".$data["delivery_street_address"]."',";
377 $set.="'".$data["delivery_postcode"]."',";
378 $set.="'".$data["delivery_city"]."',";
379 if (in_array($data["delivery_country"],$GLOBALS["LAND"])) {
380 $set.="'".$GLOBALS["LAND"][$data["delivery_country"]]."',";
382 $set.="'".$data["delivery_country"]."',";
384 $set.="'".$data["customers_telephone"]."',";
385 $set.="'".$data["customers_email_address"]."'";
386 $sql="insert into shipto (trans_id,shiptoname,shiptodepartment_1,shiptostreet,shiptozipcode,shiptocity,";
387 $sql.="shiptocountry,shiptophone,shiptoemail,module) values ($set,'CT')";
388 $rc=query("erp",$sql,"insShData");
389 if ($rc === -99) return false;
390 $sql="select shipto_id from shipto where trans_id = $id and module='CT' order by itime desc limit 1";
391 $rs=getAll("erp",$sql,"insKdData");
392 if ($rs[0]["shipto_id"]>0) {
393 $sid=$rs[0]["shipto_id"];
394 $sql="update customers_number set shipto = $sid where kdnr = $id";
395 $rc2=query("shop",$sql,"insShData");
397 //$sql="delete from shipto where shipto_id=$sid";
398 //$rc=query("shop",$sql,"insShData");
403 echo "Fehler bei abweichender Anschrift ".$data["delivery_name"];
404 //$sql="delete from shipto where shipto_id=$sid";
405 //$rc=query("shop",$sql,"insShData");
410 /**********************************************
413 **********************************************/
414 function insNewKd($data) {
415 $newID=uniqid(rand(time(),1));
416 //Kundennummer generieren
417 if ($GLOBALS["kdnum"]==1) { // von der ERP
418 $kdnr=$GLOBALS["preK"].getNextKnr();
419 } else { // durch Shop
420 $kdnr=$GLOBALS["preK"].$data["customers_id"];
422 $sql="select count(*) as cnt from customer where customernumber = '$kdnr'";
423 $rs=getAll("erp",$sql,"insKdData");
424 if ($rs[0]["cnt"]>0) { // Kundennummer gibt es schon, eine neue aus ERP
425 $kdnr=$GLOBALS["preK"].getNextKnr();
427 $sql="insert into customer (name,customernumber) values ('$newID','$kdnr')";
428 $rc=query("erp",$sql,"insKdData");
429 if ($rc === -99) return false;
430 $sql="select * from customer where name = '$newID'";
431 $rs=getAll("erp",$sql,"insKdData");
432 if (!$rs) return false;
433 if ($data["customers_company"]) {
434 $set.="set name='".$data["customers_company"]."',contact='".$data["customers_name"]."',";
436 $tmp=strrpos($data["customers_name"]," ");
438 $set.="set name='".substr($data["customers_name"],$tmp+1).", ".substr($data["customers_name"],0,$tmp)."',";
439 $set.="contact='".$data["customers_name"]."',";
441 $set.="set name='".$data["customers_name"]."',";
444 $set.="street='".$data["customers_street_address"]."',";
445 $set.="zipcode='".$data["customers_postcode"]."',";
446 $set.="city='".$data["customers_city"]."',";
447 $set.="country='".$GLOBALS["taxarray"][$data["billing_country"]]["code"]."',";
448 $set.="phone='".$data["customers_telephone"]."',";
449 $set.="email='".$data["customers_email_address"]."',";
450 $tid=(in_array($data["billing_country"],array_keys($GLOBALS["taxarray"])))?$GLOBALS["taxarray"][$data["billing_country"]]["tax"]:3;
451 $set.="taxzone_id=$tid,";
452 $set.="taxincluded='f' ";
453 $sql="update customer ".$set;
454 $sql.="where id=".$rs[0]["id"];
455 $rc=query("erp",$sql,"insKdData");
457 //$sql="delete from customer where id=".$rs[0]["id"];
458 //$rc=query("shop",$sql,"insNewKd");
460 } else { return $rs[0]["id"]; }
464 $LAND=array("Germany"=>"D","Austria"=>"A","Switzerland"=>"CH");
465 $skosten=array("Versand"=>"ot_shipping","NachName"=>"ot_cod_fee","Paypal"=>"ot_cod_fee","Minder"=>"ot_loworderfee");
466 $inclTax=checkTaxIncl();
467 $TaxFactor=($inclTax)?getTaxRate():1;
468 $bestellungen=getBestellung();
469 $ok=count($bestellungen);
473 echo "Es liegen $ok Bestellungen vor. <br>";
475 echo $gesKd." Kunde(n), davon ".$neuKd." neue(r) Kunde(n).<br>";
476 foreach ($bestellungen as $bestellung) {
477 insAuftrag($bestellung);
479 } else { echo "Es liegen keine Bestellungen vor!<br>"; };
481 <!--a href='trans.php'>zurück</a-->