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

Public Enum eInvoiceStrategy
    eInvoiceAdvanceAsk = 0 'Előleg bekérő
    eInvoiceAdvance = 2 'Előlegszámla
    eInvoice = 3 'Próbaszámla vagy számla
    eDeliveryNoteOutAndInvoice = 4 'Kiszállítás + számla
    eStorageOutAndDeliveryNoteOutAndInvoice = 5 'Kitárolás + kiszállítás + számla
    eDeliveryNoteOut = 6 'Kiszállítás
    eStorageOutAndDeliveryNoteOut = 7 'Kitárolás + kiszállítás
    eInvoiceFinal = 8 'Csak végszámla készítése
End Enum
<DefaultClassOptions()> _
<NonPersistent()> _
<NavigationItem(False), CreatableItem(False)> _
<Appearance("ORI_Invoice_Popup - AlertMessage",
                  appearanceitemtype:="ViewItem",
                  criteria:="AlertMessage=''",
                  context:="DetailView",
                  targetitems:="AlertMessage",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - InvoiceStrategy in ('eInvoice','eInvoiceAdvanceAsk','eInvoiceAdvance')",
                  appearanceitemtype:="ViewItem",
                  criteria:="InvoiceStrategy in ('eInvoice','eInvoiceAdvanceAsk','eInvoiceAdvance','eInvoiceFinal')",
                  context:="DetailView",
                  targetitems:="TransportAddress;Storage;StorageMoveType;DeliveryNoteOutType;IsAPCSheet;InvoiceStrategy;ShipmentOption",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - InvoiceStrategy='eInvoiceAdvanceAsk'",
                  appearanceitemtype:="ViewItem",
                  criteria:="InvoiceStrategy in ('eInvoiceAdvanceAsk')",
                  context:="DetailView",
                  targetitems:="IsLiquidAssets;LiquidAssets;IsFinalInvoice",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - InvoiceStrategy='eInvoiceAdvance'",
                  appearanceitemtype:="ViewItem",
                  criteria:="InvoiceStrategy in ('eInvoiceAdvance')",
                  context:="DetailView",
                  targetitems:="InvoiceStrategy;ShipmentOption;Storage;StorageMoveType;DeliveryNoteOutType;QualityOption;" + _
                               "TransportAddress;LiquidAssets;IsLiquidAssets;IsFinalInvoice;PriceMode;IsAPCSheet",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - InvoiceStrategy='eInvoiceAdvance' - Enable",
                  appearanceitemtype:="ViewItem",
                  criteria:="InvoiceStrategy in ('eInvoiceAdvance')",
                  context:="DetailView",
                  targetitems:="DateExecution,PaymentOption;BankInformation;DatePayment;DateCreated",
                  enabled:=False)> _
<Appearance("ORI_Invoice_Popup - CurrencyName='HUF'",
                  appearanceitemtype:="ViewItem",
                  criteria:="CurrencyName.ShortName='HUF'",
                  context:="DetailView",
                  targetitems:="CurrencyRate;PriceMode",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - PaymentOption.PayMode != 'InCash'",
                  appearanceitemtype:="ViewItem",
                  criteria:="PaymentOption.PayMode != 'InCash'",
                  context:="DetailView",
                  targetitems:="IsLiquidAssets;LiquidAssets",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - PaymentOption.PayMode != 'InTransfer'",
                  appearanceitemtype:="ViewItem",
                  criteria:="PaymentOption.PayMode != 'InTransfer'",
                  context:="DetailView",
                  targetitems:="BankInformation",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - IsLiquidAssets",
                  appearanceitemtype:="ViewItem",
                  criteria:="IsLiquidAssets != True",
                  context:="DetailView",
                  targetitems:="LiquidAssets",
                  visibility:=ViewItemVisibility.Hide)> _
<Appearance("ORI_Invoice_Popup - IsAPCSheet",
                  appearanceitemtype:="ViewItem",
                  criteria:="IsAPCSheet = True",
                  context:="DetailView",
                  targetitems:="DatePayment;PaymentOption",
                  enabled:=False)> _
<Appearance("ORI_Invoice_Popup - InvoiceAdvance",
                  appearanceitemtype:="ViewItem",
                  criteria:="InvoiceStrategy <> 8",
                  context:="DetailView",
                  targetitems:="InvoiceAdvance",
                  visibility:=ViewItemVisibility.Hide)> _
