unit uViewControGridStockEDI;

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, uViewFiltrosStock;

type
  IViewControlGridStockEDI = interface(IViewGrid)
  ['{9342442A-28CD-4EA9-95C3-79B1B5302BCE}']
  end;

  TfrViewControlGridStockEDI = class(TfrViewGrid, IViewControlGridStockEDI)
    dxComponentPrinterLink1: TdxGridReportLink;
    cxEditRepository1: TcxEditRepository;
    cxEditDia: TcxEditRepositoryTextItem;
    cxEditImporte: TcxEditRepositoryCurrencyItem;
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewCENTRO: TcxGridDBColumn;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewTIPO_PRODUCTO: TcxGridDBColumn;
    cxGridViewGRUPO_PRODUCTO: TcxGridDBColumn;
    cxGridViewCOLECCION: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridViewE38: TcxGridDBColumn;
    cxGridViewE40: TcxGridDBColumn;
    cxGridViewE42: TcxGridDBColumn;
    cxGridViewE44: TcxGridDBColumn;
    cxGridViewE46: TcxGridDBColumn;
    cxGridViewE48: TcxGridDBColumn;
    cxGridViewTE: TcxGridDBColumn;
    cxGridViewS38: TcxGridDBColumn;
    cxGridViewS40: TcxGridDBColumn;
    cxGridViewS42: TcxGridDBColumn;
    cxGridViewS44: TcxGridDBColumn;
    cxGridViewS46: TcxGridDBColumn;
    cxGridViewS48: TcxGridDBColumn;
    cxGridViewTS: 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 cxGridViewStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewE38GetDataText(Sender: TcxCustomGridTableItem;
      ARecordIndex: Integer; var AText: String);
    procedure cxGridViewTEStylesGetContentStyle(
      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;
  public
    procedure Preview; override;
    procedure Print; override;
    constructor Create(AOwner: TComponent); override;
    procedure ShowEmbedded(const AParent : TWinControl); override;
    procedure Refresh; override;
  end;

implementation
{$R *.dfm}

uses uViewGridBase;

{ TfrViewControlGridResumenVentas }

procedure TfrViewControlGridStockEDI.Preview;
begin
  inherited;
  dxComponentPrinterLink1.ReportTitle.Text := 'Control de stock de colección ' + frViewFiltrosStock1.cbColeccion.Text + ' en centro(s) ' + frViewFiltrosStock1.ccbCentro.Text;
  dxComponentPrinterLink1.Preview;
end;

procedure TfrViewControlGridStockEDI.Print;
begin
  inherited;
  dxComponentPrinterLink1.ReportTitle.Text := 'Control de stock de colección ' + frViewFiltrosStock1.cbColeccion.Text + ' en centro(s) ' + frViewFiltrosStock1.ccbCentro.Text;
  dxComponentPrinterLink1.Print(True, Nil);
end;

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

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

procedure TfrViewControlGridStockEDI.cxGridViewStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  //
  if Assigned(AItem) then
    if (AItem.Index = cxGridViewTE.Index) or
       (AItem.Index = cxGridViewTS.Index) or
       (AItem.Index = cxGridViewKT.Index) then
       AStyle := cxStyleTotales;
end;

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

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

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


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

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

end.

