Programmer:Stock Item v2

From AutoCount Resource Center

Technical Specification

  1. Default ItemCode size = 30
  2. Item must have at least one UOM
  3. Must have base UOM, which the UOM rate is 1.
  4. When create new item, the first UOM's Rate must be equal to 1, before adding UOM's Rate that is greater than 1.
  5. Though AutoCount Accounting supports more than one UOM with the rate of 1, there can only be one base uom.
  6. Rename ItemCode at ItemDataAccess or when Edit the item is not allow.
    • Must Implement Change Item Code to rename the ItemCode
  7. Item cannot be deleted, when the item is used in documents.

References of AutoCount Accounting version 2.0

AutoCount.Accounting.dll
AutoCount.Accounting.UI.dll
AutoCount.dll
AutoCount.MainEntry.dll
AutoCount.UI.dll
AutoCount.StockMaint.dll

Load data with API

Single Record

Load one item by specifying "ItemCode" and "UOM".

public void ExampleOfSingleItemRecord(string itemCode, string uom, AutoCount.Data.DBSetting dbset)
{
    AutoCount.Data.ItemRecord itemRec = AutoCount.Data.RecordUtils.GetItem(dbset, itemCode, uom);
    if (itemRec != null)
    {
        string description = itemRec.Description;
        string desc2 = itemRec.Desc2;
        string furtherDesc = itemRec.FurtherDescription;
        string defaultSaleUOM = itemRec.SalesUOM;
        string defaultPurchaseUOM = itemRec.PurchaseUOM;
        decimal? sellingPrice = itemRec.Price;
        decimal? standardCost = itemRec.Cost;
        bool isStockControl = itemRec.StockControl;
        bool isActive = itemRec.IsActive;

        //Data that is of UDF object requires to perform conversion to appropriate datatype
        string state = itemRec.UDF["State"].ToString();
        decimal commissionRate = AutoCount.Converter.ToDecimal(itemRec.UDF["COMMRATE"]);

        //itemRec.UDF of boolean type returns "T" or "F";
        //Must use TextToBoolean(object) method to convert the UDF of boolean type to bool.
        bool calcCommission = AutoCount.Converter.TextToBoolean(itemRec.UDF["CALCCOMM"]);
    }
}

Multiple items with Filter

Load Items on selection of Item Groups

Load multiple items that filter item of selected Item Groups.

Below coding requires to add directive of:-
using System.Data;
using System.Linq;
public DataTable GetItemDataByItemGroup(string[] itemGroups, AutoCount.Authentication.UserSession userSession)
{
    //Build Sql List of Item Group
    string itemGroupInSql = BuildItemGroupInSql(itemGroups);

    //Construct Sql Select string with selection of Item Groups
    string sqlSelectItemInItemGroup = string.Format("SELECT ItemCode FROM Item WHERE IsActive='T' AND ItemGroup IN ({0})", itemGroupInSql);

    //Get Item Code List in a table from Sql Server
    DataTable tblItemCode = userSession.DBSetting.GetDataTable(sqlSelectItemInItemGroup, false);

    //Convert Table of ItemCode to an Array of string
    string[] itemArray = tblItemCode.AsEnumerable().Select(r => r.Field<string>("ItemCode")).ToArray();

    //Create ItemDataAccess object,
    //and call LoadAllItem(string[]) method to filter ItemCode
    AutoCount.Stock.Item.ItemDataAccess cmd = AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntities items = cmd.LoadAllItem(itemArray);

    //items.ItemTable consists of UOM in single table
    return items.ItemTable;
}
private string BuildItemGroupInSql(string[] itemGroups)
{
    //Generate and return a Sql string of ItemGroup
    //If ItemGroups string array is null or empty, the return is "SELECT Item FROM LIST('')"
    return string.Format("SELECT Item FROM LIST('{0}')",
        itemGroups.Aggregate("",
        (g1, g2) => string.IsNullOrEmpty(g2)
        ? g1
        : string.IsNullOrEmpty(g1) ? g2 : string.Format("{0},{1}", g1, g2)
    ));
}
  • To call the method of GetItemDataByItemGroup
//mySession is the object of AutoCount.Authentication.UserSession
GetItemDataByItemGroup(new string[] { "FINISHED", "RAW" }, mySession)



Load Items that was last updated on specific date

Load multiple items that filter item that has been updated since a specific date.

Below coding requires to add directive of:-
using System.Data;
using System.Linq;
public DataTable GetModifiedItemData(DateTime filterModifiedDate, AutoCount.Authentication.UserSession userSession)
{
    //Get ItemCode List that is edited (modified) after a specific date
    DataTable tblItemCode = userSession.DBSetting.GetDataTable("SELECT ItemCode FROM Item WHERE IsActive='T' AND LastModified>=?", false, filterModifiedDate);
    string[] itemArray = tblItemCode.AsEnumerable().Select(r => r.Field<string>("ItemCode")).ToArray();

    //Create ItemDataAccess object,
    //and call LoadAllItem(string[]) method to filter ItemCode
    AutoCount.Stock.Item.ItemDataAccess cmd = AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntities items = cmd.LoadAllItem(itemArray);

    //items.ItemTable consists of UOM in single table
    return items.ItemTable;
}


