fcn-dolibarr 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/python3
  2. import argparse
  3. import configparser
  4. import psycopg2
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("selection", type=str,
  7. choices=["get-subscribers"])
  8. parser.add_argument("-c", "--config", type=str,
  9. default="/etc/fcn-toolbox/config.ini",
  10. help="specify a configuration file")
  11. parser.add_argument("--product", type=str,
  12. default="%",
  13. help="product code (can contain %)")
  14. args = parser.parse_args()
  15. config = configparser.RawConfigParser()
  16. config.sections()
  17. config.read(args.config)
  18. configdb = config['database']
  19. conn = psycopg2.connect(database=configdb['database'],
  20. user=configdb['user'], password=configdb['password'])
  21. cur = conn.cursor()
  22. def getMemberCount():
  23. cur.execute("SELECT count(*) FROM llx_adherent WHERE statut = '1'")
  24. return cur.fetchone()[0]
  25. def getSubscriberInfo(product, fields):
  26. fieldsel = ','.join(fields)
  27. cur.execute("""SELECT %s
  28. FROM llx_societe, llx_contrat, llx_contratdet, llx_product
  29. WHERE llx_societe.rowid=llx_contrat.fk_soc
  30. AND llx_contrat.rowid=llx_contratdet.fk_contrat
  31. AND llx_product.rowid=llx_contratdet.fk_product
  32. AND llx_product.ref LIKE %s
  33. AND llx_contrat.statut=1
  34. AND llx_contratdet.statut=4
  35. ORDER BY llx_product.ref, llx_contrat.rowid ASC;""" % (fieldsel, '%s'), (product, ))
  36. return cur.fetchall()
  37. if args.selection == "get-subscribers":
  38. subscribers = getSubscriberInfo(args.product, ['llx_societe.email'])
  39. print("\n".join(map(lambda x: x[0], subscribers)))