mirror of
https://github.com/stokebob/BealeEngineering.git
synced 2026-03-19 06:37:15 +00:00
167 lines
8.1 KiB
C#
167 lines
8.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 ImportXeroInvoiceFlatFile
|
|
{
|
|
public ImportXeroInvoiceFlatFile()
|
|
{
|
|
// ensure XeroInvoiceFlatFile hasn't changed
|
|
int propertyCount = new Model.Import.XeroInvoiceFlatFile().GetType().GetProperties().Count();
|
|
if (propertyCount != 32)
|
|
{
|
|
throw new Exception("Model.Import.XeroInvoiceFlatFile has changed, it's adapter class may need updating.");
|
|
}
|
|
propertyCount = new Model.Import.XeroInvoiceFlatFile.LineItem().GetType().GetProperties().Count();
|
|
if (propertyCount != 13)
|
|
{
|
|
throw new Exception("Model.Import.XeroInvoiceFlatFile.LineItem has changed, it's adapter class may need updating.");
|
|
}
|
|
}
|
|
|
|
public List<Model.Import.XeroInvoiceFlatFile> SaleInvoice(List<Model.Sale.Invoice> invoiceList)
|
|
{
|
|
if (invoiceList == null || !invoiceList.Any()) { return null; }
|
|
|
|
var xeroInvoiceList = new List<Model.Import.XeroInvoiceFlatFile>();
|
|
for (int i = 0; i < invoiceList.Count(); i++)
|
|
{
|
|
var xeroInvoice = new Model.Import.XeroInvoiceFlatFile();
|
|
|
|
xeroInvoice.ContactName = invoiceList[i].ContactName;
|
|
xeroInvoice.Currency = invoiceList[i].CurrencyCode;
|
|
if (invoiceList[i].DueDateIsSet) { xeroInvoice.DueDate = invoiceList[i].DueDate; }
|
|
xeroInvoice.InvoiceDate = invoiceList[i].InvoiceDate;
|
|
xeroInvoice.InvoiceNumber = invoiceList[i].SaleInvoiceNumber;
|
|
xeroInvoice.Reference = invoiceList[i].Reference;
|
|
xeroInvoice.Status = invoiceList[i].Status;
|
|
xeroInvoice.TaxTotal = invoiceList[i].TaxTotal;
|
|
xeroInvoice.Total = invoiceList[i].InvoiceTotal;
|
|
if (invoiceList[i].IsCreditNote) { xeroInvoice.Type = "Sales credit note"; }
|
|
else { xeroInvoice.Type = "Sales invoice"; }
|
|
|
|
// invoice lines
|
|
if (invoiceList[i].InvoiceLineListIsSet)
|
|
{
|
|
xeroInvoice.LineItemList = new List<Model.Import.XeroInvoiceFlatFile.LineItem>();
|
|
for (int j = 0; j < invoiceList[i].InvoiceLineList.Count(); j++)
|
|
{
|
|
var line = new Model.Import.XeroInvoiceFlatFile.LineItem();
|
|
line.AccountCode = invoiceList[i].InvoiceLineList[j].AccountCode;
|
|
line.Description = invoiceList[i].InvoiceLineList[j].Description;
|
|
line.Discount = invoiceList[i].InvoiceLineList[j].Discount;
|
|
line.InventoryItemCode = invoiceList[i].InvoiceLineList[j].InventoryItemCode;
|
|
line.LineAmount = invoiceList[i].InvoiceLineList[j].LineNetAmount;
|
|
line.Quantity = invoiceList[i].InvoiceLineList[j].Quantity;
|
|
line.TaxAmount = invoiceList[i].InvoiceLineList[j].TaxAmount;
|
|
line.TaxType = invoiceList[i].InvoiceLineList[j].TaxType;
|
|
line.UnitAmount = invoiceList[i].InvoiceLineList[j].UnitAmount;
|
|
|
|
xeroInvoice.LineItemList.Add(line);
|
|
}
|
|
}
|
|
xeroInvoiceList.Add(xeroInvoice);
|
|
}
|
|
return xeroInvoiceList;
|
|
}
|
|
|
|
public List<Model.Import.XeroInvoiceFlatFile> XeroInvoiceFlatFileDTO(List<Model.Import.XeroInvoiceFlatFileDTO> flatData)
|
|
{
|
|
if (flatData == null || !flatData.Any()) { return null; }
|
|
|
|
// ensure flat data is in invoice number order
|
|
var invDictionary = new Dictionary<string, int>();
|
|
string lastUniqueString = "";
|
|
foreach (var line in flatData)
|
|
{
|
|
// invoice number isn't unique, can be duplicated if one invoice is void/deleted
|
|
string uniqueString = line.InvoiceNumber + line.Status;
|
|
if (uniqueString != lastUniqueString)
|
|
{
|
|
lastUniqueString = uniqueString;
|
|
if (invDictionary.ContainsKey(lastUniqueString))
|
|
{
|
|
throw new Exception("Invoices are not grouped in CSV flatfile.");
|
|
}
|
|
else
|
|
{
|
|
invDictionary.Add(lastUniqueString, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
// create the invoice list
|
|
var dictionaryList = new Dictionary<string, Model.Import.XeroInvoiceFlatFile>();
|
|
|
|
for (int i = 0; i < flatData.Count(); i++)
|
|
//foreach (var line in flatData)
|
|
{
|
|
string invoiceNumber = flatData[i].InvoiceNumber;
|
|
|
|
if (!dictionaryList.ContainsKey(invoiceNumber))
|
|
{
|
|
var invoice = new Model.Import.XeroInvoiceFlatFile();
|
|
|
|
invoice.ContactName = flatData[i].ContactName;
|
|
invoice.Currency = flatData[i].Currency;
|
|
invoice.DueDate = flatData[i].DueDate;
|
|
invoice.EmailAddress = flatData[i].EmailAddress;
|
|
invoice.InvoiceAmountDue = flatData[i].InvoiceAmountDue;
|
|
invoice.InvoiceAmountPaid = flatData[i].InvoiceAmountPaid;
|
|
invoice.InvoiceDate = flatData[i].InvoiceDate;
|
|
invoice.InvoiceNumber = flatData[i].InvoiceNumber;
|
|
invoice.PlannedDate = flatData[i].PlannedDate;
|
|
invoice.POAddressLine1 = flatData[i].POAddressLine1;
|
|
invoice.POAddressLine2 = flatData[i].POAddressLine2;
|
|
invoice.POAddressLine3 = flatData[i].POAddressLine3;
|
|
invoice.POAddressLine4 = flatData[i].POAddressLine4;
|
|
invoice.POCity = flatData[i].POCity;
|
|
invoice.POCountry = flatData[i].POCountry;
|
|
invoice.POPostalCode = flatData[i].POPostalCode;
|
|
invoice.PORegion = flatData[i].PORegion;
|
|
invoice.Reference = flatData[i].Reference;
|
|
invoice.SAAddressLine1 = flatData[i].SAAddressLine1;
|
|
invoice.SAAddressLine2 = flatData[i].SAAddressLine2;
|
|
invoice.SAAddressLine3 = flatData[i].SAAddressLine3;
|
|
invoice.SAAddressLine4 = flatData[i].SAAddressLine4;
|
|
invoice.SACity = flatData[i].SACity;
|
|
invoice.SACountry = flatData[i].SACountry;
|
|
invoice.SAPostalCode = flatData[i].SAPostalCode;
|
|
invoice.SARegion = flatData[i].SARegion;
|
|
invoice.Sent = flatData[i].Sent;
|
|
invoice.Status = flatData[i].Status;
|
|
invoice.TaxTotal = flatData[i].TaxTotal;
|
|
invoice.Total = flatData[i].Total;
|
|
invoice.Type = flatData[i].Type;
|
|
|
|
dictionaryList.Add(invoiceNumber, invoice);
|
|
}
|
|
|
|
var item = new Model.Import.XeroInvoiceFlatFile.LineItem();
|
|
|
|
item.AccountCode = flatData[i].AccountCode;
|
|
item.Description = flatData[i].Description;
|
|
item.Discount = flatData[i].Discount;
|
|
item.InventoryItemCode = flatData[i].InventoryItemCode;
|
|
item.LineAmount = flatData[i].LineAmount;
|
|
item.Quantity = flatData[i].Quantity;
|
|
item.TaxAmount = flatData[i].TaxAmount;
|
|
item.TaxType = flatData[i].TaxType;
|
|
item.TrackingName1 = flatData[i].TrackingName1;
|
|
item.TrackingName2 = flatData[i].TrackingName2;
|
|
item.TrackingOption1 = flatData[i].TrackingOption1;
|
|
item.TrackingOption2 = flatData[i].TrackingOption2;
|
|
item.UnitAmount = flatData[i].UnitAmount;
|
|
|
|
dictionaryList[invoiceNumber].LineItemList.Add(item);
|
|
}
|
|
|
|
return dictionaryList.Values.ToList();
|
|
}
|
|
}
|
|
}
|