git-svn-id: https://192.168.0.254/svn/Proyectos.Noviseda_FactuGES2/trunk@132 f33bb606-9f5c-448d-9c99-757f00063c96
1116 lines
33 KiB
ObjectPascal
1116 lines
33 KiB
ObjectPascal
unit uPedidosClienteController;
|
||
|
||
interface
|
||
|
||
|
||
uses
|
||
Classes, SysUtils, uDADataTable, uEditorDBItem,
|
||
uControllerBase, uIDataModulePedidosCliente, uClientesController,
|
||
uDetallesPedidoClienteController, uBizPedidosCliente, uBizDireccionesContacto,
|
||
uBizDetallesPedidoCliente, uViewGridBase;
|
||
|
||
type
|
||
IPedidosClienteController = interface(IControllerBase)
|
||
['{21ED0332-F0E0-468D-8D53-8CA362757191}']
|
||
function GetClienteController: IClientesController;
|
||
procedure SetClienteController(const Value: IClientesController);
|
||
property ClienteController: IClientesController read GetClienteController write SetClienteController;
|
||
|
||
function GetDetallesController: IDetallesPedidoClienteController;
|
||
procedure SetDetallesController(const Value: IDetallesPedidoClienteController);
|
||
property DetallesController: IDetallesPedidoClienteController read GetDetallesController write SetDetallesController;
|
||
|
||
procedure RecuperarCliente(APedido : IBizPedidoCliente);
|
||
function Buscar(const ID: Integer): IBizPedidoCliente;
|
||
function BuscarTodos: IBizPedidoCliente; overload;
|
||
function BuscarTodos(const ID_Cliente: Integer): IBizPedidoCliente; overload;
|
||
function BuscarTodosVendedor(const ID_Vendedor: Integer): IBizPedidoCliente;
|
||
function BuscarPendientes : IBizPedidoCliente;
|
||
function BuscarArticulosPendientes: IBizArticulosPendientes;
|
||
procedure Ver(APedido : IBizPedidoCliente);
|
||
procedure VerTodos(APedidos: IBizPedidoCliente);
|
||
procedure VerTodosPedidos(APedidos: IBizPedidoCliente;
|
||
const AVerModal : Boolean = False; const AWindowCaption: String = '';
|
||
const AHeaderText: String = '');
|
||
procedure VerDireccionEntrega(APedido : IBizPedidoCliente);
|
||
procedure VerArticulosPendientes;
|
||
function Nuevo : IBizPedidoCliente;
|
||
function Anadir(APedido : IBizPedidoCliente) : Boolean;
|
||
function Eliminar(const ID : Integer): Boolean; overload;
|
||
function Eliminar(APedido : IBizPedidoCliente; AllItems: Boolean = false): Boolean; overload;
|
||
function Guardar(APedido : IBizPedidoCliente): Boolean;
|
||
procedure DescartarCambios(APedido : IBizPedidoCliente);
|
||
function Existe(const ID: Integer) : Boolean;
|
||
function Duplicar(APedido: IBizPedidoCliente): IBizPedidoCliente;
|
||
|
||
function Localizar(APedidos: IBizPedidoCliente; const ID : Integer): Boolean;
|
||
function ExtraerSeleccionados(APedido: IBizPedidoCliente) : IBizPedidoCliente;
|
||
function ElegirPedidos(APedido: IBizPedidoCliente; AMensaje: String; AMultiSelect: Boolean): IBizPedidoCliente;
|
||
|
||
procedure CopiarDireccionEnvio (const ADireccionEnvio: IBizDireccionesContacto;
|
||
APedido: IBizPedidoCliente);
|
||
procedure QuitarDireccionEnvio(APedido: IBizPedidoCliente);
|
||
|
||
procedure RecalcularImportes(APedido : IBizPedidoCliente);
|
||
function EsModificable(APedido : IBizPedidoCliente): Boolean;
|
||
function EsEliminable(APedido : IBizPedidoCliente): Boolean;
|
||
|
||
procedure Preview(APedido : IBizPedidoCliente; AllItems: Boolean = false);
|
||
function Print(APedido : IBizPedidoCliente; AllItems: Boolean = false): Boolean;
|
||
|
||
function EnviarEmailPedidos(APedidos : IBizPedidoCliente): Boolean;
|
||
function GenerarEmailPedido(APedido : IBizPedidoCliente): Boolean;
|
||
function EnviarPedidoPorEMail(APedido: IBizPedidoCliente;
|
||
const AEnviarDirectamente: Boolean = True;
|
||
const ADireccionEMail: String = ''; const AAsuntoEMail: String = '';
|
||
const ATextoEMail: String = ''): Boolean;
|
||
|
||
function DarListaAnosPedidos: TStringList;
|
||
procedure FiltrarAno(APedido: IBizPedidoCliente; AWhereDataTable: String; const Ano: String);
|
||
end;
|
||
|
||
TPedidosClienteController = class(TControllerBase, IPedidosClienteController)
|
||
protected
|
||
FDataModule : IDataModulePedidosCliente;
|
||
FClienteController : IClientesController;
|
||
FDetallesController : IDetallesPedidoClienteController;
|
||
|
||
function GetClienteController: IClientesController;
|
||
procedure SetClienteController(const Value: IClientesController);
|
||
function GetDetallesController: IDetallesPedidoClienteController;
|
||
procedure SetDetallesController(const Value: IDetallesPedidoClienteController);
|
||
|
||
//Estos son los tres m<>todos a sobre escribir si se desea heredar toda la logica de
|
||
//este controller
|
||
procedure AsignarDataModule; virtual;
|
||
procedure RecuperarObjetos(APedido: IBizPedidoCliente); virtual;
|
||
|
||
procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
|
||
function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean;
|
||
procedure FiltrarEmpresa(APedido: IBizPedidoCliente);
|
||
function _Vacio : IBizPedidoCliente;
|
||
function ValidarPedido(APedido: IBizPedidoCliente): Boolean;
|
||
|
||
public
|
||
property ClienteController: IClientesController read GetClienteController write SetClienteController;
|
||
property DetallesController: IDetallesPedidoClienteController read GetDetallesController write SetDetallesController;
|
||
|
||
constructor Create; virtual;
|
||
destructor Destroy; override;
|
||
|
||
function Localizar(APedidos: IBizPedidoCliente; const ID : Integer): Boolean;
|
||
|
||
procedure RecuperarCliente(APedido : IBizPedidoCliente);
|
||
function Eliminar(const ID : Integer): Boolean; overload;
|
||
function Eliminar(APedido : IBizPedidoCliente; AllItems: Boolean = false): Boolean; overload;
|
||
function Guardar(APedido : IBizPedidoCliente): Boolean;
|
||
procedure DescartarCambios(APedido : IBizPedidoCliente); virtual;
|
||
function Existe(const ID: Integer) : Boolean; virtual;
|
||
function Anadir(APedido : IBizPedidoCliente) : Boolean;
|
||
function Buscar(const ID: Integer): IBizPedidoCliente;
|
||
function BuscarTodos: IBizPedidoCliente; overload;
|
||
function BuscarTodos(const ID_Cliente: Integer): IBizPedidoCliente; overload;
|
||
function BuscarTodosVendedor(const ID_Vendedor: Integer): IBizPedidoCliente;
|
||
function BuscarPendientes : IBizPedidoCliente;
|
||
function BuscarArticulosPendientes: IBizArticulosPendientes;
|
||
function Nuevo : IBizPedidoCliente;
|
||
procedure Ver(APedido : IBizPedidoCliente);
|
||
procedure VerTodos(APedidos: IBizPedidoCliente);
|
||
procedure VerTodosPedidos(APedidos: IBizPedidoCliente;
|
||
const AVerModal : Boolean = False; const AWindowCaption: String = '';
|
||
const AHeaderText: String = '');
|
||
procedure VerDireccionEntrega(APedido : IBizPedidoCliente);
|
||
procedure VerArticulosPendientes;
|
||
function Duplicar(APedido: IBizPedidoCliente): IBizPedidoCliente;
|
||
procedure CopiarDireccionEnvio (const ADireccionEnvio: IBizDireccionesContacto;
|
||
APedido: IBizPedidoCliente);
|
||
|
||
procedure QuitarDireccionEnvio(APedido: IBizPedidoCliente);
|
||
function ExtraerSeleccionados(APedido: IBizPedidoCliente) : IBizPedidoCliente;
|
||
function ElegirPedidos(APedido: IBizPedidoCliente; AMensaje: String; AMultiSelect: Boolean): IBizPedidoCliente;
|
||
|
||
procedure RecalcularImportes(APedido : IBizPedidoCliente);
|
||
function EsModificable(APedido : IBizPedidoCliente): Boolean;
|
||
function EsEliminable(APedido : IBizPedidoCliente): Boolean;
|
||
|
||
procedure Preview(APedido : IBizPedidoCliente; AllItems: Boolean = false);
|
||
function Print(APedido : IBizPedidoCliente; AllItems: Boolean = false): Boolean;
|
||
|
||
function EnviarEmailPedidos(APedidos : IBizPedidoCliente): Boolean;
|
||
function GenerarEmailPedido(APedido : IBizPedidoCliente): Boolean;
|
||
function EnviarPedidoPorEMail(APedido: IBizPedidoCliente;
|
||
const AEnviarDirectamente: Boolean = True;
|
||
const ADireccionEMail: String = ''; const AAsuntoEMail: String = '';
|
||
const ATextoEMail: String = ''): Boolean;
|
||
|
||
function DarListaAnosPedidos: TStringList;
|
||
procedure FiltrarAno(APedido: IBizPedidoCliente; AWhereDataTable: String; const Ano: String);
|
||
end;
|
||
|
||
implementation
|
||
|
||
uses
|
||
uROTypes, Controls, cxControls, DB, uEditorRegistryUtils, uEditorPreview, DateUtils,
|
||
uIEditorPedidosCliente, uDataModulePedidosCliente, Variants,
|
||
uBizContactos, uDataTableUtils, uDataModuleUsuarios, uFactuGES_App,
|
||
schPedidosClienteClient_Intf, uDAInterfaces, uDateUtils, uIEditorPedidoCliente,
|
||
uIEditorElegirPedidosCliente, uIEditorDireccionEntregaPedidoCliente,
|
||
schContactosClient_Intf, uPedidosClienteReportController, uIntegerListUtils,
|
||
uIDialogListaPedidosCliEnvioEMail, uSistemaFunc, uStringsUtils, uDialogElegirEMail,
|
||
uIEditorArticulosPendientes, Forms, uEMailUtils;
|
||
|
||
{ TPedidosClienteController }
|
||
|
||
function TPedidosClienteController.Anadir(APedido: IBizPedidoCliente): Boolean;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado (Anadir)');
|
||
APedido.Insert;
|
||
Result := True;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.AsignarDataModule;
|
||
begin
|
||
FDataModule := TDataModulePedidosCliente.Create(Nil);
|
||
end;
|
||
|
||
function TPedidosClienteController.Buscar(const ID: Integer): IBizPedidoCliente;
|
||
begin
|
||
Result := FDataModule.GetItem(ID);
|
||
// FiltrarEmpresa(Result);
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarTodos: IBizPedidoCliente;
|
||
begin
|
||
Result := FDataModule.GetItems;
|
||
// FiltrarEmpresa(Result);
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarPendientes: IBizPedidoCliente;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
with Result.DataTable.Where do
|
||
begin
|
||
if NotEmpty then
|
||
AddOperator(opAND);
|
||
OpenBraket;
|
||
AddText(fld_PedidosClienteSITUACION + ' <> ''' + SITUACION_PEDIDO_SERVIDO + '''');
|
||
CloseBraket;
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarTodos(const ID_Cliente: Integer): IBizPedidoCliente;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
|
||
with Result.DataTable.DynamicWhere do
|
||
begin
|
||
// ID_CLIENTE
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteID_CLIENTE), NewConstant(ID_Cliente, datInteger), dboEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarTodosVendedor(
|
||
const ID_Vendedor: Integer): IBizPedidoCliente;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
|
||
with Result.DataTable.DynamicWhere do
|
||
begin
|
||
// ID_VENDEDOR
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteID_VENDEDOR), NewConstant(ID_VENDEDOR, datInteger), dboEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarArticulosPendientes: IBizArticulosPendientes;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := FDataModule.GetArticulosPendientes;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.CopiarDireccionEnvio(
|
||
const ADireccionEnvio: IBizDireccionesContacto; APedido: IBizPedidoCliente);
|
||
var
|
||
bEnEdicion : Boolean;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado (CopiarDireccionEnvio)');
|
||
|
||
if not Assigned(ADireccionEnvio) then
|
||
raise Exception.Create ('No se ha indicado la direcci<63>n (CopiarDireccionEnvio)');
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
if ADireccionEnvio.DataTable.Active then
|
||
ADireccionEnvio.DataTable.Active := True;
|
||
|
||
|
||
bEnEdicion := (APedido.DataTable.State in dsEditModes);
|
||
if not bEnEdicion then
|
||
APedido.Edit;
|
||
|
||
ShowHourglassCursor;
|
||
APedido.Edit;
|
||
try
|
||
APedido.CALLE := ADireccionEnvio.CALLE;
|
||
APedido.POBLACION := ADireccionEnvio.POBLACION;
|
||
APedido.CODIGO_POSTAL := ADireccionEnvio.CODIGO_POSTAL;
|
||
APedido.PROVINCIA := ADireccionEnvio.PROVINCIA;
|
||
APedido.TELEFONO := ADireccionEnvio.TELEFONO;
|
||
APedido.PERSONA_CONTACTO := ADireccionEnvio.PERSONA_CONTACTO;
|
||
|
||
if not bEnEdicion then
|
||
APedido.Post;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
constructor TPedidosClienteController.Create;
|
||
begin
|
||
inherited;
|
||
AsignarDataModule;
|
||
|
||
FClienteController := TClientesController.Create;
|
||
FDetallesController := TDetallesPedidoClienteController.Create;
|
||
FDetallesController.addObservador(Self);
|
||
end;
|
||
|
||
function TPedidosClienteController.CreateEditor(const AName: String;
|
||
const IID: TGUID; out Intf): Boolean;
|
||
begin
|
||
Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf);
|
||
end;
|
||
|
||
function TPedidosClienteController.DarListaAnosPedidos: TStringList;
|
||
begin
|
||
Result := FDataModule.GetAnosItems;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.DescartarCambios(APedido: IBizPedidoCliente);
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignada');
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
if (APedido.State in dsEditModes) then
|
||
APedido.Cancel;
|
||
|
||
APedido.DataTable.CancelUpdates;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
destructor TPedidosClienteController.Destroy;
|
||
begin
|
||
FDataModule := Nil;
|
||
FClienteController := Nil;
|
||
FDetallesController := Nil;
|
||
inherited;
|
||
end;
|
||
|
||
function TPedidosClienteController.Duplicar(
|
||
APedido: IBizPedidoCliente): IBizPedidoCliente;
|
||
begin
|
||
Result := Self._Vacio;
|
||
ShowHourglassCursor;
|
||
try
|
||
DuplicarRegistros(APedido.DataTable, Result.DataTable, mdrActual);
|
||
DuplicarRegistros(APedido.Detalles.DataTable, Result.Detalles.DataTable, mdrTodos);
|
||
|
||
// Hay que dejar algunos campos como si fuera un presupuesto nuevo
|
||
Result.Edit;
|
||
with Result do
|
||
begin
|
||
ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
|
||
USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
||
FECHA_PEDIDO := DateOf(Now);
|
||
FECHA_PREVISTA_ENVIO := 0;
|
||
INCIDENCIAS_ACTIVAS := 0;
|
||
// INCIDENCIAS := Nil;
|
||
REFERENCIA := '';
|
||
SetFieldNull(DataTable, fld_PedidosClienteREF_TIENDA_WEB);
|
||
end;
|
||
Result.Post;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.ValidarPedido(
|
||
APedido: IBizPedidoCliente): Boolean;
|
||
begin
|
||
Result := False;
|
||
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado');
|
||
|
||
if (APedido.DataTable.State in dsEditModes) then
|
||
APedido.DataTable.Post;
|
||
|
||
//Tambien hacemos post de sus tablas hija
|
||
if (APedido.Detalles.DataTable.State in dsEditModes) then
|
||
APedido.Detalles.DataTable.Post;
|
||
|
||
// Abrir el cliente para la validaci<63>n
|
||
if Assigned(APedido.Cliente) then
|
||
APedido.Cliente.DataTable.Active := True;
|
||
|
||
if (APedido.ID_Cliente < 0) or
|
||
(not Assigned(APedido.Cliente)) or
|
||
(APedido.Cliente.IsEmpty) then
|
||
raise Exception.Create('Debe indicar el cliente de este pedido');
|
||
|
||
if (EsFechaVacia(APedido.FECHA_PEDIDO)) then
|
||
raise Exception.Create('Debe indicar la fecha de este pedido');
|
||
|
||
{ Un albaran debe tener una forma de pago asignada. }
|
||
if (APedido.ID_FORMA_PAGO = 0) then
|
||
raise Exception.Create('Debe indicar una forma de pago para este pedido');
|
||
|
||
if (not EsFechaVacia(APedido.FECHA_PREVISTA_ENVIO)) then
|
||
begin
|
||
if (APedido.FECHA_PREVISTA_ENVIO < APedido.FECHA_PEDIDO) then
|
||
raise Exception.Create('La fecha prevista de env<6E>o no puede ser anterior a la fecha del pedido');
|
||
end;
|
||
|
||
if (APedido.Detalles.DataTable.RecordCount = 0) then
|
||
raise Exception.Create('El pedido debe tener en su contenido al menos un concepto');
|
||
|
||
{ Asegurarse de valores en campos "autom<6F>ticos" tanto
|
||
en MODIFICACI<43>N como en INSERCI<43>N. }
|
||
APedido.Edit;
|
||
try
|
||
APedido.USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
||
|
||
if Assigned(APedido.Cliente)
|
||
and (APedido.ID_CLIENTE <> APedido.Cliente.ID) then
|
||
APedido.ID_CLIENTE := APedido.Cliente.ID;
|
||
|
||
Result := True;
|
||
finally
|
||
APedido.Post;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.Ver(APedido: IBizPedidoCliente);
|
||
var
|
||
AEditor : IEditorPedidoCliente;
|
||
begin
|
||
AEditor := NIL;
|
||
ShowHourglassCursor;
|
||
try
|
||
RecuperarObjetos(APedido);
|
||
CreateEditor('EditorPedidoCliente', IEditorPedidoCliente, AEditor);
|
||
|
||
if Assigned(AEditor) then
|
||
with AEditor do
|
||
begin
|
||
Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
||
Pedido := APedido;
|
||
|
||
//MODO CONSULTAR
|
||
if not EsModificable(APedido) then
|
||
begin
|
||
SetDataTableReadOnly(APedido.DataTable, True);
|
||
ReadOnly := True;
|
||
end;
|
||
|
||
ShowModal;
|
||
|
||
//MODO CONSULTAR (Se deja la tabla como estaba)
|
||
if ReadOnly then
|
||
SetDataTableReadOnly(APedido.DataTable, False);
|
||
|
||
AEditor.Release;
|
||
end;
|
||
finally
|
||
AEditor := NIL;
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.VerArticulosPendientes;
|
||
var
|
||
AEditor : IEditorArticulosPendientes;
|
||
begin
|
||
AEditor := NIL;
|
||
ShowHourglassCursor;
|
||
try
|
||
CreateEditor('EditorArticulosPendientes', IEditorArticulosPendientes, AEditor);
|
||
if Assigned(AEditor) then
|
||
with AEditor do
|
||
begin
|
||
// Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
||
ArticulosPendientes := BuscarArticulosPendientes;
|
||
|
||
ShowModal;
|
||
AEditor.Release;
|
||
|
||
end;
|
||
finally
|
||
AEditor := NIL;
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.VerDireccionEntrega(
|
||
APedido: IBizPedidoCliente);
|
||
var
|
||
AEditor : IEditorDireccionEntregaPedidoCliente;
|
||
begin
|
||
AEditor := NIL;
|
||
ShowHourglassCursor;
|
||
try
|
||
//RecuperarObjetos(APedido); <- No descomentar. No hace falta
|
||
CreateEditor('EditorDireccionEntregaPedidoCliente', IEditorDireccionEntregaPedidoCliente, AEditor);
|
||
|
||
with (AEditor as IEditorDireccionEntregaPedidoCliente) do
|
||
begin
|
||
Pedido := APedido;
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
|
||
if Assigned(AEditor) then
|
||
try
|
||
AEditor.ShowModal;
|
||
AEditor.Release;
|
||
finally
|
||
AEditor := NIL;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.VerTodos(APedidos: IBizPedidoCliente);
|
||
var
|
||
AEditor : IEditorPedidosCliente;
|
||
begin
|
||
AEditor := NIL;
|
||
ShowHourglassCursor;
|
||
try
|
||
CreateEditor('EditorPedidosCliente', IEditorPedidosCliente, AEditor);
|
||
if Assigned(AEditor) then
|
||
with AEditor do
|
||
begin
|
||
Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
||
Pedidos := APedidos;
|
||
MultiSelect := True;
|
||
ShowEmbedded;
|
||
end;
|
||
finally
|
||
AEditor := NIL;
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.VerTodosPedidos(APedidos: IBizPedidoCliente;
|
||
const AVerModal: Boolean; const AWindowCaption, AHeaderText: String);
|
||
var
|
||
AEditor : IEditorPedidosCliente;
|
||
begin
|
||
AEditor := NIL;
|
||
CreateEditor('EditorPedidosCliente', IEditorPedidosCliente, AEditor);
|
||
if Assigned(AEditor) then
|
||
try
|
||
if not EsCadenaVacia(AWindowCaption) then
|
||
AEditor.WindowCaption := AWindowCaption;
|
||
|
||
if not EsCadenaVacia(AHeaderText) then
|
||
AEditor.HeaderText := AHeaderText;
|
||
|
||
AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
||
AEditor.Pedidos := APedidos;
|
||
AEditor.MultiSelect := True;
|
||
|
||
if AVerModal then
|
||
AEditor.ShowModal
|
||
else
|
||
AEditor.ShowEmbedded;
|
||
finally
|
||
if AVerModal then
|
||
AEditor.Release;
|
||
AEditor := NIL;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController._Vacio: IBizPedidoCliente;
|
||
begin
|
||
Result := Buscar(ID_NULO);
|
||
end;
|
||
|
||
function TPedidosClienteController.Eliminar(const ID: Integer): Boolean;
|
||
var
|
||
APedido : IBizPedidoCliente;
|
||
begin
|
||
APedido := Buscar(ID);
|
||
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create(Format('No se ha encontrado el pedido con ID = %d', [ID]));
|
||
|
||
Result := Eliminar(APedido);
|
||
APedido := NIL;
|
||
end;
|
||
|
||
function TPedidosClienteController.ElegirPedidos(APedido: IBizPedidoCliente; AMensaje: String; AMultiSelect: Boolean): IBizPedidoCliente;
|
||
var
|
||
AEditor : IEditorElegirPedidosCliente;
|
||
begin
|
||
Result := NIL;
|
||
|
||
CreateEditor('EditorElegirPedidosCliente', IEditorElegirPedidosCliente, AEditor);
|
||
try
|
||
with AEditor do
|
||
begin
|
||
Controller := Self;
|
||
Pedidos := APedido;
|
||
MultiSelect := AMultiSelect;
|
||
Mensaje := AMensaje;
|
||
if IsPositiveResult(ShowModal) then
|
||
Result := PedidosClienteSeleccionados;
|
||
Release;
|
||
end;
|
||
finally
|
||
AEditor := NIL;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.Eliminar(APedido: IBizPedidoCliente; AllItems: Boolean = false): Boolean;
|
||
//En el caso de eliminar almenos un elemento del conjunto se devuelve true
|
||
var
|
||
bEliminado: Boolean;
|
||
|
||
begin
|
||
bEliminado := False;
|
||
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('APedido no asignado');
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
if not APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
if (APedido.State in dsEditModes) then
|
||
APedido.Cancel;
|
||
|
||
//Siempre eliminaremos el seleccionado
|
||
if EsEliminable(APedido) then
|
||
begin
|
||
APedido.Delete;
|
||
bEliminado := True;
|
||
end;
|
||
|
||
//En el caso de querer eliminar todos los items del objeto AAlbaran
|
||
if AllItems then
|
||
begin
|
||
with APedido.DataTable do
|
||
begin
|
||
First;
|
||
while not EOF do
|
||
begin
|
||
if EsEliminable(APedido) then
|
||
begin
|
||
APedido.Delete;
|
||
bEliminado := True
|
||
end
|
||
else Next;
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
if bEliminado then
|
||
begin
|
||
APedido.DataTable.ApplyUpdates;
|
||
Result := True;
|
||
end
|
||
else
|
||
Result := False;
|
||
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.EnviarEmailPedidos(APedidos: IBizPedidoCliente): Boolean;
|
||
var
|
||
ADialog : IDialogListaPedidosCliEnvioEMail;
|
||
ARespuesta : Integer;
|
||
begin
|
||
ADialog := NIL;
|
||
|
||
if not Assigned(APedidos) then
|
||
raise Exception.Create ('Facturas no asignadas (EnviarPedidosCliPorEMail)');
|
||
|
||
if APedidos.DataTable.Active then
|
||
APedidos.DataTable.Active := True;
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
CreateEditor('DialogListaPedidosCliEnvioEMail', IDialogListaPedidosCliEnvioEMail, ADialog);
|
||
|
||
if Assigned(ADialog) then
|
||
begin
|
||
try
|
||
ADialog.Pedidos := APedidos;
|
||
ARespuesta := ADialog.ShowModal;
|
||
Result := (ARespuesta = mrOK)
|
||
finally
|
||
ADialog.Release;
|
||
end;
|
||
end;
|
||
finally
|
||
ADialog := NIL;
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.EnviarPedidoPorEMail(
|
||
APedido: IBizPedidoCliente; const AEnviarDirectamente: Boolean;
|
||
const ADireccionEMail, AAsuntoEMail, ATextoEMail: String): Boolean;
|
||
var
|
||
AReportController : IPedidosClienteReportController;
|
||
AFicheroTMP : TFileName;
|
||
AEMail : String;
|
||
AAsunto : String;
|
||
AListaEmail : TStringList;
|
||
begin
|
||
{
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignada (EnviarFacturaPorEMail)');
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
RecuperarCliente(APedido);
|
||
APedido.Cliente.DataTable.Active := True;
|
||
|
||
AFicheroTMP := DarFicheroPDFTemporal(EscapeIllegalChars(APedido.REFERENCIA));
|
||
|
||
if not EsCadenaVacia(ADireccionEMail) then
|
||
AEMail := ADireccionEMail
|
||
else begin
|
||
AListaEmail := TStringList.Create;
|
||
try
|
||
if not EsCadenaVacia(APedido.Cliente.EMAIL_1) then
|
||
AListaEmail.Add(APedido.Cliente.EMAIL_1);
|
||
|
||
if not ElegirEMail(AListaEmail, AEMail) then
|
||
Exit;
|
||
finally
|
||
FreeANDNIL(AListaEmail);
|
||
end;
|
||
end;
|
||
|
||
if not EsCadenaVacia(AAsuntoEMail) then
|
||
AAsunto := AAsuntoEMail
|
||
else
|
||
AAsunto := 'Pedido ' + APedido.REFERENCIA;
|
||
|
||
ShowHourglassCursor;
|
||
Application.ProcessMessages;
|
||
|
||
AReportController := TPedidosClienteReportController.Create;
|
||
try
|
||
AReportController.ExportToPDF(APedido.ID, AFicheroTMP);
|
||
Result := EnviarEMailMAPI(AAsunto, ATextoEMail, AFicheroTMP, '', '', APedido.Cliente.NOMBRE, AEMail, AEnviarDirectamente);
|
||
finally
|
||
SysUtils.DeleteFile(AFicheroTMP);
|
||
AReportController := NIL;
|
||
HideHourglassCursor;
|
||
Application.ProcessMessages;
|
||
end;
|
||
}
|
||
end;
|
||
|
||
function TPedidosClienteController.EsEliminable(APedido: IBizPedidoCliente): Boolean;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado: EsEliminable');
|
||
|
||
Result := (APedido.SITUACION = SITUACION_PEDIDO_PENDIENTE);
|
||
end;
|
||
|
||
function TPedidosClienteController.EsModificable(APedido: IBizPedidoCliente): Boolean;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado: EsModificable');
|
||
|
||
Result := (APedido.SITUACION <> SITUACION_PEDIDO_SERVIDO);
|
||
end;
|
||
|
||
procedure TPedidosClienteController.RecalcularImportes(APedido: IBizPedidoCliente);
|
||
var
|
||
bEnEdicion : Boolean;
|
||
ADetallePosAct : Integer;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado (RecalcularImportes)');
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
{ Hay que guardar la posici<63>n en la que estamos en los detalles por que
|
||
la asignaci<63>n de valores a los campos IMPORTE_NETO e IMPORTE_PORTE
|
||
(ver m<>s adelante) colocan el puntero en la tabla detalle al principio.
|
||
No he encontrado la raz<61>n por la que mueve el puntero. }
|
||
|
||
ADetallePosAct := APedido.Detalles.POSICION;
|
||
|
||
bEnEdicion := (APedido.DataTable.State in dsEditModes);
|
||
if not bEnEdicion then
|
||
APedido.Edit;
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
APedido.IMPORTE_NETO := FDetallesController.DarTotalImporteTotal(APedido.Detalles);
|
||
if not bEnEdicion then
|
||
APedido.Post;
|
||
finally
|
||
HideHourglassCursor;
|
||
// Restaurar la posici<63>n que ten<65>amos en los detalles.
|
||
FDetallesController.LocalizarPosicion(APedido.Detalles, ADetallePosAct);
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable);
|
||
var
|
||
APedido : IBizPedidoCliente;
|
||
ADetalles : IBizDetallesPedidoCliente;
|
||
begin
|
||
inherited;
|
||
|
||
if Supports(ADataTable, IBizDetallesPedidoCliente, ADetalles) and
|
||
Supports(ADetalles.DataTable.MasterSource.DataTable, IBizPedidoCliente, APedido) then
|
||
begin
|
||
RecalcularImportes(APedido);
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.RecuperarObjetos(APedido: IBizPedidoCliente);
|
||
begin
|
||
RecuperarCliente(APedido);
|
||
end;
|
||
|
||
procedure TPedidosClienteController.RecuperarCliente(
|
||
APedido: IBizPedidoCliente);
|
||
begin
|
||
APedido._Cliente := (FClienteController.Buscar(APedido.ID_Cliente) as IBizCliente);
|
||
end;
|
||
|
||
function TPedidosClienteController.Existe(const ID: Integer): Boolean;
|
||
var
|
||
APedido : IBizPedidoCliente;
|
||
begin
|
||
try
|
||
APedido := Buscar(ID);
|
||
Result := Assigned(APedido) and (APedido.ID = ID);
|
||
finally
|
||
APedido := NIL;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.ExtraerSeleccionados(APedido: IBizPedidoCliente): IBizPedidoCliente;
|
||
var
|
||
ASeleccionados : IBizPedidoCliente;
|
||
begin
|
||
ASeleccionados := (Self.Buscar(ID_NULO) as IBizPedidoCliente);
|
||
CopyDataTableDA5(APedido.DataTable, ASeleccionados.DataTable, True);
|
||
Result := ASeleccionados;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.FiltrarAno(APedido: IBizPedidoCliente; AWhereDataTable: String; const Ano: String);
|
||
var
|
||
FechaIni: String;
|
||
FechaFin: String;
|
||
|
||
begin
|
||
APedido.DataTable.Where.Clear;
|
||
APedido.DataTable.Where.AddText(AWhereDataTable);
|
||
|
||
if (Ano <> 'Todos') then
|
||
begin
|
||
// Filtrar las pedidos actuales por empresa
|
||
FechaIni := '01.01.' + Ano;
|
||
FechaFin := '31.12.' + Ano;
|
||
with APedido.DataTable.Where do
|
||
begin
|
||
if NotEmpty then
|
||
AddOperator(opAND);
|
||
AddCondition(fld_PedidosClienteFECHA_PEDIDO, cMajorOrEqual, FechaIni);
|
||
AddOperator(opAND);
|
||
AddCondition(fld_PedidosClienteFECHA_PEDIDO, cLessOrEqual, FechaFin);
|
||
end;
|
||
end;
|
||
|
||
end;
|
||
|
||
procedure TPedidosClienteController.FiltrarEmpresa(APedido: IBizPedidoCliente);
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := False;
|
||
|
||
// Filtrar los pedidos actuales por empresa
|
||
with APedido.DataTable.DynamicWhere do
|
||
begin
|
||
// (ID_EMPRESA >= ID)
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteID_EMPRESA), NewConstant(AppFactuGES.EmpresaActiva.ID, datInteger), dboEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := False;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.SetClienteController(const Value: IClientesController);
|
||
begin
|
||
FClienteController := Value;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.SetDetallesController(const Value: IDetallesPedidoClienteController);
|
||
begin
|
||
FDetallesController := Value;
|
||
end;
|
||
|
||
function TPedidosClienteController.Guardar(APedido: IBizPedidoCliente): Boolean;
|
||
var
|
||
IDNuevo: Integer;
|
||
begin
|
||
Result := False;
|
||
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignada');
|
||
|
||
if not Assigned(FDetallesController) then
|
||
raise Exception.Create ('Controller detalles no asignado');
|
||
|
||
if ValidarPedido(APedido) then
|
||
begin
|
||
ShowHourglassCursor;
|
||
|
||
// Asegurarnos de que todos los importes est<73>n bien.
|
||
RecalcularImportes(APedido);
|
||
|
||
try
|
||
{ if (APedido.EsNuevo) then
|
||
IDNuevo := FDataModule.GetNextID(APedido.DataTable.LogicalName)
|
||
else
|
||
IDNuevo := APedido.ID;
|
||
}
|
||
// AsignarID(APedido, IDNuevo);
|
||
APedido.DataTable.ApplyUpdates;
|
||
|
||
Result := True;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.Localizar(APedidos: IBizPedidoCliente;
|
||
const ID: Integer): Boolean;
|
||
begin
|
||
Result := True;
|
||
ShowHourglassCursor;
|
||
try
|
||
with APedidos.DataTable do
|
||
begin
|
||
DisableControls;
|
||
First;
|
||
if not Locate(fld_PedidosClienteID, ID, []) then
|
||
Result := False;
|
||
EnableControls;
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.Nuevo: IBizPedidoCliente;
|
||
var
|
||
APedido : IBizPedidoCliente;
|
||
begin
|
||
APedido := FDataModule.NewItem;
|
||
FiltrarEmpresa(APedido);
|
||
APedido.DataTable.Active := True;
|
||
APedido.Insert;
|
||
Result := APedido;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.QuitarDireccionEnvio(
|
||
APedido: IBizPedidoCliente);
|
||
var
|
||
bEnEdicion : Boolean;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado (QuitarDireccionEnvio)');
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
bEnEdicion := (APedido.DataTable.State in dsEditModes);
|
||
if not bEnEdicion then
|
||
APedido.Edit;
|
||
|
||
ShowHourglassCursor;
|
||
APedido.Edit;
|
||
try
|
||
APedido.DataTable.FieldByName(fld_PedidosClienteCALLE).Clear;
|
||
APedido.DataTable.FieldByName(fld_PedidosClientePOBLACION).Clear;
|
||
APedido.DataTable.FieldByName(fld_PedidosClienteCODIGO_POSTAL).Clear;
|
||
APedido.DataTable.FieldByName(fld_PedidosClientePROVINCIA).Clear;
|
||
APedido.DataTable.FieldByName(fld_PedidosClienteTELEFONO).Clear;
|
||
APedido.DataTable.FieldByName(fld_PedidosClientePERSONA_CONTACTO).Clear;
|
||
|
||
if not bEnEdicion then
|
||
APedido.Post;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.Preview(APedido: IBizPedidoCliente; AllItems: Boolean = false);
|
||
var
|
||
AReportController : IPedidosClienteReportController;
|
||
ID_Pedidos: TIntegerList;
|
||
|
||
begin
|
||
AReportController := TPedidosClienteReportController.Create;
|
||
ID_Pedidos := TIntegerList.Create;
|
||
|
||
try
|
||
|
||
//Si deseamos previsualizar todos los items del objeto albaran
|
||
if AllItems then
|
||
begin
|
||
with APedido.DataTable do
|
||
begin
|
||
First;
|
||
while not EOF do
|
||
begin
|
||
ID_Pedidos.Add(APedido.ID);
|
||
Next;
|
||
end;
|
||
end;
|
||
end
|
||
//Solo previsualizamos el item seleccionado
|
||
else
|
||
ID_Pedidos.Add(APedido.ID);
|
||
|
||
AReportController.Preview(ID_Pedidos);
|
||
|
||
finally
|
||
AReportController := NIL;
|
||
FreeANDNIL(ID_Pedidos);
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.Print(APedido: IBizPedidoCliente; AllItems: Boolean = false): Boolean;
|
||
var
|
||
AReportController : IPedidosClienteReportController;
|
||
ID_Pedidos: TIntegerList;
|
||
|
||
begin
|
||
AReportController := TPedidosClienteReportController.Create;
|
||
ID_Pedidos := TIntegerList.Create;
|
||
|
||
try
|
||
|
||
//Si deseamos previsualizar todos los items del objeto albaran
|
||
if AllItems then
|
||
begin
|
||
with APedido.DataTable do
|
||
begin
|
||
First;
|
||
while not EOF do
|
||
begin
|
||
ID_Pedidos.Add(APedido.ID);
|
||
Next;
|
||
end;
|
||
end;
|
||
end
|
||
//Solo previsualizamos el item seleccionado
|
||
else
|
||
ID_Pedidos.Add(APedido.ID);
|
||
|
||
AReportController.Print(ID_Pedidos);
|
||
|
||
finally
|
||
AReportController := NIL;
|
||
FreeANDNIL(ID_Pedidos);
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.GenerarEmailPedido(APedido: IBizPedidoCliente): Boolean;
|
||
begin
|
||
{
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedidos no asignadas (GenerarEmailPedido)');
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
RecuperarCliente(APedido);
|
||
EnviarPedidoPorEMail(APedido, False, APedido.Cliente.EMAIL_ADMINISTRACION);
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
}
|
||
end;
|
||
|
||
function TPedidosClienteController.GetClienteController: IClientesController;
|
||
begin
|
||
Result := FClienteController;
|
||
end;
|
||
|
||
function TPedidosClienteController.GetDetallesController: IDetallesPedidoClienteController;
|
||
begin
|
||
Result := FDetallesController;
|
||
end;
|
||
|
||
end.
|