Thursday 17 July 2008

Posting an Invoice in C#

In many respects the Sage Data Objects library is much easier to use from VB.NET than C#, why?, because VB.NET deals with object casting in a much less strict way. This way, you can say:

InvItem.Fields.Item("Text").Value = "Invoice Line Text"

Rather than

object TEXT = "Text";
InvItem.Fields.Item(ref TEXT).Value = "Invoice Line Text";

Further more, since the internals of Sage hide many implementation details, you may not know what class an object is, since .NET will only report that it's a ComObject, Nothing more.

In most cases, we'd recommend that you create a stub library in VB.NET to host our component, and provide services to your C# Application, however, if that is not possible, then here is how to carry out a common task in sage, - posting an Invoice

public void CreateInvoice(DateTime InvoiceDate, int InvoiceNumber)
{
SageDataObject50.InvoicePost InvPost;

InvPost = (SageDataObject50.InvoicePost)ws50.CreateObject("InvoicePost");

InvPost.Type =(SageDataObject50.InvoiceType)SageDataObject50.LedgerType.sdoLedgerInvoice;
object Invoice_Number = "Invoice_Number";
object Invoice_Date = "Invoice_Date";

SageDataObject50.IFields ifHeaderFields = null;

Type t = InvPost.Header.GetType();

Object oFields = t.InvokeMember("Fields",
BindingFlags.GetProperty
, null, InvPost.Header, new Object[0]);

ifHeaderFields = (SageDataObject50.IFields)oFields;

ifHeaderFields.Item(ref Invoice_Number).Value = InvoiceNumber;
ifHeaderFields.Item(ref Invoice_Date).Value = InvoiceDate.ToString("MM/dd/yyyy");

t = InvPost.Items.GetType();

Object oItem = t.InvokeMember("Add",
BindingFlags.InvokeMethod,
null,
InvPost.Items,
new Object[0]);

SageDataObject50.InvoiceItem InvItem;
InvItem = (SageDataObject50.InvoiceItem)oItem;

// This is the text for the line
InvItem.Text = "Description";
object TEXT = "Text";
object SERVICE_ITEM_LINES = "Service_Item_Lines";
object NET_AMOUNT = "Net_Amount";
object FULL_NET_AMOUNT = "Full_Net_Amount";
object TAX_AMOUNT = "Tax_Amount";
object NOMINAL_CODE = "Nominal_Code";
object TAX_CODE = "Tax_Code";
object TAX_RATE = "Tax_Rate";

InvItem.Fields.Item(ref TEXT).Value = "Invoice Line Text";
InvItem.Fields.Item(ref SERVICE_ITEM_LINES).Value = (double)1;
InvItem.Fields.Item(ref NET_AMOUNT).Value = (double)100;
InvItem.Fields.Item(ref FULL_NET_AMOUNT).Value = (double)100;
InvItem.Fields.Item(ref TAX_AMOUNT).Value = (double)0;
InvItem.Fields.Item(ref NOMINAL_CODE).Value = "4000";
InvItem.Fields.Item(ref TAX_CODE).Value = "T9";
InvItem.Fields.Item(ref TAX_RATE).Value = (decimal)0;

if (!InvPost.Update())
{
MessageBox.Show("Unable to create invoice");
}
else
{
MessageBox.Show("Invoice Created");
}
}

No comments: