{
===============================================================================
  Copyright (©) 2007. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 
  Versión actual: 1.0.0
  Fecha versión actual: 
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit uOscManufacturersController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase,

  uBizOscManufacturers, uIDataModuleTiendaWeb;

type
  IOscManufacturersController = interface(IObservador)
  ['{62BF402A-82BD-4C51-B373-B8DDE20E43F1}']
    function Nuevo : IBizOscManufacturer;
    procedure Anadir(AOscManufacturers : IBizOscManufacturer);
    function Eliminar(AOscManufacturers : IBizOscManufacturer): Boolean; overload;
    function Guardar(AOscManufacturers : IBizOscManufacturer): Boolean;
    procedure DescartarCambios(AOscManufacturers : IBizOscManufacturer);
		function Duplicar(AOscManufacturers: IBizOscManufacturer): IBizOscManufacturer;

	  function Buscar(const IDLocal: Integer): IBizOscManufacturer;
	  function BuscarTodos: IBizOscManufacturer;
    procedure VerTodos(AOscManufacturers: IBizOscManufacturer);
    procedure Ver(AOscManufacturers: IBizOscManufacturer);
    function Localizar(AOscManufacturers: IBizOscManufacturer; AIDLocal: Integer): Boolean;
    function DarListaOscManufacturers: TStringList;
    function ExtraerSeleccionados(AOscManufacturers: IBizOscManufacturer) : IBizOscManufacturer;
    function ElegirOscManufacturers(AOscManufacturers: IBizOscManufacturer; AMensaje: String; AMultiSelect: Boolean): IBizOscManufacturer;

    // Descomentar esto si hay informe
    // procedure Preview(AOscManufacturers : IBizOscManufacturer);
    // procedure Print(AOscManufacturers : IBizOscManufacturer);   
  end;

  TOscManufacturersController = class(TObservador, IOscManufacturersController)
  protected
    FDataModule : IDataModuleTiendaWeb;

    procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
	function _Vacio : IBizOscManufacturer;
	
    function ValidarOscManufacturers(AOscManufacturers: IBizOscManufacturer): Boolean;
    procedure AsignarDataModule;
    procedure AsignarID(AOscManufacturers: IBizOscManufacturer; const IDNuevo: Integer);
  public
    constructor Create; virtual;
    destructor Destroy; override;

    function Nuevo : IBizOscManufacturer;
    procedure Anadir(AOscManufacturers : IBizOscManufacturer);
    function Eliminar(AOscManufacturers : IBizOscManufacturer): Boolean; overload;
    function Guardar(AOscManufacturers : IBizOscManufacturer): Boolean;
    procedure DescartarCambios(AOscManufacturers : IBizOscManufacturer);
    function Duplicar(AOscManufacturers: IBizOscManufacturer): IBizOscManufacturer;

	  function Buscar(const IDLocal: Integer): IBizOscManufacturer;
    function BuscarTodos: IBizOscManufacturer;
    procedure VerTodos(AOscManufacturers: IBizOscManufacturer);
    procedure Ver(AOscManufacturers: IBizOscManufacturer);
    function Localizar(AOscManufacturers: IBizOscManufacturer; AIDLocal: Integer): Boolean;
    function DarListaOscManufacturers: TStringList;
    function ExtraerSeleccionados(AOscManufacturers: IBizOscManufacturer) : IBizOscManufacturer;
    function ElegirOscManufacturers(AOscManufacturers: IBizOscManufacturer; AMensaje: String; AMultiSelect: Boolean): IBizOscManufacturer;

    // Descomentar esto si hay informe
    // procedure Preview(AOscManufacturers : IBizOscManufacturer);
    // procedure Print(AOscManufacturers : IBizOscManufacturer);   
  end;

implementation

uses
  cxControls, DB, uEditorRegistryUtils, 
  uDAInterfaces, uDataTableUtils, 
  uDateUtils, uROTypes, DateUtils, Controls, Windows,
  
  // Descomentar esto si hay informe
  // uOscManufacturersReportController,
  
  schTiendaWebClient_Intf, uDataModuleTiendaWeb;

{ TOscManufacturersController }

procedure TOscManufacturersController.Anadir(AOscManufacturers: IBizOscManufacturer);
begin
  AOscManufacturers.Insert;
end;

procedure TOscManufacturersController.AsignarDataModule;
begin
  FDataModule := TDataModuleTiendaWeb.Create(Nil);
end;

procedure TOscManufacturersController.AsignarID(AOscManufacturers: IBizOscManufacturer;
  const IDNuevo: Integer);
var
  AContador : Integer;
begin
  if not Assigned(AOscManufacturers) then
    raise Exception.Create ('OscManufacturers no asignado');

  // { Los detalles hay que comprobarlos siempre
  //   tanto en inserción como en modificación. }
  {if Assigned(AOscManufacturers.Detalles) then
    FDetallesController.AsignarID(AOscManufacturers.Detalles, IDNuevo, AOscManufacturers.EsNuevo);}

  if AOscManufacturers.EsNuevo then
  begin
    AOscManufacturers.Edit;
    AOscManufacturers.manufacturers_id := IDNuevo;
    AOscManufacturers.Post;
  end;
end;

function TOscManufacturersController.BuscarTodos: IBizOscManufacturer;
begin
  Result := FDataModule.GetOSCManufacturers;
end;

constructor TOscManufacturersController.Create;
begin
  AsignarDataModule;
end;

function TOscManufacturersController.Buscar(const IDLocal: Integer): IBizOscManufacturer;
begin
  Result := FDataModule.GetOSCManufacturer(IDLocal);
end;

function TOscManufacturersController._Vacio: IBizOscManufacturer;
begin
  Result := Buscar(ID_NULO);
end;

function TOscManufacturersController.DarListaOscManufacturers: TStringList;
{var
  AOscManufacturers: IBizOscManufacturer;}
begin
{  AOscManufacturers := BuscarTodos;
  AOscManufacturers.DataTable.Active := True;
  Result := TStringList.Create;
  try
    with Result do
    begin
      AOscManufacturers.DataTable.First;
      while not AOscManufacturers.DataTable.EOF do
      begin
        Add(AOscManufacturers.DESCRIPCION);
        AOscManufacturers.DataTable.Next;
      end;
    end;
  finally
    AOscManufacturers := NIL;
  end;}
end;

procedure TOscManufacturersController.DescartarCambios(AOscManufacturers: IBizOscManufacturer);
begin
  if not Assigned(AOscManufacturers) then
    raise Exception.Create ('OscManufacturers no asignado');

  ShowHourglassCursor;
  try
    if (AOscManufacturers.State in dsEditModes) then
      AOscManufacturers.Cancel;

    AOscManufacturers.DataTable.CancelUpdates;
  finally
    HideHourglassCursor;
  end;
end;

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


function TOscManufacturersController.Duplicar(AOscManufacturers: IBizOscManufacturer): IBizOscManufacturer;
begin
  Result := Self._Vacio;
  ShowHourglassCursor;
  try
    DuplicarRegistros(AOscManufacturers.DataTable, Result.DataTable, mdrActual);
    
    // Descomentar esto si hay detalles 
    // DuplicarRegistros(AOscManufacturers.Detalles.DataTable, Result.Detalles.DataTable, mdrTodos);

    // ¡CUIDADO! Hay que dejar algunos campos como si fuera todo nuevo
    Result.Edit;
    with Result do
    begin
    	// Ejemplos
      // ID_EMPRESA := dmUsuarios.IDEmpresaActual;
      // USUARIO := dmUsuarios.LoginInfo.Usuario;
      // REFERENCIA := ''; //Para que se asigne una nueva
      // FECHA_FACTURA := DateOf(Now);
      // SITUACION := SITUACION_PENDIENTE;
    end;

    Result.Post;
  finally
    HideHourglassCursor;
  end;
end;


function TOscManufacturersController.ValidarOscManufacturers(AOscManufacturers: IBizOscManufacturer): Boolean;
begin
  Result := False;

  if not Assigned(AOscManufacturers) then
    raise Exception.Create ('OscManufacturers no asignado');

  if (AOscManufacturers.DataTable.State in dsEditModes) then
    AOscManufacturers.DataTable.Post;

	// Descomentar esto si hay detalles 
  // // Tambien hacemos post de sus tablas hija
  // if (AOscManufacturers.Detalles.DataTable.State in dsEditModes) then
      // AOscManufacturers.Detalles.DataTable.Post;


	// Ejemplos de validaciones
	{
  if (AOscManufacturers.ID_CLIENTE < 0) or
     (not Assigned(AOscManufacturers.Cliente)) or
     (AOscManufacturers.Cliente.IsEmpty) then
    raise Exception.Create('Debe indicar el cliente de esta factura');

  if (EsFechaVacia(AOscManufacturers.FECHA_FACTURA)) then
    raise Exception.Create('Debe indicar la fecha de esta factura');
	
  if Length(AOscManufacturers.REFERENCIA) = 0 then
    raise Exception.Create('Debe indicar una referencia para esta forma de pago.');

  if Length(AOscManufacturers.DESCRIPCION) = 0 then
    raise Exception.Create('Debe indicar una descripción para esta forma de pago.');
  }


  { Asegurarse de valores en campos "automáticos" tanto
    en MODIFICACIÓN como en INSERCIÓN. }
{  AOscManufacturers.Edit;
  try
    AOscManufacturers.USUARIO := dmUsuarios.LoginInfo.Usuario;
    Result := True;
  finally
    AOscManufacturers.Post;
  end;}

  Result := True;
end;

procedure TOscManufacturersController.Ver(AOscManufacturers: IBizOscManufacturer);
{var
  AEditor : IEditorOscManufacturers;}
begin
{  AEditor := NIL;
  ShowHourglassCursor;
  try
    CreateEditor('EditorOscManufacturers', IEditorOscManufacturers, AEditor);
    with AEditor do
      OscManufacturers := AOscManufacturers;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    try
      AEditor.ShowModal;
      AEditor.Release;
    finally
      AEditor := NIL;
    end;}
end;

procedure TOscManufacturersController.VerTodos(AOscManufacturers: IBizOscManufacturer);
{var
  AEditor : IEditorOscManufacturers;}
begin
{  AEditor := NIL;
  ShowHourglassCursor;
  try
    CreateEditor('EditorOscManufacturers', IEditorOscManufacturers, AEditor);
    with AEditor do
      OscManufacturers := AOscManufacturers;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    try
      AEditor.ShowModal;
      AEditor.Release;
    finally
      AEditor := NIL;
    end;} 
end;

function TOscManufacturersController.Eliminar(AOscManufacturers: IBizOscManufacturer): Boolean;
begin
  Result := False;

  if not Assigned(AOscManufacturers) then
    raise Exception.Create ('OscManufacturers no asignado');

  ShowHourglassCursor;
  try
    if (AOscManufacturers.State in dsEditModes) then
      AOscManufacturers.Cancel;

    AOscManufacturers.Delete;
    AOscManufacturers.DataTable.ApplyUpdates;
    HideHourglassCursor;
    Result := True;
  finally
    HideHourglassCursor;
  end;
end;

procedure TOscManufacturersController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable);
// Descomentar si hubiera detalles
{var
  AOscManufacturers : IBizOscManufacturer;
  ADetalles : IBizDetallesOscManufacturers;}
