Example #1
0
        private List <int> AddGiveToMoney(MaaserGiven maaserGiven)
        {
            List <int> moneyIds = new List <int>();

            using (var ctx = new MaaserContext(_connString))
            {
                IEnumerable <MoneyEarned> money = ctx.MoneyEarned.Where(z => z.UserId == _user.Id);
                MoneyEarned m      = money.FirstOrDefault(mo => mo.PaidUp == false);
                decimal     amount = maaserGiven.Amount * 10;
                if (m == null)
                {
                    return(null);
                }

                while (m.PaidUp == false && amount != 0 && m.AmountLeft != 0)
                {
                    m.AmountLeft = m.AmountLeft - amount;
                    moneyIds.Add(m.Id);
                    if (m.AmountLeft < 0)
                    {
                        amount       = 0 - m.AmountLeft;
                        m.AmountLeft = 0;
                        m.PaidUp     = true;
                        ctx.MoneyEarned.Attach(m);
                        ctx.Entry(m).State = EntityState.Modified;
                        m = money.FirstOrDefault(mo => mo.PaidUp == false);
                        if (m == null)
                        {
                            break;
                        }
                    }
                    else if (m.AmountLeft == 0)
                    {
                        m.PaidUp = true;
                        ctx.MoneyEarned.Attach(m);
                        ctx.Entry(m).State = EntityState.Modified;
                    }
                    else
                    {
                        ctx.SaveChanges();
                        break;
                    }
                    ctx.SaveChanges();
                }

                return(moneyIds);
            }
        }
Example #2
0
        public void AddMaaserGiven(MaaserGiven maaserGiven)
        {
            using (var ctx = new MaaserContext(_connString))
            {
                maaserGiven.UserId = _user.Id;
                ctx.MaaserGiven.Add(maaserGiven);
                ctx.SaveChanges();
                var money = AddGiveToMoney(maaserGiven);
                if (money.Count > 1)
                {
                    maaserGiven.GiveToMoney = money.Select(i => new GiveToMoney
                    {
                        MaaserGivenId = maaserGiven.Id, UserId = _user.Id, MoneyId = i
                    }).ToList();
                    ctx.MaaserGiven.Attach(maaserGiven);
                    ctx.Entry(maaserGiven).State = EntityState.Modified;
                }



                ctx.SaveChanges();
            }
        }