Imports System
Imports System.ComponentModel

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports System.Linq
Imports System.Linq.Expressions
Public Enum eOrderOutState
    InProgress = 0
    Started = 1
    Closed = 2
End Enum
'-- Kimenő megrendelés ---------------------------------------------------------------------------------------------------
Public Enum eOrderOutMainType
    eFromProducts = 0 'Terméket rendelünk meg
    eFromInstallation = 1 'Gyártmányt rendelünk meg
    eFromServices = 2 'Szolgáltatást rendelünk meg
End Enum

'<EditorStateRuleAttribute("OrderOutHeader Disable properties if !IsEditable ", "*", ViewType.DetailView, "!IsEditable", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable CustomersOrderDocuments when is not empty", "CustomersOrderDocuments", ViewType.DetailView, "IsNull(CustomersOrderDocuments) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable Contacts when is not empty", "Contacts", ViewType.DetailView, "IsNull(Contacts) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable CustomersFrom when is not empty", "CustomersFrom", ViewType.DetailView, "IsNull(CustomersFrom) = False", EditorState.Disabled)> _
'<EditorStateRuleAttribute("Disable CustomersOrder when is not empty", "CustomersOrder", ViewType.DetailView, "IsNull(CustomersOrder) = False", EditorState.Disabled)> _
'<EditorStateRule("OrderOutHeader.DocumentNumber", "DocumentNumber", ViewType.DetailView, "1=1", EditorState.Disabled)> _

