Files
Common-VS2019/SacsStandardAccounts/Accounts/QuickBooks/Base/SaQuickBooksDataItem.cs
T

200 lines
6.5 KiB
C#

using Sarand.Net.Standard.Accounts.QuickBooks.App;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Bus;
using System;
using System.Collections.Generic;
using System.Text;
namespace Sarand.Net.Standard.Accounts.QuickBooks.Base
{
//=======================================================================
public class SaQuickBooksDataItem : SacsDisposable
{
//-------------------------------------------------------------------
// Data members.
private readonly NamedValue.Map values = new NamedValue.Map();
//-------------------------------------------------------------------
// Static initialisation.
static SaQuickBooksDataItem ()
{
}
//-------------------------------------------------------------------
// Construction.
protected SaQuickBooksDataItem (Qbk.EntityType entityType)
{
EntityType = entityType;
}
protected override void Dispose (bool disposing)
{
if (disposing)
{
}
}
//-------------------------------------------------------------------
// Properties.
public Qbk.EntityType EntityType { get; }
//-------------------------------------------------------------------
// Field values.
public bool HasValue (String name)
{
return values.HasValue(name);
}
public DataValue.DataType GetValueType (String name)
{
DataValue value = GetValue(name);
return value?.GetDataType() ?? DataValue.DataType.Unknown;
}
public DataValue GetValue (String name)
{
return values.GetValue(name);
}
public NamedValue.Map GetValues ()
{
return new NamedValue.Map(values.Values);
}
//-------------------------------------------------------------------
// Update attributes.
public bool SetValue (String name, DataValue value)
{
return values.AddValue(name, value);
}
public bool SetValue (String name, String value)
{
return SetValue(name, new DataValue(value));
}
public bool SetValue (String name, bool value)
{
return SetValue(name, new DataValue(value));
}
public bool SetValue (String name, long value)
{
return SetValue(name, new DataValue(value));
}
public bool SetValue (String name, double value)
{
return SetValue(name, new DataValue(value));
}
//-------------------------------------------------------------------
// P a r a m e t e r s
//-------------------------------------------------------------------
/// <summary>Data item parameters.</summary>
public class Params
{
/// <summary>Online: EntityId.</summary>
public String EntityListId { get; set; } = "";
/// <summary>Item identifier (ListId etc, partial = 'starts with').</summary>
public String ItemId { get; set; } = "";
/// <summary>Item identifier (ListId etc, partial = 'starts with').</summary>
public String ItemIdPartial { get; set; } = "";
public String EntityCode { get; set; } = "";
/// <summary>Item name or description (partial = 'starts with').</summary>
public String EntityName { get; set; } = "";
/// <summary>Item name or description (partial = 'starts with').</summary>
public String EntityNamePartial { get; set; } = "";
/// <summary>FOB (invoice) filter.</summary>
public String Fob { get; set; } = "";
/// <summary>Avoid processing older items (orders etc.).</summary>
public DateTime EarliestDate { get; set; } = DateTime.MinValue;
public int MaxItems { get; set; } = 0;
}
//-------------------------------------------------------------------
// I t e r a t o r
//-------------------------------------------------------------------
public abstract class Iterator : SaErrorStateItem
{
//---------------------------------------------------------------
// Data members.
private SaQuickBooksDataItem.Params prmQbk;
private int itemCount = 0;
//---------------------------------------------------------------
// Construction.
protected Iterator (Qbk.EntityType entityType)
{
EntityType = entityType;
}
protected override void Dispose (bool disposing)
{
if (disposing)
{
}
base.Dispose(disposing);
}
//---------------------------------------------------------------
// Properties.
public Qbk.EntityType EntityType { get; }
public SaQuickBooksDataItem.Params Params { get { return prmQbk; } }
public static int PageLengthDefault { get; } = 20;
public int ItemsFound { get; protected set; }
public bool AtStart { get; protected set; }
//---------------------------------------------------------------
// Data items.
/// <summary>Total number of items (do this after GotoStart).</summary>
public virtual int GetItemCount ()
{
ClearErrorState();
if (itemCount < 0 && AtStart)
{
// Retrieve and discard the first item in the list.
SaQuickBooksDataItem item = GetNextItem();
if (item != null) item.Dispose();
// Reset to start of list.
ItemsFound = 0;
}
return 0;
}
/// <summary>Reset the item iteration.</summary>
public virtual void GotoStart (SaQuickBooksDataItem.Params prmQbk)
{
ClearErrorState();
this.prmQbk = prmQbk;
AtStart = true;
ItemsFound = 0;
}
public abstract SaQuickBooksDataItem GetNextItem ();
/// <summary>Retrieve specific item (exception if more than one found).</summary>
public SaQuickBooksDataItem GetItem (SaQuickBooksDataItem.Params prmQbk)
{
ClearErrorState();
Iterator iterator = Clone();
iterator.GotoStart(prmQbk);
SaQuickBooksDataItem item1 = iterator.GetNextItem();
SaQuickBooksDataItem item2 = iterator.GetNextItem();
if (item2 != null)
{
// Only 1 item expected.
if (item1 != null) item1.Dispose(); item1 = null;
if (item2 != null) item2.Dispose(); item2 = null;
SetErrorState("Multiple items found");
}
return item1;
}
/// <summary>List of matching items (use where volume is limited).</summary>
public List<SaQuickBooksDataItem> GetItems (SaQuickBooksDataItem.Params prmQbk)
{
ClearErrorState();
List<SaQuickBooksDataItem> items = new List<SaQuickBooksDataItem>();
Iterator iterator = Clone();
iterator.GotoStart(prmQbk);
SaQuickBooksDataItem item = null;
while ((item = iterator.GetNextItem()) != null)
items.Add(item);
return items;
}
//---------------------------------------------------------------
// Utilities.
protected abstract Iterator Clone ();
//---------------------------------------------------------------
}
//-------------------------------------------------------------------
}
//=======================================================================
}