public void SaveOrderHistoryAsConfirmation(OrderHistoryFile histFile)
        {
            NewRelic.Api.Agent.NewRelic.AddCustomParameter("ControlNumber", histFile.Header.ControlNumber);
            NewRelic.Api.Agent.NewRelic.AddCustomParameter("CustomerNumber", histFile.Header.CustomerNumber);
            NewRelic.Api.Agent.NewRelic.AddCustomParameter("BranchId", histFile.Header.BranchId);

            if (histFile.Header.OrderSystem == Core.Enumerations.Order.OrderSource.Entree)
            {
                ConfirmationFile confirmation = histFile.ToConfirmationFile();
                PurchaseOrder    po           = GetCsPurchaseOrderByNumber(histFile.Header.ControlNumber);

                if (po != null)
                {
                    // need to save away pre and post status info, then if different, add something to the messaging
                    LineItem[] currLineItems = new LineItem[po.LineItemCount];
                    LineItem[] origLineItems = new LineItem[po.LineItemCount];
                    po.OrderForms[0].LineItems.CopyTo(currLineItems, 0);
                    po.OrderForms[0].LineItems.CopyTo(origLineItems, 0);
                    string originalStatus = po.Status;

                    SetCsLineInfo(currLineItems, confirmation);

                    SetCsHeaderInfo(confirmation, po, currLineItems);

                    po.Save();
                }
            }
        }
        private EF.OrderHistoryHeader FindHeader(OrderHistoryFile currentFile)
        {
            // first attempt to find the order, look by confirmation number
            EF.OrderHistoryHeader header = null;

            if (!String.IsNullOrEmpty(currentFile.Header.ControlNumber) && !String.IsNullOrEmpty(currentFile.Header.OrderSystem.ToShortString()))
            {
                header = _headerRepo.ReadByConfirmationNumber(currentFile.Header.ControlNumber, currentFile.Header.OrderSystem.ToShortString()).FirstOrDefault();
            }

            // second attempt to find the order, look by invioce number
            if (header == null && !currentFile.Header.InvoiceNumber.Equals("Processing"))
            {
                header = _headerRepo.ReadForInvoice(currentFile.Header.BranchId, currentFile.Header.InvoiceNumber).FirstOrDefault();
            }

            // last ditch effort is to create a new header
            if (header == null)
            {
                header = new EF.OrderHistoryHeader();
                header.OrderDetails = new List <EF.OrderHistoryDetail>();
            }

            return(header);
        }
        private void Create(OrderHistoryFile currentFile, bool isSpecialOrder)
        {
            EF.OrderHistoryHeader header = GetHeaderAndMergeCurrentFile(currentFile, isSpecialOrder);

            if (Configuration.DiagnosticsAuditOrderHistoryHeaderChanges)
            {
                ChangeAuditor.AuditChanges(_unitOfWork.Context, header, _log);
            }

            bool hasSpecialItems = false;

            foreach (OrderHistoryDetail currentDetail in currentFile.Details.ToList())
            {
                if (string.IsNullOrWhiteSpace(currentDetail.SpecialOrderHeaderId))
                {
                    hasSpecialItems = true;
                }

                DetermineDetailOnOrder(isSpecialOrder, header, currentDetail);
            }
            RecalcOrderSubtotal(currentFile, header);

            _headerRepo.CreateOrUpdate(header);

            if (hasSpecialItems)
            {
                RemoveSpecialOrderItemsFromHistory(header);
            }
        }
        public OrderHistoryFileReturn ParseMainframeFile(TextReader reader)
        {
            OrderHistoryFileReturn retVal = new OrderHistoryFileReturn();

            OrderHistoryFile currentFile = null;

            while (reader.Peek() != -1)
            {
                string line = reader.ReadLine();

                switch (line.Substring(RECORDTYPE_STARTPOS, RECORDTYPE_LENGTH))
                {
                case "H":
                    SetErrorStatusAndFutureItemsOnFile(currentFile);

                    currentFile = CreateOrderHistoryFileFromHeaderLine(line);
                    retVal.Files.Add(currentFile);
                    break;

                case "D":
                    var orderDetail = new OrderHistoryDetail();
                    orderDetail.Parse(line);
                    currentFile.Details.Add(orderDetail);
                    break;

                default:
                    break;
                }
            } // end of while

            SetErrorStatusAndFutureItemsOnFile(currentFile);

            return(retVal);
        }
        public OrderHistoryFile ParseNewOrderHistoryFileFromHeaderLine(string line)
        {
            var currentFile = new OrderHistoryFile();

            currentFile.Header.Parse(line);

            return(currentFile);
        }
 private void SetErrorStatusAndFutureItemsOnFile(OrderHistoryFile currentFile)
 {
     if (currentFile != null)
     {
         currentFile.Header.ErrorStatus = (from OrderHistoryDetail detail in currentFile.Details
                                           where detail.ItemStatus != string.Empty
                                           select true).FirstOrDefault();
         currentFile.Header.FutureItems = (from OrderHistoryDetail detail in currentFile.Details
                                           where detail.FutureItem == true
                                           select true).FirstOrDefault();
     }
 }
