Imports tsl5.SEPA_3414

Namespace SEPA3414
    Public Class Ordenante
        Property CIF As String
        Property NOMBRE As String
        Property DOMICILIO As String
        Property NUMERO As String
        Property CPO As String
        Property CIUDAD As String
        Property PAIS As String
        Property IBAN As String
        Property BIC As String
        Property SUFIJO As String
        Property ENELMISMODIA As Boolean = False

    End Class
    Public Class Beneficiario
        Property CIF As String
        Property ImporteTransferencia As Double
        Property IdentificacionPago As String
        Property IdentificacionPagoFichero As String
        Property IBAN As String
        Property BIC As String
        Property NOMBRE As String
        Property Proposito As String

    End Class
    Public Class Utilidades
        Public Shared Sub GeneraXML(Ordenante As Ordenante, ListaBeneficiarios As List(Of Beneficiario), FicheroXML As String, FechaEnvio As DateTime, FechaEjecucion As DateTime)
            Try
                '
                ' COMPROBACIONES
                '
                Dim benibaninc = ListaBeneficiarios.Where(Function(x) tsl5.Bancos.Genericas.IBANCorrecto(x.IBAN) = False).ToList
                Dim ListaErrores As String = ""
                If benibaninc.Count > 0 Then
                    For Each ben In benibaninc
                        ListaErrores &= "El beneficiacio " & ben.NOMBRE & " Pago: " & ben.IdentificacionPago & " tiene un IBAN Incorrecto." & vbCrLf
                    Next
                End If
                If ListaErrores <> "" Then Throw New Exception(ListaErrores)

                Dim org(0) As GenericOrganisationIdentification1
                org(0) = New GenericOrganisationIdentification1 With {.Id = Ordenante.CIF & Ordenante.SUFIJO}


                Dim Document As New SEPA_3414.Document
                Document.CstmrCdtTrfInitn = New SEPA_3414.CustomerCreditTransferInitiationV03
                Document.CstmrCdtTrfInitn.GrpHdr = New SEPA_3414.GroupHeader32
                'Dim GrupoCabecera = Document.CstmrCdtTrfInitn.GrpHdr
                Document.CstmrCdtTrfInitn.GrpHdr.MsgId = Now.ToString  'IDENTIFICACION UNICA
                ' Dim FechaEnvio As DateTime = Now
                ' Dim FechaEjecucion As DateTime = Now
                Document.CstmrCdtTrfInitn.GrpHdr.CreDtTm = FechaEnvio.ToString("yyyy-MM-ddTHH:mm:ss")
                Document.CstmrCdtTrfInitn.GrpHdr.NbOfTxs = ListaBeneficiarios.Where(Function(X) X.ImporteTransferencia > 0).Count.ToString
                Document.CstmrCdtTrfInitn.GrpHdr.CtrlSum = ListaBeneficiarios.Where(Function(X) X.ImporteTransferencia > 0).Sum(Function(x) x.ImporteTransferencia).ToString("F2").Replace(",", ".")
                Document.CstmrCdtTrfInitn.GrpHdr.CtrlSumSpecified = True
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty = New SEPA_3414.PartyIdentification32
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.Nm = Ordenante.NOMBRE
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.Id = New Party6Choice With {.Item = New OrganisationIdentification4 With {.Othr = org}}

                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.PstlAdr = New SEPA_3414.PostalAddress6
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.PstlAdr.StrtNm = Ordenante.DOMICILIO
                If Ordenante.NUMERO <> "" Then Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.PstlAdr.BldgNb = Ordenante.NUMERO
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.PstlAdr.PstCd = Ordenante.CPO
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.PstlAdr.TwnNm = Ordenante.CIUDAD
                Document.CstmrCdtTrfInitn.GrpHdr.InitgPty.PstlAdr.Ctry = Ordenante.PAIS
                'Dim listaInformacionPago As List(Of SEPA_3414.PaymentInstructionInformation3)
                'Dim informacionpago As SEPA_3414.PaymentInstructionInformation3
                Dim ListaPmtInf As New List(Of SEPA_3414.PaymentInstructionInformation3)
                Dim PmtInf As New SEPA_3414.PaymentInstructionInformation3
                PmtInf.PmtInfId = Ordenante.CIF
                PmtInf.PmtMtd = SEPA_3414.PaymentMethod3Code.TRF
                If Ordenante.BIC = "UCJAES2MXXX" Then
                    PmtInf.BtchBookg = False ' para unicaja
                    PmtInf.BtchBookgSpecified = True ' para unicaja
                    PmtInf.NbOfTxs = Document.CstmrCdtTrfInitn.GrpHdr.NbOfTxs
                    PmtInf.CtrlSum = Math.Round(ListaBeneficiarios.Where(Function(X) X.ImporteTransferencia > 0).Sum(Function(x) x.ImporteTransferencia), 2, MidpointRounding.AwayFromZero)
                    PmtInf.CtrlSumSpecified = True ' para unicaja
                    PmtInf.PmtTpInf = New SEPA_3414.PaymentTypeInformation19
                    PmtInf.PmtTpInf.SvcLvl = New SEPA_3414.ServiceLevel8Choice With {.Item = "SEPA"} ' para Unicaja
                    PmtInf.PmtTpInf.LclInstrm = New SEPA_3414.LocalInstrument2Choice With {.Item = "SDCL"} ' para Unicaja
                    If ListaBeneficiarios(0).Proposito IsNot Nothing AndAlso ListaBeneficiarios(0).Proposito <> "" Then
                        PmtInf.PmtTpInf.CtgyPurp = New SEPA_3414.CategoryPurpose1Choice ' para Unicaja
                        PmtInf.PmtTpInf.CtgyPurp.Item = ListaBeneficiarios(0).Proposito ' para Unicaja
                    End If

                End If

                PmtInf.ReqdExctnDt = FechaEjecucion
                'PmtInf.BtchBookg = False
                PmtInf.Dbtr = New SEPA_3414.PartyIdentification32
                PmtInf.Dbtr.Id = New Party6Choice
                PmtInf.Dbtr.Id.Item = New OrganisationIdentification4 With {.Othr = org}
                PmtInf.Dbtr.Nm = Ordenante.NOMBRE
                PmtInf.Dbtr.PstlAdr = New SEPA_3414.PostalAddress6
                PmtInf.Dbtr.PstlAdr.StrtNm = Ordenante.DOMICILIO
                If Ordenante.NUMERO <> "" Then PmtInf.Dbtr.PstlAdr.BldgNb = Ordenante.NUMERO
                PmtInf.Dbtr.PstlAdr.PstCd = Ordenante.CPO
                PmtInf.Dbtr.PstlAdr.TwnNm = Ordenante.CIUDAD
                PmtInf.Dbtr.PstlAdr.Ctry = Ordenante.PAIS
                PmtInf.DbtrAcct = New SEPA_3414.CashAccount16
                PmtInf.DbtrAcct.Id = New SEPA_3414.AccountIdentification4Choice With {.Item = Ordenante.IBAN}
                PmtInf.DbtrAgt = New SEPA_3414.BranchAndFinancialInstitutionIdentification4
                PmtInf.DbtrAgt.FinInstnId = New SEPA_3414.FinancialInstitutionIdentification7
                PmtInf.DbtrAgt.FinInstnId.BIC = Ordenante.BIC
                'If Ordenante.ENELMISMODIA Then
                '    PmtInf.PmtTpInf = New SEPA_3414.PaymentTypeInformation19
                '    PmtInf.PmtTpInf.SvcLvl = New SEPA_3414.ServiceLevel8Choice With {.Item = "SEPA"}
                '    PmtInf.PmtTpInf.LclInstrm = New SEPA_3414.LocalInstrument2Choice With {.Item = "SDCL"}
                'End If
                '   PmtInf.Dbtr.Id.Item = Ordenante.CIF & "SEV"
                Dim cts As New List(Of SEPA_3414.CreditTransferTransactionInformation10)
                Dim ct As SEPA_3414.CreditTransferTransactionInformation10
                For Each Beneficiario In ListaBeneficiarios
                    If Beneficiario.ImporteTransferencia > 0 Then
                        ct = New SEPA_3414.CreditTransferTransactionInformation10
                        ct.PmtId = New SEPA_3414.PaymentIdentification1
                        ct.PmtId.InstrId = Beneficiario.IdentificacionPago.Trim.PadRight(35, " ").Substring(0, 35)
                        ct.PmtId.EndToEndId = Beneficiario.IdentificacionPago.Trim.PadRight(35, " ").Substring(0, 35)
                        If Beneficiario.IdentificacionPagoFichero <> "" Then
                            ct.RmtInf = New SEPA_3414.RemittanceInformation5
                            ct.RmtInf.Ustrd = {Beneficiario.IdentificacionPagoFichero}
                        End If
                        If Beneficiario.Proposito <> "" Then
                            ct.PmtTpInf = New SEPA_3414.PaymentTypeInformation19
                            If Ordenante.ENELMISMODIA Then
                                ct.PmtTpInf.SvcLvl = New SEPA_3414.ServiceLevel8Choice With {.Item = "SEPA"}
                                ct.PmtTpInf.LclInstrm = New SEPA_3414.LocalInstrument2Choice With {.Item = "SDCL"}
                            End If
                            ct.PmtTpInf.CtgyPurp = New SEPA_3414.CategoryPurpose1Choice
                            ct.PmtTpInf.CtgyPurp.Item = Beneficiario.Proposito
                        End If
                        Dim sImporteTransferencia = Beneficiario.ImporteTransferencia.ToString("F2").Replace(",", ".")
                        ct.Amt = New SEPA_3414.AmountType3Choice
                        ct.Amt.Item = New SEPA_3414.ActiveOrHistoricCurrencyAndAmount With {.Ccy = "EUR", .Value = sImporteTransferencia}
                        ct.ChrgBr = SEPA_3414.ChargeBearerType1Code.DEBT
                        ct.CdtrAgt = New SEPA_3414.BranchAndFinancialInstitutionIdentification4
                        ct.CdtrAgt.FinInstnId = New SEPA_3414.FinancialInstitutionIdentification7
                        '   ct.CdtrAgt.FinInstnId.BIC = Beneficiario.BIC
                        ct.Cdtr = New SEPA_3414.PartyIdentification32
                        ct.Cdtr.Nm = tsl5.Extensiones.StringExtensions.ConvierteAAlfanumerico(Beneficiario.NOMBRE, "ÁÉÍÓÚáéíóúÑñÜü", "AEIOUaeiouNnUu")
                        ct.CdtrAcct = New SEPA_3414.CashAccount16
                        ct.CdtrAcct.Id = New SEPA_3414.AccountIdentification4Choice With {.Item = Beneficiario.IBAN}
                        cts.Add(ct)
                    End If
                Next
                PmtInf.CdtTrfTxInf = cts.ToArray
                ListaPmtInf.Add(PmtInf)
                '   Document.CstmrCdtTrfInitn = New SEPA_3414.CustomerCreditTransferInitiationV03
                Document.CstmrCdtTrfInitn.PmtInf = ListaPmtInf.ToArray
                tsl5.Utilidades.SerializarUTF8(Document, FicheroXML)
            Catch ex As Exception
                Throw New Exception(ex.Message, ex)
            End Try
        End Sub
    End Class
End Namespace
