private void SaveClients(AddCustomerAdminViewModel model)
        {
            if (model.LetUsGetInfo)
            {
                var customer = new Customer
                {
                    Address           = model.Address,
                    City              = model.City,
                    Email             = model.Email,
                    Firstname         = model.Firstname,
                    IpAdress          = model.IpAdress,
                    Lastname          = model.Lastname,
                    SocialSecurity    = model.SocialSecurity,
                    HasConfirmed      = model.HasConfirmed,
                    Paymentmethod     = model.Paymentmethod,
                    Postnumber        = model.Postnumber,
                    ClientId          = model.ClientId,
                    LetUsGetInfo      = model.LetUsGetInfo,
                    DaySigned         = DateTime.Now,
                    PaymentAddress    = model.FakturaAddress != null ? model.FakturaAddress : model.Address,
                    PaymentCity       = model.FakturaCity != null ? model.FakturaCity : model.City,
                    PaymentPostnumber = model.FakturaPostnumber != null ? model.FakturaPostnumber : model.Postnumber
                };
                db.Customers.Add(customer);
                db.SaveChanges();
            }
            else
            {
                var customer = new Customer
                {
                    Address           = model.Address,
                    City              = model.City,
                    DaySigned         = DateTime.Now,
                    Email             = model.Email,
                    Firstname         = model.Firstname,
                    IpAdress          = model.IpAdress,
                    Lastname          = model.Lastname,
                    SocialSecurity    = model.SocialSecurity,
                    HasConfirmed      = model.HasConfirmed,
                    Paymentmethod     = model.Paymentmethod,
                    Postnumber        = model.Postnumber,
                    ClientId          = model.ClientId,
                    LetUsGetInfo      = model.LetUsGetInfo,
                    AreaCode          = model.AreaCode,
                    StartDate         = model.StartDate,
                    PropertyCode      = model.PropertyCode,
                    PaymentAddress    = model.FakturaAddress != null ? model.FakturaAddress : model.Address,
                    PaymentCity       = model.FakturaCity != null ? model.FakturaCity : model.City,
                    PaymentPostnumber = model.FakturaPostnumber != null ? model.FakturaPostnumber : model.Postnumber
                };

                db.Customers.Add(customer);
                db.SaveChanges();
            }
        }
Beispiel #2
0
        private void RemoveContent(string typ, int förbrukning, int ElområdeId)
        {
            DbEl db   = new DbEl();
            var  list = db.ScrapeModels.Where(i => i.Typ == typ && i.Förbrukning == förbrukning && i.ElområdeId == ElområdeId).ToList();

            db.ScrapeModels.RemoveRange(list);
            db.SaveChanges();
        }
        public void RemovePost(int id)
        {
            var db   = new DbEl();
            var post = db.BlogPosts.Find(id);

            db.BlogPosts.Remove(post);
            db.SaveChanges();
        }
Beispiel #4
0
        public bool SaveElBolag(ElBolagViewModel el)
        {
            var IsUnique = db.Companies.Any(i => i.Name == el.Name);

            if (!IsUnique)
            {
                var imgPath = SaveImage(el.Image);
                var bolag   = new ElBolag {
                    Name  = el.Name,
                    Phone = el.Phone,
                    Image = imgPath
                };
                db.Companies.Add(bolag);
                db.SaveChanges();
                return(true);
            }
            return(false);
        }
