unit uFormasPagoController;

interface


uses
   Classes, SysUtils, uDADataTable, uControllerBase, 
   uBizFormasPago, uFormasPagoPlazosController, uIDataModuleFormasPago;
   
type
  IFormasPagoController = interface(IControllerBase)
  ['{94E5F2B6-64C8-4331-B9CB-3ED730478529}']
    function BuscarTodos: IBizFormaPago;
    function Buscar(ID: Integer): IBizFormaPago;
    function BuscarID(ADescripcion: String): Integer;
    
    procedure VerTodos(AFormasPago: IBizFormaPago);
    procedure Ver(AFormaPago: IBizFormaPago);
    procedure Anadir(AFormaPago : IBizFormaPago);
    function Eliminar(AFormaPago : IBizFormaPago): Boolean; 
    function Guardar(AFormaPago : IBizFormaPago): Boolean;
    procedure DescartarCambios(AFormaPago : IBizFormaPago);
    function Localizar(AFormasPago: IBizFormaPago; ADescripcion:String): Boolean;
    function DarListaFormasPago: TStringList;
  end;

  TFormasPagoController = class(TControllerBase, IFormasPagoController)
  protected
    FDataModule : IDataModuleFormasPago;
    FPlazosController : IFormasPagoPlazosController;

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

    function ValidarFormaPago(AFormaPago: IBizFormaPago): Boolean;
    procedure AsignarDataModule;

  public
    constructor Create; override;
    destructor Destroy; override;

    function Eliminar(AFormaPago : IBizFormaPago): Boolean;
    function Guardar(AFormaPago : IBizFormaPago): Boolean; virtual;
    procedure DescartarCambios(AFormaPago : IBizFormaPago); virtual;
    procedure Anadir(AFormaPago : IBizFormaPago);
    function BuscarTodos: IBizFormaPago;
    function Buscar(ID: Integer): IBizFormaPago;
    function BuscarID(ADescripcion: String): Integer;
    procedure VerTodos(AFormasPago: IBizFormaPago);
    procedure Ver(AFormaPago: IBizFormaPago);
    function Localizar(AFormasPago: IBizFormaPago; ADescripcion:String): Boolean;
    function DarListaFormasPago: TStringList;
  end;

implementation

uses
  cxControls, DB, uEditorRegistryUtils, schFormasPagoClient_Intf,
  uIEditorFormasPago, uIEditorFormaPago, uDataModuleFormasPago,
  uDAInterfaces, uDataTableUtils, uDialogUtils,
  uDateUtils, uROTypes, DateUtils, Controls, Windows;

{ TFormasPagoController }

procedure TFormasPagoController.Anadir(AFormaPago: IBizFormaPago);
begin
  AFormaPago.Insert;
end;

procedure TFormasPagoController.AsignarDataModule;
begin
  FDataModule := TDataModuleFormasPago.Create(Nil);
end;

function TFormasPagoController.BuscarID(ADescripcion: String): Integer;
var
	AFormaPago : IBizFormaPago;
begin
  Result := -1;
  ShowHourglassCursor;
  AFormaPago := BuscarTodos;
  try
    AFormaPago.Open;
    if Localizar(AFormaPago, ADescripcion) then
      Result := AFormaPago.ID;
  finally
		AFormaPago := NIL;
    HideHourglassCursor;
  end;
end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TFormasPagoController.BuscarTodos: IBizFormaPago;
begin
  Result := FDataModule.GetItems;
end;

constructor TFormasPagoController.Create;
begin
  inherited;
  AsignarDataModule;
  FPlazosController := TFormasPagoPlazosController.Create;  
end;

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

function TFormasPagoController.DarListaFormasPago: TStringList;
var
  AFormasPago: IBizFormaPago;
begin
  AFormasPago := BuscarTodos;
  AFormasPago.DataTable.Active := True;
  Result := TStringList.Create;
  try
    with Result do
    begin
      AFormasPago.DataTable.First;
      while not AFormasPago.DataTable.EOF do
      begin
        Add(AFormasPago.DESCRIPCION);
        AFormasPago.DataTable.Next;
      end;
    end;
  finally
    AFormasPago := NIL;
  end;
end;

procedure TFormasPagoController.DescartarCambios(AFormaPago: IBizFormaPago);
begin
  if not Assigned(AFormaPago) then
    raise Exception.Create ('Forma de pago no asignada');

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

    AFormaPago.DataTable.CancelUpdates;
  finally
    HideHourglassCursor;
  end;
end;

destructor TFormasPagoController.Destroy;
begin
  FDataModule:= NIL;
  FPlazosController := NIL;
  inherited;
end;

function TFormasPagoController.ValidarFormaPago(AFormaPago: IBizFormaPago): Boolean;
begin
  Result := False;
  
  if not Assigned(AFormaPago) then
    raise Exception.Create ('Forma de pago no asignada');

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

  if Length(AFormaPago.REFERENCIA) = 0 then
    raise Exception.Create('Debe indicar una referencia para esta forma de pago.');

  if Length(AFormaPago.DESCRIPCION) = 0 then
    raise Exception.Create('Debe indicar una descripción para esta forma de pago.');

  Result := True;
end;

procedure TFormasPagoController.Ver(AFormaPago: IBizFormaPago);
var
  AEditor : IEditorFormaPago;
begin
  AEditor := NIL;

  CreateEditor('EditorFormaPago', IEditorFormaPago, AEditor);
  if Assigned(AEditor) then
  begin
    try
      AEditor.FormaPago := AFormaPago;
      AEditor.ShowModal;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
  end;
end;

procedure TFormasPagoController.VerTodos(AFormasPago: IBizFormaPago);
var
  AEditor : IEditorFormasPago;
begin
  AEditor := NIL;

  CreateEditor('EditorFormasPago', IEditorFormasPago, AEditor);
  if Assigned(AEditor) then
    try
      AEditor.FormasPago := AFormasPago;
      AEditor.ShowModal;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
end;

function TFormasPagoController.Eliminar(AFormaPago: IBizFormaPago): Boolean;
begin
  if not Assigned(AFormaPago) then
    raise Exception.Create ('Forma de pago no asignada');

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

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

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

function TFormasPagoController.Guardar(AFormaPago: IBizFormaPago): Boolean;
begin
  Result := False;

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

function TFormasPagoController.Localizar(AFormasPago: IBizFormaPago; ADescripcion: String): Boolean;
begin
  Result := True;
  ShowHourglassCursor;
  try
    with AFormasPago.DataTable do
    begin
      if not Active then
        Open;
        
      DisableControls;
      try
        First;
        if not Locate(fld_FormasPagoDESCRIPCION, ADescripcion, []) then
           Result := False;
      finally
        EnableControls;
      end;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
