unit uFormCadFornecedores;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uFormModelodeCadastro, Mask, DBCtrls, daDBEdit, StdCtrls, daEdit,
  Menus, DB, Buttons, ExtCtrls, FMTBcd, SqlExpr, Provider, DBClient, MaskUtils;

type
  TfrmCadFornecedores = class(TfrmModelodeCadastro)
    CDSCadFornecedores: TClientDataSet;
    DSPCadFornecedores: TDataSetProvider;
    SDSCadFornecedores: TSQLDataSet;
    SDSCadFornecedoresID_FORNECEDOR: TIntegerField;
    SDSCadFornecedoresFORNECEDOR: TStringField;
    SDSCadFornecedoresFANTASIA: TStringField;
    SDSCadFornecedoresCPFCNPJ: TStringField;
    SDSCadFornecedoresRGINSC: TStringField;
    SDSCadFornecedoresCEP: TStringField;
    SDSCadFornecedoresENDERECO: TStringField;
    SDSCadFornecedoresNUMERO: TSmallintField;
    SDSCadFornecedoresBAIRRO: TStringField;
    SDSCadFornecedoresCIDADE: TStringField;
    SDSCadFornecedoresESTADO: TStringField;
    SDSCadFornecedoresTELEFONE: TStringField;
    SDSCadFornecedoresFAX: TStringField;
    SDSCadFornecedoresCONTATO: TStringField;
    SDSCadFornecedoresEMAIL: TStringField;
    SDSCadFornecedoresFONECONTATO: TStringField;
    SDSCadFornecedoresFLAG: TSmallintField;
    CDSCadFornecedoresID_FORNECEDOR: TIntegerField;
    CDSCadFornecedoresFORNECEDOR: TStringField;
    CDSCadFornecedoresFANTASIA: TStringField;
    CDSCadFornecedoresCPFCNPJ: TStringField;
    CDSCadFornecedoresRGINSC: TStringField;
    CDSCadFornecedoresCEP: TStringField;
    CDSCadFornecedoresENDERECO: TStringField;
    CDSCadFornecedoresNUMERO: TSmallintField;
    CDSCadFornecedoresBAIRRO: TStringField;
    CDSCadFornecedoresCIDADE: TStringField;
    CDSCadFornecedoresESTADO: TStringField;
    CDSCadFornecedoresTELEFONE: TStringField;
    CDSCadFornecedoresFAX: TStringField;
    CDSCadFornecedoresCONTATO: TStringField;
    CDSCadFornecedoresEMAIL: TStringField;
    CDSCadFornecedoresFONECONTATO: TStringField;
    CDSCadFornecedoresFLAG: TSmallintField;
    qryFornecedor: TSQLQuery;
    Label2: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label7: TLabel;
    Label6: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    edtCodFor: TdaEdit;
    edtCPFCNPJFor: TdaDBEdit;
    edtRGIEFor: TdaDBEdit;
    edtDescFor: TdaDBEdit;
    edtFantasiaFor: TdaDBEdit;
    edtCEPFor: TdaDBEdit;
    edtEndereFor: TdaDBEdit;
    edtNumFor: TdaDBEdit;
    edtBaiFor: TdaDBEdit;
    edtCidFor: TdaDBEdit;
    edtUFFor: TdaDBEdit;
    edtFonFor: TdaDBEdit;
    edtFaxFor: TdaDBEdit;
    edtContFor: TdaDBEdit;
    edtEmailFor: TdaDBEdit;
    edtFonCont: TdaDBEdit;
    procedure CDSCadFornecedoresAfterPost(DataSet: TDataSet);
    procedure CDSCadFornecedoresBeforePost(DataSet: TDataSet);
    procedure btnNovoClick(Sender: TObject);
    procedure btnSalvarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure btnAlterarClick(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
    procedure btnPesquisarClick(Sender: TObject);
    procedure F10Click(Sender: TObject);
  private
    procedure LimpaFormulario;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCadFornecedores: TfrmCadFornecedores;

implementation

uses uDM, uFuncoes, uFormPesqFornecedores;

{$R *.dfm}

procedure TfrmCadFornecedores.btnAlterarClick(Sender: TObject);
begin
  inherited;
  edtDescFor.SetFocus;
  btnAlterar.Enabled := False;
end;

procedure TfrmCadFornecedores.btnCancelarClick(Sender: TObject);
begin
  inherited;
  LimpaFormulario;
end;

procedure TfrmCadFornecedores.btnExcluirClick(Sender: TObject);
begin
  inherited;
  if Confirma then
  begin
    Confirma := False;
    edtCodFor.Clear;
    btnNovo.SetFocus;
  end;
end;

procedure TfrmCadFornecedores.btnNovoClick(Sender: TObject);
begin
  inherited;
  qryFornecedor.Close;
  qryFornecedor.Open;
  edtCodFor.Text := qryFornecedor.FieldByName('id_fornecedor').AsString;
  edtDescFor.SetFocus;
end;

procedure TfrmCadFornecedores.btnPesquisarClick(Sender: TObject);
begin
  inherited;
  if btnPesquisar.Enabled = True then
    begin
      Application.CreateForm(TfrmPesqFornecedores,frmPesqFornecedores);
      if frmPesqFornecedores.ShowModal = mrOk then
      begin
        SDSCadFornecedores.Close;
        CDSCadFornecedores.Close;
        SDSCadFornecedores.ParamByName('id_fornecedor').AsInteger := frmPesqFornecedores.CDSPesqFornecedoresID_FORNECEDOR.AsInteger;
        CDSCadFornecedores.Open;

        edtCodFor.Text := frmPesqFornecedores.CDSPesqFornecedoresID_FORNECEDOR.AsString;

        AtivarBotoes(True);   
        btnSalvar.Enabled := False;  

        edtCodFor.Enabled := False;
        frmPesqFornecedores.CDSPesqFornecedores.Close;
        PanelCenter.Enabled := False;
        btnAlterar.SetFocus;

      end;
      FreeAndNil(frmPesqFornecedores);
    end;
end;

procedure TfrmCadFornecedores.btnSalvarClick(Sender: TObject);
begin
  inherited;
  TRY
    dstGenerico.DataSet.Post;
    AtivarBotoes(False);
    CaixadeMensagemAviso('Registro Gravado com Sucesso!');
    LimpaFormulario;
  Except
    ON EDataBaseError DO
      BEGIN
        CaixadeMensagemAviso('Campo [ FORNECEDOR ] deve ser Preenchido!');
      END;
  END;
end;

procedure TfrmCadFornecedores.LimpaFormulario;
begin
  CDSCadFornecedores.Close;
  edtCodFor.Clear;
  PanelCenter.Enabled := False;
  btnNovo.SetFocus;
end;

procedure TfrmCadFornecedores.CDSCadFornecedoresAfterPost(DataSet: TDataSet);
begin
  inherited;
  CDSCadFornecedores.ApplyUpdates(-1);
end;

procedure TfrmCadFornecedores.CDSCadFornecedoresBeforePost(DataSet: TDataSet);
begin
  inherited;
  CDSCadFornecedoresID_FORNECEDOR.AsInteger := StrToInt(edtCodFor.Text);
end;

procedure TfrmCadFornecedores.F10Click(Sender: TObject);
begin
  inherited;
  btnPesquisarClick(btnPesquisar);
end;

end.