Stock Balance (Quantity & Costing) Table and with filter location

public System.Data.DataTable GetItemLocationBalanceData(DateTime filterOnDate, AutoCount.Authentication.UserSession userSession)
{
    AutoCount.Stock.StockBalance.StockBalanceHelper sbHelper = new AutoCount.Stock.StockBalance.StockBalanceHelper(userSession);

    //Filter the stock balance to show location from "PJ" to "PJ"
    sbHelper.Criteria.LocationFilter.Type = AutoCount.SearchFilter.FilterType.ByRange;
    sbHelper.Criteria.LocationFilter.From = "PJ";
    sbHelper.Criteria.LocationFilter.To = "PJ";

    sbHelper.Inquire(filterOnDate);

    //sbHelper.ResultDataSet returns 2 tables
    //1. Master (Item Balance)
    //2. ItemSerialNo (Serial Number)
    //System.Data.DataSet dsBalanceWithSerial = sbHelper.ResultDataSet;

    return sbHelper.ResultTable;
}

Return of sbHelper.ResultTable is shown as below. (One table is split to 2 images)

  • Balance is the balance quantity of the UOM of this (current) row.
  • SmallestBalQty is the total quantity of all UOMs in smallest uom (base uom in Item Maintenance).
  • Get Stock Item Costing...Click here.


Stock Item API Usage

New

  • It is advised that Item Group is assigned to item. See Item Group.

Method 1 : Simple assign value to new Item

public void NewStockItem(AutoCount.Authentication.UserSession userSession)
{
    //If true, cost of this item will be recalculated upon saving
    //Since this is a new item, so there is no transactions to be recalculated.
    bool recalculate = false;
    AutoCount.Stock.Item.ItemDataAccess cmd =
        AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntity itemEntity = cmd.NewItem();

    itemEntity.ItemCode = "FG002";

    //ItemGroup must already exist in "Item Group Maintenance"
    itemEntity.ItemGroup = "FINISHED";
    itemEntity.Description = "Finished Goods 002";

    //0 : Fixed Cost
    //1 : Weighted Average
    //2 : FIFO
    //3 : LIFO
    //If not define, default Costing method will be assigned to this item
    itemEntity.CostingMethod = 2;

    itemEntity.BaseUomRecord.Uom = "UNIT";
    itemEntity.BaseUomRecord.StandardCost = 80;
    itemEntity.BaseUomRecord.StandardSellingPrice = 100;

    cmd.SaveData(itemEntity, ref recalculate);
}

Method 2 : Auto generate ItemCode

  • This example shows get auto generate ItemCode and multiple UOMs
public void NewStockItemWithMultiUOM(AutoCount.Authentication.UserSession userSession)
{
    //If true, cost of this item will be recalculated upon saving
    //Since this is a new item, so there is no transactions to be recalculated.
    bool recalculate = false;

    AutoCount.Stock.Item.ItemDataAccess cmd =
        AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntity itemEntity = cmd.NewItem();
    AutoCount.Stock.Item.ItemUomEntity uomEntity;

    //Refer to "Auto Generate ItemCode for New Item with ItemCodeHelper" in this page
    itemEntity.ItemCode = GetAutoItemCodeByItemGroup("FINISHED", userSession.DBSetting);

    //ItemGroup must already exist in "Item Group Maintenance"
    itemEntity.ItemGroup = "FINISHED";
    itemEntity.Description = "Finished Goods 001";

    itemEntity.CostingMethod = 1;

    //Sale Tax and Purchase Tax can be bound to this item
    itemEntity.TaxType = "SR-S";
    itemEntity.PurchaseTaxType = "TX-S";

    //UOM
    //While creating new item, system will auto initiate a new UOM.
    //The value of this UOM, is taken from "Default UOM",
    //which it is maintained in Tools | Option, under Stock | General Stock Setting
    //Next line will remove the new auto created uom, before adding my uom(s).
    itemEntity.DeleteItemUom(0);

    //The rate of first UOM must be 1
    //The first UOM that is rate 1 will be auto assign to BaseUOM of this item

    //Assuming from here is a loop to create UOMs
    uomEntity = itemEntity.NewUom("UNIT", 1);
    uomEntity.StandardCost = 50;
    uomEntity.StandardSellingPrice = 100;

    uomEntity = itemEntity.NewUom("PCS", 1);
    uomEntity.StandardCost = 52;
    uomEntity.StandardSellingPrice = 100;

    uomEntity = itemEntity.NewUom("BOX", 6);
    uomEntity.StandardCost = 150;
    uomEntity.StandardSellingPrice = 300;
    //End of adding UOM
    //The result of the above uom:
    //Total of 3 uoms added to this item
    //There are two uom with Rate = 1
    //"UNIT" is the base uom

    cmd.SaveData(itemEntity, ref recalculate);
}

