Ejemplo n.º 1
0
        public static Form AmountByTagsReport(Control parameters)
        {
            const String columnTag = "tag";
            const String columnSpent = "spent";
            const String columnEarned = "earned";
            const String columnDelta = "delta";
            const String columnTransactions = "transactions";
            try
            {
                MoneyDataKeeper keeper = MoneyDataKeeper.Instance;
                DateTypesTagsControl pars = parameters as DateTypesTagsControl;
                DateTime StartDate = DateTime.Now;
                DateTime EndDate = DateTime.Now;
                IEnumerable<MoneyDataSet.AccountTypesRow> AccountTypes = null;
                IEnumerable<MoneyDataSet.TransactionTypesRow> TransactionTypes = null;
                IEnumerable<String> Tags = null;
                if (pars != null)
                {
                    MethodInvoker inv = delegate()
                    {
                        StartDate = pars.dtpStartDate.Value;
                        EndDate = pars.dtpEndDate.Value;
                        AccountTypes = pars.lbAccountTypes.SelectedItems.Cast<MoneyDataSet.AccountTypesRow>().ToArray();
                        TransactionTypes = pars.lbTransactionTypes.SelectedItems.Cast<MoneyDataSet.TransactionTypesRow>().ToArray();
                        Tags = pars.lbTags.SelectedItems.Cast<String>().ToArray();
                    };
                    pars.Invoke(inv);

                    ReportGridForm form = new ReportGridForm();
                    form.Text = String.Format(Resources.Labels.AmountByTagsFormTitleFormat, StartDate.ToShortDateString(), EndDate.ToShortDateString());

                    form.Grid.Columns.Add(columnTag, Resources.Labels.TagTitle);
                    form.Grid.Columns.Add(columnSpent, Resources.Labels.SpentTitle);
                    form.Grid.Columns.Add(columnEarned, Resources.Labels.EarnedTitle);
                    form.Grid.Columns.Add(columnDelta, Resources.Labels.DeltaTitle);
                    form.Grid.Columns.Add(columnTransactions, Resources.Labels.TransactionsTitle);

                    form.Grid.Columns[columnSpent].CellTemplate.Style.Format = Consts.UI.NumericFormat;
                    form.Grid.Columns[columnEarned].CellTemplate.Style.Format = Consts.UI.NumericFormat;
                    form.Grid.Columns[columnDelta].CellTemplate.Style.Format = Consts.UI.NumericFormat;
                    form.Grid.Columns[columnTransactions].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                    form.Grid.Columns[columnTag].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                    form.Grid.Columns[columnSpent].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    form.Grid.Columns[columnEarned].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    form.Grid.Columns[columnDelta].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

                    form.Grid.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke;

                    // preparing data
                    foreach (MoneyDataSet.TagsRow tag in keeper.DataSet.Tags.Where(t => (Tags.Contains(t.Title))))
                    {
                        double spent = 0;
                        double earned = 0;
                        StringBuilder transactions = new StringBuilder();

                        IEnumerable<MoneyDataSet.TransactionsRow> transactionList = keeper.Transactions.Where(t =>
                            ((t.IsActive) && (!t.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.Correction)) &&
                            (TransactionTypes.Contains(t.TransactionTypesRow)) &&
                            (AccountTypes.Contains(t.AccountRow.AccountTypesRow)) &&
                            (t.EntryTime >= StartDate.Date) &&
                            (t.EntryTime < EndDate.Date.AddDays(1)) &&
                            (t.GetTransactionTagsRows().Where(tt => (tt.TagID == tag.ID)).Any())));

                        foreach (MoneyDataSet.TransactionsRow transaction in transactionList)
                        {

                            if (transaction.TransactionTypesRow.IsIncome)
                            {
                                earned += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                            }
                            else
                            {
                                spent += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                            }
                            if (transactions.Length > 0)
                            {
                                transactions.Append(", ");
                            }
                            transactions.Append(transaction.Title);
                        }
                        if ((spent != 0) || (earned != 0))
                        {
                            form.Grid.Rows.Add(tag.Title, spent, earned, (earned - spent), transactions.ToString());
                        }
                    }

                    form.Grid.Columns[columnTag].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnSpent].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnEarned].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnDelta].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                    form.Grid.Sort(form.Grid.Columns[columnTag], ListSortDirection.Ascending);

                    form.Grid.Refresh();

                    return form;
                }
            }
            catch (Exception e)
            {
                Log.Write(e);
            }
            return null;
        }