Beispiel #5
0
        public void SaveDeal(AddDealViewModel deal)
        {
            var client = new ClientModel
            {
                Price = deal.Price,
                Automatiskförlängning = deal.Automatiskförlängning,
                Contract           = deal.Contract,
                ElBolagId          = deal.ElBolagId,
                ElområdeId         = deal.ElområdeId,
                Engångsavgift      = deal.Engångsavgift,
                ExtraInfo          = deal.ExtraInfo,
                Fastpris           = deal.Fastpris,
                MinFörbrukning     = deal.MinFörbrukning,
                Appartment         = deal.Appartment,
                Autogiro           = deal.Autogiro,
                Bio                = deal.Bio,
                EFaktura           = deal.EFaktura,
                House              = deal.House,
                Miljömärkt         = deal.Miljömärkt,
                Pappersfaktura     = deal.Pappersfaktura,
                Vind               = deal.Vind,
                Sol                = deal.Sol,
                Vatten             = deal.Vatten,
                Förbrukning        = deal.MaxFörbrukning,
                Rating             = deal.Rating,
                Miljöpåslag        = deal.Miljöpåslag,
                ÅrsAvgift          = deal.ÅrsAvgift,
                Uppsägningstid     = deal.Uppsägningstid,
                Typ                = deal.Typ,
                Moms               = deal.Moms,
                Omteckningsrätt    = deal.Omteckningsrätt,
                RörligtPåslag      = deal.RörligtPåslag,
                RörligtMiljöpåslag = deal.RörligtMiljöpåslag,
                RörligtInköpsPris  = deal.RörligtInköpsPris,
                Rabatt             = deal.Rabatt
            };

            db.ClientModels.Add(client);
            db.SaveChanges();
        }
        public void UpdatePost(AddBlogPostViewModel model)
        {
            var db   = new DbEl();
            var post = db.BlogPosts.Find(model.BlogModelId);

            post.Header      = model.Header;
            post.HtmlContent = model.HtmlContent;
            post.Ingress     = model.Ingress;
            post.Timestamp   = DateTime.Now;

            db.Entry(post).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void SavePost(AddBlogPostViewModel model)
        {
            var db   = new DbEl();
            var post = new BlogModel
            {
                Header      = model.Header,
                HtmlContent = model.HtmlContent,
                ImagePath   = _ImageHelper.SaveImage(model.Image),
                Ingress     = model.Ingress,
                Timestamp   = DateTime.Now
            };

            db.BlogPosts.Add(post);
            db.SaveChanges();
        }
Beispiel #8
0
        public void UpdateClientDeal(AddDealViewModel model)
        {
            var _db       = new DbEl();
            var deal      = _db.ClientModels.Find(model.ClientId);
            var HasChange = EqualsClient(model, deal);

            if (HasChange)
            {
                return;
            }

            if (_db.Customers.Any(i => i.ClientId == deal.ClientId))
            {
                throw new Exception();
            }

            _db.ClientModels.Remove(deal);
            SaveDeal(model);

            _db.SaveChanges();
        }
        public void UpdateJsonFromFile()
        {
            try
            {
                FillDbEl();


                string path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Models/Webscrape/"), "jsonEl.json");
                if (File.Exists(path))
                {
                    var watch1 = new Stopwatch();
                    watch1.Start();
                    dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(path, Encoding.UTF8));
                    watch1.Stop();
                    System.Diagnostics.Debug.WriteLine("Läsa in fil: " + watch1.Elapsed);

                    var el1 = db.Elområden.Single(i => i.Area == "SE01");
                    var el2 = db.Elområden.Single(i => i.Area == "SE02");
                    var el3 = db.Elområden.Single(i => i.Area == "SE03");
                    var el4 = db.Elområden.Single(i => i.Area == "SE04");

                    var watch2 = new Stopwatch();
                    watch2.Start();

                    var list = new List <Postnummer>();

                    foreach (var obj in json)
                    {
                        if (obj.Elomrade == "SE01")
                        {
                            var postnummer = new Postnummer
                            {
                                Number     = obj.Postnummer,
                                ElområdeId = el1.ElområdeId
                            };
                            list.Add(postnummer);

                            continue;
                        }

                        if (obj.Elomrade == "SE02")
                        {
                            var postnummer = new Postnummer
                            {
                                Number     = obj.Postnummer,
                                ElområdeId = el2.ElområdeId
                            };
                            list.Add(postnummer);

                            continue;
                        }

                        if (obj.Elomrade == "SE03")
                        {
                            var postnummer = new Postnummer
                            {
                                Number     = obj.Postnummer,
                                ElområdeId = el3.ElområdeId
                            };
                            list.Add(postnummer);

                            continue;
                        }
                        else
                        {
                            var postnummer = new Postnummer
                            {
                                Number     = obj.Postnummer,
                                ElområdeId = el4.ElområdeId
                            };
                            list.Add(postnummer);

                            continue;
                        }
                    }
                    watch2.Stop();
                    System.Diagnostics.Debug.WriteLine("Sortera poster: " + watch2.Elapsed);

                    var watch3 = new Stopwatch();
                    watch3.Start();
                    db.Postnummers.AddRange(list);
                    db.SaveChanges();
                    watch3.Stop();
                    System.Diagnostics.Debug.WriteLine("Spara i databasen: " + watch3.Elapsed);
                }
            }
            catch (Exception exc) {
                throw new Exception();
            }
        }
