Files
bnhtrade/src/bnhtrade.Core/Data/Database/Account/ReadTaxCode.cs
Bobbie Hodgetts f1d7119306 the abandoned fba repricing feature
* Merge master into branch (#15)

* Bug fix

* Bug fix when retriving shipment stock status id

* Various bug fixs and improvements to stock SKU reconciliation

* Last MWS report import date time saved

* master into branch (#16)

* Bug fix

* Bug fix when retriving shipment stock status id

* Various bug fixs and improvements to stock SKU reconciliation

* Last MWS report import date time saved

* wip

* wip

* wip

* wip

* wip

* some extra tidying up to get it to complie and the main merge is complete

* wip
2024-04-17 13:23:33 +01:00

219 lines
7.4 KiB
C#

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 Data.Database.SqlWhereBuilder whereBuilder;
public ReadTaxCode()
{
whereBuilder = new SqlWhereBuilder();
}
private List<Model.Account.TaxCodeInfo> Execute(string sqlWhere, Dictionary<string, object> parameters)
{
var resultList = new List<Model.Account.TaxCodeInfo>();
//build sql query
string sqlString = @"
SELECT
TaxCode
,TaxCodeName
,TaxCodeDescription
,TaxRatePercent
,IsMarginScheme
,IsValidOnExpense
,IsVailidOnIncome
,IsActive
,TaxType
FROM tblAccountTaxCode";
sqlString += sqlWhere;
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
{
foreach (var paramter in parameters)
{
cmd.Parameters.AddWithValue(paramter.Key, paramter.Value);
}
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
string taxCodeId = reader.GetString(0);
string name = reader.GetString(1);
string description = null;
if (!reader.IsDBNull(2)) { description = reader.GetString(2); }
decimal rate = reader.GetDecimal(3);
bool isMargin = reader.GetBoolean(4);
bool isValidOnExpense = reader.GetBoolean(5);
bool isValidOnIncome = reader.GetBoolean(6);
bool isActive = reader.GetBoolean(7);
string taxType = reader.GetString(8);
var result = new Model.Account.TaxCodeInfo(
taxCodeId,
name,
description,
rate,
isMargin,
isValidOnExpense,
isValidOnIncome,
taxType,
isActive);
resultList.Add(result);
}
}
}
}
}
return resultList;
}
public List<Model.Account.TaxCodeInfo> GetByTaxCode(List<string> taxcodeList)
{
var resultList = new List<Model.Account.TaxCodeInfo>();
if (taxcodeList == null || !taxcodeList.Any())
{
return resultList;
}
taxcodeList = taxcodeList.Distinct().ToList();
whereBuilder.Innit();
whereBuilder.In("TaxCode", taxcodeList, "WHERE");
return Execute(whereBuilder.SqlWhereString, whereBuilder.ParameterList);
}
public List<Model.Account.TaxCodeInfo> GetAllActive()
{
string sqlWhere = @"
WHERE IsActive=@isActive;";
var parameters = new Dictionary<string, object>();
parameters.Add("@isActive", true);
return Execute(sqlWhere, parameters);
}
public Dictionary<string, string> GetTaxCodeBySkuNumber(List<string> skuNumberList)
{
var resultList = new Dictionary<string, string>();
if (skuNumberList == null || !skuNumberList.Any())
{
return resultList;
}
skuNumberList = skuNumberList.Distinct().ToList();
string sql = @"
SELECT tblSku.skuSkuNumber
,tblAccountTaxCode.TaxCode
FROM tblSku
INNER JOIN tblAccountTaxCode ON tblSku.AccountTaxCodeID = tblAccountTaxCode.AccountTaxCodeID ";
whereBuilder.Innit();
whereBuilder.In("tblSku.skuSkuNumber", skuNumberList, "WHERE");
sql += whereBuilder.SqlWhereString;
using (var conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (var cmd = new SqlCommand(sql, conn))
{
foreach (var param in whereBuilder.ParameterList)
{
cmd.Parameters.AddWithValue(param.Key, param.Value);
}
using (var reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
{
return resultList;
}
while (reader.Read())
{
resultList.Add(reader.GetString(0), reader.GetString(1));
}
}
}
}
return resultList;
}
public Dictionary<string, string> GetTaxCodeByInvoiceLineItemCode(List<string> lineItemCode)
{
var resultList = new Dictionary<string, string>();
if (lineItemCode == null || !lineItemCode.Any())
{
return resultList;
}
lineItemCode = lineItemCode.Distinct().ToList();
string sql = @"
SELECT tblAccountInvoiceLineItem.ItemCode
,tblAccountTaxCode.TaxCode
FROM tblAccountInvoiceLineItem
LEFT OUTER JOIN tblAccountTaxCode ON tblAccountInvoiceLineItem.AccountTaxCodeID_Default = tblAccountTaxCode.AccountTaxCodeID
";
whereBuilder.Innit();
whereBuilder.In("tblAccountInvoiceLineItem.ItemCode", lineItemCode, "WHERE");
sql += whereBuilder.SqlWhereString;
using (var conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (var cmd = new SqlCommand(sql, conn))
{
foreach (var param in whereBuilder.ParameterList)
{
cmd.Parameters.AddWithValue(param.Key, param.Value);
}
using (var reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
{
return resultList;
}
while (reader.Read())
{
if (reader.IsDBNull(1)) { resultList.Add(reader.GetString(0), null); }
else { resultList.Add(reader.GetString(0), reader.GetString(1)); }
}
}
}
}
return resultList;
}
}
}