You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
17 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import models, fields
  5. import datetime
  6. class AbstractReportXslx(models.AbstractModel):
  7. _name = 'report.account_financial_report.abstract_report_xlsx'
  8. _inherit = 'report.report_xlsx.abstract'
  9. def __init__(self, pool, cr):
  10. # main sheet which will contains report
  11. self.sheet = None
  12. # columns of the report
  13. self.columns = None
  14. # row_pos must be incremented at each writing lines
  15. self.row_pos = None
  16. # Formats
  17. self.format_right = None
  18. self.format_left = None
  19. self.format_right_bold_italic = None
  20. self.format_bold = None
  21. self.format_header_left = None
  22. self.format_header_center = None
  23. self.format_header_right = None
  24. self.format_header_amount = None
  25. self.format_amount = None
  26. self.format_percent_bold_italic = None
  27. def get_workbook_options(self):
  28. return {'constant_memory': True}
  29. def generate_xlsx_report(self, workbook, data, objects):
  30. report = objects
  31. self.row_pos = 0
  32. self._define_formats(workbook)
  33. report_name = self._get_report_name(report)
  34. report_footer = self._get_report_footer()
  35. filters = self._get_report_filters(report)
  36. self.columns = self._get_report_columns(report)
  37. self.workbook = workbook
  38. self.sheet = workbook.add_worksheet(report_name[:31])
  39. self._set_column_width()
  40. self._write_report_title(report_name)
  41. self._write_filters(filters)
  42. self._generate_report_content(workbook, report)
  43. self._write_report_footer(report_footer)
  44. def _define_formats(self, workbook):
  45. """ Add cell formats to current workbook.
  46. Those formats can be used on all cell.
  47. Available formats are :
  48. * format_bold
  49. * format_right
  50. * format_right_bold_italic
  51. * format_header_left
  52. * format_header_center
  53. * format_header_right
  54. * format_header_amount
  55. * format_amount
  56. * format_percent_bold_italic
  57. """
  58. self.format_bold = workbook.add_format({'bold': True})
  59. self.format_right = workbook.add_format({'align': 'right'})
  60. self.format_left = workbook.add_format({'align': 'left'})
  61. self.format_right_bold_italic = workbook.add_format(
  62. {'align': 'right', 'bold': True, 'italic': True}
  63. )
  64. self.format_header_left = workbook.add_format(
  65. {'bold': True,
  66. 'border': True,
  67. 'bg_color': '#FFFFCC'})
  68. self.format_header_center = workbook.add_format(
  69. {'bold': True,
  70. 'align': 'center',
  71. 'border': True,
  72. 'bg_color': '#FFFFCC'})
  73. self.format_header_right = workbook.add_format(
  74. {'bold': True,
  75. 'align': 'right',
  76. 'border': True,
  77. 'bg_color': '#FFFFCC'})
  78. self.format_header_amount = workbook.add_format(
  79. {'bold': True,
  80. 'border': True,
  81. 'bg_color': '#FFFFCC'})
  82. currency_id = self.env['res.company']._get_user_currency()
  83. self.format_header_amount.set_num_format(
  84. '#,##0.'+'0'*currency_id.decimal_places)
  85. self.format_amount = workbook.add_format()
  86. self.format_amount.set_num_format(
  87. '#,##0.'+'0'*currency_id.decimal_places)
  88. self.format_amount_bold = workbook.add_format({'bold': True})
  89. self.format_amount_bold.set_num_format(
  90. '#,##0.' + '0' * currency_id.decimal_places)
  91. self.format_percent_bold_italic = workbook.add_format(
  92. {'bold': True, 'italic': True}
  93. )
  94. self.format_percent_bold_italic.set_num_format('#,##0.00%')
  95. def _set_column_width(self):
  96. """Set width for all defined columns.
  97. Columns are defined with `_get_report_columns` method.
  98. """
  99. for position, column in self.columns.items():
  100. self.sheet.set_column(position, position, column['width'])
  101. def _write_report_title(self, title):
  102. """Write report title on current line using all defined columns width.
  103. Columns are defined with `_get_report_columns` method.
  104. """
  105. self.sheet.merge_range(
  106. self.row_pos, 0, self.row_pos, len(self.columns) - 1,
  107. title, self.format_bold
  108. )
  109. self.row_pos += 3
  110. def _write_report_footer(self, footer):
  111. """Write report footer .
  112. Columns are defined with `_get_report_columns` method.
  113. """
  114. if footer:
  115. self.row_pos += 1
  116. self.sheet.merge_range(
  117. self.row_pos, 0, self.row_pos, len(self.columns) - 1,
  118. footer, self.format_left
  119. )
  120. self.row_pos += 1
  121. def _write_filters(self, filters):
  122. """Write one line per filters on starting on current line.
  123. Columns number for filter name is defined
  124. with `_get_col_count_filter_name` method.
  125. Columns number for filter value is define
  126. with `_get_col_count_filter_value` method.
  127. """
  128. col_name = 1
  129. col_count_filter_name = self._get_col_count_filter_name()
  130. col_count_filter_value = self._get_col_count_filter_value()
  131. col_value = col_name + col_count_filter_name + 1
  132. for title, value in filters:
  133. self.sheet.merge_range(
  134. self.row_pos, col_name,
  135. self.row_pos, col_name + col_count_filter_name - 1,
  136. title, self.format_header_left)
  137. self.sheet.merge_range(
  138. self.row_pos, col_value,
  139. self.row_pos, col_value + col_count_filter_value - 1,
  140. value)
  141. self.row_pos += 1
  142. self.row_pos += 2
  143. def write_array_title(self, title):
  144. """Write array title on current line using all defined columns width.
  145. Columns are defined with `_get_report_columns` method.
  146. """
  147. self.sheet.merge_range(
  148. self.row_pos, 0, self.row_pos, len(self.columns) - 1,
  149. title, self.format_bold
  150. )
  151. self.row_pos += 1
  152. def write_array_header(self):
  153. """Write array header on current line using all defined columns name.
  154. Columns are defined with `_get_report_columns` method.
  155. """
  156. for col_pos, column in self.columns.items():
  157. self.sheet.write(self.row_pos, col_pos, column['header'],
  158. self.format_header_center)
  159. self.row_pos += 1
  160. def write_line(self, line_object):
  161. """Write a line on current line using all defined columns field name.
  162. Columns are defined with `_get_report_columns` method.
  163. """
  164. for col_pos, column in self.columns.items():
  165. value = getattr(line_object, column['field'])
  166. if isinstance(value, datetime.date):
  167. value = fields.Date.to_string(value)
  168. cell_type = column.get('type', 'string')
  169. if cell_type == 'many2one':
  170. self.sheet.write_string(
  171. self.row_pos, col_pos, value.name or '', self.format_right)
  172. elif cell_type == 'string':
  173. if hasattr(line_object, 'account_group_id') and \
  174. line_object.account_group_id:
  175. self.sheet.write_string(self.row_pos, col_pos, value or '',
  176. self.format_bold)
  177. else:
  178. self.sheet.write_string(self.row_pos, col_pos, value or '')
  179. elif cell_type == 'amount':
  180. if hasattr(line_object, 'account_group_id') and \
  181. line_object.account_group_id:
  182. cell_format = self.format_amount_bold
  183. else:
  184. cell_format = self.format_amount
  185. self.sheet.write_number(
  186. self.row_pos, col_pos, float(value), cell_format
  187. )
  188. elif cell_type == 'amount_currency':
  189. if line_object.currency_id:
  190. format_amt = self._get_currency_amt_format(line_object)
  191. self.sheet.write_number(
  192. self.row_pos, col_pos, float(value), format_amt
  193. )
  194. self.row_pos += 1
  195. def write_initial_balance(self, my_object, label):
  196. """Write a specific initial balance line on current line
  197. using defined columns field_initial_balance name.
  198. Columns are defined with `_get_report_columns` method.
  199. """
  200. col_pos_label = self._get_col_pos_initial_balance_label()
  201. self.sheet.write(self.row_pos, col_pos_label, label, self.format_right)
  202. for col_pos, column in self.columns.items():
  203. if column.get('field_initial_balance'):
  204. value = getattr(my_object, column['field_initial_balance'])
  205. cell_type = column.get('type', 'string')
  206. if cell_type == 'string':
  207. self.sheet.write_string(self.row_pos, col_pos, value or '')
  208. elif cell_type == 'amount':
  209. self.sheet.write_number(
  210. self.row_pos, col_pos, float(value), self.format_amount
  211. )
  212. elif cell_type == 'amount_currency':
  213. if my_object.currency_id:
  214. format_amt = self._get_currency_amt_format(
  215. my_object)
  216. self.sheet.write_number(
  217. self.row_pos, col_pos,
  218. float(value), format_amt
  219. )
  220. elif column.get('field_currency_balance'):
  221. value = getattr(my_object, column['field_currency_balance'])
  222. cell_type = column.get('type', 'string')
  223. if cell_type == 'many2one':
  224. if my_object.currency_id:
  225. self.sheet.write_string(
  226. self.row_pos, col_pos,
  227. value.name or '',
  228. self.format_right
  229. )
  230. self.row_pos += 1
  231. def write_ending_balance(self, my_object, name, label):
  232. """Write a specific ending balance line on current line
  233. using defined columns field_final_balance name.
  234. Columns are defined with `_get_report_columns` method.
  235. """
  236. for i in range(0, len(self.columns)):
  237. self.sheet.write(self.row_pos, i, '', self.format_header_right)
  238. row_count_name = self._get_col_count_final_balance_name()
  239. col_pos_label = self._get_col_pos_final_balance_label()
  240. self.sheet.merge_range(
  241. self.row_pos, 0, self.row_pos, row_count_name - 1, name,
  242. self.format_header_left
  243. )
  244. self.sheet.write(self.row_pos, col_pos_label, label,
  245. self.format_header_right)
  246. for col_pos, column in self.columns.items():
  247. if column.get('field_final_balance'):
  248. value = getattr(my_object, column['field_final_balance'])
  249. cell_type = column.get('type', 'string')
  250. if cell_type == 'string':
  251. self.sheet.write_string(self.row_pos, col_pos, value or '',
  252. self.format_header_right)
  253. elif cell_type == 'amount':
  254. self.sheet.write_number(
  255. self.row_pos, col_pos, float(value),
  256. self.format_header_amount
  257. )
  258. elif cell_type == 'amount_currency':
  259. if my_object.currency_id:
  260. format_amt = self._get_currency_amt_header_format(
  261. my_object)
  262. self.sheet.write_number(
  263. self.row_pos, col_pos, float(value),
  264. format_amt
  265. )
  266. elif column.get('field_currency_balance'):
  267. value = getattr(my_object, column['field_currency_balance'])
  268. cell_type = column.get('type', 'string')
  269. if cell_type == 'many2one':
  270. if my_object.currency_id:
  271. self.sheet.write_string(
  272. self.row_pos, col_pos,
  273. value.name or '',
  274. self.format_header_right
  275. )
  276. self.row_pos += 1
  277. def _get_currency_amt_format(self, line_object):
  278. """ Return amount format specific for each currency. """
  279. if hasattr(line_object, 'account_group_id') and \
  280. line_object.account_group_id:
  281. format_amt = getattr(self, 'format_amount_bold')
  282. field_prefix = 'format_amount_bold'
  283. else:
  284. format_amt = getattr(self, 'format_amount')
  285. field_prefix = 'format_amount'
  286. if line_object.currency_id:
  287. field_name = \
  288. '%s_%s' % (field_prefix, line_object.currency_id.name)
  289. if hasattr(self, field_name):
  290. format_amt = getattr(self, field_name)
  291. else:
  292. format_amt = self.workbook.add_format()
  293. setattr(self, 'field_name', format_amt)
  294. format_amount = \
  295. '#,##0.' + ('0' * line_object.currency_id.decimal_places)
  296. format_amt.set_num_format(format_amount)
  297. return format_amt
  298. def _get_currency_amt_header_format(self, line_object):
  299. """ Return amount header format for each currency. """
  300. format_amt = getattr(self, 'format_header_amount')
  301. if line_object.currency_id:
  302. field_name = \
  303. 'format_header_amount_%s' % line_object.currency_id.name
  304. if hasattr(self, field_name):
  305. format_amt = getattr(self, field_name)
  306. else:
  307. format_amt = self.workbook.add_format(
  308. {'bold': True,
  309. 'border': True,
  310. 'bg_color': '#FFFFCC'})
  311. setattr(self, 'field_name', format_amt)
  312. format_amount = \
  313. '#,##0.' + ('0' * line_object.currency_id.decimal_places)
  314. format_amt.set_num_format(format_amount)
  315. return format_amt
  316. def _generate_report_content(self, workbook, report):
  317. """
  318. Allow to fetch report content to be displayed.
  319. """
  320. raise NotImplementedError()
  321. def _get_report_complete_name(self, report, prefix):
  322. if report.company_id:
  323. suffix = ' - %s - %s' % (
  324. report.company_id.name, report.company_id.currency_id.name)
  325. return prefix + suffix
  326. return prefix
  327. def _get_report_name(self, report):
  328. """
  329. Allow to define the report name.
  330. Report name will be used as sheet name and as report title.
  331. :return: the report name
  332. """
  333. raise NotImplementedError()
  334. def _get_report_footer(self):
  335. """
  336. Allow to define the report footer.
  337. :return: the report footer
  338. """
  339. return False
  340. def _get_report_columns(self, report):
  341. """
  342. Allow to define the report columns
  343. which will be used to generate report.
  344. :return: the report columns as dict
  345. :Example:
  346. {
  347. 0: {'header': 'Simple column',
  348. 'field': 'field_name_on_my_object',
  349. 'width': 11},
  350. 1: {'header': 'Amount column',
  351. 'field': 'field_name_on_my_object',
  352. 'type': 'amount',
  353. 'width': 14},
  354. }
  355. """
  356. raise NotImplementedError()
  357. def _get_report_filters(self, report):
  358. """
  359. :return: the report filters as list
  360. :Example:
  361. [
  362. ['first_filter_name', 'first_filter_value'],
  363. ['second_filter_name', 'second_filter_value']
  364. ]
  365. """
  366. raise NotImplementedError()
  367. def _get_col_count_filter_name(self):
  368. """
  369. :return: the columns number used for filter names.
  370. """
  371. raise NotImplementedError()
  372. def _get_col_count_filter_value(self):
  373. """
  374. :return: the columns number used for filter values.
  375. """
  376. raise NotImplementedError()
  377. def _get_col_pos_initial_balance_label(self):
  378. """
  379. :return: the columns position used for initial balance label.
  380. """
  381. raise NotImplementedError()
  382. def _get_col_count_final_balance_name(self):
  383. """
  384. :return: the columns number used for final balance name.
  385. """
  386. raise NotImplementedError()
  387. def _get_col_pos_final_balance_label(self):
  388. """
  389. :return: the columns position used for final balance label.
  390. """
  391. raise NotImplementedError()