unit uBalancesController;

interface


uses
  Classes, SysUtils, uDADataTable, uControllerBase,
	uBizBalances, uIDataModuleContabilidad;
type
  IBalancesController = interface(IControllerBase)
  ['{94E5F2B6-64C8-4331-B9CB-3ED730478529}']
    function BuscarTodos: IBizBalance;
    function Buscar(ID: Integer): IBizBalance;
    procedure VerTodos(ABalances: IBizBalance);
    procedure Ver(ABalance: IBizBalance);
    procedure Anadir(ABalance : IBizBalance);
    function Eliminar(ABalance : IBizBalance): Boolean;
    function Guardar(ABalance : IBizBalance): Boolean;
    procedure DescartarCambios(ABalance : IBizBalance);
    function Localizar(ABalances: IBizBalance; ADescripcion:String): Boolean;
    function DarListaBalances: TStringList;

    function ExtraerSeleccionados(ABalances: IBizBalance) : IBizBalance;
    function ElegirBalance(ABalances : IBizBalance; AMensaje: String; AMultiSelect: Boolean): IBizBalance;
  end;

  TBalancesController = class(TControllerBase, IBalancesController)
  protected
    FDataModule : IDataModuleContabilidad;

    procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
    function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean;

    function ValidarBalance(ABalance: IBizBalance): Boolean;
    procedure AsignarDataModule;

  public
    constructor Create; override;
    destructor Destroy; override;

    function Eliminar(ABalance : IBizBalance): Boolean;
    function Guardar(ABalance : IBizBalance): Boolean; virtual;
    procedure DescartarCambios(ABalance : IBizBalance); virtual;
    procedure Anadir(ABalance : IBizBalance);
    function BuscarTodos: IBizBalance;
    function Buscar(ID: Integer): IBizBalance;
    procedure VerTodos(ABalances: IBizBalance);
    procedure Ver(ABalance: IBizBalance);
    function Localizar(ABalances: IBizBalance; ADescripcion:String): Boolean;
    function DarListaBalances: TStringList;

    function ExtraerSeleccionados(ABalances: IBizBalance) : IBizBalance;
    function ElegirBalance(ABalances : IBizBalance; AMensaje: String; AMultiSelect: Boolean): IBizBalance;
  end;

implementation

uses
  cxControls, DB, uEditorRegistryUtils, schContabilidadClient_Intf,
  uIEditorBalances, uIEditorBalance, uIEditorElegirBalances, uDataModuleContabilidad,
  uDAInterfaces, uDataTableUtils, uDialogUtils,
  uDateUtils, uROTypes, DateUtils, Controls, Windows;

{ TBalancesController }

procedure TBalancesController.Anadir(ABalance: IBizBalance);
begin
  ABalance.Insert;
end;

procedure TBalancesController.AsignarDataModule;
begin
  FDataModule := TDataModuleContabilidad.Create(Nil);
end;

function TBalancesController.Buscar(ID: Integer): IBizBalance;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := BuscarTodos;

    with Result.DataTable.DynamicWhere do
    begin
      // (ID = :ID)
      Condicion := NewBinaryExpression(NewField('', fld_BalancesID), NewConstant(ID, datInteger), dboEqual);

      if IsEmpty then
         Expression := Condicion
      else
         Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
    end;

  finally
    HideHourglassCursor;
  end;
end;

function TBalancesController.BuscarTodos: IBizBalance;
begin
  Result := FDataModule.GetBalanceItems;
end;

constructor TBalancesController.Create;
begin
  inherited;
  AsignarDataModule;
end;

function TBalancesController.CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
begin
  Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf);
end;

function TBalancesController.DarListaBalances: TStringList;
var
  ABalances: IBizBalance;
begin
  ABalances := BuscarTodos;
  ABalances.DataTable.Active := True;
  Result := TStringList.Create;
  try
    with Result do
    begin
      ABalances.DataTable.First;
      while not ABalances.DataTable.EOF do
      begin
        Add(ABalances.REF_BALANCE);
        ABalances.DataTable.Next;
      end;
    end;
  finally
    ABalances := NIL;
  end;
