unit DenegarNF;

interface

uses
  Windows, system.UItypes, IB_Access,  Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TelaComum, ImgList, ExtCtrls, StdCtrls, Buttons, IB_Grid, Mask,
  IB_Controls, IB_Components,Rotinas, ComCtrls, OleCtrls, SHDocVw,pcnConversao,
  IB_EditButton, System.ImageList;

type
  TfrmDenegar_NF = class(TfrmTelaComum)
    qryNF: TIB_Query;
    dtNF: TIB_DataSource;
    qryHistorico: TIB_Query;
    OpenDialog1: TOpenDialog;
    Seleciona: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel6: TPanel;
    PageControl7: TPageControl;
    TabSheet24: TTabSheet;
    MemoResp: TMemo;
    TabSheet25: TTabSheet;
    WBResposta: TWebBrowser;
    TabSheet26: TTabSheet;
    memoLog: TMemo;
    TabSheet27: TTabSheet;
    trvwNFe: TTreeView;
    GroupBox3: TGroupBox;
    dbNF: TIB_Edit;
    btnPesquisaNF: TBitBtn;
    IB_Edit1: TIB_Edit;
    IB_Edit3: TIB_Edit;
    GroupBox7: TGroupBox;
    edtCidadeTransporte: TIB_Edit;
    dbNomeFantasia: TIB_Edit;
    dbInscricao: TIB_Edit;
    edtUF: TIB_LookupCombo;
    IB_Edit2: TIB_Edit;
    procedure BtGravarClick(Sender: TObject);
    procedure btnPesquisaNFClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure dbNFExit(Sender: TObject);
  private
    procedure GeraValidaXML;
    procedure CancelaNF;
    procedure LoadXML(MyMemo: TMemo; MyWebBrowser: TWebBrowser);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDenegar_NF: TfrmDenegar_NF;
  NomeArquivo_Cancelar:String;
implementation

uses Justificativa, DataModulo, ModeloPesquisa, cadNF, ConfigacaoSistema,
  DataModulo_NFe;

{$R *.dfm}

