Programmer:Stock Item

From AutoCount Resource Center
Jump to navigation Jump to search

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 1.8

BCE.AutoCount.dll
BCE.AutoCount.CommonAccounting.dll
BCE.AutoCount.MainEntry.dll
BCE.Utils.dll
BCE.Utils.UI.dll
BCE.AutoCount.StockMaint.dll

Load data with API

Single Record

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

public void ExampleOfSingleItemRecord(string itemCode, string uom, BCE.Data.DBSetting dbset)
{
    BCE.AutoCount.Data.ItemRecord itemRec = BCE.AutoCount.Data.CommonRecordUtils.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 = BCE.Data.Convert.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 = BCE.Data.Convert.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, BCE.Data.DBSetting dbset)
{
    //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 = dbset.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
    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbset);
    BCE.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
GetItemDataByItemGroup(new string[] { "FINISHED", "RAW" }, mydbset)



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, BCE.Data.DBSetting dbset)
{
    //Get ItemCode List that is edited (modified) after a specific date
    DataTable tblItemCode = dbset.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
    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbset);
    BCE.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, BCE.Data.DBSetting dbset)
{
    BCE.AutoCount.Stock.StockBalance.StockBalanceHelper sbHelper = new BCE.AutoCount.Stock.StockBalance.StockBalanceHelper(dbset);

    //Filter the stock balance to show location from "PJ" to "PJ"
    sbHelper.Criteria.LocationFilter.Type = BCE.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;
}
  • On AutoCount Accounting 1.8.31.188, Master table consists of below columns
    Location, ItemCode, UOM, BatchNo, Balance, StandardCost, TotalCost, Rate, Shelf, ItemGroup,
    ItemType, Description, Desc2, ReportUOM, LocationDesc, LocationDesc2, CostingMethodString, ExpiryDate, ManufacturedDate,
    AverageCost, SmallestBalQty
  • 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 advisable that Item Group is assigned to an item. See Item Group.

Method 1 : Simple assign value to new Item

public void NewStockItem(BCE.Data.DBSetting dbSetting)
{
    //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;
    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbSetting);
    BCE.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 NewStockItem(BCE.Data.DBSetting dbSetting)
{
    //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;

    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbSetting);
    BCE.AutoCount.Stock.Item.ItemEntity itemEntity = cmd.NewItem();
    BCE.AutoCount.Stock.Item.ItemUomEntity uomEntity;

    //Refer to "Auto Generate ItemCode for New Item with ItemCodeHelper" in this page
    itemEntity.ItemCode = GetAutoItemCodeByItemGroup("FINISHED", 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(BCE.Data.DBSetting dbSetting)
{
    //If true, cost of this item will be recalculated upon saving
    bool recalculate = true;

    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbSetting);
    BCE.AutoCount.Stock.Item.ItemEntity itemEntity;
    BCE.AutoCount.Stock.Item.ItemUomEntity uomEntity;

    itemEntity = cmd.LoadItem("FG001", BCE.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, BCE.Data.DBSetting dbSetting)
{
    //If true, cost of this item will be recalculated upon saving
    bool recalculate = false;

    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbSetting);
    BCE.AutoCount.Stock.Item.ItemEntity itemEntity = cmd.LoadItem("FG001", BCE.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, BCE.Data.DBSetting dbSetting)
{
    //If true, cost of this item will be recalculated upon saving
    bool recalculate = false;

    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(dbSetting);
    BCE.AutoCount.Stock.Item.ItemEntity itemEntity = cmd.LoadItem("FG001", BCE.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, BCE.Data.DBSetting dbSetting)
{
    BCE.AutoCount.Stock.Item.ItemDataAccess cmd = BCE.AutoCount.Stock.Item.ItemDataAccess.Create(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, BCE.Data.DBSetting dbSetting)
{
    string itemCodeFormat = GetDefaultItemCodeFormat(dbSetting);
    BCE.AutoCount.Stock.Item.ItemCodeHelper itemHelper = BCE.AutoCount.Stock.Item.ItemCodeHelper.Create(dbSetting);
    return itemHelper.GenerateItemCode(itemGroup, null, itemCodeFormat);
}

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

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

Default Item Code Format

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

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

See Also

Go to menu

IconAC81.png Go to top
ProgrammerGo.jpg Resources For AutoCount Software Developers