mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-21 07:17:15 +00:00
Migrated projects to dotnet8
migrated all projects over to .net8 incomplete feature for gui shipments
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user