unit InformeContratoCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, InformeBase, FR_IBXDB, FR_Shape, FR_DSet, FR_DBSet, FR_Class,
  DB, IBCustomDataSet, IBQuery, RdxEmpresaActiva, Entidades, FR_Rich, TablaDocumentos;

type
  TdmInformeContratoCliente = class(TdmInformeBase)
    TablaCab: TfrDBDataSet;
    TablaDet: TfrDBDataSet;
    TablaContrato: TIBQuery;
    TablaDetallesContrato: TIBQuery;
    TablaPropiedadesContrato: TIBQuery;
    TablaProp: TfrDBDataSet;
    dsDetallesContrato: TDataSource;
    dsPropiedadesContrato: TDataSource;
    dsContrato: TDataSource;
    TablaPrecios1: TIBQuery;
    TablaPrec1: TfrDBDataSet;
    dsPrecios1: TDataSource;
    frRichObject1: TfrRichObject;
  private
    FCodigoContrato : Variant;
    FEntidad: TRdxEntidad;
    FDocumento : TDocumento;
    procedure SetEntidad(const Value: TRdxEntidad);
  protected
    procedure PrepararInforme; override;
    procedure PrepararConsultas; override;
    procedure RellenarBanda(Band: TfrBand); override;    
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property CodigoContrato : variant read FCodigoContrato write FCodigoContrato;
    property Entidad : TRdxEntidad read FEntidad write SetEntidad;
  end;

var
  dmInformeContratoCliente: TdmInformeContratoCliente;

implementation

{$R *.dfm}

uses
  StrFunc, Constantes, ComCtrls;

{ TdmInformeContratoCliente }

constructor TdmInformeContratoCliente.Create(AOwner: TComponent);
begin
  inherited;
  FDocumento := TDocumento.Create('MCOCINA');
end;

destructor TdmInformeContratoCliente.Destroy;
begin
  FDocumento.Free;
  FDocumento := NIL;
  inherited;
end;

procedure TdmInformeContratoCliente.PrepararConsultas;
begin
  inherited;

  with TablaContrato do
  begin
    Database := FBaseDatos;
    Transaction := FTransaccion;
    SQL.Clear;
    SQL.Add('select * from CONTRATOSCLIENTE ');
    SQL.Add('where CODIGO = :CODIGO');
    ParamByName('CODIGO').AsString := FCodigoContrato;
    Prepare;
    Open;
  end;

  with TablaDetallesContrato do
  begin
    Database := FBaseDatos;
    Transaction := FTransaccion;
    SQL.Clear;
    SQL.Add('select * from DETALLESCONTRATOSARTICULOS ');
    SQL.Add('where CODIGOCONTRATO = :CODIGO ');

    case Entidad of
      entContratoCocina,
      entContratoBano      : SQL.Add('and NUMCONCEPTO = 0');

      entContratoArmarios  : SQL.Add('and NUMCONCEPTO < 2');
    end;
    Params.ParseSQL(SQL.Text, True);
    Prepare;
    Open;
  end;

  with TablaPropiedadesContrato do
  begin
    Database := FBaseDatos;
    Transaction := FTransaccion;
    SQL.Clear;
    SQL.Add('select CODIGOCONTRATO, NUMCONCEPTO, NUMPROPIEDAD, ');
    SQL.Add('PROP.DESCRIPCION as DESCRIPCION, CODIGOPROPIEDAD, VALOR ');
    SQL.Add('from DETALLESCONTRATOSPROPIEDADES, PROPIEDADES PROP ');
    SQL.Add('where CODIGOCONTRATO = :CODIGOCONTRATO and ');
    SQL.Add('NUMCONCEPTO = :NUMCONCEPTO and ');
    SQL.Add('PROP.CODIGO = CODIGOPROPIEDAD ');
    SQL.Add('order by NUMPROPIEDAD');
    Params.ParseSQL(SQL.Text, True);
    Prepare;
    Open;
  end;

  TablaCab.Open;
  TablaDet.Open;
  TablaProp.Open;

  with TablaPrecios1 do
  begin
    Database := FBaseDatos;
    Transaction := FTransaccion;
    SQL.Clear;
    SQL.Add('select * ');
    SQL.Add('from DETALLESCONTRATOSARTICULOS ');
    SQL.Add('where CODIGOCONTRATO = :CODIGO and ');
    SQL.Add('TIPOCONCEPTO = ''PRECIO'' ');
    SQL.Add('order by NUMCONCEPTO');
    Params.ParseSQL(SQL.Text, True);
    Prepare;
  end;
  TablaPrec1.Open;
end;

procedure TdmInformeContratoCliente.PrepararInforme;
begin
  inherited;

  if Entidad = entContratoCocina then
  begin
    if (EsCadenaVacia(FDocumento.ContratoVenta)) then
      FReport.Pages.Pages[1].Visible := False;

    if (EsCadenaVacia(FDocumento.Recomendaciones)) then
      FReport.Pages.Pages[2].Visible := False;
  end;
end;

procedure TdmInformeContratoCliente.RellenarBanda(Band: TfrBand);
var
  iCont : Integer;
  Objeto : TfrView;
  CadenaAux : String;
begin
  with Band do
  begin
     for iCont := 0 to Objects.Count - 1 do
      begin
        Objeto := Objects[iCont];
        if ((Objeto is TfrMemoView) and (Pos('Conforme', Objeto.Name) > 0)) then
        begin
          with (Objeto as TfrMemoView), EmpresaActiva do
          begin
            Memo.Clear;
            Memo.Add('CONFORME ' + NombreComercial);
          end;
        end;

        if ((Objeto is TfrRichView) and (Pos('rtfContrato', Objeto.Name) > 0)) then
        begin
          with (Objeto as TfrRichView).RichEdit do
          begin
            Clear;
            SelStart := GetTextLen;
            SelText  := FDocumento.ContratoVenta + #13#10;
          end;
        end;

        if ((Objeto is TfrRichView) and (Pos('rtfRecomendaciones', Objeto.Name) > 0)) then
        begin
          with (Objeto as TfrRichView).RichEdit do
          begin
            Clear;
            SelStart := GetTextLen;
            SelText  := FDocumento.Recomendaciones + #13#10;
          end;
        end;

      end;
  end;
end;

procedure TdmInformeContratoCliente.SetEntidad(
  const Value: TRdxEntidad);
begin
  if FEntidad = Value then
    Exit;
  FEntidad := Value;
  case FEntidad of
    entContratoCocina  : FNombreInforme := 'ContratoCocina.frf';
    entContratoBano    : FNombreInforme := 'ContratoBano.frf';
    entContratoArmarios : FNombreInforme := 'ContratoArmarios.frf';
  else
    FNombreInforme := 'ContratoCliente.frf';
  end;
end;

end.
