/// <summary> /// Raises the on line update event. /// </summary> /// <param name="args">The <see cref="Rendition.LineUpdateEventArgs"/> instance containing the event data.</param> internal void raiseOnLineUpdate(LineUpdateEventArgs args) { if(LineUpdated != null) { LineUpdated(this, args); }; }
/// <summary> /// Updates an item that is already in an order. /// </summary> /// <param name="args">The line arguments.</param> /// <returns>{error:0,desc:""}</returns> public static Dictionary<string, object> UpdateOrderItem(Dictionary<string, object> args) { ("FUNCTION /w SP updateOrderItem").Debug(10); Dictionary<string, object> j = new Dictionary<string, object>(); if(!(args.ContainsKey("cartId") && args.ContainsKey("sessionId"))) { j.Add("error", 1); j.Add("description", "key cartId or sessionId is missing"); return j; } /* get the form from the database */ string sourceCode = ""; string formName = ""; string itemNumber = ""; using(SqlConnection cn = Site.CreateConnection(true, true)) { cn.Open(); SqlTransaction trans = cn.BeginTransaction("updateOrderItem"); Guid cartId = new Guid(args["cartId"].ToString()); Commerce.Order order = Order.GetOrderByCartId(cartId, cn, trans); if(order == null) { throw new Exception("updateOrderItem => cartId does not resolve to an order => " + cartId.ToString()); } Commerce.Line line = order.Lines.Find(delegate(Commerce.Line li) { return li.CartId == cartId; }); if(line == null) { throw new Exception("updateOrderItem => cartId does not resolve to a line => " + cartId.ToString()); } using(SqlCommand cmd = new SqlCommand("dbo.getForm @cartId", cn, trans)) { cmd.Parameters.Add("@cartId", SqlDbType.UniqueIdentifier).Value = new Guid(cartId.ToString()); using(SqlDataReader d = cmd.ExecuteReader()) { if(d.HasRows) { d.Read(); sourceCode = d.GetString(0); formName = d.GetString(1); itemNumber = d.GetString(2); } } } Commerce.Form form = new Commerce.Form(Main.Site.Items.GetItemByItemNumber(itemNumber), sourceCode, formName); List<Commerce.Input> formInputs = form.Inputs; /* remove the existing cartdetail entries to make way for the new impoved cartdetail entries */ using(SqlCommand cmd = new SqlCommand(@"update cart set qty = @qty, price = @price where cartId = @cartId; delete from cartdetail where cartId = @cartId;", cn, trans)) { cmd.Parameters.Add("@cartId", SqlDbType.UniqueIdentifier).Value = new Guid(args["cartId"].ToString()); cmd.Parameters.Add("@qty", SqlDbType.VarChar).Value = args["qty"].ToString(); cmd.Parameters.Add("@price", SqlDbType.VarChar).Value = args["price"].ToString(); cmd.ExecuteNonQuery(); } for(int x = 0; line.Form.Inputs.Count > x; x++) { Commerce.Input i = line.Form.Inputs[x]; if(args.ContainsKey(i.Name)) { i.Value = Convert.ToString(args[i.Name]); } else if(args.ContainsKey(i.Id.EncodeXMLId())) { i.Value = Convert.ToString(args[i.Id.EncodeXMLId()]); } else { i.Value = ""; } i.Id = Guid.NewGuid(); form.Inputs.Find(delegate(Commerce.Input inp) { return inp.Name.l() == i.Name.l(); }).Value = i.Value; using(SqlCommand cmd = new SqlCommand("dbo.insertCartDetail @cartDetailId,@cartId,@inputName,@value,@sessionId;", cn, trans)) { cmd.Parameters.Add("@cartId", SqlDbType.UniqueIdentifier).Value = new Guid(args["cartId"].ToString()); cmd.Parameters.Add("@sessionId", SqlDbType.UniqueIdentifier).Value = new Guid(args["sessionId"].ToString()); cmd.Parameters.Add("@cartDetailId", SqlDbType.UniqueIdentifier).Value = new Guid(i.Id.ToString()); cmd.Parameters.Add("@inputName", SqlDbType.VarChar).Value = i.Name; cmd.Parameters.Add("@value", SqlDbType.VarChar).Value = i.Value; cmd.ExecuteNonQuery(); } } LineUpdateEventArgs lineArgs = new LineUpdateEventArgs(line, order, cn, trans); Main.Site.raiseOnLineUpdate(lineArgs); trans.Commit(); } j.Add("error", 0); j.Add("description", ""); return j; }