begin
  inherited;

  // Descomentar si hubiera detalles
  {if Supports(ADataTable, IBizDetallesOscManufacturers, ADetalles) and
     Supports(ADetalles.DataTable.MasterSource.DataTable, IBizOscManufacturer, AOscManufacturers) then
  begin
    AOscManufacturers.Edit;
    try
      AOscManufacturers.IMPORTE_TOTAL := FDetallesController.DarTotalImporteTotal(ADetalles);
    finally
      AOscManufacturers.Post;
    end;
  end;}
end;

function TOscManufacturersController.Guardar(AOscManufacturers: IBizOscManufacturer): Boolean;
var
  NuevoID : Integer;
begin
  Result := False;

  if ValidarOscManufacturers(AOscManufacturers) then
  begin
    ShowHourglassCursor;
    try
{      if AOscManufacturers.EsNuevo then
        NuevoID := FDataModule.GetNextID(AOscManufacturers.DataTable.LogicalName)
      else}
        NuevoID := AOscManufacturers.manufacturers_id;

      AsignarID(AOscManufacturers, NuevoID);
      AOscManufacturers.DataTable.ApplyUpdates;
      
      Result := True;
    finally
      HideHourglassCursor;
    end;
  end;
end;

function TOscManufacturersController.Localizar(AOscManufacturers: IBizOscManufacturer; AIDLocal: Integer): Boolean;
begin
  Result := True;
  ShowHourglassCursor;
  try
    with AOscManufacturers.DataTable do
    begin
      DisableControls;
      First;
      if not Locate(fld_osc_Manufacturersrdx_manufacturers_id_local, AIDLocal, []) then
         Result := False;
      EnableControls;   
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TOscManufacturersController.Nuevo: IBizOscManufacturer;
var
  AOSCManufacturer : IBizOscManufacturer;
