示例#1
0
        public int AddBillMember(BillMember bm)
        {
            context.BillMembers.Add(bm);
            var res = context.SaveChanges();

            return(res);
        }
示例#2
0
        public int AddIndividualBill(IndividualBillDataModel model)
        {
            var ib = new IndividualBills();
            List <Transactions> trans = new List <Transactions>();
            var bm = new List <int>();
            var dt = DateTime.Now;

            ib.Ib_DateTime = dt;
            ib.Ib_Name     = model.Ib_Name;
            ib.Ib_PaidBy   = model.Ib_PaidBy;
            ib.Ib_Amount   = model.Ib_Amount;

            bm = model.billMembers;

            context.IndividualBills.Add(ib);
            context.SaveChanges();

            ib = context.IndividualBills.OrderByDescending(d => d.Ib_Id).FirstOrDefault();
            for (int i = 0; i < bm.Count; i++)
            {
                var bmem = new BillMember();
                var temp = new Transactions();
                temp.T_PaidBy           = model.Ib_PaidBy;
                temp.T_Amount           = model.Ib_Amount / (model.billMembers.Count + 1);
                temp.T_ReceivedByFriend = bm[i];
                temp.T_DateTime         = dt;
                bmem.Bm_BillId          = ib.Ib_Id;
                bmem.Bm_Paidfor         = bm[i];
                context.BillMembers.Add(bmem);
                context.Transactions.Add(temp);
            }

            //add activity
            var act = new Activity();

            act.A_DoneBy      = model.Ib_PaidBy;
            act.A_Description = "Added Bill " + model.Ib_Name;
            act.A_Date        = dt;
            context.Activities.Add(act);
            var res = context.SaveChanges();

            return(res);
        }
示例#3
0
        public async Task <Bill> InsertBillAsync(BillModel bill)
        {
            Bill newBill = new Bill();

            newBill.BillName    = bill.BillName;
            newBill.CreatorId   = bill.CreatorId;
            newBill.Amount      = bill.Amount;
            newBill.CreatedDate = bill.CreatedDate;
            newBill.GroupId     = bill.GroupId;
            _Context.Bill.Add(newBill);


            foreach (var person in bill.Payer)
            {
                Payer payer = new Payer();
                payer.BillId     = newBill.BillId;
                payer.PayerId    = person.Id;
                payer.PaidAmount = person.Amount;
                _Context.Payer.Add(payer);
            }


            foreach (var person in bill.SharedMember)
            {
                BillMember member = new BillMember();
                member.Billid         = newBill.BillId;
                member.SharedMemberId = person.Id;
                member.AmountToPay    = person.Amount;
                _Context.BillMember.Add(member);
            }


            for (var i = 0; i < bill.SharedMember.Count - 1; i++)
            {
                for (var j = i + 1; j < bill.SharedMember.Count; j++)
                {
                    var fExist = _Context.FriendList.SingleOrDefault(c => c.UserId == bill.SharedMember[i].Id && c.FriendId == bill.SharedMember[j].Id);
                    if (fExist == null)
                    {
                        var Exist = _Context.FriendList.SingleOrDefault(c => c.UserId == bill.SharedMember[j].Id && c.FriendId == bill.SharedMember[i].Id);
                        if (Exist == null)
                        {
                            FriendList newFriend = new FriendList
                            {
                                UserId   = bill.SharedMember[i].Id,
                                FriendId = bill.SharedMember[j].Id
                            };
                            _Context.FriendList.Add(newFriend);
                            await _Context.SaveChangesAsync();
                        }
                    }
                }
            }


            foreach (var data in bill.SettleModels)
            {
                if (data.GroupId == null)
                {
                    var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.PayerId && c.SharedMemberId == data.SharedMemberId && c.GroupId == null);

                    if (settle != null)
                    {
                        settle.TotalAmount = settle.TotalAmount + data.TotalAmount;
                        _Context.Settlement.Attach(settle);
                    }
                    else
                    {
                        var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.SharedMemberId && c.SharedMemberId == data.PayerId && c.GroupId == null);

                        if (setle != null)
                        {
                            setle.TotalAmount = setle.TotalAmount - data.TotalAmount;
                            _Context.Settlement.Attach(setle);
                        }
                        else
                        {
                            var newSettle = new Settlement();
                            newSettle.PayerId        = data.PayerId;
                            newSettle.SharedMemberId = data.SharedMemberId;
                            newSettle.TotalAmount    = data.TotalAmount;
                            _Context.Settlement.Add(newSettle);
                        }
                    }
                }
                else
                {
                    var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.PayerId && c.SharedMemberId == data.SharedMemberId && c.GroupId == data.GroupId);

                    if (settle != null)
                    {
                        settle.TotalAmount = settle.TotalAmount + data.TotalAmount;
                        _Context.Settlement.Attach(settle);
                    }
                    else
                    {
                        var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == data.SharedMemberId && c.SharedMemberId == data.PayerId && c.GroupId == data.GroupId);

                        if (setle != null)
                        {
                            setle.TotalAmount = setle.TotalAmount - data.TotalAmount;
                            _Context.Settlement.Attach(setle);
                        }
                        else
                        {
                            var newSettle = new Settlement();
                            newSettle.PayerId        = data.PayerId;
                            newSettle.SharedMemberId = data.SharedMemberId;
                            newSettle.TotalAmount    = data.TotalAmount;
                            newSettle.GroupId        = data.GroupId;
                            _Context.Settlement.Add(newSettle);
                        }
                    }
                }
            }
            //if (newBill.GroupId == null)
            //{
            //    foreach (var person in bill.Payer)
            //    {
            //        foreach (var member in bill.SharedMember)
            //        {
            //            if (person.Id != member.Id)
            //            {
            //                Settlement settlement = new Settlement();
            //                settlement.PayerId = member.Id;
            //                settlement.SharedMemberId = person.Id;
            //                settlement.TotalAmount = member.Amount;

            //                var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId==null);
            //                if (settle != null)
            //                {
            //                    settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
            //                    _Context.Settlement.Attach(settle);
            //                }
            //                else
            //                {
            //                    var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == null);
            //                    if (setle != null)
            //                    {
            //                        setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
            //                        _Context.Settlement.Attach(setle);
            //                    }
            //                    else
            //                    {
            //                        _Context.Settlement.Add(settlement);
            //                    }
            //                }

            //            }
            //        }
            //    }
            //}
            //else
            //{
            //    foreach (var person in bill.Payer)
            //    {
            //        foreach (var member in bill.SharedMember)
            //        {
            //            if (person.Id != member.Id)
            //            {
            //                Settlement settlement = new Settlement();
            //                settlement.GroupId = newBill.GroupId;
            //                settlement.PayerId = member.Id;
            //                settlement.SharedMemberId = person.Id;
            //                settlement.TotalAmount = member.Amount;

            //                var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId == settlement.GroupId);
            //                if (settle != null)
            //                {
            //                    settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
            //                    _Context.Settlement.Attach(settle);
            //                }
            //                else
            //                {
            //                    var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == settlement.GroupId);
            //                    if (setle != null)
            //                    {
            //                        setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
            //                        _Context.Settlement.Attach(setle);
            //                    }
            //                    else
            //                    {
            //                        _Context.Settlement.Add(settlement);
            //                    }
            //                }
            //            }
            //        }
            //    }

            //}
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertBillAsync)}: " + exp.Message);
            }

            return(newBill);
        }
