unit frecibosbanco;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls,
  Data.DB, Vcl.StdCtrls, ZDataset, Vcl.ComCtrls, ZAbstractRODataset,
  ZAbstractDataset, Vcl.DBCtrls;

type
  Trecibosbanco = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    DBGrid1: TDBGrid;
    qrecibos: TZQuery;
    fdesde: TDateTimePicker;
    dsqrecibos: TDataSource;
    qinsertarrecibos: TZReadOnlyQuery;
    qinsertarrecibos_previos: TZReadOnlyQuery;
    qborrarrecibos_previos: TZReadOnlyQuery;
    qborrarrecibos: TZReadOnlyQuery;
    qrecibosId: TIntegerField;
    qrecibosIDC: TIntegerField;
    qrecibosIDEMPRESA: TIntegerField;
    qrecibosANYO: TIntegerField;
    qrecibosMES: TWideStringField;
    qrecibosNIF: TWideStringField;
    qrecibosNOMBRE: TWideStringField;
    qrecibosDIRECCION: TWideStringField;
    qrecibosCP: TWideStringField;
    qrecibosPOBLACION: TWideStringField;
    qrecibosTOTALCOBRAR: TFloatField;
    qrecibosCONCEPTO: TWideStringField;
    qrecibosCUENTAPAGO: TWideStringField;
    Label1: TLabel;
    Label2: TLabel;
    fhasta: TDateTimePicker;
    Label3: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Label4: TLabel;
    Edit1: TEdit;
    Label5: TLabel;
    Button4: TButton;
    Button5: TButton;
    dsqremesas: TDataSource;
    qremesas: TZQuery;
    dsqrecibosdetalle: TDataSource;
    qrecibosdetalle: TZQuery;
    DBGrid2: TDBGrid;
    DBGrid3: TDBGrid;
    qremesasID: TIntegerField;
    qremesasIDEMPRESA: TIntegerField;
    qremesasNOMEMPRESA: TWideStringField;
    qremesasFECHA: TDateField;
    qremesasFECHAINICIO: TDateField;
    qremesasFECHAFIN: TDateField;
    qremesasPERIODO: TIntegerField;
    qremesasNOMPERIODO: TWideStringField;
    qremesasANYO: TIntegerField;
    qremesasFORMAPAGO: TWideStringField;
    qremesasCANT_LINEAS: TIntegerField;
    qremesasIMPORTETOTAL: TFloatField;
    qremesasACTIVO: TIntegerField;
    qrecibosdetalleId: TIntegerField;
    qrecibosdetalleIDC: TIntegerField;
    qrecibosdetalleIDEMPRESA: TIntegerField;
    qrecibosdetalleANYO: TIntegerField;
    qrecibosdetalleMES: TWideStringField;
    qrecibosdetalleNIF: TWideStringField;
    qrecibosdetalleNOMBRE: TWideStringField;
    qrecibosdetalleDIRECCION: TWideStringField;
    qrecibosdetalleCP: TWideStringField;
    qrecibosdetallePOBLACION: TWideStringField;
    qrecibosdetalleTOTALCOBRAR: TFloatField;
    qrecibosdetalleCONCEPTO: TWideStringField;
    qrecibosdetalleCUENTAPAGO: TWideStringField;
    qremesasCC: TWideStringField;
    qremesasCCBIC: TWideStringField;
    qempresa: TZReadOnlyQuery;
    qempresaid: TIntegerField;
    qempresacif: TWideStringField;
    qempresanombre: TWideStringField;
    qempresaccbic: TWideStringField;
    qempresacuenta: TWideStringField;
    qrecibosdetalleIDRECIBO: TIntegerField;
    qactualizarremesa: TZReadOnlyQuery;
    DBText1: TDBText;
    Label6: TLabel;
    Label7: TLabel;
    DBText2: TDBText;
    Label8: TLabel;
    DBText3: TDBText;
    Label9: TLabel;
    DBText4: TDBText;
    listaoperaciones: TComboBox;
    lblista: TLabel;
    qinsertarrecibospagos_previos: TZReadOnlyQuery;
    qinsertarrecibospagos: TZReadOnlyQuery;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure qrecibosdetalleBeforeOpen(DataSet: TDataSet);
    procedure dsqremesasDataChange(Sender: TObject; Field: TField);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  recibosbanco: Trecibosbanco;

implementation

{$R *.dfm}