Edit

public void ChangeStockItemPricing(AutoCount.Authentication.UserSession userSession)
{
    //If true, cost of this item will be recalculated upon saving
    bool recalculate = true;

    AutoCount.Stock.Item.ItemDataAccess cmd =
        AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntity itemEntity;
    AutoCount.Stock.Item.ItemUomEntity uomEntity;

    itemEntity = cmd.LoadItem("FG001", AutoCount.Stock.Item.ItemEntryAction.Edit);

    //Load the UOM of this Item, where Cost and Price are stored
    uomEntity = itemEntity.GetUomByName("UNIT");
    uomEntity.StandardCost = 45;

    //Calculate Standard Selling Price base on Standard Cost
    //Eg. Set the Selling Price at 80% of the cost
    uomEntity.StandardSellingPrice = uomEntity.StandardCost * 1.8M;

    cmd.SaveData(itemEntity, ref recalculate);
}

Discontinue an Item

public void SetItemDiscontinue(bool discontinue, AutoCount.Authentication.UserSession userSession)
{
    //If true, cost of this item will be recalculated upon saving
    bool recalculate = false;

    AutoCount.Stock.Item.ItemDataAccess cmd =
        AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntity itemEntity = cmd.LoadItem("FG001", AutoCount.Stock.Item.ItemEntryAction.Edit);

    //Discontinued = true, this item no longer can be selected in purchase documents 
    itemEntity.Discontinued = discontinue;

    cmd.SaveData(itemEntity, ref recalculate);
}

Inactive an Item

public void SetItemActive(bool active, AutoCount.Authentication.UserSession userSession)
{
    //If true, cost of this item will be recalculated upon saving
    bool recalculate = false;

    AutoCount.Stock.Item.ItemDataAccess cmd =
        AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    AutoCount.Stock.Item.ItemEntity itemEntity = cmd.LoadItem("FG001", AutoCount.Stock.Item.ItemEntryAction.Edit);

    //IsActive = false, this item no longer can be selected at all entry form
    itemEntity.IsActive = active;

    cmd.SaveData(itemEntity, ref recalculate);
}

Delete

public void DeleteStock(string itemCode, AutoCount.Authentication.UserSession userSession)
{
    AutoCount.Stock.Item.ItemDataAccess cmd = AutoCount.Stock.Item.ItemDataAccess.Create(userSession, userSession.DBSetting);
    cmd.Delete(itemCode);
}

Auto Generate ItemCode for New Item with ItemCodeHelper

  • Auto generate item code depends on a valid Item Code Format setting.
  • Item Code Format is maintained in Tools | Options, go to Stock | General Stock Setting.
  • One of the 3 methods below can be used when generates item code.
    1. Get an auto generate item code with the short code of Item Group
    2. Get an auto generate item code with the short code of Item Type
    3. Get an auto generate item code with the short code of both Item Group and Item Type
public string GetAutoItemCodeByItemGroup(string itemGroup, AutoCount.Data.DBSetting dbSetting)
{
    string itemCodeFormat = GetDefaultItemCodeFormat(dbSetting);
    AutoCount.Stock.Item.ItemCodeHelper itemHelper =
        AutoCount.Stock.Item.ItemCodeHelper.Create(dbSetting);
    return itemHelper.GenerateItemCode(itemGroup, null, null, null, null, itemCodeFormat);
}

public string GetAutoItemCodeByItemType(string itemType, AutoCount.Data.DBSetting dbSetting)
{
    string itemCodeFormat = GetDefaultItemCodeFormat(dbSetting);
    AutoCount.Stock.Item.ItemCodeHelper itemHelper = AutoCount.Stock.Item.ItemCodeHelper.Create(dbSetting);
    return itemHelper.GenerateItemCode(null, itemType, null, null, null, itemCodeFormat);
}

public string GetAutoItemCodeByItemGroupAndItemType(string itemGroup, string itemType, AutoCount.Data.DBSetting dbSetting)
{
    string itemCodeFormat = GetDefaultItemCodeFormat(dbSetting);
    AutoCount.Stock.Item.ItemCodeHelper itemHelper = AutoCount.Stock.Item.ItemCodeHelper.Create(dbSetting);
    return itemHelper.GenerateItemCode(itemGroup, itemType, null, null, null, itemCodeFormat);
}

Default Item Code Format

Return the text formatting of ItemCode when the formatting is required by auto generate ItemCode

private string GetDefaultItemCodeFormat(AutoCount.Data.DBSetting dbSetting)
{
    return AutoCount.Data.DBRegistry.Create(dbSetting)
        .GetString(new AutoCount.RegistryID.ItemCodeFormat());
}

See Also

Go to menu

Go to top
Resources For AutoCount Software Developers