3 Funktionen für den Zugriff auf den xtc 3.04 newxtc 1.05
16 var $shopcode = 'ISO-8859-1';
17 var $erpcode = 'UTF-8';
19 var $EU = array('AT','BE','BG','CZ','DK','EE','ES','FI','FR','GB','GR','HR','HU','IE','IT','LU','LV','MT','NL','PL','PT','RO','SE','SI','SK');
20 var $Kategorien = False;
26 var $tableerp = array("partnumber"=>"products_model","description"=>"products_name","notes"=>"products_description","ean"=>"products_ean",
27 "unit"=>"products_vpe","weight"=>"products_weight","sellprice"=>"products_price","partsgroup"=>"products_partsgroup",
28 "tax"=>"products_tax","image"=>"products_image","onhand"=>"products_quantity");
29 var $custshop = array("kontakt"=>"contact","ort"=>"city","plz"=>"zipcode","land"=>"country","tel"=>"phone",
30 "fax"=>"fax","email"=>"email","beschreibung"=>"notes","strasse"=>"street","firma"=>"name",
31 "kunden_nr"=>"customer_id","anrede"=>"greeting","k_id"=>"shopid",
32 "bankname"=>"bank","blz"=>"bank_code","kontonummer"=>"account_number",
33 "iban"=>"iban","bic"=>"bic","attributwert1"=>"ustid");
34 var $ordershop = array("datum"=>"transdate","rechnungsbetrag"=>"amount","rechnungs_nr"=>"",
35 "waehrung"=>"currency","beschreibung"=>"notes",
36 "mwst"=>"mwst","versandart"=>"shipvia");
37 var $orderparts = array("products_name"=>"description","products_description"=>"longdescription","products_price"=>"sellprice",
38 "products_quantity"=>"qty","products_model"=>"partnumber",
39 "partsgroup"=>"partsgroup","products_weight"=>"weight","products_ean"=>"ean",
40 "products_tax"=>"taxrate","products_image"=>"image","products_vpe_name"=>"unit");
42 function xtc($db,$error,$dbname,$divStd,$divVerm,$minder,$nachn,$versandS,$versandV,$paypal,$mwstLX,$mwstS,$variantnr,$unit,
43 $pic=false,$nopic=false,$nopicerr=false,$nofiles=false,
44 $erpcode='UTF-8',$shopcode='ISO-8859-1') {
46 $this->error = $error;
47 $this->divStd = $divStd ;
48 $this->divVerm = $divVerm ;
49 $this->minder = $minder ;
50 $this->nachn = $nachn ;
51 $this->versandS = $versandS ;
52 $this->versandV = $versandV ;
53 $this->paypal = $paypal ;
54 $this->erpcode = $erpcode;
55 $this->mwstLX = $mwstLX;
56 $this->mwstS = $mwstS;
58 $this->VariantNr = $variantnr;
59 $this->language = $this->defaultLang();
60 $this->geozone = $this->getDefaultGZ();
62 $this->nopic = ( $nopic != '' )?$nopic:false;
63 $this->nopicerr = ( $nopicerr != '' )?true:false;
64 $this->nofiles = $nofiles;
65 if ($shopcode == 'AUTO') {
66 $sql = "SELECT TABLE_COLLATION FROM information_schema.TABLES WHERE table_schema = '$dbname' AND table_name = 'customers'";
67 $rs = $this->db->getOne($sql);
69 preg_match('/([^_]+)/',$rs['table_collation'],$hits);
71 $this->shopcode = $hits[1];
73 $this->shopcode = 'ISO-8859-1';
77 $this->shopcode = $shopcode;
80 function getDefaultGZ() {
81 $sql = "SELECT geo_zone_id FROM zones_to_geo_zones Z ";
82 $sql .= "LEFT JOIN countries CO ON Z.zone_country_id=CO.countries_id ";
83 $sql .= "LEFT JOIN configuration CF ON CO.countries_iso_code_2=CF.configuration_value ";
84 $sql .= "WHERE configuration_key = 'DEFAULT_LANGUAGE'";
85 $rs = $this->db->getOne($sql);
86 return $rs['geo_zone_id'];
88 function defaultLang() {
89 $sql = "SELECT languages_id FROM languages L LEFT JOIN configuration C ON L.code = C.configuration_value ";
90 $sql .= "WHERE C.configuration_key = 'DEFAULT_LANGUAGE'";
91 $rs = $this->db->getOne($sql);
92 return $rs['languages_id'];
94 function _toERP($txt) {
95 return mb_convert_encoding($txt,$this->erpcode,$this->shopcode);
97 function _toShop($txt) {
98 return mb_convert_encoding($txt,$this->shopcode,$this->erpcode);
100 function translateTable($data,$table) {
102 foreach ($data as $key=>$val) {
103 if ($this->{$table}[$key])
104 $newdata[$this->{$table}[$key]] = $val;
108 function getCategoryID($name) {
112 $name = $this->_toShop($name);
114 //Kategorien werden durch die ERP mit "!" getrennt
115 preg_match_all("/([^!]+)!?/",$name,$kat);
122 $sql="select D.*,C.parent_id from categories C left join categories_description D on C.categories_id=D.categories_id ";
123 $sql.="where categories_name = '%s' and ";
124 $sql.="C.parent_id=%d and language_id=".$this->language;
125 if (count($kat)>0) foreach ($kat as $wg) {
126 $sql_ = sprintf($sql,$wg,$parent);
127 $rs=$this->db->getOne($sql_);
128 if ($rs["categories_id"]) { // gefunden
129 $parent=$rs["categories_id"];
130 } else { // nicht gefunden, anlegen
131 $parent=$this->createCategory($wg,$parent);
136 function createCategory($name,$parent) {
137 echo "Kategorie: $name<br>";
138 //Kategorie nicht vorhanden, anlegen
139 $newID = uniqid(rand());
140 $sql = "INSERT INTO categories (categories_image,parent_id,date_added) VALUES ('$newID',$parent,now())";
141 $rc = $this->db->query($sql);
142 if ( !$rc ) return false;
143 $sql = "SELECT * FROM categories WHERE categories_image = '$newID'";
144 $rs = $this->db->getOne($sql);
145 $id = $rs["categories_id"];
146 $sql = "UPDATE categories SET categories_image = null WHERE categories_id=$id";
147 $rc = $this->db->query($sql);
148 if ( !$rc ) return false;
149 $rc = $this->createCategoryLang($id,$name);
152 /*******************************************
153 * createCategoryLang($id,$lang,$name)
154 * Kategorie für eine Sprache anlegen. Ist immer
155 * in der gleichen Sprache, da ERP nur eine hat.
156 *******************************************/
157 function createCategoryLang($id,$name) {
158 $sql = "INSERT INTO categories_description (categories_id,language_id,categories_name,categories_meta_title) ";
159 $sql .= "VALUES ($id,".$this->language.",'$name','$name')";
160 $rc = $this->db->query($sql);
163 function getTax($tax) {
164 $sql = "SELECT * FROM tax_rates WHERE tax_rate = $tax and tax_zone_id =".$this->geozone;
165 $rs = $this->db->getOne($sql);
166 return $rs['tax_class_id'];
168 function getVPE($vpe) {
169 $sql = "SELECT products_vpe_id FROM products_vpe WHERE products_vpe_name = '$vpe' AND language_id = ".$this->language;
170 $rs = $this->db->getOne($sql);
171 return $rs['products_vpe_id'];
173 function saveArtikel($data,$lang) {
174 $values = $this->translateTable($data,"tableerp"); //$this->tableerp);
175 if ($this->mwstLX) { //ERP-Bruttopreis
176 $values['products_price'] = round($values['products_price'] / (1 + $values["products_tax"]),2);
178 $values["products_name"] = $this->_toShop($values["products_name"]);
179 $values["products_description"] = $this->_toShop($values["products_description"]);
180 $values["products_tax_class_id"] = $this->getTax($values["products_tax"]*100);
181 $values["categories_id"] = $this->getCategoryID($values["products_partsgroup"]);
182 $values["products_vpe"] = $this->getVPE($values["products_vpe"]);
183 $values["products_id"] = $this->checkArtikelOK($values["products_model"]);
184 if ($values["products_id"]>0) {
185 $rc = $this->updateArtikel($values);
187 $rc = $this->insertArtikel($values);
191 function checkArtikelOK($nr) {
192 $sql = "SELECT * FROM products WHERE products_model = '".$nr."'";
193 $rs = $this->db->getOne($sql);
194 return $rs["products_id"];
196 function insertArtikel($data) {
197 $newID = uniqid(rand());
199 $sql = "INSERT INTO products (products_model,products_image,products_status) VALUES ('".$data["products_model"]."','$newID',1)";
200 $rc = $this->db->query($sql);
201 $sql = "SELECT * FROM products WHERE products_image='$newID'";
202 $rs = $this->db->getOne($sql);
203 $sql = "INSERT INTO products_description (products_id,products_name,language_id) VALUES (".$rs['products_id'].",'".$data['products_name']."',".$this->language.")";
204 $rc = $this->db->query($sql);
205 if ($rs['products_image'] == $newID) {
206 $data["products_id"] = $rs["products_id"];
207 $statement = "INSERT INTO products_to_categories (products_id,categories_id) VALUES (?,?)";
208 $values = array($rs["products_id"],$data["categories_id"]);
209 $rc = $this->db->insert($statement,$values);
211 echo $data['products_model'].' konnte nicht zur Gruppe '.$data['categories_id'].' zugefügt werden.';
212 $this->error->write('xtc',$data['products_model'].' konnte nicht zur Gruppe '.$data['categories_id'].' zugefügt werden.');
213 $this->db->Rollback();
218 $rc = $this->updateArtikel($data,true);
219 } else { return false; }
222 function updateArtikel($values,$insert=false) {
223 if ($this->mwstLX) $values['products_price'] = round($values['products_price'] / (100+$values['products_tax'])*100,2);
224 $sql = "UPDATE products SET products_price = :products_price, products_weight = :products_weight, ";
225 if ( !$this->nofiles || $insert) {
226 //vorhandene Bilder übertragen
227 $sql .= "products_image = :products_image, ";
228 if ( $values['products_image'] != '' ) {
229 preg_match("/(.+)\.(jpg|png|jpeg|gif)/i",$values['products_image'],$tmp);
230 $sql .= "products_image = :products_image, ";
231 if ( count($tmp) == 3 ) {
232 if ( $this->pic && $this->pic->copyImage($values['products_id'],$values['products_image'],$tmp[2]) ) {
233 $values['products_image'] = $values['products_id']."_0.".$tmp[2];
234 } else if ( $this->nopic ){
235 $values['products_image'] = $this->nopic;
238 } else if ( $this->nopic && !$this->nopicerr ){
239 $values['products_image'] = $this->nopic;
242 $sql .= "products_tax_class_id = :products_tax_class_id, products_vpe = :products_vpe, products_ean = :products_ean ";
243 //Kein Lagerbestand übergeben, also nichts ändern
244 if ($values['products_quantity'] != '') $sql .= ",products_quantity = :products_quantity ";
245 $sql .= "WHERE products_id = :products_id ";
246 $rc = $this->db->update($sql,$values);
247 $sql = "UPDATE products_description SET products_name = :products_name, products_description = :products_description ";
248 #products_short_description products_keywords <== aus CVars
249 $sql .= "WHERE products_id = :products_id AND language_id = ".$this->language;
250 $rc = $this->db->update($sql,$values);
252 return $values["products_id"];
257 function getVersand($class,$orderid,$country) {
258 $tmp = explode("_",$class);
259 if ( $tmp[1] == "" ) $tmp[1] = "dp";
260 $sql = "SELECT geo_zone_id FROM zones_to_geo_zones WHERE zone_country_id = (";
261 $sql .= "SELECT countries_id FROM countries WHERE countries_name = '$country')";
262 $rs = $this->db->getOne($sql);
263 $sql = "SELECT tax_rate FROM tax_rates WHERE tax_zone_id = ".$rs['geo_zone_id']." AND tax_class_id = (";
264 $sql .= "SELECT configuration_value FROM configuration WHERE configuration_key = 'MODULE_SHIPPING_".strtoupper($tmp[1])."_TAX_CLASS') ";
265 $rs = $this->db->getOne($sql);
266 $preis = $this->getTotal($orderid,'ot_shipping');
269 $preis = round($preis/(100+$rs['tax_rate'])*100,2);
271 if ($rs['tax_rate'] == $this->versandV['TAX']) {
272 $artikel = array("partnumber"=>$this->versandV['NR'],"description"=>$this->versandV['TXT'],
273 "qty"=>1,"unit"=>$this->versandV['Unit'],"sellprice"=>$preis);
275 $artikel = array("partnumber"=>$this->versandS['NR'],"description"=>$this->versandS['TXT'],"taxrate"=>$this->versandS['TAX'],
276 "qty"=>1,"unit"=>$this->versandS['Unit'],"sellprice"=>$preis);
282 function getKosten($kosten,$orderid,$country) {
283 $sql = "SELECT geo_zone_id FROM zones_to_geo_zones WHERE zone_country_id = (";
284 $sql .= "SELECT countries_id FROM countries WHERE countries_name = '$country')";
285 $rs = $this->db->getOne($sql);
286 $sql = "SELECT tax_rate FROM tax_rates WHERE tax_zone_id = ".$rs['geo_zone_id']." AND tax_class_id = (";
287 $sql .= "SELECT configuration_value FROM configuration WHERE configuration_key = 'MODULE_SHIPPING_".strtoupper($kosten)."_TAX_CLASS') ";
288 $rs = $this->db->getOne($sql);
289 $preis = $this->getTotal($orderid,'ot_'.$kosten);
291 //Shop muß immer Nettopreise liefern.
292 if ( $this->mwstS ) {
293 $preis = round($preis / (100+$rs['tax_rate'])*100,2);
295 if ( $kosten == 'cod_fee' ) { $erp = $this->nachn; }
296 else if ( $kosten == 'loworderfee' ) { $erp = $this->minder; }
297 else if ( $kosten == 'paypal_fee' ) { $erp = $this->paypal; }
298 $artikel = array("partnumber"=>$erp['NR'],"description"=>$erp['TXT'],
299 "qty"=>1,"unit"=>$erp['Unit'],"sellprice"=>$preis);
304 function getTotal($orderid,$type) {
305 $sql = "SELECT value FROM orders_total WHERE orders_id = $orderid AND class = '$type'";
306 $rs = $this->db->getOne($sql);
309 function getBestellung($employee_id) {
310 $sql = "SELECT * FROM orders WHERE orders_status=1 order by orders_id limit 1";
311 $rs=$this->db->getAll($sql);
312 if (!$rs) return array();
314 foreach ($rs as $row) {
315 $artikel = $this->getBestellArtikel($row["orders_id"]);
316 $versand = $this->getVersand($row["shipping_class"],$row['orders_id'],$row['delivery_country']);
317 if ($versand) $artikel[] = $versand;
318 $nachn = $this->getKosten('cod_fee',$row['orders_id'],$row['delivery_country']);
319 if ($nachn) $artikel[] = $nachn;
320 $minder = $this->getKosten('loworderfee',$row['orders_id'],$row['delivery_country']);
321 if ($minder) $artikel[] = $minder;
322 $paypal = $this->getKosten('paypal',$row['orders_id'],$row['delivery_country']);
323 if ($paypal) $artikel[] = $paypal;
324 if ($row["delivery_country_iso_code_2"] == "DE") {
325 $rowdata['taxzone_id'] = 0;
326 } else if (in_array($this->EU,$row["delivery_country_iso_code_2"])) {
327 if (preg_match('/^[^0-9]{2,3}[ 0-9]+$/',$row["customers_vat_id"])) {
328 $rowdata['taxzone_id'] = 1;
330 $rowdata['taxzone_id'] = 2;
333 $rowdata['taxzone_id'] = 3;
335 if ($rowdata["cc_number"]) {
336 $rowdata["kreditkarte"] = $row['cc_type']."\n";
337 $rowdata["kreditkarte"] = $row['cc_number']." ID:".$row['cc_cvv']."\n";
338 $rowdata["kreditkarte"] = $row['cc_expires']."\n";
339 $rowdata["kreditkarte"] = $row['cc_owner']."\n";
341 $rowdata['cusordnumber'] = $row['orders_id'];
342 $rowdata["versandart"] = $this->_toERP($row["shipping_method"]);
343 $rowdata["notes"] = $this->_toERP($row["comments"]);
344 $rowdata["curr"] = $this->_toERP($row["currency"]);
345 $rowdata["transdate"] = substr($row["date_purchased"],0,10);
346 $rowdata["shipvia"] = $this->_toERP($row["shipping_method"]);
347 $rowdata["employee_id"] = $employee_id;
348 $rowdata["parts"] = $artikel;
349 $rowdata["mwst"] = $this->getTotal($row['orders_id'],'ot_tax');
350 $rowdata["amount"]= $this->getTotal($row['orders_id'],'ot_total');
351 $rowdata["netamount"] = $this->getTotal($row['orders_id'],'ot_subtotal');
352 if ($row['delivery_company']) {
353 $delivery['name'] = $row['delivery_company'];
354 $delivery['contact'] = $row['delivery_firstname']." ".$row['delivery_lastname'];
356 $delivery['name'] = $row['delivery_lastname'].', '.$row['delivery_firstname'];
358 $delivery['street'] = $row['delivery_street_address'];
359 $delivery['city'] = $row['delivery_city'];
360 $delivery['zipcode'] = $row['delivery_postcode'];
361 $delivery['country'] = $row['delivery_country'];
362 $delivery['phone'] = $row['delivery_telephone'];
363 $delivery['email'] = $row['delivery_email_address'];
364 if ($row['customers_company']) {
365 $customer['name'] = $row['customers_company'];
366 $customer['contact'] = $row['customers_firstname']." ".$row['customers_lastname'];
367 $customer['greeting'] = 'Firma';
369 $customer['name'] = $row['customers_lastname'].', '.$row['customers_firstname'];
370 $customer['greeting'] = ($row['customers_gender'] == 'm')?'Herr':'Frau';
372 $customer['shopid'] = $row['customers_id'];
373 $customer['street'] = $row['customers_street_address'];
374 $customer['city'] = $row['customers_city'];
375 $customer['zipcode'] = $row['customers_postcode'];
376 $customer['country'] = $row['customers_country'];
377 $customer['phone'] = $row['customers_telephone'];
378 $customer['email'] = $row['customers_email_address'];
379 $customer['contact'] = $row['customers_name'];
380 $customer['customer_id'] = $row['customers_cid'];
381 $rowdata['customer'] = $customer;
382 if ( $customer != $delivery ) $rowdata['delivery'] = $delivery;
383 $rowdata["customer"]['customernumber'] = $row['customers_cid'];
388 function getBestellArtikel($bestellung) {
389 if (!$this->kategorien) $this->getKategorien();
390 $sql = "SELECT OP.*,D.products_description,PC.categories_id as katid,PV.products_vpe_name,P.products_ean,P.products_image,P.products_weight ";
391 $sql .= "FROM orders_products OP ";
392 $sql .= "LEFT JOIN products_description D on OP.products_id=D.products_id ";
393 $sql .= "LEFT JOIN products P on OP.products_id=P.products_id ";
394 $sql .= "LEFT JOIN products_to_categories PC on OP.products_id = PC.products_id ";
395 $sql .= "LEFT JOIN products_vpe PV ON PV.products_vpe_id = P.products_vpe ";
396 $sql .= "WHERE (PV.language_id = ".$this->language." OR PV.language_id is Null) AND D.language_id = ".$this->language;
397 $sql .= " AND OP.orders_id = $bestellung";
398 $rs=$this->db->getAll($sql);
400 $this->error->write("xtc","Die Artikel der Bestellung $bestellung konnte nicht gelesen werden");
403 foreach ($rs as $row) {
404 $row['partsgroup'] = $this->_toERP($this->Kategorien[$row['katid']]);
405 $variant = $this->getVariant($row['orders_id'],$row['orders_products_id']);
406 if ( $this->variantnr == 1 ) {
407 $row['products_name'] = $this->_toERP($row['products_name'].$variant);
408 $row['products_description'] = $this->_toERP($row['products_description']);
410 $row['products_name'] = $this->_toERP($row['products_name']);
411 $row['products_description'] = $this->_toERP($variant.$row['products_description']);
413 //Shop muß immer Nettopreise liefern.
415 $row['products_price'] = round($row['products_price'] / (100+$row['products_tax'])*100,2);
417 $row['products_vpe_name'] = ( $row['products_vpe_name'] )?$this->_toERP($row['products_vpe_name']):$this->unit;
418 $row['partsgroup'] = $this->_toERP($this->Kategorien[$row['katid']]);
419 $data[] = $this->translateTable($row,"orderparts");
423 function getVariant($oid,$aid) {
424 $sql = "SELECT * FROM orders_products_attributes WHERE orders_id = $oid AND orders_products_id = $aid";
425 $rs = $this->db->getAll($sql);
426 if ( $this->variantnr == 1 ) { $start = "\n"; $end = ""; }
427 else { $start = ""; $end = "\n"; }
429 if ($rs) foreach ($rs as $row) {
430 $txt .= $start.sprintf('%s: %s %s%0.2f',$row['products_options'],$row['products_options_values'],$row['price_prefix'],$row['options_values_price']).$end;
434 function setAbgeholt($bestellung) {
435 $sql = "UPDATE orders SET orders_status ='3' WHERE orders_id = $bestellung"; // in ($bestellungen)";
436 $rc = $this->db->query($sql);
438 $this->error->write("xtc","Die Bestellung $bestellung konnten nicht als abgeholt markiert werden");
444 function setKundenNr($id,$nr) {
445 $sql = "UPDATE customers SET customers_cid = '$nr' WHERE customers_id = $id";
446 $rc = $this->db->query($sql);
448 $this->error->write("xtc","Die Kundennummer $nr konnte nicht dem Kunden $id zugeordnet werden");
454 function getAllArtikel() {
455 if (!$this->Kategorien) $this->getKategorien();
456 $sql = "SELECT P.*,D.*,PC.categories_id as katid,T.tax_rate as products_tax,PV.products_vpe_name FROM products P ";
457 $sql .= "LEFT JOIN products_description D on P.products_id=D.products_id ";
458 $sql .= "LEFT JOIN products_to_categories PC on P.products_id = PC.products_id ";
459 $sql .= "LEFT JOIN tax_rates T ON T.tax_class_id = P.products_tax_class_id ";
460 $sql .= "LEFT JOIN products_vpe PV ON PV.products_vpe_id = P.products_vpe ";
461 $sql .= "WHERE T.tax_zone_id = ".$this->tax_zone." AND ";
462 $sql .= "PV.language_id = ".$this->language." AND D.language_id = ".$this->language;
463 $rs = $this->db->getAll($sql);
464 if ($rs) foreach ($rs as $row) {
465 $row['partsgroup'] = $this->_toERP($this->Kategorien[$row['katid']]);
466 $row['name'] = $this->_toERP($row['products_name']);
467 $row['beschreibung'] = $this->_toERP($row['products_description']);
468 //if (!$this->mwstLX) $row['products_price'] = round(($row['products_price'] / (100 + $row['mwst_satz']) * 100),$this->dezimal);
469 $data[] = $this->translateTable($row,"orderparts");
473 function getKategorien() {
474 $sql = "SELECT C.categories_id,D.categories_name,C.parent_id ";
475 $sql .= "FROM categories C LEFT JOIN categories_description D ON C.categories_id=D.categories_id ";
476 $sql .= "WHERE C.parent_id >= 0 order by C.parent_id AND D.language_id = ".$this->language;
477 $rs = $this->db->getAll($sql);
479 foreach($rs as $row) { $this->katrs[$row['categories_id']] = $row;};
480 foreach($this->katrs as $row) {
481 if ($row['parent_id'] == '0') {
482 $name = $row['categories_name'];
484 $name = $this->mkKategorien($row['categories_id'],'');
486 $this->Kategorien[$row['categories_id']] = $name;
490 function mkKategorien($id,$name) {
491 if ($this->katrs[$id]['parent_id'] == '0') {
493 return $this->katrs[$id]['categories_name'].'!'.$name;
495 return $this->katrs[$id]['categories_name']."#";
499 $name = $this->katrs[$id]['categories_name'];
501 $name = $this->katrs[$id]['categories_name'].'!'.$name;
503 $name = $this->mkKategorien($this->katrs[$id]['parent_id'],$name);