mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-21 15:27:15 +00:00
Migrated projects to dotnet8
migrated all projects over to .net8 incomplete feature for gui shipments
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
//using System.Data.SqlClient;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
208
src/bnhtrade.Core/Data/Database/Account/CreateJournal.cs
Normal file
208
src/bnhtrade.Core/Data/Database/Account/CreateJournal.cs
Normal file
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
internal class CreateJournal : Connection
|
||||
{
|
||||
/// <summary>
|
||||
/// Old code needs sorting
|
||||
/// </summary>
|
||||
public int AccountJournalInsert(int journalTypeId, DateTime entryDate, string currencyCode,
|
||||
decimal amount, int debitAccountId = 0, int creditAccountId = 0, bool lockEntry = false)
|
||||
{
|
||||
int defaultDebit = 0;
|
||||
int defaultCredit = 0;
|
||||
|
||||
// ensure date is UTC
|
||||
entryDate = DateTime.SpecifyKind(entryDate, DateTimeKind.Utc);
|
||||
|
||||
// debit and credit locks are checked in journal post method
|
||||
using (TransactionScope scope = new TransactionScope())
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// insert the journal entry
|
||||
int journalId;
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAccountJournal
|
||||
(AccountJournalTypeID, EntryDate, IsLocked)
|
||||
OUTPUT INSERTED.AccountJournalID
|
||||
VALUES
|
||||
(@journalTypeId, @entryDate, @lockEntry)
|
||||
", conn))
|
||||
{
|
||||
// add parameters
|
||||
cmd.Parameters.AddWithValue("@journalTypeId", journalTypeId);
|
||||
cmd.Parameters.AddWithValue("@entryDate", entryDate.ToUniversalTime());
|
||||
cmd.Parameters.AddWithValue("@lockEntry", lockEntry);
|
||||
|
||||
//execute
|
||||
journalId = (int)cmd.ExecuteScalar();
|
||||
}
|
||||
|
||||
// insert journal entries
|
||||
//bool postResult = AccountJournalPostInsert(sqlConnectionString, journalId, entryDate, currencyCode, amount, debitAccountId, creditAccountId);
|
||||
bool postResult = AccountJournalPostInsert(journalId, entryDate, currencyCode, amount, debitAccountId, creditAccountId);
|
||||
|
||||
scope.Complete();
|
||||
return journalId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Old code needs sorting
|
||||
/// </summary>
|
||||
internal bool AccountJournalPostInsert(int journalId, DateTime entryDate,
|
||||
string currencyCode, decimal amount, int debitAccountId = 0, int creditAccountId = 0)
|
||||
{
|
||||
int defaultDebit;
|
||||
int defaultCredit;
|
||||
entryDate = DateTime.SpecifyKind(entryDate, DateTimeKind.Utc);
|
||||
|
||||
using (TransactionScope scope = new TransactionScope())
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// ensure their are no other entries
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT
|
||||
Count(tblAccountJournalPost.AccountJournalPostID) AS CountOfAccountJournalPostID
|
||||
FROM
|
||||
tblAccountJournalPost
|
||||
WHERE
|
||||
(((tblAccountJournalPost.AccountJournalID)=@AccountJournalID));
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@AccountJournalID", journalId);
|
||||
|
||||
int count = (int)cmd.ExecuteScalar();
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
throw new Exception("Unable the insert journal posts, post already present AccountJournalID=" + journalId);
|
||||
}
|
||||
}
|
||||
|
||||
//checks
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT
|
||||
tblAccountJournalType.ChartOfAccountID_Debit, tblAccountJournalType.ChartOfAccountID_Credit
|
||||
FROM
|
||||
tblAccountJournal
|
||||
INNER JOIN tblAccountJournalType
|
||||
ON tblAccountJournal.AccountJournalTypeID = tblAccountJournalType.AccountJournalTypeID
|
||||
WHERE
|
||||
(((tblAccountJournal.AccountJournalID)=@journalId));
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@journalId", journalId);
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
// debit check
|
||||
if (reader.IsDBNull(0))
|
||||
{
|
||||
if (debitAccountId == 0)
|
||||
{
|
||||
throw new Exception("Debit Account ID required, default not set for journal type");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
defaultDebit = reader.GetInt32(0);
|
||||
if (debitAccountId == 0)
|
||||
{
|
||||
debitAccountId = defaultDebit;
|
||||
}
|
||||
else if (debitAccountId != defaultDebit)
|
||||
{
|
||||
throw new Exception("Debit Account ID supplied does not match default set for journal type");
|
||||
}
|
||||
|
||||
}
|
||||
// credit check
|
||||
if (reader.IsDBNull(1))
|
||||
{
|
||||
if (creditAccountId == 0)
|
||||
{
|
||||
throw new Exception("Credit Account ID required, default not set for journal type");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
defaultCredit = reader.GetInt32(1);
|
||||
if (creditAccountId == 0)
|
||||
{
|
||||
creditAccountId = defaultCredit;
|
||||
}
|
||||
else if (creditAccountId != defaultCredit)
|
||||
{
|
||||
throw new Exception("Credit Account ID supplied does not match default set for journal type");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("AccountJournalID '" + journalId + "' does not exist.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// currency conversion
|
||||
if (currencyCode != "GBP")
|
||||
{
|
||||
amount = new Data.Database.Account.Currency().CurrencyConvertToGbp(currencyCode, amount, entryDate);
|
||||
}
|
||||
|
||||
// ensure decimal is rounded
|
||||
amount = Math.Round(amount, 2);
|
||||
|
||||
// insert debit post
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAccountJournalPost
|
||||
(AccountJournalID, AccountChartOfID, AmountGbp)
|
||||
VALUES
|
||||
(@AccountJournalId, @AccountChartOfId, @AmountGbp)
|
||||
", conn))
|
||||
{
|
||||
// add parameters
|
||||
cmd.Parameters.AddWithValue("@AccountJournalId", journalId);
|
||||
cmd.Parameters.AddWithValue("@AccountChartOfId", debitAccountId);
|
||||
cmd.Parameters.AddWithValue("@AmountGbp", amount);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
// insert credit post
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAccountJournalPost
|
||||
(AccountJournalID, AccountChartOfID, AmountGbp)
|
||||
VALUES
|
||||
(@AccountJournalId, @AccountChartOfId, @AmountGbp)
|
||||
", conn))
|
||||
{
|
||||
// add parameters
|
||||
cmd.Parameters.AddWithValue("@AccountJournalId", journalId);
|
||||
cmd.Parameters.AddWithValue("@AccountChartOfId", creditAccountId);
|
||||
cmd.Parameters.AddWithValue("@AmountGbp", (amount * -1));
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
scope.Complete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
199
src/bnhtrade.Core/Data/Database/Account/Currency.cs
Normal file
199
src/bnhtrade.Core/Data/Database/Account/Currency.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
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 Currency : Connection
|
||||
{
|
||||
public decimal CurrencyConvertToGbp(string currencyCode, decimal amount, DateTime conversionDate)
|
||||
{
|
||||
if (currencyCode == "GBP" || amount == 0M)
|
||||
{
|
||||
return amount;
|
||||
}
|
||||
|
||||
if (currencyCode.Length != 3)
|
||||
{
|
||||
throw new Exception("Invalid currency code '" + currencyCode + "'");
|
||||
}
|
||||
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT CurrencyUnitsPerGBP
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE CurrencyCode=@currencyCode AND StartDate<=@conversionDate AND EndDate>@conversionDate
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
cmd.Parameters.AddWithValue("@conversionDate", conversionDate);
|
||||
|
||||
object result = cmd.ExecuteScalar();
|
||||
if (result != null)
|
||||
{
|
||||
return amount / Convert.ToDecimal(result);
|
||||
}
|
||||
}
|
||||
|
||||
// return reason for no record found
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT CurrencyUnitsPerGBP
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE CurrencyCode=@currencyCode
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
|
||||
object result = cmd.ExecuteScalar();
|
||||
if (result == null)
|
||||
{
|
||||
throw new Exception("Currency code '" + currencyCode + "' does not exist in Exchange Rate table");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Date range for " + currencyCode + " " + conversionDate.ToShortDateString() + " " +
|
||||
conversionDate.ToLongTimeString() + "' does not exist in Exchange Rate table");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int CurrencyExchangeRateInsert(int exchangeRateSource, string currencyCode,
|
||||
decimal currencyUnitsPerGbp, DateTime periodStart, DateTime periodEnd, bool checkOverride = false)
|
||||
{
|
||||
currencyUnitsPerGbp = decimal.Round(currencyUnitsPerGbp, 4);
|
||||
periodStart = DateTime.SpecifyKind(periodStart, DateTimeKind.Utc);
|
||||
periodEnd = DateTime.SpecifyKind(periodEnd, DateTimeKind.Utc);
|
||||
|
||||
// CHECKS
|
||||
// HMRC source only
|
||||
if (exchangeRateSource != 1)
|
||||
{
|
||||
throw new Exception("Function does not currently accept exchange rates from sources other than HMRC");
|
||||
}
|
||||
// currency code upper case only
|
||||
currencyCode = currencyCode.ToUpper();
|
||||
if (currencyCode.Length != 3)
|
||||
{
|
||||
throw new Exception("Invalid currency code '" + currencyCode + "'");
|
||||
}
|
||||
|
||||
if (periodEnd <= periodStart)
|
||||
{
|
||||
throw new Exception("Invalid date period.");
|
||||
}
|
||||
|
||||
if (checkOverride == false && (periodEnd - periodStart).Days > 31)
|
||||
{
|
||||
throw new Exception("Date period is greater than 31 days.");
|
||||
}
|
||||
|
||||
// retirve previous data
|
||||
DateTime? periodEndLast = null;
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT Max(tblAccountExchangeRate.EndDate) AS MaxOfEndDate
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE (((tblAccountExchangeRate.CurrencyCode) = @currencyCode))
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
|
||||
object obj = cmd.ExecuteScalar();
|
||||
|
||||
// currency code not existing
|
||||
if (obj == DBNull.Value && checkOverride == false)
|
||||
{
|
||||
throw new Exception("Currency code '" + currencyCode + "' does not exist in table");
|
||||
}
|
||||
// currency code exists
|
||||
else
|
||||
{
|
||||
periodEndLast = DateTime.SpecifyKind(Convert.ToDateTime(obj), DateTimeKind.Utc);
|
||||
|
||||
if (periodStart != periodEndLast)
|
||||
{
|
||||
throw new Exception("Invalid period start date -- must equal previous period end-date.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// retrive previous exchange rate and check
|
||||
decimal currencyUnitsPerGbpLast = 0;
|
||||
if (periodEndLast != null)
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT tblAccountExchangeRate.AccountExchangeRateID, tblAccountExchangeRate.CurrencyUnitsPerGBP
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE (tblAccountExchangeRate.EndDate = @periodEndLast)
|
||||
AND (CurrencyCode = @currencyCode);
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@periodEndLast", periodEndLast);
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
currencyUnitsPerGbpLast = reader.GetDecimal(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Error that shouldn't happen! Check code @ 5129f3e6-2f7e-4883-bc73-b317d8fa4050");
|
||||
}
|
||||
// error if >1 line
|
||||
if (reader.Read())
|
||||
{
|
||||
string errText = "Multiple lines in currency exchange table for '" + currencyCode + "' where [EndDate]=" + periodEndLast.ToString();
|
||||
new Logic.Log.LogEvent().LogError(errText);
|
||||
throw new Exception(errText);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check difference between current and previous exchange rates isn't too great
|
||||
if (checkOverride == false &&
|
||||
(currencyUnitsPerGbpLast > (currencyUnitsPerGbp * 1.05m) || currencyUnitsPerGbpLast < (currencyUnitsPerGbp * 0.95m))
|
||||
)
|
||||
{
|
||||
throw new Exception("Difference between supplied and previous exchange rates is greater than 5%");
|
||||
}
|
||||
|
||||
// MAKE THE INSERT
|
||||
int recordId = 0;
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAccountExchangeRate (ExchangeRateSource, CurrencyCode, CurrencyUnitsPerGBP, StartDate, EndDate)
|
||||
OUTPUT INSERTED.AccountExchangeRateID
|
||||
VALUES (@exchangeRateSource, @currencyCode, @currencyUnitsPerGbp, @periodStart, @periodEnd);
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@exchangeRateSource", exchangeRateSource);
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
cmd.Parameters.AddWithValue("@currencyUnitsPerGbp", currencyUnitsPerGbp);
|
||||
cmd.Parameters.AddWithValue("@periodStart", periodStart);
|
||||
cmd.Parameters.AddWithValue("@periodEnd", periodEnd);
|
||||
|
||||
recordId = (int)cmd.ExecuteScalar();
|
||||
|
||||
if (recordId < 1)
|
||||
{
|
||||
throw new Exception("Error inserting record, did not retrive new record ID.");
|
||||
}
|
||||
}
|
||||
|
||||
return recordId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
64
src/bnhtrade.Core/Data/Database/Account/DeleteJournal.cs
Normal file
64
src/bnhtrade.Core/Data/Database/Account/DeleteJournal.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
internal class DeleteJournal : Connection
|
||||
{
|
||||
/// <summary>
|
||||
/// Old code needs sorting
|
||||
/// </summary>
|
||||
public bool AccountJournalDelete(int accountJournalId)
|
||||
{
|
||||
// check if journal entry is locked
|
||||
using (TransactionScope scope = new TransactionScope())
|
||||
{
|
||||
bool IsLocked = new Data.Database.Account.ReadJournal().EntryIsLocked(accountJournalId);
|
||||
if (IsLocked == true)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// make the delete
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
DELETE FROM tblAccountJournalPost
|
||||
WHERE AccountJournalID=@accountJournalId;
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
|
||||
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
|
||||
if (rows == 0)
|
||||
{
|
||||
throw new Exception("Journal entry and/or entry posts do not exist for AccountJournalId=" + accountJournalId);
|
||||
}
|
||||
}
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
DELETE FROM tblAccountJournal
|
||||
WHERE AccountJournalID=@accountJournalId;
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
scope.Complete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
105
src/bnhtrade.Core/Data/Database/Account/ReadContact.cs
Normal file
105
src/bnhtrade.Core/Data/Database/Account/ReadContact.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
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 ReadContact : Connection
|
||||
{
|
||||
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
|
||||
|
||||
public List<int> ContactIdList { get; set; }
|
||||
|
||||
public ReadContact()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
sqlBuilder = new SqlWhereBuilder();
|
||||
ContactIdList = new List<int>();
|
||||
}
|
||||
|
||||
public Dictionary<int, Model.Account.Contact> Read()
|
||||
{
|
||||
var returnList = new Dictionary<int, Model.Account.Contact>();
|
||||
sqlBuilder.Init();
|
||||
|
||||
//build sql query
|
||||
string sql = @"
|
||||
SELECT [ContactID]
|
||||
,[ContactName]
|
||||
,[PaypalName]
|
||||
,[PaypalEmail]
|
||||
,[EbayUsername]
|
||||
,[EbayEmail]
|
||||
,[RecordCreated]
|
||||
,[RecordModified]
|
||||
FROM [e2A].[dbo].[tblContact]
|
||||
WHERE 1=1 ";
|
||||
|
||||
// build the where statments
|
||||
if (ContactIdList.Any())
|
||||
{
|
||||
sqlBuilder.In("[ContactID]", ContactIdList, "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())
|
||||
{
|
||||
int contactId = reader.GetInt32(0);
|
||||
string contactName = null;
|
||||
if (!reader.IsDBNull(1)) { contactName = reader.GetString(1); }
|
||||
string paypalName = null;
|
||||
if (!reader.IsDBNull(2)) { paypalName = reader.GetString(2); }
|
||||
string paypalEmail = null;
|
||||
if (!reader.IsDBNull(3)) { paypalEmail = reader.GetString(3); }
|
||||
string ebayUsername = null;
|
||||
if (!reader.IsDBNull(4)) { ebayUsername = reader.GetString(4); }
|
||||
string ebayEmail = null;
|
||||
if (!reader.IsDBNull(5)) { ebayEmail = reader.GetString(5); }
|
||||
DateTime recordCreated = DateTime.SpecifyKind(reader.GetDateTime(6), DateTimeKind.Utc);
|
||||
DateTime? recordModified = null;
|
||||
if (!reader.IsDBNull(7)) { recordModified = DateTime.SpecifyKind(reader.GetDateTime(7), DateTimeKind.Utc); }
|
||||
|
||||
var contact = new Model.Account.Contact();
|
||||
contact.ContactId = contactId;
|
||||
contact.ContantName = contactName;
|
||||
contact.ContactPaypalName = paypalName;
|
||||
contact.ContactPaypalEmail = paypalEmail;
|
||||
contact.ContactEbayName = ebayUsername;
|
||||
contact.ContactEbayEmail = ebayEmail;
|
||||
contact.Created = recordCreated;
|
||||
contact.Modified = recordModified;
|
||||
|
||||
returnList.Add(contactId, contact);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
}
|
||||
242
src/bnhtrade.Core/Data/Database/Account/ReadJournal.cs
Normal file
242
src/bnhtrade.Core/Data/Database/Account/ReadJournal.cs
Normal file
@@ -0,0 +1,242 @@
|
||||
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 ReadJournal : Connection
|
||||
{
|
||||
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// Filter the read results
|
||||
/// </summary>
|
||||
public List<uint> AccountJournalId { get; set; }
|
||||
|
||||
public ReadJournal()
|
||||
{
|
||||
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.ReadJournalType();
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for locked journal entry
|
||||
/// </summary>
|
||||
/// <returns>False on locked journal entry</returns>
|
||||
public bool EntryIsLocked(int journalId)
|
||||
{
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT
|
||||
tblAccountJournal.IsLocked
|
||||
FROM
|
||||
tblAccountJournal
|
||||
WHERE
|
||||
tblAccountJournal.AccountJournalID=@accountJournalId;
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
|
||||
|
||||
object obj = cmd.ExecuteScalar();
|
||||
if (obj == null)
|
||||
{
|
||||
throw new Exception("Journal entry not found for AccountJournalID=" + journalId);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (bool)obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
144
src/bnhtrade.Core/Data/Database/Account/ReadJournalType.cs
Normal file
144
src/bnhtrade.Core/Data/Database/Account/ReadJournalType.cs
Normal 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 ReadJournalType : 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 ReadJournalType()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
178
src/bnhtrade.Core/Data/Database/Account/ReadPurchaseInvoice.cs
Normal file
178
src/bnhtrade.Core/Data/Database/Account/ReadPurchaseInvoice.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.ComponentModel.Design.ObjectSelectorEditor;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
public class ReadPurchaseInvoice : Connection
|
||||
{
|
||||
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
|
||||
|
||||
public List<int> PurchaseInvoiceIdList { get; set; }
|
||||
|
||||
public ReadPurchaseInvoice()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
sqlBuilder = new SqlWhereBuilder();
|
||||
PurchaseInvoiceIdList = new List<int>();
|
||||
}
|
||||
|
||||
public Dictionary<int, Model.Account.PurchaseInvoice> Read()
|
||||
{
|
||||
var returnList = new Dictionary<int, Model.Account.PurchaseInvoice>();
|
||||
sqlBuilder.Init();
|
||||
|
||||
//build sql query
|
||||
string sql = @"
|
||||
SELECT tblPurchase.PurchaseID
|
||||
,tblPurchase.PurchaseNumber
|
||||
,tblPurchase.RecordID
|
||||
,tblPurchase.PurchaseDate
|
||||
,tblPurchase.ContactID
|
||||
,tblPurchase.SupplierRef
|
||||
,tblPurchase.PurchaseTotalAmount
|
||||
,tblPurchase.VatInclusiveAmounts
|
||||
,tblPurchase.RecordCreated
|
||||
,tblPurchase.RecordModified
|
||||
,tblPurchase.IsActive
|
||||
,tblAccountCurrency.CurrencyCode
|
||||
,tblPurchaseChannel.PurchaseChannelName
|
||||
,tblPurchaseStatus.PurchaseStatus
|
||||
FROM tblPurchase
|
||||
LEFT OUTER JOIN tblAccountCurrency ON tblPurchase.AccountCurrencyID = tblAccountCurrency.AccountCurrencyID
|
||||
LEFT OUTER JOIN tblPurchaseStatus ON tblPurchase.PurchaseStatusID = tblPurchaseStatus.PurchaseStatusID
|
||||
LEFT OUTER JOIN tblPurchaseChannel ON tblPurchase.PurchaseChannelID = tblPurchaseChannel.PurchaseChannelID
|
||||
WHERE 1 = 1
|
||||
";
|
||||
|
||||
// build the where statments
|
||||
if (PurchaseInvoiceIdList.Any())
|
||||
{
|
||||
sqlBuilder.In("[PurchaseID]", PurchaseInvoiceIdList, "AND");
|
||||
}
|
||||
|
||||
// append where string to the sql
|
||||
if (sqlBuilder.IsSetSqlWhereString)
|
||||
{
|
||||
sql = sql + sqlBuilder.SqlWhereString;
|
||||
}
|
||||
|
||||
// dictionary so we can fill in details afterwards
|
||||
var invoiceContactDict = new Dictionary<int, int>();
|
||||
var purchaseIdList = new List<int>();
|
||||
|
||||
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())
|
||||
{
|
||||
|
||||
|
||||
int purchaseID = reader.GetInt32(0);
|
||||
int purchaseNumber = reader.GetInt32(1);
|
||||
int? recordID = null;
|
||||
if (!reader.IsDBNull(2)) { recordID = reader.GetInt32(2); }
|
||||
DateTime purchaseDate = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc);
|
||||
int? contactID = null;
|
||||
if (!reader.IsDBNull(4)) { contactID = reader.GetInt32(4);}
|
||||
string supplierRef = null;
|
||||
if (!reader.IsDBNull(5)) { supplierRef = reader.GetString(5);}
|
||||
decimal? purchaseTotalAmount = null;
|
||||
if (!reader.IsDBNull(6)) { purchaseTotalAmount = reader.GetDecimal(6);}
|
||||
bool vatInclusiveAmounts = reader.GetBoolean(7);
|
||||
DateTime recordCreated = DateTime.SpecifyKind(reader.GetDateTime(8), DateTimeKind.Utc);
|
||||
DateTime recordModified = DateTime.SpecifyKind(reader.GetDateTime(9), DateTimeKind.Utc);
|
||||
bool isActive = reader.GetBoolean(10);
|
||||
string currencyCode = reader.GetString(11);
|
||||
string purchaseChannelName = reader.GetString(12);
|
||||
string purchaseStatus = null;
|
||||
if (!reader.IsDBNull(13)) { purchaseStatus = reader.GetString(13);}
|
||||
|
||||
var invoice = new Model.Account.PurchaseInvoice();
|
||||
invoice.PurchaseID = purchaseID;
|
||||
invoice.PurchaseNumber = purchaseNumber;
|
||||
invoice.RecordID = recordID;
|
||||
invoice.PurchaseDate = purchaseDate;
|
||||
invoice.SupplierRef = supplierRef;
|
||||
//invoice.PurchaseTotalAmount = purchaseTotalAmount;
|
||||
invoice.VatInclusiveAmounts = vatInclusiveAmounts;
|
||||
invoice.RecordCreated = recordCreated;
|
||||
invoice.RecordModified = recordModified;
|
||||
invoice.IsActive = isActive;
|
||||
invoice.CurrencyCode = currencyCode;
|
||||
invoice.PurchaseChannel = purchaseChannelName;
|
||||
|
||||
// is there contact info that needs to be added?
|
||||
if (contactID != null)
|
||||
{
|
||||
invoiceContactDict.Add(purchaseID, (int)contactID);
|
||||
}
|
||||
|
||||
purchaseIdList.Add(purchaseID);
|
||||
|
||||
returnList.Add(purchaseID, invoice);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// add contact info
|
||||
if (invoiceContactDict.Any())
|
||||
{
|
||||
var readContact = new Data.Database.Account.ReadContact();
|
||||
readContact.ContactIdList = invoiceContactDict.Values.ToList();
|
||||
var contactDict = readContact.Read();
|
||||
|
||||
if (contactDict.Any())
|
||||
{
|
||||
foreach ( var invoice in returnList)
|
||||
{
|
||||
if (invoiceContactDict.ContainsKey(invoice.Value.PurchaseID))
|
||||
{
|
||||
int contactId = invoiceContactDict[invoice.Value.PurchaseID];
|
||||
invoice.Value.Contact = contactDict[contactId];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add invoice lines
|
||||
var readLines = new Data.Database.Account.ReadPurchaseInvoiceLine();
|
||||
readLines.InvoiceIdList = purchaseIdList;
|
||||
var lines = readLines.Read();
|
||||
foreach(var invoice in returnList.Values)
|
||||
{
|
||||
foreach(var line in lines.Values)
|
||||
{
|
||||
if (line.PurchaseId == invoice.PurchaseID)
|
||||
{
|
||||
if (invoice.InvoiceLines == null)
|
||||
{
|
||||
invoice.InvoiceLines = new List<Model.Account.PurchaseInvoice.Line>();
|
||||
}
|
||||
invoice.InvoiceLines.Add(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
using Amazon.Runtime.Internal.Transform;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.ComponentModel.Design.ObjectSelectorEditor;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
internal class ReadPurchaseInvoiceLine : Connection
|
||||
{
|
||||
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// Results filter
|
||||
/// </summary>
|
||||
public List<int> InvoiceIdList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Results filter
|
||||
/// </summary>
|
||||
public List<int> InvoiceLineIdList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Results filter
|
||||
/// </summary>
|
||||
public List<string> StatusList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Searches for the specificed phases within the item description. Uses the LIKE AND sql function
|
||||
/// </summary>
|
||||
public List<string> ItemDescription { get; set; }
|
||||
|
||||
public ReadPurchaseInvoiceLine()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
sqlBuilder = new SqlWhereBuilder();
|
||||
InvoiceIdList = new List<int>();
|
||||
InvoiceLineIdList = new List<int>();
|
||||
StatusList = new List<string>();
|
||||
ItemDescription = new List<string>();
|
||||
}
|
||||
|
||||
public Dictionary<int, Model.Account.PurchaseInvoice.Line> Read()
|
||||
{
|
||||
var returnList = new Dictionary<int, Model.Account.PurchaseInvoice.Line>();
|
||||
sqlBuilder.Init();
|
||||
|
||||
//build sql query
|
||||
string sql = @"
|
||||
SELECT tblPurchaseLine.PurchaseLineID
|
||||
,tblPurchaseLine.PurchaseID
|
||||
,tblPurchaseLineStatus.PurchaseLineStatus
|
||||
,tblPurchaseLine.SupplierRef
|
||||
,tblPurchaseLine.CheckedIn
|
||||
,tblPurchaseLine.ItemDescription
|
||||
,tblPurchaseLine.ItemQuantity
|
||||
,tblPurchaseLine.ItemGross
|
||||
,tblPurchaseLine.ItemTax
|
||||
,tblPurchaseLine.ShippingGross
|
||||
,tblPurchaseLine.ShippingTax
|
||||
,tblPurchaseLine.OtherGross
|
||||
,tblPurchaseLine.OtherTax
|
||||
,tblPurchaseLine.AccountTaxCodeID
|
||||
,tblPurchaseLine.Tax_AccountTransactionID
|
||||
,tblPurchaseLine.Net_AccountChartOfID
|
||||
,tblPurchaseLine.Net_AccountTransactionID
|
||||
,tblPurchaseLine.RecordCreated
|
||||
,tblPurchaseLine.RecordModified
|
||||
,tblPurchaseLine.IsActive
|
||||
,tblAccountTaxCode.TaxCode
|
||||
FROM tblPurchaseLine
|
||||
INNER JOIN tblPurchaseLineStatus ON tblPurchaseLine.PurchaseLineStatusID = tblPurchaseLineStatus.PurchaseLineStatusID
|
||||
LEFT OUTER JOIN tblAccountTaxCode ON tblPurchaseLine.AccountTaxCodeID = tblAccountTaxCode.AccountTaxCodeID
|
||||
WHERE 1 = 1 ";
|
||||
|
||||
// build the where statments
|
||||
if (InvoiceIdList.Any())
|
||||
{
|
||||
sqlBuilder.In("PurchaseID", InvoiceLineIdList, "AND");
|
||||
}
|
||||
if (InvoiceLineIdList.Any())
|
||||
{
|
||||
sqlBuilder.In("PurchaseLineID", InvoiceLineIdList, "AND");
|
||||
}
|
||||
if (StatusList.Any())
|
||||
{
|
||||
sqlBuilder.In("PurchaseLineStatus", InvoiceLineIdList, "AND");
|
||||
}
|
||||
if (ItemDescription.Any())
|
||||
{
|
||||
sqlBuilder.LikeAnd("ItemDescription", ItemDescription, "AND");
|
||||
}
|
||||
|
||||
|
||||
// append where string to the sql
|
||||
if (sqlBuilder.IsSetSqlWhereString)
|
||||
{
|
||||
sql = sql + sqlBuilder.SqlWhereString;
|
||||
}
|
||||
|
||||
// catch taxcode to add in after db read
|
||||
var lineTaxCodeDict = new Dictionary<int, string>();
|
||||
|
||||
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())
|
||||
{
|
||||
int purchaseLineID = reader.GetInt32(0);
|
||||
int purchaseID = reader.GetInt32(1);
|
||||
string purchaseLineStatus = reader.GetString(2);
|
||||
string supplierRef = null;
|
||||
if (!reader.IsDBNull(3)) { supplierRef = reader.GetString(3); }
|
||||
DateTime? checkedIn = null;
|
||||
if (!reader.IsDBNull(4)) { checkedIn = DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc); }
|
||||
string itemDescription = null;
|
||||
if (!reader.IsDBNull(5)) { itemDescription = reader.GetString(5); }
|
||||
int itemQuantity = reader.GetInt32(6);
|
||||
decimal itemGross = 0;
|
||||
if (!reader.IsDBNull(7)) { itemGross = reader.GetDecimal(7); }
|
||||
decimal itemTax = 0;
|
||||
if (!reader.IsDBNull(8)) { itemTax = reader.GetDecimal(8); }
|
||||
decimal shippingGross = 0;
|
||||
if (!reader.IsDBNull(9)) { shippingGross = reader.GetDecimal(9); }
|
||||
decimal shippingTax = 0;
|
||||
if (!reader.IsDBNull(10)) { shippingTax = reader.GetDecimal(10); }
|
||||
decimal otherGross = 0;
|
||||
if (!reader.IsDBNull(11)) { otherGross = reader.GetDecimal(11); }
|
||||
decimal otherTax = 0;
|
||||
if (!reader.IsDBNull(12)) { otherTax = reader.GetDecimal(12); }
|
||||
int accountTaxCodeID = reader.GetInt32(13);
|
||||
int? tax_AccountTransactionID = null;
|
||||
if (!reader.IsDBNull(14)) { tax_AccountTransactionID = reader.GetInt32(14); }
|
||||
int net_AccountChartOfID = reader.GetInt32(15);
|
||||
int? net_AccountTransactionID = null;
|
||||
if (!reader.IsDBNull(16)) { net_AccountTransactionID = reader.GetInt32(16); }
|
||||
DateTime recordModified;
|
||||
DateTime recordCreated = DateTime.SpecifyKind(reader.GetDateTime(17), DateTimeKind.Utc);
|
||||
if (reader.IsDBNull(18)) { recordModified = recordCreated; }
|
||||
else { recordModified = DateTime.SpecifyKind(reader.GetDateTime(18), DateTimeKind.Utc); }
|
||||
bool isActive = reader.GetBoolean(19);
|
||||
string accountTaxCode = reader.GetString(20);
|
||||
|
||||
var line = new Model.Account.PurchaseInvoice.Line();
|
||||
line.PurchaseLineId = purchaseLineID;
|
||||
line.PurchaseId = purchaseID;
|
||||
line.Status = purchaseLineStatus;
|
||||
line.SupplierRef = supplierRef;
|
||||
line.CheckedIn = checkedIn;
|
||||
line.ItemDescription = itemDescription;
|
||||
line.ItemQuantity = itemQuantity;
|
||||
line.ItemGross = itemGross;
|
||||
line.ItemTax = itemTax;
|
||||
line.ShippingGross = shippingGross;
|
||||
line.ShippingTax = shippingTax;
|
||||
line.OtherGross = otherGross;
|
||||
line.OtherTax = otherTax;
|
||||
line.Tax_AccountTransactionId = tax_AccountTransactionID;
|
||||
line.Net_AccountChartOfId = net_AccountChartOfID;
|
||||
line.Net_AccountTransactionId = net_AccountTransactionID;
|
||||
line.RecordModified = recordModified;
|
||||
line.RecordCreated = recordCreated;
|
||||
line.IsActive = isActive;
|
||||
|
||||
returnList.Add(purchaseLineID, line);
|
||||
|
||||
lineTaxCodeDict.Add(purchaseLineID, accountTaxCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// read tax codes form db and add to return object
|
||||
var taxcodeList = new Data.Database.Account.ReadTaxCode().GetByTaxCode(lineTaxCodeDict.Values.ToList());
|
||||
|
||||
foreach (var line in returnList.Values)
|
||||
{
|
||||
foreach(var taxcode in taxcodeList)
|
||||
{
|
||||
if (taxcode.TaxCode == lineTaxCodeDict[line.PurchaseLineId])
|
||||
{
|
||||
line.AccountTaxCode = taxcode;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (line.AccountTaxCode == null)
|
||||
{
|
||||
throw new Exception("Fail safe, this really shouodn't happen");
|
||||
}
|
||||
}
|
||||
|
||||
// all done
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
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 ReadPurchaseInvoiceLineStatus : Connection
|
||||
{
|
||||
public Dictionary<int, Model.Account.PurchaseInvoiceLineStatus> Read()
|
||||
{
|
||||
var returnList = new Dictionary<int, Model.Account.PurchaseInvoiceLineStatus>();
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT [PurchaseLineStatusID]
|
||||
,[PurchaseLineStatus]
|
||||
,[ListSort]
|
||||
,[TimeStamp]
|
||||
FROM [e2A].[dbo].[tblPurchaseLineStatus]
|
||||
ORDER BY [ListSort]
|
||||
", conn))
|
||||
{
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (!reader.HasRows)
|
||||
{
|
||||
// do something
|
||||
}
|
||||
else
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
int id = reader.GetInt32(0);
|
||||
string name = reader.GetString(1);
|
||||
int lineSort = reader.GetInt32(2);
|
||||
|
||||
var returnItem = new Model.Account.PurchaseInvoiceLineStatus(id, name,lineSort);
|
||||
returnList.Add(id, returnItem );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using FikaAmazonAPI.AmazonSpApiSDK.Models.ProductFees;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.ComponentModel.Design.ObjectSelectorEditor;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
internal class ReadPurchaseInvoiceLineSummary : Connection
|
||||
{
|
||||
public List<Model.Account.PurchaseInvoiceLineSummary> Read(DateTime periodTo, string lineStatus, List<string> descriptionSearch)
|
||||
{
|
||||
var returnList = new List<Model.Account.PurchaseInvoiceLineSummary>();
|
||||
var sqlBuilder = new bnhtrade.Core.Data.Database.SqlWhereBuilder();
|
||||
|
||||
//build sql query
|
||||
string sql = @"
|
||||
SELECT tblPurchase.PurchaseDate
|
||||
,tblPurchase.PurchaseID
|
||||
,tblPurchase.PurchaseNumber
|
||||
,tblPurchaseLine.PurchaseLineID
|
||||
,tblPurchaseLine.ItemDescription
|
||||
,tblPurchaseLineStatus.PurchaseLineStatus
|
||||
FROM tblPurchase
|
||||
INNER JOIN tblPurchaseLine ON tblPurchase.PurchaseID = tblPurchaseLine.PurchaseID
|
||||
INNER JOIN tblPurchaseLineStatus ON tblPurchaseLine.PurchaseLineStatusID = tblPurchaseLineStatus.PurchaseLineStatusID
|
||||
WHERE tblPurchase.PurchaseDate <= @purchaseDate
|
||||
";
|
||||
|
||||
if (lineStatus != null)
|
||||
{
|
||||
sql = sql + " AND PurchaseLineStatus = @purchaseLineStatus ";
|
||||
}
|
||||
|
||||
// build the where statments
|
||||
if (descriptionSearch.Any())
|
||||
{
|
||||
sqlBuilder.LikeAnd("ItemDescription", descriptionSearch, "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))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@purchaseDate", periodTo);
|
||||
if (lineStatus != null)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@purchaseLineStatus", lineStatus);
|
||||
}
|
||||
|
||||
sqlBuilder.AddParametersToSqlCommand(cmd);
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
DateTime purchaseDate = DateTime.SpecifyKind(reader.GetDateTime(0), DateTimeKind.Utc);
|
||||
int purchaseID = reader.GetInt32(1);
|
||||
int purchaseNumber = reader.GetInt32(2);
|
||||
int purchaseLineID = reader.GetInt32(3);
|
||||
string itemDescription = reader.GetString(4);
|
||||
string purchaseLineStatus = reader.GetString(5);
|
||||
|
||||
var item = new Model.Account.PurchaseInvoiceLineSummary();
|
||||
item.PurchaseDate = purchaseDate;
|
||||
item.PurchaseId = purchaseID;
|
||||
item.PurchaseNumber = purchaseNumber;
|
||||
item.PurchaseLineId = purchaseLineID;
|
||||
item.ItemDescription = itemDescription;
|
||||
item.LineStatus = purchaseLineStatus;
|
||||
|
||||
returnList.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
src/bnhtrade.Core/Data/Database/Account/UpdateJournal.cs
Normal file
75
src/bnhtrade.Core/Data/Database/Account/UpdateJournal.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
internal class UpdateJournal : Connection
|
||||
{
|
||||
public bool AccountJournalPostUpdate(int journalId, string currencyCode, decimal amount, int debitAccountId = 0, int creditAccountId = 0)
|
||||
{
|
||||
using (TransactionScope scope = new TransactionScope())
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// retrive journal entry date
|
||||
DateTime entryDate;
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT
|
||||
tblAccountJournal.EntryDate
|
||||
FROM
|
||||
tblAccountJournal
|
||||
WHERE
|
||||
(((tblAccountJournal.AccountJournalID)=@accountJournalId));
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
|
||||
|
||||
entryDate = DateTime.SpecifyKind((DateTime)cmd.ExecuteScalar(), DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
// delete the original posts
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
DELETE FROM
|
||||
tblAccountJournalPost
|
||||
WHERE
|
||||
(((tblAccountJournalPost.AccountJournalID)=@accountJournalId));
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
//insert new posts
|
||||
//bool postResult = AccountJournalPostInsert(sqlConnectionString, journalId, entryDate, currencyCode, amount, debitAccountId, creditAccountId);
|
||||
bool postResult = new Data.Database.Account.CreateJournal().AccountJournalPostInsert(journalId, entryDate, currencyCode, amount, debitAccountId, creditAccountId);
|
||||
|
||||
// update modified date on journal
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
UPDATE
|
||||
tblAccountJournal
|
||||
SET
|
||||
tblAccountJournal.LastModified=@utcNow
|
||||
WHERE
|
||||
(((tblAccountJournal.AccountJournalID)=@accountJournalId));
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
|
||||
cmd.Parameters.AddWithValue("@utcNow", DateTime.UtcNow);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
scope.Complete();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user