unit frm_SelectProvAlbaranes;

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.Grids, Vcl.DBGrids,
  Data.DB, FireDAC.Comp.Client, FireDAC.Stan.Param, uAppTheme, frm_BaseSelect;

type
  TfrmSelectProvAlbaranes = class(TfrmBaseSelect)
  private
    FProveedorId: Integer;
    FEmpresaId: Integer;
    FLastClickColName: string;
    procedure dbGridSelectMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure dbGridSelectKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ToggleComprobado;
  protected
    procedure DoSearch; override;
    procedure DoSelect; override;
  public
    procedure FormShow(Sender: TObject);
    procedure dbGridSelectDblClick(Sender: TObject); override;
    constructor Create(AOwner: TComponent); override;
    property ProveedorId: Integer read FProveedorId write FProveedorId;
    property EmpresaId: Integer read FEmpresaId write FEmpresaId;
  end;

var
  frmSelectProvAlbaranes: TfrmSelectProvAlbaranes;

implementation

uses
  dmg_Main;

{$R *.dfm}

constructor TfrmSelectProvAlbaranes.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  MultiSelect := True;
end;

procedure TfrmSelectProvAlbaranes.FormShow(Sender: TObject);
begin
  inherited FormShow(Sender);
  dbGridSelect.OnMouseUp := dbGridSelectMouseUp;
  dbGridSelect.OnKeyDown := dbGridSelectKeyDown;
end;

procedure TfrmSelectProvAlbaranes.ToggleComprobado;
var
  LField: TField;
  LNewVal: Integer;
  LAlbaranId: Integer;
  LQryUpd: TFDQuery;
  LPrevReadOnly: Boolean;
begin
  if not dmgMain.qryExec.Active or dmgMain.qryExec.IsEmpty then Exit;
  LField := dmgMain.qryExec.FindField('comprobado');
  if not Assigned(LField) then Exit;

  LAlbaranId := dmgMain.qryExec.FieldByName('id').AsInteger;
  if LAlbaranId <= 0 then Exit;

  if LField.IsNull or (LField.AsInteger = 0) then
    LNewVal := 1
  else
    LNewVal := 0;

  LQryUpd := TFDQuery.Create(nil);
  try
    LQryUpd.Connection := dmgMain.dbConn;
    LQryUpd.SQL.Text := 'UPDATE ge_prov_albaranes SET comprobado = :val WHERE id = :id';
    LQryUpd.ParamByName('val').AsInteger := LNewVal;
    LQryUpd.ParamByName('id').AsInteger := LAlbaranId;
    LQryUpd.ExecSQL;
  finally
    LQryUpd.Free;
  end;

  LPrevReadOnly := LField.ReadOnly;
  try
    LField.ReadOnly := False;
    if not (dmgMain.qryExec.State in [dsEdit, dsInsert]) then
      dmgMain.qryExec.Edit;
    LField.AsInteger := LNewVal;
    dmgMain.qryExec.Post;
  finally
    LField.ReadOnly := LPrevReadOnly;
  end;

  dbGridSelect.Invalidate;
end;

procedure TfrmSelectProvAlbaranes.dbGridSelectMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  LCoord: TGridCoord;
  LColIdx: Integer;
  LCol: TColumn;
begin
  if Button <> mbLeft then Exit;
  FLastClickColName := '';

  LCoord := dbGridSelect.MouseCoord(X, Y);
  if (LCoord.Y < 1) or (LCoord.X < 0) then Exit;

  LColIdx := LCoord.X;
  if dgIndicator in dbGridSelect.Options then
    Dec(LColIdx);

  if (LColIdx >= 0) and (LColIdx < dbGridSelect.Columns.Count) then
  begin
    LCol := dbGridSelect.Columns[LColIdx];
    if Assigned(LCol.Field) then
    begin
      FLastClickColName := LCol.Field.FieldName;
      if SameText(FLastClickColName, 'comprobado') then
        ToggleComprobado;
    end;
  end;
end;

procedure TfrmSelectProvAlbaranes.dbGridSelectDblClick(Sender: TObject);
begin
  if SameText(FLastClickColName, 'comprobado') then
    Exit;
  inherited dbGridSelectDblClick(Sender);
end;

procedure TfrmSelectProvAlbaranes.dbGridSelectKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_SPACE then
  begin
    ToggleComprobado;
    Key := 0;
  end;
end;

procedure TfrmSelectProvAlbaranes.DoSearch;
var
  LSql: string;
  LSearch: string;
