Skip to content

Commit ea693f9

Browse files
Fix booking import view to hide inactive accounts (#363)
Co-authored-by: Lukas Grützmacher <44983012+lg2de@users.noreply.github.com>
1 parent 1a03a84 commit ea693f9

3 files changed

Lines changed: 82 additions & 10 deletions

File tree

src/SimpleAccounting/Model/AccountingData.Extensions.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ internal AccountingDataJournal CloseYear(
117117
ulong bookingId = 1;
118118

119119
// Asset Accounts (Bestandskonten), Credit and Debit Accounts
120-
var accounts = this.AllAccounts.Where(
121-
a =>
122-
a.Type == AccountDefinitionType.Asset
123-
|| a.Type == AccountDefinitionType.Credit
124-
|| a.Type == AccountDefinitionType.Debit);
120+
var accounts = this.AllAccounts.Where(a =>
121+
a.Type == AccountDefinitionType.Asset
122+
|| a.Type == AccountDefinitionType.Credit
123+
|| a.Type == AccountDefinitionType.Debit);
125124
foreach (var account in accounts)
126125
{
127126
if (currentModelJournal.Booking == null)
@@ -289,6 +288,11 @@ public partial class AccountDefinitionImportMapping
289288
/// </summary>
290289
public bool IsValid()
291290
{
291+
if (this.Columns == null)
292+
{
293+
return false;
294+
}
295+
292296
return
293297
this.Columns.Exists(x => x.Target == AccountDefinitionImportMappingColumnTarget.Date)
294298
&& this.Columns.Exists(x => x.Target == AccountDefinitionImportMappingColumnTarget.Value);

src/SimpleAccounting/Presentation/ImportBookingsViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ImportBookingsViewModel(
4141
this.dialogs = dialogs;
4242
this.fileSystem = fileSystem;
4343
this.ProjectData = projectData;
44-
this.accounts = projectData.Storage.AllAccounts.ToList();
44+
this.accounts = projectData.Storage.AllAccounts.Where(a => a.Active).ToList();
4545
this.FirstBookingNumber = projectData.MaxBookIdent + 1;
4646

4747
this.RangeMin = this.ProjectData.CurrentYear.DateStart.ToDateTime();
@@ -57,7 +57,6 @@ public ImportBookingsViewModel(
5757
/// </summary>
5858
public IEnumerable<AccountDefinition> ImportAccounts => this.accounts
5959
.Where(a => a.ImportMapping != null && a.ImportMapping.IsValid());
60-
6160
/// <summary>
6261
/// Gets a value indicating whether importing is currently possible.
6362
/// </summary>
@@ -221,7 +220,7 @@ ImportEntryViewModel ToViewModel(AccountingDataJournalBooking entry)
221220
// remote account may be null in case of split booking
222221
var remoteAccount = this.accounts.Find(x => x.ID == remoteIdentifier);
223222

224-
return new ImportEntryViewModel(this.accounts)
223+
return new ImportEntryViewModel(this.accounts.Where(x => x.ID != this.SelectedAccountNumber))
225224
{
226225
IsExisting = true,
227226
Identifier = entry.ID,

tests/SimpleAccounting.UnitTests/Presentation/ImportBookingsViewModelTests.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace lg2de.SimpleAccounting.UnitTests.Presentation;
77
using System;
88
using System.Globalization;
99
using System.Linq;
10+
using System.Text;
1011
using System.Threading.Tasks;
1112
using System.Windows.Forms;
1213
using lg2de.SimpleAccounting.Abstractions;
@@ -32,6 +33,32 @@ public void Ctor_SampleData_AccountsFiltered()
3233
sut.ImportAccounts.Should().BeEquivalentTo([new { Name = "Bank account" }]);
3334
}
3435

36+
[Fact]
37+
public void Ctor_InactiveAccountWithMapping_Excluded()
38+
{
39+
var clock = Substitute.For<IClock>();
40+
var projectData = new ProjectData(new Settings(), null!, null!, null!, clock, null!);
41+
var activeAccount = new AccountDefinition
42+
{
43+
ID = 101, Name = "Bank (Active)", Active = true, ImportMapping = Samples.SimpleImportConfiguration
44+
};
45+
var inactiveAccount = new AccountDefinition
46+
{
47+
ID = 102,
48+
Name = "Old Bank (Inactive)",
49+
Active = false,
50+
ImportMapping = Samples.SimpleImportConfiguration
51+
};
52+
projectData.Storage.Accounts =
53+
[
54+
new AccountingDataAccountGroup { Account = [activeAccount, inactiveAccount] }
55+
];
56+
57+
var sut = new ImportBookingsViewModel(null!, null!, projectData);
58+
59+
sut.ImportAccounts.Should().BeEquivalentTo([new { Name = "Bank (Active)" }]);
60+
}
61+
3562
[Fact]
3663
public void ImportStatus_NoImportAccount()
3764
{
@@ -194,6 +221,49 @@ public void LoadDataCommand_LastImportFolder_LastUsedAndNewStored()
194221
dialogs.Received(1).ShowOpenFileDialog(Arg.Any<string>(), Arg.Is<string>(x => x == "E:\\MySelectedFolder"));
195222
}
196223

224+
[CulturedFact(["de"])]
225+
public void LoadDataCommand_RemoteAccountList_ContainsOnlyActiveAccounts()
226+
{
227+
var clock = Substitute.For<IClock>();
228+
var projectData = new ProjectData(new Settings(), null!, null!, null!, clock, null!);
229+
var bankAccount = new AccountDefinition
230+
{
231+
ID = 100, Name = "My Bank", Active = true, ImportMapping = Samples.SimpleImportConfiguration
232+
};
233+
var activeRemote = new AccountDefinition
234+
{
235+
ID = 601, Name = "New Shoes (Active)", Active = true, ImportMapping = Samples.SimpleImportConfiguration
236+
};
237+
var inactiveRemote = new AccountDefinition
238+
{
239+
ID = 600,
240+
Name = "Old Shoes (Inactive)",
241+
Active = false,
242+
ImportMapping = Samples.SimpleImportConfiguration
243+
};
244+
projectData.Storage.Accounts =
245+
[
246+
new AccountingDataAccountGroup { Account = [bankAccount, activeRemote, inactiveRemote] }
247+
];
248+
var dialogs = Substitute.For<IDialogs>();
249+
dialogs.ShowOpenFileDialog(Arg.Any<string>(), Arg.Any<string>())
250+
.Returns((DialogResult.OK, "test.csv"));
251+
var fileSystem = Substitute.For<IFileSystem>();
252+
var sut = new ImportBookingsViewModel(dialogs, fileSystem, projectData)
253+
{
254+
SelectedAccount = bankAccount, SelectedAccountNumber = bankAccount.ID
255+
};
256+
var validDate = sut.RangeMin.AddDays(5).ToString("dd.MM.yyyy", CultureInfo.CurrentUICulture);
257+
var csvContent = $"Datum;Text;Betrag\n{validDate};Test Booking;-100,00";
258+
fileSystem.ReadAllBytesFromFile(Arg.Any<string>())
259+
.Returns(Encoding.UTF8.GetBytes(csvContent));
260+
261+
sut.LoadDataCommand.Execute(null);
262+
263+
sut.LoadedData.Should().ContainSingle()
264+
.Which.Accounts.Should().BeEquivalentTo([new { ID = 601, Name = "New Shoes (Active)" }]);
265+
}
266+
197267
[Fact]
198268
public void SetRemoteAccountsCommand_EntryNotMapped_CanExecute()
199269
{
@@ -264,8 +334,7 @@ public void SetRemoteAccountsCommand_EntryNotMapped_AccountsSelected()
264334
.BeEquivalentTo(
265335
new object[]
266336
{
267-
new { RemoteAccount = new { Name = "Bank account" } },
268-
new { RemoteAccount = (object)null },
337+
new { RemoteAccount = new { Name = "Bank account" } }, new { RemoteAccount = (object)null },
269338
new { RemoteAccount = new { Name = "Shoes" } },
270339
new { RemoteAccount = new { Name = "Friends debit" } }
271340
});

0 commit comments

Comments
 (0)