public static void UpdateData()
        {
            var newData = GetDataFromGFR();

            if (newData != null)
            {
                using (Lib.Data.DbEntities db = new DbEntities())
                {
                    foreach (var key in newData.Keys)
                    {
                        var exist = db.NespolehlivyPlatceDPH.Where(i => i.Ico == key).FirstOrDefault();
                        if (exist != null)
                        {
                            if (exist.FromDate.HasValue && newData[key].FromDate.HasValue && exist.FromDate != newData[key].FromDate)
                            {
                                exist.FromDate = newData[key].FromDate;
                            }

                            if (exist.ToDate.HasValue) //is back on the list, remove end
                            {
                                exist.ToDate = null;
                            }
                        }
                        else
                        {
                            var newItem = new NespolehlivyPlatceDPH()
                            {
                                Ico      = newData[key].Ico,
                                FromDate = newData[key].FromDate
                            };
                            db.NespolehlivyPlatceDPH.Add(newItem);
                        }
                    }
                    db.SaveChanges();
                    //check ico removed from newData
                    var inDb             = db.NespolehlivyPlatceDPH.Where(m => m.ToDate == null).Select(m => m.Ico).ToArray();
                    var missingInNewData = inDb.Except(newData.Keys);
                    foreach (var ico in missingInNewData)
                    {
                        var exist = db.NespolehlivyPlatceDPH.Where(i => i.Ico == ico).FirstOrDefault();
                        if (exist != null)
                        {
                            if (exist.ToDate == null)
                            {
                                exist.ToDate = DateTime.Now.Date;
                            }
                        }
                    }
                    db.SaveChanges();
                }
            }
        }
Esempio n. 2
0
        public static HlidacStatu.Lib.OCR.Api.Result AddNewTask(ItemToOcrType itemType, string itemId, string itemSubType = null, int priority = 5)
        {
            using (DbEntities db = new DbEntities())
            {
                IQueryable <ItemToOcrQueue> sql = CreateQuery(db, itemType, itemSubType);
                sql = sql.Where(m => m.itemId == itemId);
                if (sql.Any()) //already in the queue
                {
                    return new OCR.Api.Result()
                           {
                               IsValid = OCR.Api.Result.ResultStatus.InQueueWithCallback,
                               Id      = "uknown"
                           }
                }
                ;

                ItemToOcrQueue i = new ItemToOcrQueue();
                i.created     = DateTime.Now;
                i.itemId      = itemId;
                i.itemType    = itemType.ToString();
                i.itemSubType = itemSubType;
                i.priority    = priority;
                db.ItemToOcrQueue.Add(i);
                db.SaveChanges();
                return(new OCR.Api.Result()
                {
                    IsValid = OCR.Api.Result.ResultStatus.InQueueWithCallback,
                    Id = "uknown"
                });
            }
        }
