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 $SHOPincl = true; //Shoppreise sind Brutto
32 function erp($db,$error,$divStd,$divVerm,$doordnr,$docustnr,$preordnr,$precustnr,$INVnetto,$SHOPincl,$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->SHOPincl = ($SHOPincl == 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] || !array_key_exists($nr, $this->TAX) ) {
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";
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 ";
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);
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 if ($this->INVnetto) {
217 $row['sellprice'] = round($row['sellprice'] / (100 + $row['taxrate']) * 100,2);
219 if (!$this->SHOPincl)
220 $row['sellprice'] = round($row['sellprice'] * (100 + $row['taxrate']) * 100,2);
222 $row['unit'] = $this->chkUnit($row['unit']);
224 //$row['longdescription'] = addslashes($row['longdescription']);
225 $row['longdescription'] = $row['longdescription'];
227 //$row['longdescription'] = addslashes($tmp['longdescription']);
228 $row['longdescription'] = $tmp['longdescription'];
230 //$row['description'] = addslashes($row['description']);
231 $sql = "INSERT INTO orderitems (trans_id,parts_id,description,longdescription,qty,sellprice,unit,pricegroup_id,discount) ";
232 $sql .= "VALUES (:trans_id,:parts_id,:description,:longdescription,:qty,:sellprice,:unit,0,0)";
233 $row["trans_id"]=$trans_id;
234 $rc = $this->db->insert($sql,$row);
236 $this->db->rollback();
243 function insCustomer($data) {
244 $this->error->out('Insert:'.$data["name"].' ');
245 if ($this->docustnr == 1) {
246 $data['customernumber'] = $this->getNewNr('customer');
248 $data['customernumber'] = $data['shopid'];
250 $data['customernumber'] = $this->precustnr.$data['customernumber'];
251 if ($data['customernumber']>0) {
252 if (!$data['greeting']) $data['greeting'] = '';
253 $sql = "INSERT INTO customer (greeting,name,street,city,zipcode,country,contact,phone,email,customernumber)";
254 $sql .= " VALUES (:greeting,:name,:street,:city,:zipcode,:country,:contact,:phone,:email,:customernumber)";
255 $rc = $this->db->insert($sql,$data);
256 $sql = "SELECT id FROM customer WHERE customernumber = '".$data['customernumber']."'";
257 $rs = $this->db->getOne($sql);
259 $this->error->out("Kd-Nr: ".$data['customernumber'].":".$rs['id']);
261 $this->error->write('erplib','Kunde anlegen: '.$data["name"]);
262 $this->db->rollback();
267 function chkCustomer($data) {
268 if ($data['customer_id']>0) {
269 $sql = "SELECT * FROM customer WHERE id = ".$data['customer_id'];
270 $rs = $this->db->getOne($sql);
271 if ($rs['id'] == $data['customer_id']) {
272 $this->error->out('Update:'.$data['customer_id'].' ');
273 $sql = "UPDATE customer SET greeting = :greeting,name = :name,street = :street,city = :city,country = :country,";
274 $sql .= "zipcode = :zipcode,contact = :contact,phone = :phone,email = :email WHERE id = :customer_id";
275 $rc = $this->db->update($sql,$data);
276 if ($rc) $rc = $data['customer_id'];
278 $rc = $this->insCustomer($data);
281 $rc = $this->insCustomer($data);
285 function mkAuftrag($data,$shop,$longtxt) {
287 $data["notes"] .= "\nBezahlung:".$data['bezahlung']."\n";
288 if ($data['bezahlung'] == "Kreditkarte") $data["notes"] .= $data['kreditkarte']."\n";
290 $data["intnotes"] = "Shop: $shop";
292 $data["intnotes"] = "";
294 $data["customer_id"] = $this->chkCustomer($data["customer"]);
295 $parts = $data['parts'];
296 unset($data['parts']);
297 unset($data['customer']);
298 if ($this->doordnr == 1) {
299 $data["ordnumber"] = $this->getNewNr('so');
301 $data["ordnumber"] = $data['cusordnumber'];
303 $data["ordnumber"] = $this->preordnr.$data["ordnumber"];
304 $tid = $this->newOrder($data);
306 $rc = $this->insParts($tid,$parts,$longtxt);
308 $this->error->write('erplib','Artikel zu Auftrag');
312 $this->error->write('erplib','Auftrag anlegen');
315 $this->error->out($data["customer"]["firma"]." ");
316 $rc = $this->db->Commit();
317 return $data["customer_id"];
319 function chkPartsgroup($pg,$new=True) {
320 /*gibt es die Warengruppe?
321 Rückgabe nichts oder die partsgroup.id
323 $sql = "SELECT * FROM partsgroup WHERE partsgroup = '".$pg."'";
324 $rs = $this->db->getOne($sql);
327 } else if ($this->mkPart and $new) {
328 return $this->mkNewPartsgroup($pg);
333 function mkNewPartsgroup($name) {
334 $sql = "INSERT INTO partsgroup (partsgroup) VALUES ('".$name."')";
335 $rc = $this->db->query($sql);
337 return $this->chkPartsgroup($name,False);
342 function chkUnit($unit) {
343 /*Prüfen ob es die Unit gibt.
344 wenn nicht, die Standardunit zurückgeben*/
346 return $this->stdUnit();
348 $sql = "SELECT * FROM units WHERE name ilike '".$unit."'";
349 $rs = $this->db->getOne($sql);
353 return $this->stdUnit();
358 $sql = "SELECT * FROM units WHERE type = 'dimension' ORDER BY sortkey LIMIT 1";
359 $rs = $this->db->getOne($sql);
362 function chkPartnumber($data,$new=True,$long=false) {
363 $sql = "SELECT * FROM parts WHERE partnumber = '".$data["partnumber"]."'";
364 $rs = $this->db->getOne($sql);
371 } else if ($new and $this->mkPart) {
372 $data['id'] = $this->mkNewPart($data);
382 function mkNewPart($data) {
383 /*eine neue Ware anlegen, sollte nicht direkt aufgerufen werden.
384 Auf vorhandene partnumber wird nicht geprüft.
385 Folgendes Array muß übergeben werden:
386 $data = array(partnumber,description,longdescription,weight,sellprice,taxrate,partsgroup,unit)
389 $link = '<a href="../ic.pl?action=edit&id=%d" target="_blank">';
390 if ($data['partnumber'] == '') {
391 $this->error->write('erplib','Artikelnummer fehlt');
394 if ($data['description'] == '') {
395 $this->error->write('erplib','Artikelbezeichnung fehlt');
398 $data['notes'] = addslashes($data['longdescription']);
399 if ($data['weight']*1 != $data['weight']) $data['weight']=0;
400 if ($data['sellprice']*1 != $data['sellprice']) $data['sellprice']=0;
401 if (!in_array($data["buchungsgruppen_id"],$this->TAX)) {
402 foreach ($this->TAX as $key=>$tax) {
403 if ($tax["rate"] == $data["taxrate"]/100) {
404 $data["buchungsgruppen_id"] = $key;
408 if (!$data["buchungsgruppen_id"]) {
409 $this->error->write('erplib','Buchungsgruppe konnte nicht zugeordnet werden');
413 if ($data["partsgroup"]) {
414 $data["partsgroup_id"] = $this->chkPartsgroup($data["partsgroup"]);
416 $data["partsgroup_id"] = '';
418 $data['unit'] = $this->chkUnit($data['unit']);
419 if ($data['unit'] == '') {
420 $this->error->write('erplib','Artikeleinheit fehlt oder falsch');
424 $sql = "INSERT INTO parts (partnumber,description,sellprice,weight,notes,shop,unit,partsgroup_id,";
425 $sql .= "image,buchungsgruppen_id,inventory_accno_id,income_accno_id,expense_accno_id) ";
426 $sql .= "VALUES (:partnumber,:description,:sellprice,:weight,:notes,:shop,:unit,:partsgroup_id,";
427 $sql .= ":image,:buchungsgruppen_id,1,1,1)";
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,:shoppreis)";
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();