示例#4
0
        public async Task <Bill> InsertBillAsync(BillModel bill)
        {
            Bill newBill = new Bill();

            newBill.BillName    = bill.BillName;
            newBill.CreatorId   = bill.CreatorId;
            newBill.CreatedDate = bill.CreatedDate;
            newBill.GroupId     = bill.GroupId;
            _Context.Bill.Add(newBill);


            foreach (var person in bill.Payer)
            {
                Payer payer = new Payer();
                payer.BillId     = newBill.BillId;
                payer.PayerId    = person.Id;
                payer.PaidAmount = person.Amount;
                _Context.Payer.Add(payer);
            }


            foreach (var person in bill.SharedMember)
            {
                BillMember member = new BillMember();
                member.Billid         = newBill.BillId;
                member.SharedMemberId = person.Id;
                member.AmountToPay    = person.Amount;
                _Context.BillMember.Add(member);
            }


            if (newBill.GroupId == null)
            {
                foreach (var person in bill.Payer)
                {
                    foreach (var member in bill.SharedMember)
                    {
                        if (person.Id != member.Id)
                        {
                            Settlement settlement = new Settlement();
                            settlement.PayerId        = member.Id;
                            settlement.SharedMemberId = person.Id;
                            settlement.TotalAmount    = member.Amount;

                            var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId == null);

                            if (settle != null)
                            {
                                settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
                                _Context.Settlement.Attach(settle);
                            }
                            else
                            {
                                var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == null);

                                if (setle != null)
                                {
                                    setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
                                    _Context.Settlement.Attach(setle);
                                }
                                else
                                {
                                    _Context.Settlement.Add(settlement);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var person in bill.Payer)
                {
                    foreach (var member in bill.SharedMember)
                    {
                        if (person.Id != member.Id)
                        {
                            Settlement settlement = new Settlement();
                            settlement.GroupId        = newBill.GroupId;
                            settlement.PayerId        = member.Id;
                            settlement.SharedMemberId = person.Id;
                            settlement.TotalAmount    = member.Amount;

                            var settle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.PayerId && c.SharedMemberId == settlement.SharedMemberId && c.GroupId == settlement.GroupId);

                            if (settle != null)
                            {
                                settle.TotalAmount = settle.TotalAmount + settlement.TotalAmount;
                                _Context.Settlement.Attach(settle);
                            }
                            else
                            {
                                var setle = await _Context.Settlement.SingleOrDefaultAsync(c => c.PayerId == settlement.SharedMemberId && c.SharedMemberId == settlement.PayerId && c.GroupId == settlement.GroupId);

                                if (setle != null)
                                {
                                    setle.TotalAmount = setle.TotalAmount - settlement.TotalAmount;
                                    _Context.Settlement.Attach(setle);
                                }
                                else
                                {
                                    _Context.Settlement.Add(settlement);
                                }
                            }
                        }
                    }
                }
            }
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertBillAsync)}: " + exp.Message);
            }

            return(newBill);
        }