dolibarrWriter.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import settings
  2. import os
  3. import codecs
  4. class Writer(object):
  5. output_files = settings.get('OUTPUT_FILES')
  6. output_dir = settings.get('OUTPUT_DIR')
  7. @staticmethod
  8. def write(journal, entries, years):
  9. filename = Writer.output_files[journal]
  10. output = os.path.join(Writer.output_dir, filename)
  11. entries_by_year = entries.get_by_year()
  12. for year in entries_by_year:
  13. if year in years:
  14. output_file = output.replace("%year%", year)
  15. output_dir = os.path.dirname(output_file)
  16. if not os.path.exists(output_dir):
  17. os.makedirs(os.path.dirname(output_file))
  18. elif not os.path.isdir(output_dir):
  19. print "Error: %s is not a dir\n" % (output_dir)
  20. raise os.error()
  21. f = codecs.open(output_file, 'w', 'utf-8')
  22. for entry in entries_by_year[year]:
  23. f.write(entry.get_ledger())
  24. f.write("\n")
  25. f.close()
  26. @staticmethod
  27. def write_hreport_chart_of_accounts(years):
  28. pc_names = settings.get('PC_NAMES')
  29. pc_descriptions = settings.get('PC_DESCRIPTIONS')
  30. for year in years:
  31. filename = Writer.output_files['chart_of_accounts']
  32. output_file = os.path.join(Writer.output_dir, filename).replace("%year%", year)
  33. f = codecs.open(output_file, 'w', 'utf-8')
  34. f.write("%s/01/01 * Chart of account\n" % (year))
  35. for pc in sorted(pc_names.keys()):
  36. name = pc_names[pc]
  37. if pc in pc_descriptions:
  38. desc = pc_descriptions[pc]
  39. else:
  40. desc = name
  41. s = " %s 0 ; %s %s\n" % (name, pc, desc)
  42. f.write(s)
  43. f.close()