unit uFormCadLotes;

interface

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

type
  TfrmCadLotes = class(TfrmModelodeCadastro)
    Label2: TLabel;
    edtCodLote: TdaEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    edtDescricao: TdaDBEdit;
    Label7: TLabel;
    SDSCadLotes: TSQLDataSet;
    DSPCadLotes: TDataSetProvider;
    CDSCadLotes: TClientDataSet;
    SDSCadLotesID: TIntegerField;
    SDSCadLotesID_LOTE: TIntegerField;
    SDSCadLotesDESCRICAO: TStringField;
    SDSCadLotesFLAG: TSmallintField;
    CDSCadLotesID: TIntegerField;
    CDSCadLotesID_LOTE: TIntegerField;
    CDSCadLotesDESCRICAO: TStringField;
    CDSCadLotesFLAG: TSmallintField;
    qryLotes: TSQLQuery;
    Label5: TLabel;
    btnIncluir: TButton;
    btnDeletar: TButton;
    GroupBox1: TGroupBox;
    DBGrid1: TDBGrid;
    SDSCadTanques: TSQLDataSet;
    DSPCadTanques: TDataSetProvider;
    CDSCadTanques: TClientDataSet;
    DSCadTanques: TDataSource;
    SDSCadTanquesID_TANQUE: TIntegerField;
    SDSCadTanquesQTDE_ESTOCADA: TIntegerField;
    SDSCadTanquesVOLUME_TANQUE: TFMTBCDField;
    SDSCadTanquesID_LOTE: TIntegerField;
    CDSCadTanquesID_TANQUE: TIntegerField;
    CDSCadTanquesQTDE_ESTOCADA: TIntegerField;
    CDSCadTanquesVOLUME_TANQUE: TFMTBCDField;
    CDSCadTanquesID_LOTE: TIntegerField;
    edtCodTanque: TdaEdit;
    edtQuantEstocada: TdaEdit;
    edtVolume: TdaEdit;
    SDSCadTanquesFLAG: TSmallintField;
    CDSCadTanquesFLAG: TSmallintField;
    Label8: TLabel;
    DBText1: TDBText;
    CDSCadTanquesTotalEstocada: TAggregateField;
    procedure CDSCadLotesAfterPost(DataSet: TDataSet);
    procedure CDSCadLotesBeforePost(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);
    procedure btnIncluirClick(Sender: TObject);
    procedure btnDeletarClick(Sender: TObject);
    procedure CDSCadTanquesBeforeDelete(DataSet: TDataSet);
  private
    procedure LimpaFormulario;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCadLotes: TfrmCadLotes;

implementation

uses uDM, uFuncoes, uFormPesqLotes;

{$R *.dfm}

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

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

procedure TfrmCadLotes.btnDeletarClick(Sender: TObject);
begin
  inherited;

  if not CDSCadTanques.IsEmpty then
  begin
    if CDSCadTanques.Locate('ID_TANQUE', CDSCadTanquesID_TANQUE.AsInteger,[]) THEN
    begin
      // CDSCadTanques.Prior;
      CDSCadTanques.Edit;
      CDSCadTanquesFLAG.AsInteger := 1;    
      CDSCadTanques.Post;
      CDSCadTanques.Delete;
    end;
  end;
end;

procedure TfrmCadLotes.btnExcluirClick(Sender: TObject);
begin
  inherited;
  if Confirma then
  begin       
    Confirma := False;
    CDSCadTanques.Close;
    dbgrid1.Repaint;
    edtCodLote.Clear;
    btnNovo.SetFocus;
  end;
end;

procedure TfrmCadLotes.btnIncluirClick(Sender: TObject);
begin
  inherited;

  if not DSCadTanques.DataSet.Active then
    DSCadTanques.DataSet.Open;

  DSCadTanques.DataSet.Append;

  CDSCadTanquesID_TANQUE.AsInteger := StrToInt(edtCodTanque.Text);
  CDSCadTanquesQTDE_ESTOCADA.AsInteger := StrToInt(edtQuantEstocada.Text);
  CDSCadTanquesVOLUME_TANQUE.AsCurrency := StrToCurr(edtVolume.Text);
  CDSCadTanquesID_LOTE.AsInteger := StrToInt(edtCodLote.Text);


  DSCadTanques.DataSet.Post;

  edtCodTanque.Clear;
  edtQuantEstocada.Clear;
  edtVolume.Clear;

  edtCodTanque.SetFocus;


end;

procedure TfrmCadLotes.btnNovoClick(Sender: TObject);
begin
  inherited;
  SDSCadTanques.Close;
  CDSCadTanques.Close;
  qryLotes.Close;
  qryLotes.Open;
  edtCodLote.Text := qryLotes.FieldByName('id_lote').AsString;
  edtDescricao.SetFocus;
end;

procedure TfrmCadLotes.btnPesquisarClick(Sender: TObject);
begin
  inherited;
  if btnPesquisar.Enabled = True then
    begin
      Application.CreateForm(TfrmPesqLotes,frmPesqLotes);
      if frmPesqLotes.ShowModal = mrOk then
      begin
        SDSCadLotes.Close;
        CDSCadLotes.Close;
        SDSCadLotes.ParamByName('id_lote').AsInteger := frmPesqLotes.CDSPesqLotesID_LOTE.AsInteger;
        CDSCadLotes.Open;

        SDSCadTanques.Close;
        CDSCadTanques.Close;
        SDSCadTanques.ParamByName('id_lote').AsInteger := frmPesqLotes.CDSPesqLotesID_LOTE.AsInteger;
        CDSCadTanques.Open;

        edtCodLote.Text := frmPesqLotes.CDSPesqLotesID_LOTE.AsString;

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

        edtCodLote.Enabled := False;
        frmPesqLotes.CDSPesqLotes.Close;
        PanelCenter.Enabled := False;
        btnAlterar.SetFocus;

      end;
      FreeAndNil(frmPesqLotes);
    end;
end;

procedure TfrmCadLotes.btnSalvarClick(Sender: TObject);
begin
  inherited;
  TRY
    dstGenerico.DataSet.Post;       
    AtivarBotoes(False);
    CaixadeMensagemAviso('Registro Gravado com Sucesso!');
    LimpaFormulario;
  Except
    ON EDataBaseError DO
      BEGIN
        CaixadeMensagemAviso('Deve conter [ TANQUES ] Preenchido!');
      END;
  END;
end;

procedure TfrmCadLotes.LimpaFormulario;
begin
  CDSCadLotes.Close;
  CDSCadTanques.Close;
  DBGrid1.Repaint;
  edtCodLote.Clear;
  PanelCenter.Enabled := False;
  btnNovo.SetFocus;
end;

procedure TfrmCadLotes.CDSCadLotesAfterPost(DataSet: TDataSet);
begin
  inherited;
  CDSCadLotes.ApplyUpdates(-1);
 // if not CDSCadTanques.IsEmpty then
    CDSCadTanques.ApplyUpdates(-1);
end;

procedure TfrmCadLotes.CDSCadLotesBeforePost(DataSet: TDataSet);
begin
  inherited;
  CDSCadLotesID.AsInteger := StrToInt(edtCodLote.Text);
  CDSCadLotesID_LOTE.AsInteger := StrToInt(edtCodLote.Text);    
end;

procedure TfrmCadLotes.CDSCadTanquesBeforeDelete(DataSet: TDataSet);
begin
  inherited;
  CDSCadTanques.ApplyUpdates(-1);
end;

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

end.