Beispiel #7
0
            public void OrderHistory_HasValidHeadersAndOrderDate()
            {
                // arrange
                IOrderHistoryLogic testunit = MakeUnitToBeTested();

                // act
                string jsonOrderHistoryFile = testunit.ReadOrderFromQueue();

                // assert
                OrderHistoryFile orderHistoryFile = JsonConvert.DeserializeObject <OrderHistoryFile>(jsonOrderHistoryFile);

                CheckFile(orderHistoryFile);
            }
        public static ConfirmationFile ToConfirmationFile(this OrderHistoryFile historyFile)
        {
            ConfirmationFile confirmation = new ConfirmationFile();

            foreach (OrderHistoryDetail historyDetail in historyFile.Details)
            {
                ConfirmationDetail detail = new ConfirmationDetail()
                {
                    RecordNumber     = historyDetail.LineNumber.ToString(),
                    ItemNumber       = historyDetail.ItemNumber,
                    QuantityOrdered  = historyDetail.OrderQuantity,
                    BrokenCase       = (historyDetail.UnitOfMeasure == UnitOfMeasure.Package ? "Y" : "N"),
                    QuantityShipped  = historyDetail.ShippedQuantity,
                    ShipWeight       = historyDetail.TotalShippedWeight,
                    SalesGross       = historyDetail.SellPrice * historyDetail.ShippedQuantity,
                    SalesNet         = historyDetail.SellPrice * historyDetail.ShippedQuantity,
                    PriceNet         = (historyDetail.UnitOfMeasure == UnitOfMeasure.Case ? historyDetail.SellPrice : 0.0),
                    PriceGross       = (historyDetail.UnitOfMeasure == UnitOfMeasure.Case ? historyDetail.SellPrice : 0.0),
                    SplitPriceNet    = (historyDetail.UnitOfMeasure == UnitOfMeasure.Case ? 0.0 : historyDetail.SellPrice),
                    SplitPriceGross  = (historyDetail.UnitOfMeasure == UnitOfMeasure.Case ? 0.0 : historyDetail.SellPrice),
                    ReasonNotShipped = historyDetail.ItemStatus
                                       //CaseCube
                                       //CaseWeight
                };

                detail.ConfirmationMessage = detail.DisplayStatus();
                confirmation.Detail.Add(detail);
            }

            confirmation.Header.Branch             = historyFile.Header.BranchId;
            confirmation.Header.ConfirmationNumber = historyFile.Header.ControlNumber;
            confirmation.Header.CustomerNumber     = historyFile.Header.CustomerNumber;
            confirmation.Header.InvoiceNumber      = historyFile.Header.InvoiceNumber;
            confirmation.Header.ConfirmationDate   = DateTime.Now.ToLongDateFormatWithTime();
            confirmation.Header.ShipDate           = historyFile.Header.DeliveryDate;
            confirmation.Header.RemoteOrderNumber  = historyFile.Header.ControlNumber;
            // a confirmation will never have this data, and it is coming back wrong now
            //confirmation.Header.RouteNumber = historyFile.Header.RouteNumber;
            //confirmation.Header.StopNumber = historyFile.Header.StopNumber;
            confirmation.Header.TotalQuantityOrdered = confirmation.Detail.Sum(d => d.QuantityOrdered);
            confirmation.Header.TotalQuantityShipped = confirmation.Detail.Sum(d => d.QuantityShipped);
            //confirmation.Header.TotalInvoice
            confirmation.Header.ConfirmationStatus  = historyFile.Header.OrderStatus;
            confirmation.Header.ConfirmationMessage = confirmation.Header.GetDisplayStatus();
            //confirmation.Header.SpecialInstructions
            //confirmation.Header.SpecialInstructionsExtended
            //confirmation.Header.TotalCube
            //confirmation.Header.TotalWeight

            return(confirmation);
        }
