using System; using System.Collections.Generic; using System.Linq; using System.Web; using CodeCafe.Web; using System.Data.SqlClient; using System.Web.UI.WebControls; /// /// Class to handle all database transactions to the BasketItem table. /// Generated by Code Cafe Class Generator Version 1.0 /// Create date: 5/3/2011 2:42:08 PM /// public class clsBasketItem { /****************************************************************************************************/ MSSqlTools db = new MSSqlTools(); SqlDataReader rdr = null; SqlCommand cmd; public int bi_id; public int sb_id; public int pi_id; public float pi_price; public int bi_qty; public int bi_term; public bool IsRecurringPayment; //Please note this field is only populated if ListForEmail is called public int MaxTerm; //Please note this field is only populated if ListForEmail is called /****************************************************************************************************/ public clsBasketItem() { Clear(); } /****************************************************************************************************/ /// /// Clears all the public variables /// public void Clear() { bi_id = 0; sb_id = 0; pi_id = 0; pi_price = 0; bi_qty = 0; bi_term = 1; IsRecurringPayment = false; MaxTerm = 1; } /****************************************************************************************************/ /// /// Populates a drop down list with values from the Database /// /// /// The drop down list to populate all items in the list will be cleared /// /// /// The Select record ID of a list item /// /// /// Specifies the type of list /// public void List(DropDownList List, int SelID, ListTypes ListType) { List.Items.Clear(); switch (ListType) { case ListTypes.AllowNew: List.Items.Add(new ListItem("- Add New -", "0")); break; case ListTypes.AllowSelect: List.Items.Add(new ListItem("- Select -", "0")); break; case ListTypes.AllowNone: List.Items.Add(new ListItem("- None -", "0")); break; } try { db.DBase.Open(); cmd = new SqlCommand("BasketItem_List", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter Error = cmd.Parameters.AddWithValue("@ErrorCode", 0); Error.Direction = System.Data.ParameterDirection.Output; rdr = cmd.ExecuteReader(); while (rdr.Read()) { ListItem item = new ListItem(rdr["sb_id"].ToString(), rdr["bi_id"].ToString()); item.Selected = (int.Parse(rdr["bi_id"].ToString()) == SelID); List.Items.Add(item); } rdr.Close(); } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } } /****************************************************************************************************/ /// /// Loads record specified by ID and populates values into the public variables /// /// /// Specifies the Primary key value of the record to load /// /// /// True on successful load /// False if an error occurs /// public bool Load(int ContentID) { bool result = false; try { db.DBase.Open(); cmd = new SqlCommand("BasketItem_LoadByID", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ID", ContentID); SqlParameter Error = cmd.Parameters.AddWithValue("@ErrorCode", 0); Error.Direction = System.Data.ParameterDirection.Output; rdr = cmd.ExecuteReader(); if (rdr.Read()) { bi_id = db.ProcessField(rdr["bi_id"], 0); sb_id = db.ProcessField(rdr["sb_id"], 0); pi_id = db.ProcessField(rdr["pi_id"], 0); pi_price = db.ProcessField(rdr["pi_price"], 0); bi_qty = db.ProcessField(rdr["bi_qty"], 0); bi_term = db.ProcessField(rdr["bi_term"], 0); } rdr.Close(); result = true; } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Saves values contained in the public variables to a new record if Primary Key ID is 0 or updates /// an existing record if Primary Key ID > 0 /// /// /// The ID of record saved or updated. /// public int Save() { int result = 0; try { db.DBase.Open(); cmd = new SqlCommand("BasketItem_Save", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@bi_id", bi_id); cmd.Parameters.AddWithValue("@sb_id", sb_id); cmd.Parameters.AddWithValue("@pi_id", pi_id); cmd.Parameters.AddWithValue("@pi_price", pi_price); cmd.Parameters.AddWithValue("@bi_qty", bi_qty); cmd.Parameters.AddWithValue("@bi_term", bi_term); SqlParameter Saved = cmd.Parameters.AddWithValue("@SavedID", 0); Saved.Direction = System.Data.ParameterDirection.Output; cmd.ExecuteNonQuery(); result = (int)cmd.Parameters["@SavedID"].Value; } catch (Exception err) { Logger.Log(err); result = 0; } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Deletes the record specified by ID /// /// /// Specifies the Primary key value of the record to delete /// /// /// True on successful delete /// False if an error occurs /// public bool Delete(int ContentID) { bool result = false; try { db.DBase.Open(); cmd = new SqlCommand("BasketItem_Delete", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ID", ContentID); SqlParameter Error = cmd.Parameters.AddWithValue("@ErrorCode", 0); Error.Direction = System.Data.ParameterDirection.Output; cmd.ExecuteNonQuery(); result = true; } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } /****************************************************************************************************/ /// /// Lists the basket for email inclusion. /// /// The basket ID. /// public string ListForEmail(int BasketID, bool FullScreen) { string result = ""; //float total = 0; float totalOnceOff = 0; float totalRecurring = 0; try { db.DBase.Open(); cmd = new SqlCommand("BasketItem_ListByBasket", db.DBase); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@BasketID", BasketID); int width = 90; if (FullScreen) width = 100; float vat = Utils.StrToFloatDef(System.Configuration.ConfigurationManager.AppSettings.Get("VATAmount"), 1.14f); IsRecurringPayment = false; MaxTerm = 1; rdr = cmd.ExecuteReader(); if (rdr.HasRows) { result = ""; result += ""; while (rdr.Read()) { float price = db.ProcessField(rdr["pi_price"], 0); int qty = db.ProcessField(rdr["bi_qty"], 0); int term = db.ProcessField(rdr["bi_term"], 0); bool recurrs = db.ProcessField(rdr["pi_recurring"], false); result += ""; result += ""; result += ""; result += ""; } //result += ""; //result += ""; //result += ""; //result += ""; //result += ""; //result += ""; string OrderPrefix = ""; if (IsRecurringPayment) OrderPrefix = "Once-off "; if (totalOnceOff > 0) { result += ""; result += ""; result += ""; result += ""; result += ""; result += ""; } if (IsRecurringPayment) { result += ""; result += ""; result += ""; result += ""; result += ""; result += ""; result += ""; } result += "
ProductQtyPriceTotal
" + db.ProcessField(rdr["pi_name"], ""); if (recurrs && (term > 1)) { result += "
For a period of " + term + " months"; IsRecurringPayment = true; if (term > MaxTerm) MaxTerm = term; totalRecurring += (price * qty); } else totalOnceOff += (price * qty); result += "
" + qty + "R " + price.ToString("N2") + "R " + (price * qty).ToString("N2") + "
Sub Total (Excl. Vat):R " + total.ToString("N2") + "
Vat:R " + ((total * vat) - total).ToString("N2") + "
"; //if (IsRecurringPayment) result += "Monthly "; //result += "Order Total (Incl. Vat):R " + (total * vat).ToString("N2") + "
" + OrderPrefix + "Sub Total (Excl. Vat):R " + totalOnceOff.ToString("N2") + "
Vat:R " + ((totalOnceOff * vat) - totalOnceOff).ToString("N2") + "
"; result += OrderPrefix + "Order Total (Incl. Vat):R " + (totalOnceOff * vat).ToString("N2") + "
 
Monthly Sub Total (Excl. Vat):R " + totalRecurring.ToString("N2") + "
Vat:R " + ((totalRecurring * vat) - totalRecurring).ToString("N2") + "
"; result += "Monthly Order Total (Incl. Vat):R " + (totalRecurring * vat).ToString("N2") + "
"; } rdr.Close(); } catch (Exception err) { Logger.Log(err); } finally { db.DBase.Close(); } return result; } }