private BalustradeQuoteModel GetModel(string id, int type, int color, int glass, string mes, int price)
        {
            var model = DbSession.QueryOver <BalustradeModel>().Where(bm => bm.Url == id).Cacheable().CacheMode(CacheMode.Normal).SingleOrDefault();

            if (model == null)
            {
                return(null);
            }
            var t = DbSession.Get <BalustradeSystemLocal>(type);
            var c = t.Colors.FirstOrDefault(clr => clr.ID == color) ?? t.Colors.FirstOrDefault();
            var g = t.GlassSystems.FirstOrDefault(gl => gl.ID == glass) ?? t.GlassSystems.FirstOrDefault();

            var bal = new Balustrade {
                BalustradeSystem = t,
                Color            = c,
                GlassSystem      = g
            };

            bal.UpdateSystemDefaults();
            bal.StartType = BalustradeSection.SectionFinishType.Wall;
            bal.EndType   = BalustradeSection.SectionFinishType.Wall;
            bal.ModelId   = model.ID;

            var mesParts = (mes ?? "").Split(";".ToCharArray())
                           .Select(p =>
            {
                int parsed;
                return(Int32.TryParse(p, out parsed) ? parsed : 0);
            })
                           .Where(p => 0 < p && p < 999999)
                           .ToArray();

            var angle = mesParts.Length > 1 ? 90D : 0D;
            var i     = 0;

            foreach (var dim in mesParts)
            {
                bal.RawBalustradeSections.Add(new BalustradeSection
                {
                    Balustrade              = bal,
                    Length                  = dim,
                    Angle                   = angle,
                    Depth                   = (i < model.Sections.Count && model.Sections[i].Curved) ? 10 : 0,
                    CalculatePostsQuantity  = true,
                    CalculateHandrailJoints = true,
                    PreferStockGlass        = t.PreferStockGlass
                });
                i++;
                angle -= 90;
            }

            var viewModel = new BalustradeQuoteModel(model, bal, price, DbSession.CurrentUser());

            return(viewModel);
        }
        public ActionResult AmendQuote(Guid?id)
        {
            Quote quote;

            if (!id.HasValue ||
                (quote = DbSession.Get <Quote>(id.Value)) == null ||
                quote.FirstLine == null)
            {
                return(HttpNotFound());
            }
            var bal = quote.FirstLine.ProductDetails as Balustrade;

            if (bal == null || bal.ModelId == null)
            {
                return(HttpNotFound());
            }

            var model = DbSession.Get <BalustradeModel>(bal.ModelId.Value);

            var viewmodel = new BalustradeQuoteModel(model, bal);

            return(View("Model", viewmodel));
        }
        public ActionResult Quote(BalustradeQuoteModel model)
        {
            if (!model.ModelId.HasValue)
            {
                return(RedirectToAction("quote"));
            }

            var balustrade = new Balustrade();

            balustrade.Color            = DbSession.Get <ColorLocal>(model.ColorId);
            balustrade.GlassSystem      = DbSession.Get <GlassSystemLocal>(model.GlassId);
            balustrade.BalustradeSystem = DbSession.Get <BalustradeSystemLocal>(model.TypeId);
            balustrade.UpdateSystemDefaults();
            balustrade.StartType = BalustradeSection.SectionFinishType.Wall;
            balustrade.EndType   = BalustradeSection.SectionFinishType.Wall;
            balustrade.ModelId   = model.ModelId;

            var angle = model.Dims.Count > 1 ? 90D : 0D;

            foreach (var dimension in model.Dims)
            {
                var section = new BalustradeSection
                {
                    Balustrade              = balustrade,
                    Angle                   = angle,
                    Length                  = dimension.Length ?? 0,
                    Depth                   = dimension.Curved ? 10 : 0,
                    CalculatePostsQuantity  = true,
                    CalculateHandrailJoints = true,
                    PreferStockGlass        = balustrade.BalustradeSystem.PreferStockGlass
                };
                balustrade.RawBalustradeSections.Add(section);
                angle -= 90;
            }

            var modelFromDb = DbSession.Get <BalustradeModel>(model.ModelId.Value);

            Session[SessionKeys.LAST_QUOTE] = new BalustradeQuoteModel(modelFromDb, balustrade, model.P);

            if (model.Action == BalustradeQuoteModel.AddToCartText)
            {
                DbSession.SaveOrUpdate(balustrade);

                AddToCart(balustrade, 1);
                DbSession.Flush();

                return(RedirectToAction("cart", "customer", new { areas = AreaKind }));
            }

            var line = new QuoteLine();

            line.Name        = balustrade.Name;
            line.Price       = balustrade.SilverSellingPrice;
            line.BronzePrice = balustrade.BronzeSellingPrice;
            line.GoldPrice   = balustrade.GoldSellingPrice;

            line.ProductDetails = balustrade;
            Session[SessionKeys.PENDING_QUOTE_LINE] = line;


            return(RedirectToAction("create-quote", "customer", new { areas = AreaKind }));
        }