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);
        }
        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();
            }
        }