Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports System.Linq
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Utils

Public Class IntrastatHeader_VC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Private Sub aIntrastatHeader_GenerateIntrastatRows_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aIntrastatHeader_GenerateIntrastatRows.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _IntrastatHeader As IntrastatHeader = TryCast(View.SelectedObjects(0), IntrastatHeader)
            Dim _IntrastatRows_DELCollection As New XPCollection(Of IntrastatRows)(_ocur.Session, DevExpress.Data.Filtering.CriteriaOperator.Parse("IntrastatHeader=?", _IntrastatHeader))


            '// Nullás súlyok terméktörzsből update




            _ocur.Delete(_IntrastatRows_DELCollection)
            _ocur.CommitChanges()

            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace


            Dim _DeliveryNoteInRows_XPQuery = New XPQuery(Of DeliveryNoteInRows)(_onew.Session)
            Dim query = From _DeliveryNoteInRows In _DeliveryNoteInRows_XPQuery _
                        Where _DeliveryNoteInRows.DeliveryNoteInHeader.DateExecution.Year = _IntrastatHeader.DateExecution.Year And
                                _DeliveryNoteInRows.DeliveryNoteInHeader.DateExecution.Month = _IntrastatHeader.DateExecution.Month
                        Group _DeliveryNoteInRows By _DeliveryNoteInRows.DeliveryNoteInHeader.ShipmentOption,
                              _DeliveryNoteInRows.Products.CustomsTariff,
                              _DeliveryNoteInRows.DeliveryNoteInHeader.Customers.Address1.Country,
                              _DeliveryNoteInRows.Products.CountryFrom,
                              _DeliveryNoteInRows.ShipingCode,
                              _DeliveryNoteInRows.ITransactionNumber,
                              _DeliveryNoteInRows.DeliveryNoteInHeader.DateExecution Into g = Group _
                       Select New With {Key .ShipmentOption = ShipmentOption, _
                                        Key .CustomTariffs = CustomsTariff, _
                                        Key .ITransactionNumber = ITransactionNumber, _
                                        Key .CountryFrom = CountryFrom, _
                                        Key .ShipingCode = ShipingCode, _
                                        Key .CountrySender = Country, _
                                        Key .DateExecution = DateExecution, _
                                        Key .AmountHUF_Sum = g.Sum(Function(inv) (inv.ListPriceHUF - inv.DiscountPriceHUF) * inv.QTT), _
                                        Key .Area_Sum = g.Sum(Function(inv) inv.Area), _
                                        Key .Weight_Sum = g.Sum(Function(inv) inv.Weight)}


            Dim _RowIndex As Long = 1
            RaiseEvent InitWork(1, 1, query.Count)
            For Each item In query
                RaiseEvent DoWork()
                If GLOBAL_EUMemberStates.Contains(item.CountrySender.GetMemberValue("ShortName")) Then
                    Dim _IntrastatRows As New IntrastatRows(_onew.Session)
                    With _IntrastatRows
                        .IntrastatHeader = _onew.GetObject(_IntrastatHeader)
                        .RowIndex = _RowIndex
                        .ITransactionNumber = item.ITransactionNumber
                        .ShipmentOption = item.ShipmentOption
                        .CustomsTariffs = item.CustomTariffs
                        .CountrySender = item.CountrySender
                        .ShipingCode = item.ShipingCode
                        .CountryFrom = item.CountryFrom
                        .AmountHUF = item.AmountHUF_Sum
                        .Area = item.Area_Sum
                        .Weight = item.Weight_Sum
                    End With
                    _RowIndex += 1
                End If
            Next

            _onew.CommitChanges()

            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        Finally
            RaiseEvent FinalWork
        End Try
    End Sub

    Private Sub aIntrastatCheckPrice_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aIntrastatCheckPrice.Execute
        '// számlaigazolásból árak ellenőrzése
        Dim _DeliveryNoteInRows As DeliveryNoteInRows
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _DeliveryNoteInRows In View.SelectedObjects
            RaiseEvent DoWork()
            If _DeliveryNoteInRows.DeliveryNoteInHeader.CurrencyName.ShortName <> "HUF" Then
                If _DeliveryNoteInRows.DeliveryNoteInRRFC.Count > 0 Then
                    _DeliveryNoteInRows.ListPriceHUF = Math.Round(_DeliveryNoteInRows.ListPriceDEV * _DeliveryNoteInRows.DeliveryNoteInHeader.CurrencyRate, 0, MidpointRounding.AwayFromZero)
                    If _DeliveryNoteInRows.Weight <= 0 Then
                        _DeliveryNoteInRows.WeightMode = eWeightMode.eTotalWeightperQuantity
                        If _DeliveryNoteInRows.Products.NettoWeight <> 0 Then
                            _DeliveryNoteInRows.Weight = Math.Round(_DeliveryNoteInRows.QTT * _DeliveryNoteInRows.Products.NettoWeight, 4, 0)
                        End If
                    End If
                End If
            End If
        Next
        _ocur.CommitChanges()
        RaiseEvent FinalWork
    End Sub

    Private Sub SimpleAction1_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aIntrastatCheckWeight.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _DeliveryNoteInRows As DeliveryNoteInRows


        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _DeliveryNoteInRows In View.SelectedObjects
            RaiseEvent DoWork()
            Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(_ocur.Session,
                                                                            CriteriaOperator.Parse("DeliveryNoteInRows=?", _DeliveryNoteInRows))
            Dim _StorageInRows As StorageInRows

            For Each _StorageInRows In _StorageInRows_Collection
                If _StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                    If _StorageInRows.DeliveryNoteInRows.Weight <> 0 Then
                        If _StorageInRows.DeliveryNoteInRows.QTT <> 0 Then
                            _StorageInRows.Weight = Math.Round((_StorageInRows.DeliveryNoteInRows.Weight / _StorageInRows.DeliveryNoteInRows.QTT) * _StorageInRows.QTT, 2, MidpointRounding.AwayFromZero)
                        End If
                    End If
                End If
                If _StorageInRows.StorageInRowsFrom IsNot Nothing Then
                    If _StorageInRows.StorageInRowsFrom.DeliveryNoteInRows IsNot Nothing Then
                        _StorageInRows.DeliveryNoteInRows = _StorageInRows.StorageInRowsFrom.DeliveryNoteInRows
                        If _StorageInRows.StorageInRowsFrom.DeliveryNoteInRows.Weight <> 0 Then
                            If _StorageInRows.StorageInRowsFrom.DeliveryNoteInRows.QTT <> 0 Then
                                _StorageInRows.Weight = Math.Round((_StorageInRows.StorageInRowsFrom.DeliveryNoteInRows.Weight / _StorageInRows.StorageInRowsFrom.DeliveryNoteInRows.QTT) * _StorageInRows.QTT, 2, MidpointRounding.AwayFromZero)
                            End If
                        End If
                    End If
                End If

                Dim _StorageInRows_From_Collection As New XPCollection(Of StorageInRows)(_ocur.Session, CriteriaOperator.Parse("StorageInRowsFrom=?", _StorageInRows))
                Dim _StorageInRows_From As StorageInRows

                For Each _StorageInRows_From In _StorageInRows_From_Collection
                    _StorageInRows_From.DeliveryNoteInRows = _StorageInRows.DeliveryNoteInRows
                    _StorageInRows_From.Weight = (_StorageInRows.Weight / _StorageInRows.QTT) * _StorageInRows_From.QTT
                Next
                _ocur.CommitChanges()
            Next
        Next
        RaiseEvent FinalWork
    End Sub

    Private Sub aPrintIntrastat_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aPrintIntrastat.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _IntrastatHeader As IntrastatHeader = TryCast(View.SelectedObjects(0), IntrastatHeader)
            If _IntrastatHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _ReportData As ReportData = _IntrastatHeader.IntrastatParameters.Report
            If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!" + vbNewLine + "A folyamat megszakadt!")

            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("IntrastatHeader.Oid=?", _IntrastatHeader.Oid), True)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
