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);
        }
Example #2
0
        public void Create(OrderFile file)
        {
            var query = _specialOrderDbContext.RequestHeaderIds
                        .SqlQuery("dbo.spGetNextRequestHeaderId @branchId", new SqlParameter("branchId", file.Header.Branch));
            string headerId = query.FirstAsync().Result.CurrentId.PadLeft(CONFNUMBER_LENGTH, CONFNUMBER_PADDINGCHAR);

            // next, call create after converting OrderFile to RequestHeader
            RequestHeader requestHeader = BuildRequestHeader(file.Header, headerId);

            _specialOrderDbContext.RequestHeaders.Add(requestHeader);

            _specialOrderDbContext.Context.SaveChanges();


            foreach (OrderDetail detail in file.Details)
            {
                _specialOrderDbContext.RequestItems.Add(BuildRequestDetail(detail, headerId, file.Header.Branch, file.Header.CustomerNumber, file.Header.PONumber));
            }

            _specialOrderDbContext.Context.SaveChanges();

            // add idToUse to order history
            var orderHistory = _orderHistory.ReadByConfirmationNumber(file.Header.ControlNumber.ToString().PadLeft(CONFNUMBER_LENGTH, CONFNUMBER_PADDINGCHAR), "B").FirstOrDefault();

            if (orderHistory == null)
            {
                // this could only happen if the order was created in Dev and it is being processed in QA
                StringBuilder warningMessage = new StringBuilder();
                warningMessage.Append("Order not found in order history. This should only happen when created in one environment and processed in another(e.g. Local and Dev). ");
                warningMessage.Append("Affected order number is ");
                warningMessage.Append(file.Header.ControlNumber);

                _log.WriteWarningLog(warningMessage.ToString());
            }
            else
            {
                //details
                foreach (var orderItem in orderHistory.OrderDetails)
                {
                    var detailItems = file.Details.Where(x => x.ItemNumber == orderItem.ItemNumber.Trim()).ToList();
                    foreach (var detailItem in detailItems)
                    {
                        orderItem.ManufacturerId         = detailItem.ManufacturerName;//todo
                        orderItem.SpecialOrderLineNumber = detailItem.LineNumber.ToString();
                        orderItem.SpecialOrderHeaderId   = headerId;
                    }
                }
                _unitOfWork.SaveChangesAndClearContext();
            }

            requestHeader.OrderStatusId = KSOS_STATUS_SEND;
            _specialOrderDbContext.Context.SaveChanges();
        }
        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();
            }
        }