unit uViewDetallesArticulos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewDetallesBase, 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, uArticulosController,
  cxTextEdit, uDAInterfaces, cxDropDownEdit, cxButtonEdit, 
  Menus, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxEditRepositoryItems, uViewDetallesDTO;

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

  TfrViewDetallesArticulos = class(TfrViewDetallesBase, IViewDetallesArticulos)
    cxGridViewID_ARTICULO: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    actAnadirArticulos: TAction;
    actBuscarArticulo: TAction;
    TBXSeparatorItem8: TTBXSeparatorItem;
    cxEditRepository1: TcxEditRepository;
    cxEditRepositoryComboBox_IMPORTEUNIDAD: TcxEditRepositoryComboBoxItem;
    cxEditRepositoryCurrencyItem_IMPORTEUNIDAD: TcxEditRepositoryCurrencyItem;
    procedure actAnadirArticulosExecute(Sender: TObject);
    procedure actAnadirArticulosUpdate(Sender: TObject);
    procedure OnValidateReferenciaProveedor(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); virtual;
    procedure CustomViewShow(Sender: TObject);
    procedure cxGridViewREFERENCIAPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure actBuscarArticuloExecute(Sender: TObject);
    procedure actBuscarArticuloUpdate(Sender: TObject);
    procedure actDetalleCopiarExecute(Sender: TObject);
    procedure actDetallePegarExecute(Sender: TObject);
    procedure actDetalleCortarExecute(Sender: TObject);
    procedure cxGridViewREFERENCIA_PROVEEDORPropertiesButtonClick(
      Sender: TObject; AButtonIndex: Integer);
    procedure cxGridViewREFERENCIA_FABRICANTEPropertiesButtonClick(
      Sender: TObject; AButtonIndex: Integer);
  protected
    procedure AnadirArticulosInterno; virtual;
    procedure RellenarArticulosInterno; virtual;
  end;

implementation
{$R *.dfm}

uses
  uCalculosUtils, uControllerDetallesBase, uControllerDetallesArticulos, uUnidadesMedidaController;

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;

  SeleccionarFilaActual;
end;

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

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

procedure TfrViewDetallesArticulos.cxGridViewREFERENCIAPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  actBuscarArticulo.Execute;
end;

procedure TfrViewDetallesArticulos.cxGridViewREFERENCIA_FABRICANTEPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  actBuscarArticulo.Execute;
end;

procedure TfrViewDetallesArticulos.cxGridViewREFERENCIA_PROVEEDORPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  actBuscarArticulo.Execute;
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;
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;

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

    RellenarArticulosInterno;
  finally
    cxGridView.EndUpdate;
  end;

  SeleccionarFilaActual;
end;

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

procedure TfrViewDetallesArticulos.actDetalleCopiarExecute(Sender: TObject);
begin
  inherited;
  cxGridView.CopyToClipboard(False);
end;

procedure TfrViewDetallesArticulos.actDetalleCortarExecute(Sender: TObject);
begin
  inherited;
//
end;

procedure TfrViewDetallesArticulos.actDetallePegarExecute(Sender: TObject);
begin
  inherited;
//
end;


end.
