BeforeSave

From AutoCount Resource Center

BeforeSave

BeforeSave is triggered when user clicked [Save] button.
There are many tasks programmer can do in the event of BeforeSave.
The special feature of BeforeSave is that it allows programmer to abort the process of saving that has been started, on given condition.
This event is useful for addition checking or verify user input, beside system default checking. Addition to this, programmer often want to do a summarizing just before the document is saved.

OnDetailColumnChanged event in Sales Invoice

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)

Example

Prompt user to decide whether to Post to Stock in Sale Invoice

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
{
    e.MasterRecord.PostToStock = BCE.Application.AppMessage.ShowConfirmMessage("Post to stock?");
}

Empty Project and Zero Unit Price checking at BeforeSave

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
{
    BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceDetailRecord dtlRec;

    for (int i=0; i<e.MasterRecord.DetailCount; i++)
    {
        dtlRec = e.MasterRecord.GetDetailRecord(i);

        //Check Project is empty using HasValue property
        if (!dtlRec.ProjNo.HasValue)
        {
            e.ErrorMessage = string.Format("Empty ProjNo at record {0} is not allowed.", i+1);
        }
        //Check UnitPrice is empty and UnitPrice amount is greater than zero
        else if (!(dtlRec.UnitPrice.HasValue && dtlRec.UnitPrice > 0))
        {
            e.ErrorMessage = string.Format("Unit Price at record {0} must be greater than 0.", i+1);
        }
    }
}

Assign string to e.ErrorMessage has triggered e.AbortSave(). It is not necessary to call e.AbortSave() again after assign error message.


Generate a string that summarize total of Quantity and UOM, then update Master.UDF["UOMPACK"] at BeforeSave

private Dictionary<string, decimal> summaryUOM = new Dictionary<string, decimal>();

public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
{
    BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceDetailRecord dtlRec;
    summaryUOM.Clear();

    for (int i=0; i < e.MasterRecord.DetailCount; i++)
    {
        dtlRec = e.MasterRecord.GetDetailRecord(i);
        UpdateUOMQtyList(dtlRec.UOM, dtlRec.Qty);
    }

    e.MasterRecord.UDF["UOMPACK"] = GetSummaryOfUOMQty(e.DBSetting);
}

private void UpdateUOMQtyList(string uom, decimal qty)
{
    if (summaryUOM.ContainsKey(uom))
    {
        summaryUOM[uom] += qty;
    }
    else
    {
        summaryUOM.Add(uom, qty);
    }
}

private string GetSummaryOfUOMQty(DBSetting dbset)
{
    BCE.AutoCount.Settings.DecimalSetting formatting = BCE.AutoCount.Settings.DecimalSetting.GetOrCreate(dbset);
    return summaryUOM.Select(s =>
            string.Format("{0} {1}", formatting.FormatQuantity(s.Value), s.Key))
        .Aggregate((str1, str2) => str1 + "; " + str2);
}

Result sample:
10 CTN; 15 BOX
13 UNIT; 5 PCS; 5 KG

Go to menu

Go to top
Resources For AutoCount Software Developers