4 shop muß gesetzt sein, obsolet nicht
6 Multishop: Hierfür müssen benutzerdefinierte Variablen angelegt werden.
7 Typ:checkbox, Name=shop[0-9A-Z]+, Bearbeitbar=nein
22 var $docustnr = false;
24 var $warehouse_id = 0;
28 var $OEinsPart = false;
29 var $INVnetto = true; //Rechnungen mit Nettopreisen
30 var $ERPincl = false; //ERP-Preise sind Netto
32 function erp($db,$error,$divStd,$divVerm,$doordnr,$docustnr,$preordnr,$precustnr,$INVnetto,$ERPincl,$OEinsPart,$lager,$pricegroup,$ERPusrID) {
34 $this->pricegroup = $pricegroup;
35 $this->employee_id = $ERPusrID;
36 $this->error = $error;
37 $this->divStd = $divStd;
38 $this->divVerm = $divVerm;
39 $this->doordnr = $doordnr;
40 $this->preordnr = $preordnr;
41 $this->docustnr = $docustnr;
42 $this->precustnr = $precustnr;
43 $this->INVnetto = ($INVnetto == 1)?true:false;
44 $this->ERPincl = ($ERPincl == 1)?true:false;
45 $this->OEinsPart = ($OEinsPart == 1)?true:false;
46 $this->lager = ($lager)?$lager:1;
49 $sql = "SELECT warehouse_id from bin where id = ".$this->lager;
50 $rs = $this->db->getOne($sql);
51 if ( $rs['warehouse_id'] > 0 ) {
52 $this->warehouse_id = $rs['warehouse_id'];
53 $sql = "SELECT id from transfer_type WHERE direction = 'in' and description = 'stock'";
54 $rs = $this->db->getOne($sql);
55 $this->transtype = $rs['id'];
63 $sql = "SELECT BG.id AS bugru,T.rate,TK.startdate,C.taxkey_id, ";
64 $sql .= "(SELECT id FROM chart WHERE accno = T.taxnumber) AS tax_id, ";
65 $sql .= "BG.income_accno_id_0,BG.expense_accno_id_0 ";
66 $sql .= "FROM buchungsgruppen BG LEFT JOIN chart C ON BG.income_accno_id_0=C.id ";
67 $sql .= "LEFT JOIN taxkeys TK ON TK.chart_id=C.id ";
68 $sql .= "LEFT JOIN tax T ON T.id=TK.tax_id WHERE TK.startdate <= now()";
69 $rs = $this->db->getAll($sql);
70 if ($rs) foreach ($rs as $row) {
72 if (!$this->TAX[$nr]) {
74 $data['startdate'] = $row['startdate'];
75 $data['rate'] = $row['rate'];
76 $data['taxkey'] = $row['taxkey_id'];
77 $data['taxid'] = $row['tax_id'];
78 $data['income'] = $row['income_accno_id_0'];
79 $data['expense'] = $row['expense_accno_id_0'];
80 $this->TAX[$nr] = $data;
81 } else if ($this->TAX[$nr]['startdate'] < $row['startdate']) {
82 $this->TAX[$nr]["startdate"] = $row['startdate'];
83 $this->TAX[$nr]["rate"] = $row['rate'];
84 $this->TAX[$nr]["taxkey"] = $row['taxkey_id'];
85 $this->TAX[$nr]["taxid"] = $row['tax_id'];
86 $this->TAX[$nr]["income"] = $row['income_accno_id_0'];
87 $this->TAX[$nr]["expense"] = $row['expense_accno_id_0'];
92 function getParts($stdprice=0,$shop=0) {
93 $where = "WHERE 1=1 ";
95 $sql = "SELECT P.partnumber,P.description,P.notes,P.weight,G.price as sellprice,P.sellprice as stdprice,";
96 $sql .= "PG.partsgroup,P.image,P.buchungsgruppen_id as bugru,P.unit,P.ean ";
98 $sql .= ",(select sum(qty) from inventory where bin_id = ".$this->lager." and parts_id = P.id) as onhand ";
100 $sql .= ",P.onhand ";
102 $sql .= "FROM parts P ";
103 $sql .= "LEFT JOIN partsgroup PG on PG.id=P.partsgroup_id ";
104 $sql .= "LEFT JOIN prices G on G.parts_id=P.id ";
105 $where .= "AND (G.pricegroup_id=$stdprice ";
106 $where .= "or G.pricegroup_id is null) ";
108 $sql = "SELECT P.partnumber,P.description,P.notes,P.weight,P.sellprice,";
109 $sql .= "PG.partsgroup,P.image,P.buchungsgruppen_id as bugru,P.unit,P.ean ";
110 if ($this->lager>1) {
111 $sql .= ",(select sum(qty) from inventory where bin_id = ".$this->lager." and parts_id = P.id) as onhand ";
113 $sql .= ",P.onhand ";
115 $sql .= "FROM parts P left join partsgroup PG on PG.id=P.partsgroup_id ";
118 $sql .= "LEFT JOIN custom_variables CV on CV.trans_id=P.id ";
119 $where .= "AND (CV.config_id = $shop AND bool_value = 't')";
121 $where .= "AND shop = 't' ";
122 $where .= "AND obsolete = 'f' ORDER BY P.partnumber";
123 $rs = $this->db->getAll($sql.$where);
124 if ($rs) for($i = 0; $i < count($rs); $i++) {
125 $rs[$i]['tax'] = $this->TAX[$rs[$i]['bugru']]['rate'];
130 function getPartsLang($lang,$alle) {
131 $sql = "SELECT P.partnumber,L.translation,P.description,L.longdescription,P.notes,PG.partsgroup ";
132 $sql .= "FROM parts P left join translation L on L.parts_id=P.id left join partsgroup PG on PG.id=P.partsgroup_id ";
133 $sql .= "WHERE P.shop='t' and (L.language_id = $lang";
135 $sql .= " or L.language_id is Null)";
139 $rs = $this->getAll($sql);
141 if ($rs) foreach ($rs as $row) {
142 if (!$data[$row["partnumber"]]) $data[$row["partnumber"]]=$row;
146 function getNewNr($typ) {
149 customer = Kundennummer
152 $sql = "SELECT $typ FROM defaults";
153 $rs = $this->db->getOne($sql);
154 $i=strlen($rs["$typ"])-1;
155 //Nummern können Buchstaben, Zeichen und Zahlen enthalten
156 //nur die Zahlen von rechts werden aber inkrementiert.
158 if ($rs["$typ"][$i] >= "0" and $rs["$typ"][$i]<="9") {
159 $n=$rs["$typ"][$i].$n;
162 $pre = substr($rs["$typ"],0,$i+1);
168 $sql = "UPDATE defaults SET $typ = '$sonr'";
169 $rc = $this->db->query($sql);
171 $this->error->write('erplib','Neue Nummer ($typ) nicht gesichert: '.$sonr);
175 function newOrder($data) {
176 /*Einen neuen Auftrag anlegen. Folgendes Array muß übergeben werden:
177 $data = array(ordnumber,customer_id,employee_id,taxzone_id,amount,netamount,transdate,notes,intnotes,shipvia)
180 $incltax = ($this->INVnetto)?'f':'t';
181 $sql = "INSERT INTO oe (ordnumber,customer_id,employee_id,taxzone_id,taxincluded,curr,amount,netamount,transdate,notes,intnotes,shipvia,cusordnumber) ";
182 $sql .= "values (:ordnumber,:customer_id,:employee_id,:taxzone_id,'$incltax',:curr,:amount,:netamount,:transdate,:notes,:intnotes,:shipvia,:cusordnumber)";
183 $rc = $this->db->insert($sql,$data);
184 $sql = "SELECT * FROM oe where ordnumber = '".$data["ordnumber"]."'";
185 $rs = $this->db->getOne($sql);
187 $this->error->write('erplib','Auftrag erzeugen: '.$data["ordnumber"]);
188 $this->db->rollback();
191 $this->error->out(" Auftrag: ".$data["ordnumber"]." ");
195 function insParts($trans_id,$data,$longtxt) {
196 /*Artikel in die orderitem einfügen. Folgende Daten müssen übergeben werden:
197 $trans_id = (int) oe.id
198 $data = array(trans_id,partnumber,description,longdescription,qty,sellprice,unit)*/
199 foreach ($data as $row) {
200 $row['trans_id'] = $trans_id;
201 //$sql = "SELECT id FROM parts WHERE partnumber = '".$row['partnumber']."'";
202 //$tmp = $this->db->getOne($sql);
203 $tmp = $this->chkPartnumber($row,$this->OEinsPart,true,true);
205 $row['parts_id'] = $tmp['id'];
207 if ($this->TAX[$this->divStd['BUGRU']]['rate'] == $row['mwst']/100) {
208 $row['parts_id'] = $this->divStd['ID'];
209 } else if ($this->TAX[$this->divVerm['BUGRU']]['rate'] == $row['mwst']/100) {
210 $row['parts_id'] = $this->divVerm['ID'];
212 $row['parts_id'] = $this->divStd['ID'];
215 //Shop soll immer Nettopreise liefern!
216 if (!$this->INVnetto) {
217 $row['sellprice'] = round($row['sellprice'] * (100 + $row['taxrate']) / 100,2);
219 $row['unit'] = $this->chkUnit($row['unit']);
221 //$row['longdescription'] = addslashes($row['longdescription']);
222 $row['longdescription'] = $row['longdescription'];
224 //$row['longdescription'] = addslashes($tmp['longdescription']);
225 $row['longdescription'] = $tmp['longdescription'];
227 //$row['description'] = addslashes($row['description']);
228 $sql = "INSERT INTO orderitems (trans_id,parts_id,description,longdescription,qty,sellprice,unit,pricegroup_id,discount) ";
229 $sql .= "VALUES (:trans_id,:parts_id,:description,:longdescription,:qty,:sellprice,:unit,0,0)";
230 $row["trans_id"]=$trans_id;
231 $rc = $this->db->insert($sql,$row);
233 $this->db->rollback();
240 function insCustomer($data) {
241 $this->error->out('Insert:'.$data["name"].' ');
242 if ($this->docustnr == 1) {
243 $data['customernumber'] = $this->getNewNr('customer');
245 $data['customernumber'] = $data['shopid'];
247 $data['customernumber'] = $this->precustnr.$data['customernumber'];
248 if ($data['customernumber']>0) {
249 if (!$data['greeting']) $data['greeting'] = '';
250 $sql = "INSERT INTO customer (greeting,name,street,city,zipcode,country,contact,phone,email,customernumber)";
251 $sql .= " VALUES (:greeting,:name,:street,:city,:zipcode,:country,:contact,:phone,:email,:customernumber)";
252 $rc = $this->db->insert($sql,$data);
253 $sql = "SELECT id FROM customer WHERE customernumber = '".$data['customernumber']."'";
254 $rs = $this->db->getOne($sql);
256 $this->error->out("Kd-Nr: ".$data['customernumber'].":".$rs['id']);
258 $this->error->write('erplib','Kunde anlegen: '.$data["name"]);
259 $this->db->rollback();
264 function chkCustomer($data) {
265 if ($data['customer_id']>0) {
266 $sql = "SELECT * FROM customer WHERE id = ".$data['customer_id'];
267 $rs = $this->db->getOne($sql);
268 if ($rs['id'] == $data['customer_id']) {
269 $this->error->out('Update:'.$data['customer_id'].' ');
270 $sql = "UPDATE customer SET greeting = :greeting,name = :name,street = :street,city = :city,country = :country,";
271 $sql .= "zipcode = :zipcode,contact = :contact,phone = :phone,email = :email WHERE id = :customer_id";
272 $rc = $this->db->update($sql,$data);
273 if ($rc) $rc = $data['customer_id'];
275 $rc = $this->insCustomer($data);
278 $rc = $this->insCustomer($data);
282 function mkAuftrag($data,$shop,$longtxt) {
284 $data["notes"] .= "\nBezahlung:".$data['bezahlung']."\n";
285 if ($data['bezahlung'] == "Kreditkarte") $data["notes"] .= $data['kreditkarte']."\n";
287 $data["intnotes"] = "Shop: $shop";
289 $data["intnotes"] = "";
291 $data["customer_id"] = $this->chkCustomer($data["customer"]);
292 $parts = $data['parts'];
293 unset($data['parts']);
294 unset($data['customer']);
295 if ($this->doordnr == 1) {
296 $data["ordnumber"] = $this->getNewNr('so');
298 $data["ordnumber"] = $data['cusordnumber'];
300 $data["ordnumber"] = $this->preordnr.$data["ordnumber"];
301 $tid = $this->newOrder($data);
303 $rc = $this->insParts($tid,$parts,$longtxt);
305 $this->error->write('erplib','Artikel zu Auftrag');
309 $this->error->write('erplib','Auftrag anlegen');
312 $this->error->out($data["customer"]["firma"]." ".$data["shopid"]);
313 $rc = $this->db->Commit();
314 return $data["customer_id"];
316 function chkPartsgroup($pg,$new=True) {
317 /*gibt es die Warengruppe?
318 Rückgabe nichts oder die partsgroup.id
320 $sql = "SELECT * FROM partsgroup WHERE partsgroup = '".$pg."'";
321 $rs = $this->db->getOne($sql);
324 } else if ($this->mkPart and $new) {
325 return $this->mkNewPartsgroup($pg);
330 function mkNewPartsgroup($name) {
331 $sql = "INSERT INTO partsgroup (partsgroup) VALUES ('".$name."')";
332 $rc = $this->db->query($sql);
334 return $this->chkPartsgroup($name,False);
339 function chkUnit($unit) {
340 /*Prüfen ob es die Unit gibt.
341 wenn nicht, die Standardunit zurückgeben*/
343 return $this->stdUnit();
345 $sql = "SELECT * FROM units WHERE name ilike '".$unit."'";
346 $rs = $this->db->getOne($sql);
350 return $this->stdUnit();
355 $sql = "SELECT * FROM units WHERE type = 'dimension' ORDER BY sortkey LIMIT 1";
356 $rs = $this->db->getOne($sql);
359 function chkPartnumber($data,$new=True,$long=false,$bestellung=false) {
360 $sql = "SELECT * FROM parts WHERE partnumber = '".$data["partnumber"]."'";
361 $rs = $this->db->getOne($sql);
368 } else if ($new and $this->mkPart) {
369 $data['id'] = $this->mkNewPart($data,$bestellung);
379 function mkNewPart($data,$bestellung) {
380 /*eine neue Ware anlegen, sollte nicht direkt aufgerufen werden.
381 Auf vorhandene partnumber wird nicht geprüft.
382 Folgendes Array muß übergeben werden:
383 $data = array(partnumber,description,longdescription,weight,sellprice,taxrate,partsgroup,unit)
386 $link = '<a href="../ic.pl?action=edit&id=%d" target="_blank">';
387 if ($data['partnumber'] == '') {
388 $this->error->write('erplib','Artikelnummer fehlt');
391 if ($data['description'] == '') {
392 $this->error->write('erplib','Artikelbezeichnung fehlt');
395 $data['notes'] = addslashes($data['longdescription']);
396 if ($data['weight']*1 != $data['weight']) $data['weight']=0;
397 if ($data['sellprice']*1 != $data['sellprice']) $data['sellprice']=0;
398 if (!in_array($data["buchungsgruppen_id"],$this->TAX)) {
399 foreach ($this->TAX as $key=>$tax) {
400 if ($tax["rate"] == $data["taxrate"]/100) {
401 $data["buchungsgruppen_id"] = $key;
405 if (!$data["buchungsgruppen_id"]) {
406 $this->error->write('erplib','Buchungsgruppe konnte nicht zugeordnet werden');
410 if ($data["partsgroup"]) {
411 $data["partsgroup_id"] = $this->chkPartsgroup($data["partsgroup"]);
413 $data["partsgroup_id"] = '';
415 $data['unit'] = $this->chkUnit($data['unit']);
416 if ($data['unit'] == '') {
417 $this->error->write('erplib','Artikeleinheit fehlt oder falsch');
421 $sql = "INSERT INTO parts (partnumber,description,sellprice,weight,notes,shop,unit,partsgroup_id,";
422 $sql .= "ean,image,buchungsgruppen_id,inventory_accno_id,income_accno_id,expense_accno_id) ";
423 $sql .= "VALUES (:partnumber,:description,:sellprice,:weight,:notes,:shop,:unit,:partsgroup_id,";
424 $sql .= ":ean,:image,:buchungsgruppen_id,1,1,1)";
425 if ($this->ERPincl) {
426 $data['sellprice'] = round($data['sellprice'] * (100+$data['taxrate'])/100,2);
428 $rc = $this->db->insert($sql,$data);
429 $data['parts_id'] = $this->chkPartnumber($data,false);
430 if ( $this->pricegroup > 0 ) {
431 $sql = "INSERT INTO prices (parts_id,pricegroup_id,price) VALUES (:parts_id,:pricegroup,:sellprice)";
432 $data['pricegroup'] = $this->pricegroup;
433 $rc = $this->db->insert($sql,$data);
435 if ( $data['onhand'] > 0 and $this->lager > 1) $this->insLager($data);
436 $x = $this->chkPartnumber($data,False);
437 $this->error->write('erplib',$data['description'].' '.$data['partnumber']);
438 $this->error->out(sprintf($link,$data['parts_id']).$data['description'].' '.$data['partnumber'].'</a>',true);
441 function insLager($data) {
442 $rc = $this->db->Begin();
443 $sql = "SELECT nextval(('id'::text)::regclass) as id from id";
444 $rs = $this->db->getOne($sql);
445 $sql = "INSERT INTO inventory (warehouse_id,parts_id,shippingdate,employee_id,bin_id,qty,trans_id,trans_type_id,comment) ";
446 $sql .= "VALUES (:wid,:parts_id,now(),:employee_id,:bid,:onhand,:next,:tt,'Shopübernahme')";
447 $data['next'] = $rs['id'];
448 $data['tt'] = $this->transtype;
449 $data['bid'] = $this->lager;
450 $data['wid'] = $this->warehouse_id;
451 $data['employee_id'] = $this->employee_id;
452 $rc = $this->db->insert($sql,$data);
456 $this->db->Rollback();