end;

procedure TBalancesController.DescartarCambios(ABalance: IBizBalance);
begin
  if not Assigned(ABalance) then
    raise Exception.Create ('Balance no asignado');

  ShowHourglassCursor;
  try
    if (ABalance.State in dsEditModes) then
      ABalance.Cancel;

    ABalance.DataTable.CancelUpdates;
  finally
    HideHourglassCursor;
  end;
end;

destructor TBalancesController.Destroy;
begin
  FDataModule:= NIL;
  inherited;
end;

function TBalancesController.ValidarBalance(ABalance: IBizBalance): Boolean;
begin
  Result := False;

  if not Assigned(ABalance) then
    raise Exception.Create ('Balance no asignado');

  if (ABalance.DataTable.State in dsEditModes) then
    ABalance.DataTable.Post;

  if Length(ABalance.REF_BALANCE) = 0 then
    raise Exception.Create('Debe indicar un nombre para este Balance.');

  Result := True;
end;

procedure TBalancesController.Ver(ABalance: IBizBalance);
var
  AEditor : IEditorBalance;
begin
  AEditor := NIL;

  CreateEditor('EditorBalance', IEditorBalance, AEditor);
  if Assigned(AEditor) then
    try
      AEditor.Balance := ABalance;
      AEditor.ShowModal;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
end;

procedure TBalancesController.VerTodos(ABalances: IBizBalance);
var
  AEditor : IEditorBalances;
begin
  AEditor := NIL;

  CreateEditor('EditorBalances', IEditorBalances, AEditor);
  if Assigned(AEditor) then
    try
      AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
      AEditor.Balances := ABalances;
      AEditor.ShowModal;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
end;

function TBalancesController.ElegirBalance(ABalances: IBizBalance; AMensaje: String; AMultiSelect: Boolean): IBizBalance;
var
  AEditor : IEditorElegirBalances;
begin
  Result := NIL;

  CreateEditor('EditorElegirBalances', IEditorElegirBalances, AEditor);
  if Assigned(AEditor) then
    try
      AEditor.Balances := ABalances;
      AEditor.Controller := Self;
      AEditor.Mensaje := AMensaje;
      if IsPositiveResult(AEditor.ShowModal) then
        Result := AEditor.BalancesSeleccionados;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
end;

function TBalancesController.Eliminar(ABalance: IBizBalance): Boolean;
begin
  if not Assigned(ABalance) then
    raise Exception.Create ('Balance no asignado');

  ShowHourglassCursor;
  try
    if (ABalance.State in dsEditModes) then
      ABalance.Cancel;

    ABalance.Delete;
    ABalance.DataTable.ApplyUpdates;
    HideHourglassCursor;
    Result := True;
  finally
    HideHourglassCursor;
  end;
end;

function TBalancesController.ExtraerSeleccionados(ABalances: IBizBalance): IBizBalance;
var
  ASeleccionados : IBizBalance;
begin
  ASeleccionados := Self.Buscar(ID_NULO);
  CopyDataTableDA5(ABalances.DataTable, ASeleccionados.DataTable, True);
  Result := ASeleccionados;
end;

procedure TBalancesController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable);
begin
  inherited;
//
end;

function TBalancesController.Guardar(ABalance: IBizBalance): Boolean;
begin
  Result := False;

  if ValidarBalance(ABalance) then
  begin
    ShowHourglassCursor;
    try
      ABalance.DataTable.ApplyUpdates;
      Result := True;
    finally
      HideHourglassCursor;
    end;
  end;
end;

function TBalancesController.Localizar(ABalances: IBizBalance; ADescripcion: String): Boolean;
begin
  Result := True;
  ShowHourglassCursor;
  try
    with ABalances.DataTable do
    begin
      DisableControls;
      First;
      if not Locate(fld_BalancesREF_BALANCE, ADescripcion, []) then
         Result := False;
      EnableControls;   
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
