Exemplo n.º 1
0
    public Expense AddExpense(ExpenseModel expenseModel)
    {
        this.context.Expenses.Add(expenseModel.Expense);
        context.SaveChanges();

        if (expenseModel.Expense
            .GroupId != null)
        {
            GroupExpense groupExpense = new GroupExpense()
            {
                ExpenseId = expenseModel.Expense.ExpenseId,
                GroupId   = expenseModel.Expense.GroupId,
            };
            context.GroupsofExpenses.Add(groupExpense);
            context.SaveChanges();
        }


        foreach (var item in expenseModel.Shares)
        {
            item.ExpenseId = expenseModel.Expense.ExpenseId;
            context.Shareinfo.Add(item);
            context.SaveChanges();
        }
        foreach (var item in expenseModel.Payers)
        {
            var share = GetShare(item.PayerId, expenseModel.Expense.ExpenseId);

            Expenseinfo expenseinfo = new Expenseinfo();
            expenseinfo.ExpenseId   = expenseModel.Expense.ExpenseId;
            expenseinfo.PaidAmouunt = item.Amount;
            expenseinfo.UserId      = item.PayerId;
            expenseinfo.ShareId     = share.ShareId;
            expenseinfo.shareamount = share.ShareAmount;
            if ((share.ShareAmount - item.Amount) >= 0)
            {
                expenseinfo.BorrowedAmout = share.ShareAmount - item.Amount;
                expenseinfo.LentedAmout   = 0;
            }
            else
            {
                expenseinfo.LentedAmout = item.Amount - share.ShareAmount;
            }

            context.Expensesinfo.Add(expenseinfo);
            context.SaveChanges();
        }

        var expenseinfolist = context.Expensesinfo.ToList().Where(x => x.ExpenseId == expenseModel.Expense.ExpenseId);

        foreach (var item in expenseinfolist)
        {
            var LentedAmout = item.LentedAmout;
            if (LentedAmout > 0)
            {
                foreach (var subitem in expenseinfolist)
                {
                    var BorrowedAmout = subitem.BorrowedAmout;

                    if (LentedAmout < 0)
                    {
                        break;
                    }
                    if (item.UserId == subitem.UserId)
                    {
                        continue;
                    }

                    if (BorrowedAmout > 0)
                    {
                        Settelement settelement = new Settelement();
                        settelement.ExpenseId  = expenseModel.Expense.ExpenseId;
                        settelement.BorrowerId = subitem.UserId;
                        settelement.LenterId   = item.UserId;
                        settelement.GroupId    = expenseModel.Expense.GroupId;
                        if (LentedAmout >= BorrowedAmout)
                        {
                            settelement.SettelementAmount = BorrowedAmout;
                            LentedAmout   = LentedAmout - BorrowedAmout;
                            BorrowedAmout = 0;
                        }
                        if (LentedAmout < BorrowedAmout)
                        {
                            settelement.SettelementAmount = LentedAmout;
                            LentedAmout   = 0;
                            BorrowedAmout = BorrowedAmout - LentedAmout;
                        }

                        context.Settelements.Add(settelement);
                        context.SaveChanges();
                    }
                }
            }
        }

        var activity = new Activity()
        {
            Activitydata = "You" + expenseModel.Expense.Title + "Added",
            Date         = DateTime.Now.ToString(),
            UserId       = expenseModel.Expense.UserId,
        };

        context.Activities.Add(activity);
        context.SaveChanges();



        return(expenseModel.Expense);
    }
Exemplo n.º 2
0
    public ExpenseModel EditExpense(int id, ExpenseModel expenseModel)
    {
        var oldexpense = context.Expenses.FirstOrDefault(x => x.ExpenseId == id);

        oldexpense.SplitType = expenseModel.Expense.SplitType;
        oldexpense.Amount    = expenseModel.Expense.Amount;
        oldexpense.Date      = expenseModel.Expense.Date;
        oldexpense.GroupId   = expenseModel.Expense.GroupId;
        oldexpense.Title     = expenseModel.Expense.Title;
        oldexpense.UserId    = expenseModel.Expense.UserId;

        var expense = context.Expenses.Attach(oldexpense);

        expense.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
        context.SaveChanges();



        var expensesinfo = context.Expensesinfo.Where(x => x.ExpenseId == id);

        context.Expensesinfo.RemoveRange(expensesinfo);
        var Settelements = context.Settelements.Where(x => x.ExpenseId == id);

        context.Settelements.RemoveRange(Settelements);
        var shareinfo = context.Shareinfo.Where(x => x.ExpenseId == id);

        context.Shareinfo.RemoveRange(shareinfo);

        foreach (var item in expenseModel.Shares)
        {
            item.ExpenseId = oldexpense.ExpenseId;
            context.Shareinfo.Add(item);
            context.SaveChanges();
        }
        foreach (var item in expenseModel.Payers)
        {
            var share = GetShare(item.Payer.Id, oldexpense.ExpenseId);

            Expenseinfo expenseinfo = new Expenseinfo();
            expenseinfo.ExpenseId   = oldexpense.ExpenseId;
            expenseinfo.PaidAmouunt = item.Amount;
            expenseinfo.UserId      = item.Payer.Id;
            expenseinfo.ShareId     = share.ShareId;
            expenseinfo.shareamount = share.ShareAmount;
            if ((share.ShareAmount - item.Amount) >= 0)
            {
                expenseinfo.BorrowedAmout = share.ShareAmount - item.Amount;
                expenseinfo.LentedAmout   = 0;
            }
            else
            {
                expenseinfo.LentedAmout = item.Amount - share.ShareAmount;
            }

            context.Expensesinfo.Add(expenseinfo);
            context.SaveChanges();
        }

        var expenseinfolist = context.Expensesinfo.ToList().Where(x => x.ExpenseId == oldexpense.ExpenseId);

        foreach (var item in expenseinfolist)
        {
            if (item.LentedAmout > 0)
            {
                foreach (var subitem in expenseinfolist)
                {
                    if (item.LentedAmout < 0)
                    {
                        break;
                    }
                    if (item.UserId == subitem.UserId)
                    {
                        continue;
                    }

                    if (subitem.BorrowedAmout > 0)
                    {
                        Settelement settelement = new Settelement();
                        settelement.ExpenseId  = oldexpense.ExpenseId;
                        settelement.BorrowerId = subitem.UserId;
                        settelement.LenterId   = item.UserId;
                        settelement.GroupId    = expenseModel.Expense.GroupId;
                        if (item.LentedAmout >= subitem.BorrowedAmout)
                        {
                            settelement.SettelementAmount = subitem.BorrowedAmout;
                            item.LentedAmout      = item.LentedAmout - subitem.BorrowedAmout;
                            subitem.BorrowedAmout = 0;
                        }
                        if (item.LentedAmout < subitem.BorrowedAmout)
                        {
                            settelement.SettelementAmount = item.LentedAmout;
                            item.LentedAmout      = 0;
                            subitem.BorrowedAmout = subitem.BorrowedAmout - item.LentedAmout;
                        }

                        context.Settelements.Add(settelement);
                        context.SaveChanges();
                    }
                }
            }
        }

        var activity = new Activity()
        {
            Activitydata = "You" + expenseModel.Expense.Title + "Edited",
            Date         = DateTime.Now.ToString(),
            UserId       = expenseModel.Expense.UserId,
        };

        context.Activities.Add(activity);
        context.SaveChanges();


        return(expenseModel);
    }