Ejemplo n.º 2
0
        public static Form MonthBalanceReport(Control parameters)
        {
            const String columnDate = "date";
            const String columnType = "type";
            const String columnTypeIcon = "typeIcon";
            const String columnStatus = "status";
            const String columnTransaction = "transaction";
            const String columnAmountEarned = "amountearned";
            const String columnAmountSpent = "amountspent";
            const String columnPlannedAmountEarn = "plannedamountearn";
            const String columnPlannedAmountSpend = "plannedamountspend";
            const String columnAccount = "account";
            const String columnDifferenceIcon = "differenceicon";
            const String columnDifference = "difference";
            const String columnTags = "tags";

            try
            {
                MoneyDataKeeper keeper = MoneyDataKeeper.Instance;

                DateTime current = DateTime.Now;
                if (parameters != null)
                {
                    ComboBox cbMonth = (parameters as MonthPickerControl).cbMonth;
                    MethodInvoker inv = delegate() { current = (DateTime)cbMonth.SelectedItem; };
                    cbMonth.Invoke(inv);
                }
                else
                {
                    current = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                }
                ReportGridForm form = new ReportGridForm();
                form.Text = String.Format(Resources.Labels.MonthBalanceFormTitleFormat, current);

                form.Grid.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;

                form.Grid.Columns.Add(columnDate, Resources.Labels.DateTitle);
                form.Grid.Columns.Add(columnStatus, Resources.Labels.StatusTitle);

                DataGridViewImageColumn columnIcon = new DataGridViewImageColumn();
                columnIcon.Name = columnDifferenceIcon;
                columnIcon.HeaderText = Resources.Labels.DifferenceIconTitle;

                form.Grid.Columns.Add(columnIcon);

                form.Grid.Columns.Add(columnType, Resources.Labels.TypeTitle);
                DataGridViewImageColumn columnIconType = new DataGridViewImageColumn();
                columnIconType.Name = columnTypeIcon;
                columnIconType.HeaderText = Resources.Labels.TypeIconTitle;

                form.Grid.Columns.Add(columnIconType);

                form.Grid.Columns.Add(columnTransaction, Resources.Labels.TransactionTitle);
                form.Grid.Columns.Add(columnAmountEarned, Resources.Labels.EarnedTitle);
                form.Grid.Columns.Add(columnAmountSpent, Resources.Labels.SpentTitle);
                form.Grid.Columns.Add(columnPlannedAmountEarn, Resources.Labels.EarnTitle);
                form.Grid.Columns.Add(columnPlannedAmountSpend, Resources.Labels.SpendTitle);

                form.Grid.Columns.Add(columnDifference, Resources.Labels.DifferenceTitle);
                form.Grid.Columns.Add(columnAccount, Resources.Labels.AccountTitle);
                form.Grid.Columns.Add(columnTags, Resources.Labels.TagsTitle);

                form.Grid.Columns[columnDate].CellTemplate.Style.Format = Consts.UI.DateFormat;
                form.Grid.Columns[columnTags].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                form.Grid.Columns[columnType].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                form.Grid.Columns[columnStatus].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

                form.Grid.Columns[columnDate].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                form.Grid.Columns[columnAmountEarned].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                form.Grid.Columns[columnPlannedAmountEarn].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                form.Grid.Columns[columnAmountSpent].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                form.Grid.Columns[columnPlannedAmountSpend].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                form.Grid.Columns[columnDifference].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

                form.Grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                form.Grid.RowHeadersVisible = false;

                form.Grid.AllowUserToOrderColumns = false;

                form.Grid.Columns[columnDate].DividerWidth = 1;
                form.Grid.Columns[columnDifferenceIcon].DividerWidth = 1;
                form.Grid.Columns[columnTransaction].DividerWidth = 1;
                form.Grid.Columns[columnAmountSpent].DividerWidth = 1;
                form.Grid.Columns[columnPlannedAmountSpend].DividerWidth = 1;
                form.Grid.Columns[columnAccount].DividerWidth = 1;
                form.Grid.Columns[columnDifference].DividerWidth = 1;

                form.Grid.Columns[columnDate].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnStatus].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnType].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnTransaction].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnAmountEarned].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnAmountSpent].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnPlannedAmountSpend].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnPlannedAmountEarn].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnDifferenceIcon].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnAccount].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnDifference].SortMode = DataGridViewColumnSortMode.NotSortable;
                form.Grid.Columns[columnTags].SortMode = DataGridViewColumnSortMode.NotSortable;

                DateTime end = current.AddMonths(1);

                // getting default currency
                CultureInfo defaultCurrencyCulture = keeper.GetDefaultCurrency().CurrencyCultureInfo;

                double monthTotalEarned = 0;
                double monthTotalSpent = 0;
                double monthTotalPlannedEarn = 0;
                double monthTotalPlannedSpend = 0;
                double monthTotalDifference = 0;

                int lastRow = -1;

                while (current < end)
                {
                    lastRow = -1;

                    double dayTotalEarned = 0;
                    double dayTotalSpent = 0;
                    double dayTotalPlannedEarn = 0;
                    double dayTotalPlannedSpend = 0;
                    double dayTotalDifference = 0;

                    DateTime nextDay = current.AddDays(1);

                    // getting active plans for this day
                    foreach (MoneyDataSet.PlannedTransactionsRow plan in
                        keeper.GetActivePlannedTransactions(current, current, false).Select(s => (s.PlannedTransaction)))
                    {
                        Image differenceIcon = Properties.Resources.bullet_black;
                        String status = String.Empty;
                        String difference = String.Empty;
                        String plannedAmountEarn = String.Empty;
                        String plannedAmountSpend = String.Empty;
                        String actualAmountEarned = String.Empty;
                        String actualAmountSpent = String.Empty;

                        IEnumerable<MoneyDataSet.TransactionsRow> implementations = keeper.GetPlannedTransactionImplementations(plan, current);

                        double implementationEarned = 0;
                        double implementationSpent = 0;

                        foreach (MoneyDataSet.TransactionsRow transaction in implementations)
                        {
                            if (transaction.TransactionTypesRow.IsIncome)
                            {
                                implementationEarned += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                            }
                            else
                            {
                                implementationSpent += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                            }
                        }

                        if (implementationEarned != 0)
                        {
                            actualAmountEarned = implementationEarned.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                        }
                        if (implementationSpent != 0)
                        {
                            actualAmountSpent = implementationSpent.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                        }

                        double diff = 0;

                        if (plan.TransactionTypeRow.IsIncome)
                        {
                            dayTotalPlannedEarn += plan.Amount * plan.CurrenciesRow.ExchangeRate;

                            diff += Math.Abs(implementationEarned - (plan.Amount * plan.CurrenciesRow.ExchangeRate));

                            if (plan.Amount != 0)
                            {
                                plannedAmountEarn = plan.Amount.ToString(Consts.UI.CurrencyFormat,
                                    CultureInfo.CreateSpecificCulture(plan.CurrenciesRow.CurrencyCulture));
                            }
                        }
                        else
                        {
                            dayTotalPlannedSpend += plan.Amount * plan.CurrenciesRow.ExchangeRate;

                            diff += Math.Abs(implementationSpent - (plan.Amount * plan.CurrenciesRow.ExchangeRate));

                            if (plan.Amount != 0)
                            {
                                plannedAmountSpend = plan.Amount.ToString(Consts.UI.CurrencyFormat,
                                    plan.CurrenciesRow.CurrencyCultureInfo);
                            }
                        }

                        if (implementations.Where(t => (t.TransactionTime.Date.Equals(current))).Any())
                        {
                            dayTotalDifference += diff;
                            if (diff != 0)
                            {
                                difference = diff.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                            } if (diff > Math.Abs(plan.Amount * plan.CurrenciesRow.ExchangeRate * Consts.Reports.DifferenceErrorThreshold))
                            {
                                differenceIcon = Properties.Resources.exclamation;
                                status = Resources.Labels.SignificantDifferenceTitle;
                            }
                            else if (diff > Math.Abs(plan.Amount * plan.CurrenciesRow.ExchangeRate * Consts.Reports.DifferenceWarningThreshold))
                            {
                                differenceIcon = Properties.Resources.error;
                                status = Resources.Labels.DeviationFromPlanTitle;
                            }
                            else
                            {
                                differenceIcon = Properties.Resources.tick;
                                status = Resources.Labels.ImplementedTitle;
                            }
                        }
                        else if (implementations.Any())
                        {
                            dayTotalDifference += diff;
                            if (diff != 0)
                            {
                                difference = diff.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                            }

                            if (diff > Math.Abs(plan.Amount * plan.CurrenciesRow.ExchangeRate * Consts.Reports.DifferenceErrorThreshold))
                            {
                                differenceIcon = Properties.Resources.exclamation;
                                status = Resources.Labels.SignificantDifferenceTitle;
                            }
                            if (diff > Math.Abs(plan.Amount * plan.CurrenciesRow.ExchangeRate * Consts.Reports.DifferenceWarningThreshold))
                            {
                                differenceIcon = Properties.Resources.error;
                                if (plan.IsAggregated)
                                {
                                    status = Resources.Labels.DeviationFromPlanTitle;
                                }
                                else
                                {
                                    status = Resources.Labels.DeviationAnotherDayTitle;
                                }
                            }
                            else if (plan.IsAggregated)
                            {
                                status = Resources.Labels.ImplementedTitle;
                                differenceIcon = Properties.Resources.tick;
                            }
                            else
                            {
                                status = Resources.Labels.ImplementedOnAnotherDayTitle;
                                differenceIcon = Properties.Resources.date_go;
                            }
                        }
                        else
                        {
                            if (current < DateTime.Now)
                            {
                                status = Resources.Labels.NotImplementedTitle;
                                dayTotalDifference += Math.Abs(plan.Amount) * plan.CurrenciesRow.ExchangeRate;

                                difference = Math.Abs(plan.Amount).ToString(Consts.UI.CurrencyFormat,
                                    plan.CurrenciesRow.CurrencyCultureInfo);

                                differenceIcon = Properties.Resources.exclamation;
                            }
                            else
                            {
                                status = Resources.Labels.NotImplementedYetTitle;
                                differenceIcon = Properties.Resources.flag_blue;
                            }
                        }

                        int i = form.Grid.Rows.Add(current, status, differenceIcon, plan.TransactionTypeRow.Title, Properties.Resources.date, plan.Title,
                            actualAmountEarned, actualAmountSpent, plannedAmountEarn, plannedAmountSpend,
                            difference, plan.AccountTypeRow.Title, String.Join(Consts.UI.EnumerableSeparator,
                            plan.GetPlannedTransactionTagsRows().Select(tt => (tt.TagRow.Title))));
                        lastRow = i;

                        if (implementations.Any())
                        {
                            // this plan is implemented
                            if (diff > Math.Abs(plan.Amount * plan.CurrenciesRow.ExchangeRate * Consts.Reports.DifferenceErrorThreshold))
                            {
                                form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.Coral;
                            }
                            else if (diff > Math.Abs(plan.Amount * plan.CurrenciesRow.ExchangeRate * Consts.Reports.DifferenceWarningThreshold))
                            {
                                form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.Moccasin;
                            }
                            else
                            {
                                form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                            }
                        }
                        else if (current < DateTime.Now)
                        {
                            form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.LightSalmon;
                        }
                        else
                        {
                            form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.PowderBlue;
                        }
                    }

                    // getting transactions for this day
                    foreach (MoneyDataSet.TransactionsRow transaction in keeper.Transactions.Where(t =>
                        (t.TransactionTime >= current) && (t.TransactionTime < nextDay)).OrderBy(o => (o.TransactionTime)))
                    {
                        String status = String.Empty;
                        String planAmountSpend = String.Empty;
                        String planAmountEarn = String.Empty;
                        String difference = String.Empty;
                        bool isPlanned = false;

                        Image differenceIcon = Properties.Resources.bullet_black;

                        if (transaction.IsPlannedTransactionIDNull())
                        {
                            differenceIcon = Properties.Resources.error;
                            status = Resources.Labels.NotPlannedTitle;
                            isPlanned = false;

                            if (((!transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferIn)) &&
                                (!transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferOut))) ||
                                (transaction.IsPairReferenceIDNull()) || (transaction.PairReferenceID == 0))
                            {
                                dayTotalDifference += Math.Abs(transaction.Amount) * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                                difference = Math.Abs(transaction.Amount).ToString(Consts.UI.CurrencyFormat,
                                    transaction.AccountRow.CurrenciesRow.CurrencyCultureInfo);
                            }
                        }
                        else
                        {
                            status = Resources.Labels.ImplementsTitle;
                            differenceIcon = Properties.Resources.tick;
                            isPlanned = true;
                        }

                        String amountSpent = String.Empty;
                        String amountEarned = String.Empty;

                        if (transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.Correction))
                        {
                            if (transaction.Amount > 0)
                            {
                                dayTotalSpent += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                                amountSpent = transaction.Amount.ToString(Consts.UI.CurrencyFormat,
                                    transaction.AccountRow.CurrenciesRow.CurrencyCultureInfo);
                            }
                            else
                            {
                                dayTotalEarned += Math.Abs(transaction.Amount) * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                                amountEarned = Math.Abs(transaction.Amount).ToString(Consts.UI.CurrencyFormat,
                                    transaction.AccountRow.CurrenciesRow.CurrencyCultureInfo);
                            }
                        }
                        else if (transaction.TransactionTypesRow.IsIncome)
                        {
                            if (((!transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferIn)) &&
                               (!transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferOut))) ||
                               (transaction.IsPairReferenceIDNull()) || (transaction.PairReferenceID == 0))
                            {
                                dayTotalEarned += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                            }
                            amountEarned = transaction.Amount.ToString(Consts.UI.CurrencyFormat,
                                transaction.AccountRow.CurrenciesRow.CurrencyCultureInfo);
                        }
                        else
                        {
                            if (((!transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferIn)) &&
                               (!transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferOut))) ||
                               (transaction.IsPairReferenceIDNull()) || (transaction.PairReferenceID == 0))
                            {
                                dayTotalSpent += transaction.Amount * transaction.AccountRow.CurrenciesRow.ExchangeRate;
                            }
                            amountSpent = transaction.Amount.ToString(Consts.UI.CurrencyFormat,
                                transaction.AccountRow.CurrenciesRow.CurrencyCultureInfo);
                        }

                        Image typeIcon = null;

                        if (transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.Correction))
                        {
                            typeIcon = Properties.Resources.error;
                        }
                        else if (transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferIn))
                        {
                            typeIcon = Properties.Resources.arrow_in;
                        }
                        else if (transaction.TypeID.Equals(MoneyDataSet.IDs.TransactionTypes.TransferOut))
                        {
                            typeIcon = Properties.Resources.arrow_out;
                        }
                        else
                        {
                            typeIcon = Properties.Resources.application_form;
                        }

                        int i = form.Grid.Rows.Add(current, status, differenceIcon, transaction.TransactionTypesRow.Title, typeIcon,
                            transaction.Title, amountEarned, amountSpent, planAmountEarn, planAmountSpend,
                            difference, transaction.AccountRow.FullTitle,
                            String.Join(Consts.UI.EnumerableSeparator, transaction.GetTransactionTagsRows().Select(tt => (tt.TagRow.Title))));

                        lastRow = i;
                        if (isPlanned)
                        {
                            form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
                        }
                        else
                        {
                            form.Grid.Rows[i].DefaultCellStyle.BackColor = Color.Wheat;
                        }
                    }

                    if (lastRow >= 0)
                    {
                        monthTotalDifference += dayTotalDifference;
                        monthTotalEarned += dayTotalEarned;
                        monthTotalPlannedEarn += dayTotalPlannedEarn;
                        monthTotalPlannedSpend += dayTotalPlannedSpend;
                        monthTotalSpent += dayTotalSpent;

                        String DTE = String.Empty;
                        String DTS = String.Empty;
                        String DTPE = String.Empty;
                        String DTPS = String.Empty;
                        String DTD = String.Empty;

                        if ((dayTotalEarned != 0) || (dayTotalSpent != 0))
                        {
                            DTE = dayTotalEarned.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                            DTS = dayTotalSpent.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                        }

                        if ((dayTotalPlannedEarn != 0) || (dayTotalPlannedSpend != 0))
                        {
                            DTPE = dayTotalPlannedEarn.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                            DTPS = dayTotalPlannedSpend.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                        }

                        if (dayTotalDifference != 0)
                        {
                            DTD = dayTotalDifference.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture);
                        }

                        Image dayIcon;
                        if ((dayTotalDifference > (dayTotalEarned * Consts.Reports.DifferenceErrorThreshold)) &&
                            (dayTotalDifference > (dayTotalSpent * Consts.Reports.DifferenceErrorThreshold)))
                        {
                            dayIcon = Properties.Resources.exclamation;
                        }
                        else if ((dayTotalDifference > (dayTotalEarned * Consts.Reports.DifferenceErrorThreshold)) &&
                                (dayTotalDifference > (dayTotalSpent * Consts.Reports.DifferenceErrorThreshold)))
                        {
                            dayIcon = Properties.Resources.error;
                        }
                        else
                        {
                            dayIcon = Properties.Resources.tick;
                        }

                        lastRow = form.Grid.Rows.Add(current, String.Empty, dayIcon, String.Empty, Properties.Resources.calculator,
                            Resources.Labels.DayTotalsTitle, DTE, DTS, DTPE, DTPS, DTD, String.Empty, String.Empty);

                        form.Grid.Rows[lastRow].DefaultCellStyle.Font = new Font(form.Grid.DefaultCellStyle.Font, FontStyle.Bold);
                        form.Grid.Rows[lastRow].DividerHeight = 3;
                    }
                    current = nextDay;
                }
                if (lastRow >= 0)
                {
                    form.Grid.Rows[lastRow].DividerHeight = 6;
                }

                Image monthIcon;
                if ((monthTotalDifference > (monthTotalEarned * Consts.Reports.DifferenceErrorThreshold)) &&
                    (monthTotalDifference > (monthTotalSpent * Consts.Reports.DifferenceErrorThreshold)))
                {
                    monthIcon = Properties.Resources.exclamation;
                }
                else if ((monthTotalDifference > (monthTotalEarned * Consts.Reports.DifferenceErrorThreshold)) &&
                        (monthTotalDifference > (monthTotalSpent * Consts.Reports.DifferenceErrorThreshold)))
                {
                    monthIcon = Properties.Resources.error;
                }
                else
                {
                    monthIcon = new Bitmap(1, 1);
                }
                int j = form.Grid.Rows.Add(null, String.Empty, monthIcon, String.Empty, Properties.Resources.calculator, Resources.Labels.MonthTotalsTitle,
                    monthTotalEarned.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture),
                    monthTotalSpent.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture),
                    monthTotalPlannedEarn.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture),
                    monthTotalPlannedSpend.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture),
                    monthTotalDifference.ToString(Consts.UI.CurrencyFormat, defaultCurrencyCulture),
                    String.Empty, String.Empty);

                form.Grid.Rows[j].DefaultCellStyle.BackColor = Color.LightGray;
                form.Grid.Rows[j].DefaultCellStyle.Font = new Font(form.Grid.DefaultCellStyle.Font.FontFamily, form.Grid.DefaultCellStyle.Font.Size + 2, FontStyle.Bold);

                form.Grid.Columns[columnDate].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnStatus].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnType].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnTypeIcon].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnTransaction].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnAmountSpent].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnAmountEarned].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnPlannedAmountSpend].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnPlannedAmountEarn].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnAccount].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnDifferenceIcon].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                form.Grid.Columns[columnDifference].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                form.Grid.Refresh();
                form.WindowState = FormWindowState.Maximized;

                return form;
            }
            catch (Exception e)
            {
                Log.Write(e);
            }
            return null;
        }
