Imports Telerik.Pivot.Core
Imports Telerik.Pivot.Core.Aggregates

Public Class CommissionCalculatedField
    Inherits CalculatedField
    Private extendPriceField As RequiredField

    Public Sub New()
        Me.Name = "Commission"
        Me.extendPriceField = RequiredField.ForProperty("ExtendedPrice")
    End Sub

    Protected Overrides Function RequiredFields() As IEnumerable(Of RequiredField)
        Return New List(Of RequiredField) From {extendPriceField}
    End Function

    Protected Overrides Function CalculateValue(aggregateValues As IAggregateValues) As AggregateValue
        Dim aggregateValue = aggregateValues.GetAggregateValue(Me.extendPriceField)

        If AggregateValueExtensions.IsError(aggregateValue) Then
            Return aggregateValue

        End If

        Dim extendedPrice As Double = AggregateValueExtensions.ConvertOrDefault(Of Double)(aggregateValue)
        If extendedPrice > 15000 Then
            Return New DoubleAggregateValue(extendedPrice * 0.1)
        End If

        Return Nothing
    End Function
End Class