cli.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import unicode_literals
  4. import getopt
  5. import locale
  6. import logging
  7. import sys
  8. import codecs
  9. import getpass
  10. from himport import settings
  11. from himport.dolibarrWriter import Writer
  12. from himport.dolibarrAlchemyHledger import HledgerDolibarrSQLAlchemy
  13. logging.basicConfig(level=logging.INFO)
  14. logger = logging.getLogger('hreport')
  15. def process_args():
  16. options = {}
  17. usage = '''Usage: himport -v -y <YEAR> [ -y <YEAR> ] ...
  18. options:
  19. -v : verbose mode
  20. -y <YEAR> : import the corresponding accounting year
  21. '''
  22. try:
  23. opts, args = getopt.getopt(
  24. sys.argv[1:], "hvy:",
  25. ["mysql-password=", "mysql-port", "year="]
  26. )
  27. except getopt.GetoptError:
  28. print("himport: Invalid options")
  29. print(usage)
  30. sys.exit(2)
  31. options['years'] = list()
  32. options['verbose'] = 0
  33. for opt, arg in opts:
  34. if opt == '-h':
  35. print(usage)
  36. sys.exit()
  37. elif opt in ("-v", "--verbose"):
  38. options['verbose'] += 1
  39. elif opt in ("-y", "--year"):
  40. options['years'].append(str(arg))
  41. if len(options['years']) == 0:
  42. print("You need to specify the accounting years")
  43. print("")
  44. print(usage)
  45. sys.exit(1)
  46. return options
  47. def do_sqlalchemy(options):
  48. # On recupere les donnees via la base de donnees de dolibarr
  49. s = settings.get('MYSQL_SETTINGS')
  50. password = s['password']
  51. if password is None or password == "":
  52. password = getpass.getpass("password for mysql user '%s': " % (s['user']))
  53. dolibarr = HledgerDolibarrSQLAlchemy(s['host'], s['port'], s['database'], s['user'], password, options['verbose'] >= 2)
  54. dolibarr.connect()
  55. bank_journal = dolibarr.get_bank_journal()
  56. sell_journal = dolibarr.get_sell_journal()
  57. supplier_journal = dolibarr.get_supplier_journal()
  58. social_journal = dolibarr.get_social_journal()
  59. # On verifie s'il manque des postes comptables dans les ecritures
  60. pc_missing = set()
  61. pc_missing.update(bank_journal.check_pc())
  62. pc_missing.update(sell_journal.check_pc())
  63. pc_missing.update(supplier_journal.check_pc())
  64. pc_missing.update(social_journal.check_pc())
  65. if len(pc_missing) > 0:
  66. print("WARNING: poste comptable manquant")
  67. for pc in pc_missing:
  68. print("%s" % (pc))
  69. # On ecrie les fichiers hledger
  70. Writer.write("bank", bank_journal, options['years'])
  71. Writer.write("sells", sell_journal, options['years'])
  72. Writer.write("suppliers", supplier_journal, options['years'])
  73. Writer.write("social", social_journal, options['years'])
  74. Writer.write_hreport_chart_of_accounts(options['years'])
  75. dolibarr.disconnect()
  76. def main():
  77. locale.setlocale(locale.LC_ALL, 'fr_FR.utf-8')
  78. options = process_args()
  79. do_sqlalchemy(options)
  80. if __name__ == "__main__":
  81. try:
  82. main(sys.argv[1:])
  83. except Exception as e:
  84. print(e.message)
  85. sys.exit(1)