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:
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