3 Funktionen für den Zugriff auf den Peppershop
14 var $treuhand = false;
16 var $shopcode = 'ISO-8859-1';
17 var $erpcode = 'UTF-8';
18 var $VariantNr = true;
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;
23 var $tableerp = array("partnumber"=>"artikel_nr","description"=>"name","notes"=>"beschreibung",
24 "unit"=>"anzahl_einheit","weight"=>"gewicht","sellprice"=>"preis",
25 "tax"=>"mwst_satz","image"=>"bild_gross","onhand"=>"lagerbestand");
26 var $tableshop = array("datum"=>"transdate","rechnungsbetrag"=>"amount","nettobetrag"=>"netamount","waehrung"=>"curr",
27 "anmerkung"=>"notes","mwst"=>"mwst","bestellungs_id"=>"cusordnumber","bezahlungsart"=>"bezahlung",
28 "kreditkarte"=>"kreditkarte","versandart"=>"shipvia");
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("artikelname"=>"description","name"=>"description","preis"=>"sellprice","anzahl"=>"qty","artikel_nr"=>"partnumber",
38 "partsgroup"=>"partsgroup","beschreibung"=>"longdescription","gewicht"=>"weight","shoppreis"=>"shoppreis",
39 "mwst_satz"=>"taxrate","bild_gross"=>"image","anzahl_einheit"=>"unit","lagerbestand"=>"onhand");
42 function pepper($db,$error,$dbname,
43 $divStd,$divVerm,$minder,$nachn,$versandS,$versandV,$paypal,$treuhand,
44 $mwstLX,$mwstS,$variantnr,$pic=false,$nopic=false,$nopicerr=false,$nofiles=false,
45 $erpcode='UTF-8',$shopcode='ISO-8859-1') {
47 $this->error = $error;
48 $this->divStd = $divStd ;
49 $this->divVerm = $divVerm ;
50 $this->minder = $minder ;
51 $this->nachn = $nachn ;
52 $this->versandS = $versandS ;
53 $this->versandV = $versandV ;
54 $this->paypal = $paypal ;
55 $this->treuhand = $treuhand;
56 $this->erpcode = $erpcode;
57 $this->mwstLX = $mwstLX;
58 $this->mwstS = $mwstS;
59 $this->VariantNr = ($variantnr==1)?true:false;
61 $this->nopic = ( $nopic != '' )?$nopic:false;
62 $this->nopicerr = ( $nopicerr != '' )?true:false;
63 $this->nofiles = $nofiles;
64 if ($shopcode == 'AUTO') {
65 $sql = "SELECT TABLE_COLLATION FROM information_schema.TABLES WHERE table_schema = '$dbname' AND table_name = 'kunde'";
66 $rs = $this->db->getOne($sql);
68 preg_match('/([^_]+)/',$rs['table_collation'],$hits);
70 $this->shopcode = $hits[1];
72 $this->shopcode = 'ISO-8859-1';
76 $this->shopcode = $shopcode;
79 function _toERP($txt) {
80 return mb_convert_encoding($txt,$this->erpcode,$this->shopcode);
82 function _toShop($txt) {
83 return mb_convert_encoding($txt,$this->shopcode,$this->erpcode);
85 function translateTable($data,$table) {
87 foreach ($data as $key=>$val) {
88 if (array_key_exists($key, $this->{$table}))
89 $newdata[$this->{$table}[$key]] = $val;
93 function getCategoryID($name,$mwst) {
97 $name = $this->_toShop($name);
99 //Kategorien werden durch die ERP mit "!" getrennt
100 preg_match_all("/([^!]+)!?/",$name,$kat);
107 $sql = "select * from kategorien where name like '%s' and parent_id = %d";
108 if (count($kat)>0) foreach ($kat as $wg) {
109 $sql_ = sprintf($sql,$wg,$parent);
110 $rs=$this->db->getOne($sql_);
111 if ($rs["kategorie_id"]) { // gefunden
112 $parent=$rs["kategorie_id"];
113 $mwst=$rs["mwst_satz"];
114 } else { // nicht gefunden, anlegen
115 $parent=$this->createCategory($wg,$mwst,$parent);
120 function createCategory($name,$mwst,$parent) {
121 $sql = "select max(positions_nr) as cnt from kategorien WHERE parent_id = ".$parent;
122 $rs=$this->db->getOne($sql);
123 $pos = $rs['cnt'] + 1;
124 $sql = "INSERT INTO kategorien (positions_nr,name,mwst_satz,ist_sichtbar,parent_id) ";
125 $sql .= "VALUES (".$pos.",'".$name."',".$mwst.",'Y',".$parent.")";
126 $rc = $this->db->query($sql);
128 $sql = "SELECT kategorie_id FROM kategorien where name = '".$name."' and parent_id = ".$parent;
129 $rs = $this->db->getOne($sql);
130 return $rs['kategorie_id'];
135 function getLang($lang) {
136 $sql = "SELECT * FROM locale WHERE iso_639_1_code like '$lang' and aktiviert = 'Y'";
137 $rs = $this->db->getOne($sql);
138 return $rs['locale_id'];
140 function saveArtikel($data,$lang) {
141 $langID = $this->getLang(strtolower($lang));
142 $values = $this->translateTable($data,"tableerp"); //$this->tableerp);
143 if ($this->mwstLX and !$this->mwstS) { //ERP-Bruttopreis Shop-Nettopreis
144 $values['preis'] = round($values['preis'] / (1 + $values["mwst_satz"]),2);
145 } else if (!$this->mwstLX and $this->mwstS) { //ERP-Nettopreis Shop-Bruttopreis
146 $values['preis'] = round($values['preis'] * (1 + $values["mwst_satz"]),2);
148 $values["name"] = $this->_toShop($values["name"]);
149 $values["beschreibung"] = $this->_toShop($values["beschreibung"]);
150 $values["mwst_satz"] = $values["mwst_satz"] * 100;
151 $values["kategorie_id"] = $this->getCategoryID($data["partsgroup"],$values["mwst_satz"]);
152 $values["artikel_id"] = $this->checkArtikelOK($values["artikel_nr"]);
153 if ($values["artikel_id"]>0) {
154 $rc = $this->updateArtikel($values);
156 $rc = $this->insertArtikel($values);
160 function checkArtikelOK($nr) {
161 $sql = "SELECT artikel_id,artikel_nr FROM artikel WHERE artikel_nr = '".$nr."'";
162 $rs = $this->db->getOne($sql);
163 if ( $rs["artikel_nr"] == $nr ) {
164 return $rs["artikel_id"];
169 function insertArtikel($data) {
170 $newID = uniqid(rand());
172 $sql = "INSERT INTO artikel (artikel_nr,name) VALUES ('".$data["artikel_nr"]."','$newID')";
173 $rc = $this->db->query($sql);
174 $sql = "SELECT * FROM artikel WHERE name='$newID'";
175 $rs = $this->db->getOne($sql);
176 if ($rs['name'] == $newID) {
177 $data["artikel_id"] = $rs["artikel_id"];
178 $statement = "INSERT INTO artikel_kategorie (fk_artikel_id,fk_kategorie_id) VALUES (?,?)";
179 $values = array($rs["artikel_id"],$data["kategorie_id"]);
180 $rc = $this->db->insert($statement,$values);
182 $this->error->out($data['artikel_nr'].' konnte nicht zur Gruppe '.$data['kategorie_id'].' zugefügt werden.');
183 $this->error->write('pepper',$data['artikel_nr'].' konnte nicht zur Gruppe '.$data['kategorie_id'].' zugefügt werden.');
184 $this->db->Rollback();
188 $this->error->out($data['artikel_nr']." insert ",true);
189 $rc = $this->updateArtikel($data);
190 } else { return false; }
193 function updateArtikel($values) {
194 $sql = "UPDATE artikel SET name = :name, beschreibung = :beschreibung, preis = :preis, gewicht = :gewicht, ";
195 if ( !$this->nofiles ) {
196 //vorhandene Bilder übertragen
197 if ( $values['bild_gross'] != '' ) {
198 preg_match("/(.+)\.(jpg|png|jpeg|gif)/i",$values['bild_gross'],$tmp);
199 $sql .= "bild_gross = :bild_gross, bild_klein = :bild_klein, bildtyp = :bildtyp, ";
200 if ( count($tmp) == 3 ) {
201 if ( $this->pic && $this->pic->copyImage($values['artikel_id'],$values['bild_gross'],$tmp[2]) ) {
202 $values['bild_gross'] = $values['artikel_id']."_gr.".$tmp[2];
203 $values['bild_klein'] = $values['artikel_id']."_kl.".$tmp[2];
204 $values['bildtyp'] = $tmp[2];
205 } else if ( $this->nopic ){
206 $sql .= "bild_gross = :bild_gross, bild_klein = :bild_klein, bildtyp = :bildtyp, ";
207 $values['bild_gross'] = $this->nopic."_gr.jpg";
208 $values['bild_klein'] = $this->nopic."_kl.jpg";
209 $values['bildtyp'] = 'jpg';
212 } else if ( $this->nopic && !$this->nopicerr ){
213 $sql .= "bild_gross = :bild_gross, bild_klein = :bild_klein, bildtyp = :bildtyp, ";
214 $values['bild_gross'] = $this->nopic."_gr.jpg";
215 $values['bild_klein'] = $this->nopic."_kl.jpg";
216 $values['bildtyp'] = 'jpg';
219 $sql .= "mwst_satz = :mwst_satz, anzahl_einheit = :anzahl_einheit ";
220 //Kein Lagerbestand übergeben, also nichts ändern
221 if ( $values['lagerbestand'] != '' ) $sql .= ",lagerbestand = :lagerbestand ";
222 $sql .= "WHERE artikel_id = :artikel_id ";
223 $rc = $this->db->update($sql,$values);
225 return $values["artikel_id"];
230 function getBestellung($employee_id) {
231 $sql = "SELECT * FROM mehrwertsteuer WHERE beschreibung = 'Porto und Verpackung'";
232 $rs = $this->db->getOne($sql);
233 $versandsteuer = $rs["mwst_satz"];
234 $sql = "SELECT * FROM bestellung WHERE bestellung_bezahlt='N' ";
235 $sql .= "AND rechnungs_nr != '' AND session_id = '' ";
236 $sql .= 'ORDER BY bestellungs_id';
237 $rs=$this->db->getAll($sql);
238 if (!$rs) return array();
240 foreach ($rs as $row) {
241 $tmp = $this->getBestellArtikel($row["bestellungs_id"]);
242 $artikel = $tmp['data'];
243 if ($versandsteuer == -2) $versandsteuer = $tmp['mwst'];
244 if ($row["versandkosten"]>0) {
245 if ($versandsteuer == $this->versandV['TAX']) {
246 $artikel[] = array("partnumber"=>$this->versandV['NR'],"description"=>$this->versandV['TXT'],
247 "qty"=>1,"unit"=>$this->versandV['Unit'],"sellprice"=>$row["versandkosten"]);
249 $artikel[] = array("partnumber"=>$this->versandS['NR'],"description"=>$this->versandS['TXT'],"taxrate"=>$this->versandS['TAX'],
250 "qty"=>1,"unit"=>$this->versandS['Unit'],"sellprice"=>$row["versandkosten"]);
253 if ($row["nachnamebetrag"]>0)
254 $artikel[] = array("partnumber"=>$this->nachn['NR'],"description"=>$this->nachn['TXT'],"taxrate"=>$this->nachn['TAX'],
255 "qty"=>1,"unit"=>$this->nachn['Unit'],"sellprice"=>$row["nachnamebetrag"]);
256 if ($row["paypalkosten"]>0)
257 $artikel[] = array("partnumber"=>$this->paypal['NR'],"description"=>$this->paypal['TXT'],"taxrate"=>$this->paypal['TAX'],
258 "qty"=>1,"unit"=>$this->paypal['Unit'],"sellprice"=>round($row["paypalkosten"],2));
259 if ($row["treuhandkosten"]>0)
260 $artikel[] = array("partnumber"=>$this->treuhand['NR'],"description"=>$this->treuhand['TXT'],"taxrate"=>$this->treuhand['TAX'],
261 "qty"=>1,"unit"=>$this->treuhand['Unit'],"sellprice"=>$row["treuhandkosten"]);
262 if ($row["mindermengenzuschlag"]>0)
263 $artikel[] = array("partnumber"=>$this->minder['NR'],"description"=>$this->minder['TXT'],"taxrate"=>$this->minder['TAX'],
264 "qty"=>1,"unit"=>$this->minder['Unit'],"sellprice"=>$row["mindermengenzuschlag"]);
265 if ($row["versandland_id"] == "DE") {
267 } else if (in_array($this->EU,$row["versandland_id"])) {
268 if (preg_match('/^[^0-9]{2,3}[ 0-9]+$/',$row["customer"]['ustid'])) {
276 if ($row["kreditkarten_nummer"]) {
277 $row["kreditkarte"] = $row['kreditkarten_hersteller']."\n";
278 $row["kreditkarte"] = $row['kreditkarten_nummer']." ID:".$row['kreditkarten_id']."\n";
279 $row["kreditkarte"] = $row['kreditkarten_ablaufdatum']."\n";
280 $row["kreditkarte"] = $row['kreditkarten_vorname']." ".$row['kreditkarten_nachname']."\n";
282 $row["versandart"] = $this->_toERP($row["versandart"]);
283 $row = $this->translateTable($row,"tableshop");
284 $row["taxzone_id"] = $taxzone_id;
285 $row["notes"] = $this->_toERP($row["notes"]);
286 $row["employee_id"] = $employee_id;
287 $row["parts"] = $artikel;
288 $row["mwst"] = round($row["mwst"],2);
289 $row["amount"]= round($row["amount"],2);
290 $row["netamount"] = $row["amount"] - $row["mwst"];
291 $row["customer"] = $this->getBestellungKunde($row["cusordnumber"]);
296 function getBestellungKunde($bestellung) {
297 $sql = "SELECT * FROM kunde LEFT JOIN bestellung_kunde ON Kunden_ID=FK_Kunden_ID ";
298 $sql .= "WHERE FK_Bestellungs_ID=$bestellung";
299 $rs=$this->db->getOne($sql);
301 $rs["kontakt"] = $this->_toERP($rs["vorname"]." ".$rs["nachname"]);
302 $rs["firma"] = $this->_toERP($rs["firma"]);
304 $rs["kontakt"] = $this->_toERP($rs["vorname"]." ".$rs["nachname"]);
305 $rs["firma"] = $this->_toERP($rs["nachname"].", ".$rs["vorname"]);
307 $rs["strasse"] = $this->_toERP($rs["strasse"])." ".$rs['hausnummer'];
308 $rs["ort"] = $this->_toERP($rs["ort"]);
309 $rs["bankname"] = $this->_toERP($rs["bankname"]);
311 return $this->translateTable($rs,"custshop");
313 $this->error->write("pepper","Die Kunde der Bestellung $bestellung konnte nicht gelesen werden");
317 function getBestellArtikel($bestellung) {
318 if (!$this->kategorien) $this->getKategorien();
319 $sql = "SELECT B.*,P.artikel_nr,P.beschreibung,P.mwst_satz,P.anzahl_einheit,P.bild_gross,";
320 $sql .= "AK.fk_kategorie_id as partsgroup ";
321 $sql .= "FROM artikel_bestellung B LEFT JOIN artikel P ";
322 $sql .= "ON B.fk_artikel_id=P.artikel_id LEFT JOIN artikel_kategorie AK on AK.fk_artikel_id=P.artikel_id ";
323 $sql .= "WHERE fk_bestellungs_id=".$bestellung;
324 $rs=$this->db->getAll($sql);
326 $this->error->write("pepper","Die Artikel der Bestellung $bestellung konnte nicht gelesen werden");
330 foreach ($rs as $row) {
331 if (in_array($row['a_b_id'],$a_b_ID)) continue;
332 $row['artikelname'] = $this->_toERP($row['artikelname']);
333 $row['beschreibung'] = $this->_toERP($row['beschreibung']);
334 if ($row['variation'] != '') {
335 $tmp = $this->splitVariant($row['variation'],$row['anzahl'],$row['fk_artikel_id']);
336 $row['artikelname'] .= $this->_toERP($tmp['text']);
337 $row['preis'] += $tmp['preis'];
338 if ($tmp['nr']) $row['artikel_nr'] .= '-'.$tmp['nr'];
340 if ($row['optionen'] != '') {
341 $tmp = $this->splitOption($row['optionen'],$row['anzahl']);
342 $row['artikelname'] .= $this->_toERP($tmp['text']);
343 $row['preis'] += $tmp['preis'];
345 $row['anzahl_einheit'] = $this->_toERP($row['anzahl_einheit']);
346 $row['partsgroup'] = $this->_toERP($this->Kategorien[$row['partsgroup']]);
347 $mwst[$row['mwst_satz']] = $row['preis'] * $row['anzahl'] / (100+$row['mwst_satz']) * 100;
348 $data[] = $this->translateTable($row,"orderparts");
349 $a_b_ID[] = $row['a_b_id'];
352 $tmp = each($mwst); //MwSt-Satz mit grösstem Anteil
353 return array('data'=>$data,'mwst'=>$tmp['key']);
355 function splitVariant($txt,$qty,$artnr) {
356 $vari=split(chr(254),$txt);
360 for($cnt=0; $cnt<count($vari); $cnt++) {
362 $tmp = split('<::>',$vari[$cnt]);
363 if ($this->VariantNr) {
364 //$nr = $this->_getVariantNr($tmp[0],$tmp[1],$artnr);
365 $sql = 'SELECT variations_nr FROM artikel_variationen where fk_artikel_id = '.$artnr;
366 $sql .= ' and variationstext = \''.$tmp[1].'\' and variations_grp = (';
367 $sql .= 'SELECT gruppen_nr FROM artikel_variationsgruppen WHERE fk_artikel_id = '.$artnr;
368 $sql .= ' AND gruppentext = \''.$tmp[0].'\')';
369 $rs=$this->db->getOne($sql);
370 $nr = $rs['variations_nr'];
372 //$text.="\n".$tmp[0].": ".$tmp[1];
373 $text.=", ".$tmp[0].": ".$tmp[1];
375 $preis+=trim($vari[$cnt]) * $qty;
378 return array("preis"=>$preis,"text"=>$text,"nr"=>$nr);
380 function splitOption($txt,$qty) {
381 $vari=split(chr(254),$txt);
385 for($cnt=0; $cnt<count($vari); $cnt++) {
386 $text.="\n".str_replace('<::>',': ',$vari[$cnt]);
388 $preis+=trim($vari[$cnt]) * $qty;
391 return array("preis"=>$preis,"text"=>$text);
393 function setAbgeholt($bestellung) {
394 $sql = "UPDATE bestellung SET Bestellung_bezahlt='Y' WHERE Bestellungs_ID = $bestellung"; // in ($bestellungen)";
395 $rc = $this->db->query($sql);
397 $this->error->write("pepper","Die Bestellung $bestellung konnten nicht als abgeholt markiert werden");
403 function setKundenNr($id,$nr) {
404 $sql = "UPDATE kunde SET kunden_nr = '$nr' WHERE k_id = $id";
405 $rc = $this->db->query($sql);
407 $this->error->write("pepper","Die Kundennummer $nr konnte nicht dem Kunden $id zugeordnet werden");
413 function getAllArtikel() {
414 if (!$this->Kategorien) $this->getKategorien();
415 $sql = "SELECT a.*,k.fk_kategorie_id as katid FROM artikel a LEFT JOIN artikel_kategorie k on a.artikel_id = k.fk_artikel_id";
416 $rs = $this->db->getAll($sql);
417 if ($rs) foreach ($rs as $row) {
418 $row['partsgroup'] = $this->_toERP($this->Kategorien[$row['katid']]);
419 $row['name'] = $this->_toERP($row['name']);
420 $row['beschreibung'] = $this->_toERP($row['beschreibung']);
421 $row['shoppreis'] = $row['preis'];
422 if (!$this->mwstLX) $row['preis'] = round(($row['preis'] / (100 + $row['mwst_satz']) * 100),$this->dezimal);
423 $data[] = $this->translateTable($row,"orderparts");
427 function getKategorien() {
428 $sql = "SELECT kategorie_id,name,parent_id FROM kategorien WHERE parent_id >= 0 order by parent_id";
429 $rs = $this->db->getAll($sql);
431 foreach($rs as $row) { $this->katrs[$row['kategorie_id']] = $row;};
432 foreach($this->katrs as $row) {
433 if ($row['parent_id'] == '0') {
434 $name = $row['name'];
436 $name = $this->mkKategorien($row['kategorie_id'],'');
438 $this->Kategorien[$row['kategorie_id']] = $name;
442 function mkKategorien($id,$name) {
443 if ($this->katrs[$id]['parent_id'] == '0') {
445 return $this->katrs[$id]['name'].'!'.$name;
447 return $this->katrs[$id]['name']."#";
451 $name = $this->katrs[$id]['name'];
453 $name = $this->katrs[$id]['name'].'!'.$name;
455 $name = $this->mkKategorien($this->katrs[$id]['parent_id'],$name);