BeforeSave: Difference between revisions

From AutoCount Resource Center
Content added Content deleted
mNo edit summary
No edit summary
Line 1: Line 1:
==BeforeSave==
==BeforeSave==
'''BeforeSave''' is triggered when user clicked [Save] button.
'''BeforeSave''' is triggered when user clicked [Save] button.
There are many tasks programmer can do in the event of '''BeforeSave'''.<br/>
The special feature of BeforeSave is that it allows programmer to abort the process of saving that has been started, on given condition.<br />
The special feature of BeforeSave is that it allows programmer to abort the process of saving that has been started, on given condition.<br />
This event is useful for addition checking or verify user input, beside system default checking.
This event is useful for addition checking or verify user input, beside system default checking.
Line 6: Line 7:
Both cases are illustrated in examples below.
Both cases are illustrated in examples below.


===OnDetailColumnChanged event in Sales Invoice===
==OnDetailColumnChanged event in Sales Invoice==
<syntaxhighlight lang="csharp">
<syntaxhighlight lang="csharp">
public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
</syntaxhighlight>
</syntaxhighlight>


===Example===
==Example==
===Prompt user to decide whether to Post to Stock in Sale Invoice===
*''Empty Project'' and ''Zero Unit Price'' checking, in BeforeSave
<syntaxhighlight lang="csharp">
public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
{
e.MasterRecord.PostToStock = BCE.Application.AppMessage.ShowConfirmMessage("Post to stock?");
}
</syntaxhighlight>

===''Empty Project'' and ''Zero Unit Price'' checking, in BeforeSave===
<syntaxhighlight lang="csharp" highlight="12,17">
<syntaxhighlight lang="csharp" highlight="12,17">
public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
public void BeforeSave(BCE.AutoCount.Invoicing.Sales.Invoice.InvoiceBeforeSaveEventArgs e)
Line 38: Line 47:


<br />
<br />
*Generate a string that summarize total of Quantity and UOM, then update '''Master.UDF["UOMPACK"]''', in BeforeSave
===Generate a string that summarize total of Quantity and UOM, then update '''Master.UDF["UOMPACK"]''', in BeforeSave===
<syntaxhighlight lang="csharp" highlight="14">
<syntaxhighlight lang="csharp" highlight="14">
private Dictionary<string, decimal> summaryUOM = new Dictionary<string, decimal>();
private Dictionary<string, decimal> summaryUOM = new Dictionary<string, decimal>();

Revision as of 04:29, 7 August 2018

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.
Both cases are illustrated in examples below.

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, in 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"], in 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