mirror of
https://github.com/stokebob/BealeEngineering.git
synced 2026-03-19 06:37:15 +00:00
83 lines
4.1 KiB
C#
83 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BealeEngineering.Core.Logic.Adapter
|
|
{
|
|
public class SaleInvoice
|
|
{
|
|
public SaleInvoice()
|
|
{
|
|
// ensure sale invoice hasn't changed
|
|
int propertyCount = new Model.Sale.Invoice().GetType().GetProperties().Count();
|
|
if (propertyCount != 14)
|
|
{
|
|
throw new Exception("Model.Import.XeroInvoiceFlatFile has changed, it's adapter class may need updating.");
|
|
}
|
|
propertyCount = new Model.Sale.Invoice.InvoiceLine().GetType().GetProperties().Count();
|
|
if (propertyCount != 10)
|
|
{
|
|
throw new Exception("Model.Import.XeroInvoiceFlatFile.LineItem has changed, it's adapter class may need updating.");
|
|
}
|
|
}
|
|
|
|
public Model.Sale.Invoice XeroInvoiceFlatFIle(Model.Import.XeroInvoiceFlatFile xeroInvoice)
|
|
{
|
|
var result = XeroInvoiceFlatFile(new List<Model.Import.XeroInvoiceFlatFile> { xeroInvoice });
|
|
if (result == null) { return null; }
|
|
else { return result[0]; }
|
|
}
|
|
|
|
public List<Model.Sale.Invoice> XeroInvoiceFlatFile(List<Model.Import.XeroInvoiceFlatFile> xeroInvoiceList)
|
|
{
|
|
var dbInvoiceData = new List<Model.Sale.Invoice>();
|
|
for (var i = 0; i < xeroInvoiceList.Count; i++)
|
|
{
|
|
if (xeroInvoiceList[i] == null) { throw new NullReferenceException(); }
|
|
var invoice = new Model.Sale.Invoice();
|
|
|
|
invoice.ContactName = xeroInvoiceList[i].ContactName;
|
|
invoice.CurrencyCode = xeroInvoiceList[i].Currency;
|
|
invoice.DueDate = xeroInvoiceList[i].DueDate;
|
|
invoice.InvoiceDate = xeroInvoiceList[i].InvoiceDate;
|
|
invoice.InvoiceTotal = xeroInvoiceList[i].Total;
|
|
if (xeroInvoiceList[i].Type == "Sales invoice") { invoice.IsCreditNote = false; }
|
|
else if (xeroInvoiceList[i].Type == "Sales credit note") { invoice.IsCreditNote = true; }
|
|
else { throw new FormatException("Unknow value '" + xeroInvoiceList[i].Type + "' found in 'Type' field"); }
|
|
invoice.Reference = xeroInvoiceList[i].Reference;
|
|
invoice.SaleInvoiceNumber = xeroInvoiceList[i].InvoiceNumber;
|
|
invoice.Status = xeroInvoiceList[i].Status;
|
|
invoice.TaxTotal = xeroInvoiceList[i].TaxTotal;
|
|
|
|
invoice.InvoiceLineList = new List<Model.Sale.Invoice.InvoiceLine>();
|
|
for (int j = 0; j < xeroInvoiceList[i].LineItemList.Count; j++)
|
|
{
|
|
if (xeroInvoiceList[i].LineItemList[j] == null) { throw new NullReferenceException(); }
|
|
var invoiceLine = new Model.Sale.Invoice.InvoiceLine();
|
|
|
|
invoiceLine.LineNumber = j + 1;
|
|
invoiceLine.AccountCode = xeroInvoiceList[i].LineItemList[j].AccountCode;
|
|
invoiceLine.Description = xeroInvoiceList[i].LineItemList[j].Description;
|
|
invoiceLine.Discount = xeroInvoiceList[i].LineItemList[j].Discount;
|
|
invoiceLine.InventoryItemCode = xeroInvoiceList[i].LineItemList[j].InventoryItemCode;
|
|
invoiceLine.Quantity = xeroInvoiceList[i].LineItemList[j].Quantity;
|
|
invoiceLine.TaxAmount = xeroInvoiceList[i].LineItemList[j].TaxAmount;
|
|
invoiceLine.TaxType = xeroInvoiceList[i].LineItemList[j].TaxType;
|
|
invoiceLine.UnitAmount = xeroInvoiceList[i].LineItemList[j].UnitAmount;
|
|
|
|
//check, as line amount is same as calculated in model
|
|
if (invoiceLine.LineAmount != xeroInvoiceList[i].LineItemList[j].LineAmount)
|
|
{
|
|
throw new Exception("Imported line total does not equal caluclated.");
|
|
}
|
|
invoice.InvoiceLineList.Add(invoiceLine);
|
|
}
|
|
dbInvoiceData.Add(invoice);
|
|
}
|
|
return dbInvoiceData;
|
|
}
|
|
}
|
|
}
|