AlonsoYSal_FactuGES2/Source/Modulos/Facturas de cliente/Model/uBizFacturasCliente.pas
roberto b3a08ad5b4 Versión 2.2.2
Se hace que la forma de pago y el iva se cojan siempre de la empresa y no del cliente. contratos y facturas....
Se mete forma de pago de las facturas en la lista de pagos y cobros para saber como se ha pagado

git-svn-id: https://192.168.0.254/svn/Proyectos.AlonsoYSal_FactuGES2/trunk@26 40301925-124e-1c4e-b97d-170ad7a8785b
2021-06-15 15:00:45 +00:00

403 lines
11 KiB
ObjectPascal
Raw Blame History

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 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 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
uses
SysUtils, uDataTableUtils, DB, uDateUtils, DateUtils, uDataModuleUsuarios,
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 := 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 := (DESCUENTO / 100) * BASE_IMPONIBLE;
end;
procedure TBizFacturaCliente.CalcularImporteTotal;
begin
DataTable.DisableControls;
DataTable.Fields.FieldEventsDisabled := True;
try
CalcularBaseImponible;
CalcularIVA;
CalcularRE;
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 := (IVA / 100) * BASE_IMPONIBLE;
end;
procedure TBizFacturaCliente.CalcularRE;
begin
if not Self.DataTable.Editing then
Edit;
IMPORTE_RE := (RE / 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;
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);
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;
//CONTABILIDAD
// if Assigned(AppFactuGES.EjercicioActivo) then
// IGNORAR_CONTABILIDAD := 0
// else
IGNORAR_CONTABILIDAD := 1;
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.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;
//En alonso y sal no se coge la forma de pago ni el iva del contacto sino de la empresa
// 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;
//Si el cliente tiene la contabilidad desactivada la factura que realizamos la desactivamos tambi<62>n.
if (FCliente.IGNORAR_CONTABILIDAD = 1) then
IGNORAR_CONTABILIDAD := 1;
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.