Beispiel #9
0
        public List <OrderHistoryFile> GetLastFiveOrderHistory(UserSelectedContext catalogInfo, string itemNumber)
        {
            List <OrderHistoryFile> returnValue = new List <OrderHistoryFile>();

            List <EF.OrderHistoryHeader> history = _orderHeaderRepo.GetLastFiveOrdersByItem(catalogInfo.BranchId, catalogInfo.CustomerId, itemNumber);

            foreach (EF.OrderHistoryHeader h in history)
            {
                OrderHistoryFile root = new OrderHistoryFile()
                {
                    Header = new OrderHistoryHeader()
                    {
                        BranchId              = h.BranchId,
                        CustomerNumber        = h.CustomerNumber,
                        InvoiceNumber         = h.CustomerNumber,
                        DeliveryDate          = h.DeliveryDate,
                        PONumber              = h.PONumber,
                        ControlNumber         = h.ControlNumber,
                        OrderStatus           = h.OrderStatus,
                        FutureItems           = h.FutureItems,
                        ErrorStatus           = h.ErrorStatus,
                        ActualDeliveryTime    = h.ActualDeliveryTime,
                        EstimatedDeliveryTime = h.EstimatedDeliveryTime,
                        ScheduledDeliveryTime = h.ScheduledDeliveryTime,
                        DeliveryOutOfSequence = h.DeliveryOutOfSequence,
                        RouteNumber           = h.RouteNumber,
                        StopNumber            = h.StopNumber
                    }
                };

                var orderHistoryDetails = _orderDetailRepo
                                          .Read(d => d.BranchId == h.BranchId &&
                                                d.OrderHistoryHeader.Id == h.Id &&
                                                d.ItemNumber == itemNumber)
                                          .Select(x => new OrderHistoryDetail()
                {
                    LineNumber      = x.LineNumber,
                    ItemNumber      = x.ItemNumber,
                    OrderQuantity   = x.OrderQuantity,
                    ShippedQuantity = x.ShippedQuantity
                })
                                          .ToList();

                root.Details.AddRange(orderHistoryDetails);

                returnValue.Add(root);
            }

            return(returnValue);
        }
        public static OrderHistoryFile ToOrderHistoryFile(this CS.PurchaseOrder value, UserSelectedContext customerInfo, string specialCatalogId = null)
        {
            OrderHistoryFile retVal = new OrderHistoryFile();

            retVal.Header = value.ToOrderHistoryHeader(customerInfo, specialCatalogId);
            if (value.Properties["LineItems"] != null)
            {
                retVal.Details = ((CommerceServer.Foundation.CommerceRelationshipList)value.Properties["LineItems"])
                                 .Select(l => ToOrderHistoryDetail((CS.LineItem)l.Target, customerInfo.BranchId, retVal.Header.InvoiceNumber)).ToList();
            }


            return(retVal);
        }
        private void LookupAverageWeightOnDetails(OrderHistoryFile currentFile)
        {
            var products = _catalogLogic.GetProductsByIds(currentFile.Header.BranchId,
                                                          currentFile.Details.Select(l => l.ItemNumber.Trim()).Distinct().ToList());

            var productDict = products.Products.ToDictionary(p => p.ItemNumber);

            Parallel.ForEach(currentFile.Details, item => {
                var prod = productDict.ContainsKey(item.ItemNumber.Trim()) ? productDict[item.ItemNumber.Trim()] : null;
                if (prod != null)
                {
                    item.AverageWeight = prod.AverageWeight;
                }
            });
        }
Beispiel #12
0
            public void OrderHistory_HasValidOrderDate()
            {
                // arrange
                string jsonOrderHistoryFile = GetMockData("OrderHistoryFile.json");

                //expect
                string expectedCustomerNumber = "734161";
                string expectedOrderdate      = new DateTime(2018, 07, 19, 16, 34, 51).ToLongDateFormatWithTime();

                // act
                OrderHistoryFile orderHistoryFile = JsonConvert.DeserializeObject <OrderHistoryFile>(jsonOrderHistoryFile);

                // assert
                orderHistoryFile.Header.CustomerNumber.Should().Be(expectedCustomerNumber);
                orderHistoryFile.Header.OrderDateTime.Should().Be(expectedOrderdate);
            }
