unit UDataModulo;

interface

uses
  SysUtils,Classes,DBTables,DB,Dialogs,Variants,Forms;

type
  TModulo = class(TDataModule)
    TBServico: TTable;
    TBValores: TTable;
    TBCliente: TTable;
    Query1: TQuery;
    TbReceber: TTable;
    TbExclusoes: TTable;
    TbExclusoesCodigo: TFloatField;
    TbExclusoesDataExclusao: TDateField;
    TbExclusoesHoraExclusao: TTimeField;
    TbExclusoesJustificativa: TMemoField;
    TbExclusoesValor: TCurrencyField;
    TbExclusoesVeiculo: TStringField;
    TbExclusoesPlaca: TStringField;
    TbRecebimentos: TTable;
    TbRecebimentosCodigo: TFloatField;
    TbRecebimentosData: TDateField;
    TbRecebimentosNatureza: TStringField;
    TbRecebimentosCliente: TStringField;
    TbRecebimentosValor: TCurrencyField;
    TbCaixa: TTable;
    TbRecebimentosEntradaN: TFloatField;
    TbCaixaCodigo: TFloatField;
    TbCaixaData: TDateField;
    TbCaixaTotal: TCurrencyField;
    TBClienteCodCliente: TFloatField;
    TBClienteNome: TStringField;
    TBClienteData: TDateField;
    TBClienteNomeFantasia: TStringField;
    TBClienteCGC: TStringField;
    TBClienteIE: TStringField;
    TBClienteEndereco: TStringField;
    TBClienteBairro: TStringField;
    TBClienteCidade: TStringField;
    TBClienteCEP: TStringField;
    TBClienteUf: TStringField;
    TBClienteFone: TStringField;
    TBClienteFoneFax: TStringField;
    TBClienteContato: TStringField;
    TBClienteEmail: TStringField;
    TBServicoEntradaN: TFloatField;
    TBServicoPlaca: TStringField;
    TBValoresComentario1: TMemoField;
    TBValoresComentario2: TMemoField;
    TBValoresComentario3: TMemoField;
    TBServicoVrdiaria: TCurrencyField;
    TBServicoVrhora: TCurrencyField;
    TBServicoVritens: TCurrencyField;
    TBServicoVrcera: TCurrencyField;
    TBServicoVrmotor: TCurrencyField;
    TBServicoVrterceiro: TCurrencyField;
    TBServicoSubTotal: TCurrencyField;
    TBServicoDesconto: TCurrencyField;
    TBServicoTotal: TCurrencyField;
    TBServicoQtddias: TFloatField;
    TBServicoQtdhora: TFloatField;
    TBServicoVeculo: TStringField;
    TBServicoTipo: TStringField;
    TBServicoItens: TStringField;
    TBServicoComplemento: TStringField;
    TBServicoDataentrada: TDateField;
    TBServicoHoraentrada: TTimeField;
    TBServicoDatasaida: TDateField;
    TBServicoHorasaida: TTimeField;
    TBServicoConvenio: TStringField;
    TBServicoCodCliente: TFloatField;
    TBServicoEspecie: TStringField;
    TBValoresNome: TStringField;
    TBValoresEndereco: TStringField;
    TBValoresFone: TStringField;
    TBValoresEmail: TStringField;
    TBValoresClausula: TMemoField;
    TBValoresVrInterna: TCurrencyField;
    TBValoresVrExterna: TCurrencyField;
    TBValoresVrCompleta: TCurrencyField;
    TBValoresVrcera: TCurrencyField;
    TBValoresVrmotor: TCurrencyField;
    TBValoresVrhora: TCurrencyField;
    TBValoresVrdiaria: TCurrencyField;
    TBValoresVrterceiro: TCurrencyField;
    TBServicoVencPago: TDateTimeField;
    procedure TBServicoAfterScroll(DataSet: TDataSet);
    procedure TBServicoBeforeDelete(DataSet: TDataSet);
    procedure TBServicoBeforeEdit(DataSet: TDataSet);
    procedure TBClienteBeforeEdit(DataSet: TDataSet);
    procedure TBServicoAfterDelete(DataSet: TDataSet);
    procedure TBServicoAfterOpen(DataSet: TDataSet);
    procedure TBServicoBeforeInsert(DataSet: TDataSet);
    procedure TBServicoAfterPost(DataSet: TDataSet);
    procedure TBServicoBeforePost(DataSet: TDataSet);
    procedure TBClienteAfterOpen(DataSet: TDataSet);
    procedure TBClienteBeforePost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Modulo: TModulo;

implementation

uses uservico, uvalor;

{$R *.dfm}
function VerCampo:Boolean;
begin
  if ((Modulo.TBServico.State=dsInsert)or(Modulo.TBServico.State=dsEdit))and
     ((Modulo.TBServico['Veículo']=null) or (Modulo.TBServico['Placa']=null)) then
    result:=True
  else
    result:=False;
