Exemplo n.º 1
0
 /// <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;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Gets the form info.
 /// </summary>
 /// <param name="args">The args.</param>
 /// <returns></returns>
 public static Dictionary<string, object> GetFormInfo( Dictionary<string, object> args )
 {
     ( "FUNCTION /w SP,fileSystem getFormInfo" ).Debug( 10 );
     Dictionary<string, object> j = new Dictionary<string, object>();
     Commerce.Item item = null;
     string formName = "";
     if( args.ContainsKey( "formName" ) ) { /* lookup using the form name */
         formName = Convert.ToString( args[ "formName" ] );
     } else if( args.ContainsKey( "cartId" ) ) { /* if this order has been placed lookup using the stored form */
         formName = null;
     } else if( args.ContainsKey( "itemNumber" ) ) { /* lookup using the items form */
         item = Main.Site.Items.List.Find( delegate( Commerce.Item b ) {
             if( Convert.ToString( args[ "itemNumber" ] ).ToLower() == b.ItemNumber.ToLower() ) {
                 return true;
             }
             return false;
         } );
         formName = item.FormName;
     }
     if( formName != null ) {
         Commerce.Form form = new Commerce.Form( item, Main.PhysicalApplicationPath + "forms\\" + formName.Trim().ToLower() );
         if( form != null ) {
             j.Add( "name", form.Name );
             j.Add( "inputs", form.Inputs );
             j.Add( "HTML", form.Html );
             j.Add( "error", 0 );
             j.Add( "description", "" );
         } else {
             ( "getFormInfo error -1 ==> form not found:" + formName.Trim() ).Debug( 2 );
             j.Add( "error", -1 );
             j.Add( "description", "Form not found" );
         }
     } else if( args.ContainsKey( "cartId" ) ) {
         string sourceCode = "";
         Guid cartId = new Guid( Convert.ToString( args[ "cartId" ] ) );
         Guid sessionId = Guid.Empty;
         if(args.ContainsKey("sessionId")) {
             sessionId = new Guid(Convert.ToString(args["sessionId"]));
         }
         using(SqlConnection cn = Site.CreateConnection(true, true)) {
             cn.Open();
             /* check if this is an order or a cart item */
             bool existingOrder = false;
             using(SqlCommand cmd = new SqlCommand(@"select 0 from cart with (nolock) where cartId = @cartId and not orderId = -1", cn)) {
                 cmd.Parameters.Add("@cartId", SqlDbType.UniqueIdentifier).Value = cartId;
                 using(SqlDataReader d = cmd.ExecuteReader()) {
                     existingOrder = d.HasRows;
                 }
             }
             if(!existingOrder && sessionId != Guid.Empty) {
                 Session session = new Session(Main.Site, sessionId, cn, null);
                 session.Cart.Refresh();
                 /* find the selected id */
                 Commerce.CartItem cartItem = session.Cart.Items.Find(delegate(Commerce.CartItem it) {
                     return it.CartId == cartId;
                 });
                 if(cartItem == null) {
                     j.Add("error", -1);
                     j.Add("description", "No data for cartId " + args["cartId"].ToString());
                     return j;
                 }
                 if(cartItem.Form == null) {
                     j.Add("error", 0);/* this isn't really an error becuase items might contain no form data */
                     j.Add("description", "No form data for cartId " + args["cartId"].ToString());
                 } else {
                     j.Add("name", cartItem.Form.Name);
                     j.Add("inputs", cartItem.Inputs);
                     j.Add("HTML", cartItem.HtmlWithValues);
                     j.Add("emptyHTML", cartItem.Item.Form.Html);
                     j.Add("error", 0);
                     j.Add("description", "");
                 }
                 return j;
             }else{
                 using(SqlCommand cmd = new SqlCommand("dbo.getOrderForm @cartId", cn)) {
                     cmd.Parameters.Add("@cartId", SqlDbType.UniqueIdentifier).Value = cartId;
                     formName = "";
                     string itemNumber = "";
                     using(SqlDataReader d = cmd.ExecuteReader()) {
                         if(d.HasRows) {
                             d.Read();
                             sourceCode = d.GetValue(0).ToString();
                             formName = d.GetValue(1).ToString();
                             itemNumber = d.GetValue(2).ToString();
                         }
                     }
                 }
                 /* find the order */
                 Commerce.Order order = Commerce.Order.GetOrderByCartId(cartId, cn, null);
                 /* find the line */
                 Commerce.Line line = order.Lines.Find(delegate(Commerce.Line l) {
                     return l.CartId == cartId;
                 });
                 /* return the data */
                 if(line.Form != null) {
                     j.Add("name", line.Form.Name);
                     j.Add("inputs", line.Form.Inputs);
                     j.Add("HTML", line.Form.HtmlWithValues());
                     j.Add("emptyHTML", line.Form.Html);
                     j.Add("error", 0);
                     j.Add("description", "");
                 } else {
                     j.Add("error", 0);/* this isn't really an error becuase items might contain no form data */
                     j.Add("description", "No form data for cartId " + args["cartId"].ToString());
                 }
             }
         }
     }
     return j;
 }