示例#1
0
        public DebtsRegister(
            TablesDbContext dbc,
            TablesDbContextForReader rdbc,
            IPersonDebtsRegister personDebtsRegister,
            IPersonDebtsRegisterReader personDebtsRegisterReader,
            IPairDebtsRegister pairDebtsRegister,
            IPairDebtsRegisterReader pairDebtsRegisterReader,
            IDebtDealsRegister debtDealsRegister,
            IDebtDealsRegisterReader debtDealsRegisterReader,
            IAchieversRegister achieversRegister,
            IAchieversRegisterReader achieversRegisterReader
            )
        {
            this.dbc  = dbc;
            this.rdbc = rdbc;

            this.Person = personDebtsRegister;
            this.personDebtsRegisterReader = personDebtsRegisterReader;

            this.Pairs = pairDebtsRegister;
            this.pairDebtsRegisterReader = pairDebtsRegisterReader;

            this.Deals = debtDealsRegister;
            this.debtDealsRegisterReader = debtDealsRegisterReader;

            this.Achievers = achieversRegister;
            this.achieversRegisterReader = achieversRegisterReader;
        }
示例#2
0
        public PairDebtsRegister(
            TablesDbContext dbc,
            TablesDbContextForReader rdbc,
            IDebtDealsRegister debtDealsRegister
            )
        {
            this.dbc  = dbc;
            this.rdbc = rdbc;

            debtDealsRegister.DebtDealReceived += this.OnDebtDealReceived;
        }
示例#3
0
        public AchieversRegister(
            TablesDbContext dbc,
            TablesDbContextForReader rdbc,
            IMongoCollection <AchieversDoc> achieversCollection,
            IDebtDealsRegister debtDealsRegister,
            IPersonDebtsRegister personDebtsRegister
            )
        {
            this.dbc                 = dbc;
            this.rdbc                = rdbc;
            this.mongoCollection     = achieversCollection;
            this.personDebtsRegister = personDebtsRegister;

            debtDealsRegister.DebtDealReceived += this.OnDebtDealReceived;
            debtDealsRegister.DebtDealAdded    += this.OnDebtDealAdded;

            this.ByID = new AchieversByID(() => this.currentAchieversDoc);

            this.InitializeDocument();
        }
示例#4
0
        MakeDeals
        (
            IRegister rwRoot,
            TablesDbContextForReader rdbc,
            string giverId, string takerId, string thirdId,
            decimal totalAmount, uint countOfPaybacks,
            string logIndent
        )
        {
            IDebtDealsRegister rwDebtDeals = rwRoot.Debts.Deals;
            long creditDebtDealId          = rwDebtDeals.Add(new DebtDealRow {
                Time    = DateTime.Now,
                GiverId = giverId, TakerId = takerId,
                Amount  = totalAmount
            });

            decimal amountPaidBack     = 0;
            decimal accruedRepayError  = 0;
            var     paybackDebtDealIds = new List <long>();

            for (uint n = 0; n < countOfPaybacks; n++)
            {
                uint    nPaybacksLeft = countOfPaybacks - n;
                decimal dueAmount     = totalAmount - amountPaidBack;
                if (dueAmount < DebtConstants.ValueEpsilon)
                {
                    throw new InvalidOperationException(
                              $"got too small dueAmount: {dueAmount}");
                }

                string logIndent2 = logIndent + logIndent;
                this.output.WriteLine($"{logIndent}n = {n}:");
                this.output.WriteLine($"{logIndent2}dueAmount = {dueAmount}");

                decimal minPaybackLimit
                    = Math.Max(
                          dueAmount / nPaybacksLeft / 10
                          , DebtConstants.ValueEpsilon * nPaybacksLeft);
                decimal maxPaybackLimit
                    = dueAmount
                      - minPaybackLimit * (countOfPaybacks - n);

                decimal paybackAmount
                    = (decimal)(new Random().NextDouble())
                      * (maxPaybackLimit - minPaybackLimit)
                      + minPaybackLimit;

                this.output.WriteLine(
                    $"{logIndent2}paybackAmount = {paybackAmount}");

                if (n == countOfPaybacks - 1)
                {
                    paybackAmount = rwRoot.Debts.Pairs
                                    .GetCurrentDebt(giverId, takerId);

                    this.output.WriteLine(
                        $"{logIndent2}corrected last paybackAmount = "
                        + paybackAmount);
                }

                long paybackDebtDealId = rwDebtDeals.Add(new DebtDealRow {
                    Time    = DateTime.Now,
                    GiverId = takerId, TakerId = giverId,
                    Amount  = paybackAmount
                });
                paybackDebtDealIds.Add(paybackDebtDealId);
                amountPaidBack += paybackAmount;
                accruedRepayError
                    += DebtConstants.ValueRelativeError * amountPaidBack;
            }

            Assert.InRange(
                Math.Abs(amountPaidBack - totalAmount),
                0, accruedRepayError);

            return(creditDebtDealId, paybackDebtDealIds.ToArray());
        }