コード例 #1
0
        /// <summary>
        /// Create a new Customer in NopCommerce from a Mercator CLI
        /// </summary>
        /// <param name="c"></param>
        /// <param name="context"></param>
        private void createNopClient(CLI c)
        {
            //Check if email/username already exists
            CheckEmailExistence(c);
            //checkUsernameExistence(c);

            //Generation random password, or use C_ID?
            //string password = PasswordManagement.CreatePassword();
            string password = c.C_ID;

            //Add address
            string  addressResult = WebService.Post(WebApiEntities.ADDRESS, ParserJSon.ParseAddressToJson(c, OptionsMercator).ToString());
            JObject newAddress    = JObject.Parse(addressResult);
            int     addressId     = (int)newAddress["Id"];


            string clientJson = ParserJSon.ParseClientToJson(c.C_EMAIL.TrimEnd(), DateTimeOffset.Now, password, /*mercatorPrefix+*/ c.C_ID.TrimEnd(), c.C_MODIFTAG).ToString();

            string clientResult = WebService.Post(ENTITY, clientJson);

            JObject newCli = JObject.Parse(clientResult);
            int     id     = (int)newCli["Id"];

            c.C_ID_WEB = id;

            //Add TVA Number (if not_empty)
            //VatNumberStatudId : 10 = empty, 20 = valid
            if (!String.IsNullOrWhiteSpace(c.C_NUM_TVA))
            {
                List <JObject> entries = ParserJSon.ParseClientCompanyToJson(id, c.C_NUM_TVA.TrimEnd(), c.C_NOM.TrimEnd());
                foreach (JObject e in entries)
                {
                    WebService.Post(WebApiEntities.GENERIC_ATTRIBUTE, e.ToString());
                }
            }

            JObject linkCliToAddress = new JObject();

            linkCliToAddress.Add("@odata.id", WebService.GetStoreAddress() + new UrlBuilder("Address").Id(addressId).BuildQuery());
            //link address
            WebService.Put(urlBuilder.BuildQueryRef(c.C_ID_WEB, "Addresses"), linkCliToAddress.ToString());

            //link address as billing address
            //WebService.Put(String.Format(WebServiceUrls.LINK_CUSTOMER_TO_BILLING_ADDRESS, c.C_ID_WEB.ToString()), linkCliToAddress.ToString());

            //link role
            //3 = Registered (so that he can connect)
            JObject linkCliToCR = new JObject();

            linkCliToCR.Add("@odata.id", WebService.GetStoreAddress() + new UrlBuilder("CustomerRole").Id(3).BuildQuery());
            WebService.Put(urlBuilder.BuildQueryRef(c.C_ID_WEB, "CustomerRoles"), linkCliToCR.ToString());

            JObject genericAttributeMercatorId = ParserJSon.GetGenericAttributeJSon(id, ENTITY, KEY_MERCATORID, c.C_ID);

            WebService.Post(WebApiEntities.GENERIC_ATTRIBUTE, genericAttributeMercatorId);
            JObject genericAttributeModiftagId = ParserJSon.GetGenericAttributeJSon(id, ENTITY, KEY_MODIFTAG, c.C_MODIFTAG.ToString());

            WebService.Post(WebApiEntities.GENERIC_ATTRIBUTE, genericAttributeModiftagId);
            //From Mercator, tarif indiqué dans fiche article
            //string roleIdResult = WebService.Get(String.Format(WebServiceUrls.CUSTOMER_ROLE_BY_SYSTEM_NAME, WebServiceUrls.TARIF_ + c.C_TARIF));
            //JToken[] roleId = ParserJSon.ParseResultToJTokenList(roleIdResult);

            //linkCliToCR = "{\"@odata.id\":\"" + ConfigurationManager.AppSettings["storeAddress"] + String.Format(WebServiceUrls.CUSTOMER_ROLE_ID, roleId.FirstOrDefault()["Id"].ToString()) + "\"}";
            //WebService.Put(String.Format(WebServiceUrls.LINK_CUSTOMER_TO_CR_WS, c.C_ID_WEB.ToString()), linkCliToCR);
            SigCli sigCli = new SigCli();

            sigCli.Read(c.C_ID);

            sigCli.SetValueForField("C_ID_WEB", c.C_ID_WEB);
            sigCli.Update();

            //context.SaveChanges();
        }