Globale Variablen $::userspath, $::templates, $::membersfile nach %::lx_office_conf...
[kivitendo-erp.git] / oscom / shoplib.php
1 <?php
2
3 $login=$_GET["login"];
4 $debug=false;
5 require_once "DB.php";
6 if (file_exists ("conf$login.php")) {
7 echo "User";
8         require "conf$login.php";
9 } else {
10 echo "Global";
11         require "conf.php";
12 }
13
14
15 $landarray=array("DEUTSCHLAND"=>"D","STEREICH"=>"A","OESTEREICH"=>"A","SCHWEIZ"=>"CH");
16 $EU=2;  //EU ohne UStID, 1 = EU mit UStID
17 $taxarray=array(
18         'Germany'=>array('code'=>'DE','tax'=>0),                'Austria'=>array('code'=>'AU','tax'=>$EU),                      'Belgium'=>array('code'=>'BE','tax'=>$EU),
19         'Bulgaria'=>array('code'=>'BG','tax'=>$EU),             'Czech Republic'=>array('code'=>'CZ','tax'=>$EU),       'Denmark'=>array('code'=>'DK','tax'=>$EU),
20         'Estonia'=>array('code'=>'EE','tax'=>$EU),              'Spain'=>array('code'=>'ES','tax'=>$EU),                        'Finland'=>array('code'=>'FI','tax'=>$EU),
21         'France'=>array('code'=>'FR','tax'=>$EU),               'United Kingdom'=>array('code'=>'GB','tax'=>$EU),       'Greece'=>array('code'=>'GR','tax'=>$EU),
22         'Hungary'=>array('code'=>'HU','tax'=>$EU),              'Ireland'=>array('code'=>'IE','tax'=>$EU),                      'Italy'=>array('code'=>'IT','tax'=>$EU),
23         'Luxembourg'=>array('code'=>'LU','tax'=>$EU),   'Malta'=>array('code'=>'MT','tax'=>$EU),                        'Netherlands'=>array('code'=>'NL','tax'=>$EU),
24         'Poland'=>array('code'=>'PL','tax'=>$EU),               'Portugal'=>array('code'=>'PT','tax'=>$EU),             'Romania'=>array('code'=>'RO','tax'=>$EU),
25         'Sweden'=>array('code'=>'SE','tax'=>$EU),               'Slovenia'=>array('code'=>'SI','tax'=>$EU),             'Slovakia (Slovak Republic)'=>array('code'=>'SK','tax'=>$EU),
26         'Cyprus'=>array('code'=>'CY','tax'=>$EU),               'Lithuania'=>array('code'=>'LT','tax'=>$EU),            'Latvia'=>array('code'=>'LV','tax'=>$EU));
27
28 $defaultland="D";
29 $taxid=0;
30 $log=false;
31 $erp=false;
32 $shop=false;
33
34
35 $ERPdns= array('phptype'  => 'pgsql',
36                'username' => $ERPuser,
37                'password' => $ERPpass,
38                'hostspec' => $ERPhost,
39                'database' => $ERPdbname,
40                'port'     => $ERPport);
41
42 $SHOPdns=array('phptype'  => 'mysql',
43                'username' => $SHOPuser,
44                'password' => $SHOPpass,
45                'hostspec' => $SHOPhost,
46                'database' => $SHOPdbname,
47                'port'     => $SHOPport,
48                'AutoCommit' => 0);
49
50 /****************************************************
51 * Debugmeldungen in File schreiben
52 ****************************************************/
53 if ($debug) { $log=fopen("tmp/shop.log","a"); } // zum Debuggen
54 else { $log=false; };
55
56 /****************************************************
57 * Shopverbindung aufbauen
58 ****************************************************/
59 $shop=DB::connect($SHOPdns);
60 if (!$shop) shopFehler("",$shop->getDebugInfo());
61 if (DB::isError($shop)) {
62         $nun=date("Y-m-d H:i:s");
63         if ($log) fputs($log,$nun.": Shop-Connect\n");
64         shopFehler("",$shop->getDebugInfo());
65         die ($shop->getDebugInfo());
66 };
67
68 /****************************************************
69 * ERPverbindung aufbauen
70 ****************************************************/
71 $erp=DB::connect($ERPdns);
72 if (!$erp) shopFehler("",$erp->getDebugInfo());
73 if (DB::isError($erp)) {
74         $nun=date("Y-m-d H:i:s");
75         if ($log) fputs($log,$nun.": ERP-Connect\n");
76         shopFehler("",$erp->getDebugInfo());
77         die ($erp->getDebugInfo());
78 } else {
79         $erp->autoCommit(true);
80 };
81
82
83 /****************************************************
84 * SQL-Befehle absetzen
85 ****************************************************/
86 function query($db,$sql,$function="--") {
87         $nun=date("d.m.y H:i:s");
88         //if ($db<>"shop") { echo "$sql!$db!<br>"; flush(); };
89         if ($GLOBALS["log"]) fputs($GLOBALS["log"],$nun.": ".$function."\n".$sql."\n");
90         $rc=$GLOBALS[$db]->query($sql);
91         if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rc,true)."\n");
92         if ($rc!==1) {
93                 return -99;
94         } else {
95                 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rs,true)."\n");
96                 return true;
97         }
98 }
99
100 /****************************************************
101 * Datenbank abfragen
102 ****************************************************/
103 function getAll($db,$sql,$function="--") {
104         $nun=date("d.m.y H:i:s");
105         if ($GLOBALS["log"]) fputs($GLOBALS["log"],$nun.": ".$function."\n".$sql."\n");
106         $rs=$GLOBALS[$db]->getAll($sql,DB_FETCHMODE_ASSOC);
107         if ($rs["message"]<>"") {
108                 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rs,true)."\n");
109                 return false;
110         } else {
111                 if ($GLOBALS["log"]) fputs($GLOBALS["log"],print_r($rs,true)."\n");
112                 return $rs;
113         }
114 }
115
116 /****************************************************
117 * shopFehler
118 * in: sql,err = string
119 * out:
120 * Fehlermeldungen ausgeben
121 *****************************************************/
122 function shopFehler($sql,$err) {
123 global $showErr;
124         if ($showErr)
125                 echo "</td></tr></table><font color='red'>$sql : $err</font><br>";
126 }
127
128 /****************************************************
129 * Nächste Auftragsnummer (ERP) holen
130 ****************************************************/
131 function getNextAnr() {
132         $sql="select * from defaults";
133         $sql1="update defaults set sonumber=";
134         $rs2=getAll("erp",$sql,"getNextAnr");
135         if ($rs2[0]["sonumber"]) {
136                 $auftrag=$rs2[0]["sonumber"]+1;
137                 $rc=query("erp",$sql1.$auftrag,"getNextAnr");
138                 if ($rc === -99) {
139                         echo "Kann keine Auftragsnummer erzeugen - Abbruch";
140                         exit();
141                 }
142                 return $auftrag;
143         } else {
144                 return false;
145         }
146 }
147
148 /****************************************************
149 * Nächste Kundennummer (ERP) holen
150 ****************************************************/
151 function getNextKnr() {
152         $sql="select * from defaults";
153         $sql1="update defaults set customernumber='";
154         $rs2=getAll("erp",$sql,"getNextKnr");
155         if ($rs2[0]["customernumber"]) {
156                 $kdnr=$rs2[0]["customernumber"]+1;
157                 $rc=query("erp",$sql1.$kdnr."'","getNextKnr");
158                 if ($rc === -99) {
159                         echo "Kann keine Kundennummer erzeugen - Abbruch";
160                         exit();
161                 }
162                 return $kdnr;
163         } else {
164                 return false;
165         }
166 }
167
168
169 $buchungsgruppen=array();
170 $warengruppen=array();
171
172 function getBugru() {
173         $sql ="select B.id,tax.rate from buchungsgruppen B left join chart on income_accno_id_0=chart.id left join taxkeys T on ";
174         $sql.="T.chart_id=income_accno_id_0 left join tax on tax.id=T.tax_id where T.startdate<=now()";
175         $rs=getAll("erp",$sql,"getBugru");
176         if ($rs) foreach ($rs as $row) {
177                 $steuer=sprintf("%0.2f",$row["rate"]*100);
178                 $GLOBALS["buchungsgruppen"][$steuer]=$row["id"];
179         }
180 }
181
182 $wg=1000;
183
184 getBugru();
185 ?>