unit Form.Main;

interface

uses
  Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes, System.Actions,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ActnList,
  Vcl.StdCtrls,
  System.JSON, ufuncional.gateway.sales, uClasse.JSON.Recuperacao, Vcl.ExtCtrls,
  Vcl.ComCtrls, uClasse.Json.confirmacao, Vcl.OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    mmoToken: TMemo;
    btnConfirmacao: TButton;
    btnRecuperacao: TButton;
    btnToken: TButton;
    pbmFuncionalGatewaySales1: TpbmFuncionalGatewaySales;
    edtCNPJ: TLabeledEdit;
    edtCartao: TLabeledEdit;
    edtAutorizacao: TLabeledEdit;
    edtData: TDateTimePicker;
    LinkLabel1: TLinkLabel;
    Bevel1: TBevel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Memo1: TMemo;
    Label1: TLabel;
    WebBrowser1: TWebBrowser;
    Label2: TLabel;
    LabeledEdit1: TLabeledEdit;
    procedure FormCreate(Sender: TObject);
    procedure actQueryUserExecute(Sender: TObject);
    procedure actQueryRepoListExecute(Sender: TObject);
    procedure actGetIssuesListExecute(Sender: TObject);
    procedure pbmFuncionalGatewaySales1Resposta(pMensagemOperador: AnsiString; Funcao: TFuncionalTipoOperacao; RetornoOK: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Label1DblClick(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  System.NetConsts,
  System.Net.HttpClient,
  System.Net.URLClient,
  UrlMon, System.Generics.Collections ;

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  mmoToken.Lines.SaveToFile('.\token.txt');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Memo1.Align := alClient;
  if FileExists('.\token.txt') then
     mmoToken.Lines.LoadFromFile('.\token.txt');
  edtData.Date := Now;
  Memo1.Clear;
  PageControl1.ActivePageIndex := 0;
end;

procedure TForm1.Label1DblClick(Sender: TObject);
begin
  PageControl1.ActivePageIndex := 0;
  WebBrowser1.Navigate(Label1.Caption)
end;

procedure TForm1.pbmFuncionalGatewaySales1Resposta(pMensagemOperador: AnsiString; Funcao: TFuncionalTipoOperacao; RetornoOK: Boolean);
var
  i: integer;
begin

  if pMensagemOperador <> '' then
  begin
    ShowMessage(pMensagemOperador);
    exit;
  end;

  if Funcao = fncAutenticacao then
  begin
     mmoToken.Clear;
     mmoToken.Lines.Add( pbmFuncionalGatewaySales1.Identificacao.Token);
     mmoToken.Lines.SaveToFile('.\token.txt');
  end;


  if Funcao = fncRecuperacao then
  begin

    try

      Memo1.Lines.Add('--------------------[RECUPERAÇÃO]-----------------------------------------------------------------------');Memo1.Lines.Add('Data................: ' + FormatDateTime('dd/mm/yyyy hh:mm:ss', pbmFuncionalGatewaySales1.Autorizacao.Data.
      Sales_Transaction.CreatedAt));
      Memo1.Lines.Add('Autorizacao ID......: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.AuthorizationID.ToString());
      Memo1.Lines.Add('Valor Total.........: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.TotalValue.ToString);
      Memo1.Lines.Add('Pagamento A Vista...: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.MoneyPaidValue.ToString);
      Memo1.Lines.Add('Pagamento Cartao/Outro' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.CardPaidValue.ToString);

      // percorrendo os prodrutos
      for i := 0 to pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Count - 1 do
      begin
        Memo1.Lines.Add('================================= Produtos =========================================');
        Memo1.Lines.Add('Ean............: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].Ean);
        Memo1.Lines.Add('Descricao......: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].Description);
        Memo1.Lines.Add('PMC............: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MaxConsumerPrice.ToString);
        Memo1.Lines.Add('Preço Unitário.: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].UnitPrice.ToString);
        Memo1.Lines.Add('Quantidade.....: ' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].Quantity.ToString);
        try
          if pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Quantity > 0 then
          begin
            Memo1.Lines.Add('+++++++++++++++++++++++++++++  R E C E I T A ++++++++++++++++++++++++++++++++++++++');
            //Memo1.Lines.Add('Receita Data.:' + FormatDateTime('DD/MM/YYYY', pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Date));
            Memo1.Lines.Add('Receita Data.:' + DateToStr(pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Date));
            Memo1.Lines.Add('Receita Qtde.:' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Quantity.ToString);

            Memo1.Lines.Add('----------------Dados do prescritor -----------');
            Memo1.Lines.Add('Conselho.....:' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Prescriber.Council);
            Memo1.Lines.Add('No. Registro.:' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Prescriber.RegisterNumber.ToString);
            Memo1.Lines.Add('UF Conselho..:' + pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Items[i].MedicalPrescription.Prescriber.StateAbbr);
            Memo1.Lines.Add('');
            Memo1.Lines.Add('');
            Memo1.Lines.Add('');

          end;
        except

        end;
      end;

    except

    end;

  end;



  if Funcao = fncConfirmacao then
  begin

    try
      Memo1.Lines.Add('--------------------[CONFIRMAÇÃO]-----------------------------------------------------------------------');
      Memo1.Lines.Add('Autorizacao ID......: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.AuthorizationID.ToString);
      Memo1.Lines.Add('Autorizacao ID......: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.CardBalance.ToString);
      Memo1.Lines.Add('Autorizacao ID......: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.CardPaidValue.ToString);
      Memo1.Lines.Add('Pagamento Dinheiro..: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.MoneyPaidValue.ToString);
      Memo1.Lines.Add('Data Venda..........: ' + FormatDateTime('dd/mm/yy hh:mm:ss',pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.CreatedAt) );
      Memo1.Lines.Add('Autorizacao.........: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.TotalValue.ToString);
      Memo1.Lines.Add('Status..............: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.Status);
      Memo1.Lines.Add('Status Code.........: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.StatusCode.ToString );
      Memo1.Lines.Add('Status Mensagem.....: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.StatusMessage );
      Memo1.Lines.Add('');
      Memo1.Lines.Add('--------------------------   R E C I B O   ------------------------------------');
      Memo1.Lines.Add(pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.Receipt);
      Memo1.Lines.Add('');
      Memo1.Lines.Add('-------------------------------------------------------------------------------------------');

      // percorrendo os prodrutos
      for i := 0 to pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.Products.Count - 1 do
      begin
        Memo1.Lines.Add('================================= Produtos =========================================');
        Memo1.Lines.Add('Ean............: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.Products.Items[i].Ean);
        Memo1.Lines.Add('Descricao......: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.Products.Items[i].Description);
        Memo1.Lines.Add('Quantidade.....: ' + pbmFuncionalGatewaySales1.Confirmacao.Data.Sales_ConfirmPreAuthorizedSale.Products.Items[i].Quantity.ToString);
      end;

    except

    end;

  end;

  Memo1.Lines.Add('----------------------');


end;

procedure TForm1.actQueryUserExecute(Sender: TObject);
begin
  pbmFuncionalGatewaySales1.Autenticacao;
end;

procedure TForm1.actQueryRepoListExecute(Sender: TObject);
begin



  //Exemplo de como excluir um  produto antes da confirmacao
  //pbmFuncionalGatewaySales1.Autorizacao.Data.Sales_Transaction.Products.Delete(1);

  pbmFuncionalGatewaySales1.Identificacao.Token := mmoToken.Text;
  pbmFuncionalGatewaySales1.ConfirmaAutorizacao(edtCartao.Text, edtAutorizacao.Text, now);

end;

procedure TForm1.actGetIssuesListExecute(Sender: TObject);
  var
    cartao, autorizacao:string;
begin
  PageControl1.ActivePageIndex := 1;

  pbmFuncionalGatewaySales1.Identificacao.Token := mmoToken.Text;
  pbmFuncionalGatewaySales1.RecuperaAutorizacao(edtCartao.Text, edtAutorizacao.Text, edtData.Date);

end;

end.
