unit uContratosClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleContratosClienteReport,
  uClientesController, uContratosClienteController, uDetallesContratoClienteController,
  uBizContratosCliente, uIntegerListUtils, uDataModuleContratosCliente;

type
  IContratosClienteReportController = interface(IControllerBase)
  ['{FEF47B69-26A3-462A-AF6D-4B2073D4F9DC}']
    procedure Preview(const AListaID : TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = CTE_TamLetraIni);
    procedure Print(const AListaID : TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = CTE_TamLetraIni);
    function ExportToPDF(const AID: Integer; const AFileName : String = ''; const AFirmado: Boolean = False): Boolean;
    function getDataModule: IDataModuleContratosClienteReport;
    property DataModule: IDataModuleContratosClienteReport read getDataModule;
  end;

  TContratosClienteReportController = class(TControllerBase, IContratosClienteReportController)
  private
    FDataModule : IDataModuleContratosClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
    function getDataModule: IDataModuleContratosClienteReport;
  public
    property DataModule: IDataModuleContratosClienteReport read getDataModule;

    constructor Create; override;
    destructor Destroy; override;

    procedure Preview(const AListaID : TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = CTE_TamLetraIni);
    procedure Print(const AListaID : TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = CTE_TamLetraIni);
    function ExportToPDF(const AID: Integer; const AFileName : String = ''; const AFirmado: Boolean = False): Boolean;
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorContratosClientePreview,
  uEditorPreview, uEditorBase,
  cxControls, uFactuGES_App, uStringsUtils, uSistemaFunc;

{ TContratosClienteReportController }

constructor TContratosClienteReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleContratosCliente.Create(Nil);
end;

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


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

function TContratosClienteReportController.ExportToPDF(const AID: Integer; const AFileName: String; const AFirmado: Boolean): Boolean;
var
  AStream: Binary;
begin
  Result := False;
  if EsCadenaVacia(AFileName) then
    Exit;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptPDFContrato(AID, false, AFirmado);
    try
      AStream.SaveToFile(AFileName);
      Result := True;
    finally
      FreeAndNil(AStream);
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TContratosClienteReportController.getDataModule: IDataModuleContratosClienteReport;
begin
  Result := FDataModule;
end;

procedure TContratosClienteReportController.Preview(const AListaID : TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = CTE_TamLetraIni);
var
  AStream: Binary;
  AEditor : IEditorContratosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptContratos(AListaID, AVerCondiciones, TamLetra);
    try
      CreateEditor('EditorContratosClientePreview', IEditorContratosClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Vercondiciones := AVerCondiciones;
          AEditor.TamLetra := TamLetra;
          AEditor.Title := 'Presupuesto - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TContratosClienteReportController.Print(const AListaID : TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = CTE_TamLetraIni);
var
  AStream: Binary;
  AEditor : IEditorContratosClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptContratos(AListaID, AVerCondiciones, TamLetra);
    try
      CreateEditor('EditorContratosClientePreview', IEditorContratosClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Vercondiciones := AVerCondiciones;
          AEditor.TamLetra := TamLetra;
          AEditor.Title := 'Presupuesto - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
