public static int DeleteLiability(int id)
        {
            Entities           entities    = new Entities();
            List <Liabilities> liabilities = entities.Liabilities.Where(x => (x.Id == id || x.ParentLiabilityId == id) && !x.DisabledDate.HasValue).ToList();
            var parentLiability            = liabilities.Where(x => x.Id == id).FirstOrDefault();

            foreach (var liability in liabilities)
            {
                liability.DisabledDate = DateTime.Now;
                liability.DisabledBy   = Constants.Constants.USER;
                entities.Liabilities.Attach(liability);
                var entry = entities.Entry(liability);
                entry.Property(x => x.DisabledDate).IsModified = true;
                entry.Property(x => x.DisabledBy).IsModified   = true;
            }

            Log log = LogQueries.CreateLog((int)Constants.Constants.LOG_TYPE.DELETE, "khoản nợ \"" + parentLiability.Name + "\"", parentLiability.Username, parentLiability.Value, DateTime.Now);

            entities.Log.Add(log);

            int result = entities.SaveChanges();

            return(result);
        }
        public static int UpdateLiability(Liabilities data)
        {
            Entities    entities        = new Entities();
            DateTime    current         = DateTime.Now;
            int         result          = 0;
            Liabilities parentLiability = entities.Liabilities.Where(x => x.Id == data.Id).FirstOrDefault();

            if (data.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.REAL_ESTATE ||
                data.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.BUSINESS || data.LiabilityType == (int)Constants.Constants.LIABILITY_TYPE.STOCK)
            {
                List <Liabilities> liabilities = entities.Liabilities.Where(x => x.ParentLiabilityId == data.Id && !x.DisabledDate.HasValue).OrderByDescending(x => x.StartDate).ToList();

                if (data.InterestRate > 0)
                {
                    Liabilities start = liabilities.Where(x => x.StartDate < data.StartDate).OrderByDescending(x => x.StartDate).FirstOrDefault();
                    if (start != null)
                    {
                        start.EndDate = data.StartDate.Value.AddMonths(-1);
                        entities.Liabilities.Attach(start);
                        var entry = entities.Entry(start);
                        entry.Property(x => x.EndDate).IsModified = true;
                    }

                    List <Liabilities> middle = liabilities.Where(x => x.StartDate >= data.StartDate && x.EndDate <= data.EndDate).ToList();
                    if (middle.Any())
                    {
                        foreach (var item in middle)
                        {
                            item.DisabledDate = current;
                            item.DisabledBy   = Constants.Constants.USER;
                        }
                    }

                    Liabilities updated_liability = new Liabilities();
                    updated_liability.Name              = parentLiability.Name;
                    updated_liability.Value             = parentLiability.Value;
                    updated_liability.InterestType      = parentLiability.InterestType;
                    updated_liability.InterestRate      = data.InterestRate;
                    updated_liability.CreatedDate       = current;
                    updated_liability.CreatedBy         = Constants.Constants.USER;
                    updated_liability.StartDate         = data.StartDate;
                    updated_liability.EndDate           = data.EndDate;
                    updated_liability.AssetId           = parentLiability.AssetId;
                    updated_liability.ParentLiabilityId = parentLiability.Id;
                    updated_liability.LiabilityType     = parentLiability.LiabilityType;
                    entities.Liabilities.Add(updated_liability);

                    Liabilities end = liabilities.Where(x => x.EndDate > data.EndDate).OrderBy(x => x.EndDate).FirstOrDefault();
                    if (end != null)
                    {
                        end.StartDate = data.EndDate.Value.AddMonths(1);
                        entities.Liabilities.Attach(end);
                        var entry = entities.Entry(end);
                        entry.Property(x => x.StartDate).IsModified    = true;
                        entry.Property(x => x.InterestRate).IsModified = true;
                    }
                    else if (data.EndDate != parentLiability.EndDate)
                    {
                        end                   = new Liabilities();
                        end.Name              = parentLiability.Name;
                        end.Value             = parentLiability.Value;
                        end.InterestType      = parentLiability.InterestType;
                        end.InterestRate      = parentLiability.InterestRate;
                        end.CreatedDate       = current;
                        end.CreatedBy         = Constants.Constants.USER;
                        end.StartDate         = data.EndDate.Value.AddMonths(1);
                        end.EndDate           = parentLiability.EndDate;
                        end.AssetId           = parentLiability.AssetId;
                        end.ParentLiabilityId = parentLiability.Id;
                        end.LiabilityType     = parentLiability.LiabilityType;
                        entities.Liabilities.Add(end);
                    }
                }
                else
                {
                    parentLiability.Name         = data.Name;
                    parentLiability.Value        = data.Value;
                    parentLiability.InterestType = data.InterestType;
                    parentLiability.StartDate    = data.StartDate;
                    parentLiability.EndDate      = data.EndDate;

                    entities.Liabilities.Attach(parentLiability);
                    var entry = entities.Entry(parentLiability);
                    entry.Property(x => x.Name).IsModified         = true;
                    entry.Property(x => x.Value).IsModified        = true;
                    entry.Property(x => x.InterestType).IsModified = true;
                    entry.Property(x => x.StartDate).IsModified    = true;
                    entry.Property(x => x.EndDate).IsModified      = true;

                    foreach (var liability in liabilities)
                    {
                        liability.DisabledDate = current;
                        liability.DisabledBy   = Constants.Constants.USER;
                        entities.Liabilities.Attach(liability);
                        entry = entities.Entry(liability);
                        entry.Property(x => x.DisabledDate).IsModified = true;
                        entry.Property(x => x.DisabledBy).IsModified   = true;
                    }

                    Liabilities childLiability = new Liabilities();
                    childLiability.Name              = data.Name;
                    childLiability.Value             = data.Value;
                    childLiability.InterestType      = data.InterestType;
                    childLiability.InterestRate      = parentLiability.InterestRate;
                    childLiability.StartDate         = data.StartDate;
                    childLiability.EndDate           = data.EndDate;
                    childLiability.CreatedDate       = current;
                    childLiability.CreatedBy         = Constants.Constants.USER;
                    childLiability.AssetId           = parentLiability.AssetId;
                    childLiability.ParentLiabilityId = data.Id;
                    childLiability.LiabilityType     = parentLiability.LiabilityType;

                    entities.Liabilities.Add(childLiability);
                }
            }
            else
            {
                parentLiability.DisabledDate = DateTime.Now;
                parentLiability.DisabledBy   = Constants.Constants.USER;

                entities.Liabilities.Attach(parentLiability);
                var entry = entities.Entry(parentLiability);
                entry.Property(x => x.DisabledBy).IsModified   = true;
                entry.Property(x => x.DisabledDate).IsModified = true;

                Liabilities updated_liability = new Liabilities();
                updated_liability.Name          = data.Name;
                updated_liability.Purpose       = data.Purpose;
                updated_liability.OriginalValue = data.OriginalValue;
                updated_liability.Value         = data.Value;
                updated_liability.InterestType  = data.InterestType;
                updated_liability.InterestRate  = parentLiability.InterestRate;
                updated_liability.StartDate     = data.StartDate;
                updated_liability.EndDate       = data.EndDate;
                updated_liability.CreatedDate   = current;
                updated_liability.CreatedBy     = Constants.Constants.USER;
                updated_liability.Username      = parentLiability.Username;
                updated_liability.LiabilityType = parentLiability.LiabilityType;
                updated_liability.TransactionId = parentLiability.TransactionId;
                updated_liability.Note          = data.Note;

                entities.Liabilities.Add(updated_liability);
            }

            Log log = LogQueries.CreateLog((int)Constants.Constants.LOG_TYPE.UPDATE, "khoản nợ \"" + parentLiability.Name + "\"", parentLiability.Username, parentLiability.Value, DateTime.Now);

            entities.Log.Add(log);
            result = entities.SaveChanges();
            return(result);
        }