|
@@ -6,6 +6,7 @@ import '../services/base_service.dart';
|
|
|
import '../models/models.dart';
|
|
|
import '../services/services.dart';
|
|
|
import '../services/repo_service.dart';
|
|
|
+import '../views/producto/producto_imagen.dart';
|
|
|
|
|
|
class ProductoViewModel<T> extends ChangeNotifier {
|
|
|
String _busqueda = "";
|
|
@@ -182,7 +183,10 @@ class ProductoViewModel<T> extends ChangeNotifier {
|
|
|
String? claveSucursal = await obtenerClaveSucursal();
|
|
|
|
|
|
try {
|
|
|
- Map<String, String> parametros = {"claveSucursal": claveSucursal!};
|
|
|
+ Map<String, String> parametros = {
|
|
|
+ "claveSucursal": claveSucursal!,
|
|
|
+ "limite": "-1"
|
|
|
+ };
|
|
|
final response = ApiResponse(await BaseService()
|
|
|
.get('/pos/categoria', queryParameters: parametros));
|
|
|
|
|
@@ -212,7 +216,8 @@ class ProductoViewModel<T> extends ChangeNotifier {
|
|
|
try {
|
|
|
Map<String, String> parametros = {
|
|
|
"limite": "-1",
|
|
|
- "claveSucursal": claveSucursal!
|
|
|
+ "claveSucursal": claveSucursal!,
|
|
|
+ "expand": "media"
|
|
|
};
|
|
|
final response = ApiResponse(await BaseService()
|
|
|
.get('/pos/producto', queryParameters: parametros));
|
|
@@ -222,10 +227,32 @@ class ProductoViewModel<T> extends ChangeNotifier {
|
|
|
response.resultados!.map((json) => Producto.fromApi(json)).toList();
|
|
|
|
|
|
if (productosApi.isNotEmpty) {
|
|
|
+ print("Productos API obtenidos: ${productosApi.length}");
|
|
|
+
|
|
|
+ // Aquí mantengo tu lógica de descarga de imágenes.
|
|
|
+ for (var productoApi in productosApi) {
|
|
|
+ if (productoApi.media != null && productoApi.media!.isNotEmpty) {
|
|
|
+ for (var media in productoApi.media!) {
|
|
|
+ // Descargar y guardar la imagen localmente
|
|
|
+ String? localImagePath = await downloadAndStoreImage(
|
|
|
+ media.ruta!, productoApi.id!, media.nombre!);
|
|
|
+
|
|
|
+ if (localImagePath != null) {
|
|
|
+ productoApi.imagen = localImagePath;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Delegamos la sincronización de los productos al RepoService
|
|
|
await RepoService().sincronizarProductos(productosApi);
|
|
|
notifyListeners();
|
|
|
return true;
|
|
|
+ } else {
|
|
|
+ print("No se encontraron productos en la API.");
|
|
|
}
|
|
|
+ } else {
|
|
|
+ print("Error en la respuesta de la API o resultados nulos.");
|
|
|
}
|
|
|
return false;
|
|
|
} catch (e) {
|
|
@@ -234,25 +261,61 @@ class ProductoViewModel<T> extends ChangeNotifier {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ Future<bool> sincronizarProductoTopping() async {
|
|
|
+ String? claveSucursal = await obtenerClaveSucursal();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Map<String, String> parametros = {
|
|
|
+ "limite": "-1",
|
|
|
+ "claveSucursal": claveSucursal!,
|
|
|
+ };
|
|
|
+
|
|
|
+ final response = ApiResponse(await BaseService()
|
|
|
+ .get('/pos/producto-topping', queryParameters: parametros));
|
|
|
+
|
|
|
+ if (response.isOk && response.resultados != null) {
|
|
|
+ List<ProductoTopping> toppingApi = response.resultados!
|
|
|
+ .map((json) => ProductoTopping.fromApi(json))
|
|
|
+ .toList();
|
|
|
+
|
|
|
+ if (toppingApi.isNotEmpty) {
|
|
|
+ print("Producto Toppings API obtenidos: ${toppingApi.length}");
|
|
|
+
|
|
|
+ // Delegamos la sincronización de los toppings al RepoService
|
|
|
+ await RepoService().sincronizarProductoTopping(toppingApi);
|
|
|
+ notifyListeners();
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ print("No se encontraron toppings de producto en la API.");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ print("Error en la respuesta de la API o resultados nulos.");
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ } catch (e) {
|
|
|
+ print('Error al sincronizar producto toppings: $e');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
Future<void> sincronizarProductosYCategorias() async {
|
|
|
- //print('Sincronizando productos');
|
|
|
setIsLoading(true);
|
|
|
try {
|
|
|
bool categoriasSincronizadas = await sincronizarCategorias();
|
|
|
- //print('Categorias sincronizadas: $categoriasSincronizadas');
|
|
|
|
|
|
if (categoriasSincronizadas) {
|
|
|
bool productosSincronizados = await sincronizarProductos();
|
|
|
if (productosSincronizados) {
|
|
|
- await fetchLocalAll();
|
|
|
+ bool toppingsSincronizados = await sincronizarProductoTopping();
|
|
|
+ if (toppingsSincronizados) {
|
|
|
+ await fetchLocalAll();
|
|
|
+ }
|
|
|
}
|
|
|
- //print('Productos sincronizados: $productosSincronizados');
|
|
|
}
|
|
|
notifyListeners();
|
|
|
} catch (e, stackTrace) {
|
|
|
- // Capturar el mensaje detallado del error
|
|
|
throw Exception(
|
|
|
- "Error al sincronizar productos y categorías: $e\n$stackTrace");
|
|
|
+ "Error al sincronizar productos, categorías y toppings: $e\n$stackTrace");
|
|
|
} finally {
|
|
|
setIsLoading(false);
|
|
|
}
|