dolibarr.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python3
  2. # Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/
  3. # Docstring Conventions https://www.python.org/dev/peps/pep-0257/
  4. #import psycopg2.sql as sql # >= 2.7
  5. from . import sql
  6. class Instance:
  7. def __init__(self, conn):
  8. """Create an instance
  9. Arguments:
  10. conn - Database connection (psycopg2)"""
  11. self._conn = conn
  12. self._cur = conn.cursor()
  13. def get_adherent(self, fields=None, extrafields=None, activeonly=True):
  14. if fields is None:
  15. fields = ["firstname", "lastname", "email"]
  16. if extrafields is None:
  17. extrafields = []
  18. if activeonly:
  19. sqlcond = sql.SQL('adh.statut = 1')
  20. else:
  21. sqlcond = sql.SQL('1 = 1')
  22. # Prepare query
  23. sqlfields = [sql.SQL('adh.') + sql.Identifier(a) for a in fields] + [sql.SQL('adhx.') + sql.Identifier(b) for b in extrafields]
  24. sqlfields = sql.SQL(',').join(sqlfields)
  25. sqljoin = sql.SQL('LEFT OUTER JOIN llx_adherent_extrafields AS adhx ON (adh.rowid = adhx.fk_object)')
  26. sqlquery = sql.SQL("SELECT {0} FROM llx_adherent AS adh {1} WHERE {2}").format(sqlfields, sqljoin, sqlcond)
  27. self._cur.execute(sqlquery.as_string(self._cur))
  28. return self._cur
  29. def get_adherent_count(self, activeonly=True):
  30. if activeonly:
  31. sqlcond = sql.SQL('adh.statut = 1')
  32. else:
  33. sqlcond = sql.SQL('1 = 1')
  34. # Prepare query
  35. sqlquery = sql.SQL("SELECT COUNT(*) FROM llx_adherent WHERE {2}").format(sqlcond)
  36. # Execute query
  37. self._cur.execute(sqlquery)
  38. return self._cur.fetchone()[0]
  39. def get_subscriber(self, productRef, fields=None, activeonly=True):
  40. # Prepare query
  41. if fields is None:
  42. fields = ['email']
  43. sqlfields = [sql.SQL('llx_societe.') + sql.Identifier(field) for field in fields]
  44. sqlfields = sql.SQL(',').join(sqlfields)
  45. sqlcond = sql.SQL("""llx_societe.rowid=llx_contrat.fk_soc
  46. AND llx_contrat.rowid=llx_contratdet.fk_contrat
  47. AND llx_product.rowid=llx_contratdet.fk_product
  48. AND llx_product.ref LIKE {0}
  49. AND llx_contratdet.statut=4""").format(sql.Literal(productRef))
  50. if activeonly:
  51. sqlcond += sql.SQL('AND llx_contrat.statut=1')
  52. sqlquery = sql.SQL("""SELECT {0}
  53. FROM llx_societe, llx_contrat, llx_contratdet, llx_product
  54. WHERE {1}
  55. ORDER BY llx_product.ref, llx_contrat.rowid ASC""").format(sqlfields, sqlcond)
  56. self._cur.execute(sqlquery.as_string(self._cur))
  57. return self._cur
  58. def get_bank_accounts(self, fields=None):
  59. # Prepare query
  60. if fields is None:
  61. fields = ['bank', 'proprio', 'iban_prefix']
  62. sqlquery = sql.SQL('SELECT {0} FROM llx_bank_account WHERE courant = 1 AND clos = 0'.format(','.join(fields)))
  63. self._cur.execute(sqlquery.as_string(self._cur))
  64. return self._cur