Beispiel #13
0
            public void OrderHistory_HasValidHeadersAndOrderDate()
            {
                // arrange
                MockDependents     mockDependents = new MockDependents();
                IOrderHistoryLogic testunit       = MakeUnitToBeTested(true, mockDependents);

                // act
                string jsonOrderHistoryFile = testunit.ReadOrderFromQueue();

                // assert
                mockDependents.MockGenericQueueRepository
                .Verify(m => m.ConsumeFromQueue(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once, "not called.");

                OrderHistoryFile orderHistoryFile = JsonConvert.DeserializeObject <OrderHistoryFile>(jsonOrderHistoryFile);

                CheckFile(orderHistoryFile);
            }
        public void ProcessOrder(string rawOrder)
        {
            OrderHistoryFile historyFile = JsonConvert.DeserializeObject <OrderHistoryFile>(rawOrder);

            _log.WriteInformationLog(string.Format("Consuming order update from queue for message ({0})", historyFile.MessageId));

            Create(historyFile, false);

            _conversionLogic.SaveOrderHistoryAsConfirmation(historyFile);

            int      attemptLimit    = Configuration.OrderHistoryPersistenceAttemptLimit;
            TimeSpan attemptInterval = Configuration.OrderHistoryPersistenceAttemptInterval;

            Func <int> saveChanges = () => _unitOfWork.SaveChangesAndClearContext();

            Retry.Do <int>(saveChanges, _log, attemptInterval, attemptLimit);
        }
        public static OrderHistoryFile ToOrderHistoryFile(this ConfirmationFile confirmation)
        {
            OrderHistoryFile history = new OrderHistoryFile();

            foreach (ConfirmationDetail confDetail in confirmation.Detail)
            {
                OrderHistoryDetail detail = new OrderHistoryDetail()
                {
                    LineNumber         = int.Parse(confDetail.RecordNumber),
                    ItemNumber         = confDetail.ItemNumber,
                    OrderQuantity      = confDetail.QuantityOrdered,
                    UnitOfMeasure      = confDetail.BrokenCase.Equals("Y", StringComparison.InvariantCultureIgnoreCase) ? UnitOfMeasure.Package : UnitOfMeasure.Case,
                    ShippedQuantity    = confDetail.QuantityShipped,
                    TotalShippedWeight = confDetail.ShipWeight,
                    SellPrice          = (confDetail.BrokenCase.Equals("Y", StringComparison.InvariantCultureIgnoreCase) ? confDetail.SplitPriceNet : confDetail.PriceNet),
                    ItemStatus         = confDetail.ReasonNotShipped
                                         //CatchWeight,
                                         //FutureItem,
                                         //ItemDeleted,
                                         //ReplacedOriginalItemNumber,
                                         //SubbedOriginalItemNumber,
                };

                history.Details.Add(detail);
            }

            history.Header.BranchId       = confirmation.Header.Branch;
            history.Header.ControlNumber  = confirmation.Header.ConfirmationNumber;
            history.Header.CustomerNumber = confirmation.Header.CustomerNumber;
            history.Header.InvoiceNumber  = confirmation.Header.InvoiceNumber;
            history.Header.DeliveryDate   = confirmation.Header.ShipDate;
            //history.Header.ControlNumber = confirmation.Header.RemoteOrderNumber;
            //history.Header.RouteNumber = confirmation.Header.RouteNumber;
            //history.Header.StopNumber = confirmation.Header.StopNumber;
            history.Header.OrderStatus = confirmation.Header.ConfirmationStatus;
            history.Header.OrderStatus = string.Empty;
            history.Header.OrderSystem = OrderSource.Entree;
            //history.Header.PONumber
            //history.Header.FutureItems
            //history.Header.ErrorStatus

            return(history);
        }
        private EF.OrderHistoryHeader GetHeaderAndMergeCurrentFile(OrderHistoryFile currentFile, bool isSpecialOrder)
        {
            // add retry helper logic to attempt to resolve race conflict
            EF.OrderHistoryHeader header = KeithLink.Svc.Impl.Helpers.Retry.Do <EF.OrderHistoryHeader>
                                               (() => FindHeader(currentFile),
                                               TimeSpan.FromSeconds(1), 3);

            currentFile.Header.MergeWithEntity(ref header);

            // set isSpecialOrder if that is true; but don't set otherwise (used from two places)
            if (isSpecialOrder)
            {
                header.IsSpecialOrder = true;
            }

            if (string.IsNullOrEmpty(header.OriginalControlNumber))
            {
                header.OriginalControlNumber = currentFile.Header.ControlNumber;
            }

            return(header);
        }
        public OrderHistoryFile CreateOrderHistoryFileFromHeaderLine(string line)
        {
            var currentFile = new OrderHistoryFile();

            currentFile.ValidHeader = false;

            // check for length of header record to make sure there is data
            if (line.Trim().Length > 1)
            {
                try
                {
                    currentFile.Header.Parse(line);
                    currentFile.ValidHeader = true;
                }
                catch
                {
                    currentFile.ValidHeader = false;
                }
            }

            return(currentFile);
        }
        /// <summary>
        /// Parse an array of strings as a file
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private OrderHistoryFileReturn ParseFile(string[] data)
        {
            OrderHistoryFileReturn retVal = new OrderHistoryFileReturn();

            OrderHistoryFile currentFile = null;

            for (int i = 0; i < data.Length; i++)
            {
                string line = data[i];

                if (line.Contains("END###"))
                {
                    break;
                }

                switch (line.Substring(RECORDTYPE_STARTPOS, RECORDTYPE_LENGTH))
                {
                case "H":
                    SetErrorStatusAndFutureItemsOnFile(currentFile);

                    currentFile = ParseNewOrderHistoryFileFromHeaderLine(line);
                    retVal.Files.Add(currentFile);
                    break;

                case "D":
                    var orderDetail = new OrderHistoryDetail();
                    orderDetail.Parse(line);
                    currentFile.Details.Add(orderDetail);
                    break;

                default:
                    break;
                }
            } // end of for loop

            SetErrorStatusAndFutureItemsOnFile(currentFile);

            return(retVal);
        }
Beispiel #19
0
        private static void CheckFile(OrderHistoryFile file)
        {
            file.ValidHeader.Should().BeTrue();

            CheckHeader(file.Header);
        }
        public void SaveConfirmationAsOrderHistory(ConfirmationFile confFile)
        {
            if (confFile.Header.ConfirmationStatus.Equals(Constants.CONFIRMATION_HEADER_REJECTED_CODE, StringComparison.InvariantCultureIgnoreCase))
            {
                SaveRejectedConfirmationAsOrderHistory(confFile);
            }
            else
            {
                OrderHistoryFile currentFile = confFile.ToOrderHistoryFile();

                EF.OrderHistoryHeader header = _historyRepo.ReadForInvoice(currentFile.Header.BranchId, currentFile.Header.InvoiceNumber).FirstOrDefault();

                // second attempt to find the order, look by confirmation number
                if (header == null)
                {
                    header = _historyRepo.ReadByConfirmationNumber(currentFile.Header.ControlNumber, currentFile.Header.OrderSystem.ToShortString()).FirstOrDefault();

                    if (header != null)
                    {
                        header.InvoiceNumber = confFile.Header.InvoiceNumber;
                    }
                }

                // last ditch effort is to create a new header
                if (header == null)
                {
                    header = new EF.OrderHistoryHeader();
                    header.OrderDetails = new List <EF.OrderHistoryDetail>();
                }

                currentFile.Header.MergeWithEntity(ref header);

                foreach (OrderHistoryDetail currentDetail in currentFile.Details)
                {
                    EF.OrderHistoryDetail detail = null;

                    if (header.OrderDetails != null && header.OrderDetails.Count > 0)
                    {
                        detail = header.OrderDetails.Where(d => (d.LineNumber == currentDetail.LineNumber)).FirstOrDefault();
                    }

                    if (detail == null)
                    {
                        EF.OrderHistoryDetail tempDetail = currentDetail.ToEntityFrameworkModel();
                        tempDetail.BranchId      = header.BranchId;
                        tempDetail.InvoiceNumber = header.InvoiceNumber;

                        header.OrderDetails.Add(currentDetail.ToEntityFrameworkModel());
                    }
                    else
                    {
                        currentDetail.MergeWithEntityFrameworkModel(ref detail);

                        detail.BranchId      = header.BranchId;
                        detail.InvoiceNumber = header.InvoiceNumber;
                    }
                }

                //Mark missing items as deleted
                foreach (var deletedItem in header.OrderDetails.Where(d => !currentFile.Details.Any(c => c.LineNumber.Equals(d.LineNumber))).ToList())
                {
                    deletedItem.ItemDeleted     = true;
                    deletedItem.OrderQuantity   = 0;
                    deletedItem.ShippedQuantity = 0;
                }

                GetSubtotal(header);

                _historyRepo.CreateOrUpdate(header);
                _uow.SaveChangesAndClearContext();
            }
        }
 private void RecalcOrderSubtotal(OrderHistoryFile currentFile, EF.OrderHistoryHeader header)
 {
     // since we keep an ordersubtotal in the header, we need to recalibrate it (especially for catchweight items) when processing order updates
     LookupAverageWeightOnDetails(currentFile);
     header.OrderSubtotal = (decimal)currentFile.Details.Sum(i => i.LineTotal);
 }
        public void SaveOrder(OrderHistoryFile historyFile, bool isSpecialOrder)
        {
            Create(historyFile, isSpecialOrder);

            _unitOfWork.SaveChanges();
        }