Exemplo n.º 1
0
        static bool TrySolveSequences(GrouppedTransactions groupOfTransactions, IEnumerable <GrouppedTransactions> grouppedTransactions)
        {
            var lenders = groupOfTransactions.Transactions.Select(db => db.LenderId);

            if (!transactions.Any(t => lenders.Contains(t.DebtorId)))
            {
                return(false);
            }

            var debtOfLender = transactions.Where(t => lenders.Contains(t.DebtorId))
                               .OrderByDescending(db => db.Amount)
                               .First();

            var debtToLender = groupOfTransactions.Transactions.First(db => db.LenderId == debtOfLender.DebtorId);

            var existingTransaction = transactions.Where(t => t.DebtorId == debtToLender.DebtorId &&
                                                         t.LenderId == debtOfLender.LenderId)
                                      .FirstOrDefault();

            if (debtToLender.Amount >= debtOfLender.Amount)
            {
                if (existingTransaction != null)
                {
                    existingTransaction.Amount += debtOfLender.Amount;
                }
                else
                {
                    transactions.Add(new Transaction
                    {
                        DebtorId = debtToLender.DebtorId,
                        LenderId = debtOfLender.LenderId,
                        Amount   = debtOfLender.Amount
                    });
                }

                debtToLender.Amount -= debtOfLender.Amount;
                transactions.Remove(debtOfLender);
            }
            else
            {
                if (existingTransaction != null)
                {
                    existingTransaction.Amount += debtToLender.Amount;
                }
                else
                {
                    transactions.Add(new Transaction
                    {
                        DebtorId = debtToLender.DebtorId,
                        LenderId = debtOfLender.LenderId,
                        Amount   = debtToLender.Amount
                    });
                }

                debtOfLender.Amount -= debtToLender.Amount;
                transactions.Remove(debtToLender);
            }

            return(true);
        }
Exemplo n.º 2
0
        static bool TrySolveNeighbours(GrouppedTransactions groupOfTransactions, IEnumerable <GrouppedTransactions> grouppedTransactions)
        {
            var lenders = groupOfTransactions.Transactions.Select(db => db.LenderId);

            var subjectWithCommonLender = grouppedTransactions.Where(g => g.Transactions.Any(t => t.LenderId == groupOfTransactions.SubjectId) &&
                                                                     g.Transactions.Any(t => lenders.Contains(t.LenderId)))
                                          .Select(g => new
            {
                CommonDebt    = g.Transactions.First(t => lenders.Contains(t.LenderId)),
                DebtToSubject = g.Transactions.First(t => t.LenderId == groupOfTransactions.SubjectId)
            })
                                          .FirstOrDefault();

            if (subjectWithCommonLender == null)
            {
                return(false);
            }

            var commonDebt = groupOfTransactions.Transactions.First(t => t.LenderId == subjectWithCommonLender.CommonDebt.LenderId);

            if (subjectWithCommonLender.DebtToSubject.Amount >= commonDebt.Amount)
            {
                subjectWithCommonLender.CommonDebt.Amount    += commonDebt.Amount;
                subjectWithCommonLender.DebtToSubject.Amount -= commonDebt.Amount;

                transactions.Remove(commonDebt);
            }
            else
            {
                commonDebt.Amount -= subjectWithCommonLender.DebtToSubject.Amount;
                subjectWithCommonLender.CommonDebt.Amount += subjectWithCommonLender.DebtToSubject.Amount;

                transactions.Remove(subjectWithCommonLender.DebtToSubject);
            }

            return(true);
        }