git-svn-id: https://192.168.0.254/svn/Proyectos.AbetoDesign_FactuGES/trunk@151 93f398dd-4eb6-7a46-baf6-13f46f578da2
420 lines
12 KiB
ObjectPascal
420 lines
12 KiB
ObjectPascal
unit uBizFacturasCliente;
|
||
|
||
interface
|
||
|
||
uses
|
||
uDAInterfaces, uDADataTable, schFacturasClienteClient_Intf,
|
||
uBizContactos, uBizDetallesFacturaCliente, uDBSelectionListUtils, Classes;
|
||
|
||
const
|
||
BIZ_CLIENT_FACTURA_CLIENTE = 'Client.FacturaCliente';
|
||
|
||
CTE_TIPO_ABONO = 'A';
|
||
CTE_TIPO_FACTURA = 'F';
|
||
|
||
CTE_PENDIENTE = 'PENDIENTE';
|
||
CTE_PARCIAMENTE_PAGADA = 'PARCIALMENTE PAGADA';
|
||
CTE_PAGADA = 'PAGADA';
|
||
|
||
type
|
||
IBizFacturaCliente = interface(IFacturasCliente)
|
||
['{8C6F2523-41FB-4240-A242-C14390FBD2B7}']
|
||
|
||
procedure SetCliente(AValue : IBizCliente);
|
||
function GetCliente : IBizCliente;
|
||
property Cliente : IBizCliente read GetCliente write SetCliente;
|
||
|
||
// Esta propidad es para que el controlador pueda acceder directamente
|
||
// a la propiedad Cliente
|
||
procedure _SetCliente(AValue : IBizCliente);
|
||
function _GetCliente : IBizCliente;
|
||
property _Cliente : IBizCliente read _GetCliente write _SetCliente;
|
||
|
||
function GetDetalles: IBizDetallesFacturaCliente;
|
||
procedure SetDetalles(Value: IBizDetallesFacturaCliente);
|
||
property Detalles: IBizDetallesFacturaCliente read GetDetalles write SetDetalles;
|
||
|
||
function EsNuevo : Boolean;
|
||
procedure CalcularImporteTotal;
|
||
end;
|
||
|
||
TBizFacturaCliente = class(TFacturasClienteDataTableRules, IBizFacturaCliente, ISeleccionable)
|
||
private
|
||
FSeleccionableInterface : ISeleccionable;
|
||
procedure CalcularDescuento;
|
||
procedure CalcularIVA;
|
||
procedure CalcularRE;
|
||
procedure CalcularRetencion;
|
||
procedure CalcularBaseImponible;
|
||
procedure AsignarTipoIVA (IDTipoIVA : Integer);
|
||
|
||
protected
|
||
FCliente : IBizCliente;
|
||
FDetalles : IBizDetallesFacturaCliente;
|
||
FDetallesLink : TDADataSource;
|
||
|
||
procedure SetCliente(AValue : IBizCliente);
|
||
function GetCliente : IBizCliente;
|
||
|
||
procedure _SetCliente(AValue : IBizCliente);
|
||
function _GetCliente : IBizCliente;
|
||
|
||
function GetDetalles: IBizDetallesFacturaCliente;
|
||
procedure SetDetalles(Value: IBizDetallesFacturaCliente);
|
||
|
||
procedure RECARGO_EQUIVALENCIAOnChange(Sender: TDACustomField);
|
||
procedure ID_TIPO_IVAOnChange(Sender: TDACustomField);
|
||
|
||
procedure IMPORTE_NETOOnChange(Sender: TDACustomField);
|
||
procedure IMPORTE_PORTEOnChange(Sender: TDACustomField);
|
||
procedure DESCUENTOOnChange(Sender: TDACustomField);
|
||
procedure IVAOnChange(Sender: TDACustomField);
|
||
procedure REOnChange(Sender: TDACustomField);
|
||
procedure RetencionOnChange(Sender: TDACustomField);
|
||
procedure SetID_TIENDAValue(const aValue: Integer);
|
||
procedure SetID_SUBCUENTAValue(const aValue: Integer);
|
||
|
||
procedure OnNewRecord(Sender: TDADataTable); override;
|
||
|
||
function GetSITUACIONValue: String; override;
|
||
// procedure SetID_FORMA_PAGOValue(const aValue: Integer); override;
|
||
procedure SetID_COMISION_LIQUIDADAValue(const aValue: Integer); override;
|
||
procedure SetID_TIPO_IVAValue(const aValue: Integer); override;
|
||
|
||
public
|
||
function EsNuevo : Boolean;
|
||
procedure IniciarValoresFacturaNueva;
|
||
procedure CalcularImporteTotal;
|
||
|
||
property Cliente : IBizCliente read GetCliente write SetCliente;
|
||
property _Cliente : IBizCliente read _GetCliente write _SetCliente;
|
||
property Detalles: IBizDetallesFacturaCliente read GetDetalles write SetDetalles;
|
||
|
||
property SeleccionableInterface : ISeleccionable read FSeleccionableInterface
|
||
write FSeleccionableInterface implements ISeleccionable;
|
||
|
||
constructor Create(aDataTable: TDADataTable); override;
|
||
destructor Destroy; override;
|
||
end;
|
||
|
||
implementation
|
||
{$INCLUDE ..\..\..\FactuGES.inc}
|
||
|
||
uses
|
||
SysUtils, uDataTableUtils, DB, uDateUtils, DateUtils, uDataModuleUsuarios, uNumUtils,
|
||
Variants, cxDateUtils, uTiposIVAController, uBizTiposIVA, uFactuGES_App, uBizEmpresasTiendas;
|
||
|
||
{ TBizFacturaCliente }
|
||
|
||
function TBizFacturaCliente.EsNuevo: Boolean;
|
||
begin
|
||
Result := (ID < 0);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.AsignarTipoIVA(IDTipoIVA: Integer);
|
||
var
|
||
ATiposIVAController : ITiposIVAController;
|
||
ATipoIVA : IBizTipoIVA;
|
||
begin
|
||
inherited;
|
||
|
||
ATiposIVAController := TTiposIVAController.Create;
|
||
try
|
||
ATipoIVA := ATiposIVAController.Buscar(IDTipoIVA);
|
||
ATipoIVA.DataTable.Active := True;
|
||
if Assigned(ATipoIVA) then
|
||
begin
|
||
Edit;
|
||
IVA := ATipoIVA.IVA;
|
||
if (RECARGO_EQUIVALENCIA = 1) then
|
||
RE := ATipoIVA.RE
|
||
else
|
||
RE := 0;
|
||
end;
|
||
finally
|
||
ATiposIVAController := NIL;
|
||
end;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.CalcularBaseImponible;
|
||
begin
|
||
if not Self.DataTable.Editing then
|
||
Edit;
|
||
IMPORTE_DESCUENTO := roundCurrency(IMPORTE_NETO * (DESCUENTO/100));
|
||
BASE_IMPONIBLE := IMPORTE_NETO - IMPORTE_DESCUENTO + IMPORTE_PORTE;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.CalcularDescuento;
|
||
begin
|
||
if not Self.DataTable.Editing then
|
||
Edit;
|
||
IMPORTE_DESCUENTO := roundCurrency((DESCUENTO / 100) * BASE_IMPONIBLE);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.CalcularImporteTotal;
|
||
begin
|
||
DataTable.DisableControls;
|
||
DataTable.Fields.FieldEventsDisabled := True;
|
||
try
|
||
CalcularBaseImponible;
|
||
CalcularIVA;
|
||
CalcularRE;
|
||
CalcularRetencion;
|
||
|
||
if not Self.DataTable.Editing then
|
||
Edit;
|
||
IMPORTE_TOTAL := BASE_IMPONIBLE + IMPORTE_IVA + IMPORTE_RE;
|
||
finally
|
||
DataTable.Fields.FieldEventsDisabled := False;
|
||
DataTable.EnableControls;
|
||
end;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.CalcularIVA;
|
||
begin
|
||
if not Self.DataTable.Editing then
|
||
Edit;
|
||
IMPORTE_IVA := roundCurrency((IVA / 100) * BASE_IMPONIBLE);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.CalcularRE;
|
||
begin
|
||
if not Self.DataTable.Editing then
|
||
Edit;
|
||
IMPORTE_RE := roundCurrency((RE / 100) * BASE_IMPONIBLE);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.CalcularRetencion;
|
||
begin
|
||
if not Self.DataTable.Editing then
|
||
Edit;
|
||
IMPORTE_RETENCION := roundCurrency((RETENCION / 100) * BASE_IMPONIBLE);
|
||
end;
|
||
|
||
constructor TBizFacturaCliente.Create(aDataTable: TDADataTable);
|
||
begin
|
||
inherited;
|
||
FCliente := Nil;
|
||
|
||
with DataTable do
|
||
begin
|
||
FieldByName(fld_FacturasClienteID_TIPO_IVA).OnChange := ID_TIPO_IVAOnChange;
|
||
FieldByName(fld_FacturasClienteRECARGO_EQUIVALENCIA).OnChange := RECARGO_EQUIVALENCIAOnChange;
|
||
|
||
FieldByName(fld_FacturasClienteIMPORTE_NETO).OnChange := IMPORTE_NETOOnChange;
|
||
FieldByName(fld_FacturasClienteIMPORTE_PORTE).OnChange := IMPORTE_PORTEOnChange;
|
||
FieldByName(fld_FacturasClienteDESCUENTO).OnChange := DESCUENTOOnChange;
|
||
FieldByName(fld_FacturasClienteIVA).OnChange := IVAOnChange;
|
||
FieldByName(fld_FacturasClienteRE).OnChange := REOnChange;
|
||
FieldByName(fld_FacturasClienteRetencion).OnChange := RetencionOnChange;
|
||
end;
|
||
|
||
FDetallesLink := TDADataSource.Create(NIL);
|
||
FDetallesLink.DataTable := aDataTable;
|
||
|
||
FSeleccionableInterface := TSeleccionable.Create(aDataTable);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.DESCUENTOOnChange(Sender: TDACustomField);
|
||
begin
|
||
CalcularImporteTotal;
|
||
end;
|
||
|
||
destructor TBizFacturaCliente.Destroy;
|
||
begin
|
||
FCliente := NIL;
|
||
FDetalles := NIL;
|
||
FDetallesLink.Free;
|
||
|
||
FSeleccionableInterface := NIL;
|
||
inherited;
|
||
end;
|
||
|
||
function TBizFacturaCliente.GetCliente: IBizCliente;
|
||
begin
|
||
Result := FCliente;
|
||
end;
|
||
|
||
function TBizFacturaCliente.GetDetalles: IBizDetallesFacturaCliente;
|
||
begin
|
||
Result := FDetalles;
|
||
end;
|
||
|
||
function TBizFacturaCliente.GetSITUACIONValue: String;
|
||
begin
|
||
result := Trim(DataTable.Fields[idx_FacturasClienteSITUACION].AsString);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.ID_TIPO_IVAOnChange(Sender: TDACustomField);
|
||
begin
|
||
AsignarTipoIVA(ID_TIPO_IVA);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.IMPORTE_NETOOnChange(Sender: TDACustomField);
|
||
begin
|
||
CalcularImporteTotal;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.IMPORTE_PORTEOnChange(Sender: TDACustomField);
|
||
begin
|
||
CalcularImporteTotal;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.IniciarValoresFacturaNueva;
|
||
begin
|
||
ID_EMPRESA := AppFactuGES.EmpresaActiva.ID;
|
||
USUARIO := AppFactuGES.UsuarioActivo.UserName;
|
||
FECHA_FACTURA := DateOf(Now);
|
||
// FECHA_VENCIMIENTO := DateOf(Now);
|
||
TIPO := CTE_TIPO_FACTURA;
|
||
SITUACION := CTE_PENDIENTE;
|
||
ID_CLIENTE := ID_NULO;
|
||
SIN_COMISION := 0;
|
||
|
||
ID_FORMA_PAGO := AppFactuGES.EmpresaActiva.ID_FORMA_PAGO;
|
||
ID_TIPO_IVA := AppFactuGES.EmpresaActiva.ID_TIPO_IVA;
|
||
|
||
RE := 0;
|
||
SITUACION := CTE_PENDIENTE;
|
||
ID_TIENDA := AppFactuGES.TiendaActiva.ID;
|
||
TIENDA := AppFactuGES.TiendaActiva.NOMBRE;
|
||
|
||
{$IFDEF CONTABILIDAD}
|
||
// if Assigned(AppFactuGES.EjercicioActivo) then
|
||
// IGNORAR_CONTABILIDAD := 0
|
||
// else
|
||
IGNORAR_CONTABILIDAD := 1;
|
||
{$ENDIF}
|
||
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.IVAOnChange(Sender: TDACustomField);
|
||
begin
|
||
CalcularImporteTotal;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.OnNewRecord(Sender: TDADataTable);
|
||
begin
|
||
inherited;
|
||
IniciarValoresFacturaNueva;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.RECARGO_EQUIVALENCIAOnChange(
|
||
Sender: TDACustomField);
|
||
begin
|
||
AsignarTipoIVA(ID_TIPO_IVA);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.SetID_SUBCUENTAValue(const aValue: Integer);
|
||
begin
|
||
if aValue < 0 then
|
||
DataTable.Fields[idx_FacturasClienteID_SUBCUENTA].AsVariant := NULL
|
||
else
|
||
inherited SetID_SUBCUENTAValue(aValue);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.SetID_TIENDAValue(const aValue: Integer);
|
||
begin
|
||
if aValue < 0 then
|
||
DataTable.Fields[idx_FacturasClienteID_TIENDA].AsVariant := NULL
|
||
else
|
||
inherited SetID_TIENDAValue(aValue);
|
||
end;
|
||
|
||
|
||
procedure TBizFacturaCliente.REOnChange(Sender: TDACustomField);
|
||
begin
|
||
CalcularImporteTotal;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.RetencionOnChange(Sender: TDACustomField);
|
||
begin
|
||
CalcularImporteTotal;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.SetCliente(AValue: IBizCliente);
|
||
var
|
||
bEnEdicion : Boolean;
|
||
begin
|
||
FCliente := AValue;
|
||
|
||
bEnEdicion := (DataTable.State in dsEditModes);
|
||
if not bEnEdicion then
|
||
Edit;
|
||
|
||
if Assigned(FCliente) then
|
||
begin
|
||
if not FCliente.DataTable.Active then
|
||
FCliente.DataTable.Active := True;
|
||
|
||
ID_CLIENTE := FCliente.ID;
|
||
NOMBRE := FCliente.NOMBRE;
|
||
NIF_CIF := FCliente.NIF_CIF;
|
||
CALLE := FCliente.CALLE;
|
||
CODIGO_POSTAL := FCliente.CODIGO_POSTAL;
|
||
PROVINCIA := FCliente.PROVINCIA;
|
||
POBLACION := FCliente.POBLACION;
|
||
// La forma de pago se establece desde el tipo de documento y aunque cambie de cliente no se cambia la formade pago ya que desde la vista se pregunta y se cambia si el usuario lo desea
|
||
// if FCliente.ID_FORMA_PAGO > 0 then
|
||
// ID_FORMA_PAGO := FCliente.ID_FORMA_PAGO;
|
||
// if FCliente.ID_TIPO_IVA > 0 then
|
||
// ID_TIPO_IVA := FCliente.ID_TIPO_IVA;
|
||
RECARGO_EQUIVALENCIA := FCliente.RECARGO_EQUIVALENCIA;
|
||
// Se establece un descuento especial general a petici<63>n de angelica
|
||
DESCUENTO := FCliente.DESCUENTO;
|
||
|
||
Post;
|
||
|
||
if bEnEdicion then
|
||
Edit;
|
||
end
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.SetDetalles(Value: IBizDetallesFacturaCliente);
|
||
begin
|
||
FDetalles := Value;
|
||
EnlazarMaestroDetalle(FDetallesLink, FDetalles);
|
||
end;
|
||
|
||
procedure TBizFacturaCliente.SetID_COMISION_LIQUIDADAValue(const aValue: Integer);
|
||
begin
|
||
if (aValue = 0) then
|
||
SetFieldNull(DataTable, fld_FacturasClienteID_COMISION_LIQUIDADA)
|
||
else
|
||
inherited;
|
||
end;
|
||
|
||
{procedure TBizFacturaCliente.SetID_FORMA_PAGOValue(const aValue: Integer);
|
||
begin
|
||
if (aValue = 0) then
|
||
SetFieldNull(DataTable, fld_FacturasClienteID_FORMA_PAGO)
|
||
else
|
||
inherited;
|
||
end;}
|
||
|
||
procedure TBizFacturaCliente.SetID_TIPO_IVAValue(const aValue: Integer);
|
||
begin
|
||
if (aValue = 0) then
|
||
SetFieldNull(DataTable, fld_FacturasClienteID_TIPO_IVA)
|
||
else
|
||
inherited;
|
||
end;
|
||
|
||
function TBizFacturaCliente._GetCliente: IBizCliente;
|
||
begin
|
||
Result := FCliente;
|
||
end;
|
||
|
||
procedure TBizFacturaCliente._SetCliente(AValue: IBizCliente);
|
||
begin
|
||
FCliente := AValue;
|
||
end;
|
||
|
||
initialization
|
||
RegisterDataTableRules(BIZ_CLIENT_FACTURA_CLIENTE, TBizFacturaCliente);
|
||
|
||
finalization
|
||
|
||
end.
|
||
|