public static OrderLine ToOrderLine(this EF.OrderHistoryDetail value, string orderStatus)
        {
            OrderLine lineItem = new OrderLine();

            lineItem.LineNumber         = value.LineNumber;
            lineItem.ItemNumber         = value.ItemNumber;
            lineItem.Quantity           = (short)value.ShippedQuantity;
            lineItem.Price              = (double)value.SellPrice;
            lineItem.QuantityOrdered    = value.OrderQuantity;
            lineItem.QantityShipped     = value.ShippedQuantity;
            lineItem.OrderStatus        = orderStatus;
            lineItem.TotalShippedWeight = value.TotalShippedWeight;
            lineItem.IsDeleted          = value.ItemDeleted;

            if (!string.IsNullOrWhiteSpace(value.ReplacedOriginalItemNumber))
            {
                lineItem.SubstitutedItemNumber = value.ReplacedOriginalItemNumber.Trim();
            }
            else if (!string.IsNullOrWhiteSpace(value.SubbedOriginalItemNumber))
            {
                lineItem.SubstitutedItemNumber = value.SubbedOriginalItemNumber.Trim();
            }
            lineItem.MainFrameStatus = value.ItemStatus;
            lineItem.Each            = value.UnitOfMeasure.Equals(UnitOfMeasure.Package.ToShortString()) ? true : false;
            return(lineItem);
        }
        private void ProcessSpecialOrderItemUpdate(string rawOrder)
        {
            try
            {
                SpecialOrderResponseModel specialorder = JsonConvert.DeserializeObject <SpecialOrderResponseModel>(rawOrder);

                _log.WriteInformationLog(string.Format("Consuming specialorder update from queue for message ({0}) with status {1}", specialorder.MessageId, specialorder.Item.ItemStatusId));

                // retry trying to find the record up to 3 times. some times an exception is thrown for a bad command definition, but then processes fine the next time
                EF.OrderHistoryDetail detail =
                    Retry.Do <EF.OrderHistoryDetail>(() =>
                                                     FindOrderHistoryDetailForUpdate(specialorder), TimeSpan.FromSeconds(5), 3);

                if (detail != null)
                { // only process if we match the order specified on this system
                    ProcessOrderHistoryDetailByUpdateStatus(specialorder, detail);
                }
                else
                {
                    _log.WriteInformationLog(string.Format(" ({0}) Specialorder update from queue for message not an order on this system", specialorder.MessageId));
                }

                // Always clear context at the end of a transaction
                _unitOfWork.ClearContext();
            }
            catch (Exception ex)
            {
                _log.WriteErrorLog("ProcessSpecialOrderItemUpdate", ex);
            }
        }
        private EF.OrderHistoryDetail SeekMatchingDetail(EF.OrderHistoryHeader header, OrderHistoryDetail currentDetail)
        {
            EF.OrderHistoryDetail detail = null;

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

            return(detail);
        }
        private void UpdateOrder(SpecialOrderResponseModel specialorder, EF.OrderHistoryDetail detail)
        {
            EF.OrderHistoryHeader header = _headerRepo.ReadById(detail.OrderHistoryHeader.Id);
            header.DeliveryDate = specialorder.Item.EstimatedArrival;
            _headerRepo.Update(header);

            detail.ItemStatus = Constants.CONFIRMATION_DETAIL_FILLED_CODE;
            _detailRepo.Update(detail);

            _unitOfWork.SaveChanges();
        }
        //private void ProcessOrderHistoryDetailOnStatusUpdate(SpecialOrderResponseModel specialorder, EF.OrderHistoryDetail detail) {
        //    //_log.WriteInformationLog(string.Format(" ({0})  InternalSpecialOrderLogic.ProcessOrderHistoryDetailOnStatusUpdate", specialorder.MessageId));
        //    switch(specialorder.Item.ItemStatusId) {
        //        case Constants.SPECIALORDERITEM_NEW_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_NEW_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_ERR_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_ERR_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_2MF_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_2MF_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_REQ_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_REQ_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_ACC_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_ACC_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_APP_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_APP_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_DEL_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_DEL_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_HLD_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_HLD_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_RCV_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_RCV_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_R_H_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_R_H_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_ATT_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_ATT_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_PTL_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_PTL_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_SHP_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_SHP_STATUS_TRANSLATED_CODE;
        //            break;
        //        case Constants.SPECIALORDERITEM_PUR_STATUS_CODE:
        //            detail.ItemStatus = Constants.SPECIALORDERITEM_PUR_STATUS_TRANSLATED_CODE;
        //            break;
        //    }
        //    _detailRepo.Update(detail);
        //    _unitOfWork.SaveChanges();
        //}

        private void ProcessOrderHistoryDetailOnItemShipped
            (SpecialOrderResponseModel specialorder, EF.OrderHistoryDetail detail)
        {
            _log.WriteInformationLog
                (string.Format
                    (" ({0})  InternalSpecialOrderLogic.ProcessOrderHistoryDetailOnStatusUpdate", specialorder.MessageId));

            UpdateOrder(specialorder, detail);

            SendNotification(specialorder);
        }
 public static InvoiceItemModel ToInvoiceItem(this EF.OrderHistoryDetail value)
 {
     return(new InvoiceItemModel()
     {
         ItemNumber = value.ItemNumber,
         ItemPrice = value.SellPrice,
         QuantityShipped = value.ShippedQuantity,
         QuantityOrdered = value.OrderQuantity,
         LineNumber = value.LineNumber.ToString(),
         CatchWeight = value.CatchWeight,
         CatchWeightCode = value.CatchWeight,
         ExtCatchWeight = value.TotalShippedWeight
     });
 }
        private EF.OrderHistoryDetail FindOrderHistoryDetailForUpdate(SpecialOrderResponseModel specialorder)
        {
            EF.OrderHistoryDetail detail = null;

            // try to find detail by specialorderheaderid and linenumber
            if (!String.IsNullOrEmpty(specialorder.Header.RequestHeaderId) && !String.IsNullOrEmpty(specialorder.Item.LineNumber))
            {
                detail = _detailRepo.Read(d => d.BranchId.Equals(specialorder.Header.BranchId, StringComparison.InvariantCultureIgnoreCase) &&
                                          d.SpecialOrderHeaderId == specialorder.Header.RequestHeaderId &&
                                          d.SpecialOrderLineNumber == specialorder.Item.LineNumber).FirstOrDefault();
            }

            return(detail);
        }
        private EF.OrderHistoryDetail AddNewDetailToOrder(bool isSpecialOrder, EF.OrderHistoryHeader header, OrderHistoryDetail currentDetail)
        {
            EF.OrderHistoryDetail tempDetail = currentDetail.ToEntityFrameworkModel();
            tempDetail.BranchId           = header.BranchId;
            tempDetail.InvoiceNumber      = header.InvoiceNumber;
            tempDetail.OrderHistoryHeader = header;

            if (isSpecialOrder)
            {
                tempDetail.ItemStatus = KeithLink.Svc.Core.Constants.SPECIALORDERITEM_REQ_STATUS_TRANSLATED_CODE;
            }

            header.OrderDetails.Add(tempDetail);
            return(tempDetail);
        }
        private void DetermineDetailOnOrder(bool isSpecialOrder, EF.OrderHistoryHeader header, OrderHistoryDetail currentDetail)
        {
            EF.OrderHistoryDetail detail = SeekMatchingDetail(header, currentDetail);

            if (detail == null)
            {
                detail = AddNewDetailToOrder(isSpecialOrder, header, currentDetail);
            }
            else
            {
                detail = MergeWithCurrentOrderDetail(isSpecialOrder, header, currentDetail, detail);
            }

            if (Configuration.DiagnosticsAuditOrderHistoryDetailChanges)
            {
                ChangeAuditor.AuditChanges(_unitOfWork.Context, detail, _log);
            }
        }
 private void ProcessOrderHistoryDetailOnDeletedOrPurgedStatusUpdate(EF.OrderHistoryDetail detail)
 {
     EF.OrderHistoryHeader header = detail.OrderHistoryHeader;
     if (header != null)
     {
         if (header.OrderDetails.Count > 1)
         {
             _detailRepo.Delete(detail);
         }
         else
         {
             _headerRepo.Delete(header);
         }
         _unitOfWork.SaveChanges();
     }
     else
     {
         _detailRepo.Delete(detail);
         _unitOfWork.SaveChanges();
     }
 }
        public static OrderHistoryDetail ToOrderHistoryDetail(this EF.OrderHistoryDetail value)
        {
            OrderHistoryDetail retVal = new OrderHistoryDetail();

            retVal.ItemNumber                 = value.ItemNumber;
            retVal.LineNumber                 = value.LineNumber;
            retVal.OrderQuantity              = value.OrderQuantity;
            retVal.ShippedQuantity            = value.ShippedQuantity;
            retVal.UnitOfMeasure              = value.UnitOfMeasure.Equals("C", StringComparison.InvariantCultureIgnoreCase) ? UnitOfMeasure.Case : UnitOfMeasure.Package;
            retVal.CatchWeight                = value.CatchWeight;
            retVal.ItemDeleted                = value.ItemDeleted;
            retVal.SubbedOriginalItemNumber   = value.SubbedOriginalItemNumber;
            retVal.ReplacedOriginalItemNumber = value.ReplacedOriginalItemNumber;
            retVal.ItemStatus                 = value.ItemStatus;
            retVal.TotalShippedWeight         = (double)value.TotalShippedWeight;
            retVal.SellPrice              = (double)value.SellPrice;
            retVal.Source                 = value.Source;
            retVal.ManufacturerId         = value.ManufacturerId;
            retVal.SpecialOrderHeaderId   = value.SpecialOrderHeaderId;
            retVal.SpecialOrderLineNumber = value.SpecialOrderLineNumber;

            return(retVal);
        }
        public static EF.OrderHistoryDetail ToEntityFrameworkModel(this OrderHistoryDetail value)
        {
            EF.OrderHistoryDetail retVal = new EF.OrderHistoryDetail();

            retVal.ItemNumber                 = value.ItemNumber;
            retVal.LineNumber                 = value.LineNumber;
            retVal.OrderQuantity              = value.OrderQuantity;
            retVal.ShippedQuantity            = value.ShippedQuantity;
            retVal.UnitOfMeasure              = value.UnitOfMeasure.ToShortString();
            retVal.CatchWeight                = value.CatchWeight;
            retVal.ItemDeleted                = value.ItemDeleted;
            retVal.SubbedOriginalItemNumber   = value.SubbedOriginalItemNumber;
            retVal.ReplacedOriginalItemNumber = value.ReplacedOriginalItemNumber;
            retVal.ItemStatus                 = value.ItemStatus;
            retVal.TotalShippedWeight         = (decimal)value.TotalShippedWeight;
            retVal.SellPrice              = (decimal)value.SellPrice;
            retVal.Source                 = value.Source;
            retVal.ManufacturerId         = value.ManufacturerId;
            retVal.SpecialOrderHeaderId   = value.SpecialOrderHeaderId;
            retVal.SpecialOrderLineNumber = value.SpecialOrderLineNumber;

            return(retVal);
        }
        public static void MergeWithEntityFrameworkModel(this OrderHistoryDetail value, ref EF.OrderHistoryDetail entity)
        {
            entity.ItemNumber      = value.ItemNumber;
            entity.LineNumber      = value.LineNumber;
            entity.OrderQuantity   = value.OrderQuantity;
            entity.ShippedQuantity = (value.ItemStatus != null && value.ItemStatus.Equals(Constants.CONFIRMATION_DETAIL_ITEM_STATUS_DELETE)) ? 0 : value.ShippedQuantity;
            entity.UnitOfMeasure   = value.UnitOfMeasure.ToShortString();
            entity.CatchWeight     = value.CatchWeight;
            entity.ItemDeleted     = value.ItemDeleted;

            // The field in the table is defined as char(6) and will be padded with spaces.
            // We will avoid extraneous updates by EF if we prevent changing the value when they are equivalent.
            bool equivalentValues =
                entity.SubbedOriginalItemNumber != null &&
                value.SubbedOriginalItemNumber != null &&
                entity.SubbedOriginalItemNumber.TrimEnd() == value.SubbedOriginalItemNumber.TrimEnd();

            if (equivalentValues == false)
            {
                entity.SubbedOriginalItemNumber = value.SubbedOriginalItemNumber;
            }

            // The field in the table is defined as char(6) and will be padded with spaces.
            // We will avoid extraneous updates by EF if we prevent changing the value when they are equivalent.
            equivalentValues =
                entity.ReplacedOriginalItemNumber != null &&
                value.ReplacedOriginalItemNumber != null &&
                entity.ReplacedOriginalItemNumber.TrimEnd() == value.ReplacedOriginalItemNumber.TrimEnd();
            if (equivalentValues == false)
            {
                entity.ReplacedOriginalItemNumber = value.ReplacedOriginalItemNumber;
            }

            entity.ItemStatus         = value.ItemStatus;
            entity.TotalShippedWeight = decimal.Parse(value.TotalShippedWeight.ToString());
            entity.SellPrice          = (decimal)value.SellPrice;
        }
        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 EF.OrderHistoryDetail MergeWithCurrentOrderDetail
            (bool isSpecialOrder, EF.OrderHistoryHeader header, OrderHistoryDetail currentDetail, EF.OrderHistoryDetail detail)
        {
            currentDetail.MergeWithEntityFrameworkModel(ref detail);

            detail.BranchId      = header.BranchId;
            detail.InvoiceNumber = header.InvoiceNumber;
            if (isSpecialOrder)
            {
                detail.ItemStatus = KeithLink.Svc.Core.Constants.SPECIALORDERITEM_REQ_STATUS_TRANSLATED_CODE;
            }

            return(detail);
        }
        private void ProcessOrderHistoryDetailByUpdateStatus(SpecialOrderResponseModel specialorder, EF.OrderHistoryDetail detail)
        {
            switch (specialorder.Item.ItemStatusId)
            {
            case Constants.SPECIALORDERITEM_DEL_STATUS_CODE:
            case Constants.SPECIALORDERITEM_PUR_STATUS_CODE: {
                ProcessOrderHistoryDetailOnDeletedOrPurgedStatusUpdate(detail);
                break;
            }

            case Constants.SPECIALORDERITEM_SHP_STATUS_CODE: {
                ProcessOrderHistoryDetailOnItemShipped(specialorder, detail);
                break;
            }
                //default: // not exposing any status from KSOS right now
                //    {
                //        ProcessOrderHistoryDetailOnStatusUpdate(specialorder, detail);
                //        break;
                //    }
            }
        }