reading account journal complete

This commit is contained in:
Bobbie Hodgetts
2024-05-12 15:10:24 +01:00
parent fb058fc22f
commit 0fb45b8090
12 changed files with 626 additions and 16 deletions

View File

@@ -0,0 +1,208 @@
using FikaAmazonAPI.AmazonSpApiSDK.Models.CatalogItems;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using System.Threading.Tasks;
using static System.ComponentModel.Design.ObjectSelectorEditor;
namespace bnhtrade.Core.Data.Database.Account
{
internal class Journal : Connection
{
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
/// <summary>
/// Results filter
/// </summary>
public List<uint> AccountJournalId { get; set; }
public Journal()
{
Init();
}
public void Init()
{
sqlBuilder = new SqlWhereBuilder();
AccountJournalId = new List<uint>();
}
/// <summary>
///
/// </summary>
/// <returns>Dictionary were key is the table primary key</returns>
public Dictionary<uint, Core.Model.Account.Journal> Read()
{
sqlBuilder.Init();
//build sql query
string sql = @"
SELECT tblAccountJournal.AccountJournalID
,tblAccountJournal.AccountJournalTypeID
,tblAccountJournal.EntryDate
,tblAccountJournal.PostDate
,tblAccountJournal.LastModified
,tblAccountJournal.IsLocked
,tblAccountJournalPost.AccountJournalPostID
,tblAccountJournalPost.AccountChartOfID
,tblAccountJournalPost.AmountGbp
FROM tblAccountJournal
INNER JOIN tblAccountJournalPost ON tblAccountJournal.AccountJournalID = tblAccountJournalPost.AccountJournalID
WHERE 1 = 1 ";
// build the where statments
if (AccountJournalId.Any())
{
sqlBuilder.In("tblAccountJournal.AccountJournalID", AccountJournalId, "AND");
}
// append where string to the sql
if (sqlBuilder.IsSetSqlWhereString)
{
sql = sql + sqlBuilder.SqlWhereString;
}
// build tuple list
var dbJournalList = new List<(
uint AccountJournalId
, uint AccountJournalTypeId
, DateTime EntryDate
, DateTime PostDate
, DateTime LastModified
, bool IsLocked
)>();
var dbJournalPostList = new List<(
uint AccountJournalId
, uint AccountJournalPostId
, uint AccountChartOfId
, decimal AmountGbp
)>();
bool hasRows = false;
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
sqlBuilder.AddParametersToSqlCommand(cmd);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
hasRows = true;
uint lastJournalId = 0;
while (reader.Read())
{
// read journal header
uint journalId = (uint)reader.GetInt32(0);
if (journalId != lastJournalId)
{
lastJournalId = journalId;
(uint AccountJournalId
, uint AccountJournalTypeId
, DateTime EntryDate
, DateTime PostDate
, DateTime LastModified
, bool IsLocked
)
journal =
( journalId
, (uint)reader.GetInt32(1)
, DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc)
, DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc)
, DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc)
, reader.GetBoolean(5)
);
dbJournalList.Add(journal);
}
// read journal posts
(uint AccountJournalId
, uint AccountJournalPostId
, uint AccountChartOfId
, decimal AmountGbp
)
journalPost =
( journalId
, (uint)reader.GetInt32(6)
, (uint)reader.GetInt32(7)
, reader.GetDecimal(8)
);
dbJournalPostList.Add(journalPost);
}
}
}
}
}
var returnList = new Dictionary<uint, Core.Model.Account.Journal>();
if (hasRows)
{
// build lists to filter db results by
var journalTypeIdList = new List<uint>();
var accountIdList = new List<uint>();
foreach (var item in dbJournalList)
{
journalTypeIdList.Add(item.AccountJournalTypeId);
}
foreach (var item in dbJournalPostList)
{
accountIdList.Add(item.AccountChartOfId);
}
// get journalTypes from db
var dbJournalType = new Data.Database.Account.JournalType();
dbJournalType.IdList = journalTypeIdList;
var journalTypeDict = dbJournalType.Read();
// get accounts from db
var dbAccount = new Data.Database.Account.ReadAccountCode();
var accountDict = dbAccount.ByAccountId(accountIdList);
// build final return dictionary
foreach (var dbJournal in dbJournalList)
{
// build posts
var newPosts = new List<Core.Model.Account.Journal.Post>();
foreach (var dbJournalPost in dbJournalPostList)
{
if (dbJournalPost.AccountJournalId == dbJournal.AccountJournalId)
{
var newPost = new Core.Model.Account.Journal.Post(
dbJournalPost.AccountJournalPostId
, accountDict[dbJournalPost.AccountChartOfId]
, dbJournalPost.AmountGbp);
newPosts.Add(newPost);
}
}
// create the journal
var newJournal = new Core.Model.Account.Journal(
dbJournal.AccountJournalId
, journalTypeDict[dbJournal.AccountJournalTypeId]
, newPosts
, dbJournal.EntryDate
, dbJournal.PostDate
, dbJournal.LastModified
, dbJournal.IsLocked);
returnList.Add(dbJournal.AccountJournalId, newJournal);
}
}
// all done, return the list herevar
return returnList;
}
}
}

