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 require_once "shoplib.php";
24 /**********************************************
25 * getAttribut($oid,$pid)
27 **********************************************/
28 function getAttribut($oid,$pid) {
29 $sql="select * from ".PREFIX."orders_products_attributes where orders_id=$oid and orders_products_id=$pid";
30 $rs=getAll("shop",$sql,"getAttribut");
32 foreach ($rs as $zeile) {
33 $txt.="\n - ".$zeile["products_options"].":".$zeile["products_options_values"];
38 /**********************************************
41 **********************************************/
42 function getBrutto($id) {
43 $sql="select * from ".PREFIX."orders_total where orders_id=$id and class='ot_total'";
44 $rs=getAll("shop",$sql,"getBrutto");
46 return $rs[0]["value"];
52 /**********************************************
55 **********************************************/
56 function getMwst($id) {
57 $sql="select * from ".PREFIX."orders_total where orders_id=$id and class='ot_tax'";
58 $rs=getAll("shop",$sql,"getMwst");
61 foreach ($rs as $zeile) {
62 $mwst+=$zeile["value"];
68 /**********************************************
69 * getSonderkosten($id,$art)
71 **********************************************/
72 function getSonderkosten($id,$art) {
73 $sql="select * from ".PREFIX."orders_total where orders_id=$id and class='".$GLOBALS["skosten"][$art]."'";
74 $rs=getAll("shop",$sql,"getSonderkosten");
75 if ($rs[0]["value"]) {
76 $kosten=round($rs[0]["value"]/(100+$GLOBALS["versand"]["TAX"])*100,2);
83 /**********************************************
84 * insBestArtikel($zeile,$transID)
86 **********************************************/
87 function insBestArtikel($ordersID,$transID) {
89 $sql="select * from ".PREFIX."orders_products where orders_id=$ordersID";
90 $rs=getAll("shop",$sql,"insBestArtikel");
92 if ($rs) foreach ($rs as $zeile) {
93 $sql="select * from parts where partnumber='".$zeile["products_model"]."'";
94 $rs2=getAll("erp",$sql,"insBestArtikel");
95 if ( $rs2[0]["id"]) {$artID=$rs2[0]["id"]; $artNr=$rs2[0]["partnumber"]; }
97 if ($zeile["products_tax"]=="7.0000") {
105 $preis=round($zeile["products_price"]/(100+$zeile["products_tax"])*100,2);
106 $text=getAttribut($ordersID,$zeile["orders_products_id"]);
107 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
108 $sql.=$transID.",".$artID.",'".$zeile["products_name"].$text."',".$zeile["products_quantity"].",".$preis.",'Stck',0,0)";
109 echo " - Artikel:[ BuNr.:$artID ArtNr:<b>$artNr</b> ".$zeile["products_name"]." ]<br>";
110 $rc=query("erp",$sql,"insBestArtikel");
111 if ($rc === -99) { $ok=false; break; };
116 function insAuftrag($data) {
117 global $ERPusr,$versand,$nachn,$minder,$paypal,$auftrnr;
118 $Zahlmethode=array("authorizenet"=>"Authorize.net","banktransfer"=>"Lastschriftverfahren","cc"=>"Kreditkarte",
119 "cod"=>"Nachnahme","eustandardtransfer"=>"EU-Standard Bank Transfer","iclear"=>"iclear Rechnungskauf",
120 "invoice"=>"Rechnung","ipayment"=>"iPayment","liberecobanktransfer"=>"Lastschriftverfahren",
121 "liberecocc"=>"Kreditkarte","moneybookers"=>"Moneybookers.com","moneyorder"=>"Scheck/Vorkasse",
122 "nochex"=>"NOCHEX","paypal"=>"PayPal","pm2checkout"=>"2CheckOut","psigate"=>"PSiGate",
123 "qenta"=>"qenta.at","secpay"=>"SECPay");
124 $brutto=getBrutto($data["orders_id"]);
125 $mwst=getMwst($data["orders_id"]);
126 $netto=$brutto-$mwst;
127 $versandK=getSonderkosten($data["orders_id"],"Versand");
128 $nachnK =getSonderkosten($data["orders_id"],"NachName");
129 $mindermK=getSonderkosten($data["orders_id"],"Minder");
130 $paypalK =getSonderkosten($data["orders_id"],"Paypal");
131 // Hier beginnt die Transaktion
132 $rc=query("erp","BEGIN","chkKunde");
133 if ($rc === -99) { echo "Probleme mit Datenbank, Abbruch!"; exit(); };
135 $auftrag=$GLOBALS["preA"].getNextAnr();
137 $auftrag=$GLOBALS["preA"].$data["orders_id"];
139 $sql="select count(*) as cnt from oe where ordnumber = '$auftrag'";
140 $rs=getAll("erp",$sql,"insAuftrag");
141 if ($rs[0]["cnt"]>0) {
142 $auftrag=$GLOBALS["preA"].getNextAnr();
144 $newID=uniqid (rand());
145 $sql="insert into oe (notes,ordnumber,cusordnumber) values ('$newID','$auftrag','".$data["kdnr"]."')";
146 $rc=query("erp",$sql,"insAuftrag");
147 $sql="select * from oe where notes = '$newID'";
148 $rs2=getAll("erp",$sql,"insAuftrag");
149 if ($data["cc_type"]) {
150 $BEZAHLEN.=$data["cc_type"]."\n".$data["cc_owner"]."\n".$data["cc_number"]."\n".$data["cc_expires"]."\n";
152 $BEZAHLEN=$Zahlmethode[$data["payment_method"]]."\nKontoinhaber: ";
153 $BEZAHLEN.=$data["banktransfer_owner"]."\nBanknummer: ".$data["banktransfer_blz"];
154 $BEZAHLEN.="\nKontonummer: ".$data["banktransfer_number"]."\nBank: ".$data["banktransfer_bankname"]."\n";
156 $sql="update oe set cusordnumber=".$data["orders_id"].", transdate='".$data["date_purchased"]."', customer_id=".$data["kdnr"].", ";
157 $sql.="amount=".$brutto.", netamount=".$netto.", reqdate='".$data["date_purchased"]."', taxincluded='f', ";
158 if ($data["shipto"]>0) $sql.="shipto_id=".$data["shipto"].", ";
159 $sql.="intnotes='".$data["comments"]."',notes='".$BEZAHLEN."', curr='EUR',employee_id=".$ERPusr["ID"].", vendor_id=0 ";
160 $sql.="where id=".$rs2[0]["id"];
161 $rc=query("erp",$sql,"insAuftrag");
163 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
164 $rc=query("erp","ROLLBACK","chkKunde");
167 echo "Auftrag:[ Buchungsnummer:".$rs2[0]["id"]." AuftrNr:<b>".$auftrag."</b> ]<br>";
168 if (!insBestArtikel($data["orders_id"],$rs2[0]["id"])) {
169 echo "Auftrag ".$data["orders_id"]." konnte nicht angelegt werden.<br>";
170 $rc=query("erp","ROLLBACK","chkKunde");
174 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
175 $sql.=$rs2[0]["id"].",".$versand["ID"].",'".$versand["TXT"]."',1,".$versandK.",'mal',0,0)";
176 $rc=query("erp",$sql,"insAuftrag");
177 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den Versandkosten<br>";
180 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
181 $sql.=$rs2[0]["id"].",".$nachn["ID"].",'".$nachn["TXT"]."',1,".$nachnK.",'mal',0,0)";
182 $rc=query("erp",$sql,"insAuftrag");
183 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den Nachnamekosten<br>";
186 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
187 $sql.=$rs2[0]["id"].",".$minder["ID"].",'".$minder["TXT"]."',1,".$mindermK.",'mal',0,0)";
188 $rc=query("erp",$sql,"insAuftrag");
189 if ($rc === -99) echo "Auftrag $auftrag : Fehler beim Mindermengenzuschlag<br>";
192 $sql="insert into orderitems (trans_id, parts_id, description, qty, sellprice, unit, ship, discount) values (";
193 $sql.=$rs2[0]["id"].",".$paypal["ID"].",'".$paypal["TXT"]."',1,".$paypalK.",'mal',0,0)";
194 $rc=query("erp",$sql,"insAuftrag");
195 if ($rc === -99) echo "Auftrag $auftrag : Fehler bei den PayPal-Kosten<br>";
197 $rc=query("erp","COMMIT","Auftrag");
199 echo "Probleme mit Datenbank, Abbruch!";
202 $sql="update ".PREFIX."orders set orders_status ='3' WHERE orders_id =".$data["orders_id"];
203 $rc=query("shop",$sql,"insBestArtikel");
207 /**********************************************
210 **********************************************/
211 function getBestellung() {
212 $sql="select b.*,h.comments,o.*,cn.kdnr from ".PREFIX."orders o left join ".PREFIX."orders_status_history h on h.orders_id=o.orders_id ";
213 $sql.="left join ".PREFIX."banktransfer b on b.orders_id =o.orders_id left join ".PREFIX."customers_number cn on ";
214 $sql.="cn.customers_id=o.customers_id where o.orders_status=1 order by o.orders_id";
215 $rs=getAll("shop",$sql,"getBestellung");
219 /**********************************************
222 **********************************************/
223 function chkKunden() {
224 $felder=array("firstname","lastname","company","street_address","city","postcode","country");
225 foreach ($GLOBALS["bestellungen"] as $bestellung) {
226 $rc=query("erp","BEGIN","chkKunde");
227 if ($rc === -99) { echo "Probleme mit Datenbank, Abbruch!"; exit(); };
228 if ($bestellung["kdnr"]>0) {
230 $kdnr=chkOldKd($bestellung);
231 if ($kdnr == -1) { //Kunde nicht gefunden, neu anlegen.
233 $kdnr=insNewKd($bestellung);
238 $kdnr=insNewKd($bestellung);
241 echo $bestellung["customers_company"]." ".$bestellung["customers_name"]." $kdnr<br>";
242 $GLOBALS["bestellungen"][$GLOBALS["gesKd"]]["kdnr"]=$kdnr;
243 $sql="delete from ".PREFIX."customers_number where customers_id=".$bestellung["customers_id"];
244 $rc=query("shop",$sql,"chkKunde");
245 $sql="insert into ".PREFIX."customers_number (customers_id,kdnr) values(".$bestellung["customers_id"].",".$kdnr.")";
246 $rc=query("shop",$sql,"chkKunde");
248 foreach($felder as $feld) {
249 if ($bestellung["delivery_$feld"]<>$bestellung["customers_$feld"]) {
250 $rc=insShData($bestellung,$kdnr);
251 if ($rc>0) $GLOBALS["bestellungen"][$GLOBALS["gesKd"]]["shipto"]=$rc;
256 if (!$kdnr || $rc === -99) {
257 $rc=query("erp","ROLLBACK","chkKunde");
258 echo $msg." ".$bestellung["customers_name"]." fehlgeschlagen!<br>";
261 $rc=query("erp","COMMIT","chkKunde");
268 function chkOldKd($data) {
269 $sql="select * from customer where id = ".$data["kdnr"];
270 $rs=getAll("erp",$sql,"chkKdData");
271 if ($rs[0]["id"]<>$data["kdnr"]) { return -1; }; // Kunde nicht gefunden
272 if ($rs[0]["zipcode"]<>$data["customers_postcode"]) $set.="zipcode='".$data["customers_postcode"]."',";
273 if ($rs[0]["city"]<>$data["customers_city"]) $set.="city='".$data["customers_city"]."',";
274 if ($rs[0]["country"]<>$GLOBALS["LAND"][$data["customers_country"]]) $set.="land='".$GLOBALS["LAND"][$data["customers_country"]]."',";
275 if ($rs[0]["phone"]<>$data["customers_phone"])$set.="phone='".$data["customers_phone"]."',";
276 if ($rs[0]["email"]<>$data["customers_email_address"])$set.="email='".$data["customers_mail_address"]."',";
277 if ($data["customers_company"]) {
278 if ($rs[0]["name"]<>$data["customers_company"]) $set.="set name='".$data["customers_company"]."',";
279 if ($rs[0]["department_1"]<>$data["customers_name"]) $set.="department_1='".$data["customers_name"]."',";
281 if ($rs[0]["name"]<>$data["customers_name"]) $set.="name='".$data["customers_name"]."',";
283 if ($rs[0]["street"]<>$data["customers_street_address"]) $set.="street='".$data["customers_street_address"]."',";
285 $sql="update customer set ".substr($set,0,-1)." where id=".$rs[0]["id"];
286 $rc=query("erp",$sql,"chkKdData");
290 return $data["kdnr"];
293 return $data["kdnr"];
297 /**********************************************
298 * insShData($data,$id)
300 **********************************************/
301 function insShData($data,$id) {
303 if ($data["delivery_company"]) { $set.=",'".$data["delivery_company"]."','".$data["delivery_name"]."',"; }
304 else { $set.=",'".$data["delivery_name"]."','',"; }
305 $set.="'".$data["delivery_street_address"]."',";
306 $set.="'".$data["delivery_postcode"]."',";
307 $set.="'".$data["delivery_city"]."',";
308 $set.="'".$data["delivery_country"]."',";
309 $set.="'".$data["customers_telephone"]."',";
310 $set.="'".$data["customers_email_address"]."'";
311 $sql="insert into shipto (trans_id,shiptoname,shiptodepartment_1,shiptostreet,shiptozipcode,shiptocity,";
312 $sql.="shiptocountry,shiptophone,shiptoemail,module) values ($set,'CT')";
313 $rc=query("erp",$sql,"insShData");
314 if ($rc === -99) return false;
315 $sql="select shipto_id from shipto where trans_id = $id and module='CT' order by itime desc limit 1";
316 $rs=getAll("erp",$sql,"insKdData");
317 if ($rs[0]["shipto_id"]>0) {
318 $sid=$rs[0]["shipto_id"];
319 $sql="update ".PREFIX."customers_number set shipto = $sid where kdnr = $id";
320 $rc2=query("shop",$sql,"insShData");
325 echo "Fehler bei abweichender Anschrift ".$data["delivery_name"];
334 /**********************************************
337 **********************************************/
338 function insNewKd($data) {
339 $taxid=array("DE"=>0,"CH"=>2,"AU"=>1,"FR"=>1,"IT"=>1,"ES"=>1,"NL"=>1); // Muß erweitert werden
340 $newID=uniqid(rand(time(),1));
341 //Kundennummer generieren
342 if ($GLOBALS["kdnum"]==1) { // von der ERP
343 $kdnr=$GLOBALS["preK"].getNextKnr();
344 } else { // durch Shop
345 $kdnr=$GLOBALS["preK"].$data["customers_id"];
347 $sql="select count(*) as cnt from customer where customernumber = '$kdnr'";
348 $rs=getAll("erp",$sql,"insKdData");
349 if ($rs[0]["cnt"]>0) { // Kundennummer gibt es schon, eine neue aus ERP
350 $kdnr=$GLOBALS["preK"].getNextKnr();
352 $sql="insert into customer (name,customernumber) values ('$newID','$kdnr')";
353 $rc=query("erp",$sql,"insKdData");
354 if ($rc === -99) return false;
355 $sql="select * from customer where name = '$newID'";
356 $rs=getAll("erp",$sql,"insKdData");
357 if (!$rs) return false;
358 if ($data["customers_company"]) {
359 $set.="set name='".$data["customers_company"]."',contact='".$data["customers_name"]."',";
361 $set.="set name='".$data["customers_lastname"].", ".$data["customers_firstname"]."',";
363 $set.="street='".$data["customers_street_address"]."',";
364 $set.="zipcode='".$data["customers_postcode"]."',";
365 $set.="city='".$data["customers_city"]."',";
366 $set.="country='".$data["billing_country_iso_code_2"]."',";
367 $set.="phone='".$data["customers_telephone"]."',";
368 $set.="email='".$data["customers_email_address"]."',";
369 $tid=(in_array($data["billing_country_iso_code_2"],$taxid))?$taxid[$data["billing_country_iso_code_2"]]:3;
370 $set.="taxzone_id=$tid,";
371 $set.="taxincluded='f' ";
372 $sql="update customer ".$set;
373 $sql.="where id=".$rs[0]["id"];
374 $rc=query("erp",$sql,"insKdData");
375 if ($rc === -99) { return false; }
376 else { return $rs[0]["id"]; }
379 $LAND=array("Germany"=>"D","Austria"=>"A","Switzerland"=>"CH");
380 $skosten=array("Versand"=>"ot_shipping","NachName"=>"ot_cod_fee","Paypal"=>"ot_cod_fee","Minder"=>"ot_loworderfee");
381 $bestellungen=getBestellung();
382 $ok=count($bestellungen);
386 echo "Es liegen $ok Bestellungen vor. <br>";
388 echo $gesKd." Kunde(n), davon ".$neuKd." neue(r) Kunde(n).<br>";
389 foreach ($bestellungen as $bestellung) {
390 $ok=insAuftrag($bestellung);
392 } else { echo "Es liegen keine Bestellungen vor!<br>"; };
394 <!--a href='trans.php'>zurück</a-->