unit frm_FormasPagoEditor;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons,
  Data.DB, FireDAC.Comp.Client, FireDAC.Stan.Param, uAppTheme, uBaseForm,
  Vcl.ComCtrls, Vcl.Grids, Vcl.DBGrids;

type
  TfrmFormasPagoEditor = class(TfrmBase)
    lblId: TLabel;
    edtId: TEdit;
    lblDescripcion: TLabel;
    edtDescripcion: TEdit;
    lblDiasPago: TLabel;
    edtDiasPago: TEdit;
    lblEdiCodigo: TLabel;
    edtEdiCodigo: TEdit;
    chkActivo: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnSalirClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  protected
    procedure DoShow; override;
    procedure DoAnadir; override;
    procedure DoModificar; override;
    procedure DoPrimero; override;
    procedure DoAnterior; override;
    procedure DoSiguiente; override;
    procedure DoUltimo; override;
  private
    FFormaPagoId : string;
    FParentForm  : TForm;
    procedure LoadData;
    procedure ClearFields;
  public
    property FormaPagoId: string read FFormaPagoId write FFormaPagoId;
    property ParentForm: TForm read FParentForm write FParentForm;
  end;

var
  frmFormasPagoEditor: TfrmFormasPagoEditor;

implementation

uses
  dmg_Main, uDbErrorHandler, frm_FormasPago;

{$R *.dfm}

procedure TfrmFormasPagoEditor.FormShow(Sender: TObject);
begin
  inherited;
  frmFormasPagoEditor := Self;
  TAppTheme.ApplyToForm(Self);

  btnGuardar.OnClick := btnSaveClick;
  btnSalir.OnClick   := btnSalirClick;

  btnCancelar.Visible := False;
  btnAnadir.Visible   := True;
  btnEliminar.Visible := False;
  btnPrimero.Visible  := True;
  btnAnterior.Visible := True;
  btnSiguiente.Visible:= True;
  btnUltimo.Visible   := True;
  btnModificar.Visible:= True;
  btnBuscar.Visible   := False;
  btnImprimir.Visible := False;
  btnIA.Visible       := False;
  btnDuplicar.Visible := False;
  btnRect.Visible     := False;
  btnAyuda.Visible    := False;
  btnBuscarArticulo.Visible := False;
  btnExcel.Visible    := False;

  TsListado.TabVisible := False;
  dbgItems.Visible     := False;
  pnlSideToolbar.Visible := False;

  LoadData;
end;

procedure TfrmFormasPagoEditor.DoShow;
begin
  inherited;
  if pgcDetails.PageCount > 0 then
    pgcDetails.ActivePageIndex := 0;
end;

procedure TfrmFormasPagoEditor.FormDestroy(Sender: TObject);
begin
  if frmFormasPagoEditor = Self then
    frmFormasPagoEditor := nil;
end;

procedure TfrmFormasPagoEditor.ClearFields;
begin
  edtId.Text := '';
  edtDescripcion.Text := '';
  edtDiasPago.Text := '1';
  edtEdiCodigo.Text := '';
  chkActivo.Checked := True;
  if edtDescripcion.CanFocus then
    edtDescripcion.SetFocus;
end;

procedure TfrmFormasPagoEditor.LoadData;
begin
  if Trim(FFormaPagoId) = '' then
  begin
    ClearFields;
    Exit;
  end;

  try
    dmgMain.qryExec.Close;
    dmgMain.qryExec.SQL.Text :=
      'SELECT id, descripcion, dias_pago, edi_codigo, activo ' +
      'FROM ge_formas_pago WHERE id = :id';
    dmgMain.qryExec.ParamByName('id').AsString := FFormaPagoId;
    dmgMain.qryExec.Open;

    if not dmgMain.qryExec.IsEmpty then
    begin
      edtId.Text          := dmgMain.qryExec.FieldByName('id').AsString;
      edtDescripcion.Text := dmgMain.qryExec.FieldByName('descripcion').AsString;
      edtDiasPago.Text    := dmgMain.qryExec.FieldByName('dias_pago').AsString;
      edtEdiCodigo.Text   := dmgMain.qryExec.FieldByName('edi_codigo').AsString;
      chkActivo.Checked   := dmgMain.qryExec.FieldByName('activo').AsInteger = 1;
    end
    else
      ClearFields;
      
    dmgMain.qryExec.Close;
  except
    on E: Exception do
      TDbErrorHandler.HandleException(E, dmgMain.qryExec.SQL.Text);
  end;
end;

procedure TfrmFormasPagoEditor.btnSaveClick(Sender: TObject);
var
  LIsNew: Boolean;
  LUserId: Integer;
