2 /***************************************************************
3 *Author: Holger Lindemann
4 *Copyright: (c) 2004 Lx-System
6 *eMail: info@lx-system.de
9 *ERP: Lx-Office ERP 2.4.x
10 ***************************************************************/
16 echo " <head><title>Lx-ERP Export der Shopartikel</title>";
17 echo ' <link type="text/css" REL="stylesheet" HREF="css/main.css"></link>';
21 define("ExportMode","2");
22 require_once "shoplib.php";
23 /**********************************************
24 * getAttribut($oid,$pid)
26 **********************************************/
27 function getAttribut($oid,$pid) {
28 $sql="select * from orders_products_attributes where orders_id=$oid and orders_products_id=$pid";
29 $rs=getAll("shop",$sql,"getAttribut");
31 foreach ($rs as $zeile) {
32 $txt.="\n - ".$zeile["products_options"].":".$zeile["products_options_values"];
37 /**********************************************
40 **********************************************/
41 function getBrutto($id) {
42 $sql="select * from orders_total where orders_id=$id and class='ot_total'";
43 $rs=getAll("shop",$sql,"getBrutto");
44 return $rs[0]["value"];
47 /**********************************************
50 **********************************************/
51 function getMwst($id) {
52 $sql="select * from orders_total where orders_id=$id and class='ot_tax'";
53 $rs=getAll("shop",$sql,"getMwst");
56 foreach ($rs as $zeile) {
57 $mwst+=$zeile["value"];
63 /**********************************************
64 * getSonderkosten($id,$art)
66 **********************************************/
67 function getSonderkosten($id,$art) {
68 $sql="select * from orders_total where orders_id=$id and class='".$GLOBALS["skosten"][$art]."'";
69 $rs=getAll("shop",$sql,"getSonderkosten");
70 if ($rs[0]["value"]) {
71 $kosten=round($rs[0]["value"]/(100+$GLOBALS["versand"]["TAX"])*100,2);
78 /**********************************************
79 * insBestArtikel($zeile,$transID)
81 **********************************************/
82 function insBestArtikel($ordersID,$transID) {
84 $sql="select * from orders_products where orders_id=$ordersID";
85 $rs=getAll("shop",$sql,"insBestArtikel");
87 if ($rs) foreach ($rs as $zeile) {
88 $sql="select * from parts where partnumber='".$zeile["products_model"]."'";
89 $rs2=getAll("erp",$sql,"insBestArtikel");
90 if ( $rs2[0]["id"]) {$artID=$rs2[0]["id"]; $artNr=$rs2[0]["partnumber"]; }
92 if ($zeile["products_tax"]=="19.0000") {
100 $preis=round($zeile["products_price"]/(100+$zeile["products_tax"])*100,2);
101 $text=getAttribut($ordersID,$zeile["orders_products_id"]);
102 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
103 $sql.=$transID.",".$artID.",'".$zeile["products_name"].$text."',".$zeile["products_quantity"].",".$preis.",'Stck',0,0)";
104 echo " - Artikel:[ BuNr.:$artID ArtNr:<b>$artNr</b> ".$zeile["products_name"]." ]<br>";
105 $rc=query("erp",$sql,"insBestArtikel");
106 if ($rc === -99) { $ok=false; break; };
111 function insAuftrag($data) {
112 global $ERPusr,$versand,$nachn,$minder,$paypal,$auftrnr;
113 $Zahlmethode=array("authorizenet"=>"Authorize.net","banktransfer"=>"Lastschriftverfahren","cc"=>"Kreditkarte",
114 "cod"=>"Nachnahme","eustandardtransfer"=>"EU-Standard Bank Transfer","iclear"=>"iclear Rechnungskauf",
115 "invoice"=>"Rechnung","ipayment"=>"iPayment","liberecobanktransfer"=>"Lastschriftverfahren",
116 "liberecocc"=>"Kreditkarte","moneybookers"=>"Moneybookers.com","moneyorder"=>"Scheck/Vorkasse",
117 "nochex"=>"NOCHEX","paypal"=>"PayPal","pm2checkout"=>"2CheckOut","psigate"=>"PSiGate",
118 "qenta"=>"qenta.at","secpay"=>"SECPay");
119 $brutto=getBrutto($data["orders_id"]);
120 $mwst=getMwst($data["orders_id"]);
121 $netto=$brutto-$mwst;
122 $versandK=getSonderkosten($data["orders_id"],"Versand");
123 $nachnK =getSonderkosten($data["orders_id"],"NachName");
124 $mindermK=getSonderkosten($data["orders_id"],"Minder");
125 $paypalK =getSonderkosten($data["orders_id"],"Paypal");
126 // Hier beginnt die Transaktion
127 $rc=query("erp","BEGIN WORK","insAuftrag");
128 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
130 $auftrag=$GLOBALS["preA"].getNextAnr();
132 $auftrag=$GLOBALS["preA"].$data["orders_id"];
134 $sql="select count(*) as cnt from oe where ordnumber = '$auftrag'";
135 $rs=getAll("erp",$sql,"insAuftrag 1");
136 if ($rs[0]["cnt"]>0) {
137 $auftrag=$GLOBALS["preA"].getNextAnr();
139 $newID=uniqid (rand());
140 $sql="insert into oe (notes,ordnumber,cusordnumber) values ('$newID','$auftrag','".$data["kdnr"]."')";
141 $rc=query("erp",$sql,"insAuftrag 2");
143 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
144 $rc=query("erp","ROLLBACK WORK","chkKunde");
145 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
148 $sql="select * from oe where notes = '$newID'";
149 $rs2=getAll("erp",$sql,"insAuftrag 3");
151 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
152 $rc=query("erp","ROLLBACK WORK","chkKunde");
153 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
156 $BEZAHLEN=$Zahlmethode[$data["payment_method"]]."\n";
157 if ($data["cc_type"]) {
158 $BEZAHLEN.=$data["cc_type"]."\n".$data["cc_owner"]."\n".$data["cc_number"]."\n".$data["cc_expires"]."\n";
159 } else if ($data["banktransfer_blz"]) {
160 $BEZAHLEN.="Kontoinhaber: ".$data["banktransfer_owner"]."\nBanknummer: ".$data["banktransfer_blz"];
161 $BEZAHLEN.="\nBank: ".$data["banktransfer_bankname"]."\nKontonummer: ".$data["banktransfer_number"]."\n";
163 $sql="update oe set cusordnumber=".$data["orders_id"].", transdate='".$data["date_purchased"]."', customer_id=".$data["kdnr"].", ";
164 $sql.="amount=".$brutto.", netamount=".$netto.", reqdate='".$data["date_purchased"]."', taxincluded='f', ";
165 if ($data["shipto"]>0) $sql.="shipto_id=".$data["shipto"].", ";
166 $sql.="intnotes='".$data["comments"]."',notes='".$BEZAHLEN."', curr='EUR',employee_id=".$ERPusr["ID"].", vendor_id=0 ";
167 $sql.="where id=".$rs2[0]["id"];
168 $rc=query("erp",$sql,"insAuftrag 4");
170 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
171 $rc=query("erp","ROLLBACK WORK","chkKunde");
172 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
175 echo "Auftrag:[ Buchungsnummer:".$rs2[0]["id"]." AuftrNr:<b>".$auftrag."</b> ]<br>";
176 if (!insBestArtikel($data["orders_id"],$rs2[0]["id"])) {
177 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
178 $rc=query("erp","ROLLBACK WORK","chkKunde");
179 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
183 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
184 $sql.=$rs2[0]["id"].",".$versand["ID"].",'".$versand["TXT"]."',1,".$versandK.",'Stck',0,0)";
185 $rc=query("erp",$sql,"insAuftrag 8");
186 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den Versandkosten<br>";
189 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
190 $sql.=$rs2[0]["id"].",".$nachn["ID"].",'".$nachn["TXT"]."',1,".$nachnK.",'Stck',0,0)";
191 $rc=query("erp",$sql,"insAuftrag 9");
192 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den Nachnamekosten<br>";
195 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
196 $sql.=$rs2[0]["id"].",".$minder["ID"].",'".$minder["TXT"]."',1,".$mindermK.",'Stck',0,0)";
197 $rc=query("erp",$sql,"insAuftrag 10");
198 if ($rc === -99) echo "Auftrag $auftrag : Fehler beim Mindermengenzuschlag<br>";
201 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
202 $sql.=$rs2[0]["id"].",".$paypal["ID"].",'".$paypal["TXT"]."',1,".$paypalK.",'Stck',0,0)";
203 $rc=query("erp",$sql,"insAuftrag 11");
204 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den PayPal-Kosten<br>";
206 $sql="update orders set orders_status ='3' WHERE orders_id =".$data["orders_id"];
207 $rc=query("shop",$sql,"insBestArtikel 12");
208 if ($rc === -99) echo "Bestellung im Shop nicht geschlossen";
209 $rc=query("erp","COMMIT WORK","chkKunde");
210 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
214 /**********************************************
217 **********************************************/
218 function getBestellung() {
219 $sql="select b.*,h.comments,o.*,cn.kdnr from orders o left join orders_status_history h on h.orders_id=o.orders_id ";
220 $sql.="left join banktransfer b on b.orders_id =o.orders_id left join customers_number cn on ";
221 $sql.="cn.customers_id=o.customers_id where o.orders_status=1 order by o.orders_id";
222 $rs=getAll("shop",$sql,"getBestellung");
226 /**********************************************
229 **********************************************/
230 function chkKunden() {
231 $felder=array("firstname","lastname","company","street_address","city","postcode","country");
232 foreach ($GLOBALS["bestellungen"] as $bestellung) {
233 $rc=query("erp","BEGIN WORK","chkKunden");
234 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
235 if ($bestellung["kdnr"]>0) { // Bestandskunde; kdnr == ID in customers
237 $kdnr=chkOldKd($bestellung);
238 if ($kdnr == -1) { //Kunde nicht gefunden, neu anlegen.
240 $kdnr=insNewKd($bestellung);
243 echo $msg." ".$bestellung["customers_name"]." fehlgeschlagen!<br>";
249 $kdnr=insNewKd($bestellung);
252 echo $bestellung["customers_company"]." ".$bestellung["customers_name"]." $kdnr<br>";
253 $GLOBALS["bestellungen"][$GLOBALS["gesKd"]]["kdnr"]=$kdnr;
254 $sql="delete from customers_number where customers_id=".$bestellung["customers_id"];
255 $rc=query("shop",$sql,"chkKunde");
256 $sql="insert into customers_number (customers_id,kdnr) values(".$bestellung["customers_id"].",".$kdnr.")";
257 $rc=query("shop",$sql,"chkKunde");
259 foreach($felder as $feld) {
260 if ($bestellung["delivery_$feld"]<>$bestellung["customers_$feld"]) {
261 $rc=insShData($bestellung,$kdnr);
262 if ($rc>0) $GLOBALS["bestellungen"][$GLOBALS["gesKd"]]["shipto"]=$rc;
267 if (!$kdnr || $rc === -99) {
268 echo $msg." ".$bestellung["customers_name"]." fehlgeschlagen! ($kdnr,$rc)<br>";
269 $rc=query("erp","ROLLBACK WORK","chkKunde");
270 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
272 $rc=query("erp","COMMIT WORK","chkKunde");
273 if ($rc === -99) { echo "Probleme mit Transaktion. Abbruch!"; exit(); };
274 } $GLOBALS["gesKd"]++;
279 function chkOldKd($data) {
280 $sql="select * from customer where id = ".$data["kdnr"];
281 $rs=getAll("erp",$sql,"chkKdData");
282 if (!$rs || $rs[0]["id"]<>$data["kdnr"]) { return -1; }; // Kunde nicht gefunden
283 if ($rs[0]["zipcode"]<>$data["customers_postcode"]) $set.="zipcode='".$data["customers_postcode"]."',";
284 if ($rs[0]["city"]<>$data["customers_city"]) $set.="city='".$data["customers_city"]."',";
285 if (in_array($data["customers_country"],$GLOBALS["LAND"])) {
286 if ($rs[0]["country"]<>$GLOBALS["LAND"][$data["customers_country"]]) $set.="country='".$GLOBALS["LAND"][$data["customers_country"]]."',";
288 if ($rs[0]["country"]<>$data["customers_country"]) $set.="country='".$data["customers_country"]."',";
290 if ($rs[0]["phone"]<>$data["customers_phone"])$set.="phone='".$data["customers_phone"]."',";
291 if ($rs[0]["email"]<>$data["customers_email_address"])$set.="email='".$data["customers_mail_address"]."',";
292 if ($data["customers_company"]) {
293 if ($rs[0]["name"]<>$data["customers_company"]) $set.="name='".$data["customers_company"]."',";
294 if ($rs[0]["contact"]<>$data["customers_name"]) $set.="contact='".$data["customers_name"]."',";
296 if ($rs[0]["name"]<>$data["customers_name"]) $set.="name='".$data["customers_lastname"].", ".$data["customers_firstname"]."',";
298 if ($rs[0]["street"]<>$data["customers_street_address"]) $set.="street='".$data["customers_street_address"]."',";
300 $sql="update customer set ".substr($set,0,-1)." where id=".$rs[0]["id"];
301 $rc=query("erp",$sql,"chkKdData");
305 return $data["kdnr"];
308 return $data["kdnr"];
312 /**********************************************
313 * insShData($data,$id)
315 **********************************************/
316 function insShData($data,$id) {
318 if ($data["delivery_company"]) { $set.=",'".$data["delivery_company"]."','".$data["delivery_name"]."',"; }
319 else { $set.=",'".$data["delivery_name"]."','',"; }
320 $set.="'".$data["delivery_street_address"]."',";
321 $set.="'".$data["delivery_postcode"]."',";
322 $set.="'".$data["delivery_city"]."',";
323 if (in_array($data["delivery_country"],$GLOBALS["LAND"])) {
324 $set.="'".$GLOBALS["LAND"][$data["delivery_country"]]."',";
326 $set.="'".$data["delivery_country"]."',";
328 $set.="'".$data["customers_telephone"]."',";
329 $set.="'".$data["customers_email_address"]."'";
330 $sql="insert into shipto (trans_id,shiptoname,shiptodepartment_1,shiptostreet,shiptozipcode,shiptocity,";
331 $sql.="shiptocountry,shiptophone,shiptoemail,module) values ($set,'CT')";
332 $rc=query("erp",$sql,"insShData");
333 if ($rc === -99) return false;
334 $sql="select shipto_id from shipto where trans_id = $id and module='CT' order by itime desc limit 1";
335 $rs=getAll("erp",$sql,"insKdData");
336 if ($rs[0]["shipto_id"]>0) {
337 $sid=$rs[0]["shipto_id"];
338 $sql="update customers_number set shipto = $sid where kdnr = $id";
339 $rc2=query("shop",$sql,"insShData");
341 $sql="delete from shipto where shipto_id=$sid";
342 $rc=query("shop",$sql,"insShData");
347 echo "Fehler bei abweichender Anschrift ".$data["delivery_name"];
348 $sql="delete from shipto where shipto_id=$sid";
349 $rc=query("shop",$sql,"insShData");
354 /**********************************************
357 **********************************************/
358 function insNewKd($data) {
359 $taxid=array("DE"=>0,"CH"=>2,"AU"=>1,"FR"=>1,"IT"=>1,"ES"=>1,"NL"=>1); // Muß erweitert werden
360 $taxidL=array("Germany"=>0,"Switzerland"=>2,"Austria"=>1,"France"=>1,"Italy"=>1,"Spain"=>1,"Netherlands"=>1); // Muß erweitert werden
361 $newID=uniqid(rand(time(),1));
362 //Kundennummer generieren
363 if ($GLOBALS["kdnum"]==1) { // von der ERP
364 $kdnr=$GLOBALS["preK"].getNextKnr();
365 } else { // durch Shop
366 $kdnr=$GLOBALS["preK"].$data["customers_id"];
368 $sql="select count(*) as cnt from customer where customernumber = '$kdnr'";
369 $rs=getAll("erp",$sql,"insKdData");
370 if ($rs[0]["cnt"]>0) { // Kundennummer gibt es schon, eine neue aus ERP
371 $kdnr=$GLOBALS["preK"].getNextKnr();
373 $sql="insert into customer (name,customernumber) values ('$newID','$kdnr')";
374 $rc=query("erp",$sql,"insKdData");
375 if ($rc === -99) return false;
376 $sql="select * from customer where name = '$newID'";
377 $rs=getAll("erp",$sql,"insKdData");
378 if (!$rs) return false;
379 if ($data["customers_company"]) {
380 $set.="set name='".$data["customers_company"]."',contact='".$data["customers_name"]."',";
382 $set.="set name='".$data["customers_lastname"].", ".$data["customers_firstname"]."',";
383 $set.="contact='".$data["customers_name"]."',";
385 $set.="street='".$data["customers_street_address"]."',";
386 $set.="zipcode='".$data["customers_postcode"]."',";
387 $set.="city='".$data["customers_city"]."',";
388 $set.="country='".$data["delivery_country_iso_code_2"]."',";
389 $set.="phone='".$data["customers_telephone"]."',";
390 $set.="email='".$data["customers_email_address"]."',";
391 if ($data["delivery_country_iso_code_2"]) {
392 $tid=(in_array($data["delivery_country_iso_code_2"],$taxid))?$taxid[$data["delivery_country_iso_code_2"]]:0;
394 $tid=(in_array($data["delivery_country"],$taxidL))?$taxidL[$data["delivery_country"]]:0;
397 $set.="taxzone_id=$tid,";
398 $set.="taxincluded='f' ";
399 $sql="update customer ".$set;
400 $sql.="where id=".$rs[0]["id"];
401 $rc=query("erp",$sql,"insKdData");
403 $sql="delete from customer where id=".$rs[0]["id"];
404 $rc=query("shop",$sql,"insNewKd");
406 } else { return $rs[0]["id"]; }
409 $LAND=array("Germany"=>"D","Austria"=>"A","Switzerland"=>"CH");
410 $skosten=array("Versand"=>"ot_shipping","NachName"=>"ot_cod_fee","Paypal"=>"ot_paypal","Minder"=>"ot_loworderfee");
411 $bestellungen=getBestellung();
412 $ok=count($bestellungen);
416 echo "Es liegen $ok Bestellungen vor. <br>";
418 echo $gesKd." Kunde(n), davon ".$neuKd." neue(r) Kunde(n).<br>";
419 foreach ($bestellungen as $bestellung) {
420 insAuftrag($bestellung);
422 } else { echo "Es liegen keine Bestellungen vor!<br>"; };
424 <!--a href='trans.php'>zurück</a-->