123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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<bool, String> urlsAperturaCierre = {
- true: "bodega/cierre-bodega",
- false: "bodega/apertura-bodega"
- };
- class StoreViewModel {
- final BaseService _services = BaseService();
- //final LoginViewModel _loginViewModel = LoginViewModel.instance();
- Future<bool> openStore() async {
- try {
- String url = "bodega/verificar-bodega";
- String? userToken = await SessionStorage().getToken();
- http.Response? jsonResponse = await _services.get(url);
- Map<String, dynamic> response = json.decode(jsonResponse!.body);
- if (response["detalle"]["identificador"] != 2) {
- return false;
- } else {
- return true;
- }
- } catch (e) {
- print(e);
- return false;
- }
- }
- Future<int?> statusStore() async {
- try {
- String url = "bodega/verificar-bodega";
- String? userToken = await SessionStorage().getToken();
- http.Response? jsonResponse = await _services.get(url);
- Map<String, dynamic> response = json.decode(jsonResponse!.body);
- return response["detalle"]["identificador"];
- } catch (e) {
- print(e);
- return null;
- }
- }
- Future<List<Map<String, dynamic>>> getMotivosAperturaCierre() async {
- String url = "motivo-apertura-cierre?ordenar=idMotivoAperturaCierre.desc";
- String? userToken = await SessionStorage().getToken();
- http.Response? jsonResponse = await _services.get(url);
- Map<String, dynamic> response = json.decode(jsonResponse!.body);
- List<Map<String, dynamic>> motives =
- List<Map<String, dynamic>>.from(response["resultado"]);
- motives = motives
- .where((Map<String, dynamic> motive) =>
- [5, 6].contains(motive["idMotivoAperturaCierre"]))
- .toList();
- motives.sort((a, b) =>
- a["idMotivoAperturaCierre"].compareTo(b["idMotivoAperturaCierre"]));
- return motives;
- }
- Future<bool> openOrCloseStore(Map<String, dynamic> 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<String, dynamic> 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;
- }
- }
- }
|