import 'package:computo_lite/services/services.dart'; import 'package:computo_lite/viewmodels/login_view_model.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import '../data/session/session_storage.dart'; const Map urlsAperturaCierre = { true: "bodega/cierre-bodega", false: "bodega/apertura-bodega" }; class StoreViewModel { final BaseService _services = BaseService(); //final LoginViewModel _loginViewModel = LoginViewModel.instance(); Future openStore() async { try { String url = "bodega/verificar-bodega"; String? userToken = await SessionStorage().getToken(); http.Response? jsonResponse = await _services.get(url); Map response = json.decode(jsonResponse!.body); if (response["detalle"]["identificador"] != 2) { return false; } else { return true; } } catch (e) { print(e); return false; } } Future statusStore() async { try { String url = "bodega/verificar-bodega"; String? userToken = await SessionStorage().getToken(); http.Response? jsonResponse = await _services.get(url); Map response = json.decode(jsonResponse!.body); return response["detalle"]["identificador"]; } catch (e) { print(e); return null; } } Future>> getMotivosAperturaCierre() async { String url = "motivo-apertura-cierre?ordenar=idMotivoAperturaCierre.desc"; String? userToken = await SessionStorage().getToken(); http.Response? jsonResponse = await _services.get(url); Map response = json.decode(jsonResponse!.body); List> motives = List>.from(response["resultado"]); motives = motives .where((Map motive) => [5, 6].contains(motive["idMotivoAperturaCierre"])) .toList(); motives.sort((a, b) => a["idMotivoAperturaCierre"].compareTo(b["idMotivoAperturaCierre"])); return motives; } Future openOrCloseStore(Map data, bool open) async { try { String date; if (data["day"] > 9) { date = "2021-" + data["month"] + "-" + data["day"].toString(); } else { date = "2021-" + data["month"] + "-" + ("0" + data["day"].toString()); } String time = data["time"].toString().split(" ")[1].substring(0, 8); Map body = { "aplicoSelloPuerta": data["withStampDoor"], "aplicoSelloVentana": data["withStampWindow"], "observaciones": data["observations"], "asistentes": data["assistants"], }; if (!open) { body["idsMotivoAperturaCierre"] = [ data["motive"]["idMotivoAperturaCierre"] ]; body["fechaApertura"] = date + " " + time; } else { body["fechaCierre"] = date + " " + time; } String? url = urlsAperturaCierre[open]; String? userToken = await SessionStorage().getToken(); await _services.post(url!); return true; } catch (e) { print(e); return false; } } }