Пример #1
0
        protected override void Insert(Venda entity)
        {
            if (entity.DiaVencimento == 0)
            {
                throw new Exception("O dia de vencimento deve ser informada corretamente.");
            }

            using (var scope = new TransactionScope())
            {
                var loteBo       = new LoteLogic();
                var loteamentoBo = new LoteamentoLogic();
                var lote         = loteBo.Get(entity.LoteId);

                if (lote.Comprado)
                {
                    throw new Exception("O lote selecionado foi comprado por outra pessoa enquanto você concluía sua aquisição.\nPor favor, selecione outro lote.");
                }

                lote.Comprado = true;
                loteBo.Save(lote);

                if (lote.LoteamentoId != null)
                {
                    lote.Loteamento = loteamentoBo.Get((long)lote.LoteamentoId);
                }

                if (entity.QuantParcelas < 1 || entity.QuantParcelas > lote.Loteamento.QuantParcelas)
                {
                    throw new Exception($"A quantidade de parcelas deve estar entre 1 e {lote.Loteamento.QuantParcelas}.");
                }

                entity.Valor        = lote.Valor;
                entity.ValorParcela = entity.Valor / entity.QuantParcelas;
                entity.DataHora     = DateTime.Now;

                base.Insert(entity);

                var tituloBo = new TituloLogic();
                tituloBo.GerarTitulos(entity.Id);

                scope.Complete();
            }
        }
Пример #2
0
        public IEnumerable <Lote> Generate(long id)
        {
            //Criar a lista de lotes
            var lotes = new List <Lote>();

            //Instanciar o bo do loteamneto e recuperar o loteamento selecionado
            var loteamentoBo = new LoteamentoLogic();
            var loteamento   = loteamentoBo.Get(id);

            //Caso o loteamento não possua mapa retorna nulo
            if (loteamento.MapaId == null)
            {
                return(null);
            }

            //Formata o source do mapa removendo os carateres adicionados de base64
            var source = Regex.Replace(loteamento.Mapa.Source, @"data:image/svg\+xml;base64,?", "");

            //Converte o base64 em um array de bytes
            var buffer = Convert.FromBase64String(source);

            using (var ms = new MemoryStream(buffer))
            {
                //Carrega o arquivo SVG (que na verdade é um XML)
                var doc = XDocument.Load(ms);

                //Selecione os retangulos (rects) do svg, calcula a area e adiciona o lote
                var rects = doc.Descendants().Where(x => x.Name.LocalName == "rect");
                foreach (var rect in rects)
                {
                    var width  = Convert.ToDecimal(rect.Attribute("width").Value);
                    var height = Convert.ToDecimal(rect.Attribute("height").Value);
                    var style  = rect.Attribute("style").Value;
                    var cor    = Regex.Match(style, @"fill:\s*(\#*.*);").Groups[1].Value.Split(';')[0];

                    lotes.Add(new Lote
                    {
                        Codigo       = rect.Attribute("id").Value,
                        Area         = Area.Retangulo(width, height),
                        StatusAdicao = StatusAdicao.Adicao,
                        Cor          = cor
                    });
                }

                //Selecione os caminhos (paths) do svg, calcula a area e adiciona o lote
                var paths = doc.Descendants().Where(x => x.Name.LocalName == "path");
                foreach (var path in paths)
                {
                    var d = SvgPathBuilder.Parse(path.Attribute("d").Value);

                    var points = new List <PointF>();
                    foreach (var seg in d)
                    {
                        if (seg != d.Last)
                        {
                            points.Add(new PointF(seg.Start.X, seg.Start.Y));
                            points.Add(new PointF(seg.End.X, seg.End.Y));
                        }

                        if (seg.GetType() == typeof(SvgCubicCurveSegment))
                        {
                            var cubic = (SvgCubicCurveSegment)seg;

                            points.Add(new PointF(cubic.FirstControlPoint.X, cubic.FirstControlPoint.Y));
                            points.Add(new PointF(cubic.SecondControlPoint.X, cubic.SecondControlPoint.Y));
                        }
                    }

                    points.Add(points[0]);
                    var area = (int)Math.Abs(points.Take(points.Count - 1)
                                             .Select((p, i) => (points[i + 1].X - p.X) * (points[i + 1].Y + p.Y))
                                             .Sum() / 2);

                    var style = path.Attribute("style").Value;
                    var cor   = Regex.Match(style, @"fill:\s*(\#*.*);").Groups[1].Value.Split(';')[0];

                    lotes.Add(new Lote
                    {
                        Codigo       = path.Attribute("id").Value,
                        Area         = area,
                        StatusAdicao = StatusAdicao.Adicao,
                        Cor          = cor
                    });
                }
            }

            //Recuperar os lotes do loteamento
            var lotesDb = List(id).ToList();

            //Marcar os lotes como alteração
            foreach (var loteDb in lotesDb)
            {
                var lote = lotes.FirstOrDefault(x => x.Codigo == loteDb.Codigo);

                if (lote != null)
                {
                    loteDb.StatusAdicao = StatusAdicao.Alteracao;
                    lotes.Insert(lotes.IndexOf(lote), loteDb);
                    lotes.Remove(lote);
                }
            }

            //Marcar os lotes como remoção
            var lotesRemocao = lotesDb.Where(x => !lotes.Select(l => l.Codigo).Contains(x.Codigo));

            foreach (var lote in lotesRemocao)
            {
                lote.StatusAdicao = StatusAdicao.Remocao;
            }

            //Retornar a lista de lotes
            return(lotes.OrderBy(x => x.Codigo));
        }