repo_service.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import 'dart:convert';
  2. import 'package:intl/intl.dart';
  3. import 'package:path/path.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import '../models/models.dart';
  7. class RepoService<T> {
  8. static int dbVersion = 8;
  9. static String dbName = 'olivamiapos01.db';
  10. static const String id = Basico.identificadorWeb;
  11. static const String idLocal = Basico.identificadorLocal;
  12. static Database? _db;
  13. final Map<String, dynamic> contexto = {
  14. 'Categoria': CategoriaProducto(),
  15. 'Producto': Producto(),
  16. 'Pedido': Pedido(productos: []),
  17. 'PedidoProducto': PedidoProducto(),
  18. 'Gasto': Gasto(),
  19. 'Deposito': Deposito(),
  20. 'CorteCaja': CorteCaja(),
  21. };
  22. Future<Database?> get db async {
  23. if (_db != null) return _db;
  24. _db = await databaseInit();
  25. return _db;
  26. }
  27. Future<Database> databaseInit() async {
  28. String dir = (await getApplicationDocumentsDirectory()).path;
  29. String path = join(dir, dbName);
  30. return await openDatabase(
  31. path,
  32. version: dbVersion,
  33. onCreate: _onCreate,
  34. onUpgrade: _onUpgrade,
  35. );
  36. }
  37. _onCreate(Database db, int version) async {
  38. contexto.forEach((String nombre, dynamic modelo) async {
  39. Map<String, dynamic> model = json.decode(json.encode(modelo.toJson()));
  40. String sql =
  41. "CREATE TABLE ${modelo.runtimeType.toString()} (id INTEGER PRIMARY KEY AUTOINCREMENT";
  42. model.forEach((String key, dynamic value) {
  43. if (key != "id") {
  44. String tipo = value.runtimeType.toString();
  45. String sqlType = "";
  46. if (equals(tipo, 'int')) {
  47. sqlType = "INTEGER";
  48. } else if (equals(tipo, 'double')) {
  49. sqlType = "REAL";
  50. } else if (equals(tipo, 'bool')) {
  51. sqlType = "BOOLEAN";
  52. } else {
  53. sqlType = "TEXT";
  54. }
  55. sql += ", $key $sqlType";
  56. }
  57. });
  58. sql += ")";
  59. await db.execute(sql);
  60. await db.execute('''
  61. CREATE TABLE PedidoProductoTopping (
  62. id INTEGER PRIMARY KEY AUTOINCREMENT,
  63. idPedidoProducto INTEGER,
  64. idTopping INTEGER,
  65. FOREIGN KEY (idPedidoProducto) REFERENCES PedidoProducto(id),
  66. FOREIGN KEY (idTopping) REFERENCES Producto(id)
  67. )
  68. ''');
  69. if (modelo.runtimeType.toString() == 'Pedido') {
  70. await db.execute(
  71. 'ALTER TABLE Pedido ADD COLUMN descuento INTEGER DEFAULT 0');
  72. }
  73. await db.execute('''
  74. CREATE TABLE Descuento (
  75. id INTEGER PRIMARY KEY AUTOINCREMENT,
  76. porcentaje INTEGER
  77. )
  78. ''');
  79. await db.insert('Descuento', {'porcentaje': 0});
  80. await db.insert('Descuento', {'porcentaje': 5});
  81. await db.insert('Descuento', {'porcentaje': 10});
  82. await db.insert('Descuento', {'porcentaje': 15});
  83. await db.insert('Descuento', {'porcentaje': 20});
  84. await db.insert('Descuento', {'porcentaje': 25});
  85. await db.insert('Descuento', {'porcentaje': 30});
  86. });
  87. await db.execute('''
  88. CREATE TABLE ProductoTopping (
  89. id INTEGER PRIMARY KEY AUTOINCREMENT,
  90. idProducto INTEGER,
  91. idTopping INTEGER,
  92. FOREIGN KEY (idProducto) REFERENCES Producto(id),
  93. FOREIGN KEY (idTopping) REFERENCES Producto(id)
  94. )
  95. ''');
  96. await db.execute('''
  97. CREATE TABLE Variable (
  98. id INTEGER PRIMARY KEY AUTOINCREMENT,
  99. nombre TEXT,
  100. clave TEXT,
  101. descripcion TEXT,
  102. activo BOOLEAN,
  103. idLocal INTEGER,
  104. eliminado TEXT
  105. )
  106. ''');
  107. }
  108. void _onUpgrade(Database db, int oldVersion, int newVersion) async {
  109. while (oldVersion < newVersion) {
  110. switch (oldVersion) {
  111. case 1:
  112. await db.execute(
  113. "ALTER TABLE CategoriaProducto ADD COLUMN esToping INTEGER DEFAULT 0");
  114. await db.execute(
  115. "ALTER TABLE CategoriaProducto ADD COLUMN descripcion TEXT DEFAULT ''");
  116. await db.execute(
  117. "ALTER TABLE CategoriaProducto ADD COLUMN maximo INTEGER");
  118. break;
  119. case 2:
  120. await db.execute('''
  121. CREATE TABLE ProductoTopping (
  122. id INTEGER PRIMARY KEY AUTOINCREMENT,
  123. idProducto INTEGER,
  124. idTopping INTEGER,
  125. FOREIGN KEY (idProducto) REFERENCES Producto(id),
  126. FOREIGN KEY (idTopping) REFERENCES Producto(id)
  127. )
  128. ''');
  129. break;
  130. case 3:
  131. await db.execute('''
  132. CREATE TABLE PedidoProductoTopping (
  133. id INTEGER PRIMARY KEY AUTOINCREMENT,
  134. idPedidoProducto INTEGER,
  135. idTopping INTEGER,
  136. FOREIGN KEY (idPedidoProducto) REFERENCES PedidoProducto(id),
  137. FOREIGN KEY (idTopping) REFERENCES Producto(id)
  138. )
  139. ''');
  140. break;
  141. case 4:
  142. await db.execute('''
  143. ALTER TABLE Pedido ADD COLUMN descuento INTEGER DEFAULT 0
  144. ''');
  145. break;
  146. case 5:
  147. await db.execute('''
  148. CREATE TABLE IF NOT EXISTS Descuento (
  149. id INTEGER PRIMARY KEY AUTOINCREMENT,
  150. porcentaje INTEGER
  151. )
  152. ''');
  153. await db.insert('Descuento', {'porcentaje': 0});
  154. await db.insert('Descuento', {'porcentaje': 5});
  155. await db.insert('Descuento', {'porcentaje': 10});
  156. await db.insert('Descuento', {'porcentaje': 15});
  157. await db.insert('Descuento', {'porcentaje': 20});
  158. await db.insert('Descuento', {'porcentaje': 25});
  159. await db.insert('Descuento', {'porcentaje': 30});
  160. break;
  161. case 6:
  162. await db.execute('''
  163. ALTER TABLE Pedido ADD COLUMN tipoPago TEXT DEFAULT '';
  164. ''');
  165. await db.execute('''
  166. ALTER TABLE Pedido ADD COLUMN cantEfectivo REAL DEFAULT 0;
  167. ''');
  168. await db.execute('''
  169. ALTER TABLE Pedido ADD COLUMN cantTarjeta REAL DEFAULT 0;
  170. ''');
  171. await db.execute('''
  172. ALTER TABLE Pedido ADD COLUMN cantTransferencia REAL DEFAULT 0;
  173. ''');
  174. break;
  175. case 7:
  176. await db.execute('''
  177. CREATE TABLE Variable (
  178. id INTEGER PRIMARY KEY AUTOINCREMENT,
  179. nombre TEXT,
  180. clave TEXT,
  181. descripcion TEXT,
  182. activo BOOLEAN,
  183. idLocal INTEGER,
  184. eliminado TEXT
  185. )
  186. ''');
  187. break;
  188. }
  189. oldVersion++;
  190. }
  191. }
  192. Future<int> guardar(T model) async {
  193. try {
  194. var dbClient = await db;
  195. String nombreTabla = model.runtimeType.toString();
  196. String modelo = json.encode(model, toEncodable: toEncodable);
  197. Map<String, dynamic> modelMap = json.decode(modelo);
  198. int id = 0;
  199. if (modelMap['id'] != null && modelMap['id'] != 0) {
  200. await dbClient!.update(
  201. nombreTabla,
  202. modelMap,
  203. where: 'id = ?',
  204. whereArgs: [modelMap['id']],
  205. );
  206. id = modelMap['id'];
  207. } else {
  208. modelMap.remove('id');
  209. id = await dbClient!.insert(nombreTabla, modelMap);
  210. }
  211. if (model is Producto) {
  212. await _guardarToppings(dbClient, id, model.topings);
  213. }
  214. return id;
  215. } catch (e) {
  216. print('Error al guardar en $T: $e');
  217. return 0;
  218. }
  219. }
  220. Future<void> _guardarToppings(
  221. Database db, int idProducto, List<Producto>? topings) async {
  222. await db.delete('ProductoTopping',
  223. where: 'idProducto = ?', whereArgs: [idProducto]);
  224. if (topings != null) {
  225. for (var topping in topings) {
  226. await db.insert('ProductoTopping', {
  227. 'idProducto': idProducto,
  228. 'idTopping': topping.id,
  229. });
  230. }
  231. }
  232. }
  233. Future<int> guardarLocal(T model) async {
  234. var dbClient = await db;
  235. String nombreTabla = model.runtimeType.toString();
  236. String modelo = json.encode(model, toEncodable: toEncodable);
  237. Map<String, dynamic> modelMap = json.decode(modelo);
  238. if (nombreTabla == "PedidoProductoTopping") {
  239. modelMap.remove('idLocal');
  240. modelMap.remove('eliminado');
  241. }
  242. if (modelMap['id'] == null || modelMap['id'] == 0) {
  243. modelMap.remove('id');
  244. }
  245. int resultado;
  246. int? identificadorLocal = modelMap[Basico.identificadorLocal];
  247. if (identificadorLocal != null && identificadorLocal > 0) {
  248. resultado = await dbClient!.update(
  249. nombreTabla,
  250. modelMap,
  251. where: "$idLocal = ?",
  252. whereArgs: [identificadorLocal],
  253. );
  254. } else {
  255. resultado = await dbClient!.insert(nombreTabla, modelMap);
  256. var rawQuery =
  257. await dbClient.rawQuery("SELECT last_insert_rowid() as id");
  258. if (rawQuery.isNotEmpty) {
  259. resultado = int.parse(rawQuery.first["id"].toString());
  260. modelMap[Basico.identificadorLocal] = resultado;
  261. }
  262. }
  263. if (model is Pedido) {
  264. model.id = resultado;
  265. }
  266. if (model is Producto) {
  267. await _guardarToppings(dbClient!, model.id!, model.topings);
  268. }
  269. return resultado;
  270. }
  271. dynamic toEncodable(dynamic item) {
  272. if (item is Pedido) {
  273. return item.toJson();
  274. } else if (item is PedidoProducto) {
  275. return item.toJson();
  276. } else if (item is PedidoProductoTopping) {
  277. return item.toJson();
  278. } else if (item is Producto) {
  279. return item.toJson();
  280. } else if (item is CategoriaProducto) {
  281. return item.toJson();
  282. } else if (item is Variable) {
  283. return item.toJson();
  284. }
  285. throw UnsupportedError(
  286. 'Type not supported for serialization: ${item.runtimeType}');
  287. }
  288. Future<List<T>> obtenerTodos({String orderBy = 'id DESC'}) async {
  289. var db = await this.db;
  290. String tableName = T.toString();
  291. var result = await db!.query(tableName, orderBy: orderBy);
  292. return result.map((map) => fromMap<T>(map)).toList();
  293. }
  294. T fromMap<T>(Map<String, dynamic> map) {
  295. switch (T) {
  296. case Pedido:
  297. return Pedido.fromJson(map) as T;
  298. case Producto:
  299. return Producto.fromJson(map) as T;
  300. default:
  301. throw Exception('Tipo no soportado');
  302. }
  303. }
  304. Future<Pedido?> obtenerPorId(int id) async {
  305. Database? dbClient = await db;
  306. List<Map> maps =
  307. await dbClient!.query('Pedido', where: 'id = ?', whereArgs: [id]);
  308. if (maps.isNotEmpty) {
  309. return Pedido.fromJson(Map<String, dynamic>.from(maps.first));
  310. }
  311. return null;
  312. }
  313. Future<List<PedidoProducto>> obtenerPorIdPedido(int idPedido) async {
  314. Database? dbClient = await db;
  315. List<Map> maps = await dbClient!
  316. .query('PedidoProducto', where: 'idPedido = ?', whereArgs: [idPedido]);
  317. return maps
  318. .map((map) => PedidoProducto.fromJson(Map<String, dynamic>.from(map)))
  319. .toList();
  320. }
  321. Future<int> contarPedidos() async {
  322. Database? dbClient = await db;
  323. var result = await dbClient!.rawQuery('SELECT COUNT(*) FROM Pedido');
  324. return Sqflite.firstIntValue(result) ?? 0;
  325. }
  326. Future<List<Pedido>> obtenerPedidosPaginados(int limit, int offset) async {
  327. Database? dbClient = await db;
  328. List<Map<String, dynamic>> result = await dbClient!
  329. .query('Pedido', limit: limit, offset: offset, orderBy: 'id DESC');
  330. return result.map((map) => Pedido.fromJson(map)).toList();
  331. }
  332. Future<Producto?> obtenerProductoPorId(int idProducto) async {
  333. Database? dbClient = await db;
  334. List<Map> maps = await dbClient!
  335. .query('Producto', where: 'id = ?', whereArgs: [idProducto]);
  336. if (maps.isNotEmpty) {
  337. return Producto.fromJson(Map<String, dynamic>.from(maps.first));
  338. }
  339. return null;
  340. }
  341. Future<List<int>> obtenerToppingsPorProducto(int idProducto) async {
  342. var dbClient = await db;
  343. var result = await dbClient!.query(
  344. 'ProductoTopping',
  345. where: 'idProducto = ?',
  346. whereArgs: [idProducto],
  347. );
  348. return result.map((map) => map['idTopping'] as int).toList();
  349. }
  350. Future<List<PedidoProductoTopping>> obtenerToppingsPorPedidoProducto(
  351. int idPedidoProducto) async {
  352. var dbClient = await db;
  353. List<Map> maps = await dbClient!.query('PedidoProductoTopping',
  354. where: 'idPedidoProducto = ?', whereArgs: [idPedidoProducto]);
  355. if (maps.isNotEmpty) {
  356. return maps
  357. .map((map) =>
  358. PedidoProductoTopping.fromJson(Map<String, dynamic>.from(map)))
  359. .toList();
  360. }
  361. return [];
  362. }
  363. Future<int> obtenerProximoFolio() async {
  364. var dbClient = await db;
  365. var result = await dbClient!.rawQuery(
  366. 'SELECT MAX(CAST(folio AS INTEGER)) as last_folio FROM Pedido');
  367. if (result.isNotEmpty && result.first["last_folio"] != null) {
  368. return int.tryParse(result.first["last_folio"].toString())! + 1;
  369. }
  370. return 1;
  371. }
  372. Future<List<T>> buscarPorFolio(String folio) async {
  373. var dbClient = await db;
  374. List<Map<String, dynamic>> maps = await dbClient!.query(
  375. 'Pedido',
  376. where: 'folio LIKE ?',
  377. whereArgs: ['%$folio%'],
  378. );
  379. return maps.map((map) => fromMap<T>(map)).toList();
  380. }
  381. Future<List<Pedido>> buscarPorFecha(
  382. DateTime startDate, DateTime endDate) async {
  383. var dbClient = await db;
  384. String startDateString =
  385. DateFormat('yyyy-MM-dd 00:00:00').format(startDate);
  386. String endDateString = DateFormat('yyyy-MM-dd 23:59:59').format(endDate);
  387. List<Map<String, dynamic>> maps = await dbClient!.rawQuery('''
  388. SELECT * FROM Pedido
  389. WHERE
  390. (datetime(substr(peticion, 7, 4) || '-' || substr(peticion, 4, 2) || '-' || substr(peticion, 1, 2) || ' ' || substr(peticion, 12)) BETWEEN ? AND ?)
  391. OR
  392. (datetime(substr(peticion, 7, 4) || '-' || substr(peticion, 1, 2) || '-' || substr(peticion, 4, 2) || ' ' || substr(peticion, 12)) BETWEEN ? AND ?)
  393. ''', [startDateString, endDateString, startDateString, endDateString]);
  394. return maps.map((map) => Pedido.fromJson(map)).toList();
  395. }
  396. Future<List<CorteCaja>> buscarPorFechaCorte(
  397. DateTime startDate, DateTime endDate) async {
  398. var dbClient = await db;
  399. String startDateString = DateFormat('dd-MM-yyyy').format(startDate);
  400. String endDateString = DateFormat('dd-MM-yyyy 23:59:59').format(endDate);
  401. List<Map<String, dynamic>> maps = await dbClient!.query(
  402. 'CorteCaja',
  403. where: 'fecha BETWEEN ? AND ?',
  404. whereArgs: [startDateString, endDateString],
  405. );
  406. return maps.map((map) => CorteCaja.fromJson(map)).toList();
  407. }
  408. Future<void> eliminar<T>(int id) async {
  409. var dbClient = await db;
  410. String tableName = T.toString();
  411. await dbClient!.delete(tableName, where: 'id = ?', whereArgs: [id]);
  412. }
  413. Future<List<Descuento>> obtenerTodosDescuentos() async {
  414. var dbClient = await db;
  415. var result = await dbClient!.query('Descuento', orderBy: 'porcentaje ASC');
  416. return result.map((map) => Descuento.fromJson(map)).toList();
  417. }
  418. Future<int> guardarDescuento(Descuento descuento) async {
  419. var dbClient = await db;
  420. if (descuento.id != null && descuento.id! > 0) {
  421. return await dbClient!.update(
  422. 'Descuento',
  423. descuento.toJson(),
  424. where: 'id = ?',
  425. whereArgs: [descuento.id],
  426. );
  427. } else {
  428. return await dbClient!.insert('Descuento', descuento.toJson());
  429. }
  430. }
  431. Future<int> eliminarDescuento(int id) async {
  432. var dbClient = await db;
  433. return await dbClient!
  434. .delete('Descuento', where: 'id = ?', whereArgs: [id]);
  435. }
  436. }