'<Appearance("OrderOutHeader all property disable", AppearanceItemType.LayoutItem, "IsEditable=False", TargetItems:="OrderOutHeader_DetailView", visibility:=Editors.ViewItemVisibility.Hide)> _
'<Appearance("OrdersOutHeader.TotalNettoHUF Hide", AppearanceItemType.ViewItem, "CurrencyName.ShortName != 'HUF'", TargetItems:="TotalNettoHUF", visibility:=Editors.ViewItemVisibility.Hide)> _
<DefaultClassOptions()> _
<NavigationItem("BusinessTransactions")> _
<Appearance("OrderOutHeader Disable properties if !IsEditable", TargetItems:="*", Criteria:="!IsEditable", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable CustomersOrderDocuments when is not empty", TargetItems:="CustomersOrderDocuments", Criteria:="IsNull(CustomersOrderDocuments) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable Contacts when is not empty", TargetItems:="Contacts", Criteria:="IsNull(Contacts) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable CustomersFrom when is not empty", TargetItems:="CustomersFrom", Criteria:="IsNull(CustomersFrom) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("Disable CustomersOrder when is not empty", TargetItems:="CustomersOrder", Criteria:="IsNull(CustomersOrder) = False", Enabled:=False, Context:="DetailView")> _
<Appearance("OrderOutHeader.DocumentNumber", TargetItems:="DocumentNumber", Criteria:="1=1", Enabled:=False, Context:="DetailView")> _
<Appearance("OrdersOutHeader.TotalNettoDEV Hide", AppearanceItemType.ViewItem, "CustomersOrderDocuments.CurrencyName.ShortName = 'HUF'", TargetItems:="row_ListPriceDEV", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("OrdersOutHeader.TotalNettoHUF Hide", AppearanceItemType.ViewItem, "CustomersOrderDocuments.CurrencyName.ShortName != 'HUF'", TargetItems:="row_ListPriceHUF", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable ObjectCreated OrderOutHeader", AppearanceItemType.ViewItem, "1=1", TargetItems:="ObjectCreated", Enabled:=False)> _
<Appearance("Disable UserCreated OrderOutHeader", AppearanceItemType.ViewItem, "1=1", TargetItems:="UserCreated", Enabled:=False)> _
<Appearance("OrdersOutHeader row properties Hide, when IsEditable=False", AppearanceItemType.ViewItem, "IsEditable=False", TargetItems:="row_Products;row_QTT;row_ListPriceHUF;row_ListPriceDEV;row_DiscountPercent", visibility:=Editors.ViewItemVisibility.Hide)> _
Public Class OrderOutHeader
    Inherits BaseObject

    Private _CustomersFrom As CustomersFrom 'Saját cég
    Private _CustomersOrder As CustomersOrder 'Beszállító
    Private _CustomersOrderDocuments As CustomersOrderDocuments
    Private _Contacts As Contacts 'Beszállító kapcsolata
    Private _DocumentNumber As String 'Rendelési szám
    Private _DateCreated As Date 'kiállítás dátuma
    Private _DateExecution As Date 'Teljesítés dátuma
    Private _ObjectCreated As Date 'Objektum keletkezésének dátuma
    Private _UserCreated As User
    Private _PaymentOption As PaymentOption 'Fizetési opció
    Private _ShipmentOption As ShipmentOption 'Szállítási opció
    Private _QualityOption As QualityOption 'Minőségi opció
    Private _CurrencyName As CurrencyName 'Deviza
    Private _IsEditable As Boolean = True 'Szerkeszthető vagy végleges
    Private _IsStorno As Boolean = False 'Stornózott
    Private _OrderOutHeader As OrderOutHeader 'Melyik rendelés helyesbítése
    Private _TotalNettoHUF As Double = 0
    Private _TotalNettoDEV As Double = 0
    Private _Note As String ' Megjegyzés

    '-- Non persistent objektumok
    Private _row_Products As Products 'Termék, ami a szállítólevelen van
    Private _row_QTT As Double 'Termék szállított mennyisége
    Private _row_ListPriceHUF As Double 'Termék listaára HUF
    Private _row_ListPriceDEV As Double 'Termék listaára DEV
    Private _row_DiscountPercent As Double 'Termék ár kedvezmény %-ban

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        DateCreated = GetSQLTime(Session)
        DateExecution = GetSQLTime(Session)
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If String.IsNullOrEmpty(DocumentNumber) Then
            DocumentNumber = CustomersOrderDocuments.NumberGenerator.GetNewNumber(CustomersOrderDocuments.NumberGenerator)
        End If
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        End If
    End Sub
    <VisibleInListView(False)> _
    <Association("OrderOutHeader-OrderOutRows", GetType(OrderOutRows))> _
    Public ReadOnly Property OrderOutRows() As XPCollection(Of OrderOutRows)
        Get
            Return GetCollection(Of OrderOutRows)("OrderOutRows")
        End Get
    End Property


    <RuleRequiredField("OrderOutHeader.CustomersFrom", DefaultContexts.Save)> _
    Property CustomersFrom() As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(ByVal value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <RuleRequiredField("OrderOutHeader-CustomersOrder", DefaultContexts.Save)> _
    Property CustomersOrder() As CustomersOrder
        Get
            Return _CustomersOrder
        End Get
        Set(ByVal value As CustomersOrder)
            SetPropertyValue("CustomersOrder", _CustomersOrder, value)
        End Set
    End Property
    <RuleRequiredField("OrderOutHeader.CustomersOrderDocuments", DefaultContexts.Save)> _
    <DataSourceCriteria("CustomersOrder='@This.CustomersOrder'")> _
    <ImmediatePostData(True)> _
    Property CustomersOrderDocuments As CustomersOrderDocuments
        Get
            Return _CustomersOrderDocuments
        End Get
        Set(ByVal value As CustomersOrderDocuments)
            SetPropertyValue("CustomersOrderDocuments", _CustomersOrderDocuments, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                If value IsNot Nothing Then
                    Me.CurrencyName = value.CurrencyName
                Else
                    CurrencyName = Nothing

                End If
            End If
        End Set
    End Property
    Property DocumentNumber() As String
        Get
            Return _DocumentNumber
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    <RuleRequiredField("RuleRequiredField for OrderOutHeader.Contacts", DefaultContexts.Save)> _
    <DataSourceCriteria("Customers='@This.CustomersOrder'")> _
    <ImmediatePostData()> _
    Property Contacts() As Contacts
        Get
            Return _Contacts
        End Get
        Set(ByVal value As Contacts)
            SetPropertyValue("Contacts", _Contacts, value)
            'If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
            '    If value IsNot Nothing Then
            '        Me.CustomersOrder = value.Customers
            '    End If
            'End If
        End Set
    End Property
    Property DateCreated() As Date
        Get
            Return _DateCreated
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateCreated", _DateCreated, value)
        End Set
    End Property
    Property DateExecution() As Date
        Get
            Return _DateExecution
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
        End Set
    End Property
    <NonCloneable> _
    Property ObjectCreated() As Date
        Get
            Return _ObjectCreated
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    <NonCloneable> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property
    Public Property PaymentOption() As PaymentOption
        Get
            Return _PaymentOption
        End Get
        Set(ByVal value As PaymentOption)
            SetPropertyValue("PaymentOption", _PaymentOption, value)
        End Set
    End Property
    Public Property ShipmentOption() As ShipmentOption
        Get
            Return _ShipmentOption
        End Get
        Set(ByVal value As ShipmentOption)
            SetPropertyValue("ShipmentOption", _ShipmentOption, value)
        End Set
    End Property
    Public Property QualityOption() As QualityOption
        Get
            Return _QualityOption
        End Get
        Set(ByVal value As QualityOption)
            SetPropertyValue("QualityOption", _QualityOption, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Public Property CurrencyName() As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(ByVal value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    Property IsEditable() As Boolean
        Get
            Return _IsEditable
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsEditable", _IsEditable, value)
        End Set
    End Property
    Property IsStorno() As Boolean
        Get
            Return _IsStorno
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsStorno", _IsStorno, value)
        End Set
    End Property
    Property OrderOutHeader As OrderOutHeader
        Get
            Return _OrderOutHeader
        End Get
        Set(value As OrderOutHeader)
            SetPropertyValue("OrderOutHeader", _OrderOutHeader, value)
        End Set
    End Property
    Public Property TotalNettoHUF() As Double
        Get
            Return _TotalNettoHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("TotalNettoHUF", _TotalNettoHUF, value)
        End Set
    End Property
    Public Property TotalNettoDEV() As Double
        Get
            Return _TotalNettoDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("TotalNettoDEV", _TotalNettoDEV, value)
        End Set
    End Property
    <Size(-1)> _
    Property Note() As String
        Get
            Return _Note
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Note", _Note, value)
        End Set
    End Property

    ' NonPersistent propertys -------------------------------
    <NonPersistent()> _
     <RuleRequiredField("OrderOutHeader.row_Products", "aToTable_OrderOut")> _
     <VisibleInListView(False)> _
     <ImmediatePostData(True)> _
    Property row_Products() As Products
        Get
            Return _row_Products
        End Get
        Set(ByVal value As Products)
            SetPropertyValue("row_Products", _row_Products, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                If value IsNot Nothing Then
                    Me.row_ListPriceHUF = value.ListPriceInHUF
                    Me.row_ListPriceDEV = value.ListPriceInDEV
                End If
            End If
        End Set
    End Property
    <NonPersistent()> _
     <RuleRange("OrderOutHeader._row_QTT", "aToTable_OrderOut", 0, 999999999)> _
     <VisibleInListView(False)> _
    Property row_QTT As Double
        Get
            Return _row_QTT
        End Get
        Set(value As Double)
            SetPropertyValue("row_QTT", _row_QTT, value)
        End Set
    End Property
    <NonPersistent()> _
     <RuleRange("OrderOutHeader._row_ListPriceHUF", "aToTable_OrderOut", 0.0001, Double.MaxValue, TargetCriteria:="CurrencyName.ShortName='HUF'")> _
    <VisibleInListView(False)> _
    Property row_ListPriceHUF As Double
        Get
            Return _row_ListPriceHUF
        End Get
        Set(value As Double)
            SetPropertyValue("row_ListPriceHUF", _row_ListPriceHUF, value)
        End Set
    End Property
    <NonPersistent()> _
      <RuleRange("OrderOutHeader._row_ListPriceDEV", "aToTable_OrderOut", 0.0001, Double.MaxValue, TargetCriteria:="CurrencyName.ShortName!='HUF'")> _
    <VisibleInListView(False)> _
    Property row_ListPriceDEV As Double
        Get
            Return _row_ListPriceDEV
        End Get
        Set(value As Double)
            SetPropertyValue("row_ListPriceDEV", _row_ListPriceDEV, value)
        End Set
    End Property
    <NonPersistent()> _
    <RuleRange("OrderOutHeader._row_DiscountPercent", "aToTable_OrderOut", 0, 99)> _
    <VisibleInListView(False)> _
    Property row_DiscountPercent As Double
        Get
            Return _row_DiscountPercent
        End Get
        Set(value As Double)
            SetPropertyValue("row_DiscountPercent", _row_DiscountPercent, value)
        End Set
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property OrderInTo As XPCollection(Of OrderInTo)
        Get
            Return New XPCollection(Of OrderInTo)(Session, CriteriaOperator.Parse("OrderOutRows is NULL"))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property DeliveryNoteInRows As XPCollection(Of DeliveryNoteInRows)
        Get
            Return New XPCollection(Of DeliveryNoteInRows)(Session, CriteriaOperator.Parse("OrderOutRows.OrderOutHeader=?", Me))
        End Get
    End Property
End Class