begin
  AOSCManufacturer := (FDataModule as IDataModuleTiendaWeb).NewOSCManufacturer;
  AOSCManufacturer.DataTable.Active := True;
  AOSCManufacturer.Insert;
  Result := AOSCManufacturer;
end;

function TOscManufacturersController.ExtraerSeleccionados(AOscManufacturers: IBizOscManufacturer): IBizOscManufacturer;
var
  ASeleccionados : IBizOscManufacturer;
begin
  ASeleccionados := (Self.Buscar(ID_NULO) as IBizOscManufacturer);
  CopyDataTable(AOscManufacturers.DataTable, ASeleccionados.DataTable, True);
  Result := ASeleccionados;
end;

function TOscManufacturersController.ElegirOscManufacturers(AOscManufacturers: IBizOscManufacturer; AMensaje: String; AMultiSelect: Boolean): IBizOscManufacturer;
{var
  AEditor : IEditorElegirOscManufacturers;}
begin
  Result := NIL;

{  CreateEditor('EditorElegirOscManufacturers', IEditorElegirOscManufacturers, AEditor);
  try
    with AEditor do
    begin
      OscManufacturers := AOscManufacturers;
      MultiSelect := AMultiSelect;
      Mensaje := AMensaje;
      if IsPositiveResult(ShowModal) then
        Result := OscManufacturersSeleccionados;
	  Release;
    end;
  finally
    AEditor := NIL;
  end;}
end;

// Descomentar esto si hay informe
// procedure TOscManufacturersController.Preview(AOscManufacturers: IBizOscManufacturer);
// var
//   AReportController : IOscManufacturersReportController;
// begin
//   AReportController := TOscManufacturersReportController.Create;
//   try
//     AReportController.Preview(AOscManufacturers.ID);
//   finally
//     AReportController := NIL;
//   end;
// end;

// Descomentar esto si hay informe
// procedure TOscManufacturersController.Print(AOscManufacturers: IBizOscManufacturer);
// var
//   AReportController : IOscManufacturersReportController;
// begin
//   AReportController := TOscManufacturersReportController.Create;
//   try
//     AReportController.Print(AOscManufacturers.ID);
//   finally
//     AReportController := NIL;
//   end;
// end;

end.
