Esempio n. 1
0
        public void ProcessOrder()
        {
            OrderHeadCollection collection = this.FillCreateOrderData();

            try
            {
                //var result = PushOrderToSAP123(collection);

                // Call the SAP Method
                OrderHead orderHeadData = PushOrderToSAP(collection);


                // Check if Sales Doc is not Zero. Then Pass the Sales Doc
                // If the Sales Doc is Zero, then Log the message

                if (orderHeadData.ESalesdocument != "0")
                {
                    // Pass the Sales Document
                }
                else
                {
                    // Log the Message.
                    string message = orderHeadData.EText;
                }
            }
            catch (Exception ex)
            {
                // Need to log the exception details.

                throw ex;
            }
        }
Esempio n. 2
0
        // This method will be called by HttpClient which is only support by Framework 4.0
        private string PushOrderToSAP123(OrderHeadCollection collection)
        {
            string eSalesDocument = string.Empty;

            // Get Cookies and csrfToken
            if (string.IsNullOrEmpty(csrfToken))
            {
                GetCSRFToken();
            }

            var cookieContainer = new CookieContainer();

            using (var handler = new HttpClientHandler {
                CookieContainer = cookieContainer, Credentials = new NetworkCredential(sapUserName, sapPassword)
            })
                using (var client = new HttpClient(handler))
                {
                    client.BaseAddress = new Uri(sapCreateOrderURL);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Add("X-CSRF-Token", csrfToken);
                    cookieContainer.Add(client.BaseAddress, cookiestopass);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var httpContent = new StringContent(JsonConvert.SerializeObject(collection, settings)
                                                        , UnicodeEncoding.UTF8, "application/json");

                    var response = client.PostAsync(client.BaseAddress, httpContent).Result;

                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        // Get response data
                        var responseJsonData = response.Content.ReadAsStringAsync().Result;

                        // Deserialize the Response JSON data to Order data.
                        OrderHeadCollection responseCollection = JsonConvert.DeserializeObject <OrderHeadCollection>(responseJsonData);

                        // Get Sales Document
                        // Check the responseCollection if that is not null
                        eSalesDocument = responseCollection.d.ESalesdocument;
                    }
                    else if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        // Log for the bad request.
                        string errorMessage = response.Content.ReadAsStringAsync().Result;
                        //Log.Info(errorMessage);
                    }
                }

            return(eSalesDocument);
        }
Esempio n. 3
0
        private OrderHeadCollection FillCreateOrderData()
        {
            OrderHeadCollection collection = new OrderHeadCollection();

            collection.d = new OrderHead();

            // Fill Create Order Data.

            // Get the Order from Database.
            OrderHead orderdata = new OrderHead();

            // Order No , CustLineref and CustItemref needs to be unique to push Order to SAP.
            // ** OrderNo (mandatory)
            // ** SalesOrg (mandatory)
            // ** SoldTo (mandatory)
            //    ShipTo Address

            // Region - SalesOrgNumberProdCat **
            orderdata.Region = "NL068";
            // Order No Should be Unique **
            orderdata.Orderno = "15";
            // CUSTID - Sold to Number **
            orderdata.Custid = "1421200";
            // Ship to Number
            orderdata.Ardrno = "";

            orderdata.Orderdate    = "00000000";
            orderdata.Deliverydate = "00000000";

            // Should be Unique
            orderdata.Custlineref = "Ref 123 with error text test"; // This shoudl be testing error purpose

            orderdata.Goodsaddr1 = "";
            orderdata.Goodsaddr2 = "";
            orderdata.Goodsaddr3 = "";
            orderdata.Goodsaddr4 = "";
            orderdata.Autlf      = false;

            // If the Unloading Point is valid then add below EMPst & Ablad
            orderdata.Empst = "UNLOADING POINT 1";
            orderdata.Ablad = "UNLOADING POINT 1";

            orderdata.Inco1          = "";
            orderdata.Zfig           = false;
            orderdata.Type           = "";
            orderdata.Id             = "";
            orderdata.Number         = "000";
            orderdata.Message        = "";
            orderdata.LogNo          = "";
            orderdata.LogMsgNo       = "000000";
            orderdata.MessageV1      = "";
            orderdata.MessageV2      = "";
            orderdata.MessageV3      = "";
            orderdata.MessageV4      = "";
            orderdata.ESalesdocument = "";
            orderdata.EText          = "";

            // Fill Head2ItemNav
            // Loop all Order Item and add into Head2IteamsNav
            orderdata.Head2ItemsNav = new List <Head2ItemsNav>();

            Head2ItemsNav orderItem = new Head2ItemsNav();

            // Basic Fields
            orderItem.Lineno   = "000000";
            orderItem.Artno    = "725222";
            orderItem.Ordergty = "1";
            orderItem.Qtytype  = "TRP";

            // CustItemref should be Unique - Residence Name
            orderItem.Custitemref = "TESToDATA 15";

            orderItem.CondValue = "0.000000000";
            orderItem.Currency  = "";
            orderItem.CondUnit  = "";
            orderItem.CondPUnt  = "0";
            orderdata.Head2ItemsNav.Add(orderItem);

            // Fill Order Note Item
            // Loop on Order Note to add into Head2TextNav.

            orderdata.Head2TextsNav = new List <Head2TextsNav>();
            Head2TextsNav noteItem = new Head2TextsNav();

            noteItem.Tdline   = "Test Note";
            noteItem.Tdformat = "/";
            orderdata.Head2TextsNav.Add(noteItem);

            // Push Create Order to collection
            collection.d = orderdata;

            //var jsonData = JsonConvert.SerializeObject(collection, settings);
            return(collection);
        }
