unit fconfiguracionalmacen;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.DBCtrls, Vcl.Grids, Vcl.DBGrids,
  Vcl.StdCtrls, Vcl.ExtCtrls, Data.DB, ZAbstractDataset, ZDataset,
  ZAbstractRODataset;

type
  Tconfiguracionalmacen = class(TForm)
    Panel1: TPanel;
    Panel9: TPanel;
    btscancelar: TButton;
    btsmodificar: TButton;
    btsborrar: TButton;
    btsnuevo: TButton;
    btsguardar: TButton;
    listaimpresion: TComboBox;
    Panel2: TPanel;
    DBGrid1: TDBGrid;
    qalmacenes: TZReadOnlyQuery;
    qsectores: TZQuery;
    qalmacenesidempresa: TIntegerField;
    qalmacenesid: TIntegerField;
    qalmacenesnombre: TWideStringField;
    dsqalmacenes: TDataSource;
    qsectoreside: TIntegerField;
    qsectoresid: TIntegerField;
    qsectoresdescripcion: TWideStringField;
    qsectoresidalmacen: TIntegerField;
    dsqsectores: TDataSource;
    DBLookupComboBox1: TDBLookupComboBox;
    Panel3: TPanel;
    Panel4: TPanel;
    btpcancelar: TButton;
    btpmodificar: TButton;
    btpborrar: TButton;
    btpnuevo: TButton;
    btpguardar: TButton;
    ComboBox1: TComboBox;
    Panel5: TPanel;
    DBGrid2: TDBGrid;
    qpuestos: TZQuery;
    dsqpuestos: TDataSource;
    qaux: TZQuery;
    qsectoresdescalmacen: TWideStringField;
    DBLookupComboBox2: TDBLookupComboBox;
    qpuestoside: TIntegerField;
    qpuestosidsector: TIntegerField;
    qpuestosid: TIntegerField;
    qpuestosdescripcion: TWideStringField;
    qpuestosdescsector: TWideStringField;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btsnuevoClick(Sender: TObject);
    procedure dsqsectoresStateChange(Sender: TObject);
    procedure btsmodificarClick(Sender: TObject);
    procedure btsguardarClick(Sender: TObject);
    procedure btscancelarClick(Sender: TObject);
    procedure btsborrarClick(Sender: TObject);
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
    procedure DBGrid1ColExit(Sender: TObject);
    procedure DBGrid2ColExit(Sender: TObject);
    procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid2KeyPress(Sender: TObject; var Key: Char);
    procedure dsqpuestosStateChange(Sender: TObject);
    procedure btpnuevoClick(Sender: TObject);
    procedure btpborrarClick(Sender: TObject);
    procedure btpmodificarClick(Sender: TObject);
    procedure btpguardarClick(Sender: TObject);
    procedure btpcancelarClick(Sender: TObject);
    procedure qpuestosAfterPost(DataSet: TDataSet);
    procedure qsectoresAfterPost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AppMessage(var Msg: Tmsg; var Handled:Boolean);
  end;

var
  configuracionalmacen: Tconfiguracionalmacen;

implementation

{$R *.dfm}