Beispiel #10
0
        public async Task <bool> GetHtmlAsync(string typ, int förbrukning, string url, int ElområdeId)
        {
            DbEl db = new DbEl();
            var  r  = new Random();

            if (IsExisting(typ, förbrukning, ElområdeId))
            {
                RemoveContent(typ, förbrukning, ElområdeId);
            }

            var httpClient = new HttpClient();
            var html       = await httpClient.GetStringAsync(url);

            var htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(html);

            var AllListElements = htmlDoc.DocumentNode.Descendants("li")
                                  .Where(node => node.GetAttributeValue("class", "")
                                         .StartsWith("panel epk-list-item")).ToList();

            var Links = htmlDoc.DocumentNode.Descendants("a")
                        .Where(node => node.GetAttributeValue("href", "")
                               .StartsWith("/sv/Elpriskollen/Avtalssida/?ellevid="))
                        .Select(i => i.GetAttributeValue("href", "").Replace("amp;", "")).ToList();

            var headers = AllListElements.Select(i => i.Descendants("h3").Select(x => x.InnerText).ToList()).ToList();

            var Price = htmlDoc.DocumentNode.Descendants("div")
                        .Where(node => node.GetAttributeValue("class", "")
                               .Equals("epk-list-price col-md-2 col-sm-3 ")).Select(i => i.InnerText).ToList();

            var Company = htmlDoc.DocumentNode.Descendants("dd")
                          .Where(node => node.GetAttributeValue("class", "")
                                 .Equals("epk-list-contract-company")).Select(i => i.InnerText).ToList();

            var Contract = htmlDoc.DocumentNode.Descendants("dd")
                           .Where(node => node.GetAttributeValue("class", "")
                                  .Equals("epk-list-contract-type")).Select(i => i.InnerText).ToList();



            var list = new List <ScrapeModel>();

            for (int i = 0; i < Links.Count; i++)
            {
                var model = new ScrapeModel();
                model.Company     = Company[i].Trim();
                model.Contract    = Contract[i].Trim();
                model.Price       = Price[i].Trim();
                model.ExtraInfo   = headers[i][0].Trim();
                model.Typ         = typ;
                model.Förbrukning = förbrukning;
                model.ElområdeId  = ElområdeId;
                model.Rating      = 0;
                var extraUrl    = "https://www.ei.se" + Links[i];
                var extraClient = new HttpClient();
                var extraHtml   = await httpClient.GetStringAsync(extraUrl);

                var extraHtmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(extraHtml);

                var ExtraAttributes = htmlDoc.DocumentNode.Descendants("td")
                                      .Where(node => node.GetAttributeValue("class", "")
                                             .StartsWith("value")).Select(node => node.InnerText.Trim()).ToList();

                var PaymentMethod = htmlDoc.DocumentNode.Descendants("td")
                                    .Where(node => node.GetAttributeValue("class", "")
                                           .Equals("well")).Select(x => x.InnerText).ToList()[5].Split(',');

                model.Autogiro       = PaymentMethod.Any(p => p.Trim().Equals("Autogiro")) ? true : false;
                model.EFaktura       = PaymentMethod.Any(p => p.Trim().Equals("E-faktura")) ? true : false;
                model.Pappersfaktura = PaymentMethod.Any(p => p.Trim().Equals("Traditionell")) ? true : false;


                var SunSource = htmlDoc.DocumentNode.Descendants("li")
                                .Where(node => node.GetAttributeValue("class", "")
                                       .Equals("epk-energy-type epk-energy-solar")).Select(x => x.InnerText).ToList();
                model.Sol = SunSource.Count > 0 ? true : false;

                var WindSource = htmlDoc.DocumentNode.Descendants("li")
                                 .Where(node => node.GetAttributeValue("class", "")
                                        .Equals("epk-energy-type epk-energy-wind")).Select(x => x.InnerText).ToList();
                model.Vind = WindSource.Count > 0 ? true : false;

                var WaterSource = htmlDoc.DocumentNode.Descendants("li")
                                  .Where(node => node.GetAttributeValue("class", "")
                                         .Equals("epk-energy-type epk-energy-water")).Select(x => x.InnerText).ToList();
                model.Vatten = WaterSource.Count > 0 ? true : false;

                var BioSource = htmlDoc.DocumentNode.Descendants("li")
                                .Where(node => node.GetAttributeValue("class", "")
                                       .Equals("epk-energy-type epk-energy-bio")).Select(x => x.InnerText).ToList();
                model.Bio = BioSource.Count > 0 ? true : false;

                var EnvironmentalSource = htmlDoc.DocumentNode.Descendants("li")
                                          .Where(node => node.GetAttributeValue("class", "")
                                                 .Equals("epk-energy-type epk-bramiljoval")).Select(x => x.InnerText).ToList();
                model.Miljömärkt = EnvironmentalSource.Count > 0 ? true : false;


                model.ÅrsAvgift          = ExtraAttributes[0];
                model.Engångsavgift      = ExtraAttributes[1];
                model.Fastpris           = ExtraAttributes[2];
                model.RörligtInköpsPris  = ExtraAttributes[3];
                model.RörligtPåslag      = ExtraAttributes[4];
                model.Miljöpåslag        = ExtraAttributes[5];
                model.RörligtMiljöpåslag = ExtraAttributes[6];
                model.Rabatt             = ExtraAttributes[7];
                model.Moms                  = ExtraAttributes[10];
                model.Uppsägningstid        = ExtraAttributes[15];
                model.Automatiskförlängning = ExtraAttributes[16];
                model.Omteckningsrätt       = ExtraAttributes[17];
                list.Add(model);
            }
            db.ScrapeModels.AddRange(list);
            db.SaveChanges();
            return(true);
        }
        private bool MoveCustomer(Customer c)
        {
            var httpContext = HttpContext.Current;
            var db          = new DbEl();

            if (c.ScrapeId != null)
            {
                var deleted = new RemovedUserModel
                {
                    Address        = c.Address,
                    AdminName      = httpContext.User.Identity.Name,
                    AreaCode       = c.AreaCode,
                    City           = c.City,
                    Company        = c.ScrapeModel.Company,
                    Contract       = c.ScrapeModel.Contract,
                    DateMoved      = DateTime.Now.ToString(),
                    DaySigned      = c.DaySigned,
                    Email          = c.Email,
                    ExtraInfo      = c.ScrapeModel.ExtraInfo,
                    Firstname      = c.Firstname,
                    Förbrukning    = c.ScrapeModel.Förbrukning,
                    HasConfirmed   = c.HasConfirmed,
                    IpAdress       = c.IpAdress,
                    Lastname       = c.Lastname,
                    LetUsGetInfo   = c.LetUsGetInfo,
                    Paymentmethod  = c.Paymentmethod,
                    Postnumber     = c.Postnumber,
                    Price          = c.ScrapeModel.Price,
                    PropertyCode   = c.PropertyCode,
                    SocialSecurity = c.SocialSecurity,
                    StartDate      = c.StartDate,
                    Typ            = c.ScrapeModel.Typ,
                    IsClient       = false,
                    Elområde       = c.ScrapeModel.Elområde.Area
                };
                db.DeletedCustomer.Add(deleted);
                db.SaveChanges();
                return(true);
            }
            else if (c.ClientId != null)
            {
                var deleted = new RemovedUserModel
                {
                    Address        = c.Address,
                    AdminName      = httpContext.User.Identity.Name,
                    AreaCode       = c.AreaCode,
                    City           = c.City,
                    Company        = c.ClientModel.ElBolag.Name,
                    Contract       = c.ClientModel.Contract,
                    DateMoved      = DateTime.Now.ToString(),
                    DaySigned      = c.DaySigned,
                    Email          = c.Email,
                    ExtraInfo      = c.ClientModel.ExtraInfo,
                    Firstname      = c.Firstname,
                    Förbrukning    = c.ClientModel.Förbrukning,
                    HasConfirmed   = c.HasConfirmed,
                    IpAdress       = c.IpAdress,
                    Lastname       = c.Lastname,
                    LetUsGetInfo   = c.LetUsGetInfo,
                    Paymentmethod  = c.Paymentmethod,
                    Postnumber     = c.Postnumber,
                    Price          = c.ClientModel.Price.ToString(),
                    PropertyCode   = c.PropertyCode,
                    SocialSecurity = c.SocialSecurity,
                    StartDate      = c.StartDate,
                    Typ            = c.ClientModel.Typ,
                    IsClient       = true,
                    Elområde       = c.ClientModel.Elområde.Area
                };
                db.DeletedCustomer.Add(deleted);
                db.SaveChanges();
                return(true);
            }
            return(false);
        }