unit uViewControGridStockPrendasCentros;

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, Grids, DBGrids;

type
  IViewControlGridStockPrendasCentros = interface(IViewGrid)
  ['{E6251B25-10FE-4D0C-94CD-0E554F88B54D}']
  end;

  TfrViewControlGridStockPrendasCentros = class(TfrViewGrid, IViewControlGridStockPrendasCentros)
    dxComponentPrinterLink1: TdxGridReportLink;
    frViewFiltrosStock1: TfrViewFiltrosStock;
    cxStyleEntrada: TcxStyle;
    cxStyleSalida: TcxStyle;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxStyleTotales: TcxStyle;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewTIPO_PRODUCTO: TcxGridDBColumn;
    cxGridViewGRUPO_PRODUCTO: TcxGridDBColumn;
    cxGridViewCOLECCION: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridView38: TcxGridDBColumn;
    cxGridView40: TcxGridDBColumn;
    cxGridView42: TcxGridDBColumn;
    cxGridView44: TcxGridDBColumn;
    cxGridView46: TcxGridDBColumn;
    cxGridView48: TcxGridDBColumn;
    cxGridViewTOTAL: TcxGridDBColumn;
    procedure cxGridViewKTCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure cxGridViewCOLUMN7StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridView38GetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: String);
    procedure frViewFiltrosStock1cbColeccionPropertiesEditValueChanged(Sender: TObject);
    procedure frViewFiltrosStock1cbCentroPropertiesEditValueChanged(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;
    procedure Refresh; override;
  end;

implementation
{$R *.dfm}

uses uViewGridBase;

{ TfrViewControlGridResumenVentas }

procedure TfrViewControlGridStockPrendasCentros.Preview;
begin
  inherited;
  dxComponentPrinterLink1.ReportTitle.Text := 'Resumen de stock por centros de colección: ' + frViewFiltrosStock1.cbColeccion.Text;
  dxComponentPrinterLink1.Preview;
end;

procedure TfrViewControlGridStockPrendasCentros.Print;
begin
  inherited;
  dxComponentPrinterLink1.ReportTitle.Text := 'Resumen de stock por centros de colección: ' + frViewFiltrosStock1.cbColeccion.Text;
  dxComponentPrinterLink1.Print(True, Nil);
end;

procedure TfrViewControlGridStockPrendasCentros.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 TfrViewControlGridStockPrendasCentros.Create(AOwner: TComponent);
begin
  inherited;
  frViewFiltrosStock1.FocusedView := cxGridView;
  frViewFiltrosStock1.CentroColumnIndex := -1;
  frViewFiltrosStock1.ColeccionColumnIndex := cxGridViewCOLECCION.Index;
end;

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

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

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

procedure TfrViewControlGridStockPrendasCentros.Refresh;
begin
  inherited;
  frViewFiltrosStock1.Refresh;
end;

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

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

procedure TfrViewControlGridStockPrendasCentros.frViewFiltrosStock1cbCentroPropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

procedure TfrViewControlGridStockPrendasCentros.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 TfrViewControlGridStockPrendasCentros.frViewFiltrosStock1ccbCentroChange(
  Sender: TObject);
begin
  inherited;
  FreeAndNil(FNivelFiltrado);
  FilterChanged(self);
end;

end.