uses datos;
procedure Tconfiguracionalmacen.AppMessage(var Msg: Tmsg; var Handled: Boolean);
 var
  actual : TWincontrol;
 begin
 if Msg.message = WM_KEYDOWN then
    begin

    // esto es para controlar que con la flecha abajo se desplegen las
    // listas

    if Msg.wParam = VK_DOWN then
      begin
      Actual := Screen.ActiveControl;
      if Actual is TDBLookupComboBox then
            if not TDBLookupComboBox(Screen.ActiveForm.ActiveControl).listVisible then
               TDBLookupComboBox(Screen.ActiveForm.ActiveControl).DropDown;

      // si utilizas las RX
   {   if Actual is TRxDBLookupCombo then
            if not TRxDBLookupCombo(Screen.ActiveForm.ActiveControl).listVisible then
               TRxDBLookupCombo(Screen.ActiveForm.ActiveControl).DropDown;

    }
      if Actual is TComboBox then
         SendMessage(TComboBox(Screen.ActiveForm.ActiveControl).Handle,CB_SHOWDROPDOWN,-1,0);

      if (Actual is TCustomEdit) then
          Msg.wParam := VK_TAB;
      // se pueden añadir mas controles....

      end;

    // y aqui es el control del intro.
    if Msg.wParam = VK_RETURN THEN
       begin
       Actual := Screen.ActiveControl;
       // para los edit.
       if (Actual is TCustomEdit) and
          not (Actual is TCustomMemo) then
          Msg.wParam := VK_TAB;

       // para los memo. Con ctrl + enter se salta de linea dentro del
       // memo, si solo es intro salta al siguiente control
       if (Actual is TCustomMemo) and
          (HiWord(GetKeyState(VK_CONTROL)) = 0)then
          Msg.wParam := VK_TAB;

       // para los lookup
       if Actual is TDBLookupComboBox then
          if not TDBLookupComboBox(Actual).listVisible then
             Msg.wParam := VK_TAB;

       // Si utilizas las RX
{       if Actual is TRxDBLookupCombo then
          if not TRxDBLookupCombo(Actual).listVisible then
             Msg.wParam := VK_TAB;
 }
       // Los combobox
       if Actual is TCustomComboBox then
          if not TCustomComboBox(Actual).DroppedDown then
             Msg.wParam := VK_TAB;

       // el radiobutton
       if Actual is TRadioButton then
          Msg.wParam := VK_TAB;

       // en el grid
       if Actual is TDBGrid then
          if (TDBGrid(Actual).ReadOnly) or
             (HiWord(GetKeyState(VK_CONTROL)) = 1)then
             Msg.wParam := VK_TAB;

       // o esto otro Con Ctrl + intro siguiente celda con
       // ctrl + shift + intro celda anterior
       // es complicado pero es por no liar con el intro solo
       // que siempre pasa de un control a otro.
       if Actual is TDBGrid then
          begin
          if (TDBGrid(Actual).ReadOnly) or
             (HiWord(GetKeyState(VK_CONTROL)) = 0) then
             Msg.wParam := VK_TAB
          else
             if not(HiWord(GetKeyState(VK_SHIFT)) = 0) then
                begin
                if TDBGrid(Actual).selectedindex > 0 then             { increment the field }
                   TDBGrid(Actual).selectedindex := TDBGrid(Actual).selectedindex -1
                else
                   TDBGrid(Actual).selectedindex := TDBGrid(Actual).fieldcount -1;
                end
             else
                begin
                if TDBGrid(Actual).selectedindex < (TDBGrid(Actual).fieldcount -1) then             { increment the field }
                   TDBGrid(Actual).selectedindex := TDBGrid(Actual).selectedindex +1
                else
                   TDBGrid(Actual).selectedindex := 0;
                end;
          end;


       // ListBox
//       if Actual is TCustomListBox then
//          Msg.wParam := VK_TAB;

       // TabControl
//       if Actual is TCustomTabControl then
//          Msg.wParam := VK_TAB;

       // Aqui se pondrían todos los controles que usamos en la
       // aplicación y necesitamos cambiar el tab por intro.
       // de esta forma te despreocupas de cuantos añades y es más
       // limpio el código

       end;
    end;
 end;


procedure Tconfiguracionalmacen.btpborrarClick(Sender: TObject);
begin
   qpuestos.Delete;
end;

procedure Tconfiguracionalmacen.btpcancelarClick(Sender: TObject);
begin
   qpuestos.cancel;
end;

procedure Tconfiguracionalmacen.btpguardarClick(Sender: TObject);
begin
   qpuestos.post;
end;

procedure Tconfiguracionalmacen.btpmodificarClick(Sender: TObject);
begin
   qpuestos.edit;
end;

