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 Execute(string sqlWhere, Dictionary parameters) { var resultList = new List(); //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 GetByTaxCode(List taxcodeList) { var resultList = new List(); 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 GetAllActive() { string sqlWhere = @" WHERE IsActive=@isActive;"; var parameters = new Dictionary(); parameters.Add("@isActive", true); return Execute(sqlWhere, parameters); } public Dictionary GetTaxCodeBySkuNumber(List skuNumberList) { var resultList = new Dictionary(); 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 GetTaxCodeByInvoiceLineItemCode(List lineItemCode) { var resultList = new Dictionary(); 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; } } }