Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Public Enum eInsuranceState
    eNothing = 0
    eByCustomers = 1
    eByCustomersFrom = 2
End Enum
<DefaultClassOptions(), NavigationItem(False), CreatableItem(False)> _
<DefaultProperty("DisplayName")> _
Public Class APCSheetByProducts 'Agricultural Production Contracts termék sorok 
    Inherits BaseObject

    Private _RowIndex As Long
    Private _APCSheetHeader As APCSheetHeader
    Private _Products As Products 'Termék
    Private _InsuranceState As eInsuranceState 'Ki fizeti a biztosítást
    Private _ContractQuantity As Double 'Adott termékre szerződött terület ha
    Private _MaxContractQuantity As Double 'Max szerződhető mennyiség hektáronként
    Private _FundingHatoHUF As Double 'Egységre jutó finanszírozás /ha /tonna
    Private _FundingHaHUF As Double 'Egységre jutó finanszírozás /ha
    Private _FundingAmountHUF As Double 'Finanszírozás összesen
    Private _FundingAmountUsedHUF As Double 'Ebből felhasználva összesen
    Private _FundingAmountAvailableHUF As Double 'Még felhasználható összesen
    Private _ColleteralHatoHUF As Double 'Egységre jutó fedezeti érték /ha /tonna
    Private _CollateralhaHUF As Double 'Egységre jutó fedezeti érték /ha
    Private _CollateralAmountHUF As Double 'Fedezeti érték összesen
    Private _FundingDate As Date 'Fizetési határidő az elvitt termékre vonatkozólag
    Private _ProductExpirationDate As Date 'Lejárati dátum
    Private _Contracts As Contracts 'Szerződés
    Private _ContractsSignatureDate As Date 'Szerződés aláírásásnak dátuma

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
    End Sub

    Property RowIndex As Long
        Get
            Return _RowIndex
        End Get
        Set(value As Long)
            SetPropertyValue("RowIndex", _RowIndex, value)
        End Set
    End Property
    <Association("APCSheetHeader-APCSheetByProducts", GetType(APCSheetHeader)), VisibleInListView(False)> _
    Property APCSheetHeader As APCSheetHeader
        Get
            Return _APCSheetHeader
        End Get
        Set(value As APCSheetHeader)
            SetPropertyValue("APCSheetHeader", _APCSheetHeader, value)
        End Set
    End Property
    Property Products As Products
        Get
            Return _Products
        End Get
        Set(value As Products)
            SetPropertyValue("Products", _Products, value)
        End Set
    End Property
    Property InsuranceState As eInsuranceState
        Get
            Return _InsuranceState
        End Get
        Set(value As eInsuranceState)
            SetPropertyValue("InsuranceState", _InsuranceState, value)
        End Set
    End Property
    Property ContractQuantity As Double
        Get
            Return _ContractQuantity
        End Get
        Set(value As Double)
            SetPropertyValue("ContractQuantity", _ContractQuantity, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                FundingAmountHUF = value * FundingHaHUF
                CollateralAmountHUF = value * MaxContractQuantity * ColleteralHatoHUF
                FundingAmountAvailableHUF = FundingAmountHUF - FundingAmountUsedHUF
            End If
        End Set
    End Property
    Property MaxContractQuantity As Double
        Get
            Return _MaxContractQuantity
        End Get
        Set(value As Double)
            SetPropertyValue("MaxContractQuantity", _MaxContractQuantity, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                FundingHaHUF = value * FundingHatoHUF
                CollateralAmountHUF = value * ContractQuantity * ColleteralHatoHUF
            End If
        End Set
    End Property
    Property FundingHatoHUF As Double
        Get
            Return _FundingHatoHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FundingHatoHUF", _FundingHatoHUF, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                FundingHaHUF = value * MaxContractQuantity
            End If
        End Set
    End Property
    Property FundingHaHUF As Double
        Get
            Return _FundingHaHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FundingHaHUF", _FundingHaHUF, value)
        End Set
    End Property
    Property FundingAmountHUF As Double
        Get
            Return _FundingAmountHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FundingAmountHUF", _FundingAmountHUF, value)
        End Set
    End Property
    Property FundingAmountUsedHUF As Double
        Get
            Return _FundingAmountUsedHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FundingAmountUsedHUF", _FundingAmountUsedHUF, value)
        End Set
    End Property
    Property FundingAmountAvailableHUF As Double
        Get
            Return _FundingAmountAvailableHUF
        End Get
        Set(value As Double)
            SetPropertyValue("FundingAmountAvailableHUF", _FundingAmountAvailableHUF, value)
        End Set
    End Property
    Property ColleteralHatoHUF As Double
        Get
            Return _ColleteralHatoHUF
        End Get
        Set(value As Double)
            SetPropertyValue("ColleteralHatoHUF", _ColleteralHatoHUF, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                CollateralAmountHUF = value * MaxContractQuantity * ContractQuantity
                CollateralhaHUF = value * MaxContractQuantity
            End If
        End Set
    End Property
    Property CollateralhaHUF As Double
        Get
            Return _CollateralhaHUF
        End Get
        Set(value As Double)
            SetPropertyValue("CollateralhaHUF", _CollateralhaHUF, value)
        End Set
    End Property
    Property CollateralAmountHUF As Double
        Get
            Return _CollateralAmountHUF
        End Get
        Set(value As Double)
            SetPropertyValue("CollateralAmountHUF", _CollateralAmountHUF, value)
        End Set
    End Property
    Property FundingDate As Date
        Get
            Return _FundingDate
        End Get
        Set(value As Date)
            SetPropertyValue("FundingDate", _FundingDate, value)
        End Set
    End Property
    Property ProductExpirationDate As Date
        Get
            Return _ProductExpirationDate
        End Get
        Set(value As Date)
            SetPropertyValue("ProductExpirationDate", _ProductExpirationDate, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    Property Contracts As Contracts
        Get
            Return _Contracts
        End Get
        Set(value As Contracts)
            SetPropertyValue("Contracts", _Contracts, value)
        End Set
    End Property
    Property ContractsSignatureDate As Date
        Get
            Return _ContractsSignatureDate
        End Get
        Set(value As Date)
            SetPropertyValue("ContractsSignatureDate", _ContractsSignatureDate, value)
        End Set
    End Property

    ReadOnly Property DisplayName As String
        Get
            If APCSheetHeader IsNot Nothing Then
                If _Products IsNot Nothing Then
                    Return Me.APCSheetHeader.DocumentNumber & ", " & Me.Products.ShortName & ", " & Format(Me.FundingAmountHUF, "#,##0")
                Else
                    Return Me.APCSheetHeader.DocumentNumber & ", " & Format(Me.FundingAmountHUF, "#,##0")
                End If
            Else
                Return Nothing
            End If
        End Get
    End Property

    <VisibleInListView(False), VisibleInDetailView(False)> _
    ReadOnly Property SumContractQuantity As Double 'Összes szerződött mennyiség
        Get
            Return Math.Round(ContractQuantity * MaxContractQuantity, 2, MidpointRounding.AwayFromZero)
        End Get
    End Property
    <VisibleInListView(False), VisibleInDetailView(False)> _
    ReadOnly Property FundingHaHUF_Text_HUF As String 'Egységre jutó finanszírozás /ha szövegesen magyarul
        Get
            Return GeneralFunction.GetHUNumberToText(Math.Round(FundingHaHUF, 0, MidpointRounding.AwayFromZero))
        End Get
    End Property
    <VisibleInListView(False), VisibleInDetailView(False)> _
    ReadOnly Property FundingAmountHUF_Text_HUF As String 'Finanszírozás összesen szövegesen magyarul
        Get
            Return GeneralFunction.GetHUNumberToText(Math.Round(FundingAmountHUF, 0, MidpointRounding.AwayFromZero))
        End Get
    End Property
    '//XPCollection a felhasznált tételekről
    <VisibleInListView(False), VisibleInLookupListView(False), VisibleInDetailView(False)> _
    ReadOnly Property OrderInRows As XPCollection(Of OrderInRows)
        Get
            Return New XPCollection(Of OrderInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, Session,
                                                    CriteriaOperator.Parse("APCSheetByProducts=? and IsNull(GCRecord)=True and IsNull(OrderInHeader)=False", Me))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False), VisibleInDetailView(False)> _
    ReadOnly Property OrderInRowsOther As XPCollection(Of OrderInRowsOther)
        Get
            Return New XPCollection(Of OrderInRowsOther)(PersistentCriteriaEvaluationBehavior.InTransaction, Session,
                                                         CriteriaOperator.Parse("APCSheetByProducts=? and IsNull(GCRecord)=True and IsNull(OrderInHeader)=False", Me))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False), VisibleInDetailView(False)> _
    ReadOnly Property InvoiceRows As XPCollection(Of InvoiceRows)
        Get
            Return New XPCollection(Of InvoiceRows)(PersistentCriteriaEvaluationBehavior.InTransaction, Session,
                                                    CriteriaOperator.Parse("(OrderInRows.APCSheetByProducts=? or OrderInRowsOther.APCSheetByProducts=?) and IsNull(GCRecord)=True and IsNull(InvoiceHeader)=False and InvoiceHeader.IsEditable=False and InvoiceHeader.DocumentNumber !=''", Me, Me))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False), VisibleInDetailView(False)> _
    ReadOnly Property DeliveryNoteOutRows As XPCollection(Of DeliveryNoteOutRows)
        Get
            Return New XPCollection(Of DeliveryNoteOutRows)(PersistentCriteriaEvaluationBehavior.InTransaction, Session,
                                                            CriteriaOperator.Parse("StorageOutRowsInRows.StorageOutRows.OrderInRows.APCSheetByProducts=? and IsNull(GCRecord)=True and IsNull(DeliveryNoteOutHeader)=False and DeliveryNoteOutHeader.IsEditable=False", Me))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False), VisibleInDetailView(False)> _
    ReadOnly Property StorageOutRowsInRows As XPCollection(Of StorageOutRowsInRows)
        Get
            Return New XPCollection(Of StorageOutRowsInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, Session,
                                                             CriteriaOperator.Parse("StorageOutRows.OrderInRows.APCSheetByProducts=? and IsNull(GCRecord)=True and IsNull(StorageOutRows.OrderInRows.OrderInHeader)=False and IsNull(StorageOutRows.StorageOutHeader)=False", Me))
        End Get
    End Property
End Class
