What I am working on...
I am just going to post that, and watch what happens. I will note that this is code that I just wrote, from scratch.
public class TaxCalculator
{
private string conStr;
private DataSet rates;
public TaxCalculator(string conStr)
{
this.conStr = conStr;
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblTxRtes", con))
{
rates = new DataSet();
new SqlDataAdapter(cmd).Fill(rates);
Log.Write("Read " + rates.Tables[0].Rows.Count + " rates from database");
if (rates.Tables[0].Rows.Count == 0)
{
MailMessage msg = new MailMessage("important@legacy.org", "joe@legacy.com");
msg.Subject = "NO RATES IN DATABASE!!!!!";
msg.Priority = MailPriority.High;
new SmtpClient("mail.legacy.com", 9089).Send(msg);
Log.Write("No rates for taxes found in " + conStr);
throw new ApplicationException("No rates, Joe forgot to load the rates AGAIN!");
}
}
}
}
public bool Process(XmlDocument transaction)
{
try
{
Hashtable tx2tot = new Hashtable();
foreach (XmlNode o in transaction.FirstChild.ChildNodes)
{
restart:
if (o.Attributes["type"].Value == "2")
{
Log.Write("Type two transaction processing");
decimal total = decimal.Parse(o.Attributes["tot"].Value);
XmlAttribute attribute = transaction.CreateAttribute("tax");
decimal r = -1;
foreach (DataRow dataRow in rates.Tables[0].Rows)
{
if ((string)dataRow[2] == o.SelectSingleNode("//cust-details/state").Value)
{
r = decimal.Parse(dataRow[2].ToString());
}
}
Log.Write("Rate calculated and is: " + r);
o.Attributes.Append(attribute);
if (r == -1)
{
MailMessage msg = new MailMessage("important@legacy.org", "joe@legacy.com");
msg.Subject = "NO RATES FOR " + o.SelectSingleNode("//cust-details/state").Value + " TRANSACTION !!!!ABORTED!!!!";
msg.Priority = MailPriority.High;
new SmtpClient("mail.legacy.com", 9089).Send(msg);
Log.Write("No rate for transaction in tranasction state");
throw new ApplicationException("No rates, Joe forgot to load the rates AGAIN!");
}
tx2tot.Add(o.Attributes["id"], total * r);
attribute.Value = (total * r).ToString();
}
else if (o.Attributes["type"].Value == "1")
{
//2006-05-02 just need to do the calc
decimal total = 0;
foreach (XmlNode i in o.ChildNodes)
{
total += ProductPriceByNode(i);
}
try
{
// 2007-02-19 not so simple, TX has different rule
if (o.SelectSingleNode("//cust-details/state").Value == "TX")
{
total *= (decimal)1.02;
}
}
catch (NullReferenceException)
{
XmlElement element = transaction.CreateElement("state");
element.Value = "NJ";
o.SelectSingleNode("//cust-details").AppendChild(element);
}
XmlAttribute attribute = transaction.CreateAttribute("tax");
decimal r = -1;
foreach (DataRow dataRow in rates.Tables[0].Rows)
{
if ((string)dataRow[2] == o.SelectSingleNode("//cust-details/state").Value)
{
r = decimal.Parse(dataRow[2].ToString());
}
}
if (r == -1)
{
MailMessage msg = new MailMessage("important@legacy.org", "joe@legacy.com");
msg.Subject = "NO RATES FOR " + o.SelectSingleNode("//cust-details/state").Value + " TRANSACTION !!!!ABORTED!!!!";
msg.Priority = MailPriority.High;
new SmtpClient("mail.legacy.com", 9089).Send(msg);
throw new ApplicationException("No rates, Joe forgot to load the rates AGAIN!");
}
attribute.Value = (total * r).ToString();
tx2tot.Add(o.Attributes["id"], total * r);
o.Attributes.Append(attribute);
}
else if (o.Attributes["type"].Value == "@")
{
o.Attributes["type"].Value = "2";
goto restart;
// 2007-04-30 some bastard from northwind made a mistake and they have 3 months release cycle, so we have to
// fix this because they won't until sep-07
}
else
{
throw new Exception("UNKNOWN TX TYPE");
}
}
SqlConnection con2 = new SqlConnection(conStr);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
con2.Open();
foreach (DictionaryEntry d in tx2tot)
{
cmd2.CommandText = "usp_TrackTxNew";
cmd2.Parameters.Add("cid", transaction.SelectSingleNode("//cust-details/@id").Value);
cmd2.Parameters.Add("tx", d.Key);
cmd2.Parameters.Add("tot", d.Value);
cmd2.ExecuteNonQuery();
}
con2.Close();
}
catch (Exception e)
{
if (e.Message == "UNKNOWN TX TYPE")
{
return false;
}
throw e;
}
return true;
}
private decimal ProductPriceByNode(XmlNode item)
{
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblProducts WHERE pid=" + item.Attributes["id"], con))
{
DataSet set = new DataSet();
new SqlDataAdapter(cmd).Fill(set);
return (decimal)set.Tables[0].Rows[0][4];
}
}
}
}