private async void ExecuteDoWork(object sender, DoWorkEventArgs e) { try { using (me = new MedicalModel(ConfigurationManager.Connect())) { await me.Database.Connection.OpenAsync(); if (ServicesM.Erased.Count != 0) { foreach (int service in ServicesM.Erased) { try { me.servicesdata.Where(a => a.IdTD == service).Single().DeletedTD = DateTime.Now; await me.SaveChangesAsync(); } catch { } } } for (int i = 0; i < ServicesM.Services.Count; i++) { try { servicesdata tr = new servicesdata(); pricesforeachservice pfs = new pricesforeachservice(); if (ServicesM.Services[i].New) { tr.NameTD = ServicesM.Services[i].Name; pfs.VatPFS = (int)ServicesM.Services[i].Vat; pfs.PricePFS = (int)ServicesM.Services[i].Price; tr.DetailsTD = ServicesM.Services[i].Details; me.servicesdata.Add(tr); await me.SaveChangesAsync(); pfs.ServiceDataIdPFS = tr.IdTD; pfs.WhenChangedPFS = DateTime.Now; me.pricesforeachservice.Add(pfs); await me.SaveChangesAsync(); ServicesM.Services[i].ID = tr.IdTD; ServicesM.Services[i].New = false; } else { int temp = ServicesM.Services[i].ID; tr = me.servicesdata.Where(a => a.IdTD == temp).Single(); pfs = me.pricesforeachservice.Where(pf => pf.ServiceDataIdPFS == temp).OrderByDescending(pf => pf.IdPFS).FirstOrDefault(); if (!ServicesM.Services[i].Name.Equals(tr.NameTD)) { tr.NameTD = ServicesM.Services[i].Name; } if (string.IsNullOrEmpty(ServicesM.Services[i].Details) || (ServicesM.Services[i].Details != tr.DetailsTD)) { tr.DetailsTD = ServicesM.Services[i].Details; } if (!ServicesM.Services[i].Vat.Equals(pfs.VatPFS) || !ServicesM.Services[i].Price.Equals(pfs.PricePFS)) { pfs = new pricesforeachservice() { PricePFS = (int)ServicesM.Services[i].Price, VatPFS = (int)ServicesM.Services[i].Vat, ServiceDataIdPFS = temp, WhenChangedPFS = DateTime.Now }; me.pricesforeachservice.Add(pfs); } await me.SaveChangesAsync(); } } catch { } } await me.SaveChangesAsync(); ServicesM.Erased.Clear(); } workingConn = true; } catch (Exception ex) { Log.WriteException(ex); workingConn = false; } }
protected internal async Task <ObservableCollection <ChartM.Record> > ServicesPrices() { return(await Task.Run(async() => { try { List <ChartM.Record> collection = new List <ChartM.Record>(); using (me = new MedicalModel(ConfigurationManager.Connect())) { await me.Database.Connection.OpenAsync(); List <ServicesForPrice> services = Members.Count == 0 ? me.servicesdata.Select(s => new ServicesForPrice { Id = s.IdTD, Name = s.NameTD, Deleted = s.DeletedTD }).ToList() : me.servicesdata.Where(s => Members.Any(m => m == s.IdTD)).Select(s => new ServicesForPrice { Id = s.IdTD, Name = s.NameTD, Deleted = s.DeletedTD }).ToList(); DateTime LocalStart = StartTime == null ? Correction(true, me.pricesforeachservice.OrderBy(s => s.WhenChangedPFS).FirstOrDefault().WhenChangedPFS).Date : Correction(true, (DateTime)StartTime).Date; DateTime LocalFinish = FinishTime == null ? (StartTime != null ? Correction(false, (DateTime)StartTime).Date : Correction(false, DateTime.Now).Date) : Correction(false, (DateTime)FinishTime).Date; FinishTime = LocalFinish; while (LocalStart.Date < ((DateTime)FinishTime).Date) { LocalFinish = NextStep(LocalStart); foreach (ServicesForPrice item in services) { pricesforeachservice temp = me.pricesforeachservice.Where(pr => pr.ServiceDataIdPFS == item.Id && pr.WhenChangedPFS < LocalFinish). OrderByDescending(pr => pr.WhenChangedPFS).FirstOrDefault(); if (temp != null && (item.Deleted != null ? ((DateTime)item.Deleted).Date > LocalStart : true)) { collection.Add(new ChartM.Record() { Id = services.IndexOf(item), Name = item.Name, Date = LocalStart, Value1 = temp.PricePFS, Value2 = temp.PricePFS *temp.VatPFS / 100 }); } } LocalStart = NextStep(LocalStart); } } workingConn = true; return new ObservableCollection <ChartM.Record>(collection); } catch (Exception ex) { Log.WriteException(ex); workingConn = false; return null; } }, CancellationToken.None).ContinueWith(task => { if (!workingConn) { Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => ConnectionMessage())); } return task.Result; })); }