unit uBizComisiones;

interface

uses
  uDAInterfaces, uDADataTable, schComisionesClient_Intf, uDBSelectionListUtils, Classes;
//  uBizContactos, uBizDetallesFacturaCliente

const
  BIZ_CLIENT_COMISIONES = 'Client.Comisiones';
  BIZ_CLIENT_FACTURASCOMISION = 'Client.FacturasComision';

type
  IBizFacturasComision = interface(IFacturasComision)
  ['{341B5CD1-2BCD-43F5-8F0C-AF72C1780512}']
  end;

  IBizComision = interface(IComisiones)
  ['{78E17CC4-F4A6-489D-99C3-2D56F8730A33}']

    function GetFacturasComision: IBizFacturasComision;
    procedure SetFacturasComision(Value: IBizFacturasComision);
    property FacturasComision: IBizFacturasComision read GetFacturasComision write SetFacturasComision;

		function EsNuevo : Boolean;
  end;

  TBizFacturasComision = class(TFacturasComisionDataTableRules, IBizFacturasComision)
  protected
    procedure CalcularComision;
    procedure COMISIONOnChange(Sender: TDACustomField);
    procedure BeforeInsert(Sender: TDADataTable); override;    

  public
    constructor Create(aDataTable: TDADataTable); override;
    destructor Destroy; override;
  end;

  TBizComision = class(TComisionesDataTableRules, IBizComision, ISeleccionable)
  private
    FSeleccionableInterface : ISeleccionable;

  protected
    FFacturasComision : IBizFacturasComision;
    FFacturasComisionLink : TDADataSource;

    function GetFacturasComision: IBizFacturasComision;
    procedure SetFacturasComision(Value: IBizFacturasComision);

    procedure OnNewRecord(Sender: TDADataTable); override;

  public
		function EsNuevo : Boolean;
    procedure IniciarValoresComisionNueva;

    property FacturasComision: IBizFacturasComision read GetFacturasComision write SetFacturasComision;
    property SeleccionableInterface : ISeleccionable read FSeleccionableInterface write FSeleccionableInterface implements ISeleccionable;

    constructor Create(aDataTable: TDADataTable); override;
    destructor Destroy; override;
  end;

implementation

uses
  SysUtils, uDataTableUtils, DB, uDateUtils, DateUtils, uDataModuleUsuarios,
  Variants, cxDateUtils, uFactuGES_App;

{ TBizComision }
constructor TBizComision.Create(aDataTable: TDADataTable);
begin
  inherited;

  FFacturasComisionLink := TDADataSource.Create(NIL);
  FFacturasComisionLink.DataTable := aDataTable;

  FSeleccionableInterface := TSeleccionable.Create(aDataTable);
end;

destructor TBizComision.Destroy;
begin
  FFacturasComision := NIL;
  FFacturasComisionLink.Free;
  FSeleccionableInterface := NIL;
  inherited;
end;

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

function TBizComision.GetFacturasComision: IBizFacturasComision;
begin
  Result := FFacturasComision;
end;

procedure TBizComision.IniciarValoresComisionNueva;
begin
  ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
  USUARIO := AppFactuGES.UsuarioActivo.UserName;
  FECHA := DateOf(Now);
end;

procedure TBizComision.OnNewRecord(Sender: TDADataTable);
begin
  inherited;
  IniciarValoresComisionNueva;
end;

procedure TBizComision.SetFacturasComision(Value: IBizFacturasComision);
begin
  FFacturasComision := Value;
  EnlazarMaestroDetalle(FFacturasComisionLink, FFacturasComision);
end;

{ TBizFacturasComision }

procedure TBizFacturasComision.BeforeInsert(Sender: TDADataTable);
var
  AMasterTable : TDADataTable;
begin
  inherited;
  AMasterTable := DataTable.GetMasterDataTable;
  if Assigned(AMasterTable) and (AMasterTable.State = dsInsert) then
  begin
    AMasterTable.Post;
  end;
end;

procedure TBizFacturasComision.CalcularComision;
begin
  if not Self.DataTable.Editing then
    Edit;
  IMPORTE_COMISION := (COMISION / 100) * BASE_IMPONIBLE;
end;

procedure TBizFacturasComision.COMISIONOnChange(Sender: TDACustomField);
begin
  CalcularComision;
end;

constructor TBizFacturasComision.Create(aDataTable: TDADataTable);
begin
  inherited;

  with DataTable do
  begin
    FieldByName(fld_FacturasComisionCOMISION).OnChange := COMISIONOnChange;
  end;
end;

destructor TBizFacturasComision.Destroy;
begin
  with DataTable do
  begin
    FieldByName(fld_FacturasComisionCOMISION).OnChange := NIL;
  end;

  inherited;
end;

initialization
  RegisterDataTableRules(BIZ_CLIENT_COMISIONES, TBizComision);
  RegisterDataTableRules(BIZ_CLIENT_FACTURASCOMISION, TBizFacturasComision);  

finalization

end.

