unit uEditorControlStockEDI;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorControlBase, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane,
  uCustomView, uViewBase, uViewControlBase, Grids, DBGrids, pngimage, StdCtrls,
  uEditorGridBase, uViewGridBase, uViewGrid, Menus, TB2ExtItems, TBXExtItems,

  uBizStock,uViewControGridStockEDI, uViewControGridStockPrendasCentros,
  uViewControGridStockEDIInventario;

type
  IEditorControlStockEDI = interface(IEditorGridBase)
  ['{8CDABF14-FD43-48BF-8B33-B6B5A9B03CEB}']
{    function GetItems: IBizStockEDI;
    procedure SetItems(const Value: IBizStockEDI);
    property Items: IBizStockEDI read GetItems write SetItems;}
  end;

  TfEditorControlStockEDI = class(TfEditorGridBase, IEditorControlStockEDI)
    dsVentasTerminadas: TDADataSource;
    TBXSeparatorItem16: TTBXSeparatorItem;
    tbxiAnchoAuto: TTBXItem;
    actExpandir: TAction;
    actContraer: TAction;
    actAnchoAuto: TAction;
    TBXItem37: TTBXItem;
    TBXItem36: TTBXItem;
    TBXItem35: TTBXItem;
    TBXSeparatorItem17: TTBXSeparatorItem;
    TBXItem38: TTBXItem;
    TBXItem39: TTBXItem;
    frViewControlGridStockEDI1: TfrViewControlGridStockEDI;
    PageControl1: TPageControl;
    tabResumen: TTabSheet;
    tabDesglose: TTabSheet;
    frViewControlGridStockPrendasCentros1: TfrViewControlGridStockPrendasCentros;
    tabInventario: TTabSheet;
    frViewControlGridStockEDIInventario1: TfrViewControlGridStockEDIInventario;
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure FormShow(Sender: TObject); override;
    procedure actExpandirExecute(Sender: TObject);
    procedure actContraerExecute(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
  protected
    FDesglose : IBizStockEDI;
    FInventario : IBizStockEDIInventario;
    FResumen : IBizStockPrendasCentros;

    function GetItemsDesglose: IBizStockEDI;
    procedure SetItemsDesglose(const Value: IBizStockEDI);

    function GetItemsInventario: IBizStockEDIInventario;
    procedure SetItemsInventario(const Value: IBizStockEDIInventario);

    function GetItemsResumen: IBizStockPrendasCentros;
    procedure SetItemsResumen(const Value: IBizStockPrendasCentros);
  public
    property ItemsDesglose: IBizStockEDI read GetItemsDesglose write SetItemsDesglose;
    property ItemsInventario: IBizStockEDIInventario read GetItemsInventario write SetItemsInventario;
    property ItemsResumen: IBizStockPrendasCentros read GetItemsResumen write SetItemsResumen;
    constructor Create(AOwner: TComponent); override;
  end;


implementation

uses
  cxControls, uEditorUtils, DateUtils, uCustomEditor, uEditorBase,
  uDataModuleStock;

{$R *.dfm}

{ TfEditorControlStockEDI }

procedure TfEditorControlStockEDI.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  case PageControl1.ActivePageIndex of
    0   : frViewControlGridStockPrendasCentros1.AjustarAncho;
    1   : frViewControlGridStockEDI1.AjustarAncho;
    2   : frViewControlGridStockEDIInventario1.AjustarAncho;    
  end;
end;

procedure TfEditorControlStockEDI.FormShow(Sender: TObject);
begin
//  inherited;  <- No descomentar

  if not Assigned(ViewGrid) then
    raise Exception.Create('No hay una vista asignada');

  case PageControl1.ActivePageIndex of
    0 : ViewGrid.ShowEmbedded(tabResumen);
    1 : ViewGrid.ShowEmbedded(tabDesglose);
    2 : ViewGrid.ShowEmbedded(tabInventario);    
  end;

  ViewGrid.ExpandirTodo;
end;

constructor TfEditorControlStockEDI.Create(AOwner: TComponent);
begin
  inherited;
  FResumen := nil;
  FDesglose := nil;
  FInventario := nil;  
  
  Self.Caption := 'Control de stock en centros';
  JvNavPanelHeader.Caption := Self.Caption;
  PageControl1.ActivePageIndex := 0;

  if not Assigned(FResumen) then
    ItemsResumen := dmStock.GetItemsStockPrendasCentros;

  ViewGrid := frViewControlGridStockPrendasCentros1;
end;

procedure TfEditorControlStockEDI.actExpandirExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.ExpandirTodo;
end;

procedure TfEditorControlStockEDI.actContraerExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.ContraerTodo;
end;

function TfEditorControlStockEDI.GetItemsDesglose: IBizStockEDI;
begin
  Result := FDesglose;
end;

function TfEditorControlStockEDI.GetItemsResumen: IBizStockPrendasCentros;
begin
  Result := FResumen;
end;

procedure TfEditorControlStockEDI.SetItemsDesglose(const Value: IBizStockEDI);
begin
  FDesglose := Value;
  if Assigned(FDesglose) then
  begin
    ShowHourglassCursor;
    try
      dsDataTable.DataTable := FDesglose.DataTable;
      frViewControlGridStockEDI1.dsDataSource.DataTable := FDesglose.DataTable;

      if not FDesglose.DataTable.Active then
         FDesglose.DataTable.Active := True;

      actAnchoAuto.Execute;
    finally
      HideHourglassCursor;
    end;
  end;
end;

procedure TfEditorControlStockEDI.SetItemsResumen(
  const Value: IBizStockPrendasCentros);
begin
  FResumen := Value;
  if Assigned(FResumen) then
  begin
    ShowHourglassCursor;
    try
      dsDataTable.DataTable := FResumen.DataTable;
      frViewControlGridStockPrendasCentros1.dsDataSource.DataTable := FResumen.DataTable;

      if not FResumen.DataTable.Active then
         FResumen.DataTable.Active := True;

      actAnchoAuto.Execute;
    finally
      HideHourglassCursor;
    end;
  end;
end;

procedure TfEditorControlStockEDI.PageControl1Change(Sender: TObject);
begin
  inherited;
  Self.Update;

  //Quitamos el filtro que hubiera
  actQuitarFiltro.Execute;

  case PageControl1.ActivePageIndex of
    0   : begin
            ViewGrid := frViewControlGridStockPrendasCentros1;
            if not Assigned(FResumen) then
              ItemsResumen := dmStock.GetItemsStockPrendasCentros;
          end;
    1   : begin
            ViewGrid := frViewControlGridStockEDI1;
            if not Assigned(FDesglose) then
              ItemsDesglose := dmStock.GetItemsStockEDI;
          end;
    2   : begin
            ViewGrid := frViewControlGridStockEDIInventario1;
            if not Assigned(FInventario) then
              ItemsInventario := dmStock.GetItemsStockEDIInventario;
          end;
  end;

  ViewGrid.Refresh;
end;

function TfEditorControlStockEDI.GetItemsInventario: IBizStockEDIInventario;
begin
  Result := FInventario;
end;

procedure TfEditorControlStockEDI.SetItemsInventario(const Value: IBizStockEDIInventario);
begin
  FInventario := Value;
  if Assigned(FInventario) then
  begin
    ShowHourglassCursor;
    try
      dsDataTable.DataTable := FInventario.DataTable;
      frViewControlGridStockEDIInventario1.dsDataSource.DataTable := FInventario.DataTable;

      if not FInventario.DataTable.Active then
         FInventario.DataTable.Active := True;

      actAnchoAuto.Execute;
    finally
      HideHourglassCursor;
    end;
  end;
end;

end.