procedure Trecibosbanco.Button1Click(Sender: TObject);
begin
case listaoperaciones.ItemIndex of
   0:begin
     qrecibos.Close;
     qborrarrecibos_previos.ExecSQL;
     qinsertarrecibos_previos.Prepare;
     qinsertarrecibos_previos.ParamByName('DESDE').AsDate:=fdesde.Date;
     qinsertarrecibos_previos.ParamByName('HASTA').AsDate:=fhasta.Date;
     qinsertarrecibos_previos.ExecSQL;
     qrecibos.Open;
   end;
   1:begin
     qrecibos.Close;
     qborrarrecibos_previos.ExecSQL;
     qinsertarrecibospagos_previos.Prepare;
     qinsertarrecibospagos_previos.ParamByName('DESDE').AsDate:=fdesde.Date;
     qinsertarrecibospagos_previos.ParamByName('HASTA').AsDate:=fhasta.Date;
     qinsertarrecibospagos_previos.ExecSQL;
     qrecibos.Open;
   end;
end;

end;

procedure Trecibosbanco.Button2Click(Sender: TObject);
begin
   qrecibos.Delete;
end;

procedure Trecibosbanco.Button3Click(Sender: TObject);
var num_remesa:integer;
begin
    qempresa.Close;
    qempresa.Prepare;
    qempresa.ParamByName('EMPRESA').AsInteger:=qrecibosidempresa.AsInteger;
    qempresa.Open;
    qremesas.Open;
    qremesas.Append;
    qremesasidempresa.Value:=qempresaid.AsInteger;
    qremesasnomempresa.value:=qempresanombre.asstring;
    qremesasfecha.value:=date();
    qremesasfechainicio.value:=fdesde.date;
    qremesasfechafin.value:=fhasta.date;
    qremesasperiodo.value:=qrecibosmes.asinteger;
    qremesasnomperiodo.value:=qempresacif.AsString;
    qremesasanyo.value:=qrecibosanyo.AsInteger;
    qremesasformapago.value:='DOMICILIACION';
    qremesascant_lineas.value:=0;
    qremesasimportetotal.value:=0;
    qremesasactivo.value:=1;
    qremesascc.value:=qempresacuenta.asstring;
    qremesasccbic.value:=qempresaccbic.asstring;
    qremesas.Post;
    num_remesa:=qremesasid.AsInteger;
    case listaoperaciones.ItemIndex of
      0:begin
        qinsertarrecibos.Prepare;
        qinsertarrecibos.ParamByName('REMESA').AsInteger:=num_remesa;
        qinsertarrecibos.ParamByName('CONCEPTO').AsString:=edit1.Text;
        qinsertarrecibos.ExecSQL;
      end;
      1:begin
        qinsertarrecibospagos.Prepare;
        qinsertarrecibospagos.ParamByName('REMESA').AsInteger:=num_remesa;
        qinsertarrecibospagos.ParamByName('CONCEPTO').AsString:=edit1.Text;
        qinsertarrecibospagos.ExecSQL;
      end;
    end;
    qrecibosdetalle.Close;
    qrecibosdetalle.Open;
    qactualizarremesa.prepare;
    qactualizarremesa.ParamByName('REMESA').AsInteger:=num_remesa;
    qactualizarremesa.ExecSQL;
    qremesas.Refresh;

end;

procedure Trecibosbanco.Button4Click(Sender: TObject);
begin
    qborrarrecibos.prepare;
    qborrarrecibos.ParamByName('REMESA').AsInteger:=qremesasid.AsInteger;
    qborrarrecibos.ExecSQL;
    qremesas.Delete;
end;

procedure Trecibosbanco.Button5Click(Sender: TObject);
var
  anyo, mes, dia, hora, minutos, segundos, milisegundos: word;
  txanyo, txmes, txdia, txhora, txminutos, txsegundos, txnumlineas,
    txcantidadtotal, txlinea, txcuenta, txdireccion2,txdireccion2adeudo: string;
  txnombre, txdireccion,txdireccionadeudo, txtotalapagar, txnif, txbic,txconcepto: string;
  linea: real;
  F: TextFile;

