290 lines
9.7 KiB
ObjectPascal
290 lines
9.7 KiB
ObjectPascal
|
|
unit uGenerarRecibosCliUtils;
|
|||
|
|
|
|||
|
|
interface
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
Windows, SysUtils, Classes, pngimage, JSDialog,
|
|||
|
|
uBizContratosCliente, uBizRecibosCliente;
|
|||
|
|
|
|||
|
|
type
|
|||
|
|
TdmGenerarRecibosCli = class(TDataModule)
|
|||
|
|
JsListaRecibosGenerados: TJSDialog;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function GenerarReciboCli(const IDContrato : Integer) : Boolean;
|
|||
|
|
// function GenerarFacturaCli(AContrato : IBizContratoCliente; var AFactura: IBizFacturaCliente) : Boolean; overload;
|
|||
|
|
// function ElegirPedidoYGenerarAlbaranCli(var IDAlbaran: Integer) : Boolean;
|
|||
|
|
|
|||
|
|
implementation
|
|||
|
|
|
|||
|
|
{$R *.dfm}
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
uDialogUtils,
|
|||
|
|
//uBizDetallesContratoCliente, uBizDetallesFacturaCliente,
|
|||
|
|
uContratosClienteController, uFacturasClienteController, uRecibosClienteController,
|
|||
|
|
uClientesController,
|
|||
|
|
uDetallesFacturaClienteController, uControllerDetallesBase,
|
|||
|
|
uBizContactos, schFacturasClienteClient_Intf,
|
|||
|
|
schContratosClienteClient_Intf, uBizDetallesPresupuestoCliente;
|
|||
|
|
|
|||
|
|
var
|
|||
|
|
dmGenerarRecibosCli: TdmGenerarRecibosCli;
|
|||
|
|
AContratosClienteController : IContratosClienteController;
|
|||
|
|
ARecibosClienteController : IRecibosClienteController;
|
|||
|
|
AFacturasClienteController : IFacturasClienteController;
|
|||
|
|
AClientesController : IClientesController;
|
|||
|
|
|
|||
|
|
{ M<>todos auxiliares }
|
|||
|
|
{procedure CopiarContratoAFactura(AContrato: IBizContratoCliente; AFactura : IBizFacturaCliente);
|
|||
|
|
begin
|
|||
|
|
if not Assigned(AFactura) then
|
|||
|
|
raise Exception.Create ('Factura no asignado (CopiarContratoAFactura)');
|
|||
|
|
|
|||
|
|
if not Assigned(AContrato) then
|
|||
|
|
raise Exception.Create ('Contrato no asignado (CopiarContratoAFactura)');
|
|||
|
|
|
|||
|
|
if not AContrato.DataTable.Active then
|
|||
|
|
AContrato.DataTable.Active := True;
|
|||
|
|
|
|||
|
|
AContratosClienteController.RecuperarCliente(AContrato);
|
|||
|
|
AFactura.Cliente := AContrato.Cliente;
|
|||
|
|
|
|||
|
|
AFactura.NIF_CIF := AContrato.NIF_CIF;
|
|||
|
|
AFactura.NOMBRE := AContrato.NOMBRE;
|
|||
|
|
AFactura.CALLE := AContrato.CALLE;
|
|||
|
|
AFactura.POBLACION := AContrato.POBLACION;
|
|||
|
|
AFactura.PROVINCIA := AContrato.PROVINCIA;
|
|||
|
|
AFactura.CODIGO_POSTAL := AContrato.CODIGO_POSTAL;
|
|||
|
|
|
|||
|
|
|
|||
|
|
// AFactura.PERSONA_CONTACTO := AContrato.PERSONA_CONTACTO;
|
|||
|
|
AFactura.DataTable.FieldByName(fld_FacturasClienteOBSERVACIONES).AsVariant := AContrato.DataTable.FieldByName(fld_ContratosClienteOBSERVACIONES).AsVariant;
|
|||
|
|
AFactura.IMPORTE_NETO := AContrato.IMPORTE_NETO;
|
|||
|
|
AFactura.IMPORTE_PORTE := AContrato.IMPORTE_PORTE;
|
|||
|
|
AFactura.DESCUENTO := AContrato.DESCUENTO;
|
|||
|
|
AFactura.IMPORTE_DESCUENTO := AContrato.IMPORTE_DESCUENTO;
|
|||
|
|
AFactura.BASE_IMPONIBLE := AContrato.BASE_IMPONIBLE;
|
|||
|
|
AFactura.ID_TIPO_IVA := AContrato.ID_TIPO_IVA;
|
|||
|
|
AFactura.IVA := AContrato.IVA;
|
|||
|
|
AFactura.IMPORTE_IVA := AContrato.IMPORTE_IVA;
|
|||
|
|
AFactura.IMPORTE_TOTAL := AContrato.IMPORTE_TOTAL;
|
|||
|
|
// AFactura.ID_TIENDA := AContrato.ID_TIENDA;
|
|||
|
|
// AFactura.ID_VENDEDOR := AContrato.ID_VENDEDOR;
|
|||
|
|
AFactura.RECARGO_EQUIVALENCIA := AContrato.RECARGO_EQUIVALENCIA;
|
|||
|
|
AFactura.RE := AContrato.RE;
|
|||
|
|
AFactura.IMPORTE_RE := AContrato.IMPORTE_RE;
|
|||
|
|
AFactura.ID_CONTRATO := AContrato.ID;
|
|||
|
|
AFactura.TIPO_FACTURA := AContrato.TIPO_CONTRATO;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure CopiarDetallesAFactura(
|
|||
|
|
AContrato: IBizContratoCliente;
|
|||
|
|
AFactura: IBizFacturaCliente;
|
|||
|
|
AArticulos: IBizDetallesContratoCliente);
|
|||
|
|
var
|
|||
|
|
i : integer;
|
|||
|
|
ADetalles : IBizDetallesFacturaCliente;
|
|||
|
|
ADetallesController : IDetallesFacturaClienteController;
|
|||
|
|
begin
|
|||
|
|
if not Assigned(AFactura) then
|
|||
|
|
raise Exception.Create ('Factura no asignada (CopiarDetallesAFactura)');
|
|||
|
|
|
|||
|
|
if not Assigned(AContrato) then
|
|||
|
|
raise Exception.Create ('Contrato no asignado (CopiarDetallesAFactura)');
|
|||
|
|
|
|||
|
|
if not Assigned(AArticulos) then
|
|||
|
|
raise Exception.Create ('Art<72>culos no asignado (CopiarDetallesAFatura)');
|
|||
|
|
|
|||
|
|
if not AArticulos.DataTable.Active then
|
|||
|
|
AArticulos.DataTable.Active := True;
|
|||
|
|
|
|||
|
|
// El contrato tiene que venir ya abierto y posicionado donde hay que copiar
|
|||
|
|
|
|||
|
|
ADetalles := AFactura.Detalles;
|
|||
|
|
ADetallesController := TDetallesFacturaClienteController.Create;
|
|||
|
|
try
|
|||
|
|
//OJO IMPORTANTE
|
|||
|
|
//Siempre que vayamos a trabajar con los detalles debemos hacer un beginupdate de los mismos y un endupdate para
|
|||
|
|
//obligarle siempre a recalcular los detalles una sola vez
|
|||
|
|
ADetallesController.BeginUpdate(ADetalles);
|
|||
|
|
|
|||
|
|
AArticulos.DataTable.First;
|
|||
|
|
for i := 0 to AArticulos.DataTable.RecordCount - 1 do
|
|||
|
|
begin
|
|||
|
|
ADetallesController.Add(ADetalles, AArticulos.TIPO_DETALLE);
|
|||
|
|
ADetalles.Edit;
|
|||
|
|
ADetalles.REFERENCIA := AArticulos.REFERENCIA;
|
|||
|
|
ADetalles.ID_ARTICULO := AArticulos.ID_ARTICULO;
|
|||
|
|
if (AArticulos.PROPIEDAD <> '') then
|
|||
|
|
ADetalles.CONCEPTO := AArticulos.PROPIEDAD + ': '+ AArticulos.CONCEPTO
|
|||
|
|
else
|
|||
|
|
ADetalles.CONCEPTO := AArticulos.CONCEPTO;
|
|||
|
|
|
|||
|
|
if not AArticulos.CANTIDADIsNull then
|
|||
|
|
ADetalles.CANTIDAD := AArticulos.CANTIDAD;
|
|||
|
|
if not AArticulos.IMPORTE_UNIDADIsNull then
|
|||
|
|
ADetalles.IMPORTE_UNIDAD := AArticulos.IMPORTE_UNIDAD;
|
|||
|
|
if not AArticulos.IMPORTE_TOTALIsNull then
|
|||
|
|
ADetalles.IMPORTE_TOTAL := AArticulos.IMPORTE_TOTAL;
|
|||
|
|
if not AArticulos.DESCUENTOIsNull then
|
|||
|
|
ADetalles.DESCUENTO := AArticulos.DESCUENTO;
|
|||
|
|
if not AArticulos.IMPORTE_PORTEIsNull then
|
|||
|
|
ADetalles.IMPORTE_PORTE := AArticulos.IMPORTE_PORTE;
|
|||
|
|
ADetalles.VISIBLE := AArticulos.VISIBLE;
|
|||
|
|
ADetalles.REFERENCIA_PROVEEDOR := AArticulos.REFERENCIA_PROVEEDOR;
|
|||
|
|
ADetalles.Post;
|
|||
|
|
AArticulos.Next;
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
ADetallesController.EndUpdate(ADetalles);
|
|||
|
|
ADetallesController := NIL;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
procedure Inicializar;
|
|||
|
|
begin
|
|||
|
|
dmGenerarRecibosCli := TdmGenerarRecibosCli.Create(nil);
|
|||
|
|
AContratosClienteController := TContratosClienteController.Create;
|
|||
|
|
ARecibosClienteController := TRecibosClienteController.Create;
|
|||
|
|
AFacturasClienteController := TFacturasClienteController.Create;
|
|||
|
|
AClientesController := TClientesController.Create;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
procedure Finalizar;
|
|||
|
|
begin
|
|||
|
|
FreeAndNIL(dmGenerarRecibosCli);
|
|||
|
|
AContratosClienteController := nil;
|
|||
|
|
ARecibosClienteController := nil;
|
|||
|
|
AFacturasClienteController := nil;
|
|||
|
|
AClientesController := nil;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
|
|||
|
|
function GenerarReciboCli(const IDContrato : Integer) : Boolean;
|
|||
|
|
var
|
|||
|
|
AContrato : IBizContratoCliente;
|
|||
|
|
ARecibo : IBizRecibosCliente;
|
|||
|
|
begin
|
|||
|
|
Result := False;
|
|||
|
|
ARecibo := Nil;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
if not Assigned(AContratosClienteController) then
|
|||
|
|
Inicializar;
|
|||
|
|
|
|||
|
|
AContrato := AContratosClienteController.Buscar(IDContrato);
|
|||
|
|
if Assigned(AContrato) then
|
|||
|
|
begin
|
|||
|
|
if not AContrato.DataTable.Active then
|
|||
|
|
AContrato.DataTable.Active := True;
|
|||
|
|
|
|||
|
|
//
|
|||
|
|
//Comprobamos que el contrato no tenga facturas asociadas y si las tiene decir que no se pueden a<>adir cobros por esta via
|
|||
|
|
//
|
|||
|
|
|
|||
|
|
ARecibo := ARecibosClienteController.Nuevo;
|
|||
|
|
ARecibo.ID_CONTRATO := AContrato.ID;
|
|||
|
|
AContratosClienteController.RecuperarCliente(AContrato);
|
|||
|
|
ARecibo.Cliente := AContrato.Cliente;
|
|||
|
|
ARecibo.NIF_CIF := AContrato.NIF_CIF;
|
|||
|
|
ARecibo.NOMBRE := AContrato.NOMBRE;
|
|||
|
|
ARecibo.CALLE := AContrato.CALLE;
|
|||
|
|
ARecibo.POBLACION := AContrato.POBLACION;
|
|||
|
|
ARecibo.PROVINCIA := AContrato.PROVINCIA;
|
|||
|
|
ARecibo.CODIGO_POSTAL := AContrato.CODIGO_POSTAL;
|
|||
|
|
ARecibo.IMPORTE := AContrato.IMPORTE_TOTAL;
|
|||
|
|
ARecibo.DESCRIPCION := 'Pago a cuenta de contrato ' + AContrato.REFERENCIA;
|
|||
|
|
|
|||
|
|
ARecibosClienteController.Ver(ARecibo);
|
|||
|
|
Result := true; // GenerarFacturaCli(AContrato, AFactura);
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
if Assigned(AContratosClienteController) then
|
|||
|
|
Finalizar;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
function GenerarFacturaCli(AContrato : IBizContratoCliente; var AFactura: IBizFacturaCliente) : Boolean; overload;
|
|||
|
|
var
|
|||
|
|
ARespuesta : Integer;
|
|||
|
|
begin
|
|||
|
|
AFactura := NIL;
|
|||
|
|
|
|||
|
|
if not Assigned(AContrato) then
|
|||
|
|
raise Exception.Create('Contrato de cliente no asignado (GenerarContratoCli)');
|
|||
|
|
|
|||
|
|
if not AContrato.DataTable.Active then
|
|||
|
|
AContrato.DataTable.Active := True;
|
|||
|
|
|
|||
|
|
if not Assigned(AContratosClienteController) then
|
|||
|
|
Inicializar;
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
AFactura := AFacturasClienteController.Nuevo;
|
|||
|
|
CopiarContratoAFactura(AContrato, AFactura);
|
|||
|
|
CopiarDetallesAFactura(AContrato, AFactura, AContrato.Detalles);
|
|||
|
|
|
|||
|
|
//Sustituir por if de guardar
|
|||
|
|
if AFacturasClienteController.Guardar(AFactura) then
|
|||
|
|
begin
|
|||
|
|
// Asocio el contrato al presupuesto
|
|||
|
|
with dmGenerarFacturasCli.JsListaFacturasGenerados do
|
|||
|
|
begin
|
|||
|
|
Instruction.Text := 'Se ha generado la factura';
|
|||
|
|
|
|||
|
|
Content.Clear;
|
|||
|
|
Content.Add(Format('Se ha generado correctamente la factura %s a partir del contrato de cliente' + #10#13, [AFactura.REFERENCIA]));
|
|||
|
|
Execute;
|
|||
|
|
|
|||
|
|
ARespuesta := CustomButtonResult;
|
|||
|
|
case ARespuesta of
|
|||
|
|
100 : begin
|
|||
|
|
// Ver el albar<61>n
|
|||
|
|
AFacturasClienteController.Ver(AFactura);
|
|||
|
|
end;
|
|||
|
|
200 : // Continuar;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
Result := True;
|
|||
|
|
finally
|
|||
|
|
if Assigned(AContratosClienteController) then
|
|||
|
|
Finalizar;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
{
|
|||
|
|
function ElegirPedidoYGenerarAlbaranCli(var IDAlbaran: Integer) : Boolean; overload;
|
|||
|
|
var
|
|||
|
|
APedido : IBizPedidoCliente;
|
|||
|
|
AAlbaran : IBizAlbaranCliente;
|
|||
|
|
begin
|
|||
|
|
Result := False;
|
|||
|
|
IDAlbaran := -1;
|
|||
|
|
|
|||
|
|
AAlbaran := NIL;
|
|||
|
|
try
|
|||
|
|
if not Assigned(APedidosClienteController) then
|
|||
|
|
Inicializar;
|
|||
|
|
|
|||
|
|
APedido := APedidosClienteController.ElegirPedidos(APedidosClienteController.BuscarPendientes,
|
|||
|
|
'Elija el pedido de cliente que desea utilizar para dar de alta el albar<61>n de cliente.'
|
|||
|
|
, False);
|
|||
|
|
|
|||
|
|
if Assigned(APedido) then
|
|||
|
|
begin
|
|||
|
|
Result := GenerarAlbaranCli(APedido, AAlbaran);
|
|||
|
|
if Result then
|
|||
|
|
IDAlbaran := AAlbaran.ID;
|
|||
|
|
end;
|
|||
|
|
finally
|
|||
|
|
if Assigned(APedidosClienteController) then
|
|||
|
|
Finalizar;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
}
|
|||
|
|
end.
|