- Servicio: nuevo campo esRecurrente + migración DB v2 - Al marcar pagada una factura recurrente se genera la siguiente - Editar facturas existentes desde CrearFacturaScreen - Nuevo método actualizarFactura en FacturaProvider - padding inferior en listas de facturas y servicios (último item visible) - Nota de factura visible en subtítulo - Filtro Vencidas corregido - Monto pendiente total visible en dashboard - Botón undo para revertir factura pagada - Configuración de release signing (keystore) - Pantalla de política de privacidad en la app + PRIVACY_POLICY.md
100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:intl/intl.dart';
|
|
import '../database/database_helper.dart';
|
|
import '../models/factura.dart';
|
|
|
|
class FacturaProvider extends ChangeNotifier {
|
|
final DatabaseHelper _db = DatabaseHelper.instance;
|
|
List<Factura> _facturas = [];
|
|
List<Factura> _facturasVencidas = [];
|
|
bool _cargando = false;
|
|
|
|
List<Factura> get facturas => _facturas;
|
|
List<Factura> get facturasVencidas => _facturasVencidas;
|
|
bool get cargando => _cargando;
|
|
|
|
Future<void> cargarFacturas({String? estado, int? servicioId}) async {
|
|
_cargando = true;
|
|
notifyListeners();
|
|
if (estado == 'vencidas') {
|
|
_facturas = await _db.getFacturasVencidas();
|
|
} else {
|
|
_facturas = await _db.getFacturas(estado: estado, servicioId: servicioId);
|
|
}
|
|
_facturasVencidas = await _db.getFacturasVencidas();
|
|
_cargando = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> agregarFactura(Factura factura) async {
|
|
await _db.insertFactura(factura);
|
|
await cargarFacturas();
|
|
}
|
|
|
|
Future<void> marcarPagada(int id) async {
|
|
final factura = _facturas.firstWhere((f) => f.id == id);
|
|
final hoy = DateTime.now().toIso8601String().split('T')[0];
|
|
final actualizada = factura.copyWith(estado: 'Pagada', fechaPago: hoy);
|
|
await _db.updateFactura(actualizada);
|
|
|
|
final servicio = await _db.getServicio(factura.servicioId);
|
|
if (servicio != null && servicio.esRecurrente) {
|
|
final nextEmision = _sumarMes(factura.fechaEmision);
|
|
final nextVencimiento = _sumarMes(factura.fechaVencimiento);
|
|
final nueva = Factura(
|
|
servicioId: factura.servicioId,
|
|
monto: factura.monto,
|
|
fechaEmision: nextEmision,
|
|
fechaVencimiento: nextVencimiento,
|
|
nota: factura.nota,
|
|
);
|
|
await _db.insertFactura(nueva);
|
|
}
|
|
|
|
await cargarFacturas();
|
|
}
|
|
|
|
String _sumarMes(String dateStr) {
|
|
final date = DateTime.parse(dateStr);
|
|
final next = DateTime(date.year, date.month + 1, date.day);
|
|
return DateFormat('yyyy-MM-dd').format(next);
|
|
}
|
|
|
|
Future<void> marcarPendiente(int id) async {
|
|
final factura = _facturas.firstWhere((f) => f.id == id);
|
|
final actualizada = factura.copyWith(estado: 'Pendiente', fechaPago: null);
|
|
await _db.updateFactura(actualizada);
|
|
await cargarFacturas();
|
|
}
|
|
|
|
Future<void> actualizarFactura(Factura factura) async {
|
|
await _db.updateFactura(factura);
|
|
await cargarFacturas();
|
|
}
|
|
|
|
Future<void> eliminarFactura(int id) async {
|
|
await _db.deleteFactura(id);
|
|
await cargarFacturas();
|
|
}
|
|
|
|
Future<double> totalGastadoMes(int year, int month) async {
|
|
return await _db.getTotalGastadoMes(year, month);
|
|
}
|
|
|
|
Future<Map<String, double>> gastosPorServicio(int year, int month) async {
|
|
return await _db.getGastosPorServicio(year, month);
|
|
}
|
|
|
|
Future<List<Map<String, dynamic>>> gastoMensual(int year) async {
|
|
return await _db.getGastoMensual(year);
|
|
}
|
|
|
|
Future<List<Factura>> facturasPorMes(int year, int month) async {
|
|
return await _db.getFacturasPorMes(year, month);
|
|
}
|
|
|
|
Future<List<Factura>> facturasPorRango(String inicio, String fin) async {
|
|
return await _db.getFacturasPorRango(inicio, fin);
|
|
}
|
|
}
|