unit uViewDetallesArticulos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewDetallesDTO, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxImageComboBox, cxRichEdit,
  cxMaskEdit, cxCurrencyEdit, cxCheckBox, ImgList, PngImageList, uDADataTable,
  StdActns, ExtActns, ActnList, TB2Item, TBX, TB2Dock, TB2Toolbar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ComCtrls, StdCtrls, JvExStdCtrls,
  JvCombobox, JvColorCombo, ToolWin, uControllerDetallesArticulos,
  cxTextEdit, uDAInterfaces;

type
  IViewDetallesArticulos = interface(IViewDetallesDTO)
  ['{D2F72E72-9CEA-4E43-AA18-625F7561E627}']
  end;

  TfrViewDetallesArticulos = class(TfrViewDetallesDTO, IViewDetallesArticulos)
    cxGridViewID_ARTICULO: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewREFERENCIA_PROVEEDOR: TcxGridDBColumn;
    actAnadirArticulos: TAction;
    TBXItem12: TTBXItem;
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure actAnadirArticulosExecute(Sender: TObject);
    procedure actAnadirArticulosUpdate(Sender: TObject);
    procedure OnValidateReferencia(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure OnValidateReferenciaProveedor(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure CustomViewShow(Sender: TObject);

  protected
    procedure AnadirArticulosInterno; virtual;
    procedure RellenarArticulosInterno; virtual;    
    function AnadirArticulo(pReferencia: String; TipoReferencia: TEnumReferencia): Boolean; virtual;
  end;

implementation
{$R *.dfm}

uses uViewDetallesBase;

procedure TfrViewDetallesArticulos.actAnadirArticulosExecute(Sender: TObject);
begin
  cxGridView.BeginUpdate;
  try
    if cxGridView.Controller.EditingController.IsEditing then
       cxGridView.Controller.EditingController.Edit.PostEditValue;

    AnadirArticulosInterno;
  finally
    cxGridView.EndUpdate;
  end;

  SeleccionarRowActual;
end;

procedure TfrViewDetallesArticulos.AnadirArticulosInterno;
begin
  (Controller as IControllerDetallesArticulos).AnadirArticulos(Detalles);
end;

procedure TfrViewDetallesArticulos.CustomViewCreate(Sender: TObject);
begin
  inherited;
  cxGridViewREFERENCIA.Properties.OnValidate := OnValidateReferencia;
  cxGridViewREFERENCIA_PROVEEDOR.Properties.OnValidate := OnValidateReferenciaProveedor;
end;

procedure TfrViewDetallesArticulos.CustomViewDestroy(Sender: TObject);
begin
  inherited;
  cxGridViewREFERENCIA.Properties.OnValidate := Nil;
  cxGridViewREFERENCIA_PROVEEDOR.Properties.OnValidate := Nil;
end;

procedure TfrViewDetallesArticulos.CustomViewShow(Sender: TObject);
begin
  inherited;
  SeleccionarRowActual;
end;

procedure TfrViewDetallesArticulos.OnValidateReferencia(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  lsReferencia : String;
begin
  //En el caso de cadena vacia no validamos
  if (VarIsNull(DisplayValue)) then
     Exit;

  lsReferencia := VarToStr(DisplayValue);
  if (length(trim(lsReferencia)) = 0) then
     Exit;


  if not AnadirArticulo(lsReferencia, tCliente) then
  begin
    ErrorText := 'La referencia introducida no exite en el catálogo';
    Error := True;
  end;
end;

procedure TfrViewDetallesArticulos.OnValidateReferenciaProveedor(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  lsReferencia : String;
begin
  //En el caso de cadena vacia no validamos
  if (VarIsNull(DisplayValue)) then
     Exit;

  lsReferencia := VarToStr(DisplayValue);
  if (length(trim(lsReferencia)) = 0) then
     Exit;

  if not AnadirArticulo(lsReferencia, tProveedor) then
  begin
    ErrorText := 'La referencia introducida no exite en el catálogo';
    Error := True;
  end;
end;

procedure TfrViewDetallesArticulos.RellenarArticulosInterno;
begin
  (Controller as IControllerDetallesArticulos).AnadirArticulos(Detalles, False);
end;

procedure TfrViewDetallesArticulos.actAnadirArticulosUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := actAnadir.Enabled;
end;

function TfrViewDetallesArticulos.AnadirArticulo(pReferencia: String; TipoReferencia: TEnumReferencia): Boolean;
begin
  Result := (Controller as IControllerDetallesArticulos).AnadirArticulo(Detalles, pReferencia, TipoReferencia);
end;

end.