begin
  if Trim(edtDescripcion.Text) = '' then
  begin
    ShowMessage('La descripción de la forma de pago es obligatoria.');
    edtDescripcion.SetFocus;
    Exit;
  end;

  LIsNew := Trim(FFormaPagoId) = '';
  LUserId := StrToIntDef(dmgMain.CurrentUserId, 1);

  try
    dmgMain.qryExec.Close;
    if LIsNew then
    begin
      dmgMain.qryExec.SQL.Text :=
        'INSERT INTO ge_formas_pago (descripcion, dias_pago, edi_codigo, activo, id_user_creator, id_user_update) ' +
        'VALUES (:descripcion, :dias_pago, :edi_codigo, :activo, :user_creator, :user_update)';
      dmgMain.qryExec.ParamByName('user_creator').AsInteger := LUserId;
      dmgMain.qryExec.ParamByName('user_update').AsInteger  := LUserId;
    end
    else
    begin
      dmgMain.qryExec.SQL.Text :=
        'UPDATE ge_formas_pago SET ' +
        '  descripcion = :descripcion, ' +
        '  dias_pago = :dias_pago, ' +
        '  edi_codigo = :edi_codigo, ' +
        '  activo = :activo, ' +
        '  id_user_update = :user_update, ' +
        '  updated_at = CURRENT_TIMESTAMP ' +
        'WHERE id = :id';
      dmgMain.qryExec.ParamByName('id').AsString := FFormaPagoId;
      dmgMain.qryExec.ParamByName('user_update').AsInteger := LUserId;
    end;

    dmgMain.qryExec.ParamByName('descripcion').AsString := Trim(edtDescripcion.Text);
    dmgMain.qryExec.ParamByName('dias_pago').AsInteger  := StrToIntDef(Trim(edtDiasPago.Text), 1);
    if Trim(edtEdiCodigo.Text) <> '' then
      dmgMain.qryExec.ParamByName('edi_codigo').AsString := Trim(edtEdiCodigo.Text)
    else
      dmgMain.qryExec.ParamByName('edi_codigo').Clear;

    if chkActivo.Checked then
      dmgMain.qryExec.ParamByName('activo').AsInteger := 1
    else
      dmgMain.qryExec.ParamByName('activo').AsInteger := 0;

    dmgMain.qryExec.ExecSQL;
    ResetChangeTracking;
    if Assigned(FParentForm) and (FParentForm is TfrmFormasPago) then
      TfrmFormasPago(FParentForm).btnRefreshClick(nil);
    ShowMessage('Forma de pago guardada correctamente.');
  except
    on E: Exception do
      TDbErrorHandler.HandleException(E, dmgMain.qryExec.SQL.Text);
  end;
end;

procedure TfrmFormasPagoEditor.btnSalirClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmFormasPagoEditor.DoAnadir;
begin
  FFormaPagoId := '';
  ClearFields;
end;

procedure TfrmFormasPagoEditor.DoModificar;
begin
  if edtDescripcion.CanFocus then
    edtDescripcion.SetFocus;
end;

procedure TfrmFormasPagoEditor.DoPrimero;
begin
  dmgMain.qryExec.Close;
  dmgMain.qryExec.SQL.Text := 'SELECT id FROM ge_formas_pago ORDER BY descripcion ASC LIMIT 1';
  dmgMain.qryExec.Open;
  if not dmgMain.qryExec.IsEmpty then
  begin
    FFormaPagoId := dmgMain.qryExec.FieldByName('id').AsString;
    LoadData;
  end;
  dmgMain.qryExec.Close;
end;

procedure TfrmFormasPagoEditor.DoAnterior;
begin
  if Trim(FFormaPagoId) = '' then Exit;
  dmgMain.qryExec.Close;
  dmgMain.qryExec.SQL.Text :=
    'SELECT id FROM ge_formas_pago WHERE descripcion < :desc ORDER BY descripcion DESC LIMIT 1';
  dmgMain.qryExec.ParamByName('desc').AsString := edtDescripcion.Text;
  dmgMain.qryExec.Open;
  if not dmgMain.qryExec.IsEmpty then
  begin
    FFormaPagoId := dmgMain.qryExec.FieldByName('id').AsString;
    LoadData;
  end;
  dmgMain.qryExec.Close;
end;

procedure TfrmFormasPagoEditor.DoSiguiente;
begin
  if Trim(FFormaPagoId) = '' then Exit;
  dmgMain.qryExec.Close;
  dmgMain.qryExec.SQL.Text :=
    'SELECT id FROM ge_formas_pago WHERE descripcion > :desc ORDER BY descripcion ASC LIMIT 1';
  dmgMain.qryExec.ParamByName('desc').AsString := edtDescripcion.Text;
  dmgMain.qryExec.Open;
  if not dmgMain.qryExec.IsEmpty then
  begin
    FFormaPagoId := dmgMain.qryExec.FieldByName('id').AsString;
    LoadData;
  end;
  dmgMain.qryExec.Close;
end;

procedure TfrmFormasPagoEditor.DoUltimo;
begin
  dmgMain.qryExec.Close;
  dmgMain.qryExec.SQL.Text := 'SELECT id FROM ge_formas_pago ORDER BY descripcion DESC LIMIT 1';
  dmgMain.qryExec.Open;
  if not dmgMain.qryExec.IsEmpty then
  begin
    FFormaPagoId := dmgMain.qryExec.FieldByName('id').AsString;
    LoadData;
  end;
  dmgMain.qryExec.Close;
end;

end.
