unit uViewControGridStockVARELA;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewControlGrid, DB, uDADataTable, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData,
  cxTextEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxCurrencyEdit, cxCalendar, dxPSGlbl, dxPSUtl, dxPrnPg, dxBkgnd, dxWrap,
  dxPrnDev, dxPSEngn, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPScxGridLnk, dxPgsDlg,
  cxDropDownEdit, StdCtrls, cxSpinEdit, uViewGrid, cxMemo,
  cxEditRepositoryItems, uDataModuleStock, uCustomView, uViewBase,
  uViewParametrosControlGrid, uViewFiltrosStock;

type
  IViewControlGridStockVARELA = interface(IViewGrid)
  ['{19831B1A-1D3B-4352-A848-52B00BFEF4EF}']
  end;

  TfrViewControlGridStockVARELA = class(TfrViewGrid, IViewControlGridStockVARELA)
    dxComponentPrinterLink1: TdxGridReportLink;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewFILIAL: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewTIPO_PRODUTO: TcxGridDBColumn;
    cxGridViewGRUPO_PRODUTO: TcxGridDBColumn;
    cxGridViewCOLECCION: TcxGridDBColumn;
    cxGridViewK38: TcxGridDBColumn;
    cxGridViewK40: TcxGridDBColumn;
    cxGridViewK42: TcxGridDBColumn;
    cxGridViewK44: TcxGridDBColumn;
    cxGridViewK46: TcxGridDBColumn;
    cxGridViewK48: TcxGridDBColumn;
    cxGridViewKT: TcxGridDBColumn;
    frViewFiltrosStock1: TfrViewFiltrosStock;
    cxStyleEntrada: TcxStyle;
    cxStyleSalida: TcxStyle;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxStyleTotales: TcxStyle;
    procedure cxGridViewKTCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure cxGridViewK38GetDataText(Sender: TcxCustomGridTableItem;
      ARecordIndex: Integer; var AText: String);
    procedure cxGridViewKTStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure frViewFiltrosStock1cbColeccionPropertiesEditValueChanged(Sender: TObject);
    procedure frViewFiltrosStock1ccbCentroChange(Sender: TObject);
  protected
    FNivelFiltrado : TcxFilterCriteriaItemList;
    procedure FilterChanged(Sender: TObject); override;
    function GetFocusedView : TcxGridDBTableView; override;
  public
    procedure ShowEmbedded(const AParent : TWinControl); override;  
    procedure Preview; override;
    procedure Print; override;
    constructor Create(AOwner: TComponent); override;
  end;

implementation
{$R *.dfm}

uses uViewGridBase;

{ TfrViewControlGridResumenVentas }

procedure TfrViewControlGridStockVARELA.Preview;
begin
  inherited; 
  dxComponentPrinterLink1.ReportTitle.Text := 'Control de stock en almacén';
  dxComponentPrinterLink1.Preview;
end;

procedure TfrViewControlGridStockVARELA.Print;
begin
  inherited;
  dxComponentPrinterLink1.ReportTitle.Text := 'Control de stock en almacén';
  dxComponentPrinterLink1.Print(True, Nil);
end;

procedure TfrViewControlGridStockVARELA.cxGridViewKTCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;
  if not VarIsNull(AViewInfo.Value) then
    if Pos('-', AViewInfo.Value) > 0 then
       ACanvas.Canvas.Font.Color := $000000CC;
end;

constructor TfrViewControlGridStockVARELA.Create(AOwner: TComponent);
begin
  inherited;
  frViewFiltrosStock1.FocusedView := cxGridView;
  frViewFiltrosStock1.CentroColumnIndex := cxGridViewFILIAL.Index;
  frViewFiltrosStock1.ColeccionColumnIndex := cxGridViewCOLECCION.Index;
end;

function TfrViewControlGridStockVARELA.GetFocusedView: TcxGridDBTableView;
begin
  Result := cxGridView;
end;

procedure TfrViewControlGridStockVARELA.ShowEmbedded(
  const AParent: TWinControl);
begin
  inherited;
  frViewFiltrosStock1.Refresh;
end;

procedure TfrViewControlGridStockVARELA.cxGridViewK38GetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
begin
  inherited;
  ShowMessage(AText);
  if AText = '0' then
    AText := ''; 
end;

procedure TfrViewControlGridStockVARELA.cxGridViewKTStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  AStyle := cxStyleTotales;
end;

procedure TfrViewControlGridStockVARELA.frViewFiltrosStock1cbColeccionPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

procedure TfrViewControlGridStockVARELA.FilterChanged(Sender: TObject);
var
  AItemList: TcxFilterCriteriaItemList;
  i : integer;
begin
  inherited;

  cxGridView.DataController.Filter.BeginUpdate;
  AItemList := cxGridView.DataController.Filter.Root;
  FNivelFiltrado := AItemList.AddItemList(fboAnd);

  if (Length(frViewFiltrosStock1.cbColeccion.Text) > 0) and (frViewFiltrosStock1.cbColeccion.Text <> 'Todos') then
     FNivelFiltrado.AddItem(cxGridView.Columns[frViewFiltrosStock1.ColeccionColumnIndex], foEqual, frViewFiltrosStock1.cbColeccion.Text, frViewFiltrosStock1.cbColeccion.Text);

  if (Length(frViewFiltrosStock1.ccbCentro.Text) > 0) and
     (Pos('Todos', frViewFiltrosStock1.ccbCentro.Text) = 0) then
  begin
    FNivelFiltrado := FNivelFiltrado.AddItemList(fboOr);
    for i := 0 to frViewFiltrosStock1.ccbCentro.Items.Count - 1 do
      if frViewFiltrosStock1.ccbCentro.IsChecked(i) then
        FNivelFiltrado.AddItem(cxGridView.Columns[frViewFiltrosStock1.CentroColumnIndex], foEqual, frViewFiltrosStock1.ccbCentro.Items[i], frViewFiltrosStock1.ccbCentro.Items[i]);
  end;

  if cxGridView.DataController.Filter.IsEmpty then
    cxGridView.DataController.Filter.Active := False
  else
    cxGridView.DataController.Filter.Active := True;

  cxGridView.DataController.Filter.EndUpdate;
end;

procedure TfrViewControlGridStockVARELA.frViewFiltrosStock1ccbCentroChange(
  Sender: TObject);
begin
  inherited;
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

end.

