unit uEditorCobroCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponent, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvNavigationPane,
  uCustomView, uViewBase, StdCtrls, pngimage,
  AppEvnts, JvComponentBase,dxLayoutLookAndFeels,

  uBizCobrosCliente, uIEditorCobroCliente, uCobrosClienteController,
  uViewCobroCliente, JvExComCtrls, JvStatusBar;


type
  TfEditorCobroCliente = class(TfEditorDBItem, IEditorCobroCliente)
    TBXSubmenuItem2: TTBXSubmenuItem;
    TBXItem33: TTBXItem;
    dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    dxLayoutOfficeLookAndFeel1: TdxLayoutOfficeLookAndFeel;
    procedure FormShow(Sender: TObject);
    procedure frViewClienteCobro1edtlNombrePropertiesEditValueChanged(
      Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
    procedure frViewClienteCobroedtlNombrePropertiesChange(Sender: TObject);

  protected
    FController : ICobrosClienteController;
    FCobro: IBizCobroCliente;
    FViewCobro: IViewCobroCliente;

    function GetController : ICobrosClienteController;
    procedure SetController (const Value : ICobrosClienteController); virtual;
    function GetCobro: IBizCobroCliente;
    procedure SetCobro(const Value: IBizCobroCliente); virtual;

    function GetViewCobro: IViewCobroCliente;
    procedure SetViewCobro(const Value: IViewCobroCliente);
    procedure GuardarInterno; override;
    procedure EliminarInterno; override;
    procedure PrevisualizarInterno; override;
    procedure ImprimirInterno; override;
    property ViewCobroCliente: IViewCobroCliente read GetViewCobro write SetViewCobro;

    procedure OnContactoChanged(Sender : TObject);

    //Si queremos crear otra vista para el editor heredado solo tendriamos que
    //sobreescribir este metodo
    procedure AsignarVista; virtual;

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;
    property Controller : ICobrosClienteController read GetController write SetController;
    property Cobro: IBizCobroCliente read GetCobro write SetCobro;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation
{$R *.dfm}

uses
  cxDBEdit, uBizContactos, uDataModuleUsuarios;


{ TfEditorCobroCliente }

{
**************************** TfEditorCobroCliente ****************************
}
constructor TfEditorCobroCliente.Create(AOwner: TComponent);
begin
  inherited;
  pgPaginas.ActivePageIndex := 0;
  AsignarVista;
end;

procedure TfEditorCobroCliente.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsDataTable.DataTable := NIL;

  FViewCobro := NIL;
  FCobro := NIL;
end;

destructor TfEditorCobroCliente.Destroy;
begin
  // Utilizar mejor OnClose;

  inherited;
end;

procedure TfEditorCobroCliente.dsDataTableDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if Assigned(FCobro) and (not (FCobro.DataTable.Fetching) or
    not (FCobro.DataTable.Opening) or not (FCobro.DataTable.Closing)) then
    PonerTitulos;
end;

procedure TfEditorCobroCliente.EliminarInterno;
begin
  inherited;
  if (Application.MessageBox('¿Desea borrar este cobro de cliente?', 'Atención', MB_YESNO) = IDYES) then
  begin
    FController.Eliminar(Cobro);
    inherited;
  end;
end;

procedure TfEditorCobroCliente.AsignarVista;
var
  AViewCobroCliente: TfrViewCobroCliente;

begin
  AViewCobroCliente := TfrViewCobroCliente.Create(Self);
  with AViewCobroCliente do
  begin
    Parent := pagGeneral;
    Align := alClient;
    dxLayoutCobroCliente.LookAndFeel := dxLayoutOfficeLookAndFeel1;
  end;
  ViewCobroCliente := AViewCobroCliente;
end;

procedure TfEditorCobroCliente.FormShow(Sender: TObject);
begin
  inherited;

  if not Assigned(FViewCobro) then
    raise Exception.Create('No hay ninguna vista asignada');

  if not Assigned(Cobro) then
    raise Exception.Create('No hay ningún cobro asignado');

  Cobro.DataTable.Active := True;
end;

procedure TfEditorCobroCliente.frViewClienteCobro1edtlNombrePropertiesEditValueChanged(Sender: TObject);
begin
  inherited;
  with (Sender as TcxDBTextEdit) do
    Enabled := (FCobro.ID <> 0)
end;

procedure TfEditorCobroCliente.frViewClienteCobroedtlNombrePropertiesChange(
  Sender: TObject);
begin
  inherited;
  PonerTitulos;
end;

function TfEditorCobroCliente.GetController: ICobrosClienteController;
begin
  Result := FController;
end;

function TfEditorCobroCliente.GetCobro: IBizCobroCliente;
begin
  Result := FCobro;
end;

function TfEditorCobroCliente.GetViewCobro: IViewCobroCliente;
begin
  Result := FViewCobro;
end;

procedure TfEditorCobroCliente.GuardarInterno;
begin
  inherited;
  FController.Guardar(FCobro);
  Modified := False;
end;

procedure TfEditorCobroCliente.ImprimirInterno;
begin
  inherited;
  FController.Print(FCobro);
end;

procedure TfEditorCobroCliente.OnContactoChanged(Sender: TObject);
begin
  FCobro.Cliente := ViewCobroCliente.ViewClienteCobro.Cliente;
end;

procedure TfEditorCobroCliente.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  FTitulo := ATitulo;
  if (FTitulo = '') and Assigned(FCobro) then
  begin
    if FCobro.EsNuevo then
      FTitulo := 'Nuevo cobro'
    else
      FTitulo := 'Cobro'  + ' - ' + FCobro.Cliente.Nombre
  end;

  inherited PonerTitulos(FTitulo);

  Self.Caption := FTitulo + ' (' + dmUsuarios.EmpresaActual.NOMBRE + ')';
end;

procedure TfEditorCobroCliente.PrevisualizarInterno;
begin
  inherited;
  FController.Preview(FCobro);
end;

procedure TfEditorCobroCliente.SetController(const Value: ICobrosClienteController);
begin
  FController := Value;

  if Assigned(FController) then
  begin
     ViewCobroCliente.ViewClienteCobro.Controller := Controller.ClienteController;
  end;
end;

procedure TfEditorCobroCliente.SetCobro(const Value: IBizCobroCliente);
begin
  FCobro := Value;

  if Assigned(FCobro) then
  begin
    dsDataTable.DataTable := FCobro.DataTable;

    if Assigned(FViewCobro) then
    begin
      ViewCobroCliente.ViewClienteCobro.Cliente := (Controller.ClienteController.Buscar(Cobro.ID_CLIENTE) as IBizCliente);
      ViewCobroCliente.ViewClienteCobro.OnClienteChanged := OnContactoChanged;
      FViewCobro.Cobro := FCobro;
    end;

    Cobro.DataTable.Active := True;
  end;
end;

procedure TfEditorCobroCliente.SetViewCobro(const Value: IViewCobroCliente);
begin
  FViewCobro := Value;

  if Assigned(FViewCobro) and Assigned(Cobro) then
    FViewCobro.Cobro := Cobro;
end;

end.