Ejemplo n.º 3
0
        public static Form TransactionsByAccountReport(Control parameters)
        {
            const String columnDateTime = "datetime";
            const String columnType = "type";
            const String columnTitle = "title";
            const String columnAmount = "amount";
            const String columnAccount = "account";
            const String columnTags = "tags";

            try
            {
                DateAccountsTypesTags pars = parameters as DateAccountsTypesTags;

                DateTime StartDate = DateTime.Now;
                DateTime EndDate = DateTime.Now;
                IEnumerable<MoneyDataSet.AccountsRow> Accounts = null;
                IEnumerable<MoneyDataSet.TransactionTypesRow> TransactionTypes = null;
                IEnumerable<String> Tags = null;

                if (pars != null)
                {
                    MethodInvoker inv = delegate()
                    {
                        StartDate = pars.dtpStartDate.Value;
                        EndDate = pars.dtpEndDate.Value;
                        Accounts = pars.lbAccounts.SelectedItems.Cast<MoneyDataSet.AccountsRow>().ToArray();
                        TransactionTypes = pars.lbTransactionTypes.SelectedItems.Cast<MoneyDataSet.TransactionTypesRow>().ToArray();
                        Tags = pars.lbTags.SelectedItems.Cast<String>().ToArray();
                    };
                    pars.Invoke(inv);

                    String accounts = String.Join(Consts.UI.EnumerableSeparator, Accounts.Select(a => (a.Title)));
                    ReportGridForm form = new ReportGridForm();

                    form.Text = String.Format(Resources.Labels.TransactionsByAccountFormTitleFormat, accounts,
                        StartDate.ToShortDateString(), EndDate.ToShortDateString());

                    form.Grid.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;

                    form.Grid.Columns.Add(columnDateTime, Resources.Labels.DateTimeTitle);
                    form.Grid.Columns.Add(columnType, Resources.Labels.TypeTitle);
                    form.Grid.Columns.Add(columnTitle, Resources.Labels.Title);
                    form.Grid.Columns.Add(columnAmount, Resources.Labels.AmountTitle);
                    form.Grid.Columns.Add(columnAccount, Resources.Labels.AccountTitle);
                    form.Grid.Columns.Add(columnTags, Resources.Labels.TagsTitle);

                    form.Grid.Columns[columnDateTime].CellTemplate.Style.Format = Consts.UI.DateTimeFormat;
                    form.Grid.Columns[columnAmount].CellTemplate.Style.Format = Consts.UI.NumericFormat;
                    form.Grid.Columns[columnTags].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                    form.Grid.Columns[columnDateTime].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                    form.Grid.Columns[columnAmount].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

                    form.Grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    form.Grid.RowHeadersVisible = false;

                    form.Grid.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke;

                    IEnumerable<MoneyDataSet.TransactionsRow> transactions = MoneyDataKeeper.Instance.Transactions.Where(t =>
                        ((Accounts.Contains(t.AccountRow)) &&
                        (TransactionTypes.Contains(t.TransactionTypesRow)) &&
                        ((!t.GetTransactionTagsRows().Any()) ||
                        (t.GetTransactionTagsRows().Select(tt => (tt.TagRow.Title)).Intersect(Tags).Any()))
                        ));

                    foreach (MoneyDataSet.TransactionsRow transaction in transactions)
                    {
                        String accountInfo = String.Format(Consts.UI.CurrencyWithAccountFormat, transaction.AccountRow.CurrenciesRow.Title, transaction.AccountRow.FullTitle);
                        form.Grid.Rows.Add(transaction.TransactionTime, transaction.TransactionTypesRow.Title, transaction.Title,
                            transaction.Amount, accountInfo, String.Join(Consts.UI.EnumerableSeparator, transaction.GetTransactionTagsRows().Select(tt => (tt.TagRow.Title))));
                    }

                    form.Grid.Columns[columnDateTime].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnType].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnTitle].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnAmount].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    form.Grid.Columns[columnAccount].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                    form.Grid.Sort(form.Grid.Columns[columnDateTime], ListSortDirection.Ascending);

                    form.Grid.Refresh();

                    return form;
                }
            }
            catch (Exception e)
            {
                Log.Write(e);
            }
            return null;
        }