unit fmConfiguracion;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, DB, FIBDataSet, pFIBDataSet, FIBDatabase,
  pFIBDatabase, FIBQuery, pFIBQuery, pFIBStoredProc, wwdblook;

type
  TfmConfig = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Button1: TButton;
    Button2: TButton;
    trTrans: TpFIBTransaction;
    IBQuery1: TpFIBDataSet;
    spCatId: TpFIBStoredProc;
    GroupBox2: TGroupBox;
    dsListasPrecio: TpFIBDataSet;
    wwDBLookupCombo1: TwwDBLookupCombo;
    dsListasPrecioPRECIO_EMPRESA_ID: TFIBIntegerField;
    dsListasPrecioNOMBRE: TFIBStringField;
    CheckBox4: TCheckBox;
    GroupBox3: TGroupBox;
    wwDBLookupCombo2: TwwDBLookupCombo;
    dsListasPrecioVM: TpFIBDataSet;
    FIBStringField1: TFIBStringField;
    FIBIntegerField1: TFIBIntegerField;
    GroupBox4: TGroupBox;
    wwDBLookupCombo3: TwwDBLookupCombo;
    dsClientes: TpFIBDataSet;
    dsClientesNOMBRE: TFIBStringField;
    dsClientesCLIENTE_ID: TFIBIntegerField;
    Panel2: TPanel;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FVersion:Integer;
  end;

var
  fmConfig: TfmConfig;

implementation

{$R *.dfm}
uses Unit1,Unit101;

procedure TfmConfig.Button1Click(Sender: TObject);
var lPzsGratis, lMontoDscto, lPorcentajeDscto, lPrecio:String;
lElementoId:Integer;
begin
 try
  lElementoId:=0;
  if CheckBox1.Checked then lPzsGratis:='S'
  else lPzsGratis:='N';

  IBQuery1.Close;
  IBQuery1.SelectSQL.Clear;
  IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''PZSGRATIS'' ');
  IBQuery1.Open;
  lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
  if lElementoId=0  then
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''PZSGRATIS'', :VALOR);');
    spCatId.ExecProc;
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
    IBQuery1.ParamByName('VALOR').AsString:=lPzsGratis;
    IBQuery1.Open;
  end
  else
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
    IBQuery1.ParamByName('VALOR').AsString:=lPzsGratis;
    IBQuery1.Open;
  end;

  lElementoId:=0;
  if CheckBox2.Checked then lMontoDscto:='S'
  else lMontoDscto:='N';

  IBQuery1.Close;
  IBQuery1.SelectSQL.Clear;
  IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''MONTODSCTO'' ');
  IBQuery1.Open;
  lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
  if lElementoId=0  then
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''MONTODSCTO'', :VALOR);');
    spCatId.ExecProc;
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
    IBQuery1.ParamByName('VALOR').AsString:=lMontoDscto;
    IBQuery1.Open;
  end
  else
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
    IBQuery1.ParamByName('VALOR').AsString:=lMontoDscto;
    IBQuery1.Open;
  end;

  lElementoId:=0;
  if CheckBox3.Checked then lPorcentajeDscto:='S'
  else lPorcentajeDscto:='N';

  IBQuery1.Close;
  IBQuery1.SelectSQL.Clear;
  IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''PORCENTAJEDSCTO'' ');
  IBQuery1.Open;
  lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
  if lElementoId=0  then
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''PORCENTAJEDSCTO'', :VALOR);');
    spCatId.ExecProc;
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
    IBQuery1.ParamByName('VALOR').AsString:=lPorcentajeDscto;
    IBQuery1.Open;
  end
  else
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
    IBQuery1.ParamByName('VALOR').AsString:=lPorcentajeDscto;
    IBQuery1.Open;
  end;

  lElementoId:=0;
  if CheckBox4.Checked then lPrecio:='S'
  else lPrecio:='N';

  IBQuery1.Close;
  IBQuery1.SelectSQL.Clear;
  IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''PRECIO'' ');
  IBQuery1.Open;
  lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
  if lElementoId=0  then
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''PRECIO'', :VALOR);');
    spCatId.ExecProc;
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
    IBQuery1.ParamByName('VALOR').AsString:=lPrecio;
    IBQuery1.Open;
  end
  else
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
    IBQuery1.ParamByName('VALOR').AsString:=lPrecio;
    IBQuery1.Open;
  end;

  lElementoId:=0;
  IBQuery1.Close;
  IBQuery1.SelectSQL.Clear;
  IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''LISTAPMP'' ');//precio máximo al público
  IBQuery1.Open;
  lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
  if lElementoId=0  then
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''LISTAPMP'', :VALOR);');
    spCatId.ExecProc;
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
    IBQuery1.ParamByName('VALOR').AsString:=dsListasPrecio.FieldByName('PRECIO_EMPRESA_ID').AsString;
    IBQuery1.Open;
  end
  else
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
    IBQuery1.ParamByName('VALOR').AsString:=dsListasPrecio.FieldByName('PRECIO_EMPRESA_ID').AsString;
    IBQuery1.Open;
  end;

  lElementoId:=0;
  IBQuery1.Close;
  IBQuery1.SelectSQL.Clear;
  IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''LISTAPVM'' ');//precio venta mostrador
  IBQuery1.Open;
  lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
  if lElementoId=0  then
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''LISTAPVM'', :VALOR);');
    spCatId.ExecProc;
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
    IBQuery1.ParamByName('VALOR').AsString:=dsListasPrecioVM.FieldByName('PRECIO_EMPRESA_ID').AsString;
    IBQuery1.Open;
  end
  else
  begin
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
    IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
    IBQuery1.ParamByName('VALOR').AsString:=dsListasPrecioVM.FieldByName('PRECIO_EMPRESA_ID').AsString;
    IBQuery1.Open;
  end;  

  if FVersion=1 then
  begin
    lElementoId:=0;
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('SELECT A.ELEMENTO_ID FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''CLIENTE_ID'' ');
    IBQuery1.Open;
    lElementoId:=IBQuery1.FieldByName('ELEMENTO_ID').AsInteger;
    if lElementoId=0  then
    begin
      IBQuery1.Close;
      IBQuery1.SelectSQL.Clear;
      IBQuery1.SelectSQL.Add ('INSERT INTO MGNADRO_CONFIG (ELEMENTO_ID, NOMBRE, VALOR) VALUES (:ELEMENTO_ID, ''CLIENTE_ID'', :VALOR);');
      spCatId.ExecProc;
      IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=spCatId.FieldByName('CATALOGO_ID').AsInteger;
      IBQuery1.ParamByName('VALOR').AsString:=dsClientes.FieldByName('CLIENTE_ID').AsString;
      IBQuery1.Open;
    end
    else
    begin
      IBQuery1.Close;
      IBQuery1.SelectSQL.Clear;
      IBQuery1.SelectSQL.Add ('UPDATE MGNADRO_CONFIG SET VALOR = :VALOR WHERE (ELEMENTO_ID = :ELEMENTO_ID);');
      IBQuery1.ParamByName('ELEMENTO_ID').AsInteger:=lElementoId;
      IBQuery1.ParamByName('VALOR').AsString:=dsClientes.FieldByName('CLIENTE_ID').AsString;
      IBQuery1.Open;
    end;
  end;
  
  if trTrans.InTransaction  then trTrans.Commit;
  Close();

