mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 14:37:16 +00:00
* complete read invoices from db * wip * wip * wip * wip * wip * wip * wip * wip * updated nuget package spapi * WIP * wip, now test * wip, jut need to fix tax inclusive line amounts not supported * wip * wip, before I f everything up * no, it complies now, this is the one before I f everything up * wip * wip * wip, logic ready for testing * wip it builds!!!! * wip tested, working, need to complete the gui section * wip * wip * wip - created export invoice data delete, time for testing * wip testing phase * wip - delete function fully tested and working * wip on to sorting out the issue with settlement invoices not tallying * wip * wip * wip * wip * wip before I complete change the ReadInvoiceLineItem sections * that appears to have worked, on with the main quest * no it's doesn't work, saving before i remove the confusing cache system (just use a dictionary!!) * wipping picadilli * wip * wip * implemented uow on inovice export, now for testing * wip * wip all tested do invoice currency convertion fearure * wip * pretty much done so long as xero accepts the exported invoices * Complete!
179 lines
7.6 KiB
C#
179 lines
7.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.ComponentModel.Design.ObjectSelectorEditor;
|
|
|
|
namespace bnhtrade.Core.Data.Database.Account
|
|
{
|
|
internal class ReadPurchaseInvoice : Connection
|
|
{
|
|
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
|
|
|
|
public IEnumerable<int> PurchaseInvoiceIdList { get; set; }
|
|
|
|
public ReadPurchaseInvoice()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
sqlBuilder = new SqlWhereBuilder();
|
|
PurchaseInvoiceIdList = new List<int>();
|
|
}
|
|
|
|
public Dictionary<int, Model.Account.PurchaseInvoice> Read()
|
|
{
|
|
var returnList = new Dictionary<int, Model.Account.PurchaseInvoice>();
|
|
sqlBuilder.Init();
|
|
|
|
//build sql query
|
|
string sql = @"
|
|
SELECT tblPurchase.PurchaseID
|
|
,tblPurchase.PurchaseNumber
|
|
,tblPurchase.RecordID
|
|
,tblPurchase.PurchaseDate
|
|
,tblPurchase.ContactID
|
|
,tblPurchase.SupplierRef
|
|
,tblPurchase.PurchaseTotalAmount
|
|
,tblPurchase.VatInclusiveAmounts
|
|
,tblPurchase.RecordCreated
|
|
,tblPurchase.RecordModified
|
|
,tblPurchase.IsActive
|
|
,tblAccountCurrency.CurrencyCode
|
|
,tblPurchaseChannel.PurchaseChannelName
|
|
,tblPurchaseStatus.PurchaseStatus
|
|
FROM tblPurchase
|
|
LEFT OUTER JOIN tblAccountCurrency ON tblPurchase.AccountCurrencyID = tblAccountCurrency.AccountCurrencyID
|
|
LEFT OUTER JOIN tblPurchaseStatus ON tblPurchase.PurchaseStatusID = tblPurchaseStatus.PurchaseStatusID
|
|
LEFT OUTER JOIN tblPurchaseChannel ON tblPurchase.PurchaseChannelID = tblPurchaseChannel.PurchaseChannelID
|
|
WHERE 1 = 1
|
|
";
|
|
|
|
// build the where statments
|
|
if (PurchaseInvoiceIdList.Any())
|
|
{
|
|
sqlBuilder.In("[PurchaseID]", PurchaseInvoiceIdList, "AND");
|
|
}
|
|
|
|
// append where string to the sql
|
|
if (sqlBuilder.IsSetSqlWhereString)
|
|
{
|
|
sql = sql + sqlBuilder.SqlWhereString;
|
|
}
|
|
|
|
// dictionary so we can fill in details afterwards
|
|
var invoiceContactDict = new Dictionary<int, int>();
|
|
var purchaseIdList = new List<int>();
|
|
|
|
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
|
{
|
|
conn.Open();
|
|
|
|
using (SqlCommand cmd = new SqlCommand(sql, conn))
|
|
{
|
|
sqlBuilder.AddParametersToSqlCommand(cmd);
|
|
|
|
using (SqlDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
|
|
|
|
int purchaseID = reader.GetInt32(0);
|
|
int purchaseNumber = reader.GetInt32(1);
|
|
int? recordID = null;
|
|
if (!reader.IsDBNull(2)) { recordID = reader.GetInt32(2); }
|
|
DateTime purchaseDate = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc);
|
|
int? contactID = null;
|
|
if (!reader.IsDBNull(4)) { contactID = reader.GetInt32(4);}
|
|
string supplierRef = null;
|
|
if (!reader.IsDBNull(5)) { supplierRef = reader.GetString(5);}
|
|
decimal? purchaseTotalAmount = null;
|
|
if (!reader.IsDBNull(6)) { purchaseTotalAmount = reader.GetDecimal(6);}
|
|
bool vatInclusiveAmounts = reader.GetBoolean(7);
|
|
DateTime recordCreated = DateTime.SpecifyKind(reader.GetDateTime(8), DateTimeKind.Utc);
|
|
DateTime recordModified = DateTime.SpecifyKind(reader.GetDateTime(9), DateTimeKind.Utc);
|
|
bool isActive = reader.GetBoolean(10);
|
|
string currencyCode = reader.GetString(11);
|
|
string purchaseChannelName = reader.GetString(12);
|
|
string purchaseStatus = null;
|
|
if (!reader.IsDBNull(13)) { purchaseStatus = reader.GetString(13);}
|
|
|
|
var invoice = new Model.Account.PurchaseInvoice();
|
|
invoice.PurchaseID = purchaseID;
|
|
invoice.PurchaseNumber = purchaseNumber;
|
|
invoice.RecordID = recordID;
|
|
invoice.PurchaseDate = purchaseDate;
|
|
invoice.SupplierRef = supplierRef;
|
|
//invoice.PurchaseTotalAmount = purchaseTotalAmount;
|
|
invoice.VatInclusiveAmounts = vatInclusiveAmounts;
|
|
invoice.RecordCreated = recordCreated;
|
|
invoice.RecordModified = recordModified;
|
|
invoice.IsActive = isActive;
|
|
invoice.CurrencyCode = currencyCode;
|
|
invoice.PurchaseChannel = purchaseChannelName;
|
|
|
|
// is there contact info that needs to be added?
|
|
if (contactID != null)
|
|
{
|
|
invoiceContactDict.Add(purchaseID, (int)contactID);
|
|
}
|
|
|
|
purchaseIdList.Add(purchaseID);
|
|
|
|
returnList.Add(purchaseID, invoice);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// add contact info
|
|
if (invoiceContactDict.Any())
|
|
{
|
|
var readContact = new Data.Database.Account.ReadContact();
|
|
readContact.ContactIdList = invoiceContactDict.Values.ToList();
|
|
var contactDict = readContact.Read();
|
|
|
|
if (contactDict.Any())
|
|
{
|
|
foreach ( var invoice in returnList)
|
|
{
|
|
if (invoiceContactDict.ContainsKey(invoice.Value.PurchaseID))
|
|
{
|
|
int contactId = invoiceContactDict[invoice.Value.PurchaseID];
|
|
invoice.Value.Contact = contactDict[contactId];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// add invoice lines
|
|
var readLines = new Data.Database.Account.ReadPurchaseInvoiceLine();
|
|
readLines.InvoiceIdList = purchaseIdList;
|
|
var lines = readLines.Read();
|
|
foreach(var invoice in returnList.Values)
|
|
{
|
|
foreach(var line in lines.Values)
|
|
{
|
|
if (line.PurchaseId == invoice.PurchaseID)
|
|
{
|
|
if (invoice.InvoiceLines == null)
|
|
{
|
|
invoice.InvoiceLines = new List<Model.Account.PurchaseInvoice.Line>();
|
|
}
|
|
invoice.InvoiceLines.Add(line);
|
|
}
|
|
}
|
|
}
|
|
|
|
return returnList;
|
|
}
|
|
}
|
|
}
|