end;
procedure TModulo.TBServicoAfterScroll(DataSet: TDataSet);
begin
    FServico.btnAvancar.Enabled := True;
    FServico.btnUltimo.Enabled := True;
    FServico.btnPrimeiro.Enabled := True;
    FServico.btnVoltar.Enabled := True;

    if Modulo.TBServico.Bof then
    begin
      FServico.btnPrimeiro.Enabled := False;
      FServico.btnVoltar.Enabled := False;
    end;
    if Modulo.TBServico.Eof then
    begin
      FServico.btnAvancar.Enabled := False;
      FServico.btnUltimo.Enabled := False;
    end;
end;
procedure TModulo.TBServicoBeforeDelete(DataSet: TDataSet);
var
Justificativa:String;
begin
  if InputQuery('Motivo da exclusão', 'Digite o Motivo da exclusão do Registro.'
                , Justificativa) then
  begin
    if Justificativa<>'' then
    begin
      TbExclusoes.Open;
      TbExclusoes.Refresh;
      TbExclusoes.Insert;
      with query1 do
      begin
        close;
        sql.clear;
        sql.add('select max(Codigo) as codigo from tbExclusoes');
        open;
      end;

      if Query1['Codigo']<>null then
        TbExclusoes['Codigo']:=Query1['codigo']+1
      else
        TbExclusoes['Codigo']:='1';

      TbExclusoes['DataExclusao']:=DateToStr(now);
      TbExclusoes['HoraExclusao']:=TimeToStr(now);
      TbExclusoes['Valor']:=TbServico['Total'];
      TbExclusoes['Justificativa']:= Justificativa;
      tbExclusoes['Veiculo']:=TbServico['Veículo'];
      TbExclusoes['Placa']:=TbServico['Placa'];
      TbExclusoes.Post;
      TbExclusoes.Close;
      Query1.Close;
      TbRecebimentos.open;
      if TbRecebimentos.Locate('EntradaN',TBServico['EntradaN'],[])then
      begin
        TbRecebimentos.Delete;
        TbRecebimentos.Refresh;
      end;
      TbRecebimentos.Close;
    end
    else
    begin
      ShowMessage('É necessário digitar uma justificativa para Excluir o Registro.');
      Abort;
    end;
  end
  else
    Abort;

end;

procedure TModulo.TBServicoBeforeEdit(DataSet: TDataSet);
begin
  if TbServico['HoraSaida']<>null then
  begin
    Application.MessageBox('Esta Conta já foi fechada.'+#13+
                           'Não é possivel mais editá-la.','Atenção',0+48);
    Abort;
  end
  else
  begin
    FServico.btnGravar.Enabled:=True;
    FServico.btncancelar.enabled := true;
    FServico.BtCancela.Enabled:=True;
    FServico.BtGrava.Enabled:=True;
  end;
end;
procedure TModulo.TBClienteBeforeEdit(DataSet: TDataSet);
begin
  FCadastro.btngravar.enabled := True;
  FCadastro.btncancelar.enabled := True;
end;

procedure TModulo.TBServicoAfterDelete(DataSet: TDataSet);
begin
  FServico.BtnCancelar.Enabled:=False;
  FServico.BtnGravar.Enabled:=False;
  FServico.BtCancela.Enabled:=False;
  FServico.BtGrava.Enabled:=False;
  TbServico.Last;
end;

procedure TModulo.TBServicoAfterOpen(DataSet: TDataSet);
begin
  FServico.btngravar.enabled := false;
  FServico.btncancelar.enabled := false;
  FServico.BtCancela.Enabled:=False;
  FServico.BtGrava.Enabled:=False;
  TbServico.last;

end;
procedure TModulo.TBServicoBeforeInsert(DataSet: TDataSet);
begin
  FServico.btnGravar.Enabled:=True;
  FServico.btncancelar.enabled :=true;
  FServico.BtCancela.Enabled:=True;
  FServico.BtGrava.Enabled:=True;
end;

procedure TModulo.TBServicoAfterPost(DataSet: TDataSet);
begin
  FServico.btngravar.Enabled:=False;
  FServico.btncancelar.Enabled:=False;
  FServico.BtCancela.Enabled:=False;
  FServico.BtGrava.Enabled:=False;
end;

procedure TModulo.TBServicoBeforePost(DataSet: TDataSet);
begin
  if verCampo then
  begin
    if Modulo.TBServico.State=dsEdit then
      FServico.ErroCampoexecute(self);
    if Fservico.PageControl1.ActivePage=Fservico.TabSheet1 then
      Abort
    else
    begin
      Fservico.PageControl1.ActivePage:=Fservico.TabSheet1;
      Abort;
    end;
  end;
end;

procedure TModulo.TBClienteAfterOpen(DataSet: TDataSet);
begin
  TbCliente.Refresh;
  TbCliente.Last;
end;

procedure TModulo.TBClienteBeforePost(DataSet: TDataSet);
begin
  if Modulo.TBCliente['NomeFantasia']=null then
  begin
    ShowMessage('O Campo Nome não pode ser nulo.');
    Abort;
  end;
end;

end.