except on e:exception do
begin
   MessageDlg('Error general.'+e.Message, mtInformation,  [mbOk], 0);
   Exit;
end;
end;
end;

procedure TfmConfig.Button2Click(Sender: TObject);
begin
  close();
end;

procedure TfmConfig.FormShow(Sender: TObject);
var lCajeroId:Integer;
begin
  if trTrans.InTransaction then
        trTrans.Rollback;

   trTrans.StartTransaction;


   IBQuery1.Close;
   IBQuery1.SelectSQL.Clear;
   IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''PZSGRATIS'' ');
   IBQuery1.Open;
   if IBQuery1.FieldByName('VALOR').AsString <> '' then
   begin
     if IBQuery1.FieldByName('VALOR').AsString='S' then CheckBox1.Checked:=True;
   end;

   IBQuery1.Close;
   IBQuery1.SelectSQL.Clear;
   IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''MONTODSCTO'' ');
   IBQuery1.Open;
   if IBQuery1.FieldByName('VALOR').AsString <> '' then
   begin
     if IBQuery1.FieldByName('VALOR').AsString='S' then CheckBox2.Checked:=True;
   end;

   IBQuery1.Close;
   IBQuery1.SelectSQL.Clear;
   IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''PORCENTAJEDSCTO'' ');
   IBQuery1.Open;
   if IBQuery1.FieldByName('VALOR').AsString <> '' then
   begin
     if IBQuery1.FieldByName('VALOR').AsString='S' then CheckBox3.Checked:=True;
   end;

   IBQuery1.Close;
   IBQuery1.SelectSQL.Clear;
   IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''PRECIO'' ');
   IBQuery1.Open;
   if IBQuery1.FieldByName('VALOR').AsString <> '' then
   begin
     if IBQuery1.FieldByName('VALOR').AsString='S' then CheckBox4.Checked:=True;
   end;

   dsListasPrecio.CloseOpen(False);
   IBQuery1.Close;
   IBQuery1.SelectSQL.Clear;
   IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''LISTAPMP'' ');
   IBQuery1.Open;
   if IBQuery1.FieldByName('VALOR').AsString <> '' then
   begin
     dsListasPrecio.Locate('PRECIO_EMPRESA_ID',IBQuery1.FieldByName('VALOR').AsString,[]);
     wwDBLookupCombo1.Text:=dsListasPrecio.FieldByName('NOMBRE').AsString;
   end;

   dsListasPrecioVM.CloseOpen(False);
   IBQuery1.Close;
   IBQuery1.SelectSQL.Clear;
   IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''LISTAPVM'' ');
   IBQuery1.Open;
   if IBQuery1.FieldByName('VALOR').AsString <> '' then
   begin
     dsListasPrecioVM.Locate('PRECIO_EMPRESA_ID',IBQuery1.FieldByName('VALOR').AsString,[]);
     wwDBLookupCombo2.Text:=dsListasPrecioVM.FieldByName('NOMBRE').AsString;
   end;

  if FVersion=1 then
  begin
    GroupBox4.Visible:=True;
    dsClientes.CloseOpen(False);
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add ('SELECT A.VALOR FROM MGNADRO_CONFIG A WHERE A.NOMBRE = ''CLIENTE_ID'' ');
    IBQuery1.Open;
    if IBQuery1.FieldByName('VALOR').AsString <> '' then
    begin
      dsClientes.Locate('CLIENTE_ID',IBQuery1.FieldByName('VALOR').AsString,[]);
      wwDBLookupCombo3.Text:=dsClientes.FieldByName('NOMBRE').AsString;
    end;
  end
  else
  begin
    GroupBox4.Visible:=False;
  end;
end;

end.