Public Class ORI_Invoice_Popup
    Inherits BaseObject

    Private _InvoiceStrategy As eInvoiceStrategy
    Private _InvoiceType As InvoiceType
    Private _CustomersFrom As CustomersFrom 'Saját cég
    Private _Customers As Customers 'Ügyfél
    Private _DateCreated As Date 'Kiállítás dátuma
    Private _DateExecution As Date 'Teljesítés dátuma
    Private _DatePayment As Date 'Fizetési határidő
    Private _Description As String 'Számlán, szállítólevelen szereplő megjegyzés
    Private _Description_Header As String 'Számlán, szállítólevelen szereplő fejléc megjegyzés
    Private _PaymentOption As PaymentOption 'Fizetési opció (ebből automatikusan DatePayment és Date Execution)
    Private _ShipmentOption As ShipmentOption 'Szállítási kondíció
    Private _CurrencyName As CurrencyName 'Deviza
    Private _CurrencyRate As Double = 0 'Árfolyam, ha a deviza nem HUF egyébként 1
    Private _BankInformation As BankInformation ' A fejlécben megjelenő számlaszám
    Private _Storage As Storage 'Raktár a Kitárolás+szállítólevél action-hoz
    Private _StorageMoveType As StorageMoveType
    Private _DeliveryNoteOutType As DeliveryNoteOutType
    Private _QualityOption As QualityOption ' Minőségi opció
    Private _TransportAddress As Address ' -- Szállítási cím
    Private _LiquidAssets As LiquidAssets 'Ha kp, akkor ebbe a pénztárba generálja le abevételi pénztárbizonylatot.
    Private _IsLiquidAssets As Boolean 'Készűl-e pénztárbizonylat?
    Private _IsFinalInvoice As Boolean 'Egyből végszámla legyen-e? ha igen akkor lehet csak pénztárbizonylatot csinálni és ekkor kell GLCassa is!
    Private _PriceMode As ePriceMode 'DEV HUF regenerálás módja
    Private _IsAPCSheet As Boolean 'Termeltetési szerződéssel kapcsolatos-e
    Private _AlertMessage As String 'Figyelmeztető szöveg, ha valami nem jó
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        IsFinalInvoice = False
        IsLiquidAssets = False
        IsAPCSheet = False
        CurrencyRate = 1
        AlertMessage = ""
    End Sub

    Property InvoiceStrategy As eInvoiceStrategy
        Get
            Return _InvoiceStrategy
        End Get
        Set(value As eInvoiceStrategy)
            SetPropertyValue("InvoiceStrategy", _InvoiceStrategy, value)
        End Set
    End Property
    <DataSourceCriteria("CustomersFrom='@This.CustomersFrom' and InvoiceTypeBase in (2,3)")> _
    <RuleRequiredField("ORI_Invoice_Popup - InvoiceType", DefaultContexts.Save)> _
    Property InvoiceType() As InvoiceType
        Get
            Return _InvoiceType
        End Get
        Set(ByVal value As InvoiceType)
            SetPropertyValue("InvoiceType", _InvoiceType, value)
        End Set
    End Property
    Property CustomersFrom() As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(ByVal value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    Property Customers() As Customers
        Get
            Return _Customers
        End Get
        Set(ByVal value As Customers)
            SetPropertyValue("Customers", _Customers, 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
    <ImmediatePostData()> _
    Property DateExecution() As Date
        Get
            Return _DateExecution
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DateExecution", _DateExecution, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False And Me.PaymentOption IsNot Nothing Then
                If Me.PaymentOption.DatePaymentMode = eDatePaymentMode.eDateExecution Then
                    Me.DatePayment = Me.DateExecution.AddDays(Me.PaymentOption.PayDay)
                End If
            End If
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False And Me.DateExecution = Nothing Then
                Me.DatePayment = Nothing
            End If
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If CurrencyName IsNot Nothing Then
                    If CurrencyName.ShortName <> "HUF" Then
                        Dim _CurrencyRate_MNB_Collection = New XPCollection(Of CurrencyRate)(Session, CriteriaOperator.Parse("CurrencyName=? and IsMNB=True and GetDate(DateValid)<=?", _
                             CurrencyName, value.Date))
                        _CurrencyRate_MNB_Collection.Sorting.Add(New SortProperty("DateValid", DB.SortingDirection.Descending))
                        If _CurrencyRate_MNB_Collection.Count > 0 Then
                            For Each _CurrencyRate_MNB In _CurrencyRate_MNB_Collection
                                If _CurrencyRate_MNB.DateValid.Date = _DateExecution.Date Then
                                    CurrencyRate = _CurrencyRate_MNB.RateAverage
                                    Exit For
                                End If
                                Exit For
                            Next
                        End If
                    End If
                End If
            End If
        End Set
    End Property
    <RuleRequiredField("ORI_Invoice_Popup - DatePayment", DefaultContexts.Save)> _
    Property DatePayment() As Date
        Get
            Return _DatePayment
        End Get
        Set(ByVal value As Date)
            SetPropertyValue("DatePayment", _DatePayment, 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
    <Size(-1)> _
    Property Description_Header As String
        Get
            Return _Description_Header
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Description_Header", _Description_Header, value)
        End Set
    End Property
    <ImmediatePostData()> _
    <RuleRequiredField("ORI_Invoice_Popup - PaymentOption", DefaultContexts.Save)> _
    Property PaymentOption() As PaymentOption
        Get
            Return _PaymentOption
        End Get
        Set(ByVal value As PaymentOption)
            SetPropertyValue("PaymentOption", _PaymentOption, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If Me.DateExecution > #1/1/1900# And Me.PaymentOption IsNot Nothing Then
                    If IsAPCSheet = False Then
                        If value.DatePaymentMode = eDatePaymentMode.eDateCreated Then
                            Me.DatePayment = Me.DateCreated.AddDays(Me.PaymentOption.PayDay)
                        Else
                            Me.DatePayment = Me.DateExecution.AddDays(Me.PaymentOption.PayDay)
                        End If
                    End If
                End If
                If value IsNot Nothing Then
                    If value.PayMode = ePayMode.InCash Then
                        Me.DateExecution = GetSQLTime(Session)
                        Me.DatePayment = GetSQLTime(Session)
                    End If
                End If
            End If
        End Set
    End Property
    <RuleRequiredField("ORI_Invoice_Popup - ShipmentOption", DefaultContexts.Save,
                        targetcriteria:="InvoiceStrategy in ('eDeliveryNoteOutAndInvoice','eStorageOutAndDeliveryNoteOutAndInvoice','eDeliveryNoteOut')")> _
    Property ShipmentOption() As ShipmentOption
        Get
            Return _ShipmentOption
        End Get
        Set(ByVal value As ShipmentOption)
            SetPropertyValue("ShipmentOption", _ShipmentOption, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property CurrencyName() As CurrencyName
        Get
            Return _CurrencyName
        End Get
        Set(ByVal value As CurrencyName)
            SetPropertyValue("CurrencyName", _CurrencyName, value)
        End Set
    End Property
    <RuleRange("ORI_Invoice_Popup - CurrencyRate", DefaultContexts.Save, 1.01, Double.MaxValue, TargetCriteria:="CurrencyName.ShortName <> 'HUF'")> _
    Property CurrencyRate() As Double
        Get
            Return _CurrencyRate
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("CurrencyRate", _CurrencyRate, value)
        End Set
    End Property
    <DataSourceCriteria("LiquidAssests.CustomerFrom = '@This.CustomersFrom'")> _
    <RuleRequiredField("ORI_Invoice_Popup - BankInformation", DefaultContexts.Save,
                        targetcriteria:="PaymentOption.PayMode in ('InTransfer') and InvoiceStrategy !='eInvoiceAdvance'")> _
    Property BankInformation As BankInformation
        Get
            Return _BankInformation
        End Get
        Set(ByVal value As BankInformation)
            SetPropertyValue("CustomerBankAccounts", _BankInformation, value)
        End Set
    End Property
    Property Storage As Storage
        Get
            Return _Storage
        End Get
        Set(value As Storage)
            SetPropertyValue("Storage", _Storage, value)
        End Set
    End Property
    <ImmediatePostData()> _
    <DataSourceCriteria("MoveType = 0")> _
     <RuleRequiredField("ORI_Invoice_Popup - StorageMoveType", DefaultContexts.Save,
                         targetcriteria:="InvoiceStrategy in ('eStorageOutAndDeliveryNoteOut','eStorageOutAndDeliveryNoteOutAndInvoice')")> _
    Property StorageMoveType As StorageMoveType
        Get
            Return _StorageMoveType
        End Get
        Set(value As StorageMoveType)
            SetPropertyValue("StorageMoveType", _StorageMoveType, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                If value IsNot Nothing Then
                    Storage = value.Storage 'MoveType
                End If
            End If
        End Set
    End Property
    <RuleRequiredField("ORI_Invoice_Popup - DeliveryNoteOutType", DefaultContexts.Save,
                        targetcriteria:="InvoiceStrategy in ('eStorageOutAndDeliveryNoteOut','eStorageOutAndDeliveryNoteOutAndInvoice','eDeliveryNoteOut')")> _
    Property DeliveryNoteOutType As DeliveryNoteOutType
        Get
            Return _DeliveryNoteOutType
        End Get
        Set(value As DeliveryNoteOutType)
            SetPropertyValue("DeliveryNoteOutType", _DeliveryNoteOutType, value)
        End Set
    End Property
    Property QualityOption() As QualityOption
        Get
            Return _QualityOption
        End Get
        Set(ByVal value As QualityOption)
            SetPropertyValue("QualityOption", _QualityOption, value)
        End Set
    End Property
    <RuleRequiredField("ORI_Invoice_Popup.TransportAddress", DefaultContexts.Save,
                        targetcriteria:="InvoiceStrategy in ('eStorageOutAndDeliveryNoteOut','eStorageOutAndDeliveryNoteOutAndInvoice','eDeliveryNoteOut')")> _
    Property TransportAddress As Address
        Get
            Return _TransportAddress
        End Get
        Set(value As Address)
            SetPropertyValue("TransportAddress", _TransportAddress, value)
        End Set
    End Property
    <DataSourceCriteria("CurrencyName='@This.CurrencyName' and LiquidAssetsType in ('NormalCheckout','CurrencyCheckout')")> _
    Property LiquidAssets As LiquidAssets
        Get
            Return _LiquidAssets
        End Get
        Set(value As LiquidAssets)
            SetPropertyValue("LiquidAssets", _LiquidAssets, value)
        End Set
    End Property
    <ImmediatePostData()> _
    Property IsLiquidAssets As Boolean
        Get
            Return _IsLiquidAssets
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsLiquidAssets", _IsLiquidAssets, value)
        End Set
    End Property
    <ImmediatePostData()> _
    Property IsFinalInvoice As Boolean
        Get
            Return _IsFinalInvoice
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsFinalInvoice", _IsFinalInvoice, value)
        End Set
    End Property
    Property PriceMode As ePriceMode
        Get
            Return _PriceMode
        End Get
        Set(value As ePriceMode)
            SetPropertyValue("PriceMode", _PriceMode, value)
        End Set
    End Property
    Property IsAPCSheet As Boolean
        Get
            Return _IsAPCSheet
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsAPCSheet", _IsAPCSheet, value)
        End Set
    End Property
    <Size(-1)> _
    Property AlertMessage As String
        Get
            Return _AlertMessage
        End Get
        Set(value As String)
            SetPropertyValue("AlertMessage", _AlertMessage, value)
        End Set
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property InvoiceAdvance As XPCollection(Of InvoiceHeader)
        Get
            Dim _InvoiceHeader_Collection_Return As New XPCollection(Of InvoiceHeader)(Session, CriteriaOperator.Parse("1=2"))
            Dim _InvoiceHeader_Collection As New XPCollection(Of InvoiceHeader)(Session, CriteriaOperator.Parse("IsEditable=False and DocumentNumber !='' and InvoiceType.InvoiceTypeBase in ('eEarnestofTender','eAdvancePayment') and Customers=? and CustomersFrom=?", Customers, Me.CustomersFrom))
            Dim _InvoiceHeader As InvoiceHeader
            Dim _InvoiceRows_Advance As InvoiceRows
            Dim _InvoiceRows As InvoiceRows
            Dim _Need As Boolean = False
            For Each _InvoiceHeader In _InvoiceHeader_Collection
                _Need = True

                For Each _InvoiceRows In _InvoiceHeader.InvoiceRows
                    _InvoiceRows_Advance = Session.FindObject(Of InvoiceRows)(CriteriaOperator.Parse("InvoiceHeader.IsStorno=False and InvoiceRowsAdvance.Oid=? and isnull(InvoiceHeader.GCRecord)=True and IsNull(GCRecord)=True and InvoiceHeader.DocumentNumber !='' and InvoiceHeader.IsEditable=False", _InvoiceRows.Oid))
                    If _InvoiceRows_Advance IsNot Nothing Then
                        If _InvoiceRows_Advance.IsDeleted = False Then
                            _Need = False
                            Exit For
                        End If
                    End If
                Next

                If _Need Then
                    _InvoiceHeader_Collection_Return.Add(_InvoiceHeader)
                End If
            Next

            Return _InvoiceHeader_Collection_Return
        End Get
    End Property
End Class
