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, uDAInterfaces, TBXDkPanels,
  uViewParametrosCentro, uCustomView, uViewBase, uViewParametrosControlGrid,
  uViewParametrosColeccion, TB2Dock, uBizStock, uViewParametrosFecha;

type
  IViewControlGridStockEDI = interface(IViewGrid)
  ['{9342442A-28CD-4EA9-95C3-79B1B5302BCE}']
    function GetItems: IBizStockEDI;
    procedure SetItems(const Value: IBizStockEDI);
    property Items: IBizStockEDI read GetItems write SetItems;
  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;
    cxStyleEntrada: TcxStyle;
    cxStyleSalida: TcxStyle;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxStyleTotales: TcxStyle;
    TBXDockablePanel1: TTBXDockablePanel;
    frViewParametrosColeccion1: TfrViewParametrosColeccion;
    frViewParametrosCentro1: TfrViewParametrosCentro;
    TBXAlignmentPanel1: TTBXAlignmentPanel;
    TBXButton1: TTBXButton;
    TBXLabel2: TTBXLabel;
    frViewParametrosFecha1: TfrViewParametrosFecha;
    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);
  protected
    FItems : IBizStockEDI;
    function GetItems: IBizStockEDI;
    procedure SetItems(const Value: IBizStockEDI);

  public
    property Items: IBizStockEDI read GetItems write SetItems;
    procedure Preview; override;
    procedure Print; override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Refresh;

  end;

implementation
{$R *.dfm}

uses uViewGridBase;

{ TfrViewControlGridResumenVentas }

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

procedure TfrViewControlGridStockEDI.Print;
begin
  inherited;
  dxComponentPrinterLink1.ReportTitle.Text := 'Control de stock de colección ' + frViewParametrosColeccion1.ccbColeccion.Text + ' en centro(s) ' + frViewParametrosCentro1.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;
  FItems := Nil;
  frViewParametrosColeccion1.Refresh;
  frViewParametrosCentro1.Refresh;
end;

procedure TfrViewControlGridStockEDI.SetItems(const Value: IBizStockEDI);
begin
  FItems := Value;
  if Assigned(FItems) then
     dsDataSource.DataTable := FItems.DataTable;
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;

destructor TfrViewControlGridStockEDI.Destroy;
begin
  FItems := Nil;
  inherited;
end;

procedure TfrViewControlGridStockEDI.Refresh;
var
  AFecha : Variant;
  AColeccionesList : TStringList;
  ACentrosList : TStringList;
  AWhereColeccion : String;
  AWhereCentro : String;
  AWhereFecha : String;
  AWhere : String;
  i : integer;
begin
  AColeccionesList := Nil;
  ACentrosList := Nil;

  if Assigned(FItems) then
     Items.DataTable.Active := False;
  AColeccionesList := frViewParametrosColeccion1.ColeccionesSeleccionadas;
  ACentrosList := frViewParametrosCentro1.CodigoCentrosSeleccionados;
  AFecha := frViewParametrosFecha1.Fecha;
  
  ShowHourglassCursor;
  try
    if Assigned(AColeccionesList) then
    begin
       AWhereColeccion := '';
       if AColeccionesList.Count = 0 then
       begin
          ShowMessage('Debe seleccionar al menos una colección en los parámetros de la consulta.');
          Exit;
       end
       else begin
          AWhereColeccion := AWhereColeccion + ' COLECCION in (';
          for i := 0 to AColeccionesList.Count - 1 do
          begin
            if i > 0 then
              AWhereColeccion := AWhereColeccion + ', ';
            AWhereColeccion := AWhereColeccion + '''' + AColeccionesList[i] + '''';
          end;
          AWhereColeccion := AWhereColeccion + ')';
        end;
    end;

    if Assigned(ACentrosList) then
    begin
       AWhereCentro := '';
       if ACentrosList.Count = 0 then
       begin
          ShowMessage('Debe seleccionar al menos un centro en los parámetros de la consulta.');
          Exit;
       end
       else begin
          AWhereCentro := AWhereCentro + ' CENTRO in (';
          for i := 0 to ACentrosList.Count - 1 do
          begin
            if i > 0 then
              AWhereCentro := AWhereCentro + ', ';
            AWhereCentro := AWhereCentro + '''' + ACentrosList[i] + '''';
          end;
          AWhereCentro := AWhereCentro + ')';
        end;
    end;

    AWhereFecha := '';
    if not VarIsNull(AFecha) then
       AWhereFecha := '(FECHA >= ''' + VarToStr(AFecha) + ''')';

    AWhere := '';
    if AWhereColeccion <> '' then
       AWhere := AWhere + AWhereColeccion;

    if AWhereCentro <> '' then
    begin
       if AWhere <> '' then
          AWhere := AWhere + ' AND ';
       AWhere := AWhere + AWhereCentro;
    end;

    if AWhereFecha <> '' then
    begin
       if AWhere <> '' then
          AWhere := AWhere + ' AND ';
       AWhere := AWhere + AWhereFecha;
    end;

    Items.DataTable.Where.Clear;
    Items.DataTable.Where.AddText(AWhere, False);
    Items.DataTable.Active := True;

  finally
    HideHourglassCursor;
    FreeAndNIL(AColeccionesList);
    FreeAndNIL(ACentrosList);
  end;
end;

function TfrViewControlGridStockEDI.GetItems: IBizStockEDI;
begin
  Result := FItems;
end;

end.