View File

@@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Data.Database.Account
{
internal class JournalType : Connection
{
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
/// <summary>
/// Results filter
/// </summary>
public List<uint> IdList { get; set; }
/// <summary>
/// Searches for the specificed phases within the item description. Uses the LIKE AND sql function
/// </summary>
public List<string> TitleList { get; set; }
public JournalType()
{
Init();
}
public void Init()
{
sqlBuilder = new SqlWhereBuilder();
IdList = new List<uint>();
TitleList = new List<string>();
}
/// <summary>
///
/// </summary>
/// <returns>Dictionary where key is the table primary key</returns>
public Dictionary<uint, Model.Account.JournalType> Read()
{
// create the return (emptyP list) here
var returnList = new Dictionary<uint, Model.Account.JournalType>();
sqlBuilder.Init();
//build sql query
string sql = @"
SELECT [AccountJournalTypeID]
,[TypeTitle]
,[ChartOfAccountID_Debit]
,[ChartOfAccountID_Credit]
FROM [e2A].[dbo].[tblAccountJournalType]
WHERE 1 = 1 ";
// build the where statments
if (IdList.Any())
{
sqlBuilder.In("AccountJournalTypeID", IdList, "AND");
}
if (TitleList.Any())
{
sqlBuilder.In("TypeTitle", TitleList, "AND");
}
// append where string to the sql
if (sqlBuilder.IsSetSqlWhereString)
{
sql = sql + sqlBuilder.SqlWhereString;
}
// create dictionary to add credit/debit accounts on after db read
var creditDict = new Dictionary<uint, uint>();
var debitDict = new Dictionary<uint, uint>();
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
sqlBuilder.AddParametersToSqlCommand(cmd);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
// read from db and create object
uint journalTypeId = (uint)reader.GetInt32(0);
string title = reader.GetString(1);
int? debitAccountId = null;
if (!reader.IsDBNull(2)) { debitAccountId = reader.GetInt32(2); }
int? creditAccountId = null;
if (!reader.IsDBNull(3)) { creditAccountId = reader.GetInt32(3); }
// build return list
var item = new Model.Account.JournalType(journalTypeId, title);
returnList.Add(journalTypeId, item);
// build dictionaries
if (debitAccountId != null)
{
debitDict.Add(journalTypeId, (uint)debitAccountId);
}
if (creditAccountId != null)
{
creditDict.Add(journalTypeId, (uint)creditAccountId);
}
}
}
}
}
}
// get account objects from db
var accountIdList = debitDict.Values.ToList();
accountIdList.AddRange(creditDict.Values.ToList());
var dbaccount = new Data.Database.Account.ReadAccountCode();
var dbDict = dbaccount.ByAccountId(accountIdList);
// add to the returnlist
foreach (var account in returnList.Values)
{
Model.Account.Account debitAccount = null;
if (debitDict.ContainsKey(account.JournalTypeId))
{
debitAccount = dbDict[debitDict[account.JournalTypeId]];
}
Model.Account.Account creditAccount = null;
if (creditDict.ContainsKey(account.JournalTypeId))
{
creditAccount = dbDict[creditDict[account.JournalTypeId]]; // key of 59 needed
}
account.AddDefaultAccounts(creditAccount, debitAccount);
}
// all done, return the list here
return returnList;
}
}
}

View File

