/// <summary> /// Raises the oncalculatediscount event. /// </summary> /// <param name="args">The <see cref="Rendition.CalculateDiscountEventArgs"/> instance containing the event data.</param> internal void raiseOnCalculateDiscount( CalculateDiscountEventArgs args ) { if( CalculatingDiscount != null ) { CalculatingDiscount( this, args ); }; }
/// <summary> /// Emptys cart in memory and then refreshes it from the database /// </summary> public void Refresh(SqlConnection cn, SqlTransaction trans) { string commandText = "dbo.getCart @sessionId"; SqlCommand cmd; if(cn == null) { cmd = new SqlCommand(commandText, Site.SqlConnection); } else { cmd = new SqlCommand(commandText, cn, trans); } cmd.Parameters.Add("@sessionId", SqlDbType.UniqueIdentifier).Value = new Guid(Session.Id.ToString()); this.EstShipTotal = 0; SqlDataReader raw; Guid crnt = Guid.Empty; Guid crnt_d = Guid.Empty; CartItem i = null; Address addr = null; Addresses.Clear(); bool shippingServiceWasSelected = false; decimal default_rate_cost = 0; using(raw = cmd.ExecuteReader()) { /* cartDetailId,cartId,itemnumber,price,qty, * i.addressId,value,inputName,o.estShipPrice,subTotal, * taxTotal,addtime,parentCartId */ Items.Clear(); while(raw.Read()) { if(crnt == Guid.Empty) { SubTotal = Math.Round(raw.GetDecimal(9), 2, MidpointRounding.AwayFromZero); TaxTotal = Math.Round(raw.GetDecimal(10), 2, MidpointRounding.AwayFromZero); }; if(crnt != raw.GetGuid(1)) { /* construct the form inputs */ i = new CartItem(raw.GetString(2), raw.GetGuid(1), raw.GetDecimal(3), raw.GetInt32(4), raw.GetGuid(5), raw.GetDateTime(11), Session); i.ParentCartId = raw.GetGuid(12); Items.Add(i); crnt = raw.GetGuid(1); } if(i.Item.Form != null) { Input f = new Input(raw.GetString(7), raw.GetString(6)); f.Id = raw.GetGuid(0); i.Inputs.Add(f); /* make a copy of the items form */ i.Form = new Form(i.Item, i.Item.Form.SourceCode, i.Item.Form.Name); i.Form.CartItem = i; } crnt_d = raw.GetGuid(1); } raw.Close(); } /* make a reference to this CartItem on the form for the form's event handler */ commandText = "dbo.getCartContacts @sessionId"; if(cn == null) { cmd = new SqlCommand(commandText, Site.SqlConnection); } else { cmd = new SqlCommand(commandText, cn, trans); } cmd.Parameters.Add("@sessionId", SqlDbType.UniqueIdentifier).Value = new Guid(Session.Id.ToString()); //ContactID, userID, FirstName, LastName, Address1, Address2, City, State, ZIP, Country, HomePhone, WorkPhone, //Email, SpecialInstructions, Comments, sessionID, sendShipmentUpdates, emailads, rate, dateCreated, Company, //rate,selected,shippingname,selectedZip,shipZone,estShipCost using(raw = cmd.ExecuteReader()) { while(raw.Read()) { /* the contact with the sessionId == contactId is the bill to Address */ if(crnt != raw.GetGuid(0)) { crnt = raw.GetGuid(0); addr = new Address( raw.GetGuid(0), raw.GetString(2), raw.GetString(3), raw.GetString(4), raw.GetString(5), raw.GetString(6), raw.GetString(7), raw.GetString(8), raw.GetString(9), raw.GetString(10), raw.GetString(11), raw.GetString(12), raw.GetString(13), raw.GetString(14), raw.GetBoolean(16), raw.GetBoolean(17), Main.Site.Rates.List.Find(delegate(Rate rate) { return rate.Id == raw.GetInt32(18); }), raw.GetDateTime(19), raw.GetString(20) ); Addresses.Add(addr); } /* this goes outside of the "crnt!=raw.GetGuid(0)" condition above */ Rate r = Main.Site.Rates.List.Find(delegate(Rate rate) { return rate.Id == raw.GetInt32(33); }); r.Selected = raw.GetBoolean(55); r.ShipZone = raw.GetInt32(47); /* TODO event handler calculate shipping */ r.EstShippingCost = Math.Round(raw.GetDecimal(26), 2); if(!addr.Rates.Contains(r)) { addr.Rates.Add(r); } if(r.Id == Main.Site.default_rateId) { /* get the price for the default method, in case no rate, or an invalid/disabled rate was selected */ default_rate_cost = Math.Round(raw.GetDecimal(26), 2); } if(r.Selected && addr.Id != Session.Id) { shippingServiceWasSelected = true; EstShippingCost = r.EstShippingCost; this.EstShipTotal = EstShippingCost; } } raw.Close(); } cmd.Dispose(); if(!shippingServiceWasSelected) { /* update with the default price now so the user doesn't see uncalculated shipping ever */ EstShippingCost = default_rate_cost; this.EstShipTotal += EstShippingCost; /* update this person to have the default rate selected */ commandText = "dbo.updateShippingMethod @addressId,@rateId"; if(cn == null) { cmd = new SqlCommand(commandText, Site.SqlConnection); } else { cmd = new SqlCommand(commandText, cn, trans); } cmd.Parameters.Add("@addressId", SqlDbType.UniqueIdentifier).Value = new Guid(Session.Id.ToString()); cmd.Parameters.Add("@rateId", SqlDbType.Int).Value = Main.Site.default_rateId; cmd.ExecuteNonQuery(); cmd.Dispose(); } CalculateDiscountEventArgs args = new CalculateDiscountEventArgs(this, Session.User, Session, null); try { Main.Site.raiseOnCalculateDiscount(args); } catch(Exception ex) { ("raiseOncalculatediscount exception => " + ex.Message).Debug(3); args.Discount = 0; } DiscountTotal = args.Discount; GrandTotal = EstShippingCost + SubTotal + TaxTotal - DiscountTotal; }