clienti_dao.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'package:drift/drift.dart';
  2. import '../database.dart';
  3. class ClientiDao {
  4. final AppDatabase db;
  5. ClientiDao(this.db);
  6. Future<Cliente?> getById(int id) {
  7. return (db.select(db.clienti)..where((t) => t.idCliente.equals(id))).getSingleOrNull();
  8. }
  9. Future<Cliente?> getByCode(int idAgente, String code) {
  10. return (db.select(db.clienti).join([
  11. innerJoin(db.clientiAgente, db.clientiAgente.idCliente.equalsExp(db.clienti.idCliente) & db.clientiAgente.idAgente.equals(idAgente)),
  12. ])
  13. ..where(db.clienti.codCliente.equals(code)))
  14. .map((row) => row.readTable(db.clienti))
  15. .getSingleOrNull();
  16. }
  17. Future<Cliente?> getByRemoteId(int idAgente, int remoteId) {
  18. return (db.select(db.clienti)..where((t) => t.idClienteRemoto.equals(remoteId))).getSingleOrNull();
  19. }
  20. Future<List<Cliente>> getClienti(int idAgente, String filter) async {
  21. final words = filter.split(' ').where((w) => w.isNotEmpty).toList();
  22. final q = db.select(db.clienti).join([
  23. innerJoin(db.clientiAgente, db.clientiAgente.idCliente.equalsExp(db.clienti.idCliente) & db.clientiAgente.idAgente.equals(idAgente)),
  24. ])
  25. ..limit(200);
  26. if (words.isNotEmpty) {
  27. for (final word in words) {
  28. final pattern = '%$word%';
  29. q.where(db.clienti.codCliente.like(pattern) | db.clienti.ragioneSociale.like(pattern) | db.clienti.partitaIva.like(pattern));
  30. }
  31. }
  32. return q.map((row) => row.readTable(db.clienti)).get();
  33. }
  34. Future<List<Cliente>> getActiveClients(int idAgente) {
  35. return (db.select(db.clienti).join([
  36. innerJoin(
  37. db.clientiAgente,
  38. db.clientiAgente.idCliente.equalsExp(db.clienti.idCliente) & db.clientiAgente.idAgente.equals(idAgente),
  39. ),
  40. ])
  41. ..where(db.clienti.attivo.equals(1))
  42. ..orderBy([OrderingTerm(expression: db.clienti.ragioneSociale)]))
  43. .map((row) => row.readTable(db.clienti))
  44. .get();
  45. }
  46. Future<List<TopCustomerData>> topCustomers(int limit) async {
  47. return (db.select(db.topCustomer)..limit(limit)).get();
  48. }
  49. Future<List<Cliente>> getNewCustomers(int idAgente) {
  50. return (db.select(db.clienti).join([
  51. innerJoin(db.clientiAgente, db.clientiAgente.idCliente.equalsExp(db.clienti.idCliente) & db.clientiAgente.idAgente.equals(idAgente)),
  52. ])
  53. ..where(db.clienti.stato.equals('N')))
  54. .map((row) => row.readTable(db.clienti))
  55. .get();
  56. }
  57. Future<int> countNewCustomers() async {
  58. final rows = await (db.select(db.clienti)..where((t) => t.stato.equals('N'))).get();
  59. return rows.length;
  60. }
  61. Future<List<Cliente>> inactiveCustomers(int idAgente, String sinceDate) async {
  62. final recentIds = await (db.select(db.ordini)..where((t) => t.idCliente.isNotNull())).get().then((orders) => orders.map((o) => o.idCliente!).toSet());
  63. return (db.select(db.clienti).join([
  64. innerJoin(db.clientiAgente, db.clientiAgente.idCliente.equalsExp(db.clienti.idCliente) & db.clientiAgente.idAgente.equals(idAgente)),
  65. ])
  66. ..where(db.clienti.idCliente.isNotIn(recentIds)))
  67. .map((row) => row.readTable(db.clienti))
  68. .get();
  69. }
  70. Future<int> countCustomersToSend() async {
  71. final rows = await (db.select(db.clienti)..where((t) => t.stato.equals('N'))).get();
  72. return rows.length;
  73. }
  74. Future<int> countCustomersToSendForAgent(int idAgente) async {
  75. final rows = await (db.select(db.clienti).join([
  76. innerJoin(db.clientiAgente, db.clientiAgente.idCliente.equalsExp(db.clienti.idCliente) & db.clientiAgente.idAgente.equals(idAgente)),
  77. ])
  78. ..where(db.clienti.stato.equals('N')))
  79. .map((row) => row.readTable(db.clienti))
  80. .get();
  81. return rows.length;
  82. }
  83. Future<void> markAsSent(int idCliente, int idAssegnato) {
  84. return (db.update(db.clienti)..where((t) => t.idCliente.equals(idCliente))).write(ClientiCompanion(stato: const Value('T')));
  85. }
  86. Future<List<ArticoliConPromoData>> getFavoriteItems(
  87. int idCliente,
  88. String dateToUse,
  89. String filter,
  90. ) async {
  91. var filtro = '%$filter';
  92. if (filtro.length > 13) filtro = filtro.substring(0, 13);
  93. filtro += '%';
  94. final sql = '''
  95. SELECT DISTINCT a.idArticolo, a.codArticolo, a.descrizione, 0 "prezzoBase",
  96. a.attivo, a."nonDisponibile",
  97. CASE WHEN EXISTS (
  98. SELECT pa.idArticolo FROM "PromoArticoli" pa
  99. JOIN "Promozioni" p ON pa.progressivo = p.progressivo
  100. WHERE pa.idArticolo = a.idArticolo
  101. AND date('now') BETWEEN p."dataInizio" AND p."dataFine"
  102. ) THEN 1 ELSE 0 END AS inPromo,
  103. (SELECT ROUND(sv.importo / sv.qta, 2)
  104. FROM "Statistiche_vendita" sv
  105. WHERE sv.idCliente = ? AND sv.idArticolo = a.idArticolo
  106. ORDER BY sv."dataFattura" DESC LIMIT 1 OFFSET 1) AS ultimoPrezzo,
  107. (SELECT sv."dataFattura"
  108. FROM "Statistiche_vendita" sv
  109. WHERE sv.idCliente = ? AND sv.idArticolo = a.idArticolo
  110. ORDER BY sv."dataFattura" DESC LIMIT 1 OFFSET 1) AS lastDateSold
  111. FROM "Listini_cliente" lc
  112. JOIN "Articoli" a ON lc."idArticolo" = a.idArticolo
  113. WHERE lc.idCliente = ?
  114. AND (lc."validoDa" IS NULL OR lc."validoDa" <= ?)
  115. AND (lc."validoA" IS NULL OR lc."validoA" >= ?)
  116. AND a.attivo = 1
  117. AND lc.stato <> 'X'
  118. AND lc.prezzo > 0.001
  119. AND (a.descrizione LIKE ? OR a."codArticolo" LIKE ?)
  120. ORDER BY a.descrizione
  121. ''';
  122. final rows = await db.customSelect(
  123. sql,
  124. variables: [
  125. Variable.withInt(idCliente),
  126. Variable.withInt(idCliente),
  127. Variable.withInt(idCliente),
  128. Variable.withString(dateToUse),
  129. Variable.withString(dateToUse),
  130. Variable.withString(filtro),
  131. Variable.withString(filtro),
  132. ],
  133. ).get();
  134. return rows.map((row) => ArticoliConPromoData.fromJson(row.data)).toList();
  135. }
  136. Future<List<ArticoliConPromoData>> getAlreadySoldItems(
  137. int idAgente,
  138. int idCliente,
  139. String dateToUse,
  140. String filter,
  141. ) async {
  142. final words = filter.split(' ').where((w) => w.isNotEmpty).toList();
  143. final buffer = StringBuffer('''
  144. SELECT DISTINCT a.idArticolo, a.codArticolo, a.descrizione, 0 "prezzoBase",
  145. a.attivo, a."nonDisponibile",
  146. 0 AS inPromo,
  147. ROUND(sv.importo / sv.qta, 2) AS ultimoPrezzo,
  148. sv."dataFattura" AS lastDateSold
  149. FROM "Articoli" a
  150. JOIN "Statistiche_vendita" sv ON sv.idArticolo = a.idArticolo
  151. WHERE a.attivo = 1
  152. and sv.idCliente = ?
  153. AND sv.idArticolo = a.idArticolo
  154. AND sv."dataFattura" >= ?
  155. AND sv."tipoVendita" = 'N'
  156. ''');
  157. final variables = <Variable>[
  158. Variable.withInt(idCliente),
  159. Variable.withString(dateToUse),
  160. ];
  161. if (words.isNotEmpty) {
  162. for (final word in words) {
  163. buffer.write(' AND (a."codArticolo" LIKE ? OR a.descrizione LIKE ?)');
  164. final pattern = '%$word%';
  165. variables.addAll([
  166. Variable.withString(pattern),
  167. Variable.withString(pattern),
  168. ]);
  169. }
  170. }
  171. buffer.write(' ORDER BY a.descrizione');
  172. final rows = await db
  173. .customSelect(
  174. buffer.toString(),
  175. variables: variables,
  176. )
  177. .get();
  178. return rows.map((row) => ArticoliConPromoData.fromJson(row.data)).toList();
  179. }
  180. Future<Agente?> getAgenteOf(String? codCliente) async {
  181. if (codCliente == null) return null;
  182. return (db.select(db.agenti).join([
  183. innerJoin(
  184. db.clientiAgente,
  185. db.clientiAgente.idAgente.equalsExp(db.agenti.idAgente) & db.clientiAgente.attivo.equals(1),
  186. ),
  187. innerJoin(
  188. db.clienti,
  189. db.clienti.idCliente.equalsExp(db.clientiAgente.idCliente) & db.clienti.codCliente.equals(codCliente),
  190. ),
  191. ])
  192. ..limit(1))
  193. .map((row) => row.readTable(db.agenti))
  194. .getSingleOrNull();
  195. }
  196. }