mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 14:37:16 +00:00
Export amazon settlement report fix
This commit is contained in:
137
src/bnhtrade.Core/Data/Database/Account/ReadAccountCode.cs
Normal file
137
src/bnhtrade.Core/Data/Database/Account/ReadAccountCode.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
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
|
||||
{
|
||||
public class ReadAccountCode : Connection
|
||||
{
|
||||
private bool allRetrived;
|
||||
private Dictionary<int, Model.Account.AccountCode> cache;
|
||||
protected ReadAccountCode(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
allRetrived = false;
|
||||
cache = new Dictionary<int, Model.Account.AccountCode>();
|
||||
}
|
||||
protected void ClearCache()
|
||||
{
|
||||
allRetrived = false;
|
||||
cache.Clear();
|
||||
}
|
||||
public List<Model.Account.AccountCode> GetAll()
|
||||
{
|
||||
if (allRetrived == false)
|
||||
{
|
||||
UpdateCache(-1);
|
||||
allRetrived = true;
|
||||
}
|
||||
|
||||
var returnList = new List<Model.Account.AccountCode>();
|
||||
foreach (var item in cache)
|
||||
{
|
||||
returnList.Add(item.Value);
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
public Model.Account.AccountCode GetByAccountCode(int accountCode)
|
||||
{
|
||||
if (cache.ContainsKey(accountCode))
|
||||
{
|
||||
return cache[accountCode];
|
||||
}
|
||||
else if (allRetrived)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateCache(accountCode);
|
||||
if (cache.ContainsKey(accountCode))
|
||||
{
|
||||
return cache[accountCode];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void UpdateCache(int accountCode = -1)
|
||||
{
|
||||
var dicCache = new Dictionary<int, Model.Account.AccountCode>();
|
||||
|
||||
bool whereClause = false;
|
||||
//build sql query
|
||||
string sqlString = @"
|
||||
SELECT tblAccountChartOf.AccountChartOfID
|
||||
,tblAccountChartOf.AccountCode
|
||||
,tblAccountChartOf.AccountName
|
||||
,tblAccountChartOf.Description
|
||||
,tblAccountChartOfType.AccountChartOfType
|
||||
,tblAccountChartOfType.BasicType
|
||||
FROM tblAccountChartOf
|
||||
INNER JOIN tblAccountChartOfType ON tblAccountChartOf.AccountChartOfTypeID = tblAccountChartOfType.AccountChartOfTypeID";
|
||||
if (accountCode > -1)
|
||||
{
|
||||
whereClause = true;
|
||||
sqlString = sqlString + @"
|
||||
WHERE (((tblAccountChartOf.AccountCode) = @accountCode))";
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
||||
{
|
||||
if (whereClause)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@accountCode", accountCode);
|
||||
}
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.HasRows)
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var result = new Model.Account.AccountCode();
|
||||
|
||||
int tablePk = reader.GetInt32(0);
|
||||
result.AccountCodeId = reader.GetInt32(1);
|
||||
result.Title = reader.GetString(2);
|
||||
if (!reader.IsDBNull(3)) { result.Description = reader.GetString(3); }
|
||||
result.Type = reader.GetString(4);
|
||||
result.BasicType = reader.GetString(5);
|
||||
|
||||
if (whereClause)
|
||||
{
|
||||
if (cache.ContainsKey(result.AccountCodeId))
|
||||
{
|
||||
cache.Remove(result.AccountCodeId);
|
||||
}
|
||||
cache.Add(result.AccountCodeId, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
dicCache.Add(result.AccountCodeId, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// update cache
|
||||
if (!whereClause)
|
||||
{
|
||||
cache.Clear();
|
||||
allRetrived = true;
|
||||
cache = dicCache;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
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
|
||||
{
|
||||
public class ReadInvoiceLineItemCode : Connection
|
||||
{
|
||||
public ReadInvoiceLineItemCode(string sqlConnectionString) : base (sqlConnectionString)
|
||||
{
|
||||
cacheLineItemByTablePk = new Dictionary<int, Model.Account.InvoiceLineItem>();
|
||||
cacheTablePkByItemCode = new Dictionary<string, int>();
|
||||
}
|
||||
private Dictionary<int, Model.Account.InvoiceLineItem> cacheLineItemByTablePk;
|
||||
private Dictionary<string, int> cacheTablePkByItemCode;
|
||||
public bool InsertNewOnNoMatch { get; set; } = false;
|
||||
|
||||
public void CacheClear()
|
||||
{
|
||||
cacheLineItemByTablePk.Clear();
|
||||
cacheTablePkByItemCode.Clear();
|
||||
}
|
||||
|
||||
private void CacheRemove(int tablePk)
|
||||
{
|
||||
if (cacheLineItemByTablePk.ContainsKey(tablePk))
|
||||
{
|
||||
string itemCode = cacheLineItemByTablePk[tablePk].ItemCode;
|
||||
cacheLineItemByTablePk.Remove(tablePk);
|
||||
cacheTablePkByItemCode.Remove(itemCode);
|
||||
}
|
||||
}
|
||||
|
||||
public void CacheRemove(string itemCode)
|
||||
{
|
||||
if (cacheTablePkByItemCode.ContainsKey(itemCode))
|
||||
{
|
||||
CacheRemove(cacheTablePkByItemCode[itemCode]);
|
||||
}
|
||||
}
|
||||
|
||||
private void CacheUpdate(int tablePk, Model.Account.InvoiceLineItem lineItem)
|
||||
{
|
||||
if (cacheLineItemByTablePk.ContainsKey(tablePk))
|
||||
{
|
||||
CacheRemove(tablePk);
|
||||
}
|
||||
cacheLineItemByTablePk.Add(tablePk, lineItem);
|
||||
cacheTablePkByItemCode.Add(lineItem.ItemCode, tablePk);
|
||||
}
|
||||
|
||||
public Model.Account.InvoiceLineItem ByItemCode(string itemCode, bool forceCacheUpdate = false)
|
||||
{
|
||||
// check cache
|
||||
if (!forceCacheUpdate && cacheTablePkByItemCode.ContainsKey(itemCode))
|
||||
{
|
||||
return cacheLineItemByTablePk[cacheTablePkByItemCode[itemCode]];
|
||||
}
|
||||
|
||||
var returnObject = new Model.Account.InvoiceLineItem();
|
||||
returnObject.ItemCode = itemCode;
|
||||
|
||||
using (TransactionScope scopeSupress = new TransactionScope(TransactionScopeOption.Suppress))
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT tblExportAccountInvoiceLineType.ExportAccountInvoiceLineTypeID
|
||||
,tblExportAccountInvoiceLineType.TypeTitle
|
||||
,tblExportAccountInvoiceLineType.TypeDescription
|
||||
,tblExportAccountInvoiceLineType.IsNewReviewRequired
|
||||
,tblExportAccountInvoiceLineType.InvoiceLineEntryEnable
|
||||
,tblAccountChartOf.AccountCode
|
||||
,tblAccountTaxCode.TaxCode
|
||||
FROM (
|
||||
tblExportAccountInvoiceLineType LEFT JOIN tblAccountChartOf ON tblExportAccountInvoiceLineType.AccountChartOfID_Default = tblAccountChartOf.AccountChartOfID
|
||||
)
|
||||
LEFT JOIN tblAccountTaxCode ON tblExportAccountInvoiceLineType.AccountTaxCodeID_Default = tblAccountTaxCode.AccountTaxCodeID
|
||||
WHERE (((tblExportAccountInvoiceLineType.MatchString) = @matchString))
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@matchString", returnObject.ItemCode);
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
int tablePk = reader.GetInt32(0);
|
||||
returnObject.Title = reader.GetString(1);
|
||||
if (!reader.IsDBNull(2)) { returnObject.Description = reader.GetString(2); }
|
||||
returnObject.IsNewReviewRequired = reader.GetBoolean(3);
|
||||
returnObject.InvoiceLineEntryEnabled = reader.GetBoolean(4);
|
||||
if (!reader.IsDBNull(5)) { returnObject.DefaultAccountCode = reader.GetInt32(5); }
|
||||
if (!reader.IsDBNull(6)) { returnObject.DefaultTaxCode = reader.GetString(6); }
|
||||
|
||||
// add to cache
|
||||
CacheUpdate(tablePk, returnObject);
|
||||
|
||||
// return object
|
||||
return returnObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// insert new and retrive new value, if required
|
||||
if (InsertNewOnNoMatch)
|
||||
{
|
||||
int tablePk = 0;
|
||||
returnObject.Title = "NEW TYPE";
|
||||
returnObject.ItemCode = itemCode;
|
||||
returnObject.IsNewReviewRequired = true;
|
||||
|
||||
using (SqlCommand insertCmd = new SqlCommand(@"
|
||||
INSERT INTO tblExportAccountInvoiceLineType ( TypeTitle, MatchString )
|
||||
OUTPUT INSERTED.ExportAccountInvoiceLineTypeID
|
||||
VALUES ( @typeTitle, @matchString )
|
||||
", conn))
|
||||
{
|
||||
insertCmd.Parameters.AddWithValue("@typeTitle", returnObject.Title);
|
||||
insertCmd.Parameters.AddWithValue("@matchString", returnObject.ItemCode);
|
||||
|
||||
tablePk = (int)insertCmd.ExecuteScalar();
|
||||
|
||||
scopeSupress.Complete();
|
||||
}
|
||||
// add to cache
|
||||
CacheUpdate(tablePk, returnObject);
|
||||
|
||||
// return object
|
||||
return returnObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
227
src/bnhtrade.Core/Data/Database/Account/ReadTaxCode.cs
Normal file
227
src/bnhtrade.Core/Data/Database/Account/ReadTaxCode.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
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
|
||||
{
|
||||
public class ReadTaxCode : Connection
|
||||
{
|
||||
private bool allRetrived;
|
||||
private Dictionary<string, Model.Account.TaxCode> cache;
|
||||
public ReadTaxCode(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
allRetrived = false;
|
||||
cache = new Dictionary<string, Model.Account.TaxCode>();
|
||||
}
|
||||
private void ClearCache()
|
||||
{
|
||||
allRetrived = false;
|
||||
cache.Clear();
|
||||
}
|
||||
public List<Model.Account.TaxCode> GetAll()
|
||||
{
|
||||
if (allRetrived == false)
|
||||
{
|
||||
UpdateCache(null);
|
||||
allRetrived = true;
|
||||
}
|
||||
|
||||
var returnList = new List<Model.Account.TaxCode>();
|
||||
foreach (var item in cache)
|
||||
{
|
||||
returnList.Add(item.Value);
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
public Dictionary<string, Model.Account.TaxCode> BySkuNumber(List<string> skuNumberList)
|
||||
{
|
||||
// check input list for items
|
||||
if (skuNumberList == null || !skuNumberList.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// build SQL string
|
||||
string sqlString = @"
|
||||
SELECT
|
||||
tblSku.skuSkuNumber, tblAccountTaxCode.TaxCode
|
||||
FROM
|
||||
tblAccountTaxCode INNER JOIN tblSku ON tblAccountTaxCode.AccountTaxCodeID = tblSku.AccountTaxCodeID
|
||||
WHERE
|
||||
";
|
||||
|
||||
var parameterValueList = new List<Tuple<string, string>>();
|
||||
foreach (var item in skuNumberList)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(item))
|
||||
{
|
||||
int count = parameterValueList.Count;
|
||||
var parameterValue = new Tuple<string, string>("@parameter" + count, item);
|
||||
parameterValueList.Add(parameterValue);
|
||||
if (count == 0)
|
||||
{
|
||||
sqlString = sqlString + @"
|
||||
skuSkuNumber = " + parameterValue.Item1;
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlString = sqlString + @"
|
||||
OR skuSkuNumber = " + parameterValue.Item1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (parameterValueList.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// execute query and build result list
|
||||
var skuTaxCodeList = new List<Tuple<string, string>>();
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
||||
{
|
||||
foreach (var item in parameterValueList)
|
||||
{
|
||||
cmd.Parameters.AddWithValue(item.Item1, item.Item2);
|
||||
}
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (!reader.HasRows)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var skuTaxCode = new Tuple<string, string>(
|
||||
reader.GetString(0),
|
||||
reader.GetString(1)
|
||||
);
|
||||
skuTaxCodeList.Add(skuTaxCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// build dictionary of skuNumber to TaxCodeInfo
|
||||
var returnDictionary = new Dictionary<string, Model.Account.TaxCode>();
|
||||
foreach (var item in skuTaxCodeList)
|
||||
{
|
||||
returnDictionary.Add(item.Item1, GetByTaxCodeId(item.Item2));
|
||||
}
|
||||
|
||||
return returnDictionary;
|
||||
}
|
||||
}
|
||||
public Model.Account.TaxCode GetByTaxCodeId(string taxCode)
|
||||
{
|
||||
if (cache.ContainsKey(taxCode))
|
||||
{
|
||||
return cache[taxCode];
|
||||
}
|
||||
else if (allRetrived)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateCache(taxCode);
|
||||
if (cache.ContainsKey(taxCode))
|
||||
{
|
||||
return cache[taxCode];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
private void UpdateCache(string taxCode)
|
||||
{
|
||||
var dicCache = new Dictionary<string, Model.Account.TaxCode>();
|
||||
|
||||
bool whereClause = false;
|
||||
//build sql query
|
||||
string sqlString = @"
|
||||
SELECT
|
||||
TaxCode
|
||||
,TaxRateName
|
||||
,TaxRateMultiplierNet
|
||||
,TaxRateMultiplierGross
|
||||
,IsValidOnExpense
|
||||
,IsVailidOnIncome
|
||||
,Description
|
||||
,IsActive
|
||||
,TaxType
|
||||
FROM tblAccountTaxCode";
|
||||
if (!string.IsNullOrWhiteSpace(taxCode))
|
||||
{
|
||||
whereClause = true;
|
||||
sqlString = sqlString + @"
|
||||
WHERE TaxCode = @taxCode";
|
||||
}
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
||||
{
|
||||
if (whereClause)
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@taxCode", taxCode);
|
||||
}
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.HasRows)
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var result = new Model.Account.TaxCode();
|
||||
|
||||
result.TaxCodeId = reader.GetString(0);
|
||||
result.TaxRateTitle = reader.GetString(1);
|
||||
result.NetAmountMultiplier = reader.GetDecimal(2);
|
||||
result.GrossAmountMultiplier = reader.GetDecimal(3);
|
||||
result.IsValidOnExpense = reader.GetBoolean(4);
|
||||
result.IsValidOnIncome = reader.GetBoolean(5);
|
||||
if (!reader.IsDBNull(6)) { result.TaxRateDescription = reader.GetString(6); }
|
||||
result.IsActive = reader.GetBoolean(7);
|
||||
result.TaxType = reader.GetString(8);
|
||||
|
||||
if (whereClause)
|
||||
{
|
||||
if (cache.ContainsKey(result.TaxCodeId))
|
||||
{
|
||||
cache.Remove(result.TaxCodeId);
|
||||
}
|
||||
cache.Add(result.TaxCodeId, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
dicCache.Add(result.TaxCodeId, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// update cache
|
||||
if (!whereClause)
|
||||
{
|
||||
cache.Clear();
|
||||
allRetrived = true;
|
||||
cache = dicCache;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user