Ejemplo n.º 1
0
        public static NavSalesOrder StartOrderUpdationProcessForNAV(ActiveUser activeUser, List <long> jobIdList, string soNumber, string poNumber, string navAPIUrl, string navAPIUserName, string navAPIPassword, string vendorNo, bool electronicInvoice, List <SalesOrderItem> salesOrderItemRequest)
        {
            string dimensionCode       = string.Empty;
            string divisionCode        = string.Empty;
            bool   allLineItemsUpdated = true;
            string proFlag             = null;
            NavSalesOrderRequest navSalesOrderRequest = _commands.GetSalesOrderCreationData(activeUser, jobIdList, Entities.EntitiesAlias.SalesOrder);

            if (navSalesOrderRequest == null)
            {
                return(null);
            }
            var dimensions = CommonCommands.GetSalesOrderDimensionValues(navAPIUserName, navAPIPassword, navAPIUrl);

            if (dimensions != null && dimensions.NavSalesOrderDimensionValues != null && dimensions.NavSalesOrderDimensionValues.Count > 0)
            {
                divisionCode = dimensions.NavSalesOrderDimensionValues.Where(x => !string.IsNullOrEmpty(x.Dimension_Code) && x.Dimension_Code.ToUpper() == "DIVISION" && x.ERPId == navSalesOrderRequest.Sell_to_Customer_No).Any()
                                        ? dimensions.NavSalesOrderDimensionValues.Where(x => !string.IsNullOrEmpty(x.Dimension_Code) && x.Dimension_Code.ToUpper() == "DIVISION" && x.ERPId == navSalesOrderRequest.Sell_to_Customer_No).FirstOrDefault().Code
                                        : string.Empty;

                if (!string.IsNullOrEmpty(navSalesOrderRequest.Ship_from_City) && !string.IsNullOrEmpty(navSalesOrderRequest.Ship_from_County))
                {
                    string dimensionMatchCode = navSalesOrderRequest.Ship_from_City.Length >= 3 ? string.Format("{0}{1}", navSalesOrderRequest.Ship_from_City.Substring(0, 3), navSalesOrderRequest.Ship_from_County) : string.Empty;
                    if (!string.IsNullOrEmpty(dimensionMatchCode))
                    {
                        dimensionCode = dimensions.NavSalesOrderDimensionValues.Where(codeMatch => !string.IsNullOrEmpty(codeMatch.Code) && codeMatch.Code.Length > 4 && codeMatch.Code.Substring(codeMatch.Code.Length - 5).ToUpper() == dimensionMatchCode.ToUpper()).Any() ?
                                        dimensions.NavSalesOrderDimensionValues.Where(codeMatch => !string.IsNullOrEmpty(codeMatch.Code) && codeMatch.Code.Length > 4 && codeMatch.Code.Substring(codeMatch.Code.Length - 5).ToUpper() == dimensionMatchCode.ToUpper()).FirstOrDefault().Code : dimensionCode;
                    }
                }
            }

            navSalesOrderRequest.Shortcut_Dimension_2_Code = dimensionCode;
            navSalesOrderRequest.Shortcut_Dimension_1_Code = divisionCode;
            navSalesOrderRequest.Electronic_Invoice        = electronicInvoice;
            NavSalesOrder navSalesOrderResponse = UpdateSalesOrderForNAV(activeUser, navSalesOrderRequest, navAPIUrl, navAPIUserName, navAPIPassword, soNumber);

            if (navSalesOrderResponse != null && !string.IsNullOrWhiteSpace(navSalesOrderResponse.No))
            {
                salesOrderItemRequest.ForEach(x => x.Document_No = navSalesOrderResponse.No);
                //Task.Run(() =>
                //{
                UpdateSalesOrderItemDetails(activeUser, jobIdList, navAPIUrl, navAPIUserName, navAPIPassword, dimensionCode, divisionCode, navSalesOrderResponse.No, ref allLineItemsUpdated, ref proFlag, electronicInvoice, salesOrderItemRequest);
                //});
            }

            return(navSalesOrderResponse);
        }
