public ServiceResponse GetOrderModel(UserSessionModel user, OrderViewModelLight qto)
        {
            OrderViewModelLight model = null;

            if (!string.IsNullOrEmpty(qto.QuoteId.ToString()))
            {
                var query = from order in this.Db.QueryOrdersViewableByUser(user)

                            join mod in this.Db.Users on order.UpdatedByUserId equals mod.UserId into Lmod
                            from mod in Lmod.DefaultIfEmpty()
                            join project in this.Db.Projects on order.Quote.ProjectId equals project.ProjectId
                            join quote in this.Db.Quotes on order.QuoteId equals quote.QuoteId
                            join owner in this.Db.Users on project.OwnerId equals owner.UserId
                            join business in this.Db.Businesses on owner.BusinessId equals business.BusinessId
                            join ort in this.Context.OrderAttachments on order.OrderId equals ort.OrderId
                            where order.OrderId == qto.OrderId
                            select new OrderViewModelLight
                {
                    OrderId              = order.OrderId,
                    ProjectId            = order.Quote.ProjectId,
                    QuoteId              = order.QuoteId,
                    ProjectOwner         = owner.FirstName + " " + owner.LastName,
                    ProjectOwnerId       = owner.UserId,
                    BusinessId           = owner.BusinessId.Value,
                    BusinessName         = business.BusinessName,
                    OrderStatusTypeId    = (OrderStatusTypeEnum)order.OrderStatusTypeId,
                    CreatedByUserId      = mod.UserId,
                    UpdatedByUserId      = mod.UserId,
                    UpdatedByUser        = mod.FirstName + " " + mod.LastName,
                    DiscountRequestId    = order.DiscountRequestId,
                    CommissionRequestId  = order.CommissionRequestId,
                    ShipToAddressId      = order.ShipToAddressId,
                    PricingTypeId        = order.PricingTypeId,
                    PONumber             = order.PONumber,
                    TotalDiscountPercent = order.TotalDiscountPercent,
                    Comments             = order.Comments,
                    //EstimatedReleaseDate = order.EstimatedReleaseDate,
                    EstimatedDeliveryDate       = (project.EstimatedDelivery != null) ? project.EstimatedDelivery : DateTime.Now,
                    DeliveryAppointmentRequired = order.DeliveryAppointmentRequired,
                    DeliveryContactName         = order.DeliveryContactName,
                    DeliveryContactPhone        = order.DeliveryContactPhone,
                    SubmittedByUserId           = mod.UserId,
                    //SubmitDate = (order.SubmitDate != null) ? order.SubmitDate : DateTime.Now,
                    SubmitDate = (DateTime)order.SubmitDate,

                    ProjectDate          = (project.ProjectDate != null) ? project.ProjectDate : DateTime.Now,
                    ERPOrderDate         = (order.ERPOrderDate != null) ? order.ERPOrderDate.Value : (System.DateTime?)null,
                    ERPInvoiceNumber     = (order.ERPInvoiceNumber != null) ? order.ERPInvoiceNumber : null,
                    ERPComments          = (order.ERPComment != null) ? order.ERPComment : null,
                    ERPPOKey             = (order.ERPPOKey != null) ? order.ERPPOKey.Value : (int?)null,
                    ERPStatus            = (order.ERPStatus != null) ? order.ERPStatus : null,
                    POAttachmentFileName = ort.FileName,
                    Timestamp            = (order.Timestamp != null) ? order.Timestamp : DateTime.Now
                };

                model = query.FirstOrDefault();
            }

            if (model == null)
            {
                model = new OrderViewModelLight
                {
                    OrderStatusTypeId = OrderStatusTypeEnum.NewRecord,
                    QuoteId           = qto.QuoteId
                };
            }

            finaliseModelSvc.FinaliseOrderModel(this.Response.Messages, user, model);
            this.Response.Model = model;

            return(this.Response);
        }
        public ServiceResponse GetOrderListViewModel(UserSessionModel user, OrderViewModel orderVM)
        {
            List <OrderViewModel> orders = null;

            if (!string.IsNullOrEmpty(orderVM.QuoteId.ToString()))
            {
                var query = from order in this.Db.QueryOrdersViewableByUser(user)

                            join mod in this.Db.Users on order.UpdatedByUserId equals mod.UserId into Lmod
                            from mod in Lmod.DefaultIfEmpty()
                            join project in this.Db.Projects on order.Quote.ProjectId equals project.ProjectId
                            join quote in this.Db.Quotes on order.QuoteId equals quote.QuoteId
                            join owner in this.Db.Users on project.OwnerId equals owner.UserId
                            join business in this.Db.Businesses on owner.BusinessId equals business.BusinessId
                            join ort in this.Context.OrderAttachments on order.OrderId equals ort.OrderId
                            //where order.OrderId == orderVM.OrderId
                            where order.QuoteId == orderVM.QuoteId
                            select new OrderViewModel
                {
                    OrderId                     = order.OrderId,
                    ProjectId                   = project.ProjectId,
                    QuoteId                     = order.QuoteId,
                    ProjectOwner                = owner.FirstName + " " + owner.LastName,
                    ProjectOwnerId              = owner.UserId,
                    BusinessId                  = owner.BusinessId.Value,
                    BusinessName                = business.BusinessName,
                    OrderStatusTypeId           = (OrderStatusTypeEnum)order.OrderStatusTypeId,
                    CreatedByUserId             = mod.UserId,
                    UpdatedByUserId             = mod.UserId,
                    UpdatedByUser               = mod.FirstName + " " + mod.LastName,
                    DiscountRequestId           = order.DiscountRequestId,
                    CommissionRequestId         = order.CommissionRequestId,
                    ShipToAddressId             = order.ShipToAddressId,
                    PricingTypeId               = order.PricingTypeId,
                    PONumber                    = order.PONumber,
                    TotalDiscountPercent        = order.TotalDiscountPercent,
                    Comments                    = order.Comments,
                    EstimatedReleaseDate        = order.EstimatedReleaseDate,
                    EstimatedDeliveryDate       = (project.EstimatedDelivery != null) ? project.EstimatedDelivery : DateTime.Now,
                    DeliveryAppointmentRequired = order.DeliveryAppointmentRequired,
                    DeliveryContactName         = order.DeliveryContactName,
                    DeliveryContactPhone        = order.DeliveryContactPhone,
                    SubmittedByUserId           = mod.UserId,
                    SubmitDate                  = (order.SubmitDate != null) ? order.SubmitDate : DateTime.Now,
                    ProjectDate                 = (project.ProjectDate != null) ? project.ProjectDate : DateTime.Now,
                    ERPOrderDate                = (order.ERPOrderDate != null) ? order.ERPOrderDate.Value : (System.DateTime?)null,
                    ERPInvoiceNumber            = (order.ERPInvoiceNumber != null) ? order.ERPInvoiceNumber : null,
                    ERPComments                 = (order.ERPComment != null) ? order.ERPComment : null,
                    ERPPOKey                    = (order.ERPPOKey != null) ? order.ERPPOKey.Value : (int?)null,
                    ERPStatus                   = (order.ERPStatus != null) ? order.ERPStatus : null,
                    POAttachmentFileName        = ort.FileName,
                    TotalNetPrice               = quote.TotalNet,
                    OrderReleaseDate            = order.OrderReleaseDate,
                    Timestamp                   = (order.Timestamp != null) ? order.Timestamp : DateTime.Now
                };

                orders = query.ToList();
            }

            if (orders == null)
            {
                this.Response.Messages.AddError("Order not found");
                return(this.Response);
            }
            else
            {
                //FinaliseModel(user, model);
                this.Response.Model = orders;
            }

            return(this.Response);
        }