unit uBizArticulos;

interface

uses
  uDAInterfaces, uDADataTable, schArticulosClient_Intf, uDBSelectionListUtils;

const
  BIZ_CLIENT_ARTICULO = 'Client.Articulo';

type
  IBizArticulo = interface(IArticulos)
  ['{06AB61B1-C225-4791-9F5E-00A60DBA7FFD}']
  	function EsNuevo : Boolean;
    function HayImagen: Boolean;
    procedure SalvarImagen(const AFileName: String);
    procedure CargarImagen(const AFileName: String);
//    procedure QuitarImagen;
  end;

  TBizArticulo = class(TArticulosDataTableRules, IBizArticulo, ISeleccionable)
  protected
    FSeleccionableInterface : ISeleccionable;
    procedure OnNewRecord(Sender: TDADataTable); override;
  public
    procedure IniciarValoresArticuloNuevo; virtual;
    function HayImagen: Boolean;
    procedure SalvarImagen(const AFileName: String);
    procedure CargarImagen(const AFileName: String);
//    procedure QuitarImagen;
    function EsNuevo : Boolean;
    constructor Create(aDataTable: TDADataTable); override;
    destructor Destroy; override;
    property SeleccionableInterface : ISeleccionable read FSeleccionableInterface
        write FSeleccionableInterface implements ISeleccionable;
  end;

implementation

uses
  uFactuGES_App, uDataTableUtils, SysUtils, uROClasses, Classes;

{ TBizArticulo }

procedure TBizArticulo.CargarImagen(const AFileName: String);
begin
  inherited;
  DataTable.Edit;
  DataTable.FieldByName(fld_ArticulosIMAGEN).LoadFromFile(AFileName);
  DataTable.Post;
end;

constructor TBizArticulo.Create(aDataTable: TDADataTable);
begin
  inherited;
  FSeleccionableInterface := TSeleccionable.Create(aDataTable);
end;

destructor TBizArticulo.Destroy;
begin
  FSeleccionableInterface := NIL;
  inherited;
end;

function TBizArticulo.EsNuevo: Boolean;
begin
  Result := (ID < 0);
end;

function TBizArticulo.HayImagen: Boolean;
var
  AStream : TMemoryStream;
  AROStream : IROStream;
begin
  AStream := TMemoryStream.Create;
  AROStream := TROStream.Create(AStream, False);
  try
    DataTable.FieldByName(fld_ArticulosIMAGEN).SaveToStream(AROStream);
    Result := (AROStream.Size > 0);
  finally
    AROStream := NIL;
    FreeAndNIL(AStream);
  end;
end;

procedure TBizArticulo.IniciarValoresArticuloNuevo;
begin
  ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
  PRECIO_PORTE := 0;
  PRECIO_COSTE := 0;
  DESCUENTO := 0;
  PRECIO_NETO := 0;
  COMISIONABLE := 1;
  ELIMINADO := 0;
  INVENTARIABLE := 1;  
  USUARIO := AppFactuGES.UsuarioActivo.UserName;
end;

procedure TBizArticulo.OnNewRecord(Sender: TDADataTable);
begin
  inherited;
  ID := GetRecNo; // -1, -2, -3...
  IniciarValoresArticuloNuevo;
end;

{procedure TBizArticulo.QuitarImagen;
begin
//  DataTable.Edit;
//  DataTable.Fields[idx_ArticulosIMAGEN].Clear;
//  DataTable.Post;
end;}

procedure TBizArticulo.SalvarImagen(const AFileName: String);
begin
  DataTable.FieldByName(fld_ArticulosIMAGEN).SaveToFile(AFileName);
end;

initialization
  RegisterDataTableRules(BIZ_CLIENT_ARTICULO, TBizArticulo);

finalization

end.