Esempio n. 3
0
        public static IEnumerable <ItemToOcrQueue> TakeFromQueue(ItemToOcrType?itemType = null, string itemSubType = null, int maxItems = 30)
        {
            using (DbEntities db = new DbEntities())
            {
                lock (lockTakeFromQueue)
                {
                    using (var dbTran = db.Database.BeginTransaction())
                    {
                        try
                        {
                            IQueryable <ItemToOcrQueue> sql = CreateQuery(db, itemType, itemSubType);

                            sql = sql
                                  .OrderByDescending(m => m.priority)
                                  .ThenBy(m => m.created)
                                  .Take(maxItems);
                            var res = sql.ToArray();
                            foreach (var i in res)
                            {
                                i.started = DateTime.Now;
                            }
                            db.SaveChanges();
                            dbTran.Commit();
                            return(res);
                        }
                        catch (Exception e)
                        {
                            dbTran.Rollback();
                            throw e;
                        }
                    }
                }
            }
        }
        private static Sponzoring UpdateSponzoring(Sponzoring sponzoringToUpdate, Sponzoring sponzoring, string user, DbEntities db)
        {
            var sponzoringOriginal = sponzoringToUpdate.ShallowCopy();

            if (!string.IsNullOrWhiteSpace(sponzoring.IcoDarce))
            {
                sponzoringToUpdate.IcoDarce = sponzoring.IcoDarce;
            }
            if (sponzoring.OsobaIdDarce > 0)
            {
                sponzoringToUpdate.OsobaIdDarce = sponzoring.OsobaIdDarce;
            }

            sponzoringToUpdate.Edited    = DateTime.Now;
            sponzoringToUpdate.UpdatedBy = user;

            sponzoringToUpdate.DarovanoDne     = sponzoring.DarovanoDne;
            sponzoringToUpdate.Hodnota         = sponzoring.Hodnota;
            sponzoringToUpdate.IcoPrijemce     = sponzoring.IcoPrijemce;
            sponzoringToUpdate.OsobaIdPrijemce = sponzoring.OsobaIdPrijemce;
            sponzoringToUpdate.Popis           = sponzoring.Popis;
            sponzoringToUpdate.Typ             = sponzoring.Typ;
            sponzoringToUpdate.Zdroj           = sponzoring.Zdroj;

            db.SaveChanges();

            Audit.Add <Sponzoring>(Audit.Operations.Update, user, sponzoringToUpdate, sponzoringOriginal);
            return(sponzoringToUpdate);
        }
 public static AspNetUserToken CreateNew(string userId)
 {
     using (Lib.Data.DbEntities db = new DbEntities())
     {
         var t = new AspNetUserToken()
         {
             Id = userId, Count = 0, Created = DateTime.Now, LastAccess = null, Token = Guid.NewGuid()
         };
         db.AspNetUserTokens.Add(t);
         db.SaveChanges();
         return(t);
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Sets new classification on smlouva. And saves data also to audit table [].
        /// Originally created for manual override of classification.
        /// </summary>
        /// <param name="typeValues">new classification</param>
        /// <param name="username">author</param>
        public void OverrideClassification(int[] typeValues, string username)
        {
            if (typeValues.Length == 0)
            {
                throw new ArgumentException($"typeValues is empty");
            }
            if (string.IsNullOrWhiteSpace(username))
            {
                throw new ArgumentException($"username is empty");
            }

            List <SClassification.Classification> newClassification = new List <SClassification.Classification>();

            for (int i = 0; i < typeValues.Length; i++)
            {
                if (!Enum.IsDefined(typeof(SClassification.ClassificationsTypes), typeValues[i]))
                {
                    throw new ArgumentException($"TypeValue [{typeValues[i]}] is not defined in {nameof(SClassification.ClassificationsTypes)}.");
                }

                var classItem = new SClassification.Classification
                {
                    TypeValue          = typeValues[i],
                    ClassifProbability = (i == 0) ? 0.8m : 0.7m
                };
                newClassification.Add(classItem);
            }

            // save to db
            using (Lib.Data.DbEntities db = new DbEntities())
            {
                db.ClassificationOverride.Add(
                    new ClassificationOverride()
                {
                    IdSmlouvy    = this.Id,
                    Created      = DateTime.Now,
                    CreatedBy    = username,
                    OriginalCat1 = this.Classification.Types.Length > 0 ? (int?)this.Classification.Types[0].TypeValue : null,
                    OriginalCat2 = this.Classification.Types.Length > 1 ? (int?)this.Classification.Types[1].TypeValue : null,
                    CorrectCat1  = newClassification.Count > 0 ? (int?)newClassification[0].TypeValue : null,
                    CorrectCat2  = newClassification.Count > 1 ? (int?)newClassification[1].TypeValue : null
                });
                db.SaveChanges();
            }

            var idsmlouvy = this.Id;

            this.Classification.Types      = newClassification.ToArray();
            this.Classification.LastUpdate = DateTime.Now;
            this.Save();
        }
 public static void SetDone(int taskItemId, bool success, string result = null)
 {
     using (DbEntities db = new DbEntities())
     {
         ItemToOcrQueue i = db.ItemToOcrQueue.Where(m => m.pk == taskItemId).FirstOrDefault();
         if (i != null)
         {
             i.done    = DateTime.Now;
             i.success = success ? 1 : 0;
             i.result  = result;
             db.SaveChanges();
         }
     }
 }
 public static void ResetTask(int taskItemId)
 {
     using (DbEntities db = new DbEntities())
     {
         ItemToOcrQueue i = db.ItemToOcrQueue.Where(m => m.pk == taskItemId).FirstOrDefault();
         if (i != null)
         {
             i.done    = null;
             i.started = null;
             i.success = null;
             i.result  = null;
             db.SaveChanges();
         }
     }
 }
Esempio n. 9
0
        private static OsobaEvent UpdateEvent(OsobaEvent eventToUpdate, OsobaEvent osobaEvent, string user, DbEntities db)
        {
            if (eventToUpdate is null)
            {
                throw new ArgumentNullException(nameof(eventToUpdate), "Argument can't be null");
            }
            if (osobaEvent is null)
            {
                throw new ArgumentNullException(nameof(osobaEvent), "Argument can't be null");
            }
            if (db is null)
            {
                throw new ArgumentNullException(nameof(db), "Argument can't be null");
            }

            var eventOriginal = eventToUpdate.ShallowCopy();

            NormalizeOsobaEvent(osobaEvent);

            if (!string.IsNullOrWhiteSpace(osobaEvent.Ico))
            {
                eventToUpdate.Ico = osobaEvent.Ico;
            }
            if (osobaEvent.OsobaId > 0)
            {
                eventToUpdate.OsobaId = osobaEvent.OsobaId;
            }

            eventToUpdate.DatumOd    = osobaEvent.DatumOd;
            eventToUpdate.DatumDo    = osobaEvent.DatumDo;
            eventToUpdate.Organizace = ParseTools.NormalizaceStranaShortName(osobaEvent.Organizace);
            eventToUpdate.AddInfoNum = osobaEvent.AddInfoNum;
            eventToUpdate.AddInfo    = osobaEvent.AddInfo;
            eventToUpdate.Title      = osobaEvent.Title;
            eventToUpdate.Type       = osobaEvent.Type;
            eventToUpdate.Zdroj      = osobaEvent.Zdroj;
            eventToUpdate.Status     = osobaEvent.Status;
            eventToUpdate.CEO        = osobaEvent.CEO;
            eventToUpdate.Created    = DateTime.Now;

            db.SaveChanges();
            if (osobaEvent.OsobaId > 0)
            {
                Osoby.GetById.Get(osobaEvent.OsobaId).FlushCache();
            }
            Audit.Add(Audit.Operations.Update, user, eventToUpdate, eventOriginal);
            return(eventToUpdate);
        }
Esempio n. 10
0
        public void Save()
        {
            using (DbEntities db = new DbEntities())
            {
                db.FirmaHint.Attach(this);
                if (db.FirmaHint.Any(m => m.Ico == this.Ico))
                {
                    db.Entry(this).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.Entry(this).State = System.Data.Entity.EntityState.Added;
                }

                db.SaveChanges();
            }
        }
        private static Sponzoring CreateSponzoring(Sponzoring sponzoring, string user, DbEntities db)
        {
            if (sponzoring.OsobaIdDarce == 0 &&
                string.IsNullOrWhiteSpace(sponzoring.IcoDarce))
            {
                throw new Exception("Cant attach sponzoring to a person or to a company since their reference is empty");
            }

            sponzoring.Created   = DateTime.Now;
            sponzoring.Edited    = DateTime.Now;
            sponzoring.UpdatedBy = user;

            db.Sponzoring.Add(sponzoring);
            db.SaveChanges();

            Audit.Add(Audit.Operations.Create, user, sponzoring, null);
            return(sponzoring);
        }
Esempio n. 12
0
        private static OsobaEvent CreateEvent(OsobaEvent osobaEvent, string user, DbEntities db)
        {
            if (osobaEvent.OsobaId == 0 && string.IsNullOrWhiteSpace(osobaEvent.Ico))
            {
                throw new Exception("Cant attach event to a person or to a company since their reference is empty");
            }

            osobaEvent.Organizace = ParseTools.NormalizaceStranaShortName(osobaEvent.Organizace);
            osobaEvent.Created    = DateTime.Now;
            db.OsobaEvent.Add(osobaEvent);
            db.SaveChanges();
            if (osobaEvent.OsobaId > 0)
            {
                Osoby.GetById.Get(osobaEvent.OsobaId).FlushCache();
            }
            Audit.Add(Audit.Operations.Update, user, osobaEvent, null);
            return(osobaEvent);
        }
Esempio n. 13
0
 public static HlidacStatu.Lib.OCR.Api.Result AddNewTask(ItemToOcrType itemType, string itemId, string itemSubType = null, HlidacStatu.Lib.OCR.Api.Client.TaskPriority priority = OCR.Api.Client.TaskPriority.Standard)
 {
     using (DbEntities db = new DbEntities())
     {
         ItemToOcrQueue i = new ItemToOcrQueue();
         i.created     = DateTime.Now;
         i.itemId      = itemId;
         i.itemType    = itemType.ToString();
         i.itemSubType = itemSubType;
         i.priority    = (int)OCR.Api.Client.TaskPriority.Standard;
         db.ItemToOcrQueue.Add(i);
         db.SaveChanges();
         return(new OCR.Api.Result()
         {
             IsValid = OCR.Api.Result.ResultStatus.InQueueWithCallback,
             Id = "uknown"
         });
     }
 }
Esempio n. 14
0
 public static Audit Add <T>(Operations operation, string user, string ipAddress, T newObj, T prevObj)
     where T : IAuditable
 {
     using (DbEntities db = new DbEntities())
     {
         var a = new Audit();
         a.date        = DateTime.Now;
         a.objectId    = newObj?.ToAuditObjectId();
         a.objectType  = newObj?.ToAuditObjectTypeName();
         a.operation   = operation.ToString();
         a.IP          = ipAddress;
         a.userId      = user ?? "";
         a.valueBefore = prevObj?.ToAuditJson() ?? null;
         a.valueAfter  = newObj?.ToAuditJson() ?? null;
         db.Audit.Add(a);
         db.SaveChanges();
         return(a);
     }
 }
Esempio n. 15
0
 public static Audit Add(Operations operation, string user, string ipAddress,
                         string objectId, string objectType,
                         string newObjSer, string prevObjSer)
 {
     using (DbEntities db = new DbEntities())
     {
         var a = new Audit();
         a.date        = DateTime.Now;
         a.objectId    = objectId;
         a.objectType  = objectType;
         a.operation   = operation.ToString();
         a.IP          = ipAddress;
         a.userId      = user ?? "";
         a.valueBefore = prevObjSer;
         a.valueAfter  = newObjSer ?? "";
         db.Audit.Add(a);
         db.SaveChanges();
         return(a);
     }
 }
Esempio n. 16
0
        public static void ResetTask(int taskItemId, bool decreasePriority = true)
        {
            using (DbEntities db = new DbEntities())
            {
                ItemToOcrQueue i = db.ItemToOcrQueue.Where(m => m.pk == taskItemId).FirstOrDefault();
                if (i != null)
                {
                    i.done = null;

                    i.started = null;
                    i.success = null;
                    i.result  = null;
                    if (decreasePriority)
                    {
                        i.priority--;
                        if (i.priority < 1)
                        {
                            i.priority = 1;
                        }
                    }
                    db.SaveChanges();
                }
            }
        }
Esempio n. 17
0
        public static void AddOrUpdate(
            int osobaId, string dcerinkaIco,
            int kod_angm, string funkce, decimal?share, DateTime?fromDate, DateTime?toDate, string zdroj = ""
            )
        {
            using (Lib.Data.DbEntities db = new DbEntities())
            {
                var existing = db.OsobaVazby
                               .Where(m =>
                                      m.OsobaID == osobaId &&
                                      m.VazbakICO == dcerinkaIco &&
                                      m.DatumOd == fromDate &&
                                      m.DatumDo == toDate
                                      )
                               .FirstOrDefault();
                if (existing == null)
                {
                    existing = db.OsobaVazby
                               .Where(m =>
                                      m.OsobaID == osobaId &&
                                      m.VazbakICO == dcerinkaIco &&
                                      m.DatumOd == fromDate
                                      )
                               .FirstOrDefault();
                }

                if (existing != null)
                {
                    //update
                    existing.TypVazby         = kod_angm;
                    existing.PojmenovaniVazby = funkce;
                    if (existing.podil != share)
                    {
                        existing.podil = share;
                    }
                    if (existing.DatumOd != fromDate)
                    {
                        existing.DatumOd = fromDate;
                    }
                    if (existing.DatumDo != toDate)
                    {
                        existing.DatumDo = toDate;
                    }
                    existing.LastUpdate = DateTime.Now;
                }
                else //new
                {
                    OsobaVazby af = new OsobaVazby();
                    af.OsobaID          = osobaId;
                    af.VazbakICO        = dcerinkaIco;
                    af.DatumOd          = fromDate;
                    af.DatumDo          = toDate;
                    af.TypVazby         = kod_angm;
                    af.PojmenovaniVazby = funkce;
                    af.podil            = share;
                    af.LastUpdate       = DateTime.Now;
                    db.OsobaVazby.Add(af);
                }
                db.SaveChanges();
            }
        }
Esempio n. 18
0
        public static void AddOrUpdate(
            int osobaId, int vazbakOsobaId,
            int kod_angm, string funkce, decimal?share, DateTime?fromDate, DateTime?toDate, string zdroj = ""
            )
        {
            using (Lib.Data.DbEntities db = new DbEntities())
            {
                var existing = db.OsobaVazby
                               .Where(m =>
                                      m.OsobaID == osobaId &&
                                      m.VazbakOsobaId == vazbakOsobaId &&
                                      m.DatumOd == fromDate &&
                                      m.DatumDo == toDate
                                      )
                               .FirstOrDefault();
                if (existing == null)
                {
                    existing = db.OsobaVazby
                               .Where(m =>
                                      m.OsobaID == osobaId &&
                                      m.VazbakOsobaId == vazbakOsobaId &&
                                      m.DatumOd == fromDate
                                      )
                               .FirstOrDefault();
                }

                if (existing != null)
                {
                    //update
                    existing.TypVazby         = kod_angm;
                    existing.PojmenovaniVazby = funkce;

                    if (existing.podil != share)
                    {
                        existing.podil = share;
                    }
                    if (existing.DatumOd != fromDate)
                    {
                        existing.DatumOd = fromDate;
                    }
                    if (existing.DatumDo != toDate)
                    {
                        existing.DatumDo = toDate;
                    }
                    existing.LastUpdate = DateTime.Now;
                }
                else //new
                {
                    OsobaVazby af = new OsobaVazby();
                    af.OsobaID          = osobaId;
                    af.VazbakICO        = "";
                    af.VazbakOsobaId    = vazbakOsobaId;
                    af.DatumOd          = fromDate;
                    af.DatumDo          = toDate;
                    af.TypVazby         = kod_angm;
                    af.PojmenovaniVazby = funkce;
                    af.podil            = share;
                    af.LastUpdate       = DateTime.Now;
                    db.OsobaVazby.Add(af);
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    HlidacStatu.Util.Consts.Logger.Error("", e);
                    throw;
                }
            }
        }
Esempio n. 19
0
        public static Invoices CreateNew(
            Lib.Data.InvoiceItems.ShopItem sluzba,
            string adresa,
            string mesto,
            string jmenoFirmy,
            string ICO,
            string jmenoOsoby,
            string DIC,
            string PSC,
            string userId,
            string username,
            bool sendEmail = true
            )
        {
            using (Lib.Data.DbEntities db = new DbEntities())
            {
                var obj = new Lib.Data.Invoices();
                obj.Address       = adresa;
                obj.City          = mesto;
                obj.Company       = jmenoFirmy;
                obj.CompanyID     = ICO;
                obj.Country       = "CZ";
                obj.Created       = DateTime.Now;
                obj.FirstName     = jmenoOsoby;
                obj.InvoiceNumber = (db.Invoices.Count() + 1).ToString();
                obj.Status        = (int)Lib.Data.Invoices.InvoiceStatus.New;
                obj.Text          = "Služby serveru HlidacStatu.cz";
                obj.VatID         = DIC;
                obj.Zip           = PSC;
                obj.ID_Customer   = userId;
                var ii = new InvoiceItems()
                {
                    ID_ShopItem = (int)sluzba,
                    Expires     = DateTime.Now.AddYears(1),
                    Name        = "HlidacStatu.cz - " + sluzba.ToNiceDisplayName(),
                    Invoices    = obj,
                    VAT         = 1.21m,
                    Created     = DateTime.Now,
                };
                switch (sluzba)
                {
                case Lib.Data.InvoiceItems.ShopItem.Zakladni:
                    ii.Price = 14900;
                    break;

                case Lib.Data.InvoiceItems.ShopItem.Kompletni:
                    ii.Price = 29900;
                    break;

                case Lib.Data.InvoiceItems.ShopItem.NGO:
                default:
                    ii.Price = 0;
                    break;
                }

                obj.InvoiceItems = new InvoiceItems[] { ii };

                db.Invoices.Add(obj);
                db.InvoiceItems.Add(ii);

                if (sendEmail)
                {
                    try
                    {
                        using (MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"))
                        {
                            using (SmtpClient smtp = new SmtpClient())
                            {
                                msg.Subject = "Objednavka " + sluzba.ToNiceDisplayName();
                                msg.Body    = @"ico:" + ICO + "\n"
                                              + "firma:" + jmenoFirmy + "\n"
                                              + "jmeno:" + jmenoOsoby + "\n"
                                              + "email:" + username + "\n"
                                              + "userid:" + userId + "\n"
                                ;
                                msg.BodyEncoding    = System.Text.Encoding.UTF8;
                                msg.SubjectEncoding = System.Text.Encoding.UTF8;

                                HlidacStatu.Util.Consts.Logger.Info("Sending email to " + msg.To);
                                //msg.Bcc.Add("*****@*****.**");
                                smtp.Send(msg);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        HlidacStatu.Util.Consts.Logger.Error("Send email", e);
                    }
                }
                db.SaveChanges();
                return(obj);
            }
        }