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