inicio_empleados.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import 'package:flutter/material.dart';
  2. import 'package:fluttertoast/fluttertoast.dart';
  3. import 'package:google_fonts/google_fonts.dart';
  4. import 'package:localstore/localstore.dart';
  5. import 'package:quiosco_soler_2/clientes/pages/inicio_clientes.dart';
  6. import 'package:quiosco_soler_2/clientes/providers/cli_provider.dart';
  7. import 'package:quiosco_soler_2/clientes/providers/info_empleado_provider.dart';
  8. class InicioEmpleados extends StatefulWidget {
  9. const InicioEmpleados({Key? key}) : super(key: key);
  10. @override
  11. State<InicioEmpleados> createState() => _InicioEmpleadosState();
  12. }
  13. class _InicioEmpleadosState extends State<InicioEmpleados> {
  14. final _cliProvider = CliProvider();
  15. final _infoEmpleadoProvider = InfoEmpleadoProvider();
  16. final _nCliController = TextEditingController();
  17. final List<String> _listaNombres = [];
  18. final List<String> _listaID = [];
  19. final db = Localstore.instance;
  20. String _perfilParaClientes = '';
  21. String _usuarioActual = '';
  22. String _dinum = '';
  23. String _token = '';
  24. String _dbKey = '';
  25. bool _isLoading = true;
  26. bool _hasError = false;
  27. @override
  28. void initState() {
  29. super.initState();
  30. _init();
  31. }
  32. void _init() async{
  33. final items = await db.collection('clientes').get();
  34. final key = items!.keys.last;
  35. final item = items[key];
  36. _dinum = item['dinum'];
  37. _token = item['accesToken'];
  38. _dbKey = key.replaceAll('/clientes/', '');
  39. final info = await _infoEmpleadoProvider.fetchInfoEmpleado(item['accesToken'], item['dinum']);
  40. info.result.gblPerfiles.forEach((element) {
  41. if(element['IDPORTAL'] == 'CLI'){
  42. _perfilParaClientes = element['IDPERFIL']!;
  43. }
  44. });
  45. if(_perfilParaClientes != ''){
  46. final lista = await _cliProvider.fetchSListaClientes(item['accesToken'], _perfilParaClientes, item['dinum'],
  47. info.result.gblProZon, info.result.gblProDel);
  48. lista.result.forEach((element) {
  49. if(!_listaNombres.contains(element.nombre)){
  50. _listaNombres.add(element.nombre);
  51. _listaID.add(element.dinum);
  52. }
  53. });
  54. setState(() => _isLoading = false);
  55. }else{
  56. setState(() {
  57. _isLoading = false;
  58. _hasError = true;
  59. });
  60. }
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. final _screenSize = MediaQuery.of(context).size;
  65. return Scaffold(
  66. appBar: AppBar(
  67. automaticallyImplyLeading: false,
  68. backgroundColor: Colors.white,
  69. centerTitle: true,
  70. title: Row(
  71. children: <Widget>[
  72. Image.asset(
  73. 'assets/logo.png',
  74. width: 32.0,
  75. ),
  76. const SizedBox(width: 8.0),
  77. TextButton(
  78. style: TextButton.styleFrom(
  79. minimumSize: const Size(0.0, 0.0),
  80. padding: EdgeInsets.zero,
  81. tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  82. ),
  83. child: const Icon(
  84. Icons.menu,
  85. color: Colors.white,
  86. ),
  87. onPressed: null,
  88. ),
  89. const Expanded(
  90. child: Text(
  91. 'EMPLEADOS',
  92. textAlign: TextAlign.center,
  93. style: TextStyle(
  94. color: Colors.red,
  95. fontFamily: 'Helvetica Neue Black Cond',
  96. ),
  97. ),
  98. ),
  99. TextButton(
  100. style: TextButton.styleFrom(
  101. minimumSize: const Size(0.0, 0.0),
  102. padding: EdgeInsets.zero,
  103. tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  104. ),
  105. child: const Icon(
  106. Icons.menu,
  107. color: Colors.white,
  108. ),
  109. onPressed: null,
  110. ),
  111. const SizedBox(width: 16.0),
  112. TextButton(
  113. style: TextButton.styleFrom(
  114. minimumSize: const Size(0.0, 0.0),
  115. padding: EdgeInsets.zero,
  116. tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  117. ),
  118. child: const Icon(
  119. Icons.logout,
  120. color: Color(0xFFFF0000),
  121. ),
  122. onPressed: () => Navigator.pop(context),
  123. ),
  124. ],
  125. ),
  126. ),
  127. body: SafeArea(
  128. child: _isLoading ? const Center(
  129. child: CircularProgressIndicator(),
  130. ) : SingleChildScrollView(
  131. child: _hasError ? Center(
  132. child: AlertDialog(
  133. title: Text(
  134. '¡Error!',
  135. textAlign: TextAlign.center,
  136. style: GoogleFonts.roboto(
  137. color: const Color.fromRGBO(255, 0, 0, 1),
  138. fontWeight: FontWeight.bold,
  139. fontSize: 24.0,
  140. ),
  141. ),
  142. content: Column(
  143. children: [
  144. Image.asset(
  145. 'assets/error.png',
  146. width: 58.0,
  147. ),
  148. const SizedBox(height: 16.0),
  149. Text(
  150. 'No hemos podido encontrar nungún perfil relacionado a su número de usuario. Por favor contacte al departamento de TI de S&P.',
  151. textAlign: TextAlign.center,
  152. style: GoogleFonts.roboto(
  153. color: Colors.black54,
  154. fontWeight: FontWeight.w300,
  155. ),
  156. ),
  157. const SizedBox(height: 8.0),
  158. ],
  159. ),
  160. ),
  161. ) : Column(
  162. mainAxisAlignment: MainAxisAlignment.center,
  163. children: [
  164. Container(
  165. width: _screenSize.width-64.0,
  166. margin: const EdgeInsets.only(top: 16.0),
  167. child: Text(
  168. 'Acceder como cliente',
  169. textAlign: TextAlign.center,
  170. style: GoogleFonts.roboto(
  171. fontSize: 28.0,
  172. ),
  173. ),
  174. ),
  175. Container(
  176. margin: const EdgeInsets.symmetric(horizontal: 32.0, vertical: 16.0),
  177. decoration: BoxDecoration(
  178. border: Border.all(
  179. width: 2.0,
  180. ),
  181. boxShadow: [
  182. BoxShadow(
  183. color: Colors.grey.withOpacity(0.5),
  184. spreadRadius: 3,
  185. blurRadius: 7,
  186. offset: const Offset(0,3),
  187. ),
  188. ],
  189. color: Colors.white,
  190. ),
  191. width: _screenSize.width-64.0,
  192. child: Column(
  193. children: [
  194. Row(
  195. children: [
  196. Container(
  197. width: 98.0,
  198. padding: const EdgeInsets.all(8.0),
  199. decoration: const BoxDecoration(
  200. border: Border(
  201. bottom: BorderSide(
  202. width: 2.0,
  203. ),
  204. ),
  205. ),
  206. child: Text(
  207. 'Perfil:',
  208. style: GoogleFonts.roboto(
  209. fontSize: 16.0,
  210. fontWeight: FontWeight.bold,
  211. ),
  212. ),
  213. ),
  214. Expanded(
  215. child: Container(
  216. padding: const EdgeInsets.all(8.0),
  217. decoration: const BoxDecoration(
  218. border: Border(
  219. bottom: BorderSide(
  220. width: 2.0,
  221. ),
  222. ),
  223. ),
  224. child: Text(
  225. _perfilParaClientes,
  226. style: GoogleFonts.roboto(
  227. fontSize: 16.0,
  228. ),
  229. ),
  230. ),
  231. )
  232. ],
  233. ),
  234. Row(
  235. children: [
  236. Container(
  237. width: 98.0,
  238. padding: const EdgeInsets.all(8.0),
  239. child: Text(
  240. 'Usuario:',
  241. style: GoogleFonts.roboto(
  242. fontSize: 16.0,
  243. fontWeight: FontWeight.bold,
  244. ),
  245. ),
  246. ),
  247. Expanded(
  248. child: Container(
  249. padding: const EdgeInsets.all(8.0),
  250. child: Text(
  251. _dinum,
  252. style: GoogleFonts.roboto(
  253. fontSize: 16.0,
  254. ),
  255. ),
  256. ),
  257. )
  258. ],
  259. )
  260. ],
  261. ),
  262. ),
  263. Container(
  264. child: Text(
  265. 'Nombre de Cliente',
  266. style: GoogleFonts.roboto(
  267. fontSize: 16.0,
  268. fontWeight: FontWeight.bold,
  269. ),
  270. ),
  271. margin: const EdgeInsets.fromLTRB(32.0, 8.0, 32.0, 4.0),
  272. width: double.infinity,
  273. ),
  274. Container(
  275. child: DropdownButton<String>(
  276. value: _usuarioActual.isEmpty ? null : _usuarioActual,
  277. hint: Text(
  278. 'Seleccione un usuario',
  279. style: GoogleFonts.roboto(
  280. color: Colors.grey,
  281. ),
  282. ),
  283. items: _listaNombres.map((e) => DropdownMenuItem<String>(
  284. value: e,
  285. child: SizedBox(
  286. width: _screenSize.width - 96.0,
  287. child: Text(
  288. e,
  289. style: GoogleFonts.roboto(
  290. color: Colors.black,
  291. ),
  292. ),
  293. ),
  294. )).toList(),
  295. onChanged: (val) => setState(() {
  296. _usuarioActual = val!;
  297. _nCliController.clear();
  298. }),
  299. ),
  300. margin: const EdgeInsets.fromLTRB(32.0, 0.0, 32.0, 8.0),
  301. ),
  302. Container(
  303. child: Text(
  304. 'Número de Cliente',
  305. style: GoogleFonts.roboto(
  306. fontSize: 16.0,
  307. fontWeight: FontWeight.bold,
  308. ),
  309. ),
  310. margin: const EdgeInsets.fromLTRB(32.0, 0.0, 32.0, 8.0),
  311. width: double.infinity,
  312. ),
  313. Container(
  314. margin: const EdgeInsets.fromLTRB(32.0, 0.0, 32.0, 8.0),
  315. child: TextField(
  316. decoration: InputDecoration(
  317. border: const OutlineInputBorder(),
  318. contentPadding: const EdgeInsets.symmetric(horizontal: 4.0, vertical: 0.0),
  319. hintText: 'Requerido',
  320. hintStyle: GoogleFonts.roboto(
  321. fontStyle: FontStyle.italic,
  322. ),
  323. ),
  324. style: GoogleFonts.roboto(),
  325. controller: _nCliController,
  326. keyboardType: TextInputType.number,
  327. onChanged: (val) => setState(() {
  328. _usuarioActual = '';
  329. }),
  330. ),
  331. ),
  332. Container(
  333. width: _screenSize.width-64.0,
  334. margin: const EdgeInsets.symmetric(horizontal: 32.0),
  335. padding: const EdgeInsets.all(8.0),
  336. color: const Color(0xFFCCCCCC),
  337. child: Text(
  338. 'Debe indicar únicamente el Nombre del Cliente o el Número del Cliente para continuar.',
  339. style: GoogleFonts.roboto(
  340. color: Colors.white,
  341. fontSize: 18.0,
  342. ),
  343. ),
  344. ),
  345. ElevatedButton(
  346. child: Row(
  347. mainAxisSize: MainAxisSize.min,
  348. children: [
  349. Text(
  350. 'Acceder',
  351. style: GoogleFonts.roboto(
  352. fontSize: 16.0,
  353. ),
  354. ),
  355. const SizedBox(width: 8.0),
  356. const Icon(Icons.login)
  357. ],
  358. ),
  359. onPressed: _nCliController.text.isEmpty && _usuarioActual.isEmpty ? null : () async{
  360. FocusScope.of(context).requestFocus(FocusNode());
  361. Fluttertoast.showToast(
  362. msg: 'Validando datos...',
  363. toastLength: Toast.LENGTH_SHORT,
  364. gravity: ToastGravity.CENTER,
  365. timeInSecForIosWeb: 3,
  366. );
  367. String id = '';
  368. if(_nCliController.text.isEmpty && _usuarioActual.isNotEmpty){
  369. id = _listaID[_listaNombres.indexOf(_usuarioActual)];
  370. }else if(_nCliController.text.isNotEmpty && _usuarioActual.isEmpty){
  371. id = _nCliController.text;
  372. }
  373. if(!_listaID.contains(id)){
  374. Fluttertoast.showToast(
  375. msg: 'El número de cliente ingresado es inválido',
  376. toastLength: Toast.LENGTH_LONG,
  377. gravity: ToastGravity.CENTER,
  378. timeInSecForIosWeb: 3,
  379. );
  380. }else{
  381. final cliente = await _cliProvider.fetchLoginComoCliente(_token, _dinum, id);
  382. final data = await db.collection('clientes').doc(_dbKey).get();
  383. data!['accesToken'] = cliente.result.accessToken;
  384. data['dinumAfter'] = data['dinum'];
  385. data['userAfter'] = data['user'];
  386. data['dinum'] = cliente.result.dinum;
  387. data['user'] = cliente.result.user;
  388. await db.collection('clientes').doc(_dbKey).set(data);
  389. await Navigator.push(context, MaterialPageRoute(
  390. builder: (context) => const InicioClientes(),
  391. ));
  392. }
  393. },
  394. )
  395. ],
  396. ),
  397. ),
  398. ),
  399. );
  400. }
  401. }