/// <summary>
        /// prepare the test case
        /// </summary>
        public static bool ImportAndPostGiftBatch(int ALedgerNumber, out TVerificationResultCollection VerificationResult)
        {
            TGiftImporting importer = new TGiftImporting();

            string testFile = TAppSettingsManager.GetValue("GiftBatch.file", "../../csharp/ICT/Testing/lib/MFinance/SampleData/sampleGiftBatch.csv");

            StreamReader sr = new StreamReader(testFile);
            string FileContent = sr.ReadToEnd();

            FileContent = FileContent.Replace("{ledgernumber}", ALedgerNumber.ToString());
            FileContent = FileContent.Replace("{thisyear}", DateTime.Today.Year.ToString());

            sr.Close();

            Hashtable parameters = new Hashtable();
            parameters.Add("Delimiter", ",");
            parameters.Add("ALedgerNumber", ALedgerNumber);
            parameters.Add("DateFormatString", "yyyy-MM-dd");
            parameters.Add("NumberFormat", "American");
            parameters.Add("NewLine", Environment.NewLine);

            GiftBatchTDSAGiftDetailTable NeedRecipientLedgerNumber;

            if (!importer.ImportGiftBatches(parameters, FileContent, out NeedRecipientLedgerNumber, out VerificationResult))
            {
                return false;
            }

            int BatchNumber = importer.GetLastGiftBatchNumber();

            if (!TGiftTransactionWebConnector.PostGiftBatch(ALedgerNumber, BatchNumber, out VerificationResult))
            {
                CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult);

                return false;
            }

            return true;
        }
        public void TestPostGiftBatch()
        {
            TGiftImporting importer = new TGiftImporting();

            string testFile = TAppSettingsManager.GetValue("GiftBatch.file", "../../csharp/ICT/Testing/lib/MFinance/SampleData/sampleGiftBatch.csv");
            StreamReader sr = new StreamReader(testFile);
            string FileContent = sr.ReadToEnd();

            FileContent = FileContent.Replace("{ledgernumber}", FLedgerNumber.ToString());
            FileContent = FileContent.Replace("{thisyear}", DateTime.Today.Year.ToString());

            sr.Close();

            Hashtable parameters = new Hashtable();
            parameters.Add("Delimiter", ",");
            parameters.Add("ALedgerNumber", FLedgerNumber);
            parameters.Add("DateFormatString", "yyyy-MM-dd");
            parameters.Add("NumberFormat", "American");
            parameters.Add("NewLine", Environment.NewLine);

            TVerificationResultCollection VerificationResult = null;
            GiftBatchTDSAGiftDetailTable NeedRecipientLedgerNumber;

            if (!importer.ImportGiftBatches(parameters, FileContent, out NeedRecipientLedgerNumber, out VerificationResult))
            {
                Assert.Fail("Gift Batch was not imported: " + VerificationResult.BuildVerificationResultString());
            }

            int BatchNumber = importer.GetLastGiftBatchNumber();

            Assert.AreNotEqual(-1, BatchNumber, "Should have imported the gift batch and returned a valid batch number");

            if (!TGiftTransactionWebConnector.PostGiftBatch(FLedgerNumber, BatchNumber, out VerificationResult))
            {
                Assert.Fail("Gift Batch was not posted: " + VerificationResult.BuildVerificationResultString());
            }
        }
        public void TestMultipleGifts()
        {
            // import the test gift batch, and post it
            TGiftImporting importer = new TGiftImporting();

            string dirTestData = "../../csharp/ICT/Testing/lib/MFinance/server/BankImport/TestData/";
            string testFile = dirTestData + "GiftBatch.csv";
            StreamReader sr = new StreamReader(testFile);
            string FileContent = sr.ReadToEnd();

            sr.Close();
            FileContent = FileContent.Replace("2010-09-30", DateTime.Now.Year.ToString("0000") + "-09-30");

            Hashtable parameters = new Hashtable();
            parameters.Add("Delimiter", ",");
            parameters.Add("ALedgerNumber", FLedgerNumber);
            parameters.Add("DateFormatString", "yyyy-MM-dd");
            parameters.Add("NumberFormat", "American");
            parameters.Add("NewLine", Environment.NewLine);

            TVerificationResultCollection VerificationResult;
            GiftBatchTDSAGiftDetailTable NeedRecipientLedgerNumber;

            importer.ImportGiftBatches(parameters, FileContent, out NeedRecipientLedgerNumber, out VerificationResult);

            int BatchNumber = importer.GetLastGiftBatchNumber();

            Assert.AreNotEqual(-1, BatchNumber, "Failed to import gift batch: " + VerificationResult.BuildVerificationResultString());

            if (!TGiftTransactionWebConnector.PostGiftBatch(FLedgerNumber, BatchNumber, out VerificationResult))
            {
                Assert.Fail("Gift Batch was not posted: " + VerificationResult.BuildVerificationResultString());
            }

            // import the test csv file, will already do the training
            TBankStatementImport import = new TBankStatementImport();

            testFile = dirTestData + "BankStatement.csv";
            sr = new StreamReader(testFile);
            FileContent = sr.ReadToEnd();
            sr.Close();
            FileContent = FileContent.Replace("30.09.2010", "30.09." + DateTime.Now.Year.ToString("0000"));

            Int32 StatementKey;
            BankImportTDS BankImportDS = import.ImportBankStatementNonInteractive(
                FLedgerNumber,
                "6200",
                ";",
                "DMY",
                TDlgSelectCSVSeparator.NUMBERFORMAT_EUROPEAN,
                "unused,DateEffective,Description,Amount,Currency",
                "BankStatementSeptember.csv",
                FileContent);
            Assert.AreNotEqual(null, BankImportDS, "valid bank import dataset september");

            Assert.AreEqual(TSubmitChangesResult.scrOK, TBankImportWebConnector.StoreNewBankStatement(
                    BankImportDS,
                    out StatementKey), "save statement September");

            // revert the gift batch, so that the training does not get confused if the test is run again;
            // the training needs only one gift batch for that date
            Hashtable requestParams = new Hashtable();
            requestParams.Add("Function", "ReverseGiftBatch");
            requestParams.Add("ALedgerNumber", FLedgerNumber);
            requestParams.Add("BatchNumber", BatchNumber);
            requestParams.Add("GiftDetailNumber", -1);
            requestParams.Add("GiftNumber", -1);
            requestParams.Add("NewBatchSelected", false);
            requestParams.Add("GlEffectiveDate", new DateTime(DateTime.Now.Year, 09, 30));

            int AdjustmentBatchNumber;
            GiftBatchTDS GiftReverseDS = TGiftTransactionWebConnector.LoadGiftTransactionsForBatch(FLedgerNumber, BatchNumber);
            Assert.AreEqual(true, TAdjustmentWebConnector.GiftRevertAdjust(requestParams, out AdjustmentBatchNumber, GiftReverseDS), "reversing the gift batch");

            // duplicate the bank import file, for the next month
            FileContent = FileContent.Replace("30.09." + DateTime.Now.Year.ToString("0000"),
                "30.10." + DateTime.Now.Year.ToString("0000"));

            BankImportDS = import.ImportBankStatementNonInteractive(
                FLedgerNumber,
                "6200",
                ";",
                "DMY",
                TDlgSelectCSVSeparator.NUMBERFORMAT_EUROPEAN,
                "unused,DateEffective,Description,Amount,Currency",
                "BankStatementOcotober.csv",
                FileContent);
            Assert.AreNotEqual(null, BankImportDS, "valid bank import dataset october");

            Assert.AreEqual(TSubmitChangesResult.scrOK, TBankImportWebConnector.StoreNewBankStatement(
                    BankImportDS,
                    out StatementKey), "save statement October");

            // create gift batch from imported statement
            Int32 GiftBatchNumber = TBankImportWebConnector.CreateGiftBatch(
                FLedgerNumber,
                StatementKey,
                -1,
                out VerificationResult);

            CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                "cannot create gift batch from bank statement:");

            // check if the gift batch is correct
            GiftBatchTDS GiftDS = TGiftTransactionWebConnector.LoadAGiftBatchAndRelatedData(FLedgerNumber, GiftBatchNumber);

            // since we are not able to match the split gift, only 2 gifts should be matched.
            // TODO: allow 2 gifts to be merged in OpenPetra, even when they come separate on the bank statement.
            //           then 4 gifts could be matched.
            Assert.AreEqual(2, GiftDS.AGift.Rows.Count, "expected two matched gifts");
        }
        /// <summary>
        /// This will import a test gift batch, and post it.
        /// </summary>
        public static int ImportAndPostGiftBatch(DateTime AGiftDateEffective)
        {
            TGiftImporting importer = new TGiftImporting();

            string testFile = TAppSettingsManager.GetValue("GiftBatch.file",
                CommonNUnitFunctions.rootPath + "/csharp/ICT/Testing/lib/MFinance/SampleData/sampleGiftBatch.csv");

            TLogging.Log(testFile);

            StreamReader sr = new StreamReader(testFile);
            string FileContent = sr.ReadToEnd();

            sr.Close();

            FileContent = FileContent.Replace("{ledgernumber}", FLedgerNumber.ToString());
            FileContent = FileContent.Replace("{thisyear}-01-01", AGiftDateEffective.ToString("yyyy-MM-dd"));

            Hashtable parameters = new Hashtable();
            parameters.Add("Delimiter", ",");
            parameters.Add("ALedgerNumber", FLedgerNumber);
            parameters.Add("DateFormatString", "yyyy-MM-dd");
            parameters.Add("NumberFormat", "American");
            parameters.Add("NewLine", Environment.NewLine);

            TVerificationResultCollection VerificationResult;
            GiftBatchTDSAGiftDetailTable NeedRecipientLedgerNumber;

            importer.ImportGiftBatches(parameters, FileContent, out NeedRecipientLedgerNumber, out VerificationResult);

            CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                "error when importing gift batch:");

            int BatchNumber = importer.GetLastGiftBatchNumber();

            Assert.AreNotEqual(-1, BatchNumber, "Should have imported the gift batch and return a valid batch number");

            if (!TGiftTransactionWebConnector.PostGiftBatch(FLedgerNumber, BatchNumber, out VerificationResult))
            {
                string VerifResStr;

                if (VerificationResult != null)
                {
                    VerifResStr = ": " + VerificationResult.BuildVerificationResultString();
                }
                else
                {
                    VerifResStr = String.Empty;
                }

                Assert.Fail("Gift Batch was not posted" + VerifResStr);
            }

            return BatchNumber;
        }
        public void TestPostGiftBatchWithMotivationDetailCostCentre()
        {
            // import a gift batch, that we will modify later
            TGiftImporting importer = new TGiftImporting();

            string testFile = TAppSettingsManager.GetValue("GiftBatch.file", "../../csharp/ICT/Testing/lib/MFinance/SampleData/sampleGiftBatch.csv");
            StreamReader sr = new StreamReader(testFile);
            string FileContent = sr.ReadToEnd();

            FileContent = FileContent.Replace("{ledgernumber}", FLedgerNumber.ToString());
            FileContent = FileContent.Replace("{thisyear}", DateTime.Today.Year.ToString());

            sr.Close();

            Hashtable parameters = new Hashtable();
            parameters.Add("Delimiter", ",");
            parameters.Add("ALedgerNumber", FLedgerNumber);
            parameters.Add("DateFormatString", "yyyy-MM-dd");
            parameters.Add("NumberFormat", "American");
            parameters.Add("NewLine", Environment.NewLine);

            TVerificationResultCollection VerificationResult = null;
            GiftBatchTDSAGiftDetailTable NeedRecipientLedgerNumber;

            if (!importer.ImportGiftBatches(parameters, FileContent, out NeedRecipientLedgerNumber, out VerificationResult))
            {
                Assert.Fail("Gift Batch was not imported: " + VerificationResult.BuildVerificationResultString());
            }

            int BatchNumber = importer.GetLastGiftBatchNumber();

            Assert.AreNotEqual(-1, BatchNumber, "Should have imported the gift batch and returned a valid batch number");

            bool NewTransaction = false;

            TDBTransaction Transaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted, out NewTransaction);

            // create a new costcentre
            const string newCostCentre = "100001";
            if (!ACostCentreAccess.Exists(FLedgerNumber, newCostCentre, Transaction))
            {
                ACostCentreTable cc = new ACostCentreTable();
                ACostCentreRow ccrow = cc.NewRowTyped();
                ccrow.LedgerNumber = FLedgerNumber;
                ccrow.CostCentreCode = newCostCentre;
                ccrow.CostCentreName = newCostCentre;
                ccrow.PostingCostCentreFlag = true;
                ccrow.CostCentreToReportTo = (FLedgerNumber*100).ToString("0000");
                ccrow.CostCentreType = MFinanceConstants.LOCAL_CC_TYPE;
                cc.Rows.Add(ccrow);
                ACostCentreAccess.SubmitChanges(cc, Transaction, UserInfo.GUserInfo.UserID);
            }

            // create a new motivation detail
            if (!AMotivationDetailAccess.Exists(FLedgerNumber, MFinanceConstants.MOTIVATION_GROUP_GIFT, newCostCentre, Transaction))
            {
                AMotivationDetailTable mot = new AMotivationDetailTable();
                AMotivationDetailRow motrow = mot.NewRowTyped();
                motrow.LedgerNumber = FLedgerNumber;
                motrow.MotivationGroupCode = MFinanceConstants.MOTIVATION_GROUP_GIFT;
                motrow.MotivationDetailCode = newCostCentre;
                motrow.AccountCode = "0100";
                motrow.CostCentreCode = newCostCentre;
                motrow.MotivationDetailDesc = newCostCentre;
                mot.Rows.Add(motrow);
                AMotivationDetailAccess.SubmitChanges(mot, Transaction, UserInfo.GUserInfo.UserID);
            }

            // modify the gift batch with that motivation detail, but with wrong costcentre
            AGiftDetailTable giftdetails = AGiftDetailAccess.LoadByPrimaryKey(FLedgerNumber, BatchNumber, 1, 1, null, Transaction);
            giftdetails[0].MotivationDetailCode = newCostCentre;
            giftdetails[0].RecipientKey = 0;
            Assert.AreNotEqual(newCostCentre, giftdetails[0].CostCentreCode, "cost centre code should not match the one defined by the motivation detail");
            AGiftDetailAccess.SubmitChanges(giftdetails, Transaction);

            DBAccess.GDBAccessObj.CommitTransaction();

            if (!TGiftTransactionWebConnector.PostGiftBatch(FLedgerNumber, BatchNumber, out VerificationResult))
            {
                Assert.Fail("Gift Batch was not posted: " + VerificationResult.BuildVerificationResultString());
            }

            Transaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted, out NewTransaction);
            giftdetails = AGiftDetailAccess.LoadByPrimaryKey(FLedgerNumber, BatchNumber, 1, 1, null, Transaction);
            Assert.AreEqual(newCostCentre, giftdetails[0].CostCentreCode, "cost centre code should have been adjusted because of the motivation detail");
            DBAccess.GDBAccessObj.CommitTransaction();
        }