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.

317 lines
16 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2013 Noviat nv/sa (www.noviat.com). All rights reserved.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import xlwt
  23. import time
  24. from datetime import datetime
  25. from openerp.osv import orm
  26. from openerp.report import report_sxw
  27. from openerp.addons.report_xls.report_xls import report_xls
  28. from openerp.addons.report_xls.utils import rowcol_to_cell, _render
  29. from .nov_account_journal import nov_journal_print
  30. from openerp.tools.translate import _
  31. import logging
  32. _logger = logging.getLogger(__name__)
  33. class account_journal_xls_parser(nov_journal_print):
  34. def __init__(self, cr, uid, name, context):
  35. super(account_journal_xls_parser, self).__init__(cr, uid, name, context=context)
  36. journal_obj = self.pool.get('account.journal')
  37. self.context = context
  38. wanted_list = journal_obj._report_xls_fields(cr, uid, context)
  39. template_changes = journal_obj._report_xls_template(cr, uid, context)
  40. self.localcontext.update({
  41. 'datetime': datetime,
  42. 'wanted_list': wanted_list,
  43. 'template_changes': template_changes,
  44. })
  45. class account_journal_xls(report_xls):
  46. def __init__(self, name, table, rml=False, parser=False, header=True, store=False):
  47. super(account_journal_xls, self).__init__(name, table, rml, parser, header, store)
  48. # Cell Styles
  49. _xs = self.xls_styles
  50. # header
  51. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  52. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  53. self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
  54. self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
  55. # lines
  56. aml_cell_format = _xs['borders_all']
  57. self.aml_cell_style = xlwt.easyxf(aml_cell_format)
  58. self.aml_cell_style_center = xlwt.easyxf(aml_cell_format + _xs['center'])
  59. self.aml_cell_style_date = xlwt.easyxf(aml_cell_format + _xs['left'], num_format_str=report_xls.date_format)
  60. self.aml_cell_style_decimal = xlwt.easyxf(aml_cell_format + _xs['right'], num_format_str=report_xls.decimal_format)
  61. # totals
  62. rt_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  63. self.rt_cell_style = xlwt.easyxf(rt_cell_format)
  64. self.rt_cell_style_right = xlwt.easyxf(rt_cell_format + _xs['right'])
  65. self.rt_cell_style_decimal = xlwt.easyxf(rt_cell_format + _xs['right'], num_format_str=report_xls.decimal_format)
  66. # XLS Template Journal Items
  67. self.col_specs_lines_template = {
  68. 'move_name': {
  69. 'header': [1, 20, 'text', _render("_('Entry')")],
  70. 'lines': [1, 0, 'text', _render("l['move_name'] != '/' and l['move_name'] or ('*'+str(l['move_id']))")],
  71. 'totals': [1, 0, 'text', None]},
  72. 'move_date': {
  73. 'header': [1, 13, 'text', _render("_('Date')")],
  74. 'lines': [1, 0, 'date', _render("datetime.strptime(l['move_date'],'%Y-%m-%d')"), None, self.aml_cell_style_date],
  75. 'totals': [1, 0, 'text', None]},
  76. 'acc_code': {
  77. 'header': [1, 12, 'text', _render("_('Account')")],
  78. 'lines': [1, 0, 'text', _render("l['acc_code']")],
  79. 'totals': [1, 0, 'text', None]},
  80. 'aml_name': {
  81. 'header': [1, 42, 'text', _render("_('Description')")],
  82. 'lines': [1, 0, 'text', _render("l['aml_name']")],
  83. 'totals': [1, 0, 'text', None]},
  84. 'period': {
  85. 'header': [1, 12, 'text', _render("_('Period')")],
  86. 'lines': [1, 0, 'text', _render("l['period']")],
  87. 'totals': [1, 0, 'text', None]},
  88. 'partner_name': {
  89. 'header': [1, 36, 'text', _render("_('Partner')")],
  90. 'lines': [1, 0, 'text', _render("l['partner_name']")],
  91. 'totals': [1, 0, 'text', None]},
  92. 'partner_ref': {
  93. 'header': [1, 36, 'text', _render("_('Partner Reference')")],
  94. 'lines': [1, 0, 'text', _render("l['partner_ref']")],
  95. 'totals': [1, 0, 'text', None]},
  96. 'date_maturity': {
  97. 'header': [1, 13, 'text', _render("_('Maturity Date')")],
  98. 'lines': [1, 0, _render("l['date_maturity'] and 'date' or 'text'"),
  99. _render("l['date_maturity'] and datetime.strptime(l['date_maturity'],'%Y-%m-%d') or None"),
  100. None, self.aml_cell_style_date],
  101. 'totals': [1, 0, 'text', None]},
  102. 'debit': {
  103. 'header': [1, 18, 'text', _render("_('Debit')"), None, self.rh_cell_style_right],
  104. 'lines': [1, 0, 'number', _render("l['debit']"), None, self.aml_cell_style_decimal],
  105. 'totals': [1, 0, 'number', None, _render("debit_formula"), self.rt_cell_style_decimal]},
  106. 'credit': {
  107. 'header': [1, 18, 'text', _render("_('Credit')"), None, self.rh_cell_style_right],
  108. 'lines': [1, 0, 'number', _render("l['credit']"), None, self.aml_cell_style_decimal],
  109. 'totals': [1, 0, 'number', None, _render("credit_formula"), self.rt_cell_style_decimal]},
  110. 'balance': {
  111. 'header': [1, 18, 'text', _render("_('Balance')"), None, self.rh_cell_style_right],
  112. 'lines': [1, 0, 'number', None, _render("bal_formula"), self.aml_cell_style_decimal],
  113. 'totals': [1, 0, 'number', None, _render("bal_formula"), self.rt_cell_style_decimal]},
  114. 'reconcile': {
  115. 'header': [1, 12, 'text', _render("_('Rec.')"), None, self.rh_cell_style_center],
  116. 'lines': [1, 0, 'text', _render("l['reconcile']"), None, self.aml_cell_style_center],
  117. 'totals': [1, 0, 'text', None]},
  118. 'reconcile_partial': {
  119. 'header': [1, 12, 'text', _render("_('Part. Rec.')"), None, self.rh_cell_style_center],
  120. 'lines': [1, 0, 'text', _render("l['reconcile_partial']"), None, self.aml_cell_style_center],
  121. 'totals': [1, 0, 'text', None]},
  122. 'tax_code': {
  123. 'header': [1, 6, 'text', _render("_('VAT')"), None, self.rh_cell_style_center],
  124. 'lines': [1, 0, 'text', _render("l['tax_code']"), None, self.aml_cell_style_center],
  125. 'totals': [1, 0, 'text', None]},
  126. 'tax_amount': {
  127. 'header': [1, 18, 'text', _render("_('VAT Amount')"), None, self.rh_cell_style_right],
  128. 'lines': [1, 0, 'number', _render("l['tax_amount']"), None, self.aml_cell_style_decimal],
  129. 'totals': [1, 0, 'text', None]},
  130. 'amount_currency': {
  131. 'header': [1, 18, 'text', _render("_('Am. Currency')"), None, self.rh_cell_style_right],
  132. 'lines': [1, 0, _render("l['amount_currency'] and 'number' or 'text'"),
  133. _render("l['amount_currency'] or None"),
  134. None, self.aml_cell_style_decimal],
  135. 'totals': [1, 0, 'text', None]},
  136. 'currency_name': {
  137. 'header': [1, 6, 'text', _render("_('Curr.')"), None, self.rh_cell_style_center],
  138. 'lines': [1, 0, 'text', _render("l['currency_name']"), None, self.aml_cell_style_center],
  139. 'totals': [1, 0, 'text', None]},
  140. 'docname': {
  141. 'header': [1, 35, 'text', _render("_('Document')")],
  142. 'lines': [1, 0, 'text', _render("l['docname']")],
  143. 'totals': [1, 0, 'text', None]},
  144. 'move_ref': {
  145. 'header': [1, 25, 'text', _render("_('Entry Reference')")],
  146. 'lines': [1, 0, 'text', _render("l['move_ref']")],
  147. 'totals': [1, 0, 'text', None]},
  148. 'move_id': {
  149. 'header': [1, 10, 'text', _render("_('Entry Id')")],
  150. 'lines': [1, 0, 'text', _render("str(l['move_id'])")],
  151. 'totals': [1, 0, 'text', None]},
  152. }
  153. # XLS Template VAT Summary
  154. self.col_specs_vat_summary_template = {
  155. 'tax_case_name': {
  156. 'header': [1, 45, 'text', _render("_('Description')")],
  157. 'tax_totals': [1, 0, 'text', _render("t.name")]},
  158. 'tax_code': {
  159. 'header': [1, 6, 'text', _render("_('Case')")],
  160. 'tax_totals': [1, 0, 'text', _render("t.code")]},
  161. 'tax_amount': {
  162. 'header': [1, 18, 'text', _render("_('Amount')"), None, self.rh_cell_style_right],
  163. 'tax_totals': [1, 0, 'number', _render("sum_vat(o,t)"), None, self.aml_cell_style_decimal]},
  164. }
  165. def _journal_title(self, o, ws, _p, row_pos, xlwt, _xs):
  166. cell_style = xlwt.easyxf(_xs['xls_title'])
  167. report_name = (10 * ' ').join([
  168. _p.company.name,
  169. _p.title(o)[0],
  170. _p.title(o)[1],
  171. _p._("Journal Overview") + ' - ' + _p.company.currency_id.name,
  172. ])
  173. c_specs = [
  174. ('report_name', 1, 0, 'text', report_name),
  175. ]
  176. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  177. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  178. return row_pos + 1
  179. def _journal_lines(self, o, ws, _p, row_pos, xlwt, _xs):
  180. wanted_list = self.wanted_list
  181. debit_pos = self.debit_pos
  182. credit_pos = self.credit_pos
  183. # Column headers
  184. c_specs = map(lambda x: self.render(x, self.col_specs_lines_template, 'header', render_space={'_': _p._}), wanted_list)
  185. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  186. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  187. ws.set_horz_split_pos(row_pos)
  188. # account move lines
  189. aml_start_pos = row_pos
  190. aml_cnt = len(_p.lines(o))
  191. cnt = 0
  192. for l in _p.lines(o):
  193. cnt += 1
  194. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  195. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  196. bal_formula = debit_cell + '-' + credit_cell
  197. c_specs = map(lambda x: self.render(x, self.col_specs_lines_template, 'lines'), wanted_list)
  198. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  199. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.aml_cell_style)
  200. if l['draw_line'] and cnt != aml_cnt:
  201. row_pos += 1
  202. # Totals
  203. debit_start = rowcol_to_cell(aml_start_pos, debit_pos)
  204. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  205. debit_formula = 'SUM(%s:%s)' % (debit_start, debit_stop)
  206. credit_start = rowcol_to_cell(aml_start_pos, credit_pos)
  207. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  208. credit_formula = 'SUM(%s:%s)' % (credit_start, credit_stop)
  209. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  210. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  211. bal_formula = debit_cell + '-' + credit_cell
  212. c_specs = map(lambda x: self.render(x, self.col_specs_lines_template, 'totals'), wanted_list)
  213. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  214. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  215. return row_pos + 1
  216. def _journal_vat_summary(self, o, ws, _p, row_pos, xlwt, _xs):
  217. if not _p.tax_codes(o):
  218. return row_pos
  219. title_cell_style = xlwt.easyxf(_xs['bold'])
  220. c_specs = [('summary_title', 1, 0, 'text', _p._("VAT Declaration"))]
  221. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  222. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=title_cell_style) + 1
  223. wanted_list = self.wanted_list
  224. vat_summary_wanted_list = ['tax_case_name', 'tax_code', 'tax_amount']
  225. # calculate col_span
  226. cols_number = len(wanted_list)
  227. vat_summary_cols_number = len(vat_summary_wanted_list)
  228. if vat_summary_cols_number > cols_number:
  229. raise orm.except_orm(_('Programming Error!'),
  230. _("vat_summary_cols_number should be < cols_number !"))
  231. index = 0
  232. for i in range(vat_summary_cols_number):
  233. col = vat_summary_wanted_list[i]
  234. col_size = self.col_specs_lines_template[wanted_list[index]]['header'][1]
  235. templ_col_size = self.col_specs_vat_summary_template[col]['header'][1]
  236. #_logger.warn("col=%s, col_size=%s, templ_col_size=%s", col, col_size, templ_col_size)
  237. col_span = 1
  238. if templ_col_size > col_size:
  239. new_size = col_size
  240. while templ_col_size > new_size:
  241. col_span += 1
  242. index += 1
  243. new_size += self.col_specs_lines_template[wanted_list[index]]['header'][1]
  244. self.col_specs_vat_summary_template[col]['header'][0] = col_span
  245. self.col_specs_vat_summary_template[col]['tax_totals'][0] = col_span
  246. index += 1
  247. c_specs = map(lambda x: self.render(x, self.col_specs_vat_summary_template, 'header'), vat_summary_wanted_list)
  248. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  249. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style)
  250. for t in _p.tax_codes(o):
  251. c_specs = map(lambda x: self.render(x, self.col_specs_vat_summary_template, 'tax_totals'), vat_summary_wanted_list)
  252. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  253. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.aml_cell_style)
  254. return row_pos
  255. def generate_xls_report(self, _p, _xs, data, objects, wb):
  256. wanted_list = _p.wanted_list
  257. if _p.display_currency:
  258. wanted_list += ['amount_currency', 'currency_name']
  259. self.wanted_list = wanted_list
  260. self.col_specs_lines_template.update(_p.template_changes)
  261. self.debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  262. self.credit_pos = 'credit' in wanted_list and wanted_list.index('credit')
  263. if not (self.credit_pos and self.debit_pos) and 'balance' in wanted_list:
  264. raise orm.except_orm(_('Customisation Error!'),
  265. _("The 'Balance' field is a calculated XLS field requiring the presence of the 'Debit' and 'Credit' fields !"))
  266. for o in objects:
  267. sheet_name = ' - '.join([o[1].code, o[0].code])[:31].replace('/', '-')
  268. sheet_name = sheet_name[:31].replace('/', '-')
  269. ws = wb.add_sheet(sheet_name)
  270. ws.panes_frozen = True
  271. ws.remove_splits = True
  272. ws.portrait = 0 # Landscape
  273. ws.fit_width_to_pages = 1
  274. row_pos = 0
  275. # set print header/footer
  276. ws.header_str = self.xls_headers['standard']
  277. ws.footer_str = self.xls_footers['standard']
  278. # Data
  279. row_pos = self._journal_title(o, ws, _p, row_pos, xlwt, _xs)
  280. row_pos = self._journal_lines(o, ws, _p, row_pos, xlwt, _xs)
  281. row_pos = self._journal_vat_summary(o, ws, _p, row_pos, xlwt, _xs)
  282. account_journal_xls('report.nov.account.journal.xls', 'account.journal.period',
  283. parser=account_journal_xls_parser)
  284. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: