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.SystemModule
Imports DevExpress.Persistent.Base.Security
Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.ExpressApp.Filtering

Public Enum State
    <ImageName("certificate_add")> Preparation = 0 'Előkészítés alatt
    <ImageName("certificate_new")> Ready = 1 'Aláírásra kész
    <ImageName("certificate_ok")> Signed = 2 'Aláírt
    <ImageName("certificate_delete")> Terminating = 3 'Felmondás alatt
    <ImageName("certificate_broken")> Closed = 4 'Lezárt szerződés
    <ImageName("certificate_warning")> Waiting = 5 'Felfüggesztett, várakozik valamire
End Enum
Public Enum eDayOfWeek
    eMonday = 1
    eTuesday = 2
    eWednesday = 3
    eThursday = 4
    eFriday = 5
    eSaturday = 6
    eSunday = 7
End Enum
Public Enum eDayOfMonth
    eLastDay = -1
    e1 = 1
    e2 = 2
    e3 = 3
    e4 = 4
    e5 = 5
    e6 = 6
    e7 = 7
    e8 = 8
    e9 = 9
    e10 = 10
    e11 = 11
    e12 = 12
    e13 = 13
    e14 = 14
    e15 = 15
    e16 = 16
    e17 = 17
    e18 = 18
    e19 = 19
    e20 = 20
    e21 = 21
    e22 = 22
    e23 = 23
    e24 = 24
    e25 = 25
    e26 = 26
    e27 = 27
    e28 = 28
    e29 = 29
    e30 = 30
    e31 = 31
End Enum
Public Enum eActionFrequency
    eDaily = 0 'Minden nap
    eWeekly = 1 '-- Minden héten
    eTwoWeekly = 5 '-- Minen második héten
    eMonthly = 2 ' -- Minden hónapban
    eQuarter = 3 '-- Minden negyedévben
    eYear = 4 '-- Minden évben
End Enum
Public Enum eContractRowsType
    <ImageName("")> eFix = 0 'Fix számlázandó sor
    <ImageName("")> eMileStone = 1 'Fix számlázandó, de meg kell adni számlázáskor a teljesítés dátumát
    <ImageName("")> eVariable = 2 ' Változó érték, a számlázás előtt meg kell adni és lehet kézileg módosítani
    <ImageName("")> eAdvance = 3 ' Előleg sortípus, előleg számla kiállításához
    <ImageName("")> eCustomRate = 4 'Dijbekérő !!!
End Enum
Public Enum eDefinite
    eFixedTerm = 0 'Határozott időtartamú
    eIndefinite = 1 'Határozatlan időtartamú
End Enum

'<EditorStateRuleAttribute("Disable ContractTemplate", "ContractTemplate", EditorState.Disabled, "(IsNull(ContractTemplate))=False", ViewType.DetailView)> _
'<EditorStateRuleAttribute("Hide DateClosed", "DateClosed", EditorState.Hidden, "State != 4", ViewType.DetailView)> _
'<EditorStateRuleAttribute("Hide DateWaiting", "DateWaiting", EditorState.Hidden, "State != 5", ViewType.DetailView)> _
'<EditorStateRuleAttribute("Hide DateTerminating", "DateTerminating", EditorState.Hidden, "State != 3", ViewType.DetailView)> _

<DefaultClassOptions()> _
<NavigationItem("Contracts")> _
<DefaultProperty("DisplayName")> _
<FileAttachment("File")> _
<Appearance("Disable ContractTemplate", TargetItems:="ContractTemplate", Criteria:="(IsNull(ContractTemplate))=False", Enabled:=False, Context:="DetailView")> _
<Appearance("Hide DateClosed", TargetItems:="DateClosed", Criteria:="State != 4", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("Hide DateWaiting", TargetItems:="DateWaiting", Criteria:="State != 5", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("Hide DateTerminating", TargetItems:="DateTerminating", Criteria:="State != 3", Visibility:=Editors.ViewItemVisibility.Hide, Context:="DetailView")> _
<Appearance("Hide BankInformation, CurrencyName_Invoice when ContractTemplate.PartnerType=1", AppearanceItemType.ViewItem, "ContractTemplate.PartnerType=1", TargetItems:="BankInformation;CurrencyName_Invoice", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide ContractRows;AuditTrail property when DocumentNumber is nothing", AppearanceItemType.ViewItem, "IsNull(DocumentNumber)=True", TargetItems:="ContractRows;AuditTrail", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable Contracts-DocumentBody when IsNull(DocumentNumber)=True", AppearanceItemType.ViewItem, "IsNull(DocumentNumber)=True", TargetItems:="DocumentNumber", Enabled:=False)> _
<Appearance("Disable Contracts-CustomersFrom when is not nothing", AppearanceItemType.ViewItem, "IsNull(CustomersFrom)=False", TargetItems:="CustomersFrom", Enabled:=False)> _
<Appearance("Disable Contracts-Customers when is not nothing", AppearanceItemType.ViewItem, "IsNull(Customers)=False", TargetItems:="Customers", Enabled:=False)> _
<Appearance("Disable CRM_Opportunities,OrderInAcknowledgementHeader, OfferInHeader when ContractTemplate.IsCRM=False", AppearanceItemType.ViewItem, "ContractTemplate.IsCRM=False", TargetItems:="CRM_Opportunities;OrderInAcknowledgementHeader;OfferInHeader", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable CRMEstate_Opportunity_Immovables, DatePreContractSigned, DateWaiting, DateTerminating when ContractTemplate.IsImmovables=False", AppearanceItemType.ViewItem, "ContractTemplate.IsImmovables=False", TargetItems:="CRMEstate_Opportunity_Immovables;DatePreContractSigned;DateWaiting;DateTerminating", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable Contract any property when State<>0", AppearanceItemType.ViewItem, "State<>0", TargetItems:="CustomersFrom;Customers;DateCreated;" & _
    "DatePreContractSigned;DateClosed;DateWaiting;DateExecution;DateExpectClose;ContractTemplate;State;DocumentNumber;ObjectCreated;" & _
    "UserCreated;CurrencyName;CurrencyName_Invoice;BankInformation;JobNumberObjects;UniqueObjects;ShortName;HRResponsible;CRMEstate_Opportunity_Immovables;" & _
    "CRM_Opportunities;OfferInHeader;OrderInAcknowledgementHeader;Definite;TerminateDay", Enabled:=False)> _
<Appearance("Disable Contracts-File when State<>0 and State<>1", AppearanceItemType.ViewItem, "State<>0 AND State<>1", TargetItems:="File", Enabled:=False)> _
Public Class Contracts 'Szerződések
    Inherits BaseObject
    Private _CustomersFrom As CustomersFrom 'Saját cég
    Private _Customers As Customers 'Partner
    Private _ContractGroup As ContractGroup
    Private _DateCreated As Date 'Aláírás dátuma
    Private _DatePreContractSigned As Date 'Előszerződés aláírásának dátuma
    Private _DateClosed As Date 'Szerződés lezárás dátuma
    Private _DateWaiting As Date 'Felfüggesztés dátuma
    Private _DateTerminating As Date 'Felmondás dátuma
    Private _DateExecution As Date 'Hatálybalépés dátuma
    Private _DateExpectClose As Date 'Várható lezárás dátuma
    Private _ContractTemplate As ContractTemplate 'Szerződési sablon
    Private _File As FileData ' Csatolt fájl
    Private _State As State = State.Preparation
    Private _DocumentNumber As String
    Private _ObjectCreated As Date
    Private _UserCreated As User
    Private _CurrencyName As CurrencyName 'A szerződés deviza neme
    Private _CurrencyName_Invoice As CurrencyName 'A vevői szerződéshez kapcsolódó számla deviza nem
    Private _BankInformation As BankInformation 'A vevői szerződéshez kapcsolódó számlára felkerülő bankszámlaszám SAJÁT CÉG!!!
    Private _CustomerBankAccounts As CustomerBankAccounts 'ÜGYFÉL bankszámla
    Private _DocumentBody As String
    Private _JobNumberObjects As JobNumberObjects 'Munkaszám, project
    Private _UniqueObjects As UniqueObjects 'Egyedi azonosító
    Private _ShortName As String ' A szerződés tárgya
    Private _HRResponsible As HRPerson 'A szerződés felelőse
    Private _CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables 'Milyen ingatlanértékesítéshez tartozik
    Private _CRM_Opportunities As CRM_Opportunities 'Milyen CRM ügyhöz tartozik a szerződés
    Private _OfferInHeader As OfferInHeader 'Bejövő  ajánlat
    Private _OfferOutHeader As OfferOutHeader
    Private _OrderInAcknowledgementHeader As OrderInAcknowledgementHeader 'Kimenő rendelés visszaigazolása

    Private _Definite As eDefinite 'Határozott vagy határozatlan időtartamú
    Private _TerminateDay As Long 'Felmondási nap, ha határozatlan


    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If String.IsNullOrEmpty(DocumentNumber) Then
            If ContractTemplate IsNot Nothing Then
                If ContractTemplate.NumberGenerator IsNot Nothing Then
                    _DocumentNumber = ContractTemplate.NumberGenerator.GetNewNumber(ContractTemplate.NumberGenerator)
                End If
            End If
        End If
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        End If
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        CurrencyName = Session.FindObject(Of CurrencyName)(1, CriteriaOperator.Parse("ShortName='HUF'"))
    End Sub
    <RuleRequiredField("Contracts-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("Contracts-Customers", DefaultContexts.Save)>
    <ImmediatePostData(True)>
    Property Customers() As Customers
        Get
            Return _Customers
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("Customers", _Customers, value)
        End Set
    End Property
    
    Property ContractGroup As ContractGroup
        Get
            Return _ContractGroup
        End Get
        Set(value As ContractGroup)
            SetPropertyValue("ContractGroup", _ContractGroup, value)
        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
    <VisibleInListView(False), VisibleInDetailView(False), VisibleInLookupListView(False)> _
    ReadOnly Property DateCreated_ShortDate As String
        Get
            Return String.Format(DateCreated, "Short Date")
        End Get
    End Property
    Property DatePreContractSigned As Date
        Get
            Return _DatePreContractSigned
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DatePreContractSigned", _DatePreContractSigned, value)
        End Set
    End Property
    Property DateClosed As Date
        Get
            Return _DateClosed
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateClosed", _DateClosed, value)
        End Set
    End Property
    Property DateWaiting As Date
        Get
            Return _DateWaiting
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateWaiting", _DateWaiting, value)
        End Set
    End Property
    Property DateTerminating As Date
        Get
            Return _DateTerminating
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateTerminating", _DateTerminating, 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 DateExpectClose As Date
        Get
            Return _DateExpectClose
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateExpectClose", _DateExpectClose, value)
        End Set
    End Property

    <RuleRequiredField("Contracts.ContractTemplate", "aMarkAsReady_Context")>
    <ImmediatePostData(True), DataSourceCriteria("CustomersFrom='@This.CustomersFrom' And IsTechnical = False")>
    Property ContractTemplate() As ContractTemplate
        Get
            Return _ContractTemplate
        End Get
        Set(ByVal value As ContractTemplate)
            SetPropertyValue("ContractTemplate", _ContractTemplate, value)
        End Set
    End Property
    <Aggregated()> _
    <ExpandObjectMembers(ExpandObjectMembers.Never)>
    Property File() As FileData
        Get
            Return _File
        End Get
        Set(ByVal value As FileData)
            SetPropertyValue("File", _File, 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
    <NonCloneable> _
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(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
    <ImmediatePostData(True)> _
    <RuleRequiredField("Contracts.CurrencyName", DefaultContexts.Save, TargetCriteria:="ContractTemplate.PartnerType = 1")> _
    Property CurrencyName As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(ByVal value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property CurrencyName_Invoice As CurrencyName
        Get
            Return _CurrencyName_Invoice
        End Get
        Set(value As CurrencyName)
            SetPropertyValue("CurrencyName_Invoice", _CurrencyName_Invoice, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceProperty("ContractsBankInformation")>
    Property BankInformation As BankInformation
        Get
            Return _BankInformation
        End Get
        Set(ByVal value As BankInformation)
            SetPropertyValue("CustomerBankAccounts", _BankInformation, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceCriteria("Customers='@This.Customers'")>
    Property CustomerBankAccounts As CustomerBankAccounts
        Get
            Return _CustomerBankAccounts
        End Get
        Set(value As CustomerBankAccounts)
            SetPropertyValue("CustomerBankAccounts", _CustomerBankAccounts, value)
        End Set
    End Property
    Property State() As State
        Get
            Return _State
        End Get
        Set(ByVal value As State)
            SetPropertyValue("State", _State, value)
        End Set
    End Property
    <Size(-1)> _
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    Property DocumentBody() As String
        Get
            If Session.IsNewObject(Me) = True Then
                If Not _ContractTemplate Is Nothing Then
                    _DocumentBody = _ContractTemplate.DocumentBody
                End If
            End If
            Return _DocumentBody
        End Get
        Set(ByVal value As String)
            SetPropertyValue("DocumentBody", _DocumentBody, value)
        End Set
    End Property
    Property JobNumberObjects() As JobNumberObjects
        Get

            Return _JobNumberObjects
        End Get
        Set(ByVal value As JobNumberObjects)
            SetPropertyValue("JobNumberObjects", _JobNumberObjects, value)
        End Set
    End Property
    Property UniqueObjects As UniqueObjects
        Get
            Return _UniqueObjects
        End Get
        Set(value As UniqueObjects)
            SetPropertyValue("UniqueObjects", _UniqueObjects, value)
        End Set
    End Property
    <RuleRequiredField("Contract-ShortName", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    Property HRResponsible As HRPerson
        Get
            Return _HRResponsible
        End Get
        Set(ByVal value As HRPerson)
            SetPropertyValue("HRResponsible", _HRResponsible, value)
        End Set
    End Property
    Property CRMEstate_Opportunity_Immovables As CRMEstate_Opportunity_Immovables
        Get
            Return _CRMEstate_Opportunity_Immovables
        End Get
        Set(value As CRMEstate_Opportunity_Immovables)
            SetPropertyValue("CRMEstate_Opportunity_Immovables", _CRMEstate_Opportunity_Immovables, value)
        End Set
    End Property
    <Association("CRM_Opportunities-Contract", GetType(CRM_Opportunities))>
    <ImmediatePostData(True), DataSourceCriteria("CRM_Opportunities.CustomersFrom='@This.CustomersFrom' AND Customers='@This.Customers'")>
    Property CRM_Opportunities As CRM_Opportunities
        Get
            Return _CRM_Opportunities
        End Get
        Set(value As CRM_Opportunities)
            SetPropertyValue("CRM_Opportunities", _CRM_Opportunities, value)
        End Set
    End Property
    Property OfferInHeader As OfferInHeader
        Get
            Return _OfferInHeader
        End Get
        Set(value As OfferInHeader)
            SetPropertyValue("OfferInHeader", _OfferInHeader, value)
        End Set
    End Property
    Property OfferOutHeader As OfferOutHeader
        Get
            Return _OfferOutHeader
        End Get
        Set(value As OfferOutHeader)
            SetPropertyValue("OfferOutHeader", _OfferOutHeader, value)
        End Set
    End Property
    Property OrderInAcknowledgementHeader As OrderInAcknowledgementHeader
        Get
            Return _OrderInAcknowledgementHeader
        End Get
        Set(value As OrderInAcknowledgementHeader)
            SetPropertyValue("OrderInAcknowledgementHeader", _OrderInAcknowledgementHeader, value)
        End Set
    End Property

    Property Definite As eDefinite
        Get
            Return _Definite
        End Get
        Set(value As eDefinite)
            SetPropertyValue("Definite", _Definite, value)
        End Set
    End Property

    Property TerminateDay As Long
        Get
            Return _TerminateDay
        End Get
        Set(value As Long)
            SetPropertyValue("TerminateDay", _TerminateDay, value)
        End Set
    End Property
    ReadOnly Property DateEstimatedTermination As Date
        Get
            Return DateAdd(DateInterval.Day, TerminateDay, Now)
        End Get
    End Property
    'Readonly
    ReadOnly Property DisplayName() As String
        Get
            If Customers Is Nothing Then
                Return DocumentNumber & ", " & ShortName
            Else
                Return DocumentNumber & ", " & Customers.FullName & ", " & ShortName
            End If
        End Get
    End Property
    <Aggregated(), Association("Contracts-DocumentFileData", GetType(DocumentFileData)), VisibleInListView(False)> _
    ReadOnly Property DocumentFile() As XPCollection(Of DocumentFileData)
        Get
            Return GetCollection(Of DocumentFileData)("DocumentFile")
        End Get
    End Property
    <VisibleInListView(False)> _
    ReadOnly Property AuditTrail() As XPCollection(Of AuditDataItemPersistent)
        Get
            Return AuditedObjectWeakReference.GetAuditTrail(Session, Me)
        End Get
    End Property
    <Association("Contracts-ContractRows", GetType(ContractRows)), VisibleInListView(False)> _
    ReadOnly Property ContractRows() As XPCollection(Of ContractRows)
        Get
            Return GetCollection(Of ContractRows)("ContractRows")
        End Get
    End Property
    ReadOnly Property ContractsBankInformation As XPCollection(Of BankInformation)
        Get
            If Me.CustomersFrom IsNot Nothing Then
                Dim _BankInformation_Collection As New XPCollection(Of BankInformation)(Session, CriteriaOperator.Parse("1=2"))

                Dim _LiquidAssets_Collection As New XPCollection(Of LiquidAssets)(Session, CriteriaOperator.Parse("CustomerFrom=? AND LiquidAssetsType=1", Me.CustomersFrom))

                For Each _LiquidAssets As LiquidAssets In _LiquidAssets_Collection
                    Dim _BankInformation As BankInformation = Session.FindObject(Of BankInformation)(CriteriaOperator.Parse("LiquidAssests=?", _LiquidAssets))
                    If _BankInformation IsNot Nothing Then
                        _BankInformation_Collection.Add(_BankInformation)
                    End If
                Next

                Return _BankInformation_Collection
            Else
                Return Nothing
            End If
        End Get
    End Property
    <Association("Contracts-ContractProductsCredit", GetType(ContractProductsCredit)), VisibleInListView(False), VisibleInLookupListView(False)> _
       ReadOnly Property ContractProductsCredit As XPCollection(Of ContractProductsCredit)
        Get
            Return GetCollection(Of ContractProductsCredit)("ContractProductsCredit")
        End Get
    End Property
End Class