@@ -10,7 +10,6 @@ namespace bnhtrade.Core.Data.Database.Account
public class ReadAccountCode : Connection
{
private Data.Database.SqlWhereBuilder sqlWhere = new SqlWhereBuilder();
private List<Model.Account.Account> resultList;
public ReadAccountCode()
{
@@ -27,26 +26,45 @@ namespace bnhtrade.Core.Data.Database.Account
var dictionary = new Dictionary<uint, Model.Account.Account>();
foreach (var item in list)
{
dictionary.Add(item.Id, item);
dictionary.Add(item.Value.Id, item.Value);
}
return dictionary;
}
public List<Model.Account.Account> ByAccountCode(List<int> accountCodeList)
public Dictionary<uint, Model.Account.Account> ByAccountId(List<uint> accountIdList)
{
Innit();
var resultDict = new Dictionary<uint, Model.Account.Account>();
if (accountIdList == null || !accountIdList.Any())
{
return resultDict;
}
sqlWhere.In("tblAccountChartOf.AccountChartOfID", accountIdList, " WHERE ");
resultDict = Execute(sqlWhere.SqlWhereString, sqlWhere.ParameterList);
return resultDict;
}
public Dictionary<uint, Model.Account.Account> ByAccountCode(List<int> accountCodeList)
{
Innit();
var resultDict = new Dictionary<uint, Model.Account.Account>();
if (accountCodeList == null || !accountCodeList.Any())
{
return resultList;
return resultDict;
}
sqlWhere.In("tblAccountChartOf.AccountCode", accountCodeList, " WHERE ");
return Execute(sqlWhere.SqlWhereString, sqlWhere.ParameterList);
resultDict = Execute(sqlWhere.SqlWhereString, sqlWhere.ParameterList);
return resultDict;
}
private List<Model.Account.Account> Execute(string sqlWhere, Dictionary<string, object> parameters)
private Dictionary<uint, Model.Account.Account> Execute(string sqlWhere, Dictionary<string, object> parameters)
{
var resultDict = new Dictionary<uint, Model.Account.Account>();
//build sql query
string sqlString = @"
SELECT tblAccountChartOf.AccountChartOfID
@@ -91,18 +109,18 @@ namespace bnhtrade.Core.Data.Database.Account
int multiplier = reader.GetInt32(6);
var result = new Model.Account.Account(tablePk, accountCode, title, description, type, basicType, multiplier);
resultList.Add(result);
resultDict.Add(tablePk, result);
}
}
return resultList;
}
}
}
return resultDict;
}
private void Innit()
{
resultList = new List<Model.Account.Account>();
sqlWhere.Init();
}
}
}

View File

@@ -209,5 +209,26 @@ namespace bnhtrade.Core.Data.Database
In(columnReference, stringList, wherePrefix);
}
/// <summary>
/// Append an 'In' statement and parameter list to the class properties
/// </summary>
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
/// <param name="orValueList">List of values to test in condition statement</param>
/// <param name="wherePrefix">Optional prefix that gets added to the sql string result</param>
public void In(string columnReference, List<uint> orValueList, string wherePrefix = null)
{
var stringList = new List<string>();
if (orValueList != null || !orValueList.Any())
{
foreach (uint value in orValueList)
{
stringList.Add(value.ToString());
}
}
In(columnReference, stringList, wherePrefix);
}
}
}

View File

@@ -35,7 +35,7 @@ namespace bnhtrade.Core.Logic.Account
public List<Model.Account.Account> ByAccountCode(List<int> accountCodeList)
{
return readAccountCode.ByAccountCode(accountCodeList);
return readAccountCode.ByAccountCode(accountCodeList).Values.ToList();
}
public Dictionary<int, Model.Account.Account> ConvertToDictionary(List<Model.Account.Account> accountCodeList)

View File

@@ -0,0 +1,65 @@
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Model.Account
{
public class Journal : IValidatableObject
{
internal Journal(uint journalId, Core.Model.Account.JournalType type, List<Post> posts, DateTime entryDate, DateTime postDate, DateTime lastModifed, bool isLocked)
{
JournalId = journalId;
Type = type;
Posts = posts;
EntryDate = entryDate;
PostDate = postDate;
LastModified = lastModifed;
IsLocked = isLocked;
}
public uint JournalId { get; private set; }
public Core.Model.Account.JournalType Type { get; private set; }
public List<Post> Posts { get; private set; } = new List<Post>();
public DateTime EntryDate { get; private set; }
public DateTime PostDate { get; private set; }
public DateTime LastModified { get;private set; }
public bool IsLocked { get; private set; }
public class Post
{
internal Post(uint postId, Core.Model.Account.Account account, decimal amountGbp)
{
PostId = postId;
Account = account;
AmountGbp = amountGbp;
}
public uint PostId { get; private set; }
public Core.Model.Account.Account Account { get; private set; }
public decimal AmountGbp { get; private set; }
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var result = new List<ValidationResult>();
// get total of posts
decimal postTotal = 0;
foreach (var post in Posts)
{
postTotal = postTotal + post.AmountGbp;
}
if (postTotal != 0)
{
result.Add(new ValidationResult("Account journal posts do not equal zero"));
}
throw new NotImplementedException();
return result;
}
}
}

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Model.Account
{
public class JournalType
{
internal JournalType(uint journalTypeId, string title, Model.Account.Account defaultCreditAccount = null, Model.Account.Account defaultDebitAccount = null)
{
JournalTypeId = journalTypeId;
Title = title;
DefaultCreditAccount = defaultCreditAccount;
DefaultDebitAccount = defaultDebitAccount;
}
internal void AddDefaultAccounts(Model.Account.Account defaultCreditAccount = null, Model.Account.Account defaultDebitAccount = null)
{
DefaultCreditAccount = defaultCreditAccount;
DefaultDebitAccount = defaultDebitAccount;
}
public uint JournalTypeId { get ; private set; }
public string Title { get; private set; }
public Model.Account.Account DefaultDebitAccount { get; private set; }
public bool IsSetDefaultDebitAccount
{
get { return DefaultDebitAccount != null; }
}
public Model.Account.Account DefaultCreditAccount { get; private set; }
public bool IsSetDefaultCreditAccount
{
get { return DefaultCreditAccount != null; }
}
}
}

