Example #1
0
        protected override void BeginProcessing()
        {
            BankTrx normalTrx = new BankTrx(Register);

            normalTrx.NewStartNormal(blnWillAddToRegister: true,
                                     strNumber_: Number,
                                     datDate_: Date,
                                     strDescription_: Description,
                                     strMemo_: Memo == null ? "" : Memo,
                                     lngStatus_: Status,
                                     blnFake_: Fake.IsPresent,
                                     curNormalMatchRange_: NormalMatchRange,
                                     blnAwaitingReview_: AwaitingReview.IsPresent,
                                     blnAutoGenerated_: false,
                                     intRepeatSeq_: 0,
                                     strImportKey_: ImportKey == null ? "" : ImportKey,
                                     strRepeatKey_: "");
            if (OneSplit != null && Splits != null)
            {
                ThrowTerminatingError(ErrorUtilities.CreateInvalidOperation("-OneSplit and -Splits may not both be specified", "DupeSplits"));
            }
            else if (OneSplit != null)
            {
                OneSplit.AddToNormalTrx(normalTrx);
            }
            else if (Splits != null)
            {
                foreach (SplitContent split in Splits)
                {
                    split.AddToNormalTrx(normalTrx);
                }
            }
            else
            {
                ThrowTerminatingError(ErrorUtilities.CreateInvalidOperation("Either -OneSplit or -Splits must be specified", "NoSplits"));
            }
            Register.ValidationError += Register_ValidationError;
            try
            {
                normalTrx.Validate();
                if (TrxValidationError != null)
                {
                    ThrowTerminatingError(ErrorUtilities.CreateInvalidOperation(TrxValidationError, "InvalidTrx"));
                }
                Register.NewAddEnd(normalTrx, new LogAdd(), "PowershellAddNormalTrx");
            }
            finally
            {
                Register.ValidationError -= Register_ValidationError;
            }
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate    = ctlStartDate.Value.Date;
                DateTime endDate      = ctlEndDate.Value.Date;
                int      interestDays = endDate.Subtract(startDate).Days + 1;
                if (interestDays < 1)
                {
                    HostUI.ErrorMessageBox("Start date must be before end date.");
                    return;
                }
                IInterestCalculator calculator = (IInterestCalculator)cboInterestType.SelectedItem;
                if (calculator == null)
                {
                    HostUI.ErrorMessageBox("Please select an interest type.");
                    return;
                }
                string interestCatKey = GetInterestCategoryKey();
                if (interestCatKey == null)
                {
                    HostUI.ErrorMessageBox("Please select an interest category.");
                    return;
                }
                decimal annualRate;
                if (!decimal.TryParse(txtAnnualRate.Text, out annualRate))
                {
                    HostUI.ErrorMessageBox("Please enter a valid annual interest rate, like \"9.5\" for 9.5%.");
                    return;
                }

                // Compute daily balances from all selected registers
                decimal   startingBalance = 0M;
                decimal[] dailyTotals     = new decimal[interestDays];
                foreach (ListViewItem itm in lvwRegisters.CheckedItems)
                {
                    Register reg = (Register)itm.Tag;
                    foreach (BaseTrx baseTrx in new RegIterator <BaseTrx>(reg))
                    {
                        if (!baseTrx.IsFake && ((baseTrx is BankTrx) || (baseTrx is ReplicaTrx)))
                        {
                            if (baseTrx.TrxDate < startDate)
                            {
                                startingBalance += baseTrx.Amount;
                            }
                            else if (baseTrx.TrxDate <= endDate)
                            {
                                dailyTotals[baseTrx.TrxDate.Subtract(startDate).Days] += baseTrx.Amount;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                decimal[] dailyBalances    = new decimal[interestDays];
                decimal   prevBalance      = startingBalance;
                decimal   sumDailyBalances = 0M;
                for (int i = 0; i < interestDays; i++)
                {
                    dailyBalances[i]  = prevBalance + dailyTotals[i];
                    sumDailyBalances += dailyBalances[i];
                    prevBalance       = dailyBalances[i];
                }

                // Calculate interest
                decimal annualRateFraction = annualRate / 100M;
                decimal avgDailyBal        = Math.Round(sumDailyBalances / interestDays, 2);
                decimal totalInterest      = Math.Round(calculator.Calculate(startDate, dailyBalances, annualRateFraction), 2);
                string  memo = calculator.Memo(annualRateFraction, avgDailyBal);

                // Create bank trx in current register
                BankTrx  interestTrx    = new BankTrx(HostUI.GetCurrentRegister());
                DateTime dummy          = DateTime.MinValue;
                string   trxDescription = PersonalAcctExists ? "Interest:DIVIDE" : "Interest";
                interestTrx.NewStartNormal(true, "Inv", endDate, trxDescription, memo, BaseTrx.TrxStatus.Unreconciled,
                                           false, 0M, false, false, 0, "", "");
                interestTrx.AddSplit("", interestCatKey, "", "", Utilities.EmptyDate, Utilities.EmptyDate, "", "", totalInterest);
                if (!HostUI.AddNormalTrx(interestTrx, ref dummy, false, "Calculate Interest"))
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.TopException(ex);
            }
        }