begin
  decodedate(Date(), anyo, mes, dia);
  decodetime(Date(), hora, minutos, segundos, milisegundos);
  txanyo := floattostr(anyo);
  txmes := floattostr(mes);
  txdia := floattostr(dia);
  txhora := floattostr(hora);
  txminutos := floattostr(minutos);
  txsegundos := floattostr(segundos);
  txnumlineas := qremesascant_lineas.AsString;
  txcantidadtotal := FormatFloat(',0.00', qremesasIMPORTETOTAL.AsFloat);
  txcantidadtotal := stringreplace(txcantidadtotal, '.', '', [rfReplaceAll]);
  txcantidadtotal := stringreplace(txcantidadtotal, ',', '.', [rfReplaceAll]);
  txnif := qremesasnomperiodo.AsString;
  txcuenta := qremesascc.AsString;
  txbic:= qremesasccbic.AsString;
  txnombre := qremesasnomempresa.AsString;
  txnombre := stringreplace(txnombre, 'Ñ', 'N', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, '/', '?', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'º', '?', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Á', 'A', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'É', 'E', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Í', 'I', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Ó', 'O', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Ú', 'U', [rfReplaceAll]);
  txbic := qremesasccbic.AsString;
  txdireccion := ' ';
  txdireccion2 := ' . .';
  txdireccion := stringreplace(txdireccion, '/', '?', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'º', '?', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Ñ', 'N', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Á', 'A', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'É', 'E', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Í', 'I', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Ó', 'O', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Ú', 'U', [rfReplaceAll]);

  txdireccion2 := stringreplace(txdireccion2, '/', '?', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'º', '?', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Ñ', 'N', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Á', 'A', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'É', 'E', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Í', 'I', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Ó', 'O', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Ú', 'U', [rfReplaceAll]);


  if Length(txmes) = 1 then
    txmes := '0' + txmes;
  if Length(txdia) = 1 then
    txdia := '0' + txdia;
  if Length(txhora) = 1 then
    txhora := '0' + txhora;
  if Length(txminutos) = 1 then
    txminutos := '0' + txminutos;
  if Length(txsegundos) = 1 then
    txsegundos := '0' + txsegundos;
  AssignFile(F, ExtractFilePath(Application.ExeName) + 'REMESA.XML');
  Rewrite(F);
  WriteLn(F,

    '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02"><CstmrDrctDbtInitn><GrpHdr>');
  WriteLn(F, '<MsgId>' + txnif + 'AN0' + txanyo + txmes + txdia + txhora +
    txminutos + txsegundos + '</MsgId>');
  WriteLn(F, '<CreDtTm>' + txanyo + '-' + txmes + '-' + txdia + 'T' + txhora +
    ':' + txminutos + ':' + txsegundos + '</CreDtTm>');
  WriteLn(F, '<NbOfTxs>' + txnumlineas + '</NbOfTxs><CtrlSum>' + txcantidadtotal
    + '</CtrlSum>');
  WriteLn(F, '<InitgPty><Nm>' + txnombre + '</Nm><Id><OrgId><Othr><Id>' + 'ES27000' +txnif
    + '</Id><SchmeNm><Prtry>SEPA</Prtry></SchmeNm></Othr></OrgId></Id></InitgPty></GrpHdr>');
  WriteLn(F, '<PmtInf><PmtInfId>' + txnif+ txanyo + txmes + txdia + 'FRST 1</PmtInfId><PmtMtd>DD</PmtMtd><NbOfTxs>' +
    txnumlineas + '</NbOfTxs><CtrlSum>' + txcantidadtotal + '</CtrlSum><PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl><LclInstrm><Cd>CORE</Cd></LclInstrm><SeqTp>FRST</SeqTp></PmtTpInf>');
  WriteLn(F, '<ReqdColltnDt>' + txanyo + '-' + txmes + '-' + txdia +
    '</ReqdColltnDt><Cdtr><Nm>' + txnombre + '</Nm></Cdtr>');
  WriteLn(F, '<CdtrAcct><Id>');
  WriteLn(F, '<IBAN>' + txcuenta +
    '</IBAN></Id></CdtrAcct><CdtrAgt><FinInstnId></FinInstnId></CdtrAgt><ChrgBr>SLEV</ChrgBr><CdtrSchmeId><Id><PrvtId><Othr><Id>ES27000' +
      txnif +'</Id><SchmeNm><Prtry>SEPA</Prtry></SchmeNm></Othr></PrvtId></Id></CdtrSchmeId>');// <BIC>' + txbic +    '</BIC>
  qrecibosdetalle.First;
  linea := 1;
  while not qrecibosdetalle.Eof do
  begin
    txlinea := FormatFloat(',0000', linea);
    txlinea := stringreplace(txlinea, '.', '', [rfReplaceAll]);
    txnombre:= qrecibosdetallenombre.AsString;
    txnombre := stringreplace(txnombre, 'Ñ', 'N', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, '/', '?', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'º', '?', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Á', 'A', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'É', 'E', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Í', 'I', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Ó', 'O', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Ú', 'U', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'ª', '?', [rfReplaceAll]);
    txdireccionadeudo := qrecibosdetalledireccion.AsString;
    txdireccionadeudo := stringreplace(txdireccionadeudo, '/', '?', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'º', '?', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Ñ', 'N', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Á', 'A', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'É', 'E', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Í', 'I', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Ó', 'O', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Ú', 'U', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'ª', '?', [rfReplaceAll]);

    txdireccion2adeudo:=  qrecibosdetalleCP.AsString + ' ' + qrecibosdetallePOBLACION.AsString;
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, '/', '?', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'º', '?', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Ñ', 'N', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Á', 'A', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'É', 'E', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Í', 'I', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Ó', 'O', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Ú', 'U', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'ª', '?', [rfReplaceAll]);
    txconcepto:=qrecibosdetalleconcepto.AsString;
    txconcepto := stringreplace(txconcepto, '/', '?', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'º', '?', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Ñ', 'N', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Á', 'A', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'É', 'E', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Í', 'I', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Ó', 'O', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Ú', 'U', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'ª', '?', [rfReplaceAll]);

    txtotalapagar := FormatFloat(',0.00',
      qrecibosdetalletotalcobrar.AsFloat);
    txtotalapagar := stringreplace(txtotalapagar, '.', '', [rfReplaceAll]);
    txtotalapagar := stringreplace(txtotalapagar, ',', '.', [rfReplaceAll]);

    WriteLn(F,'<DrctDbtTxInf><PmtId><EndToEndId>' +qrecibosdetalleid.asstring+qrecibosdetallenif.asstring+ txanyo + txmes + txdia +
      txhora + txminutos + txsegundos +
      '</EndToEndId></PmtId>');
    WriteLn(F,'<InstdAmt Ccy="EUR">'
      + txtotalapagar + '</InstdAmt><DrctDbtTx><MndtRltdInf><MndtId>'+txlinea+'</MndtId><DtOfSgntr>'+ txanyo +'-'+ txmes+'-' + txdia +'</DtOfSgntr></MndtRltdInf></DrctDbtTx><DbtrAgt><FinInstnId></FinInstnId></DbtrAgt><Dbtr><Nm>' + txnombre + '</Nm></Dbtr><DbtrAcct><Id><IBAN>'+qrecibosdetalleCUENTAPAGO.AsString +
      '</IBAN></Id></DbtrAcct><RmtInf><Ustrd>'+txconcepto+'</Ustrd></RmtInf></DrctDbtTxInf>');//<BIC></BIC>
    qrecibosdetalle.Next;
    linea := linea + 1;
    txdireccionadeudo:='';
    txdireccion2adeudo:='';
  end;

  WriteLn(F, '</PmtInf></CstmrDrctDbtInitn></Document><!-- JOLLY ACADEMY 19 XML -->');
  CloseFile(F);

end;

procedure Trecibosbanco.Button6Click(Sender: TObject);
var
  anyo, mes, dia, hora, minutos, segundos, milisegundos: word;
  txanyo, txmes, txdia, txhora, txminutos, txsegundos, txnumlineas,
    txcantidadtotal, txlinea, txcuenta, txdireccion2,txdireccion2adeudo,txpoblacion: string;
  txnombre, txdireccion,txdireccionadeudo, txtotalapagar, txnif, txbic,txconcepto: string;
  linea: real;
  F: TextFile;

begin
  decodedate(Date(), anyo, mes, dia);
  decodetime(Date(), hora, minutos, segundos, milisegundos);
  txanyo := floattostr(anyo);
  txmes := floattostr(mes);
  txdia := floattostr(dia);
  txhora := floattostr(hora);
  txminutos := floattostr(minutos);
  txsegundos := floattostr(segundos);
  txnumlineas := qremesascant_lineas.AsString;
  txcantidadtotal := FormatFloat(',0.00', qremesasIMPORTETOTAL.AsFloat);
  txcantidadtotal := stringreplace(txcantidadtotal, '.', '', [rfReplaceAll]);
  txcantidadtotal := stringreplace(txcantidadtotal, ',', '.', [rfReplaceAll]);
  txnif := qremesasnomperiodo.AsString;
  txcuenta := qremesascc.AsString;
  txbic:= qremesasccbic.AsString;
  txnombre := qremesasnomempresa.AsString;
  txnombre := stringreplace(txnombre, 'Ñ', 'N', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, '/', '?', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'º', '?', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Á', 'A', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'É', 'E', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Í', 'I', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Ó', 'O', [rfReplaceAll]);
  txnombre := stringreplace(txnombre, 'Ú', 'U', [rfReplaceAll]);
  txbic := qremesasccbic.AsString;
  txdireccion := ' ';
  txdireccion2 := ' . .';
  txdireccion := stringreplace(txdireccion, '/', '?', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'º', '?', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Ñ', 'N', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Á', 'A', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'É', 'E', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Í', 'I', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Ó', 'O', [rfReplaceAll]);
  txdireccion := stringreplace(txdireccion, 'Ú', 'U', [rfReplaceAll]);

  txdireccion2 := stringreplace(txdireccion2, '/', '?', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'º', '?', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Ñ', 'N', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Á', 'A', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'É', 'E', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Í', 'I', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Ó', 'O', [rfReplaceAll]);
  txdireccion2 := stringreplace(txdireccion2, 'Ú', 'U', [rfReplaceAll]);


  if Length(txmes) = 1 then
    txmes := '0' + txmes;
  if Length(txdia) = 1 then
    txdia := '0' + txdia;
  if Length(txhora) = 1 then
    txhora := '0' + txhora;
  if Length(txminutos) = 1 then
    txminutos := '0' + txminutos;
  if Length(txsegundos) = 1 then
    txsegundos := '0' + txsegundos;
  AssignFile(F, ExtractFilePath(Application.ExeName) + 'REMESA.XML');
  Rewrite(F);
    WriteLn(F,
    '<?xml version="1.0" encoding="UTF-8" standalone="no"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"><CstmrCdtTrfInitn><GrpHdr>');
  WriteLn(F, '<MsgId>' + txnif + 'AN0' + txanyo + txmes + txdia + txhora +
    txminutos + txsegundos + '</MsgId>');
  WriteLn(F, '<CreDtTm>' + txanyo + '-' + txmes + '-' + txdia + 'T' + txhora +
    ':' + txminutos + ':' + txsegundos + '</CreDtTm>');
  WriteLn(F, '<NbOfTxs>' + txnumlineas + '</NbOfTxs><CtrlSum>' + txcantidadtotal
    + '</CtrlSum>');
  WriteLn(F, '<InitgPty><Nm>' + txnombre + '</Nm><Id><OrgId><Othr><Id>' + txnif
    + 'AN0</Id></Othr></OrgId></Id></InitgPty></GrpHdr>');
  WriteLn(F, '<PmtInf><PmtInfId>SCT' + txanyo + txmes + txdia + txhora +
    txminutos + txsegundos +
    '001</PmtInfId><PmtMtd>TRF</PmtMtd><BtchBookg>true</BtchBookg><NbOfTxs>' +
    txnumlineas + '</NbOfTxs><CtrlSum>' + txcantidadtotal + '</CtrlSum>');
  WriteLn(F, '<ReqdExctnDt>' + txanyo + '-' + txmes + '-' + txdia +
    '</ReqdExctnDt><Dbtr><Nm>' + txnombre + '</Nm>');
  WriteLn(F, '<PstlAdr><Ctry>ES</Ctry><AdrLine>' + txdireccion +
    '</AdrLine><AdrLine>' + txdireccion2 +
    '</AdrLine></PstlAdr></Dbtr><DbtrAcct><Id>');
  WriteLn(F, '<IBAN>' + txcuenta +
    '</IBAN></Id><Ccy>EUR</Ccy></DbtrAcct><DbtrAgt><FinInstnId><BIC>' + txbic +
    '</BIC></FinInstnId></DbtrAgt><ChrgBr>SLEV</ChrgBr>');
//////// COBROS
  qrecibosdetalle.First;
  linea := 1;
  while not qrecibosdetalle.Eof do
  begin
    txlinea := FormatFloat(',0000', linea);
    txlinea := stringreplace(txlinea, '.', '', [rfReplaceAll]);
    txnombre:= qrecibosdetallenombre.AsString;
    txnombre := stringreplace(txnombre, 'Ñ', 'N', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, '/', '?', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'º', '?', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Á', 'A', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'É', 'E', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Í', 'I', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Ó', 'O', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'Ú', 'U', [rfReplaceAll]);
    txnombre := stringreplace(txnombre, 'ª', '?', [rfReplaceAll]);
    txdireccionadeudo := qrecibosdetalledireccion.AsString;
    txdireccionadeudo := stringreplace(txdireccionadeudo, '/', '?', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'º', '?', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Ñ', 'N', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Á', 'A', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'É', 'E', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Í', 'I', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Ó', 'O', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'Ú', 'U', [rfReplaceAll]);
    txdireccionadeudo := stringreplace(txdireccionadeudo, 'ª', '?', [rfReplaceAll]);

    txdireccion2adeudo:=  qrecibosdetalleCP.AsString + ' ' + qrecibosdetallePOBLACION.AsString;
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, '/', '?', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'º', '?', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Ñ', 'N', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Á', 'A', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'É', 'E', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Í', 'I', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Ó', 'O', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'Ú', 'U', [rfReplaceAll]);
    txdireccion2adeudo := stringreplace(txdireccion2adeudo, 'ª', '?', [rfReplaceAll]);
    txconcepto:=qrecibosdetalleconcepto.AsString;
    txconcepto := stringreplace(txconcepto, '/', '?', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'º', '?', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Ñ', 'N', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Á', 'A', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'É', 'E', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Í', 'I', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Ó', 'O', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'Ú', 'U', [rfReplaceAll]);
    txconcepto := stringreplace(txconcepto, 'ª', '?', [rfReplaceAll]);
    txpoblacion:=qrecibosdetallepoblacion.AsString;
    txpoblacion := stringreplace(txpoblacion, '/', '?', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'º', '?', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'Ñ', 'N', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'Á', 'A', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'É', 'E', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'Í', 'I', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'Ó', 'O', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'Ú', 'U', [rfReplaceAll]);
    txpoblacion := stringreplace(txpoblacion, 'ª', '?', [rfReplaceAll]);


    txtotalapagar := FormatFloat(',0.00',
      qrecibosdetalletotalcobrar.AsFloat);
    txtotalapagar := stringreplace(txtotalapagar, '.', '', [rfReplaceAll]);
    txtotalapagar := stringreplace(txtotalapagar, ',', '.', [rfReplaceAll]);
    WriteLn(F, '<CdtTrfTxInf><PmtId><EndToEndId>' +
      qrecibosdetallenif.asstring + '-20161-' + txanyo + txmes + txdia +
      txhora + txminutos + txsegundos + '-' + txlinea +
      '</EndToEndId></PmtId>');
    WriteLn(F,
      '<PmtTpInf><SvcLvl><Cd>SEPA</Cd></SvcLvl><CtgyPurp><Cd>SALA</Cd></CtgyPurp></PmtTpInf><Amt><InstdAmt Ccy="EUR">'
      + txtotalapagar + '</InstdAmt></Amt>');
    WriteLn(F, '<Cdtr><Nm>' + txnombre + '</Nm><PstlAdr><Ctry>ES</Ctry>');
    WriteLn(F, '<AdrLine>' + txdireccionadeudo + '</AdrLine><AdrLine>' +
      qrecibosdetalleCP.AsString + ' ' + txpoblacion
      + ' . .</AdrLine></PstlAdr></Cdtr>');
    WriteLn(F, '<CdtrAcct><Id><IBAN>' + qrecibosdetalleCUENTAPAGO.AsString +
      '</IBAN></Id></CdtrAcct><Purp><Cd>SALA</Cd></Purp><RmtInf><Ustrd>NOMINA</Ustrd></RmtInf></CdtTrfTxInf>');
    qrecibosdetalle.Next;
    linea := linea + 1;
    txdireccionadeudo:='';
    txdireccion2adeudo:='';
  end;
  WriteLn(F, '</PmtInf></CstmrCdtTrfInitn></Document><!-- JOLLY ACADEMY 34 XML -->');
  CloseFile(F);

end;

procedure Trecibosbanco.dsqremesasDataChange(Sender: TObject; Field: TField);
begin
   if qremesas.State in [dsbrowse] then
   begin
    qrecibosdetalle.Close;
    qrecibosdetalle.Open;
   end;

end;

procedure Trecibosbanco.FormShow(Sender: TObject);
begin
   fdesde.Date:=date();
   fhasta.Date:=date();
   qremesas.Open;
end;

procedure Trecibosbanco.qrecibosdetalleBeforeOpen(DataSet: TDataSet);
begin
   qrecibosdetalle.Prepare;
   qrecibosdetalle.ParamByName('REMESA').AsInteger:=qremesasid.AsInteger;

end;

end.