View File

@@ -233,6 +233,7 @@ namespace bnhtrade.Core
{
public class AccountQuery
{
// externally called & internal
public static decimal CurrencyConvertToGbp(string sqlConnectionString, string currencyCode, decimal amount, DateTime conversionDate)
{
if (currencyCode == "GBP" || amount == 0M)
@@ -288,6 +289,7 @@ namespace bnhtrade.Core
}
}
// externally called
public static int CurrencyExchangeRateInsert(string sqlConnectionString, int exchangeRateSource, string currencyCode,
decimal currencyUnitsPerGbp, DateTime periodStart, DateTime periodEnd, bool checkOverride = false)
{
@@ -420,6 +422,7 @@ namespace bnhtrade.Core
}
}
// internally called
public static bool AccountJournalEntryIsLocked(string sqlConnectionString, int journalId)
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
@@ -449,6 +452,7 @@ namespace bnhtrade.Core
}
}
// externally called
public static int AccountJournalInsert(string sqlConnectionString, int journalTypeId, DateTime entryDate, string currencyCode,
decimal amount, int debitAccountId = 0, int creditAccountId = 0, bool lockEntry = false)
{
@@ -492,6 +496,7 @@ namespace bnhtrade.Core
}
}
// externally called
// return false on locked journal entry
public static bool AccountJournalDelete(string sqlConnectionString, int accountJournalId)
{
@@ -540,6 +545,7 @@ namespace bnhtrade.Core
}
}
// internally called
private static bool AccountJournalPostInsert(string sqlConnectionString, int journalId, DateTime entryDate,
string currencyCode, decimal amount, int debitAccountId = 0, int creditAccountId = 0)
{
@@ -685,6 +691,7 @@ namespace bnhtrade.Core
}
}
// externally called
public static bool AccountJournalPostUpdate(string sqlConnectionString, int journalId, string currencyCode, decimal amount,
int debitAccountId = 0, int creditAccountId = 0)
{

View File

@@ -11,7 +11,7 @@ namespace bnhtrade.Core.Test.Account
{
public Account()
{
PurchaseInvoiceLine();
Journal();
}
public void PurchaseInvoice()
@@ -27,5 +27,12 @@ namespace bnhtrade.Core.Test.Account
read.ItemDescription = new List<string> { "xbox", "kill" };
var result = read.Read();
}
public void Journal()
{
var read = new Data.Database.Account.Journal();
read.AccountJournalId = new List<uint> { 123, 300, 324, 5678, 22 };
var result = read.Read();
}
}
}

View File

@@ -0,0 +1,94 @@
using bnhtrade.Core.Data.Database;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Test._BoilerPlate
{
internal class xxxxxxxYourClassNameHerexxxxxxxx : Connection
{
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
/// <summary>
/// Results filter
/// </summary>
public List<int> ColumnTitle1List { get; set; }
/// <summary>
/// Searches for the specificed phases within the item description. Uses the LIKE AND sql function
/// </summary>
public List<string> ColumnTitle2List { get; set; }
public xxxxxxxYourClassNameHerexxxxxxxx()
{
Init();
}
public void Init()
{
sqlBuilder = new SqlWhereBuilder();
ColumnTitle1List = new List<int>();
ColumnTitle2List = new List<string>();
}
/// <summary>
///
/// </summary>
/// <returns>Dictionary were key is the table primary key</returns>
public Dictionary<int, string> Read()
{
// create the return (emptyP list) here
var returnList = new Dictionary<int, string>();
sqlBuilder.Init();
//build sql query
string sql = @"
SELECT item1, item2, item3
FROM tblPurchaseLine
WHERE 1 = 1 ";
// build the where statments
if (ColumnTitle1List.Any())
{
sqlBuilder.In("xxxxxxxxxxxxxxColumnTitle1xxxxxxxxxxxxxxxxxx", ColumnTitle1List, "AND");
}
if (ColumnTitle2List.Any())
{
sqlBuilder.In("xxxxxxxxxxxxxxColumnTitle2xxxxxxxxxxxxxxxxxx", ColumnTitle2List, "AND");
}
// append where string to the sql
if (sqlBuilder.IsSetSqlWhereString)
{
sql = sql + sqlBuilder.SqlWhereString;
}
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
sqlBuilder.AddParametersToSqlCommand(cmd);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
// read from db and create object
// and add to return list
}
}
}
}
}
// all done, return the list here
return returnList;
}
}
}