mirror of
https://github.com/stokebob/BealeEngineering.git
synced 2026-03-19 06:37:15 +00:00
163 lines
5.0 KiB
C#
163 lines
5.0 KiB
C#
using Dapper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BealeEngineering.Core.Data.Database.Sale
|
|
{
|
|
public class ReadInvoiceHeader : Connection
|
|
{
|
|
public ReadInvoiceHeader(string sqlConnectionString) : base(sqlConnectionString)
|
|
{
|
|
|
|
}
|
|
/// <summary>
|
|
/// Setting this will override he other filters, within the sql statement.
|
|
/// </summary>
|
|
protected List<int> SalesInvoiceIdList { get; set; }
|
|
private bool SalesInvoiceIdListListIsSet
|
|
{
|
|
get
|
|
{
|
|
if (SalesInvoiceIdList == null || !SalesInvoiceIdList.Any()) { return false; }
|
|
else { return true; }
|
|
}
|
|
}
|
|
public DateTime DateFrom { get; set; }
|
|
public bool DateFromIsSet
|
|
{
|
|
get
|
|
{
|
|
if (DateFrom == null || DateFrom == default(DateTime)) { return false; }
|
|
else { return true; }
|
|
}
|
|
}
|
|
public DateTime DateTo { get; set; }
|
|
public bool DateToIsSet
|
|
{
|
|
get
|
|
{
|
|
if (DateTo == null || DateTo == default(DateTime)) { return false; }
|
|
else { return true; }
|
|
}
|
|
}
|
|
public List<string> InvoiceNumber { get; set; }
|
|
public bool InvoiceNumberIsSet
|
|
{
|
|
get
|
|
{
|
|
if (InvoiceNumber == null || !InvoiceNumber.Any()) { return false; }
|
|
else { return true; }
|
|
}
|
|
}
|
|
public List<string> Reference { get; set; }
|
|
public bool ReferenceIsSet
|
|
{
|
|
get
|
|
{
|
|
if (Reference == null || !Reference.Any()) { return false; }
|
|
else { return true; }
|
|
}
|
|
}
|
|
public List<Model.Sale.InvoiceHeader> GetBySaleInvoiceId(List<int> orderIdList)
|
|
{
|
|
SalesInvoiceIdList = orderIdList;
|
|
try
|
|
{
|
|
return GetByFilters();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
SalesInvoiceIdList = null;
|
|
}
|
|
}
|
|
protected void AddSqlWhereString(ref string sqlString, ref DynamicParameters parameters)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(sqlString))
|
|
{ throw new Exception("SQL string is empty."); }
|
|
|
|
sqlString = sqlString + @"
|
|
WHERE 1=1";
|
|
|
|
if (SalesInvoiceIdListListIsSet)
|
|
{
|
|
sqlString = sqlString + @"
|
|
AND SaleInvoice.SaleInvoiceID IN @saleInvoiceId";
|
|
|
|
parameters.Add("@saleInvoiceId", SalesInvoiceIdList);
|
|
}
|
|
else
|
|
{
|
|
if (DateFromIsSet)
|
|
{
|
|
sqlString = sqlString + @"
|
|
AND InvoiceDate >= @dateFrom";
|
|
|
|
parameters.Add("@dateFrom", DateFrom);
|
|
}
|
|
if (DateToIsSet)
|
|
{
|
|
sqlString = sqlString + @"
|
|
AND InvoiceDate <= @dateTo";
|
|
|
|
parameters.Add("@dateTo", DateTo);
|
|
}
|
|
if (InvoiceNumberIsSet)
|
|
{
|
|
sqlString = sqlString + @"
|
|
AND SaleInvoiceNumber IN @salesInvoiceNumber";
|
|
|
|
parameters.Add("@salesInvoiceNumber", InvoiceNumber);
|
|
}
|
|
if (ReferenceIsSet)
|
|
{
|
|
sqlString = sqlString + @"
|
|
AND Reference IN @reference";
|
|
|
|
parameters.Add("@reference", Reference);
|
|
}
|
|
}
|
|
}
|
|
public List<Model.Sale.InvoiceHeader> GetByFilters()
|
|
{
|
|
// build the sql string and dapper parameters
|
|
var parameters = new DynamicParameters();
|
|
string sqlString = @"
|
|
SELECT SaleInvoice.SaleInvoiceID
|
|
,SaleInvoice.SaleInvoiceNumber
|
|
,SaleInvoice.InvoiceDate
|
|
,SaleInvoice.DateDue
|
|
,SaleInvoice.Reference
|
|
,SaleInvoice.CurrencyCode
|
|
,SaleInvoice.InvoiceTotal
|
|
,SaleInvoice.TaxTotal
|
|
,SaleInvoice.IsCreditNote
|
|
,SaleInvoice.Status
|
|
,Contact.ContactName
|
|
FROM SaleInvoice
|
|
INNER JOIN Contact ON SaleInvoice.ContactID = Contact.ContactID";
|
|
|
|
AddSqlWhereString(ref sqlString, ref parameters);
|
|
|
|
sqlString = sqlString + @"
|
|
ORDER BY SaleInvoiceID";
|
|
|
|
// make the call
|
|
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
var invoiceHeaders = conn.Query<Model.Sale.InvoiceHeader>(sqlString, parameters).ToList();
|
|
return invoiceHeaders;
|
|
}
|
|
}
|
|
}
|
|
}
|