unit uBizFacturasProforma; interface uses uDAInterfaces, uDADataTable, schFacturasProformaClient_Intf, uBizContactos, uBizDetallesFacturaProforma, uDBSelectionListUtils, Classes; const BIZ_CLIENT_FACTURA_PROFORMA = 'Client.FacturaPROFORMA'; CTE_PENDIENTE = 'PENDIENTE'; CTE_PAGADA = 'PAGADA'; type IBizFacturaProforma = interface(IFacturasProforma) ['{180381AC-4A61-4615-A66E-2E4516740285}'] 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: IBizDetallesFacturaProforma; procedure SetDetalles(Value: IBizDetallesFacturaProforma); property Detalles: IBizDetallesFacturaProforma read GetDetalles write SetDetalles; function EsNuevo : Boolean; procedure CalcularImporteTotal; end; TBizFacturaProforma = class(TFacturasProformaDataTableRules, IBizFacturaProforma, ISeleccionable) private FSeleccionableInterface : ISeleccionable; // procedure CalcularDescuento; procedure CalcularIVA; procedure CalcularRE; procedure CalcularBaseImponible; procedure AsignarTipoIVA (IDTipoIVA : Integer); protected FCliente : IBizCliente; FDetalles : IBizDetallesFacturaProforma; FDetallesLink : TDADataSource; procedure SetCliente(AValue : IBizCliente); function GetCliente : IBizCliente; procedure _SetCliente(AValue : IBizCliente); function _GetCliente : IBizCliente; function GetDetalles: IBizDetallesFacturaProforma; procedure SetDetalles(Value: IBizDetallesFacturaProforma); 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 OnNewRecord(Sender: TDADataTable); override; function GetSITUACIONValue: String; 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: IBizDetallesFacturaProforma 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; { TBizFacturaProforma } function TBizFacturaProforma.EsNuevo: Boolean; begin Result := (ID < 0); end; procedure TBizFacturaProforma.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 TBizFacturaProforma.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 TBizFacturaProforma.CalcularDescuento; begin if not Self.DataTable.Editing then Edit; IMPORTE_DESCUENTO := (DESCUENTO / 100) * IMPORTE_NETO; end;} procedure TBizFacturaProforma.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 TBizFacturaProforma.CalcularIVA; begin if not Self.DataTable.Editing then Edit; IMPORTE_IVA := (IVA / 100) * BASE_IMPONIBLE; end; procedure TBizFacturaProforma.CalcularRE; begin if not Self.DataTable.Editing then Edit; IMPORTE_RE := (RE / 100) * BASE_IMPONIBLE; end; constructor TBizFacturaProforma.Create(aDataTable: TDADataTable); begin inherited; FCliente := Nil; with DataTable do begin FieldByName(fld_FacturasProformaID_TIPO_IVA).OnChange := ID_TIPO_IVAOnChange; FieldByName(fld_FacturasProformaRECARGO_EQUIVALENCIA).OnChange := RECARGO_EQUIVALENCIAOnChange; FieldByName(fld_FacturasProformaIMPORTE_NETO).OnChange := IMPORTE_NETOOnChange; FieldByName(fld_FacturasProformaIMPORTE_PORTE).OnChange := IMPORTE_PORTEOnChange; FieldByName(fld_FacturasProformaDESCUENTO).OnChange := DESCUENTOOnChange; FieldByName(fld_FacturasProformaIVA).OnChange := IVAOnChange; FieldByName(fld_FacturasProformaRE).OnChange := REOnChange; end; FDetallesLink := TDADataSource.Create(NIL); FDetallesLink.DataTable := aDataTable; FSeleccionableInterface := TSeleccionable.Create(aDataTable); end; procedure TBizFacturaProforma.DESCUENTOOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; destructor TBizFacturaProforma.Destroy; begin FCliente := NIL; FDetalles := NIL; FDetallesLink.Free; FSeleccionableInterface := NIL; inherited; end; function TBizFacturaProforma.GetCliente: IBizCliente; begin Result := FCliente; end; function TBizFacturaProforma.GetDetalles: IBizDetallesFacturaProforma; begin Result := FDetalles; end; function TBizFacturaProforma.GetSITUACIONValue: String; begin result := Trim(DataTable.Fields[idx_FacturasProformaSITUACION].AsString); end; procedure TBizFacturaProforma.ID_TIPO_IVAOnChange(Sender: TDACustomField); begin AsignarTipoIVA(ID_TIPO_IVA); end; procedure TBizFacturaProforma.IMPORTE_NETOOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizFacturaProforma.IMPORTE_PORTEOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizFacturaProforma.IniciarValoresFacturaNueva; begin ID_EMPRESA := AppFactuGES.EmpresaActiva.ID; USUARIO := AppFactuGES.UsuarioActivo.UserName; FECHA_FACTURA := DateOf(Now); FECHA_VENCIMIENTO := DateOf(Now); ID_CLIENTE := ID_NULO; ID_FORMA_PAGO := AppFactuGES.EmpresaActiva.ID_FORMA_PAGO; ID_TIPO_IVA := AppFactuGES.EmpresaActiva.ID_TIPO_IVA; RE := 0; SITUACION := CTE_PENDIENTE; end; procedure TBizFacturaProforma.IVAOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizFacturaProforma.OnNewRecord(Sender: TDADataTable); begin inherited; IniciarValoresFacturaNueva; end; procedure TBizFacturaProforma.RECARGO_EQUIVALENCIAOnChange( Sender: TDACustomField); begin AsignarTipoIVA(ID_TIPO_IVA); end; procedure TBizFacturaProforma.REOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizFacturaProforma.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; PAIS := FCliente.PAIS; 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; DESCUENTO := FCliente.DESCUENTO; Post; if bEnEdicion then Edit; end end; procedure TBizFacturaProforma.SetDetalles(Value: IBizDetallesFacturaProforma); begin FDetalles := Value; EnlazarMaestroDetalle(FDetallesLink, FDetalles); end; procedure TBizFacturaProforma.SetID_TIPO_IVAValue(const aValue: Integer); begin if (aValue = 0) then SetFieldNull(DataTable, fld_FacturasProformaID_TIPO_IVA) else inherited; end; function TBizFacturaProforma._GetCliente: IBizCliente; begin Result := FCliente; end; procedure TBizFacturaProforma._SetCliente(AValue: IBizCliente); begin FCliente := AValue; end; initialization RegisterDataTableRules(BIZ_CLIENT_FACTURA_PROFORMA, TBizFacturaProforma); finalization end.