Tuesday, March 13, 2012

Order Details: Update “Extended Price” Field

When you create a new order detail, the Extended Price field will not be calculated and will only display N/A until you save the record.

'Extended Price' Field not updating in web application

Let’s make the field automatically update and provide instantaneous feedback to the user. Switch back to the Designer, and double-click on OrderDetails / Fields / ExtendedPrice node.

Change the following settings:

Property Text
The value of this field is calculated by a business rule expression True
Code Formula
Convert.ToDecimal(unitPrice) * Convert.ToDecimal(quantity) * (1 - Convert.ToDecimal(discount))
Context Fields ProductID, UnitPrice, Quantity, Discount

'Code Formula' in Code On Time Designer
'Context Fields' in Code On Time Designer

Press OK to save the field. Generate the application, and wait for the Preview to load. Select an order, and create a new order detail. Select a product, and you will see that the Extended Price field has been calculated. Change any field and shift focus, and the Extended Price field will be recalculated every time.

Updating 'Extended Price' Field in Code On Time web application

To handle this calculation, the web application generator created a business rules class. It can be found in the project’s App_Code/Rules folder.

C#:

namespace MyCompany.Rules
{
    public partial class OrderDetailsBusinessRules : MyCompany.Data.BusinessRules
    {
        
        [ControllerAction("OrderDetails", "Calculate", "ExtendedPrice")]
        public void CalculateOrderDetails(int? orderID, string orderCustomerID, 
            string orderCustomerCompanyName, string orderEmployeeLastName, 
            string orderShipViaCompanyName, int? productID, 
            string productProductName, string productCategoryCategoryName, 
            string productSupplierCompanyName, decimal? unitPrice, 
            short? quantity, float? discount)
        {
            UpdateFieldValue("ExtendedPrice", Convert.ToDecimal(unitPrice) * 
                Convert.ToDecimal(quantity) * (1 - Convert.ToDecimal(discount)));
        }
        
        [RowBuilder("OrderDetails", RowKind.New)]
        public void BuildNewOrderDetails()
        {
            UpdateFieldValue("Quantity", 1);
            UpdateFieldValue("Discount", 0);
        }
    }
}

VB:

Namespace MyCompany.Rules
    
    Partial Public Class OrderDetailsBusinessRules
        Inherits MyCompany.Data.BusinessRules
        
        <ControllerAction("OrderDetails", "Calculate", "ExtendedPrice")> _
        Public Sub CalculateOrderDetails(ByVal orderID As Nullable(Of Integer),
                                         ByVal orderCustomerID As String,
                                         ByVal orderCustomerCompanyName As String,
                                         ByVal orderEmployeeLastName As String,
                                         ByVal orderShipViaCompanyName As String,
                                         ByVal productID As Nullable(Of Integer),
                                         ByVal productProductName As String,
                                         ByVal productCategoryCategoryName As String,
                                         ByVal productSupplierCompanyName As String,
                                         ByVal unitPrice As Nullable(Of Decimal),
                                         ByVal quantity As Nullable(Of Short),
                                         ByVal discount As Nullable(Of Single))
            UpdateFieldValue("ExtendedPrice", Convert.ToDecimal(unitPrice) *
                             Convert.ToDecimal(quantity) *
                             (1 - Convert.ToDecimal(discount)))
        End Sub
        
        <RowBuilder("OrderDetails", RowKind.New)>  _
        Public Sub BuildNewOrderDetails()
            UpdateFieldValue("Quantity", 1)
            UpdateFieldValue("Discount", 0)
        End Sub
    End Class
End Namespace

No comments:

You can find more about Code OnTime Generator, Data Aquarium Framework, and other great products here.


© 2010 Code OnTime LLC. Intelligent code generation software for ASP.NET. Visit us at http://codeontime.com