procedure Tconfiguracionalmacen.btpnuevoClick(Sender: TObject);
begin
    qpuestos.Append;
    qpuestoside.Value:=dm.tempresaid.AsInteger;
    dbgrid2.ReadOnly:=false;
    dbgrid2.SetFocus;
end;

procedure Tconfiguracionalmacen.btsborrarClick(Sender: TObject);
begin
    qsectores.Delete;
end;

procedure Tconfiguracionalmacen.btscancelarClick(Sender: TObject);
begin
    qsectores.Cancel;
end;

procedure Tconfiguracionalmacen.btsguardarClick(Sender: TObject);
begin
    qsectores.Post;
end;

procedure Tconfiguracionalmacen.btsmodificarClick(Sender: TObject);
begin
    qsectores.Edit;
end;

procedure Tconfiguracionalmacen.btsnuevoClick(Sender: TObject);
begin
    qsectores.Append;
    qsectoreside.Value:=dm.tempresaid.AsInteger;
    dbgrid1.ReadOnly:=false;
    dbgrid1.SetFocus;
end;

procedure Tconfiguracionalmacen.DBGrid1ColExit(Sender: TObject);
begin
  if (DBGrid1.SelectedField.FieldName = DBLookupComboBox2.DataField) and (qsectores.State in [dsedit,dsinsert]) then
  begin
    DBLookupComboBox2.Visible := false;
    qsectoresdescalmacen.Value:=qalmacenesnombre.AsString;
  end;
end;

procedure Tconfiguracionalmacen.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (gdFocused in State) then
  begin
    if (Column.Field.FieldName = DBLookupComboBox2.DataField) and (qsectores.State in [dsedit,dsinsert]) then
    begin
    with DBLookupComboBox2 do
    begin
      visible:= true;
      Left := Rect.Left + DBGrid1.Left + 2;
      Top := Rect.Top + DBGrid1.Top + 2;
      Width := Rect.Right - Rect.Left;
      Width := Rect.Right - Rect.Left;
      Height := Rect.Bottom - Rect.Top;
    end;
    dblookupcombobox2.SetFocus;
    dblookupcombobox2.DropDown;
    end else dblookupcombobox2.Visible:=false;
  end

end;

procedure Tconfiguracionalmacen.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin

  if (DBGrid1.SelectedField.FieldName = DBLookupComboBox2.DataField) then
  begin
    SendMessage(DBLookupComboBox2.Handle, WM_Char, word(Key), 0);
 //   DBLookupComboBox2.SetFocus;

  end
end;

procedure Tconfiguracionalmacen.DBGrid2ColExit(Sender: TObject);
begin
  if (DBGrid2.SelectedField.FieldName = DBLookupComboBox1.DataField) and (qpuestos.State in [dsedit,dsinsert]) then
  begin
    DBLookupComboBox1.Visible := false;
    qpuestosdescsector.Value:=qsectoresdescripcion.AsString;
  end;
end;

procedure Tconfiguracionalmacen.DBGrid2DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (gdFocused in State) then
  begin
    if (Column.Field.FieldName = DBLookupComboBox1.DataField) and (qpuestos.State in [dsedit,dsinsert]) then
    begin
    with DBLookupComboBox1 do
    begin
      visible:= true;
      Left := Rect.Left + DBGrid1.Left + 2;
      Top := Rect.Top + DBGrid1.Top + 2;
      Width := Rect.Right - Rect.Left;
      Width := Rect.Right - Rect.Left;
      Height := Rect.Bottom - Rect.Top;
    end;
    dblookupcombobox1.SetFocus;
    dblookupcombobox1.DropDown;
    end else dblookupcombobox1.Visible:=false;
  end
end;

procedure Tconfiguracionalmacen.DBGrid2KeyPress(Sender: TObject; var Key: Char);
begin
  if (DBGrid2.SelectedField.FieldName = DBLookupComboBox1.DataField) then
  begin
    SendMessage(DBLookupComboBox1.Handle, WM_Char, word(Key), 0);
  end;
