public void FinaliseOrderModel(Messages messages, UserSessionModel admin, OrderViewModelLight model)
        {
            if (!string.IsNullOrEmpty(model.ProjectId.ToString()) &&
                !string.IsNullOrEmpty(model.QuoteId.ToString()))
            {
                var projectQuery = from project in this.Db.QueryProjectViewableByProjectId(admin, model.ProjectId)

                                   join quote in this.Db.Quotes on new { id = project.ProjectId, qId = model.QuoteId }
                equals new { id = quote.ProjectId, qId = quote.QuoteId } into Laq
                from quote in Laq.DefaultIfEmpty()
                select new ProjectModel
                {
                    ProjectId               = project.ProjectId,
                    OwnerId                 = project.Owner.UserId,
                    Name                    = project.Name,
                    Description             = project.Description,
                    ProjectDate             = project.ProjectDate,
                    BidDate                 = project.BidDate,
                    EstimatedClose          = project.EstimatedClose,
                    EstimatedDelivery       = project.EstimatedDelivery,
                    Expiration              = project.Expiration,
                    ProjectStatusTypeId     = (byte)project.ProjectStatusTypeId,
                    ProjectTypeId           = project.ProjectTypeId,
                    ProjectOpenStatusTypeId = project.ProjectOpenStatusTypeId,
                    ConstructionTypeId      = project.ConstructionTypeId,
                    VerticalMarketTypeId    = project.VerticalMarketTypeId,

                    CustomerAddress = new AddressModel
                    {
                        AddressId = project.CustomerAddressId,
                    },
                    SellerAddress = new AddressModel
                    {
                        AddressId = project.SellerAddressId,
                    },
                    EngineerAddress = new AddressModel
                    {
                        AddressId = project.EngineerAddressId,
                    },
                    ShipToAddress = new AddressModel
                    {
                        AddressId = project.ShipToAddressId,
                    },

                    ActiveQuoteSummary = new QuoteListModel
                    {
                        ProjectId = project.ProjectId,
                        QuoteId   = (quote == null) ? 0 : quote.QuoteId,
                        //ItemCount = (quote == null) ? 0 : quote.QuoteItems.Count(),
                        Alert                           = (quote == null) ? false : quote.RecalculationRequired,
                        Title                           = (quote == null) ? "" : quote.Title,
                        Timestamp                       = (quote == null) ? (DateTime?)null : quote.Timestamp,
                        TotalList                       = (quote == null) ? 0 : quote.TotalList,
                        TotalListSplit                  = (quote == null) ? 0 : quote.TotalListSplit,
                        TotalListVRV                    = (quote == null) ? 0 : quote.TotalListVRV,
                        TotalMisc                       = (quote == null) ? 0 : quote.TotalMisc,
                        TotalNet                        = (quote == null) ? 0 : quote.TotalNet,
                        TotalSell                       = (quote == null) ? 0 : quote.TotalSell,
                        TotalSellSplit                  = (quote == null) ? 0 : quote.TotalSellSplit,
                        TotalSellVRV                    = (quote == null) ? 0 : quote.TotalSellVRV,
                        TotalCountSplit                 = (quote == null) ? 0 : quote.TotalCountSplit,
                        TotalCountVRV                   = (quote == null) ? 0 : quote.TotalCountVRV,
                        TotalCountVRVIndoor             = (quote == null) ? 0 : quote.TotalCountVRVIndoor,
                        TotalCountVRVOutdoor            = (quote == null) ? 0 : quote.TotalCountVRVOutdoor,
                        ApprovedCommissionPercentage    = (quote == null) ? 0 : quote.ApprovedCommissionPercentage,
                        ApprovedDiscountPercentage      = (quote == null) ? 0 : quote.ApprovedDiscountPercentage,
                        ApprovedDiscountPercentageSplit = (quote == null) ? 0 : quote.ApprovedDiscountPercentageSplit,
                        ApprovedDiscountPercentageVRV   = (quote == null) ? 0 : quote.ApprovedDiscountPercentageVRV,
                        TotalNetCommission              = (quote == null) ? 0 : quote.TotalNetCommission,
                        TotalNetNonCommission           = (quote == null) ? 0 : quote.TotalNetNonCommission,
                        TotalNetSplit                   = (quote == null) ? 0 : quote.TotalNetSplit,
                        TotalNetVRV                     = (quote == null) ? 0 : quote.TotalNetVRV,
                        IsGrossMargin                   = (quote == null) ? false : quote.IsGrossMargin,
                        TotalFreight                    = (quote == null) ? 0 : quote.TotalFreight,
                        DiscountPercentage              = (quote == null) ? 0 : quote.DiscountPercentage,
                        CommissionPercentage            = (quote == null) ? 0 : quote.CommissionPercentage,
                        Revision                        = (quote == null) ? 0 : quote.Revision
                    },
                    ConstructionTypeDescription  = project.ConstructionType.Description,
                    ProjectTypeDescription       = project.ProjectType.Description,
                    ProjectOpenStatusDescription = project.ProjectOpenStatusType.Description,
                    ProjectStatusDescription     = project.ProjectStatusType.Description,
                    VerticalMarketDescription    = project.VerticalMarketType.Description,
                    Deleted   = project.Deleted,
                    Timestamp = project.Timestamp
                };

                var addressService = new AddressServices(this.Context);
            }

            if (model == null)
            {
                this.Response.AddError(Resources.DataMessages.DM007);
                return;
            }
        }
        public ServiceResponse GetQuoteQuotePackage(UserSessionModel admin, SubmittalRequestModel model)
        {
            Log.InfoFormat("Enter GetQuoteQuotePackge for {0}", model.GetType());
            Log.Debug("PageSize: " + model.PageSize);

            model.PageSize = Constants.DEFAULT_PAGESIZE_RETURN_ALL;

            // get QuoteItems items now
            var search = new SearchQuoteItem(model as Search);

            Log.Debug("Searching Filter: " + search.Filter);

            if (!model.QuoteId.HasValue)
            {
                this.Response.AddError(Resources.DataMessages.DM010);
                Log.ErrorFormat(this.Response.Messages.Items.Last().Text);
            }

            var query = from quote in this.Db.QueryQuoteViewableByQuoteId(admin, model.QuoteId)
                        join project in this.Db.Projects on quote.ProjectId equals project.ProjectId

                        join active in this.Db.Quotes on new { id = project.ProjectId, active = true } equals new { id = active.ProjectId, active = active.Active } into Laq
            from active in Laq.DefaultIfEmpty()

            select new SubmittalRequestModel
            {
                ProjectId   = quote.ProjectId,
                QuoteId     = quote.QuoteId,
                Title       = quote.Title,
                ProjectName = project.Name,
            };

            try
            {
                Log.DebugFormat("Start retrieve QuoteItemsModel for QuoteId: {0}", model.QuoteId);
                model = query.FirstOrDefault();
            }
            catch (Exception ex)
            {
                Log.FatalFormat("Exception Source: {0}", ex.Source);
                Log.FatalFormat("Exception: {0}", ex.Message);
                Log.FatalFormat("Exception Detail: {0}", ex.InnerException.Message);
            }

            if (model == null)
            {
                this.Response.AddError(Resources.DataMessages.DM010);
                Log.ErrorFormat("the return {0} model is null", model.GetType());
            }

            search.QuoteId = model.QuoteId;

            search.ReturnTotals = false;

            var response = GetSubmittalPackageQuoteItemListModel(admin, search);

            var items = response.Model as List <QuoteItemListModel>; // original items list

            //Get QuoteItemOptions
            var finalItemsList = new List <QuoteItemListModel>();

            foreach (var item in items)
            {
                if (item.LineItemTypeId == (byte)LineItemTypeEnum.Configured)
                {
                    model.HasConfiguredItem = true;
                    var optionItems = GetOptionItemsAsQuoteItemList(admin, (long)item.QuoteItemId);
                    finalItemsList.AddRange(optionItems);
                }
            }

            //Combine with original items list
            finalItemsList.AddRange(items);

            //Remove duplicated Items
            //finalItemsList = finalItemsList.GroupBy(i => i.ProductId).Select(i => i.First()).ToList();

            Log.DebugFormat("Total QuoteItemListModel return from search: {0}", finalItemsList.Count);

            model.Items = new PagedList <QuoteItemListModel>(finalItemsList, model);

            var products = finalItemsList.Cast <ProductModel>().ToList();

            Log.DebugFormat("Total Products return from search: {0}", products.Count);

            new ProductServices(this.Context).GetDocuments(products);

            var baseDirectory = Utilities.GetQuotePackageDirectory(model.QuoteId.Value);

            Log.DebugFormat("QuotePackageDirectory return: {0}", baseDirectory);

            var packagefiles = Directory.GetFiles(baseDirectory);

            Log.DebugFormat("Total files return from  QuotePackageDirectory (included lock files): {0}", packagefiles.Count());
            foreach (var file in packagefiles)
            {
                Log.Debug(file);
            }

            model.QuotePackage = new List <DocumentModel>();

            model.QuotePackage.AddRange(packagefiles.Select(f => new DocumentModel {
                Description = Path.GetFileName(f)
            }).ToList());

            var quotePackageFileName = Utilities.QuotePackageFileName(model.QuoteId.Value);

            var attachedfiles = Directory.GetFiles(baseDirectory).Where(f =>
            {
                var file = System.IO.Path.GetFileName(f);
                var isSystemPackageFile = file.StartsWith("DPO_QuotePackage_");
                var isLock = file.EndsWith(".lck");
                return(!isSystemPackageFile && !isLock);
            }).ToList();

            Log.DebugFormat("Total files return from  QuotePackageDirectory : {0}", attachedfiles.Count());
            foreach (var attFile in attachedfiles)
            {
                Log.Debug(attachedfiles);
            }

            model.QuotePackageAttachedFiles = new List <DocumentModel>();

            model.QuotePackageAttachedFiles.AddRange(attachedfiles.Select(f =>
                                                                          new DocumentModel
            {
                FileName       = Path.GetFileName(f),
                DocumentTypeId = (int)DocumentTypeEnum.QuotePackageAttachedFile,
                Type           = "QuotePackageAttachedFile",
                Description    = Path.GetFileName(f)
            }).ToList());

            Log.DebugFormat("Total QuotePackageAttachmentfiles have added to QuoteItemsModel: {0}", model.QuotePackageAttachedFiles.Count());
            foreach (var qpaf in model.QuotePackageAttachedFiles)
            {
                Log.Debug(qpaf.FileName);
            }

            this.Response.Model = model;
            Log.InfoFormat("GetQuoteQuotePackage finished");

            return(this.Response);
        }
        public void FinaliseOrderModel(UserSessionModel admin, OrderViewModel model)
        {
            if (!string.IsNullOrEmpty(model.ProjectId.ToString()) &&
                !string.IsNullOrEmpty(model.QuoteId.ToString()))
            {
                var projectQuery = from project in this.Db.QueryProjectViewableByProjectId(admin, model.ProjectId)

                                   join quote in this.Db.Quotes on new { id = project.ProjectId, qId = model.QuoteId } equals new { id = quote.ProjectId, qId = quote.QuoteId } into Laq
                from quote in Laq.DefaultIfEmpty()

                select new ProjectModel
                {
                    ProjectId               = project.ProjectId,
                    OwnerId                 = project.Owner.UserId,
                    Name                    = project.Name,
                    Description             = project.Description,
                    ProjectDate             = project.ProjectDate,
                    BidDate                 = project.BidDate,
                    EstimatedClose          = project.EstimatedClose,
                    EstimatedDelivery       = project.EstimatedDelivery,
                    Expiration              = project.Expiration,
                    ProjectStatusTypeId     = (byte)project.ProjectStatusTypeId,
                    ProjectTypeId           = project.ProjectTypeId,
                    ProjectOpenStatusTypeId = project.ProjectOpenStatusTypeId,
                    ConstructionTypeId      = project.ConstructionTypeId,
                    VerticalMarketTypeId    = project.VerticalMarketTypeId,

                    CustomerAddress = new AddressModel
                    {
                        AddressId = project.CustomerAddressId,
                    },
                    SellerAddress = new AddressModel
                    {
                        AddressId = project.SellerAddressId,
                    },
                    EngineerAddress = new AddressModel
                    {
                        AddressId = project.EngineerAddressId,
                    },
                    ShipToAddress = new AddressModel
                    {
                        AddressId = project.ShipToAddressId,
                    },

                    ActiveQuoteSummary = new QuoteListModel
                    {
                        ProjectId = project.ProjectId,
                        QuoteId   = (quote == null) ? 0 : quote.QuoteId,
                        //ItemCount = (quote == null) ? 0 : quote.QuoteItems.Count(),
                        Alert                           = (quote == null) ? false : quote.RecalculationRequired,
                        Title                           = (quote == null) ? "" : quote.Title,
                        Timestamp                       = (quote == null) ? (DateTime?)null : quote.Timestamp,
                        TotalList                       = (quote == null) ? 0 : quote.TotalList,
                        TotalListSplit                  = (quote == null) ? 0 : quote.TotalListSplit,
                        TotalListVRV                    = (quote == null) ? 0 : quote.TotalListVRV,
                        TotalMisc                       = (quote == null) ? 0 : quote.TotalMisc,
                        TotalNet                        = (quote == null) ? 0 : quote.TotalNet,
                        TotalSell                       = (quote == null) ? 0 : quote.TotalSell,
                        TotalSellSplit                  = (quote == null) ? 0 : quote.TotalSellSplit,
                        TotalSellVRV                    = (quote == null) ? 0 : quote.TotalSellVRV,
                        TotalCountSplit                 = (quote == null) ? 0 : quote.TotalCountSplit,
                        TotalCountVRV                   = (quote == null) ? 0 : quote.TotalCountVRV,
                        TotalCountVRVIndoor             = (quote == null) ? 0 : quote.TotalCountVRVIndoor,
                        TotalCountVRVOutdoor            = (quote == null) ? 0 : quote.TotalCountVRVOutdoor,
                        ApprovedCommissionPercentage    = (quote == null) ? 0 : quote.ApprovedCommissionPercentage,
                        ApprovedDiscountPercentage      = (quote == null) ? 0 : quote.ApprovedDiscountPercentage,
                        ApprovedDiscountPercentageSplit = (quote == null) ? 0 : quote.ApprovedDiscountPercentageSplit,
                        ApprovedDiscountPercentageVRV   = (quote == null) ? 0 : quote.ApprovedDiscountPercentageVRV,
                        TotalNetCommission              = (quote == null) ? 0 : quote.TotalNetCommission,
                        TotalNetNonCommission           = (quote == null) ? 0 : quote.TotalNetNonCommission,
                        TotalNetSplit                   = (quote == null) ? 0 : quote.TotalNetSplit,
                        TotalNetVRV                     = (quote == null) ? 0 : quote.TotalNetVRV,
                        IsGrossMargin                   = (quote == null) ? false : quote.IsGrossMargin,
                        TotalFreight                    = (quote == null) ? 0 : quote.TotalFreight,
                        DiscountPercentage              = (quote == null) ? 0 : quote.DiscountPercentage,
                        CommissionPercentage            = (quote == null) ? 0 : quote.CommissionPercentage,
                        Revision                        = (quote == null) ? 0 : quote.Revision
                    },
                    ConstructionTypeDescription  = project.ConstructionType.Description,
                    ProjectTypeDescription       = project.ProjectType.Description,
                    ProjectOpenStatusDescription = project.ProjectOpenStatusType.Description,
                    ProjectStatusDescription     = project.ProjectStatusType.Description,
                    VerticalMarketDescription    = project.VerticalMarketType.Description,
                    Deleted   = project.Deleted,
                    Timestamp = project.Timestamp
                };

                model.Project = projectQuery.FirstOrDefault();

                var addressService = new AddressServices(this.Context);
                model.Project.SellerAddress   = addressService.GetAddressModel(admin, model.Project.SellerAddress);
                model.Project.CustomerAddress = addressService.GetAddressModel(admin, model.Project.CustomerAddress);
                model.Project.EngineerAddress = addressService.GetAddressModel(admin, model.Project.EngineerAddress);
                model.Project.ShipToAddress   = addressService.GetAddressModel(admin, model.Project.ShipToAddress);

                //model.QuoteItems = new QuoteServices(this.Context).GetQuoteItemListModel(admin, (long)model.QuoteId).Model as List<QuoteItemListModel>;
                model.QuoteItems = new QuoteServices(this.Context).GetQuoteItems(admin, (long)model.QuoteId).Model as List <QuoteItemModel>;
            }

            if (model == null)
            {
                this.Response.AddError(Resources.DataMessages.DM007);
                return;
            }

            #region commented
            //var service = new QuoteServices();

            //if (model.ApprovedDiscount > 0)
            //{
            //    model.ApprovedTotals = service.CalculateTotalDiscountsApproved(model.Quote, model.ApprovedDiscount,
            //        model.ApprovedDiscountSplit, model.ApprovedDiscountVRV, model.RequestedCommission);
            //}
            //else
            //{
            //    model.ApprovedTotals = service.CalculateTotalDiscountsApproved(model.Quote, model.RequestedDiscount,
            //        model.RequestedDiscountSplit, model.RequestedDiscountVRV, model.RequestedCommission);
            //}

            //model.StandardTotals = service.CalculateTotalStandard(model.Quote);
            //model.RequestedDiscount *= 100M;
            //model.RequestedDiscountSplit *= 100M;
            //model.RequestedDiscountVRV *= 100M;
            //model.ApprovedDiscount *= 100M;
            //model.ApprovedDiscountSplit *= 100M;
            //model.ApprovedDiscountVRV *= 100M;
            //model.RequestedCommission *= 100;

            // Dropdowns
            //model.DiscountRequestStatusTypes = htmlService.DropDownModelDiscountRequestStatusTypes((model == null) ? null : model.DiscountRequestStatusTypeId);
            //model.SystemBasisDesignTypes = htmlService.DropDownModelSystemBasisDesignTypes((model == null) ? null : model.SystemBasisDesignTypeId);
            //model.ZoneStrategyTypes = htmlService.DropDownModelZoneStrategyTypes((model == null) ? null : model.ZoneStrategyTypeId);
            //model.BrandSpecifiedTypes = htmlService.DropDownModelBrandCompetitorTypes((model == null) ? null : model.BrandSpecifiedTypeId);
            //model.BrandApprovedTypes = htmlService.DropDownModelBrandCompetitorTypes((model == null) ? null : model.BrandApprovedTypeId);
            //model.DaikinEquipmentAtAdvantageTypes = htmlService.DropDownModelDaikinEquipmentAtAdvantageTypes((model == null) ? null : model.DaikinEquipmentAtAdvantageTypeId);
            //model.ProbabilityOfCloseTypes = htmlService.DropDownModelProbabilityOfCloseTypes((model == null) ? null : model.ProbabilityOfCloseTypeId);
            #endregion commented
        }