unit uMedicacionReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleMedicacionesReport,
  uPacientesController, uMedicacionesController, uDetallesMedicacionController,
  uBizMedicaciones, uIntegerListUtils;

type
  IMedicacionesReportController = interface(IControllerBase)
  ['{133C0962-ED71-4300-B396-484896720EB0}']
    procedure Preview(const AListaID : TIntegerList);
    procedure Print(const AListaID : TIntegerList);
  end;

  TMedicacionesReportController = class(TControllerBase, IMedicacionesReportController)
  private
    FDataModule : IDataModuleMedicacionesReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Preview(const AListaID : TIntegerList);
    procedure Print(const AListaID : TIntegerList);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorMedicacionesPreview,
  uEditorPreview, uDataModuleMedicaciones, uEditorBase, cxControls,
  uStringsUtils, uSistemaFunc, uFactuGES_App;

{ TMedicacionesReportController }

constructor TMedicacionesReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleMedicacion.Create(Nil);
end;

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


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

procedure TMedicacionesReportController.Preview(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorMedicacionesPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptMedicaciones(AListaID);
    try
      CreateEditor('EditorMedicacionesPreview', IEditorMedicacionesPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Title := 'Medicación - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TMedicacionesReportController.Print(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorMedicacionesPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptMedicaciones(AListaID);
    try
      CreateEditor('EditorMedicacionesPreview', IEditorMedicacionesPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Title := 'Medicación - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
