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

Imports System.Linq
Imports System.Linq.Expressions

'-- POPUP-HOZ osztályok-------------------------------------------------------------
<NavigationItem(False), CreatableItem(False)> _
<NonPersistent()> _
<Appearance("Hide DEV properties", AppearanceItemType.ViewItem, "CurrencyName.ShortName='HUF'", TargetItems:="AmountDEV;CurrencyRate", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide BankInformation", AppearanceItemType.ViewItem, "PaymentOption.PayMode != 1", TargetItems:="BankInformation", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Hide when IsEditable=False", AppearanceItemType.ViewItem, "IsEditable=False", TargetItems:="Controlling;CostHolder;CostPlace;UniqueObjects;JobNumberObjects;VAT;InvoiceType;PaymentOption;Controlling;DatePayment", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable when IsEditable=False", AppearanceItemType.ViewItem, "IsEditable=False", TargetItems:="CustomersFrom;Customers;CurrencyName", Enabled:=False)> _
Public Class InvoiceAdvancePopup 'Előlegszámla készítő, ez csinál InvoiceHeader és InvoiceRows-t
    Inherits BaseObject
    Private _CustomersFrom As CustomersFrom ' -- Saját cég
    Private _Customers As Customers ' -- Ügyfél
    Private _CurrencyRate As Double ' -- Árfolyam
    Private _CurrencyName As CurrencyName ' -- Deviza
    Private _Description As String ' -- Leírás
    Private _DateExecution As Date ' -- Teljesítés dátuma
    Private _DatePayment As Date ' -- Fizetési határidő
    Private _PaymentOption As PaymentOption ' -- Fizetési opció
    Private _AmountHUF As Double ' -- Összeg HUF (Bruttó!!!)
    Private _AmountDEV As Double ' -- Összeg DEV (Bruttó!!!)

    Private _Controlling As Controlling ' -- Kontroll szám
    Private _CostHolder As CostHolder ' -- Költségviselő
    Private _CostPlace As CostPlace ' -- Költséghely
    Private _UniqueObjects As UniqueObjects
    Private _JobNumberObjects As JobNumberObjects


    Private _VAT As VAT ' -- ÁFA kód
    Private _BankInformation As BankInformation ' -- Bankszámlaszám
    Private _InvoiceType As InvoiceType ' -- Számla típusa
    Private _IsEditable As Boolean
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        VAT = Session.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes=True"))
        CurrencyName = Session.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
        CurrencyRate = 1
        IsEditable = True
    End Sub
    <RuleRequiredField("InvoiceAdvancePopup.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("InvoiceAdvancePopup.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
    Property CurrencyRate As Double
        Get
            Return _CurrencyRate
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("CurrencyRate", _CurrencyRate, value)
        End Set
    End Property
    <ImmediatePostData()> _
    <RuleRequiredField("InvoiceAdvancePopup.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
    <Size(-1)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
    <ImmediatePostData()> _
    <RuleRequiredField("InvoiceAdvancePopup.DateExecution", DefaultContexts.Save)> _
    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 Then
                Me.DatePayment = value
            End If
        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("InvoiceAdvancePopup.PaymentOption", DefaultContexts.Save)> _
    <ImmediatePostData()> _
    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 value.PayMode = ePayMode.InCash Then
                    Me.DateExecution = GetSQLTime(Session)
                End If
            End If
        End Set
    End Property
    Property AmountHUF As Double
        Get
            Return _AmountHUF
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AmountHUF", _AmountHUF, value)
        End Set
    End Property
    <ImmediatePostData()>
    Property AmountDEV As Double
        Get
            Return _AmountDEV
        End Get
        Set(ByVal value As Double)
            SetPropertyValue("AmountDEV", _AmountDEV, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If CurrencyName.ShortName <> "HUF" Then AmountHUF = Math.Round(AmountDEV * CurrencyRate, 0, MidpointRounding.AwayFromZero)
            End If
        End Set
    End Property

    '-- Rule5DMatrix ------------------------------------
    <RuleRequiredField("InvoiceAdvancePopup-Controlling", DefaultContexts.Save, TargetCriteria:="InvoiceType.Rule5DMatrix.RequireControlling=True")>
    <ImmediatePostData(True), DataSourceCriteria("IsAdvancePayment=True and IsParent=False")>
    Property Controlling As Controlling
        Get
            Return _Controlling
        End Get
        Set(ByVal value As Controlling)
            SetPropertyValue("Controlling", _Controlling, value)
        End Set
    End Property
    <RuleRequiredField("InvoiceAdvancePopup-CostHolder", DefaultContexts.Save, TargetCriteria:="InvoiceType.Rule5DMatrix.RequireCostHolder=True")>
    Property CostHolder As CostHolder
        Get
            Return _CostHolder
        End Get
        Set(value As CostHolder)
            SetPropertyValue("CostHolder", _CostHolder, value)
        End Set
    End Property
    <RuleRequiredField("InvoiceAdvancePopup-CostPlace", DefaultContexts.Save, TargetCriteria:="InvoiceType.Rule5DMatrix.RequireCostPlace=True")>
    Property CostPlace As CostPlace
        Get
            Return _CostPlace
        End Get
        Set(value As CostPlace)
            SetPropertyValue("CostPlace", _CostPlace, value)
        End Set
    End Property
    <RuleRequiredField("InvoiceAdvancePopup-UniqueObjects", DefaultContexts.Save, TargetCriteria:="InvoiceType.Rule5DMatrix.RequireUniqueObjects=True")>
    Property UniqueObjects As UniqueObjects
        Get
            Return _UniqueObjects
        End Get
        Set(value As UniqueObjects)
            SetPropertyValue("UniqueObjects", _UniqueObjects, value)
        End Set
    End Property
    <RuleRequiredField("InvoiceAdvancePopup-JobNumberObjects", DefaultContexts.Save, TargetCriteria:="InvoiceType.Rule5DMatrix.RequireJobNumberObjects=True")>
    Property JobNumberObjects As JobNumberObjects
        Get
            Return _JobNumberObjects
        End Get
        Set(value As JobNumberObjects)
            SetPropertyValue("JobNumberObjects", _JobNumberObjects, value)
        End Set
    End Property


    <RuleRequiredField("InvoiceAdvancePopup.VAT", DefaultContexts.Save)>
    Property VAT As VAT
        Get
            Return _VAT
        End Get
        Set(ByVal value As VAT)
            SetPropertyValue("VAT", _VAT, value)
        End Set
    End Property
    <RuleRequiredField("InvoiceAdvancePopup.BankInformation", DefaultContexts.Save, TargetCriteria:="PaymentOption.PayMode = 1")>
    <ImmediatePostData(True), DataSourceCriteria("LiquidAssests.CustomerFrom = '@This.CustomersFrom'")>
    Property BankInformation As BankInformation
        Get
            Return _BankInformation
        End Get
        Set(ByVal value As BankInformation)
            SetPropertyValue("BankInformation", _BankInformation, value)
        End Set
    End Property
    <ImmediatePostData(True), DataSourceCriteria("CustomersFrom ='@This.CustomersFrom' AND InvoiceTypeBase = 0 OR InvoiceTypeBase = 1")>
    <RuleRequiredField("InvoiceAdvancePopup.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
    <Browsable(False)> _
    Property IsEditable As Boolean
        Get
            Return _IsEditable
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsEditable", _IsEditable, value)
        End Set
    End Property
End Class

#Region "Cool Codes"
'<CustomQueryProperties("DisplayableProperties", "Employee_Linq;Orders_Sum_Linq")> _
'Public Shared Function OrdersLinq(ByVal s As Session) As IQueryable
'    Dim orders As New XPQuery(Of Order)(s)
'    Dim queryOrders = _
'     From o In orders _
'     Order By o.Employee.FirstName Ascending _
'     Group o By GroupKey = o.Employee.FirstName & " " & o.Employee.LastName Into oo = Group _
'     Where oo.Sum(Function(o) o.Freight) > 10000 _
'     Select New With {Key .OrderID = oo.Sum(Function(o) o.OrderID), Key .Employee_Linq = GroupKey, Key .Orders_Sum_Linq = oo.Sum(Function(o) o.Freight)}
'    Return queryOrders
'End Function
#End Region

