Skip to content

Instantly share code, notes, and snippets.

@ashga
Created February 18, 2014 10:58
Show Gist options
  • Save ashga/9068723 to your computer and use it in GitHub Desktop.
Save ashga/9068723 to your computer and use it in GitHub Desktop.
SageDataObject190.SDOEngine oSDO = new SageDataObject190.SDOEngine();
SageDataObject190.WorkSpace oWS;
SageDataObject190.SalesRecord oSalesRecord;
//SageDataObject190.SopRecord oSopRecord;
SageDataObject190.SopItem oSopItem;
//SageDataObject190.SopItem oSopItem_Write;
SageDataObject190.SopPost oSopPost;
SageDataObject190.StockRecord oStockRecord;
//oWS = (SageDataObject190.WorkSpace)oSDO.Workspaces.Add("Example");
//Leaving the username and password blank generates a login dialog
//Instantiate WorkSpace
oWS = (SageDataObject190.WorkSpace)oSDO.Workspaces.Add("Example");
string szDataPath = oSDO.SelectCompany("C:\\Documents and Settings\\All Users\\Application Data\\Sage\\Accounts\\2013\\");
oWS.Connect(szDataPath, "manager", "", "SDO EXAMPLE");
//Instantiate objects
oSalesRecord = (SageDataObject190.SalesRecord)oWS.CreateObject("SalesRecord");
oStockRecord = (SageDataObject190.StockRecord)oWS.CreateObject("StockRecord");
oSopPost = (SageDataObject190.SopPost)oWS.CreateObject("SopPost");
//Read the first customer
//oSalesRecord.MoveFirst();
if (oSalesRecord.AddNew())
{
oSalesRecord.Fields.Item("ACCOUNT_REF").Value = order.SageAccountRef;
oSalesRecord.Fields.Item("NAME").Value = order.CustomerCompanyName;
oSalesRecord.Fields.Item("ADDRESS_1").Value = order.CustomerAddress1;
oSalesRecord.Fields.Item("ADDRESS_2").Value = order.CustomerAddress2;
oSalesRecord.Fields.Item("ADDRESS_3").Value = order.CustomerCity;
oSalesRecord.Fields.Item("ADDRESS_4").Value = order.CustomerRegion;
oSalesRecord.Fields.Item("ADDRESS_5").Value = order.CustomerPostCode;
oSalesRecord.Fields.Item("TELEPHONE").Value = order.CustomerTelephoneNumber;
oSalesRecord.Fields.Item("DEF_NOM_CODE").Value = Convert.ToString("4000");
}
foreach (var item in order.SalesOrderItems)
{
oSopItem = (SageDataObject190.SopItem)SDOHelper.Add(oSopPost.Items);
SDOHelper.Write(oSopItem, "STOCK_CODE", item.PartNo);
SDOHelper.Write(oSopItem, "DESCRIPTION", item.ShortDesc);
oSopItem.Fields.Item("DESCRIPTION").Value = item.ShortDesc;
SDOHelper.Write(oSopItem, "NOMINAL_CODE", "4000");
SDOHelper.Write(oSopItem, "TAX_CODE", (Int16)20.00);
//Populate other fields required for SOP Item
//From 2013 the update method now wraps internal business logic
//that calculates the vat amount if a net amount is given.
//If you wish to calculate your own Tax values you will need
//to ensure that you set the TAX_FLAG to 1 and set the TAX_AMOUNT value on the item line
//***Note if a NVD is set the item line values will be recalculated
//regardless of the Tax_Flag being set to 1***
SDOHelper.Write(oSopItem, "QTY_ORDER", item.Quantity);
SDOHelper.Write(oSopItem, "UNIT_PRICE", item.UnitPrice);
SDOHelper.Write(oSopItem, "NET_AMOUNT", item.TotalPrice);
SDOHelper.Write(oSopItem, "FULL_NET_AMOUNT", item.TotalPriceVat);
//SDOHelper.Write(oSopItem, "COMMENT_1", (String)"");
//SDOHelper.Write(oSopItem, "COMMENT_2", (String)"");
SDOHelper.Write(oSopItem, "UNIT_OF_SALE", (String)"1");
//SDOHelper.Write(oSopItem, "TAX_RATE", (Double)20);
//SDOHelper.Write(oSopItem, "TAX_CODE", (Int16)1);
}
//Update the SO
if (oSopPost.Update())
{
Console.WriteLine("Sales Order Created Successfully");
}
else
{
Console.WriteLine("Sales Order Not Created");
}
//Disconnect
oWS.Disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment