using System; using System.Collections.Generic; using System.Text; using De.Lambertz.Essentials; 6 namespace De.Lambertz.ODAL { public class WährungsSingelton : ISingelton { private static WährungsSingelton me = null; private String sessionId = ""; static readonly object padlock0 = new object(); //Threadsafe!! static readonly object padlock1 = new object(); //Threadsafe!! // LamDictionary> umrechnungen = null; LamDictionary geladenenFirmen = null; Dictionary druckZeichen = null; private WährungsSingelton() { umrechnungen = new LamDictionary>(); geladenenFirmen = new LamDictionary(); this.sessionId = KernParameterSingelton.GetInstance().SessionID; druckZeichen = new Dictionary(); druckZeichen.Add("EUR", "€"); druckZeichen.Add("USD", "$"); } private String getKey(int firmenNr, string firmenWährung) { return firmenNr.ToString("000") + firmenWährung; } public String DruckZeichenFür(String wkz) { String result = wkz; if (wkz == null) { wkz = "EUR"; } if (druckZeichen.ContainsKey(wkz)) { result = druckZeichen[wkz]; } return result; } public WährungsUmrechnung WährungsUmrechnungFür(int firmenNr, String wkz) { WährungsUmrechnung result = null; check(firmenNr); if (umrechnungen.ContainsKey(firmenNr)) { Dictionary temp = umrechnungen[firmenNr]; if (temp.ContainsKey(wkz)) { result = temp[wkz]; } } if(result == null) { throw new Exception("Keine Währung mit WKZ: " + wkz + " für Firma " + firmenNr.ToString()); } return result; } public string FirmenWährungFür(int firmenNr) { String result = null; check(firmenNr); try { Dictionary temp = umrechnungen[firmenNr]; foreach (WährungsUmrechnung wu in temp.Values) { result = wu.FirmenWährung; break; } } catch (Exception) { throw new Exception("Keine Firmenwährung für Firma " + firmenNr.ToString()); } return result; } /// /// Erste Währung ist immer Firmen-Währung /// /// /// public List WährungslisteFür(int firmenNr) { List result = new List(); check(firmenNr); Dictionary temp = umrechnungen[firmenNr]; bool firmenWährungGefunden = false; foreach (WährungsUmrechnung wu in temp.Values) { if (!result.Contains(wu)) { if (wu.FirmenWährung == wu.FremdWährung) { result.Insert(0, wu); firmenWährungGefunden = true; } else { result.Add(wu); } } } if (!firmenWährungGefunden && result.Count>0) { WährungsUmrechnung wu = new WährungsUmrechnung(); wu.FirmenNr = firmenNr; wu.FirmenWährung = result[0].FirmenWährung; wu.FirmenWährungsFaktor = result[0].FirmenWährungsFaktor; wu.FremdWährung = result[0].FirmenWährung; wu.FremdWährungsFaktor = result[0].FirmenWährungsFaktor; wu.Umrechnungsfaktor = 1; result.Insert(0, wu); } return result; } public double Umrechnen(double originalWert, int firmenNr, string startWährung, string ziehlWährung) { double result = originalWert; check(firmenNr); Dictionary z = umrechnungen[firmenNr]; if (!z.ContainsKey(ziehlWährung) && z.Count>0) { WährungsUmrechnung wu = new WährungsUmrechnung(); WährungsUmrechnung wu1 = null; foreach(WährungsUmrechnung x in z.Values) { wu1 = x; break; } if (ziehlWährung == wu1.FirmenWährung) { wu.FirmenNr = firmenNr; wu.FirmenWährung = wu1.FirmenWährung; wu.FirmenWährungsFaktor = wu1.FirmenWährungsFaktor; wu.FremdWährung = wu1.FirmenWährung; wu.FirmenWährungsFaktor = wu1.FirmenWährungsFaktor; wu.Umrechnungsfaktor = 1; z.Add(ziehlWährung, wu); } } if (z.ContainsKey(ziehlWährung)) { WährungsUmrechnung ziehl = z[ziehlWährung]; if (startWährung == null) { startWährung = ziehl.FirmenWährung; } if (startWährung == ziehl.FirmenWährung) { result = Math.Round(originalWert * ziehl.Umrechnungsfaktor * ziehl.FremdWährungsFaktor / ziehl.FirmenWährungsFaktor ,4); } else { if (z.ContainsKey(startWährung)) { WährungsUmrechnung start = z[startWährung]; //Erst in Firmenwährung umrechnen double temp = Math.Round(originalWert * start.FirmenWährungsFaktor / (start.Umrechnungsfaktor * start.FremdWährungsFaktor) , 4); //Dann in Ziehlwährung umrechnen result = Math.Round(temp * ziehl.Umrechnungsfaktor * ziehl.FremdWährungsFaktor / ziehl.FirmenWährungsFaktor, 4); } else { throw new Exception("Unmögliche Startwährung"); } } } else { throw new Exception("Unmögliche Ziehlwährung"); } return result; } private void check(int firmenNr) { lock (padlock1) { bool neuLaden = true; if (geladenenFirmen.ContainsKey(firmenNr)) { if (geladenenFirmen[firmenNr] > DateTime.Now) { neuLaden = false; } else { geladenenFirmen.Remove(firmenNr); umrechnungen.Remove(firmenNr); } } if (neuLaden) { //Dictionary liste = AS400DataLoader.GetWährungsUmrechnungen(firmenNr, this.sessionId); //geladenenFirmen.Add(firmenNr, EssentialTools.TagEnde(DateTime.Now)); //umrechnungen.Add(firmenNr, liste); } } } public static WährungsSingelton GetInstance() { lock (padlock0) { if (me == null) { me = new WährungsSingelton(); SingeltonManager.GetInstance().RegisterSingelton(me); } return me; } } void ISingelton.ManagedReset(string userName) { } void ISingelton.ManagedKill() { } } }