/// <summary> /// Loads all people donations from web /// </summary> public static async Task LoadDonationsAsync(string url, Donations donations, dynamic party, int year) { dynamic donationRecords = await LoadIndexAsync(url); foreach (var record in donationRecords) { Donor donor = new Donor() { City = record.addrCity, CompanyId = record.companyId ?? 0, Name = record.firstName, Surname = record.lastName, TitleBefore = record.titleBefore, TitleAfter = record.titleAfter, DateOfBirth = record.birthDate }; Gift gift = new Gift() { Amount = record.money ?? record.value, ICO = party.ic, Party = party.longName, Description = record.description, Date = record.date ?? new DateTime(year, 1, 1), GiftType = (record.money is null) ? Sponzoring.TypDaru.NefinancniDar : Sponzoring.TypDaru.FinancniDar }; donations.AddDonation(donor, gift); } }
/// <summary> /// Loads all people donations from web /// </summary> /// <param name="url"></param> /// <param name="donations"></param> /// <param name="party"></param> /// <param name="year"></param> /// <returns></returns> public static async Task LoadPeopleDonationsAsync(string url, Donations donations, dynamic party, int year) { dynamic donationRecords = await LoadIndexAsync(url); foreach (var record in donationRecords) { Donor donor = new Donor() { City = record.addrCity, Name = record.firstName, Surname = record.lastName, TitleBefore = record.titleBefore, TitleAfter = record.titleAfter, DateOfBirth = record.birthDate }; Gift gift = new Gift() { Amount = record.money ?? record.value, ICO = party.ic, Party = party.longName, Description = record.description, Date = record.date ?? new DateTime(year, 1, 1) }; donations.AddDonation(donor, gift); } }
/// <summary> /// Loads all people donations from web /// </summary> public static async Task LoadDonationsAsync(string url, Donations donations, dynamic party, int year) { dynamic donationRecords = await LoadIndexAsync(url); foreach (var record in donationRecords) { string firstName = record.firstName; string lastName = record.lastName; string titlesBefore = record.titleBefore; string titlesAfter = record.titleAfter; var cleanedName = Validators.SeparateNameFromTitles(firstName); var cleanedLastName = Validators.SeparateNameFromTitles(lastName); titlesBefore = MergeTitles(titlesBefore, cleanedName.titulyPred, cleanedLastName.titulyPred); titlesAfter = MergeTitles(titlesAfter, cleanedName.titulyPo, cleanedLastName.titulyPo); // if (!string.IsNullOrEmpty(cleanedName.titulyPred) // || !string.IsNullOrEmpty(cleanedName.titulyPo) // || !string.IsNullOrEmpty(cleanedLastName.titulyPred) // || !string.IsNullOrEmpty(cleanedLastName.titulyPo)) // { // var lines = new List<string>() // { // $"Opraven titul pro {party.longName} [{party.ic}] za rok [{year}]:", // $"jmeno: {firstName} => {cleanedName.jmeno}", // $"prijmeni: {lastName} => {cleanedLastName.jmeno}", // $"titulyPred: {titlesBefore}", // $"titulyZa: {titlesAfter}" // }; // System.IO.File.AppendAllLines(@"d:\udhpsherrs.txt",lines,Encoding.UTF8); // } Donor donor = new Donor() { City = record.addrCity, CompanyId = record.companyId, Name = cleanedName.jmeno, Surname = cleanedLastName.jmeno, TitleBefore = titlesBefore, TitleAfter = titlesAfter, DateOfBirth = record.birthDate }; Gift gift = new Gift() { Amount = record.money ?? record.value, ICO = party.ic, Party = party.longName, Description = record.description, Date = record.date ?? new DateTime(year, 1, 1), GiftType = (record.money is null) ? Sponzoring.TypDaru.NefinancniDar : Sponzoring.TypDaru.FinancniDar }; donations.AddDonation(donor, gift); } }
/// <summary> /// Uploads new donations to FirmaEvent table /// </summary> /// <param name="donations"></param> public static void UploadCompanyDonations(Donations donations) { foreach (var companyDonations in donations.GetDonations()) { var donor = companyDonations.Key; Firma firma = null; try { firma = Firma.FromIco(donor.CompanyId); } catch (Exception) { } if (firma is null) { Console.WriteLine($"Chybějící firma v db - ICO: {donor.CompanyId}, nazev: {donor.Name}"); continue; } var firmaEvents = firma.Events(ev => ev.Type == (int)FirmaEvent.Types.Sponzor).ToList(); foreach (var donation in companyDonations.Value) { var eventToRemove = firmaEvents.Where(oe => oe.AddInfoNum == donation.Amount && oe.Description == donation.ICO && oe.DatumOd.HasValue && oe.DatumOd.Value.Year == donation.Date.Year).FirstOrDefault(); if (eventToRemove is null) { // add event var newEvent = new FirmaEvent() { AddInfo = NormalizePartyName(donation.Party, donation.ICO), DatumOd = donation.Date, AddInfoNum = donation.Amount, Description = donation.ICO, Zdroj = _zdroj, Note = donation.Description, Type = (int)FirmaEvent.Types.Sponzor }; firma.AddOrUpdateEvent(newEvent, _user, checkDuplicates: false); } else { firmaEvents.Remove(eventToRemove); } } } }
static async Task Main(string[] args) { _partyNames = LoadPartyNames(); var peopleDonations = new Donations(new DonorEqualityComparer()); var companyDonations = new Donations(new DonorEqualityComparer()); #region loading from web foreach (string indexUrl in _addresses) { var index = await LoadIndexAsync(indexUrl); string key = index.election.key; int year = GetYearFromText(key); foreach (var party in index.parties) { IEnumerable <dynamic> files = party.files; // osoby string penizeFoUrl = files.Where(f => f.subject == "penizefo").Select(f => f.url).FirstOrDefault(); await LoadDonationsAsync(penizeFoUrl, peopleDonations, party, year); string nepenizeFoUrl = files.Where(f => f.subject == "bupfo").Select(f => f.url).FirstOrDefault(); await LoadDonationsAsync(nepenizeFoUrl, peopleDonations, party, year); //firmy string penizePoUrl = files.Where(f => f.subject == "penizepo").Select(f => f.url).FirstOrDefault(); await LoadDonationsAsync(penizePoUrl, companyDonations, party, year); string nepenizePoUrl = files.Where(f => f.subject == "buppo").Select(f => f.url).FirstOrDefault(); await LoadDonationsAsync(nepenizePoUrl, companyDonations, party, year); } } #endregion #region saving to db UploadPeopleDonations(peopleDonations); UploadCompanyDonations(companyDonations); #endregion }
/// <summary> /// Uploads new donations to OsobaEvent table /// </summary> /// <param name="donations"></param> public static void UploadPeopleDonations(Donations donations) { foreach (var personDonations in donations.GetDonations()) { var donor = personDonations.Key; Osoba osoba = Osoba.GetOrCreateNew(donor.TitleBefore, donor.Name, donor.Surname, donor.TitleAfter, donor.DateOfBirth, Osoba.StatusOsobyEnum.Sponzor, _user); var osobaEvents = osoba.NoFilteredEvents(ev => ev.Type == (int)OsobaEvent.Types.Sponzor).ToList(); foreach (var donation in personDonations.Value) { var eventToRemove = osobaEvents.Where(oe => oe.AddInfoNum == donation.Amount && oe.AddInfo == donation.ICO && oe.DatumOd.HasValue && oe.DatumOd.Value.Year == donation.Date.Year).FirstOrDefault(); if (eventToRemove is null) { // add event var newEvent = new OsobaEvent() { Organizace = NormalizePartyName(donation.Party, donation.ICO), DatumOd = donation.Date, AddInfoNum = donation.Amount, AddInfo = donation.ICO, Zdroj = _zdroj, Note = donation.Description, Type = (int)OsobaEvent.Types.Sponzor }; osoba.AddOrUpdateEvent(newEvent, _user, checkDuplicates: false); } else { osobaEvents.Remove(eventToRemove); } } } }
/// <summary> /// Uploads new donations to FirmaEvent table /// </summary> public static void UploadCompanyDonations(Donations donations) { foreach (var companyDonations in donations.GetDonations()) { var donor = companyDonations.Key; Firma firma = null; try { firma = Firma.FromIco(donor.CompanyId); } catch (Exception ex) { Console.WriteLine(ex.Message); } if (firma is null) { Console.WriteLine($"Chybějící firma v db - ICO: {donor.CompanyId}, nazev: {donor.Name}"); continue; } foreach (var donation in companyDonations.Value) { // add event var sponzoring = new Sponzoring() { DarovanoDne = donation.Date, Hodnota = donation.Amount, IcoPrijemce = donation.ICO, Zdroj = _zdroj, Popis = donation.Description, Typ = (int)donation.GiftType }; firma.AddSponsoring(sponzoring, _user); } } }
/// <summary> /// Loads all company donations from web /// </summary> /// <param name="url"></param> /// <param name="donations"></param> /// <param name="party"></param> /// <param name="year"></param> /// <returns></returns> public static async Task LoadCompanyDonationsAsync(string url, Donations donations, dynamic party, int year) { dynamic donationRecords = await LoadIndexAsync(url); foreach (var record in donationRecords) { Donor donor = null; try { donor = new Donor() { City = record.addrCity, Name = record.company, CompanyId = record.companyId }; } catch (Exception) { donor = new Donor() { City = record.addrCity, Name = record.company, }; Console.WriteLine($"Špatný formát IČO: [{record.companyId}], url:[{url}]"); } Gift gift = new Gift() { Amount = record.money ?? record.value, ICO = party.ic, Party = party.longName, Description = record.description, Date = record.date ?? new DateTime(year, 1, 1) }; donations.AddDonation(donor, gift); } }
/// <summary> /// Uploads new donations to OsobaEvent table /// </summary> public static void UploadPeopleDonations(Donations donations) { foreach (var personDonations in donations.GetDonations()) { var donor = personDonations.Key; Osoba osoba = Osoba.GetOrCreateNew(donor.TitleBefore, donor.Name, donor.Surname, donor.TitleAfter, donor.DateOfBirth, Osoba.StatusOsobyEnum.Sponzor, _user); // Výjimka pro Radek Jonke 24.12.1970 if (osoba.Jmeno == "Radek" && osoba.Prijmeni == "Jonke" && osoba.Narozeni != null && osoba.Narozeni.Value.Year == 1970 && osoba.Narozeni.Value.Month == 12 && osoba.Narozeni.Value.Day == 24) { continue; } foreach (var donation in personDonations.Value) { // add event var sponzoring = new Sponzoring() { DarovanoDne = donation.Date, Hodnota = donation.Amount, IcoPrijemce = donation.ICO, Zdroj = _zdroj, Popis = donation.Description, Typ = (int)donation.GiftType }; osoba.AddOrUpdateSponsoring(sponzoring, _user); } } }