Esempio n. 4
0
        // This method is using HttpWebRequest to POST Json Data.
        private OrderHead PushOrderToSAP(OrderHeadCollection collection)
        {
            OrderHead orderHeadData = new OrderHead();

            try
            {
                // Get Cookies and csrfToken
                if (string.IsNullOrEmpty(csrfToken))
                {
                    GetCSRFToken();
                }

                // Using the HTTPWebRequest.
                var            cookieContainer = new CookieContainer();
                HttpWebRequest req             = (HttpWebRequest)WebRequest.Create(sapCreateOrderURL);

                // Add Credentional, Header, Token & Cookies
                req.Credentials = new NetworkCredential(sapUserName, sapPassword);
                req.Method      = "POST";
                req.Headers.Add("X-CSRF-Token", csrfToken);
                req.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                req.Accept            = "application/json";
                req.ContentType       = "application/json";
                cookieContainer.Add(new Uri(sapCreateOrderURL), cookiestopass);
                req.CookieContainer = cookieContainer;

                // Get JSON Data from Entity
                var jsonData = JsonConvert.SerializeObject(collection, settings);

                // Enclding JSON Data
                UTF8Encoding encoding = new UTF8Encoding();
                Byte[]       bytes    = encoding.GetBytes(jsonData);

                Stream newStream = req.GetRequestStream();
                newStream.Write(bytes, 0, bytes.Length);
                newStream.Close();

                using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                {
                    // Get the response Content from Webresponse
                    var rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();

                    if (response.StatusCode == HttpStatusCode.Created)
                    {
                        // Deserialize the Response JSON data to Order data.
                        OrderHeadCollection responseCollection = JsonConvert.DeserializeObject <OrderHeadCollection>(rawJson);

                        if (responseCollection != null)
                        {
                            orderHeadData = responseCollection.d;
                        }
                    }
                    else if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        // Log for the bad request.
                        string errorMessage = rawJson;
                        //Log.Info(errorMessage);
                    }
                }
            }
            catch (WebException ex)
            {
                var exceptionJson = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

                // Deserialize the Response JSON data to Error Model
                ErrorMessage errorMessage = JsonConvert.DeserializeObject <ErrorMessage>(exceptionJson);

                // Log
                string message = errorMessage.error.message.value;
                //Log.Info(errorMessage);
            }
            catch (Exception ex)
            {
                // Log the message in case any exception
                //Log.Info(ex.Message);
            }

            return(orderHeadData);
        }