store_view_model.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:computo_lite/services/services.dart';
  2. import 'package:computo_lite/viewmodels/login_view_model.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'dart:convert';
  5. import '../data/session/session_storage.dart';
  6. const Map<bool, String> urlsAperturaCierre = {
  7. true: "bodega/cierre-bodega",
  8. false: "bodega/apertura-bodega"
  9. };
  10. class StoreViewModel {
  11. final BaseService _services = BaseService();
  12. //final LoginViewModel _loginViewModel = LoginViewModel.instance();
  13. Future<bool> openStore() async {
  14. try {
  15. String url = "bodega/verificar-bodega";
  16. String? userToken = await SessionStorage().getToken();
  17. http.Response? jsonResponse = await _services.get(url);
  18. Map<String, dynamic> response = json.decode(jsonResponse!.body);
  19. if (response["detalle"]["identificador"] != 2) {
  20. return false;
  21. } else {
  22. return true;
  23. }
  24. } catch (e) {
  25. print(e);
  26. return false;
  27. }
  28. }
  29. Future<int?> statusStore() async {
  30. try {
  31. String url = "bodega/verificar-bodega";
  32. String? userToken = await SessionStorage().getToken();
  33. http.Response? jsonResponse = await _services.get(url);
  34. Map<String, dynamic> response = json.decode(jsonResponse!.body);
  35. return response["detalle"]["identificador"];
  36. } catch (e) {
  37. print(e);
  38. return null;
  39. }
  40. }
  41. Future<List<Map<String, dynamic>>> getMotivosAperturaCierre() async {
  42. String url = "motivo-apertura-cierre?ordenar=idMotivoAperturaCierre.desc";
  43. String? userToken = await SessionStorage().getToken();
  44. http.Response? jsonResponse = await _services.get(url);
  45. Map<String, dynamic> response = json.decode(jsonResponse!.body);
  46. List<Map<String, dynamic>> motives =
  47. List<Map<String, dynamic>>.from(response["resultado"]);
  48. motives = motives
  49. .where((Map<String, dynamic> motive) =>
  50. [5, 6].contains(motive["idMotivoAperturaCierre"]))
  51. .toList();
  52. motives.sort((a, b) =>
  53. a["idMotivoAperturaCierre"].compareTo(b["idMotivoAperturaCierre"]));
  54. return motives;
  55. }
  56. Future<bool> openOrCloseStore(Map<String, dynamic> data, bool open) async {
  57. try {
  58. String date;
  59. if (data["day"] > 9) {
  60. date = "2021-" + data["month"] + "-" + data["day"].toString();
  61. } else {
  62. date = "2021-" + data["month"] + "-" + ("0" + data["day"].toString());
  63. }
  64. String time = data["time"].toString().split(" ")[1].substring(0, 8);
  65. Map<String, dynamic> body = {
  66. "aplicoSelloPuerta": data["withStampDoor"],
  67. "aplicoSelloVentana": data["withStampWindow"],
  68. "observaciones": data["observations"],
  69. "asistentes": data["assistants"],
  70. };
  71. if (!open) {
  72. body["idsMotivoAperturaCierre"] = [
  73. data["motive"]["idMotivoAperturaCierre"]
  74. ];
  75. body["fechaApertura"] = date + " " + time;
  76. } else {
  77. body["fechaCierre"] = date + " " + time;
  78. }
  79. String? url = urlsAperturaCierre[open];
  80. String? userToken = await SessionStorage().getToken();
  81. await _services.post(url!);
  82. return true;
  83. } catch (e) {
  84. print(e);
  85. return false;
  86. }
  87. }
  88. }