{ *******************************************************************************
Title: T2TiPDV
Description: Pesquisa por cliente e importação para a venda.

The MIT License

Copyright: Copyright (C) 2014 T2Ti.COM

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

The author may be contacted at:
t2ti.com@gmail.com

@author Albert Eije
@version 2.0
******************************************************************************* }
unit UImportaCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, UBase,
  Dialogs, Grids, DBGrids, JvExDBGrids, JvDBGrid, StdCtrls, JvExStdCtrls,
  JvButton, JvCtrls, Buttons, JvExButtons, JvBitBtn, pngimage, ExtCtrls, Mask,
  JvEdit, JvValidateEdit, JvDBSearchEdit, DB, Provider, DBClient, FMTBcd,
  SqlExpr, JvEnterTab, JvComponentBase, Tipos, JvDBUltimGrid, Biblioteca,
  Controller;

type
  TFImportaCliente = class(TFBase)
    Image1: TImage;
    Panel1: TPanel;
    Label1: TLabel;
    JvEnterAsTab1: TJvEnterAsTab;
    botaoConfirma: TJvBitBtn;
    botaoCancela: TJvImgBtn;
    DSCliente: TDataSource;
    EditLocaliza: TEdit;
    SpeedButton1: TSpeedButton;
    Label2: TLabel;
    CDSCliente: TClientDataSet;
    GridPrincipal: TJvDBUltimGrid;
    procedure Localiza;
    procedure Confirma;
    procedure FormActivate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure GridPrincipalKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure botaoConfirmaClick(Sender: TObject);
  private
    { Private declarations }
  public
    CpfCnpjPassou, QuemChamou: string;
    IdClientePassou: Integer;
    { Public declarations }
  end;

var
  FImportaCliente: TFImportaCliente;

implementation

uses
    ClienteVO,
    ClienteController,
    UDataModule, UCaixa, UIdentificaCliente, UNotaFiscal;

{$R *.dfm}

{$Region 'Infra'}
procedure TFImportaCliente.FormActivate(Sender: TObject);
begin
  EditLocaliza.SetFocus;
  Color := StringToColor(Sessao.Configuracao.CorJanelasInternas);

  // Configura a Grid do Cliente
  ConfiguraCDSFromVO(CDSCliente, TClienteVO);
  ConfiguraGridFromVO(GridPrincipal, TClienteVO);
end;

procedure TFImportaCliente.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TFImportaCliente.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_F2 then
    Localiza;

  if Key = VK_F12 then
    Confirma;
end;

procedure TFImportaCliente.GridPrincipalKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
    EditLocaliza.SetFocus;
end;
{$EndRegion 'Infra'}

{$Region 'Pesquisa e Confirmação'}
procedure TFImportaCliente.botaoConfirmaClick(Sender: TObject);
begin
  Confirma;
end;

procedure TFImportaCliente.Confirma;
begin
  if Sessao.MenuAberto = snNao then
  begin
    if CDSCliente.FieldByName('CPF_CNPJ').AsString = '' then
      Application.MessageBox('Cliente sem CPF ou CNPJ cadastrado.', 'Informação do Sistema', MB_OK + MB_ICONINFORMATION)
    else
    begin
      if QuemChamou = 'NF' then
      begin
        FNotaFiscal.editCpfCnpj.Text := CDSCliente.FieldByName('CPF_CNPJ').AsString;
        FNotaFiscal.EditNome.Text := CDSCliente.FieldByName('NOME').AsString;
        FNotaFiscal.editCodigoCliente.asinteger := CDSCliente.FieldByName('ID').asinteger;
      end
      else if QuemChamou = 'IdentificaCliente' then
      begin
        FIdentificaCliente.editCpfCnpj.Text := CDSCliente.FieldByName('CPF_CNPJ').AsString;
        FIdentificaCliente.EditNome.Text := CDSCliente.FieldByName('NOME').AsString;
        FIdentificaCliente.editIDCliente.asinteger := CDSCliente.FieldByName('ID').asinteger;
      end;
    end;
  end;
  Close;
end;

procedure TFImportaCliente.Localiza;
var
  ProcurePor, Filtro: String;
begin
  ProcurePor := '%' + EditLocaliza.Text + '%';
  Filtro := 'NOME LIKE '+ QuotedStr(ProcurePor);

  TClienteController.SetDataSet(CDSCliente);
  TController.ExecutarMetodo('ClienteController.TClienteController', 'Consulta', [Filtro, '0', False], 'GET', 'Lista');
end;

procedure TFImportaCliente.SpeedButton1Click(Sender: TObject);
begin
  Localiza;
end;
{$EndRegion 'Pesquisa e Confirmação'}

end.