begin
  dmgMain.qryExec.Close;
  LSql := 'SELECT id, documento, fecha, base, totaliva, totalentrada, comprobado ' +
          'FROM ge_prov_albaranes ' +
          'WHERE (consolidado = 0 OR consolidado IS NULL) ';

  if FProveedorId > 0 then
    LSql := LSql + 'AND idproveedor = :idprov ';

  if FEmpresaId > 0 then
    LSql := LSql + 'AND idempresa = :idempresa ';

  LSearch := Trim(edtSearch.Text);
  if LSearch <> '' then
    LSql := LSql + 'AND (documento LIKE :search OR DATE_FORMAT(fecha, ''%d/%m/%Y'') LIKE :search) ';

  LSql := LSql + 'ORDER BY fecha DESC, documento DESC';

  dmgMain.qryExec.SQL.Text := LSql;
  if FProveedorId > 0 then
    dmgMain.qryExec.ParamByName('idprov').AsInteger := FProveedorId;

  if FEmpresaId > 0 then
    dmgMain.qryExec.ParamByName('idempresa').AsInteger := FEmpresaId;

  if LSearch <> '' then
    dmgMain.qryExec.ParamByName('search').AsString := '%' + LSearch + '%';

  dmgMain.qryExec.Open;

  if dmgMain.qryExec.FindField('id') <> nil then
    dmgMain.qryExec.FieldByName('id').Visible := False;

  if dmgMain.qryExec.FindField('documento') <> nil then
  begin
    dmgMain.qryExec.FieldByName('documento').DisplayLabel := 'Nº Albarán';
    dmgMain.qryExec.FieldByName('documento').DisplayWidth := 15;
  end;

  if dmgMain.qryExec.FindField('fecha') <> nil then
  begin
    dmgMain.qryExec.FieldByName('fecha').DisplayLabel := 'Fecha';
    dmgMain.qryExec.FieldByName('fecha').DisplayWidth := 12;
  end;

  if dmgMain.qryExec.FindField('base') <> nil then
  begin
    dmgMain.qryExec.FieldByName('base').DisplayLabel := 'Base Imp.';
    dmgMain.qryExec.FieldByName('base').DisplayWidth := 12;
    TNumericField(dmgMain.qryExec.FieldByName('base')).DisplayFormat := '#,##0.00 €';
  end;

  if dmgMain.qryExec.FindField('totaliva') <> nil then
  begin
    dmgMain.qryExec.FieldByName('totaliva').DisplayLabel := 'IVA';
    dmgMain.qryExec.FieldByName('totaliva').DisplayWidth := 10;
    TNumericField(dmgMain.qryExec.FieldByName('totaliva')).DisplayFormat := '#,##0.00 €';
  end;

  if dmgMain.qryExec.FindField('totalentrada') <> nil then
  begin
    dmgMain.qryExec.FieldByName('totalentrada').DisplayLabel := 'Total Albarán';
    dmgMain.qryExec.FieldByName('totalentrada').DisplayWidth := 14;
    TNumericField(dmgMain.qryExec.FieldByName('totalentrada')).DisplayFormat := '#,##0.00 €';
  end;

  if dmgMain.qryExec.FindField('comprobado') <> nil then
  begin
    dmgMain.qryExec.FieldByName('comprobado').DisplayLabel := 'Validado';
    dmgMain.qryExec.FieldByName('comprobado').DisplayWidth := 8;
  end;
end;

procedure TfrmSelectProvAlbaranes.DoSelect;
var
  i: Integer;
begin
  SelectedIds.Clear;
  SelectedCodes.Clear;
  SelectedDescs.Clear;

  if dbGridSelect.SelectedRows.Count > 0 then
  begin
    for i := 0 to dbGridSelect.SelectedRows.Count - 1 do
    begin
      dmgMain.qryExec.Bookmark := dbGridSelect.SelectedRows[i];
      SelectedIds.Add(dmgMain.qryExec.FieldByName('id').AsString);
      SelectedCodes.Add(dmgMain.qryExec.FieldByName('documento').AsString);
      SelectedDescs.Add(dmgMain.qryExec.FieldByName('documento').AsString);
    end;
  end
  else if not dmgMain.qryExec.IsEmpty then
  begin
    SelectedIds.Add(dmgMain.qryExec.FieldByName('id').AsString);
    SelectedCodes.Add(dmgMain.qryExec.FieldByName('documento').AsString);
    SelectedDescs.Add(dmgMain.qryExec.FieldByName('documento').AsString);
  end;

  if SelectedIds.Count > 0 then
    ModalResult := mrOk;
end;

end.
