Exemplo n.º 1
0
        public async Task TestCase1(int hid, string currency, string user, short doctype)
        {
            var context = this.fixture.GetCurrentDataContext();

            // 0. Prepare the context for current home
            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            else if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            else if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
            }
            else if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
            }
            else if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
            }
            var account = context.FinanceAccount.Where(p => p.HomeID == hid && p.Status != (Byte)FinanceAccountStatus.Closed).FirstOrDefault();
            var cc      = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault();

            // 1. Create first Loan docs.
            var control   = new FinanceDocumentsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };

            // 1a. Prepare data
            var dpcontext = new FinanceLoanDocumentCreateContext();

            dpcontext.DocumentInfo = new FinanceDocument()
            {
                HomeID   = hid,
                DocType  = doctype,
                TranCurr = currency,
                Desp     = "Test 1"
            };
            var item = new FinanceDocumentItem()
            {
                DocumentHeader = dpcontext.DocumentInfo,
                ItemID         = 1,
                Desp           = "Item 1.1",
                TranType       = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_BorrowFrom
                        : FinanceTransactionType.TranType_LendTo,
                TranAmount      = 1200,
                AccountID       = account.ID,
                ControlCenterID = cc.ID,
            };

            dpcontext.DocumentInfo.Items.Add(item);
            dpcontext.AccountInfo = new FinanceAccount()
            {
                HomeID     = hid,
                Name       = "Account_8" + ".1",
                CategoryID = doctype == FinanceDocumentType.DocType_BorrowFrom
                    ? FinanceAccountCategory.AccountCategory_BorrowFrom
                    : FinanceAccountCategory.AccountCategory_LendTo,
                Owner  = user,
                Status = (Byte)FinanceAccountStatus.Normal,
            };
            var startdate = new DateTime(2020, 1, 10);
            var enddate   = new DateTime(2021, 1, 10);

            dpcontext.AccountInfo.ExtraLoan = new FinanceAccountExtraLoan()
            {
                StartDate       = startdate,
                EndDate         = enddate,
                TotalMonths     = 12,
                RepaymentMethod = LoanRepaymentMethod.EqualPrincipal,
                InterestFree    = false,
            };
            var rsts = CommonUtility.WorkoutRepeatedDatesWithAmountAndInterest(new RepeatDatesWithAmountAndInterestCalInput
            {
                RepaymentMethod  = dpcontext.AccountInfo.ExtraLoan.RepaymentMethod.Value,
                InterestFreeLoan = dpcontext.AccountInfo.ExtraLoan.InterestFree.Value,
                StartDate        = dpcontext.AccountInfo.ExtraLoan.StartDate,
                TotalAmount      = 12000,
                EndDate          = dpcontext.AccountInfo.ExtraLoan.EndDate,
                TotalMonths      = dpcontext.AccountInfo.ExtraLoan.TotalMonths.Value,
                FirstRepayDate   = new DateTime(2020, 2, 15)
            });
            var tmpdocid = 1;

            foreach (var rst in rsts)
            {
                var tmpdoc = new FinanceTmpLoanDocument
                {
                    DocumentID        = tmpdocid++,
                    TransactionAmount = rst.TranAmount,
                    InterestAmount    = rst.InterestAmount,
                    TransactionDate   = rst.TranDate,
                    HomeID            = hid,
                    ControlCenterID   = item.ControlCenterID,
                    OrderID           = item.OrderID,
                    Description       = item.Desp,
                };

                dpcontext.AccountInfo.ExtraLoan.LoanTmpDocs.Add(tmpdoc);
            }
            var resp = await control.PostLoanDocument(dpcontext);

            var doc = Assert.IsType <CreatedODataResult <FinanceDocument> >(resp).Entity;

            documentsCreated.Add(doc.ID);
            Assert.True(doc.Items.Count == 2);

            // Now check in the databse
            foreach (var docitem in doc.Items)
            {
                if (docitem.AccountID != account.ID)
                {
                    accountsCreated.Add(docitem.AccountID);

                    var acnt = context.FinanceAccount.Find(docitem.AccountID);
                    Assert.NotNull(acnt);
                    if (doctype == FinanceDocumentType.DocType_BorrowFrom)
                    {
                        Assert.True(acnt.CategoryID == FinanceAccountCategory.AccountCategory_BorrowFrom);
                    }
                    else if (doctype == FinanceDocumentType.DocType_LendTo)
                    {
                        Assert.True(acnt.CategoryID == FinanceAccountCategory.AccountCategory_LendTo);
                    }
                    var acntExtraLoan = context.FinanceAccountExtraLoan.Find(docitem.AccountID);
                    Assert.NotNull(acntExtraLoan);
                    Assert.True(acntExtraLoan.RefDocID == doc.ID);

                    var tmpdocs = context.FinanceTmpLoanDocument.Where(p => p.AccountID == docitem.AccountID).OrderBy(p => p.TransactionDate).ToList();
                    Assert.True(rsts.Count == tmpdocs.Count);

                    foreach (var rst in rsts)
                    {
                        DateTime dat  = rst.TranDate;
                        var      tdoc = tmpdocs.Find(p => p.TransactionDate.Date == dat);
                        Assert.NotNull(tdoc);
                        Assert.True(tdoc.AccountID == acntExtraLoan.AccountID);
                    }
                }
            }

            // Last, clear all created objects
            CleanupCreatedEntries();

            await context.DisposeAsync();
        }
        public async Task TestCase1_InterestFree(int hid, string currency, string user, short doctype)
        {
            var context = this.fixture.GetCurrentDataContext();

            // 0. Prepare the context for current home
            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            else if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            else if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
            }
            else if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
            }
            else if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
            }
            var account = context.FinanceAccount.Where(p => p.HomeID == hid && p.Status != (Byte)FinanceAccountStatus.Closed).FirstOrDefault();
            var cc      = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault();

            // 1. Create first Loan docs.
            var control   = new FinanceDocumentsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };

            // 1a. Prepare data
            var dpcontext = new FinanceLoanDocumentCreateContext();

            dpcontext.DocumentInfo = new FinanceDocument()
            {
                HomeID   = hid,
                DocType  = doctype,
                TranCurr = currency,
                Desp     = "Test 1"
            };
            var item = new FinanceDocumentItem()
            {
                DocumentHeader = dpcontext.DocumentInfo,
                ItemID         = 1,
                Desp           = "Item 1.1",
                TranType       = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_BorrowFrom
                        : FinanceTransactionType.TranType_LendTo,
                TranAmount      = 1200,
                AccountID       = account.ID,
                ControlCenterID = cc.ID,
            };

            dpcontext.DocumentInfo.Items.Add(item);
            dpcontext.AccountInfo = new FinanceAccount()
            {
                HomeID     = hid,
                Name       = "Account_8" + ".1",
                CategoryID = doctype == FinanceDocumentType.DocType_BorrowFrom
                    ? FinanceAccountCategory.AccountCategory_BorrowFrom
                    : FinanceAccountCategory.AccountCategory_LendTo,
                Owner  = user,
                Status = (Byte)FinanceAccountStatus.Normal,
            };
            var startdate = new DateTime(2020, 1, 10);
            var enddate   = new DateTime(2021, 1, 10);

            dpcontext.AccountInfo.ExtraLoan = new FinanceAccountExtraLoan()
            {
                StartDate       = startdate,
                EndDate         = enddate,
                TotalMonths     = 12,
                RepaymentMethod = LoanRepaymentMethod.EqualPrincipal,
                InterestFree    = true,
            };
            var rsts = CommonUtility.WorkoutRepeatedDatesWithAmountAndInterest(new RepeatDatesWithAmountAndInterestCalInput
            {
                RepaymentMethod  = dpcontext.AccountInfo.ExtraLoan.RepaymentMethod.Value,
                InterestFreeLoan = dpcontext.AccountInfo.ExtraLoan.InterestFree.Value,
                StartDate        = dpcontext.AccountInfo.ExtraLoan.StartDate,
                TotalAmount      = 1200,
                EndDate          = dpcontext.AccountInfo.ExtraLoan.EndDate,
                TotalMonths      = dpcontext.AccountInfo.ExtraLoan.TotalMonths.Value,
                FirstRepayDate   = new DateTime(2020, 2, 15)
            });
            var tmpdocid = 1;

            foreach (var rst in rsts)
            {
                var tmpdoc = new FinanceTmpLoanDocument
                {
                    DocumentID        = tmpdocid++,
                    TransactionAmount = rst.TranAmount,
                    InterestAmount    = rst.InterestAmount,
                    TransactionDate   = rst.TranDate,
                    HomeID            = hid,
                    ControlCenterID   = item.ControlCenterID,
                    OrderID           = item.OrderID,
                    Description       = item.Desp,
                };

                dpcontext.AccountInfo.ExtraLoan.LoanTmpDocs.Add(tmpdoc);
            }
            var resp = await control.PostLoanDocument(dpcontext);

            var doc = Assert.IsType <CreatedODataResult <FinanceDocument> >(resp).Entity;

            documentsCreated.Add(doc.ID);
            var loanacntid = -1;

            foreach (var docitem in doc.Items)
            {
                if (docitem.AccountID != account.ID)
                {
                    loanacntid = docitem.AccountID;
                    accountsCreated.Add(loanacntid);
                }
            }
            Assert.True(doc.Items.Count == 2);

            // 2. Switch to second controller
            var tmpcontrol = new FinanceTmpLoanDocumentsController(context);

            tmpcontrol.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };
            var tmpdocs = tmpcontrol.Get();

            Assert.NotEmpty(tmpdocs);
            Assert.Equal(rsts.Count, tmpdocs.Count());
            var dpdocs = tmpdocs.Cast <FinanceTmpLoanDocument>();

            // 3. Create repay document
            foreach (var dpdoc in dpdocs)
            {
                var contxt = new FinanceLoanRepayDocumentCreateContext();
                contxt.HomeID = hid;
                contxt.LoanTemplateDocumentID = dpdoc.DocumentID;
                contxt.DocumentInfo           = new FinanceDocument
                {
                    DocType  = FinanceDocumentType.DocType_Repay,
                    HomeID   = hid,
                    Desp     = dpdoc.Description,
                    TranCurr = currency,
                };
                contxt.DocumentInfo.Items.Add(new FinanceDocumentItem
                {
                    ItemID          = 1,
                    AccountID       = dpdoc.AccountID,
                    TranAmount      = dpdoc.TransactionAmount,
                    ControlCenterID = dpdoc.ControlCenterID,
                    OrderID         = dpdoc.OrderID,
                    TranType        = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_RepaymentIn
                        : FinanceTransactionType.TranType_RepaymentOut,
                });
                contxt.DocumentInfo.Items.Add(new FinanceDocumentItem
                {
                    ItemID          = 2,
                    AccountID       = account.ID,
                    TranAmount      = dpdoc.TransactionAmount,
                    ControlCenterID = dpdoc.ControlCenterID,
                    OrderID         = dpdoc.OrderID,
                    TranType        = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_RepaymentOut
                        : FinanceTransactionType.TranType_RepaymentIn,
                });
                var repaydocresp = await tmpcontrol.PostRepayDocument(contxt);

                var repaydoc = Assert.IsType <CreatedODataResult <FinanceDocument> >(repaydocresp);
                Assert.True(repaydoc.Entity.ID > 0);
                documentsCreated.Add(repaydoc.Entity.ID);

                // Check in the database
                var dpdocInDB = context.FinanceTmpLoanDocument.Where(p => p.DocumentID == dpdoc.DocumentID).SingleOrDefault();
                Assert.NotNull(dpdocInDB);
                Assert.NotNull(dpdocInDB.ReferenceDocumentID);
            }

            // 4. Now the account shall be closed automatically
            var account2 = context.FinanceAccount.Where(p => p.HomeID == hid && p.ID == loanacntid).FirstOrDefault();

            Assert.True(account2.Status == (Byte)FinanceAccountStatus.Closed);

            // Last, clear all created objects
            CleanupCreatedEntries();

            await context.DisposeAsync();
        }