mirror of
https://github.com/stokebob/BealeEngineering.git
synced 2026-03-21 15:47:15 +00:00
226 lines
10 KiB
C#
226 lines
10 KiB
C#
using Dapper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Transactions;
|
|
|
|
namespace BealeEngineering.Core.Data.Database.Sale
|
|
{
|
|
public class UpdateInvoice : Connection
|
|
{
|
|
private int recordsCreated = 0;
|
|
private int recordsUpdated = 0;
|
|
private int invoicesProcessed = 0;
|
|
|
|
public UpdateInvoice(string sqlConnectionString) : base(sqlConnectionString)
|
|
{
|
|
|
|
}
|
|
public int RecordsCreated { get { return recordsCreated; } }
|
|
public int RecordsUpdated { get { return recordsUpdated; } }
|
|
public int InvoicesProcessed { get { return invoicesProcessed; } }
|
|
public void ByInvoice(Model.Sale.Invoice invoice, bool insertNew = false)
|
|
{
|
|
var invoiceList = new List<Model.Sale.Invoice> { invoice };
|
|
ByInvoiceList(invoiceList, insertNew);
|
|
}
|
|
public void ByInvoiceList(List<Model.Sale.Invoice> invoiceList, bool insertNew = false)
|
|
{
|
|
recordsCreated = 0;
|
|
recordsUpdated = 0;
|
|
invoicesProcessed = 0;
|
|
|
|
// check the list
|
|
if (invoiceList == null || !invoiceList.Any()) { return; }
|
|
else { invoicesProcessed = invoiceList.Count(); }
|
|
|
|
CheckList(ref invoiceList);
|
|
|
|
using (TransactionScope scope = new TransactionScope())
|
|
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
// get list of id from db
|
|
var dic = new Dictionary<string, int>();
|
|
string sqlString = @"
|
|
SELECT SaleInvoiceNumber, SaleInvoiceID
|
|
FROM saleInvoice
|
|
WHERE SaleInvoiceNumber IN @saleInvoiceNumber;";
|
|
|
|
var parameters = new DynamicParameters();
|
|
parameters.Add("@saleInvoiceNumber", invoiceList.Select(o => o.SaleInvoiceNumber).ToList());
|
|
using (var reader = conn.ExecuteReader(sqlString, parameters))
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
if (!dic.ContainsKey(reader.GetString(0)))
|
|
{
|
|
dic.Add(reader.GetString(0), reader.GetInt32(1));
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var invoice in invoiceList)
|
|
{
|
|
int saleInvoiceId = 0;
|
|
if (dic.ContainsKey(invoice.SaleInvoiceNumber))
|
|
{
|
|
saleInvoiceId = dic[invoice.SaleInvoiceNumber];
|
|
sqlString = @"
|
|
UPDATE SaleInvoice
|
|
SET
|
|
[ContactID] = (SELECT ContactID FROM Contact WHERE ContactName = @contactName)
|
|
,[SaleInvoiceNumber] = @saleInvoiceNumber
|
|
,[InvoiceDate] = @invoiceDate
|
|
,[DateDue] = @dateDue
|
|
,[Reference] = @reference
|
|
,[CurrencyCode] = @currencyCode
|
|
,[InvoiceTotal] = @invoiceTotal
|
|
,[TaxTotal] = @taxTotal
|
|
,[IsCreditNote] = @isCreditNote
|
|
WHERE SaleInvoiceID = @saleInvoiceId;
|
|
|
|
DELETE FROM SaleInvoiceLine WHERE SaleInvoiceID = @saleInvoiceId;";
|
|
}
|
|
else if (insertNew)
|
|
{
|
|
sqlString = @"
|
|
INSERT INTO SaleInvoice(
|
|
[ContactID]
|
|
,[SaleInvoiceNumber]
|
|
,[InvoiceDate]
|
|
,[DateDue]
|
|
,[Reference]
|
|
,[CurrencyCode]
|
|
,[InvoiceTotal]
|
|
,[TaxTotal]
|
|
,[IsCreditNote]
|
|
)
|
|
OUTPUT INSERTED.SaleInvoiceID
|
|
VALUES(
|
|
(SELECT ContactID FROM Contact WHERE ContactName = @contactName)
|
|
@saleInvoiceNumber
|
|
@invoiceDate
|
|
@dateDue
|
|
@reference
|
|
@currencyCode
|
|
@invoiceTotal
|
|
@taxTotal
|
|
@isCreditNote
|
|
)";
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@contactName", invoice.ContactName);
|
|
cmd.Parameters.AddWithValue("@saleInvoiceNumber", invoice.SaleInvoiceNumber);
|
|
cmd.Parameters.AddWithValue("@invoiceDate", invoice.InvoiceDate);
|
|
if (invoice.DueDate == null) { cmd.Parameters.AddWithValue("@dateDue", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@dateDue", invoice.DueDate); }
|
|
cmd.Parameters.AddWithValue("@reference", invoice.Reference);
|
|
cmd.Parameters.AddWithValue("@currencyCode", invoice.CurrencyCode);
|
|
cmd.Parameters.AddWithValue("@invoiceTotal", invoice.InvoiceTotal);
|
|
cmd.Parameters.AddWithValue("@taxTotal", invoice.TaxTotal);
|
|
cmd.Parameters.AddWithValue("@isCreditNote", invoice.IsCreditNote);
|
|
|
|
if (saleInvoiceId == 0)
|
|
{
|
|
saleInvoiceId = (int)cmd.ExecuteScalar();
|
|
recordsCreated += 1;
|
|
}
|
|
else
|
|
{
|
|
cmd.Parameters.AddWithValue("@saleInvoiceId", saleInvoiceId);
|
|
int effected = cmd.ExecuteNonQuery();
|
|
recordsUpdated += 1;
|
|
}
|
|
}
|
|
|
|
// deal with lines
|
|
foreach (var line in invoice.InvoiceLineList)
|
|
{
|
|
sqlString = @"
|
|
INSERT INTO SaleInvoiceLine(
|
|
[SaleInvoiceID]
|
|
,[LineNumber]
|
|
,[InventoryItemCode]
|
|
,[Description]
|
|
,[Quantity]
|
|
,[UnitAmount]
|
|
,[Discount]
|
|
,[AccountCode]
|
|
,[TaxType]
|
|
,[TaxAmount]
|
|
,[LineAmount]
|
|
)
|
|
VALUES(
|
|
@saleInvoiceID
|
|
,@lineNumber
|
|
,@inventoryItemCode
|
|
,@description
|
|
,@quantity
|
|
,@unitAmount
|
|
,@discount
|
|
,@accountCode
|
|
,@taxType
|
|
,@taxAmount
|
|
,@lineAmount
|
|
)";
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sqlString, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@saleInvoiceID", saleInvoiceId);
|
|
cmd.Parameters.AddWithValue("@lineNumber", line.LineNumber);
|
|
if (string.IsNullOrWhiteSpace(line.InventoryItemCode)) { cmd.Parameters.AddWithValue("@inventoryItemCode", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@inventoryItemCode", line.InventoryItemCode); }
|
|
if (string.IsNullOrWhiteSpace(line.Description)) { cmd.Parameters.AddWithValue("@description", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@description", line.Description); }
|
|
cmd.Parameters.AddWithValue("@quantity", line.Quantity);
|
|
cmd.Parameters.AddWithValue("@unitAmount", line.UnitAmount);
|
|
if (line.Discount == null) { cmd.Parameters.AddWithValue("@discount", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@discount", line.Discount); }
|
|
if (string.IsNullOrWhiteSpace(line.AccountCode)) { cmd.Parameters.AddWithValue("@accountCode", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@accountCode", line.AccountCode); }
|
|
if (string.IsNullOrWhiteSpace(line.TaxType)) { cmd.Parameters.AddWithValue("@taxType", DBNull.Value); }
|
|
else { cmd.Parameters.AddWithValue("@taxType", line.TaxType); }
|
|
cmd.Parameters.AddWithValue("@taxAmount", line.TaxAmount);
|
|
cmd.Parameters.AddWithValue("@lineAmount", line.LineAmount);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
if (InvoicesProcessed == (RecordsCreated + RecordsUpdated))
|
|
{
|
|
scope.Complete();
|
|
}
|
|
else
|
|
{
|
|
scope.Dispose();
|
|
throw new Exception("Error updating invoices, DB transaction has been rolled back");
|
|
}
|
|
}
|
|
}
|
|
private void CheckList(ref List<Model.Sale.Invoice> invoiceList)
|
|
{
|
|
var tempDic = new Dictionary<string, int>();
|
|
foreach (var item in invoiceList)
|
|
{
|
|
if (tempDic.ContainsKey(item.SaleInvoiceNumber))
|
|
{ throw new Exception("Duplicate contact name in list."); }
|
|
else { tempDic.Add(item.SaleInvoiceNumber, 0); }
|
|
if (!item.IsValid())
|
|
{ throw new Exception("Validation check failed: " + item.ValidationResults[0]); }
|
|
}
|
|
}
|
|
}
|
|
}
|