git-svn-id: https://192.168.0.254/svn/Proyectos.LuisLeon_FactuGES2/trunk@78 b2cfbe5a-eba1-4a0c-8b32-7feea0a119f2
1110 lines
34 KiB
ObjectPascal
1110 lines
34 KiB
ObjectPascal
unit uPedidosClienteController;
|
||
|
||
interface
|
||
|
||
|
||
uses
|
||
Classes, SysUtils, uDADataTable,
|
||
uControllerBase, uIDataModulePedidosCliente, uClientesController,
|
||
uDetallesPedidoClienteController, uBizPedidosCliente, uBizDetallesPedidoCliente;
|
||
|
||
type
|
||
IPedidosClienteController = interface(IControllerBase)
|
||
['{B8C72171-D232-4472-BA6F-18C15D300A8E}']
|
||
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;
|
||
|
||
function Buscar(const ID: Integer): IBizPedidoCliente;
|
||
function BuscarTodos: IBizPedidoCliente; overload;
|
||
function BuscarTodos(const AID_Cliente: Integer): IBizPedidoCliente; overload;
|
||
function BuscarPendientes : IBizPedidoCliente;
|
||
function BuscarPendientesPedir(const ID : Integer): IBizPedidoClienteArticulosPendientesPedirAProv;
|
||
|
||
// function BuscarSinFacturar : IBizPedidoCliente;
|
||
procedure Ver(APedido : IBizPedidoCliente);
|
||
procedure VerTodos(APedidos: IBizPedidoCliente;
|
||
const AVerModal : Boolean = False; const AWindowCaption: String = '';
|
||
const AHeaderText: String = '');
|
||
procedure VerDireccionEntrega(APedido : IBizPedidoCliente);
|
||
function Nuevo : IBizPedidoCliente;
|
||
procedure Anadir(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);
|
||
function Existe(const ID: Integer) : Boolean;
|
||
function Duplicar(APedido: IBizPedidoCliente): IBizPedidoCliente;
|
||
|
||
procedure Preview(APedido : IBizPedidoCliente; AllItems: Boolean = False;
|
||
const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True);
|
||
function Print(APedido : IBizPedidoCliente; AllItems: Boolean = False;
|
||
const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
|
||
|
||
procedure RecalcularImportes(APedido: IBizPedidoCliente);
|
||
function EsModificable(APedido : IBizPedidoCliente): Boolean;
|
||
function EsEliminable(APedido : IBizPedidoCliente): Boolean;
|
||
|
||
// function CambiarSituacion(APedido: IBizPedidoCliente; ASituacion : String; AFechaPagado: TDateTime = 0; DoPost : Boolean = True): Boolean; overload;
|
||
// function CambiarSituacion(APedido: IBizPedidoCliente): Boolean; overload;
|
||
|
||
function ExtraerSeleccionados(APedido: IBizPedidoCliente) : IBizPedidoCliente;
|
||
function ElegirPedidos(APedido: IBizPedidoCliente; AMensaje: String; AMultiSelect: Boolean): IBizPedidoCliente;
|
||
procedure RecuperarCliente(APedido: IBizPedidoCliente);
|
||
|
||
procedure GenerarEmailPedido(APedido: IBizPedidoCliente);
|
||
function EnviarEmailPedidos(APedidos : IBizPedidoCliente): Boolean;
|
||
function EnviarPedidoPorEMail(APedido: IBizPedidoCliente;
|
||
const AEnviarDirectamente: Boolean = True;
|
||
const ADireccionEMail: String = ''; const AAsuntoEMail: String = '';
|
||
const ATextoEMail: String = ''): Boolean;
|
||
|
||
function ArticulosPendientes(const ID : Integer): IBizPedidoClienteArticulosPend;
|
||
|
||
// Procedure AsignarIDObra(APedido: IBizPedidoCliente; const IDObra: Variant);
|
||
// Procedure AsignarIDAlmacen(APedido: IBizPedidoCliente; const IDAlmacen: Variant);
|
||
|
||
function DarListaAnosPedidos: TStringList;
|
||
procedure FiltrarAno(APedido: IBizPedidoCliente; ADynWhereDataTable: WideString; const Ano: String);
|
||
|
||
function Localizar(APedidos: IBizPedidoCliente; const ID : Integer): Boolean;
|
||
end;
|
||
|
||
TPedidosClienteController = class(TControllerBase, IPedidosClienteController)
|
||
private
|
||
procedure _AnadirMarcaEnvioCorreo(APedido: IBizPedidoCliente);
|
||
|
||
protected
|
||
FDataModule : IDataModulePedidosCliente;
|
||
FClienteController : IClientesController;
|
||
FDetallesController : IDetallesPedidoClienteController;
|
||
|
||
function GetClienteController: IClientesController;
|
||
procedure SetClienteController(const Value: IClientesController);
|
||
procedure RecuperarCliente(APedido : IBizPedidoCliente);
|
||
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;
|
||
|
||
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;
|
||
procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
|
||
|
||
constructor Create; override;
|
||
destructor Destroy; override;
|
||
|
||
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;
|
||
procedure Anadir(APedido : IBizPedidoCliente);
|
||
function Buscar(const ID: Integer): IBizPedidoCliente;
|
||
function BuscarTodos: IBizPedidoCliente; overload;
|
||
function BuscarTodos(const AID_Cliente: Integer): IBizPedidoCliente; overload;
|
||
function BuscarPendientes : IBizPedidoCliente;
|
||
function BuscarPendientesPedir(const ID : Integer): IBizPedidoClienteArticulosPendientesPedirAProv;
|
||
|
||
// function BuscarSinFacturar : IBizPedidoCliente;
|
||
function Nuevo : IBizPedidoCliente;
|
||
procedure Ver(APedido : IBizPedidoCliente);
|
||
procedure VerTodos(APedidos: IBizPedidoCliente;
|
||
const AVerModal : Boolean = False; const AWindowCaption: String = '';
|
||
const AHeaderText: String = '');
|
||
|
||
procedure VerDireccionEntrega(APedido : IBizPedidoCliente);
|
||
function Duplicar(APedido: IBizPedidoCliente): IBizPedidoCliente;
|
||
|
||
procedure Preview(APedido : IBizPedidoCliente; AllItems: Boolean = False;
|
||
const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True);
|
||
function Print(APedido : IBizPedidoCliente; AllItems: Boolean = False;
|
||
const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
|
||
|
||
procedure RecalcularImportes(APedido: IBizPedidoCliente);
|
||
function EsModificable(APedido : IBizPedidoCliente): Boolean;
|
||
function EsEliminable(APedido : IBizPedidoCliente): Boolean;
|
||
|
||
// function CambiarSituacion(APedido: IBizPedidoCliente; ASituacion : String; AFechaRecepcion: TDateTime = 0; DoPost : Boolean = True): Boolean; overload;
|
||
// function CambiarSituacion(APedido: IBizPedidoCliente): Boolean; overload;
|
||
|
||
function ExtraerSeleccionados(APedido: IBizPedidoCliente) : IBizPedidoCliente;
|
||
function ElegirPedidos(APedido: IBizPedidoCliente; AMensaje: String; AMultiSelect: Boolean): IBizPedidoCliente;
|
||
|
||
procedure GenerarEmailPedido(APedido: IBizPedidoCliente);
|
||
function EnviarEmailPedidos(APedidos : IBizPedidoCliente): Boolean;
|
||
function EnviarPedidoPorEMail(APedido: IBizPedidoCliente;
|
||
const AEnviarDirectamente: Boolean = True;
|
||
const ADireccionEMail: String = ''; const AAsuntoEMail: String = '';
|
||
const ATextoEMail: String = ''): Boolean;
|
||
|
||
// Procedure AsignarIDObra(APedido: IBizPedidoCliente; const IDObra: Variant);
|
||
// Procedure AsignarIDAlmacen(APedido: IBizPedidoCliente; const IDAlmacen: Variant);
|
||
function ArticulosPendientes(const ID : Integer): IBizPedidoClienteArticulosPend;
|
||
|
||
function DarListaAnosPedidos: TStringList;
|
||
procedure FiltrarAno(APedido: IBizPedidoCliente; ADynWhereDataTable: WideString; const Ano: String);
|
||
function Localizar(APedidos: IBizPedidoCliente; const ID : Integer): Boolean;
|
||
end;
|
||
|
||
implementation
|
||
|
||
uses
|
||
uROTypes, Controls, cxControls, DB, uEditorRegistryUtils, uEditorPreview, DateUtils,
|
||
uIEditorPedidosCliente, uDataModulePedidosCliente, uFactuGES_App,
|
||
uBizContactos, uDataTableUtils, uDataModuleUsuarios, Forms, uDataModuleRegistroCorreos,
|
||
schPedidosClienteClient_Intf, uDAInterfaces, uPedidosClienteReportController,
|
||
uDateUtils, uIEditorPedidoCliente, uIEditorElegirPedidosCliente,
|
||
Dialogs, uIEditorDireccionEntregaPedidoCliente, uIDialogListaPedidosClienteEnvioEMail,
|
||
Variants, uSistemaFunc, uEMailUtils, uDialogElegirEMail, uIntegerListUtils, uStringsUtils;
|
||
|
||
{ TPedidosClienteController }
|
||
|
||
procedure TPedidosClienteController.Anadir(APedido: IBizPedidoCliente);
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado (Anadir)');
|
||
|
||
APedido.Insert;
|
||
end;
|
||
|
||
function TPedidosClienteController.ArticulosPendientes(const ID: Integer): IBizPedidoClienteArticulosPend;
|
||
begin
|
||
Result := FDataModule.GetArticulosPendientes(ID);
|
||
end;
|
||
|
||
procedure TPedidosClienteController.AsignarDataModule;
|
||
begin
|
||
FDataModule := TDataModulePedidosCliente.Create(Nil);
|
||
end;
|
||
|
||
{
|
||
procedure TPedidosClienteController.AsignarIDAlmacen(APedido: IBizPedidoCliente; const IDAlmacen: Variant);
|
||
begin
|
||
if Assigned(APedido) then
|
||
begin
|
||
if not APedido.DataTable.Editing then
|
||
APedido.DataTable.Edit;
|
||
|
||
if EsCadenaVacia(IDAlmacen) then
|
||
APedido.ID_ALMACENIsNull := true
|
||
else
|
||
APedido.ID_ALMACEN := IDAlmacen;
|
||
|
||
APedido.DataTable.Post;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.AsignarIDObra(APedido: IBizPedidoCliente; const IDObra: Variant);
|
||
begin
|
||
if Assigned(APedido) then
|
||
begin
|
||
if not APedido.DataTable.Editing then
|
||
APedido.DataTable.Edit;
|
||
|
||
if EsCadenaVacia(IDObra) then
|
||
APedido.ID_OBRAIsNull := true
|
||
else
|
||
APedido.ID_OBRA := IDObra;
|
||
|
||
APedido.DataTable.Post;
|
||
end;
|
||
end;
|
||
}
|
||
|
||
function TPedidosClienteController.Buscar(const ID: Integer): IBizPedidoCliente;
|
||
begin
|
||
Result := FDataModule.GetItem(ID);
|
||
FiltrarEmpresa(Result);
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarPendientes: IBizPedidoCliente;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
|
||
// Filtrar los pedidos pendientes de recepcion
|
||
with Result.DataTable.DynamicWhere do
|
||
begin
|
||
// (SITUACION <> SITUACION_PEDIDO_SERVIDO)
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteSITUACION), NewConstant(SITUACION_PEDIDO_SERVIDO, datString), dboNotEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarPendientesPedir(const ID: Integer): IBizPedidoClienteArticulosPendientesPedirAProv;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := FDataModule.GetArticulosPendientesPedirAProv(ID);
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
{function TPedidosClienteController.BuscarSinFacturar: IBizPedidoCliente;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
|
||
with Result.DataTable.DynamicWhere do
|
||
begin
|
||
// (ID_FACTURA = NULL)
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteID_FACTURA), NewNull(), dboEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;}
|
||
|
||
function TPedidosClienteController.BuscarTodos(
|
||
const AID_Cliente: Integer): IBizPedidoCliente;
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
begin
|
||
ShowHourglassCursor;
|
||
try
|
||
Result := BuscarTodos;
|
||
|
||
with Result.DataTable.DynamicWhere do
|
||
begin
|
||
// ID_PROVEEDOR
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteID_CLIENTE),
|
||
NewConstant(AID_Cliente, datInteger), dboEqual);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
|
||
end;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.BuscarTodos: IBizPedidoCliente;
|
||
begin
|
||
Result := FDataModule.GetItems;
|
||
FiltrarEmpresa(Result);
|
||
end;
|
||
|
||
constructor TPedidosClienteController.Create;
|
||
begin
|
||
inherited;
|
||
AsignarDataModule;
|
||
FClienteController := TClientesController.Create;
|
||
|
||
FDetallesController := TDetallesPedidoClienteController.Create;
|
||
FDetallesController.addObservador(Self);
|
||
// Self.addSujeto(FDetallesController);
|
||
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 asignado');
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
if (APedido.State in dsEditModes) then
|
||
APedido.Cancel;
|
||
|
||
APedido.DataTable.CancelUpdates;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
{
|
||
function TPedidosClienteController.CambiarSituacion(APedido: IBizPedidoCliente; ASituacion: String; AFechaRecepcion: TDateTime;
|
||
DoPost: Boolean): Boolean;
|
||
begin
|
||
Result := False;
|
||
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignado (CambiarSituacion)');
|
||
|
||
if not APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
// Validar la situaci<63>n del pedido
|
||
if ValidarSituacion(ASituacion, APedido.FECHA_PEDIDO, AFechaRecepcion) then
|
||
begin
|
||
ShowHourglassCursor;
|
||
APedido.DataTable.DisableControls;
|
||
try
|
||
APedido.Edit;
|
||
APedido.SITUACION := ASituacion;
|
||
if AFechaRecepcion <> APedido.FECHA_ENTREGA then
|
||
APedido.FECHA_ENTREGA := AFechaRecepcion;
|
||
APedido.Post;
|
||
if DoPost then
|
||
Guardar(APedido);
|
||
Result := True;
|
||
finally
|
||
APedido.DataTable.EnableControls;
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.CambiarSituacion(APedido: IBizPedidoCliente): Boolean;
|
||
var
|
||
AEditor : IEditorSituacionPedidoCliente;
|
||
begin
|
||
Result := False;
|
||
AEditor := NIL;
|
||
|
||
RecuperarCliente(APedido);
|
||
CreateEditor('EditorSituacionPedidoCliente', IEditorSituacionPedidoCliente, AEditor);
|
||
if Assigned(AEditor) then
|
||
try
|
||
AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
||
AEditor.PedidoCliente := APedido;
|
||
AEditor.ShowModal;
|
||
Result := True;
|
||
finally
|
||
AEditor.Release;
|
||
AEditor := NIL;
|
||
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
|
||
REFERENCIA := '';
|
||
ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
|
||
USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
||
FECHA_PEDIDO := DateOf(Now);
|
||
SITUACION := SITUACION_PEDIDO_PENDIENTE;
|
||
FECHA_PREVISTA_ENVIO := 0;
|
||
INCIDENCIAS_ACTIVAS := 0;
|
||
// INCIDENCIAS := Nil;
|
||
SetFieldNull(DataTable, fld_PedidosClienteREF_TIENDA_WEB);
|
||
end;
|
||
Result.Post;
|
||
finally
|
||
HideHourglassCursor;
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.ValidarPedido(
|
||
APedido: IBizPedidoCliente): Boolean;
|
||
begin
|
||
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;
|
||
|
||
if (APedido.ID_CLIENTE < 0) or (APedido.ID_CLIENTE = 0) 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');
|
||
|
||
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('Debe indicar al menos un concepto en el contenido del pedido');
|
||
|
||
|
||
{ 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;
|
||
|
||
RecuperarObjetos(APedido);
|
||
CreateEditor('EditorPedidoCliente', IEditorPedidoCliente, AEditor);
|
||
|
||
if Assigned(AEditor) then
|
||
try
|
||
AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
|
||
AEditor.Pedido := APedido;
|
||
|
||
//MODO CONSULTAR
|
||
if not EsModificable(APedido) then
|
||
begin
|
||
SetDataTableReadOnly(APedido.DataTable, True);
|
||
AEditor.ReadOnly := True;
|
||
end;
|
||
|
||
AEditor.ShowModal;
|
||
|
||
//MODO CONSULTAR (Se deja la tabla como estaba)
|
||
if AEditor.ReadOnly then
|
||
SetDataTableReadOnly(APedido.DataTable, False);
|
||
finally
|
||
AEditor.Release;
|
||
AEditor := NIL;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.VerTodos(APedidos: IBizPedidoCliente;
|
||
const AVerModal : Boolean = False; const AWindowCaption: String = '';
|
||
const 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;
|
||
|
||
procedure TPedidosClienteController.VerDireccionEntrega(
|
||
APedido: IBizPedidoCliente);
|
||
var
|
||
AEditor : IEditorDireccionEntregaPedidoCliente;
|
||
begin
|
||
AEditor := NIL;
|
||
|
||
//RecuperarObjetos(APedido); <- No descomentar. No hace falta
|
||
CreateEditor('EditorDireccionEntregaPedidoCliente', IEditorDireccionEntregaPedidoCliente, AEditor);
|
||
if Assigned(AEditor) then
|
||
try
|
||
AEditor.Pedido := APedido;
|
||
AEditor.ShowModal;
|
||
finally
|
||
AEditor.Release;
|
||
AEditor := NIL;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController._AnadirMarcaEnvioCorreo(APedido: IBizPedidoCliente);
|
||
var
|
||
ASoloLectura : Boolean;
|
||
begin
|
||
if not Assigned(APedido) then
|
||
raise Exception.Create ('Pedido no asignada (_AnadirMarcaEnvioCorreo)');
|
||
|
||
if APedido.DataTable.Active then
|
||
APedido.DataTable.Active := True;
|
||
|
||
if AnadirMarcaEnvioCorreo(nme_PedidosCliente, APedido.ID) then
|
||
begin
|
||
DesconectarTabla(APedido.DataTable);
|
||
try
|
||
ASoloLectura := APedido.DataTable.ReadOnly;
|
||
if ASoloLectura then
|
||
SetDataTableReadOnly(APedido.DataTable, False);
|
||
try
|
||
APedido.Edit;
|
||
APedido.NUM_CORREOS := APedido.NUM_CORREOS + 1;
|
||
APedido.Post;
|
||
finally
|
||
if ASoloLectura then
|
||
SetDataTableReadOnly(APedido.DataTable, True);
|
||
end;
|
||
finally
|
||
ConectarTabla(APedido.DataTable);
|
||
end;
|
||
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);
|
||
if Assigned(AEditor) then
|
||
try
|
||
AEditor.Controller := Self;
|
||
AEditor.Pedidos := APedido;
|
||
AEditor.MultiSelect := AMultiSelect;
|
||
AEditor.Mensaje := AMensaje;
|
||
if IsPositiveResult(AEditor.ShowModal) then
|
||
Result := AEditor.PedidosClienteSeleccionados;
|
||
finally
|
||
AEditor.Release;
|
||
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 : IDialogListaPedidosClienteEnvioEMail;
|
||
ARespuesta : Integer;
|
||
begin
|
||
ADialog := NIL;
|
||
|
||
if not Assigned(APedidos) then
|
||
raise Exception.Create ('Pedidos no asignadas (EnviarPedidosPorEMail)');
|
||
|
||
if APedidos.DataTable.Active then
|
||
APedidos.DataTable.Active := True;
|
||
|
||
ShowHourglassCursor;
|
||
try
|
||
CreateEditor('DialogListaPedidosClienteEnvioEMail', IDialogListaPedidosClienteEnvioEMail, 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 (EnviarPedidoPorEMail)');
|
||
|
||
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_ADMINISTRACION) then
|
||
AListaEmail.Add(APedido.Cliente.EMAIL_ADMINISTRACION);
|
||
|
||
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);
|
||
if Result then
|
||
_AnadirMarcaEnvioCorreo(APedido);
|
||
finally
|
||
SysUtils.DeleteFile(AFicheroTMP);
|
||
AReportController := NIL;
|
||
HideHourglassCursor;
|
||
Application.ProcessMessages;
|
||
end;
|
||
end;
|
||
|
||
procedure TPedidosClienteController.GenerarEmailPedido(
|
||
APedido: IBizPedidoCliente);
|
||
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.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; ADynWhereDataTable: WideString; const Ano: String);
|
||
var
|
||
Condicion: TDAWhereExpression;
|
||
FechaIni: String;
|
||
FechaFin: String;
|
||
|
||
begin
|
||
APedido.DataTable.DynamicWhere.Clear;
|
||
APedido.DataTable.DynamicWhere.Xml := ADynWhereDataTable;
|
||
|
||
if (Ano <> 'Todos') then
|
||
begin
|
||
// Filtrar las facturas actuales por empresa
|
||
FechaIni := '01/01/' + Ano;
|
||
FechaFin := '31/12/' + Ano;
|
||
with APedido.DataTable.DynamicWhere do
|
||
begin
|
||
// (FECHA_INICIO between FECHA_FIN)
|
||
Condicion := NewBinaryExpression(NewField('', fld_PedidosClienteFECHA_PEDIDO), NewConstant(FechaIni, datString), dboGreaterOrEqual);
|
||
Condicion := NewBinaryExpression(NewBinaryExpression(NewField('', fld_PedidosClienteFECHA_PEDIDO), NewConstant(FechaFin, datString), dboLessOrEqual), Condicion, dboAnd);
|
||
|
||
if IsEmpty then
|
||
Expression := Condicion
|
||
else
|
||
Expression := NewBinaryExpression(Condicion, Expression, dboAnd);
|
||
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;
|
||
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;
|
||
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
|
||
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.Preview(APedido: IBizPedidoCliente; AllItems: Boolean = false;
|
||
const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True);
|
||
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, VerPrecios, VerRefProveedor);
|
||
finally
|
||
AReportController := NIL;
|
||
FreeANDNIL(ID_Pedidos)
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.Print(APedido: IBizPedidoCliente; AllItems: Boolean = false;
|
||
const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
|
||
var
|
||
AReportController : IPedidosClienteReportController;
|
||
ID_Pedidos: TIntegerList;
|
||
begin
|
||
Result := False;
|
||
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);
|
||
|
||
Result := AReportController.Print(ID_Pedidos, VerPrecios, VerRefProveedor);
|
||
finally
|
||
AReportController := NIL;
|
||
FreeANDNIL(ID_Pedidos)
|
||
end;
|
||
end;
|
||
|
||
function TPedidosClienteController.GetClienteController: IClientesController;
|
||
begin
|
||
Result := FClienteController;
|
||
end;
|
||
|
||
function TPedidosClienteController.GetDetallesController: IDetallesPedidoClienteController;
|
||
begin
|
||
Result := FDetallesController;
|
||
end;
|
||
|
||
end.
|