git-svn-id: https://192.168.0.254/svn/Proyectos.AbetoDesign_FactuGES/trunk@161 93f398dd-4eb6-7a46-baf6-13f46f578da2
397 lines
11 KiB
ObjectPascal
397 lines
11 KiB
ObjectPascal
unit uEmpresasController;
|
||
|
||
interface
|
||
|
||
|
||
uses
|
||
Windows, Forms, Classes, Controls, Contnrs, SysUtils,
|
||
uBizEmpresas, uIDataModuleEmpresas, uDADataTable, uControllerBase,
|
||
uBizEmpresasTiendas, uTiendasEmpresaController;
|
||
|
||
type
|
||
IEmpresasController = interface(IControllerBase)
|
||
['{2F0AB21C-4F19-446E-87C4-B9C1038850FC}']
|
||
function Buscar(const ID: Integer): IBizEmpresa;
|
||
function BuscarTodos: IBizEmpresa; overload;
|
||
function BuscarTodos(const ID_PERFIL: Integer): IBizEmpresa; overload;
|
||
procedure Ver(AEmpresa : IBizEmpresa);
|
||
procedure VerTodos(AEmpresas: IBizEmpresa);
|
||
function Nuevo : IBizEmpresa;
|
||
procedure Anadir(AEmpresa : IBizEmpresa);
|
||
function Eliminar(const ID : Integer): Boolean; overload;
|
||
function Eliminar(AEmpresa : IBizEmpresa): Boolean; overload;
|
||
function Guardar(AEmpresa : IBizEmpresa): Boolean;
|
||
procedure DescartarCambios(AEmpresa : IBizEmpresa);
|
||
function Existe(const ID: Integer) : Boolean;
|
||
function ToStringList(AEmpresa : IBizEmpresa) : TStringList;
|
||
|
||
function DarListaTiendas(AEmpresa : IBizEmpresa): TStringList;
|
||
function DarTiendaEmpresa(AEmpresa : IBizEmpresa; const num: Integer): IBizEmpresaTienda;
|
||
function DarTiendaUsuario(AEmpresa: IBizEmpresa; const ID_USUARIO: Integer): IBizEmpresaTienda;
|
||
|
||
function GetTiendasController : ITiendasEmpresaController;
|
||
property TiendasController : ITiendasEmpresaController read GetTiendasController;
|
||
|
||
function DarListaFormasPago: TStringList;
|
||
function DarListaTiposIVA: TStringList;
|
||
Procedure AsignarIDFormaPago(AEmpresa: IBizEmpresa; const IDFormaPago: Variant);
|
||
Procedure AsignarIDTipoIVA(AEmpresa: IBizEmpresa; const IDTipoIVA: Variant);
|
||
end;
|
||
|
||
TEmpresasController = class(TControllerBase, IEmpresasController)
|
||
protected
|
||
FDataModule : IDataModuleEmpresas;
|
||
FTiendasController : ITiendasEmpresaController;
|
||
function ValidarEmpresa(AEmpresa : IBizEmpresa): Boolean; virtual;
|
||
function GetTiendasController : ITiendasEmpresaController;
|
||
public
|
||
constructor Create; override;
|
||
destructor Destroy; override;
|
||
|
||
function Eliminar(const ID : Integer): Boolean; overload;
|
||
function Eliminar(AEmpresa : IBizEmpresa): Boolean; overload;
|
||
function Guardar(AEmpresa : IBizEmpresa): Boolean;
|
||
procedure DescartarCambios(AEmpresa : IBizEmpresa); virtual;
|
||
function Existe(const ID: Integer) : Boolean; virtual;
|
||
procedure Anadir(AEmpresa : IBizEmpresa); virtual;
|
||
|
||
function Buscar(const ID: Integer): IBizEmpresa; virtual;
|
||
function BuscarTodos: IBizEmpresa; overload; virtual;
|
||
function BuscarTodos(const ID_PERFIL: Integer): IBizEmpresa; overload;
|
||
function Nuevo : IBizEmpresa; virtual;
|
||
procedure Ver(AEmpresa : IBizEmpresa); virtual;
|
||
procedure VerTodos(AEmpresas: IBizEmpresa); virtual;
|
||
function ToStringList(AEmpresa : IBizEmpresa) : TStringList; virtual;
|
||
|
||
function DarListaTiendas(AEmpresa : IBizEmpresa): TStringList;
|
||
function DarTiendaEmpresa(AEmpresa : IBizEmpresa; const num: Integer): IBizEmpresaTienda;
|
||
function DarTiendaUsuario(AEmpresa: IBizEmpresa; const ID_USUARIO: Integer): IBizEmpresaTienda;
|
||
function DarListaFormasPago: TStringList;
|
||
function DarListaTiposIVA: TStringList;
|
||
Procedure AsignarIDFormaPago(AEmpresa: IBizEmpresa; const IDFormaPago: Variant);
|
||
Procedure AsignarIDTipoIVA(AEmpresa: IBizEmpresa; const IDTipoIVA: Variant);
|
||
|
||
property TiendasController : ITiendasEmpresaController read GetTiendasController;
|
||
end;
|
||
|
||
implementation
|
||
|
||
uses
|
||
uDAInterfaces, uEditorRegistryUtils, cxControls, DB, uFactuGES_App,
|
||
uDataModuleEmpresas, uIEditorEmpresa, uStringsUtils, schEmpresasClient_Intf;
|
||
|
||
{ TEmpresasController }
|
||
|
||
procedure TEmpresasController.Anadir(AEmpresa: IBizEmpresa);
|
||
begin
|
||
AEmpresa.Insert;
|
||
end;
|
||
|
||
procedure TEmpresasController.AsignarIDFormaPago(AEmpresa: IBizEmpresa; const IDFormaPago: Variant);
|
||
begin
|
||
if Assigned(AEmpresa) then
|
||
begin
|
||
if not AEmpresa.DataTable.Editing then
|
||
AEmpresa.DataTable.Edit;
|
||
|
||
if EsCadenaVacia(IDFormaPago) then
|
||
AEmpresa.ID_FORMA_PAGOIsNull := true
|
||
else
|
||
AEmpresa.ID_FORMA_PAGO := IDFormaPago;
|
||
|
||
AEmpresa.DataTable.Post;
|
||
end;
|
||
end;
|
||
|
||
procedure TEmpresasController.AsignarIDTipoIVA(AEmpresa: IBizEmpresa;
|
||
const IDTipoIVA: Variant);
|
||
begin
|
||
if Assigned(AEmpresa) then
|
||
begin
|
||
if not AEmpresa.DataTable.Editing then
|
||
AEmpresa.DataTable.Edit;
|
||
|
||
if EsCadenaVacia(IDTipoIVA) then
|
||
AEmpresa.ID_TIPO_IVAIsNull := true
|
||
else
|
||
AEmpresa.ID_TIPO_IVA := IDTipoIVA;
|
||
|
||
AEmpresa.DataTable.Post;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.Buscar(const ID: Integer): IBizEmpresa;
|
||
begin
|
||
Result := FDataModule.GetItem(ID)
|
||
end;
|
||
|
||
function TEmpresasController.BuscarTodos(const ID_PERFIL: Integer): IBizEmpresa;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
|
||
with Result.DataTable.DynamicWhere do
|
||
begin
|
||
// ID_PERFIL
|
||
Condicion := NewBinaryExpression(NewField('', fld_EmpresasID_PERFIL), NewConstant(ID_PERFIL, datInteger), dboEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
Result.open;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.BuscarTodos: IBizEmpresa;
|
||
begin
|
||
Result := FDataModule.GetItems;
|
||
end;
|
||
|
||
constructor TEmpresasController.Create;
|
||
begin
|
||
inherited;
|
||
FDataModule := TDataModuleEmpresas.Create(Nil);
|
||
FTiendasController := TTiendasEmpresaController.Create;
|
||
end;
|
||
|
||
function TEmpresasController.DarListaFormasPago: TStringList;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := FDataModule.DarListaFormasPago;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.DarListaTiendas(
|
||
AEmpresa: IBizEmpresa): TStringList;
|
||
var
|
||
ATiendas: IBizEmpresaTienda;
|
||
begin
|
||
if not Assigned(AEmpresa) then
|
||
raise Exception.Create ('Empresa no asignada (DarListaTiendas)');
|
||
|
||
ATiendas := AEmpresa.Tiendas;
|
||
ATiendas.DataTable.Active := True;
|
||
|
||
Result := TStringList.Create;
|
||
try
|
||
with Result do
|
||
begin
|
||
// Un documento debe pertenecer a una tienda y solo a una
|
||
// Add(Format('%s=%d', [CTE_TODAS, CTE_ID_TODAS]));
|
||
|
||
ATiendas.DataTable.First;
|
||
while not ATiendas.DataTable.EOF do
|
||
begin
|
||
Add(Format('%s=%d', [ATiendas.NOMBRE, ATiendas.ID]));
|
||
ATiendas.DataTable.Next;
|
||
end;
|
||
end;
|
||
finally
|
||
ATiendas := NIL;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.DarListaTiposIVA: TStringList;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := FDataModule.DarListaTiposIVA;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.DarTiendaEmpresa(AEmpresa: IBizEmpresa; const num: Integer): IBizEmpresaTienda;
|
||
//Esta funci<63>n nos devuelve la tienda con numero de orden num (el orden de las tiendas ser<65> por id)
|
||
var
|
||
AListaTiendas: TStringList;
|
||
begin
|
||
Result := Nil;
|
||
AListaTiendas := DarListaTiendas(AEmpresa);
|
||
//Siempre nos dara en la lista de tiendas la opci<63>n Todas que evitamos
|
||
if (AListaTiendas.Count > 1) then
|
||
Result := FTiendasController.Buscar(StrToInt(AListaTiendas.ValueFromIndex[num+1]));
|
||
FreeAndNil(AListaTiendas);
|
||
end;
|
||
|
||
function TEmpresasController.DarTiendaUsuario(AEmpresa: IBizEmpresa; const ID_USUARIO: Integer): IBizEmpresaTienda;
|
||
var
|
||
AIDTienda : Integer;
|
||
begin
|
||
AIDTienda := FDataModule.DarTiendaUsuario(AEmpresa.ID, ID_USUARIO);
|
||
Result := FTiendasController.Buscar(AIDTienda);
|
||
end;
|
||
|
||
procedure TEmpresasController.DescartarCambios(AEmpresa: IBizEmpresa);
|
||
begin
|
||
if not Assigned(AEmpresa) then
|
||
raise Exception.Create ('Empresa no asignada (DescartarCambios)');
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
if (AEmpresa.State in dsEditModes) then
|
||
AEmpresa.Cancel;
|
||
|
||
AEmpresa.DataTable.CancelUpdates;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
destructor TEmpresasController.Destroy;
|
||
begin
|
||
FDataModule := NIL;
|
||
FTiendasController := NIL;
|
||
inherited;
|
||
end;
|
||
|
||
function TEmpresasController.Eliminar(AEmpresa: IBizEmpresa): Boolean;
|
||
begin
|
||
if not Assigned(AEmpresa) then
|
||
raise Exception.Create ('Empresa no asignada (Eliminar)');
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
if (AEmpresa.State in dsEditModes) then
|
||
AEmpresa.Cancel;
|
||
|
||
AEmpresa.Delete;
|
||
AEmpresa.DataTable.ApplyUpdates;
|
||
|
||
Result := True;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.Eliminar(const ID: Integer): Boolean;
|
||
var
|
||
AEmpresa : IBizEmpresa;
|
||
begin
|
||
AEmpresa := Buscar(ID);
|
||
|
||
if not Assigned(AEmpresa) then
|
||
raise Exception.Create(Format('No se ha encontrado la empresa con ID = %d', [ID]));
|
||
|
||
Result := Eliminar(AEmpresa);
|
||
AEmpresa := NIL;
|
||
end;
|
||
|
||
function TEmpresasController.Existe(const ID: Integer): Boolean;
|
||
var
|
||
AEmpresa : IBizEmpresa;
|
||
begin
|
||
try
|
||
AEmpresa := Buscar(ID);
|
||
Result := Assigned(AEmpresa) and (AEmpresa.ID = ID);
|
||
finally
|
||
AEmpresa := NIL;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.GetTiendasController: ITiendasEmpresaController;
|
||
begin
|
||
Result := FTiendasController;
|
||
end;
|
||
|
||
function TEmpresasController.Guardar(AEmpresa: IBizEmpresa): Boolean;
|
||
begin
|
||
Result := False;
|
||
|
||
if ValidarEmpresa(AEmpresa) then
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
AEmpresa.DataTable.ApplyUpdates;
|
||
Result := True;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.Nuevo: IBizEmpresa;
|
||
begin
|
||
Result := FDataModule.NewItem;
|
||
end;
|
||
|
||
function TEmpresasController.ToStringList(AEmpresa: IBizEmpresa): TStringList;
|
||
begin
|
||
Result := TStringList.Create;
|
||
with Result do
|
||
begin
|
||
AEmpresa.DataTable.Active := True;
|
||
AEmpresa.First;
|
||
while not AEmpresa.EOF do
|
||
begin
|
||
Add(AEmpresa.NOMBRE);
|
||
AEmpresa.Next;
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
function TEmpresasController.ValidarEmpresa(AEmpresa: IBizEmpresa): Boolean;
|
||
begin
|
||
if not Assigned(AEmpresa) then
|
||
raise Exception.Create ('Empresa no asignada (ValidarEmpresa)');
|
||
|
||
if (AEmpresa.DataTable.State in dsEditModes) then
|
||
AEmpresa.DataTable.Post;
|
||
|
||
if Length(AEmpresa.NOMBRE) = 0 then
|
||
raise Exception.Create('Debe indicar al menos el nombre de la empresa.');
|
||
|
||
// Asegurarse de valores en campos "autom<6F>ticos"
|
||
AEmpresa.Edit;
|
||
AEmpresa.USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
||
AEmpresa.Post;
|
||
|
||
Result := True;
|
||
end;
|
||
|
||
procedure TEmpresasController.Ver(AEmpresa: IBizEmpresa);
|
||
var
|
||
AEditor : IEditorEmpresa;
|
||
begin
|
||
AEditor := NIL;
|
||
|
||
CreateEditor('EditorEmpresa', IEditorEmpresa, AEditor);
|
||
if Assigned(AEditor) then
|
||
try
|
||
AEditor.Empresa := AEmpresa;
|
||
AEditor.Controller := Self;
|
||
AEditor.ShowModal;
|
||
finally
|
||
AEditor.Release;
|
||
AEditor := NIL;
|
||
end;
|
||
end;
|
||
|
||
procedure TEmpresasController.VerTodos(AEmpresas: IBizEmpresa);
|
||
{var
|
||
AEditor : IEditorEmpresas;}
|
||
begin
|
||
{ AEditor := NIL;
|
||
CreateEditor('EditorEmpresas', IEditorEmpresas, AEditor);
|
||
if Assigned(AEditor) then
|
||
with AEditor do
|
||
begin
|
||
Contactos := AContactos;
|
||
Controller := Self;
|
||
ShowEmbedded;
|
||
end;
|
||
}
|
||
end;
|
||
|
||
end.
|