Ejemplo n.º 2
0
        public static NavPurchaseOrder UpdatePurchaseOrderForNAV(ActiveUser activeUser, List <long> jobIdList, string poNumer, string navAPIUrl, string navAPIUserName, string navAPIPassword, bool electronicInvoice, List <PurchaseOrderItem> purchaseOrderItemRequest)
        {
            string           navPurchaseOrderJson     = string.Empty;
            string           dimensionCode            = string.Empty;
            string           divisionCode             = string.Empty;
            NavPurchaseOrder navPurchaseOrderResponse = null;
            string           proFlag = null;

            Newtonsoft.Json.Linq.JObject jsonObject = null;
            string serviceCall = string.Format("{0}/PurchaseOrder('Order', '{1}')", navAPIUrl, poNumer);

            try
            {
                NavPurchaseOrder        existingSalesOrderData  = GetPurchaseOrderForNAV(navAPIUrl, navAPIUserName, navAPIPassword, poNumer);
                NavPurchaseOrderRequest navPurchaseOrderRequest = _purchaseCommands.GetPurchaseOrderCreationData(activeUser, jobIdList, Entities.EntitiesAlias.PurchaseOrder);
                if (navPurchaseOrderRequest == null)
                {
                    return(null);
                }
                var dimensions = CommonCommands.GetSalesOrderDimensionValues(navAPIUserName, navAPIPassword, navAPIUrl);
                if (dimensions != null && dimensions.NavSalesOrderDimensionValues != null && dimensions.NavSalesOrderDimensionValues.Count > 0)
                {
                    divisionCode = dimensions.NavSalesOrderDimensionValues.Where(x => !string.IsNullOrEmpty(x.Dimension_Code) && x.Dimension_Code.ToUpper() == "DIVISION" && x.ERPId == navPurchaseOrderRequest.Sell_to_Customer_No).Any()
                                                ? dimensions.NavSalesOrderDimensionValues.Where(x => !string.IsNullOrEmpty(x.Dimension_Code) && x.Dimension_Code.ToUpper() == "DIVISION" && x.ERPId == navPurchaseOrderRequest.Sell_to_Customer_No).FirstOrDefault().Code
                                                : string.Empty;

                    if (!string.IsNullOrEmpty(navPurchaseOrderRequest.Ship_from_City) && !string.IsNullOrEmpty(navPurchaseOrderRequest.Ship_from_County))
                    {
                        string dimensionMatchCode = navPurchaseOrderRequest.Ship_from_City.Length >= 3 ? string.Format("{0}{1}", navPurchaseOrderRequest.Ship_from_City.Substring(0, 3), navPurchaseOrderRequest.Ship_from_County) : string.Empty;
                        if (!string.IsNullOrEmpty(dimensionMatchCode))
                        {
                            dimensionCode = dimensions.NavSalesOrderDimensionValues.Where(codeMatch => !string.IsNullOrEmpty(codeMatch.Code) && codeMatch.Code.Length > 4 && codeMatch.Code.Substring(codeMatch.Code.Length - 5).ToUpper() == dimensionMatchCode.ToUpper()).Any() ?
                                            dimensions.NavSalesOrderDimensionValues.Where(codeMatch => !string.IsNullOrEmpty(codeMatch.Code) && codeMatch.Code.Length > 4 && codeMatch.Code.Substring(codeMatch.Code.Length - 5).ToUpper() == dimensionMatchCode.ToUpper()).FirstOrDefault().Code : dimensionCode;
                        }
                    }
                }

                navPurchaseOrderRequest.Shortcut_Dimension_2_Code = dimensionCode;
                navPurchaseOrderRequest.Shortcut_Dimension_1_Code = divisionCode;
                navPurchaseOrderRequest.Electronic_Invoice        = electronicInvoice;
                NetworkCredential myCredentials = new NetworkCredential(navAPIUserName, navAPIPassword);
                HttpWebRequest    request       = (HttpWebRequest)WebRequest.Create(serviceCall);
                request.Credentials = myCredentials;
                request.KeepAlive   = false;
                request.ContentType = "application/json";
                request.Method      = "PATCH";
                request.Headers.Add(HttpRequestHeader.IfMatch, existingSalesOrderData.DataETag);
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    navPurchaseOrderJson = JsonConvert.SerializeObject(navPurchaseOrderRequest);
                    jsonObject           = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(navPurchaseOrderJson);
                    jsonObject.Property("Ship_from_City").Remove();
                    jsonObject.Property("Ship_from_County").Remove();
                    navPurchaseOrderJson = jsonObject.ToString();
                    streamWriter.Write(navPurchaseOrderJson);
                }

                WebResponse response = request.GetResponse();

                using (Stream navPurchaseOrderResponseStream = response.GetResponseStream())
                {
                    using (TextReader navPurchaseOrderReader = new StreamReader(navPurchaseOrderResponseStream))
                    {
                        string responceString = navPurchaseOrderReader.ReadToEnd();

                        using (var stringReader = new StringReader(responceString))
                        {
                            navPurchaseOrderResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <NavPurchaseOrder>(responceString);
                        }
                    }
                }

                if (navPurchaseOrderResponse != null && !string.IsNullOrWhiteSpace(navPurchaseOrderResponse.No))
                {
                    if (purchaseOrderItemRequest != null && purchaseOrderItemRequest.Count > 0)
                    {
                        purchaseOrderItemRequest.ForEach(x => x.Document_No = navPurchaseOrderResponse.No);
                    }

                    UpdateLineItemInformationForPurchaseOrder(activeUser, jobIdList, navAPIUrl, navAPIUserName, navAPIPassword, dimensionCode, divisionCode, navPurchaseOrderResponse.No, out proFlag, electronicInvoice, purchaseOrderItemRequest);
                }
            }
            catch (Exception exp)
            {
                proFlag = Entities.ProFlag.H.ToString();
                _logger.Log(exp, string.Format("Error is occuring while Updating the purchase order: Request Url is: {0}, Request body json was {1}", serviceCall, navPurchaseOrderJson), string.Format("Purchase order updation for JobId: {0}", Newtonsoft.Json.JsonConvert.SerializeObject(jobIdList)), LogType.Error);
            }

            _commands.UpdateJobProFlag(activeUser, proFlag, jobIdList, Entities.EntitiesAlias.PurchaseOrder);
            return(navPurchaseOrderResponse);
        }