public void AddToNormalTrx(BankTrx trx) { trx.AddSplit( strCategoryKey_: strCatKey, strPONumber_: strPONumber ?? "", strInvoiceNum_: strInvoiceNum ?? "", datInvoiceDate_: (datInvoice <= Utilities.EmptyDate) ? Utilities.EmptyDate : datInvoice, datDueDate_: (datDue <= Utilities.EmptyDate) ? Utilities.EmptyDate : datDue, strTerms_: strTerms ?? "", strBudgetKey_: strBudgetKey ?? "", strMemo_: strMemo ?? "", curAmount_: curAmount); }
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); } }