end;

procedure Tconfiguracionalmacen.dsqpuestosStateChange(Sender: TObject);
begin
    if qpuestos.State in [dsedit,dsinsert] then
    begin
       btpnuevo.enabled:=false;
       btpborrar.enabled:=false;
       btpmodificar.enabled:=false;
       btpguardar.enabled:=true;
       btpcancelar.enabled:=true;
       dbgrid2.readonly:=false;
    end;
    if qpuestos.State in [dsbrowse] then
    begin
       btpnuevo.enabled:=true;
       btpborrar.enabled:=true;
       btpmodificar.enabled:=true;
       btpguardar.enabled:=false;
       btpcancelar.enabled:=false;
       dbgrid2.readonly:=true;
    end;
end;

procedure Tconfiguracionalmacen.dsqsectoresStateChange(Sender: TObject);
begin
    if qsectores.State in [dsedit,dsinsert] then
    begin
       btsnuevo.enabled:=false;
       btsborrar.enabled:=false;
       btsmodificar.enabled:=false;
       btsguardar.enabled:=true;
       btscancelar.enabled:=true;
       dbgrid1.readonly:=false;
    end;
    if qsectores.State in [dsbrowse] then
    begin
       btsnuevo.enabled:=true;
       btsborrar.enabled:=true;
       btsmodificar.enabled:=true;
       btsguardar.enabled:=false;
       btscancelar.enabled:=false;
       dbgrid1.readonly:=true;


    end;

end;

procedure Tconfiguracionalmacen.FormCreate(Sender: TObject);
begin
 with DBLookupComboBox2 do
 begin
   DataSource := dsqsectores; // -> AdoTable1 -> DBGrid1
   ListSource := dsqalmacenes;
   DataField   := 'idalmacen'; // from AdoTable1 - displayed in the DBGrid
   KeyField  := 'id';
   ListField := 'id;nombre';
   dropdownwidth := 150;

   Visible    := False;
 end;
 with DBLookupComboBox1 do
 begin
   DataSource := dsqpuestos; // -> AdoTable1 -> DBGrid1
   ListSource := dsqsectores;
   DataField   := 'idsector'; // from AdoTable1 - displayed in the DBGrid
   KeyField  := 'id';
   ListField := 'id;descripcion';
   dropdownwidth := 150;
   Visible    := False;
 end;
end;

procedure Tconfiguracionalmacen.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

    if (activecontrol is tdbgrid) then
    begin
        if Key = 13 then Key :=9;
    end;
end;

procedure Tconfiguracionalmacen.FormKeyPress(Sender: TObject; var Key: Char);
var formatos:tformatsettings;
punto:string;
begin
    GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatos);
    punto:=formatos.DecimalSeparator;
    if punto=',' then
    begin
        key:=upcase(key);
        if (key='.') and (activecontrol is tdbedit) then
        begin
           if  (activecontrol as tdbedit).Field.DataType in [ftfloat,ftCurrency,ftbcd,ftfmtbcd] then key:=','
         end;
        if (key='.') and (activecontrol is tdbgrid) then
        begin
            key:=',';
        end;
    end;
    if (activecontrol is tdbgrid) then
    begin
        if Key = #13 then Key :=#9;
    end;

    if (key=#13) then
    begin
        Perform(WM_NextDLGCtl,0,0);
        key:=#0;
    end;


end;

procedure Tconfiguracionalmacen.FormShow(Sender: TObject);
begin
    dm.tempresa.open;
    qalmacenes.Open;
    qsectores.Open;
    qpuestos.Open;

end;

procedure Tconfiguracionalmacen.qpuestosAfterPost(DataSet: TDataSet);
begin
    btpnuevo.SetFocus;
end;

procedure Tconfiguracionalmacen.qsectoresAfterPost(DataSet: TDataSet);
begin
    btsnuevo.SetFocus;
end;

end.
