This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
LuisLeon_FactuGES2/Source/Modulos/Relaciones/Pedidos de cliente - Facturas proforma/uGenerarFacturasProformaUtils.pas
2013-12-12 17:42:50 +00:00

262 lines
8.4 KiB
ObjectPascal

unit uGenerarFacturasProformaUtils;
interface
uses
Windows, SysUtils, Classes, pngimage, JSDialog,
uBizPedidosCliente, uBizFacturasProforma;
type
TdmGenerarFacturasProforma = class(TDataModule)
JsListaFacturasProformaGeneradas: TJSDialog;
end;
function GenerarFacturaProforma(const IDPedido : Integer) : Boolean; overload;
function GenerarFacturaProforma(APedido : IBizPedidoCliente; var AFactura: IBizFacturaProforma) : Boolean; overload;
// function ElegirPedidoYGenerarAlbaranCli(var IDAlbaran: Integer) : Boolean;
implementation
{$R *.dfm}
uses
uDialogUtils, uBizDetallesPedidoCliente, uBizDetallesFacturaProforma,
uPedidosClienteController, uFacturasProformaController, uClientesController,
uDetallesFacturaProformaController, uControllerDetallesBase,
uBizContactos, schPedidosClienteClient_Intf,
schFacturasProformaClient_Intf, uDataTableUtils;
var
dmGenerarFacturasProforma: TdmGenerarFacturasProforma;
APedidosClienteController : IPedidosClienteController;
AFacturasProformaController : IFacturasProformaController;
ADetallesFacturaProformaController : IDetallesFacturaProformaController;
AClientesController : IClientesController;
{ Métodos auxiliares }
procedure CopiarPedidoAFactura(APedido: IBizPedidoCliente; AFactura : IBizFacturaProforma);
begin
if not Assigned(AFactura) then
raise Exception.Create ('Factura no asignada (CopiarPedidoAFactura)');
if not Assigned(APedido) then
raise Exception.Create ('Pedido no asignado (CopiarPedidoAFactura)');
if not APedido.DataTable.Active then
APedido.DataTable.Active := True;
APedidosClienteController.RecuperarCliente(APedido);
APedido.Cliente.DataTable.Active := True;
AFactura.Cliente := APedido.Cliente;
AFactura.ID_PEDIDO := APedido.ID;
AFactura.IMPORTE_NETO := APedido.IMPORTE_NETO;
AFactura.IMPORTE_PORTE := APedido.IMPORTE_PORTE;
AFactura.DESCUENTO := APedido.DESCUENTO;
AFactura.IMPORTE_DESCUENTO := APedido.IMPORTE_DESCUENTO;
AFactura.BASE_IMPONIBLE := APedido.BASE_IMPONIBLE;
AFactura.IMPORTE_PORTE := APedido.IMPORTE_PORTE;
AFactura.IMPORTE_TOTAL := APedido.IMPORTE_TOTAL;
AFactura.ID_FORMA_PAGO := APedido.ID_FORMA_PAGO;
end;
procedure CopiarDetallesAFactura(APedido: IBizPedidoCliente; AFactura: IBizFacturaProforma;
AArticulos: IBizDetallesPedidoCliente);
var
i : integer;
ADetalles : IBizDetallesFacturaProforma;
ADetallesController : IDetallesFacturaProformaController;
begin
if not Assigned(AFactura) then
raise Exception.Create ('Factura no asignada(CopiarDetallesAFactura)');
if not Assigned(APedido) then
raise Exception.Create ('Pedido no asignado (CopiarDetallesAFactura)');
if not Assigned(AArticulos) then
raise Exception.Create ('Artículos no asignado (CopiarDetallesAFactura)');
if not AArticulos.DataTable.Active then
AArticulos.DataTable.Active := True;
// La factura tiene que venir ya abierto y posicionado donde hay que copiar
ADetalles := AFactura.Detalles;
ADetallesController := TDetallesFacturaProformaController.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;
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;
//Añadimos el importe de porte solo en el caso de que el pedido tenga.
if (APedido.IMPORTE_PORTE > 0) then
begin
AFacturasProformaController.DetallesController.Add(AFactura.Detalles, TIPO_DETALLE_CONCEPTO);
with AFactura.Detalles do
begin
Edit;
CONCEPTO := 'Porte del pedido ';
CANTIDAD := 1;
IMPORTE_UNIDAD := APedido.IMPORTE_PORTE;
Post;
end;
end;
finally
ADetallesController.EndUpdate(ADetalles);
ADetallesController := NIL;
end;
end;
procedure Inicializar;
begin
dmGenerarFacturasProforma := TdmGenerarFacturasProforma.Create(nil);
APedidosClienteController := TPedidosClienteController.Create;
AFacturasProformaController := TFacturasProformaController.Create;
// ADetallesAlbaranesCliController := TDetallesAlbaranClienteController.Create;
AClientesController := TClientesController.Create;
end;
procedure Finalizar;
begin
FreeAndNIL(dmGenerarFacturasProforma);
APedidosClienteController := nil;
AFacturasProformaController := nil;
// ADetallesFacturasProformaController := nil;
AClientesController := nil;
end;
function GenerarFacturaProforma(const IDPedido : Integer) : Boolean; overload;
var
APedido : IBizPedidoCliente;
AFactura : IBizFacturaProforma;
begin
Result := False;
try
if not Assigned(APedidosClienteController) then
Inicializar;
APedido := APedidosClienteController.Buscar(IDPedido);
if Assigned(APedido) then
Result := GenerarFacturaProforma(APedido, AFactura);
finally
if Assigned(APedidosClienteController) then
Finalizar;
end;
end;
function GenerarFacturaProforma(APedido : IBizPedidoCliente; var AFactura: IBizFacturaProforma) : Boolean; overload;
var
ARespuesta : Integer;
AuxFactura : IBizFacturaProforma;
begin
Result := False;
AFactura := NIL;
if not Assigned(APedido) then
raise Exception.Create('Pedido de cliente no asignado (GenerarFacturaProforma)');
if not APedido.DataTable.Active then
APedido.DataTable.Active := True;
if not Assigned(APedidosClienteController) then
Inicializar;
try
AuxFactura := AFacturasProformaController.Nuevo;
CopiarPedidoAFactura(APedido, AuxFactura);
CopiarDetallesAFactura(APedido, AuxFactura, APedido.Detalles);
//Sustituir por if de guardar
if AFacturasProformaController.Guardar(AuxFactura) then
begin
AFactura := AuxFactura;
with dmGenerarFacturasProforma.JsListaFacturasProformaGeneradas do
begin
Instruction.Text := 'Se ha generado la factura proforma';
Content.Clear;
Content.Add(Format('Se ha generado correctamente la factura proforma %s a partir del pedido de cliente' + #10#13, [AuxFactura.REFERENCIA]));
Execute;
ARespuesta := CustomButtonResult;
case ARespuesta of
100 : begin
// Ver la factura proforma
AFacturasProformaController.Ver(AuxFactura);
end;
200 : // Continuar;
end;
end;
end;
Result := True;
finally
// AArticulosPendientes := NIL;
if Assigned(APedidosClienteController) 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á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.