
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
Imports DevExpress.ExpressApp.ConditionalAppearance


'<EditorStateRule("Disable properties when IsEditable=False (IN)", "*", ViewType.DetailView, "IsEditable = False", EditorState.Disabled)> _

<DefaultClassOptions()> _
<NavigationItem("BusinessTransactions")> _
<Appearance("Disable properties when IsEditable=False (IN)", TargetItems:="*", Criteria:="IsEditable = False", Enabled:=False, Context:="DetailView")> _
<RuleCriteria("RuleCriteria IF OfferLOSTReason <> NotSet OR OfferInState <> ClosedLost THEN Save (IN)", DefaultContexts.Save, "OfferLOSTReason != 0 OR OfferInState !=2", "", SkipNullOrEmptyValues:=False)> _
<Appearance("Hide OfferLOSTReasons when eOfferInState <> 2 (IN)", AppearanceItemType.ViewItem, "OfferInState <> 2", TargetItems:="OfferLOSTReason", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable OfferLOSTReasonDescription when OfferLOSTReason <> 7 (IN)", AppearanceItemType.ViewItem, "OfferLOSTReason <> 7", TargetItems:="OfferLOSTReasonDescription", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable OfferInHeader-Contact", AppearanceItemType.ViewItem, "IsNull(Contact)=False", TargetItems:="Contact", Enabled:=False)> _
<Appearance("Disable OfferInHeader-Customers", AppearanceItemType.ViewItem, "IsNull(Customers)=False", TargetItems:="Customers", Enabled:=False)> _
<Appearance("Disable OfferInHeader-OfferOutParameters", AppearanceItemType.ViewItem, "IsNull(OfferInParameters)=False", TargetItems:="OfferInParameters", Enabled:=False)> _
<Appearance("Disable aOffersInFinal", AppearanceItemType.Action, "IsEditable = False", TargetItems:="aOffersInFinal", Enabled:=False)> _
<Appearance("Hide OfferInHeader _row_XX' properties when RowType = Other", AppearanceItemType.ViewItem, "row_RowType = 1", TargetItems:="row_Installation;row_Products", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide OfferInHeader Installation properties when row_RowType = Product", AppearanceItemType.ViewItem, "row_RowType = 0", TargetItems:="row_VAT;row_Description;row_Installation;row_ShortName;row_Units", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide OfferInHeader Product propertis when row_RowType = Installation", AppearanceItemType.ViewItem, "row_RowType = 2", TargetItems:="row_VAT;row_Products;row_Units;row_Description;row_ShortName", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide CloneObject-OfferIn, addrow and removerow", AppearanceItemType.Action, "IsEditable = False", TargetItems:="CloneObject;aTotable_OfferIn;aTotableD_OfferIn", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide _row_FinalPriceDEV-OfferIn if CurrencyName is HUF", AppearanceItemType.ViewItem, "CurrencyName.ShortName='HUF'", TargetItems:="row_FinalPriceDEV", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide _row_FinalPriceHUF-OfferIn if CurrencyName is not HUF", AppearanceItemType.ViewItem, "CurrencyName.ShortName<>'HUF'", TargetItems:="row_FinalPriceHUF", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide row_ info when IsEditable=False OffersIn", AppearanceItemType.ViewItem, "IsEditable=False", TargetItems:="row_RowType; row_Products; row_Installation; row_ShortName;" _
    & "row_Description; row_QTT; row_Units; row_VAT; row_FinalPriceHUF; row_FinalPriceDEV; row_RowGroup1; row_RowGroup2", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable ObjectCreated OfferInHeader", AppearanceItemType.ViewItem, "1=1", TargetItems:="ObjectCreated", Enabled:=False)> _
<Appearance("Disable UserCreated OfferInHeader", AppearanceItemType.ViewItem, "1=1", TargetItems:="UserCreated", Enabled:=False)> _
Public Class OfferInHeader
    Inherits BaseObject
    Private _OfferInParameters As OfferInParameters 'Árajánlat paramétere, ami a sorszámot és egyéb tul. meghat.
    Private _OfferInState As eOfferInState 'Ajánlat státusz, hol tart jelenleg
    Private _OfferLOSTReason As eOfferInLOSTReason 'Ha nem fogadjuk el, miért ?
    Private _OfferLOSTReasonDescription As String ' Ha nem fogadjuk el és eOther akkor szövegesen beírni !
    Private _DocumentNumber As String 'Árajánlat száma (generálódik)
    Private _CustomersFrom As CustomersFrom 'Saját cég
    Private _Customers As Customers 'Partner, akitől az árajánlatot kapjuk szól (ez nem kötelező, de a contactból  ismerje fel !)
    Private _Contact As Contacts 'Kapcsolat, akitől az árajánlatot kapjuk (ez a fő !!! kötelező)
    Private _DateCreated As Date 'Kiállítás dátuma
    Private _DateExecution As Date 'Teljesítés dátuma
    Private _DatePayment As Date 'Fiz. hat
    Private _DateDue As Date 'Érvényesség dátuma

    Private _DateAcknowledgement As Date 'Rendelés visszaigazolás dátuma

    Private _Description As String 'Leírás (fejlécen)
    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 _ObjectCreated As Date 'Maga az objektum mikor készült el
    Private _UserCreated As User 'Ki készítette
    Private _IsEditable As Boolean = True 'Szerkeszthető vagy végleges ajánlat
    Private _IsStorno As Boolean = True 'Stornírozott ajánlat

    'Excel, Word dokumentum
    Private _DocumentBody As String 'RichText dokumentum test
    Private _ExcelProperty As String 'Excel !!!
    Private _ExcelFile As FileData

    '--- Sorok rögzítéséhez nonpersistent propertyk
    Private _row_RowType As eRowType 'Sor tipus
    Private _row_Products As Products
    Private _row_Installation As Installation
    Private _row_ShortName As String 'Megnevezés
    Private _row_Description As String 'Leírás
    Private _row_QTT As Double 'Mennyiség
    Private _row_Units As Units 'Mennyiségi egység
    Private _row_VAT As VAT 'ÁFA kód
    Private _row_FinalPriceHUF As Double 'Termék listaára HUF
    Private _row_FinalPriceDEV As Double 'Termék listaára DEV
    Private _row_RowGroup1 As String 'Csoportosító sor1
    Private _row_RowGroup2 As String 'Csoportosító sor2

    Private _CRM_Opportunities As CRM_Opportunities


    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
            If OfferInParameters Is Nothing Then
            Else
                ObjectCreated = Now
                If DocumentNumber = "" Then
                    DocumentNumber = OfferInParameters.NumberGenerator.GetNewNumber(OfferInParameters.NumberGenerator)
                End If
            End If
        End If
        MyBase.Save()
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Me.IsEditable = True
        Me.IsStorno = False
        CurrencyName = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName=?", "HUF"))
        Me.row_VAT = Session.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
        Me.row_Units = Session.FindObject(Of Units)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
    End Sub


    <ImmediatePostData(True), DataSourceCriteria("CustomersFrom = '@This.CustomersFrom'")>
    <RuleRequiredField("OfferInHeader.OfferInParameters", DefaultContexts.Save)>
    Property OfferInParameters As OfferInParameters
        Get
            Return _OfferInParameters
        End Get
        Set(ByVal value As OfferInParameters)
            SetPropertyValue("OfferInParameters", _OfferInParameters, value)
        End Set
    End Property
    <ImmediatePostData()>
    <NonCloneable()>
    Property OfferInState As eOfferInState
        Get
            Return _OfferInState
        End Get
        Set(ByVal value As eOfferInState)
            SetPropertyValue("OfferInState", _OfferInState, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Select Case OfferInState
                    Case eOfferInState.eClosedWON
                        OfferLOSTReason = eOfferInLOSTReason.eNotSet
                    Case eOfferInState.eInProgress
                        OfferLOSTReason = eOfferInLOSTReason.eNotSet
                    Case eOfferInState.eInThinkingOf
                        OfferLOSTReason = eOfferInLOSTReason.eNotSet
                End Select
            End If
        End Set
    End Property
    <ImmediatePostData()>
    <NonCloneable()>
    Property OfferLOSTReason As eOfferInLOSTReason
        Get
            Return _OfferLOSTReason
        End Get
        Set(ByVal value As eOfferInLOSTReason)
            SetPropertyValue("OfferLOSTReason", _OfferLOSTReason, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                Select Case OfferLOSTReason
                    Case eOfferInLOSTReason.eNotSet
                        OfferLOSTReasonDescription = ""
                    Case eOfferInLOSTReason.eTooExpensive
                        OfferLOSTReasonDescription = ""
                    Case eOfferInLOSTReason.eTooLate
                        OfferLOSTReasonDescription = ""
                    Case eOfferInLOSTReason.eChooseConcurrency
                        OfferLOSTReasonDescription = ""
                    Case eOfferInLOSTReason.eQualityProblem
                        OfferLOSTReasonDescription = ""
                    Case eOfferInLOSTReason.eNoAnswer
                        OfferLOSTReasonDescription = ""
                    Case eOfferInLOSTReason.eWINOther
                        OfferLOSTReasonDescription = ""
                End Select
            End If
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-OfferLOSTReasonDescription", DefaultContexts.Save, TargetCriteria:="OfferLOSTReason = 7")>
    <ImmediatePostData()>
    <Size(-1)>
    Property OfferLOSTReasonDescription As String
        Get
            Return _OfferLOSTReasonDescription
        End Get
        Set(ByVal value As String)
            SetPropertyValue("OfferLOSTReasonDescription", _OfferLOSTReasonDescription, value)
        End Set
    End Property
    <NonCloneable()>
    Property DocumentNumber As String
        Get
            Return _DocumentNumber
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DocumentNumber", _DocumentNumber, value)
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-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
    <ImmediatePostData()>
    <RuleRequiredField("OfferInHeader-Customers", DefaultContexts.Save)>
    Property Customers As Customers
        Get
            Return _Customers
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceProperty("Customers.Contacts", DataSourcePropertyIsNullMode.SelectAll)>
    Property Contact As Contacts
        Get
            Return _Contact
        End Get
        Set(ByVal value As Contacts)
            SetPropertyValue("Contact", _Contact, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False And value IsNot Nothing Then
                Me.Customers = value.Customers
            End If
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-DateCreated", DefaultContexts.Save)>
    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
    Property DatePayment As Date
        Get
            Return _DatePayment
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DatePayment", _DatePayment, value)
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-DateDue", DefaultContexts.Save)> _
    Property DateDue As Date
        Get
            Return _DateDue
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateDue", _DateDue, value)
        End Set
    End Property
    Property DateAcknowledgement As Date
        Get
            Return _DateAcknowledgement
        End Get
        Set(value As Date)
            SetPropertyValue("DateAcknowledgement", _DateAcknowledgement, value)
        End Set
    End Property

    <Size(-1)>
    Property Description As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-PaymentOption", DefaultContexts.Save)> _
    Property PaymentOption As PaymentOption
        Get
            Return _PaymentOption
        End Get
        Set(ByVal value As PaymentOption)
            SetPropertyValue("PaymentOption", _PaymentOption, value)
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-ShipmentOption", DefaultContexts.Save)> _
    Property ShipmentOption As ShipmentOption
        Get
            Return _ShipmentOption
        End Get
        Set(ByVal value As ShipmentOption)
            SetPropertyValue("ShipmentOption", _ShipmentOption, value)
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-QualityOption", DefaultContexts.Save)> _
    Property QualityOption As QualityOption
        Get
            Return _QualityOption
        End Get
        Set(ByVal value As QualityOption)
            SetPropertyValue("QualityOption", _QualityOption, value)
        End Set
    End Property
    <RuleRequiredField("OfferInHeader-CurrencyName", DefaultContexts.Save)> _
    Property CurrencyName As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(ByVal value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    <NonCloneable()> _
    Property IsEditable As Boolean
        Get
            Return _IsEditable
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsEditable", _IsEditable, value)
        End Set
    End Property
    <NonCloneable()> _
    Property IsStorno As Boolean
        Get
            Return _IsStorno
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("IsStorno", _IsStorno, 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
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    <Association("OfferInHeader", GetType(OfferInRows))> _
    Public ReadOnly Property OfferInRows() As XPCollection(Of OfferInRows)
        Get
            Return GetCollection(Of OfferInRows)("OfferInRows")
        End Get
    End Property
    <VisibleInListView(False)> _
    <Association("OfferInheader-OfferInheaderFileAttachment", GetType(OfferInheaderFileAttachment))> _
    Public ReadOnly Property OfferInheaderFileAttachment As XPCollection(Of OfferInheaderFileAttachment)
        Get
            Return GetCollection(Of OfferInheaderFileAttachment)("OfferInheaderFileAttachment")
        End Get
    End Property
    ' --- Excel, Word dokumentum
    <Size(-1), VisibleInListView(False), VisibleInLookupListView(False)> _
    Property DocumentBody() As String
        Get
            Return _DocumentBody
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DocumentBody", _DocumentBody, value)
        End Set
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    Property ExcelProperty As String
        Get
            Return _ExcelProperty
        End Get
        Set(value As String)
            SetPropertyValue("ExcelProperty", _ExcelProperty, value)
        End Set
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    Public Property ExcelFile As FileData
        Get
            Return _ExcelFile
        End Get
        Set(ByVal value As FileData)
            SetPropertyValue("ExcelFile", _ExcelFile, value)
        End Set
    End Property

    '--- Sorok rögzítéséhez nonpersistent propertyk
    <NonPersistent()> _
    <ImmediatePostData()> _
    <VisibleInListView(False)> _
    Property row_RowType As eRowType
        Get
            Return _row_RowType
        End Get
        Set(value As eRowType)
            SetPropertyValue("row_RowType", _row_RowType, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_Products As Products
        Get
            Return _row_Products
        End Get
        Set(value As Products)
            SetPropertyValue("row_Products", _row_Products, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_Installation As Installation
        Get
            Return _row_Installation
        End Get
        Set(value As Installation)
            SetPropertyValue("row_Installation", _row_Installation, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_ShortName As String
        Get
            Return _row_ShortName
        End Get
        Set(value As String)
            SetPropertyValue("row_ShortName", _row_ShortName, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_Description As String
        Get
            Return _row_Description
        End Get
        Set(value As String)
            SetPropertyValue("row_Description", _row_Description, value)
        End Set
    End Property
    <NonPersistent()> _
    <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()> _
    <VisibleInListView(False)> _
    Property row_Units As Units
        Get
            Return _row_Units
        End Get
        Set(value As Units)
            SetPropertyValue("row_Units", _row_Units, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_VAT As VAT
        Get
            Return _row_VAT
        End Get
        Set(value As VAT)
            SetPropertyValue("row_VAT", _row_VAT, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_FinalPriceHUF As Double
        Get
            Return _row_FinalPriceHUF
        End Get
        Set(value As Double)
            SetPropertyValue("row_FinalPriceHUF", _row_FinalPriceHUF, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_FinalPriceDEV As Double
        Get
            Return _row_FinalPriceDEV
        End Get
        Set(value As Double)
            SetPropertyValue("row_FinalPriceDEV", _row_FinalPriceDEV, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_RowGroup1 As String
        Get
            Return _row_RowGroup1
        End Get
        Set(value As String)
            SetPropertyValue("row_RowGroup1", _row_RowGroup1, value)
        End Set
    End Property
    <NonPersistent()> _
    <VisibleInListView(False)> _
    Property row_RowGroup2 As String
        Get
            Return _row_RowGroup2
        End Get
        Set(value As String)
            SetPropertyValue("row_RowGroup2", _row_RowGroup2, value)
        End Set
    End Property

    '--- CRM ügy 
    <Association("OfferInHeader-CRM_Opportunities", GetType(CRM_Opportunities))> _
    Property CRM_Opportunities As CRM_Opportunities
        Get
            Return _CRM_Opportunities
        End Get
        Set(value As CRM_Opportunities)
            SetPropertyValue("CRM_Opportunities", _CRM_Opportunities, value)

            If Session.IsObjectsSaving = False And Session.IsObjectsLoading = False And Me.CRM_Opportunities IsNot Nothing And Session.IsNewObject(Me) = True Then
                Me.CustomersFrom = Me.CRM_Opportunities.CustomersFrom
            End If
        End Set
    End Property
End Class
