unit uViewInventario;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frxClass, frxPreview, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB,
  cxDBData, ActnList, uDADataTable, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, StdCtrls, ExtCtrls, cxLabel, dxPSGlbl, dxPSUtl,
  dxPSEngn, dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, cxIntlPrintSys3, dxPSCore, dxPScxCommon, dxPScxGrid6Lnk, cxImage,
  cxImageComboBox, ImgList, PngImageList, cxTextEdit, Grids, DBGrids, cxDBLookupComboBox,
  cxButtonEdit, cxGridCustomPopupMenu, cxGridPopupMenu, uViewGrid,
  uBizInventario, cxSpinEdit, uViewFiltroBase, TB2Item, TBX,
  TB2Toolbar, TBXDkPanels, TB2Dock, dxPgsDlg, cxCurrencyEdit, uDAInterfaces,
  cxContainer, cxMaskEdit, cxDropDownEdit;
//  uBizAlmacenes, uBizObras;

type
  IViewInventario = interface(IViewGrid)
    ['{4BAE6CBE-4CBB-4103-B85E-80BE5310723A}']
    function GetInventario: IBizInventario;
    procedure SetInventario(const Value: IBizInventario);
    property Inventario: IBizInventario read GetInventario write SetInventario;
    function GetAlmacenes: TStringList;
    procedure SetAlmacenes(const Value: TStringList);
    property Almacenes: TStringList read GetAlmacenes write SetAlmacenes;
    function GetObras: TStringList;
    procedure SetObras(const Value: TStringList);
    property Obras: TStringList read GetObras write SetObras;
    function DarIDAlmacenObraSeleccionada: Variant;
    function ListaAlmacenesObrasVacia: Boolean;
  end;

  TfrViewInventario = class(TfrViewGrid, IViewInventario)
    cxGridDBTableView1: TcxGridDBTableView;
    cxStyleArticuloSinStock: TcxStyle;
    cxGridViewSTOCK: TcxGridDBColumn;
    cxGridViewDESCRIPCION: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewFAMILIA: TcxGridDBColumn;
    cxGridViewALMACEN: TcxGridDBColumn;
    cxGridViewID_ALMACEN: TcxGridDBColumn;
    cxGridViewRESERVA: TcxGridDBColumn;
    cxGridViewPENDIENTE_RECEPCION: TcxGridDBColumn;
    cxGridViewREFERENCIA_PROVEEDOR: TcxGridDBColumn;
    cxGridViewUNIDADES_ALMACEN: TcxGridDBColumn;
    cxGridViewPRECIO_NETO: TcxGridDBColumn;
    cxGridViewCOSTE_UNIDADES: TcxGridDBColumn;
    cxGridViewID: TcxGridDBColumn;
    cxGridViewID_ARTICULO: TcxGridDBColumn;
    cxStyleArticuloConStock: TcxStyle;
    pnlAlmacenes: TPanel;
    cxListaAlmacenesObras: TcxComboBox;
    eAlmacen: TLabel;
    procedure cxGridViewStylesGetContentStyle(Sender: TcxCustomGridTableView;
            ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out
            AStyle: TcxStyle);
    procedure cxListaObrasPropertiesEditValueChanged(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
    procedure cxGridViewDataControllerCompare(
      ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
      AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);

  private
    //Filtros relativos a la vista
    procedure AnadirFiltroAlmacenesObras;

  protected
    FAlmacenes: TStringList;
    FObras: TStringList;
    FInventario: IBizInventario;
    function GetAlmacenes: TStringList;
    procedure SetAlmacenes(const Value: TStringList);
    function GetObras: TStringList;
    procedure SetObras(const Value: TStringList);
    function GetInventario: IBizInventario;
    procedure SetInventario(const Value: IBizInventario);
  public
    property Inventario: IBizInventario read GetInventario write SetInventario;
    procedure AnadirOtrosFiltros; override;
    destructor Destroy; override;
    function DarIDAlmacenObraSeleccionada: Variant;
    function ListaAlmacenesObrasVacia: Boolean;
  end;

implementation
{$R *.dfm}

uses uDataModuleInventario, schInventarioClient_Intf, uReferenciasUtils, cxVariants;

procedure TfrViewInventario.AnadirFiltroAlmacenesObras;
var
  FFiltro : TcxFilterCriteriaItemList;
  CodigoAlmacen: Variant;
begin
  FFiltro := AddFilterGrid(fboAnd);

  CodigoAlmacen := DarIDAlmacenObraSeleccionada;

  if not VarIsNull(CodigoAlmacen) then
  begin
     FFiltro.AddItem(cxGridViewID_ALMACEN, foEqual, CodigoAlmacen, CodigoAlmacen);
     cxGridViewALMACEN.Visible := False;
  end
  else
     cxGridViewALMACEN.Visible := True;
end;

procedure TfrViewInventario.AnadirOtrosFiltros;
begin
  inherited;

  AnadirFiltroAlmacenesObras;

  //Finalmente activamos el filtro si tenemos algo
  if cxGridView.DataController.Filter.IsEmpty
  then cxGridView.DataController.Filter.Active := False
  else cxGridView.DataController.Filter.Active := True;
  cxGrid.ActiveLevel.GridView := cxGridView;
end;

procedure TfrViewInventario.CustomViewShow(Sender: TObject);
begin
  inherited;
  if Assigned(FAlmacenes) or Assigned(FObras) then
     cxListaAlmacenesObras.ItemIndex := 0
  //En el caso de ser la vista para la selección de articulos de un determinado almacén
  else
     pnlAlmacenes.Visible:= False;
end;

procedure TfrViewInventario.cxGridViewDataControllerCompare(
  ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
  AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
begin
  inherited;
  if (AItemIndex = cxGridViewREFERENCIA.Index) and
     (VarType(V1) = VarType(V2)) and (VarType(V1) = varString) then
    Compare := CompararReferencias(V1, V2)
  else
    Compare := VarCompare(V1, V2);
end;

procedure TfrViewInventario.cxGridViewStylesGetContentStyle(Sender:
        TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem:
        TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  IndiceCol: Integer;
  AStock: Variant;
begin
  inherited;
  if Assigned(ARecord) then
  begin
    IndiceCol := (Sender as TcxGridDBTableView).GetColumnByFieldName(fld_INVENTARIOSTOCK).Index;
    AStock := ARecord.DisplayTexts[IndiceCol];
    if (AStock < 0) then
      AStyle.TextColor := cxStyleArticuloSinStock.TextColor
    else
      AStyle.TextColor := cxStyleArticuloConStock.TextColor;
  end;
end;

procedure TfrViewInventario.cxListaObrasPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  RefrescarFiltro;  
end;

function TfrViewInventario.DarIDAlmacenObraSeleccionada: Variant;
begin
  //INV_ALMACENES
  if Assigned(FAlmacenes) then
     Result := FAlmacenes.Values[cxListaAlmacenesObras.EditValue]
  //INV_OBRAS
  else if Assigned(FObras) then
     Result := FObras.Values[cxListaAlmacenesObras.EditValue]
  else
     Raise Exception.Create('No se ha asignado ninguna lista de ALMACENES');
end;

destructor TfrViewInventario.Destroy;
begin
  FAlmacenes := Nil;
  FObras := Nil;
  FInventario := Nil;
  inherited;
end;

function TfrViewInventario.GetAlmacenes: TStringList;
begin
  Result := FAlmacenes;
end;

function TfrViewInventario.GetInventario: IBizInventario;
begin
  Result := FInventario;
end;

function TfrViewInventario.GetObras: TStringList;
begin
  Result := FObras;
end;

function TfrViewInventario.ListaAlmacenesObrasVacia: Boolean;
begin
  Result := (cxListaAlmacenesObras.Properties.Items.Count = 0);
end;

procedure TfrViewInventario.SetAlmacenes(const Value: TStringList);
var
  Nivel: TcxGridLevel;
  i: integer;
begin
  FAlmacenes := Value;
  eAlmacen.Caption := 'Almacén';

  if Assigned(FAlmacenes) then
  begin
    cxGrid.BeginUpdate;
    try
      with cxListaAlmacenesObras.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FAlmacenes.Count - 1 do
            Add(FAlmacenes.Names[i]);
        finally
          EndUpdate;
        end;
      end;

    finally
      cxGrid.EndUpdate;
    end;
  end;
end;

procedure TfrViewInventario.SetInventario(const Value: IBizInventario);
begin
  FInventario := Value;
  if Assigned(FInventario) then
     dsDataSource.DataTable := FInventario.DataTable
  else
     dsDataSource.DataTable := Nil;
end;

procedure TfrViewInventario.SetObras(const Value: TStringList);
var
  Nivel: TcxGridLevel;
  i: integer;
begin
  FObras := Value;
  eAlmacen.Caption := 'Obra';

  if Assigned(FObras) then
  begin
    cxGrid.BeginUpdate;
    try
      with cxListaAlmacenesObras.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FObras.Count - 1 do
            Add(FObras.Names[i]);
        finally
          EndUpdate;
        end;
      end;

    finally
      cxGrid.EndUpdate;
    end;
  end;
end;

end.