procedure TfrmDenegar_NF.BtGravarClick(Sender: TObject);
begin
  inherited;

   if qryNF.FieldByName('PROTOCOLO_AUTORIZACAO').IsNull then
   begin
     if (MessageDlg('Esta NF não parece ter sido transmitida pelo sistema,'+#13+#10+'não sendo necessário denegar, mesmo'+#13+#10+'assim deseja denegar a NF?', mtConfirmation, [mbYes, mbNo], 0) = mrNo) then
       Exit;
   end;

   frmJustificativa := TfrmJustificativa.Create(Application);
   if frmJustificativa.ShowModal = mrOk then
   begin
      qryNF.Edit;
      qryNF.FieldByName('STATUS').Value            := 'D';
      qryNF.FieldByName('COD_USUARIO_CANCELADO').Value:= Dados.qrUsuario.FieldByName('COD_USUARIO').Value;
      qryNF.FieldByName('MOTIVO_CANCELADO').Value     := 'DENEGADA';
      qryNF.Post;

      qryHistorico.Insert;
      qryHistorico.FieldByName('DATA_HISTORICO').Value:=Date;
      qryHistorico.FieldByName('COD_USUARIO').Value:= Dados.qrUsuario.FieldByName('COD_USUARIO').Value;
      qryHistorico.FieldByName('COD_FILIAL_NF').Value:= dados.qrFilial.FieldByName('COD_FILIAL').Value;
      qryHistorico.FieldByName('COD_NF').Value:=qryNF.FieldByName('NUMERO').Value;
      qryHistorico.FieldByName('HORA_HISTORICO').Value:= Time;
      qryHistorico.FieldByName('DESCRICAO').Value:= 'Denegou a NF: ' + qryNF.FieldByName('NUMERO').AsString + ' da filial ' + qryNF.FieldByName('cod_filial').AsString;
      qryHistorico.Post;

      descricao_auditoria :=qryHistorico.FieldByName('DESCRICAO').AsString;
      GravaAuditoria(False,qryNF);

      MessageDlg('Realizado! Confirme consultando a NF', mtInformation, [mbOK], 0);

   end;
   frmJustificativa.Destroy;
end;

procedure TfrmDenegar_NF.btnPesquisaNFClick(Sender: TObject);
begin
  inherited;
   frmModeloPesquisa                    := TfrmModeloPesquisa.Create(Application);
   frmModeloPesquisa.Tabela             := qryNF;
   frmModeloPesquisa.lblTitulo.Caption  := 'Pesquisa NF';

   if frmModeloPesquisa.ShowModal = mrOk then
   begin
       qryNF.Locate('NUMERO',frmModeloPesquisa.TbPrincipal.FieldByName('NUMERO').AsString,[]);

{
    qryNF.Filtered:=False;
    qryNF.Filter:= ('NUMERO='+QuotedStr(frmModeloPesquisa.TbPrincipal.FieldByName('NUMERO').AsString));
    qryNF.Filtered:=True;}
   end;
   frmModeloPesquisa.Destroy;

   Screen.OnActiveControlChange         := TrocaCor;

end;

procedure TfrmDenegar_NF.FormCreate(Sender: TObject);
begin
  inherited;
  qryNF.Open;
  if not qryNF.Bof then
  begin
     qryNF.First;
     qryNF.Prior;
  end;
end;

procedure TfrmDenegar_NF.GeraValidaXML;
begin
   frmNF := TfrmNF.Create(Application);
   frmNF.TbPrincipal.Filtered:=False;
   frmNF.TbPrincipal.Filter:='NUMERO='+QUotedStr(qryNF.FIeldbyName('NUMERO').AsString);
   frmNF.TbPrincipal.Filtered:=True;
   frmNF.AtualizaTela;
   frmNF.btnGerarXMLClick(self);
   frmNF.btnValidarClick(self);
   frmNF.Destroy;

end;

procedure TfrmDenegar_NF.CancelaNF;
var
  idLote,vAux : String;
begin

     DMNFE.NFe.NotasFiscais.Clear;
     DMNFE.NFe.NotasFiscais.LoadFromFile(NomeArquivo_Cancelar);

    idLote := '1';
    vAux:=qryNF.FieldByName('MOTIVO_CANCELADO').AsString;

    DMNFE.NFe.EventoNFe.Evento.Clear;
    DMNFE.NFe.EventoNFe.idLote := StrToInt(idLote) ;
    with DMNFE.NFe.EventoNFe.Evento.Add do
    begin
     infEvento.dhEvento := now;
     infEvento.tpEvento := teCancelamento;
     infEvento.detEvento.xJust := vAux;
    end;
    DMNFE.NFe.EnviarEvento(StrToInt(idLote));

    MemoResp.Lines.Text :=  UTF8Encode(DMNFE.NFe.WebServices.EnvEvento.RetWS);
//    memoRespWS.Lines.Text :=  UTF8Encode(DMNFE.NFe.WebServices.EnvEvento.RetornoWS);
    LoadXML(MemoResp, WBResposta);
    ShowMessage(IntToStr(DMNFE.NFe.WebServices.EnvEvento.cStat));
    ShowMessage(DMNFE.NFe.WebServices.EnvEvento.EventoRetorno.retEvento.Items[0].RetInfEvento.nProt);
end;



procedure TfrmDenegar_NF.LoadXML(MyMemo: TMemo; MyWebBrowser: TWebBrowser);
begin
  MyMemo.Lines.SaveToFile(ExtractFileDir(application.ExeName)+'\temp.xml');
  MyWebBrowser.Navigate(ExtractFileDir(application.ExeName)+'\temp.xml');
end;

procedure TfrmDenegar_NF.dbNFExit(Sender: TObject);
begin
  inherited;
  qryNF.Filtered:=False;
  qryNF.Filter:= 'NUMERO='+QuotedStr(dbNF.Text);
  qryNF.Filtered:=True;
end;

end.
