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.

337 lines
17 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. 'acc_name': {
  81. 'header': [1, 36, 'text', _render("_('Account Name')")],
  82. 'lines': [1, 0, 'text', _render("l['acc_name']")],
  83. 'totals': [1, 0, 'text', None]},
  84. 'aml_name': {
  85. 'header': [1, 42, 'text', _render("_('Description')")],
  86. 'lines': [1, 0, 'text', _render("l['aml_name']")],
  87. 'totals': [1, 0, 'text', None]},
  88. 'period': {
  89. 'header': [1, 12, 'text', _render("_('Period')")],
  90. 'lines': [1, 0, 'text', _render("l['period']")],
  91. 'totals': [1, 0, 'text', None]},
  92. 'journal': {
  93. 'header': [1, 20, 'text', _render("_('Journal')")],
  94. 'lines': [1, 0, 'text', _render("l['journal']")],
  95. 'totals': [1, 0, 'text', None]},
  96. 'journal_code': {
  97. 'header': [1, 10, 'text', _render("_('Journal')")],
  98. 'lines': [1, 0, 'text', _render("l['journal_code']")],
  99. 'totals': [1, 0, 'text', None]},
  100. 'analytic_account': {
  101. 'header': [1, 20, 'text', _render("_('Analytic Account')")],
  102. 'lines': [1, 0, 'text', _render("l['an_acc_name']")],
  103. 'totals': [1, 0, 'text', None]},
  104. 'analytic_account_code': {
  105. 'header': [1, 20, 'text', _render("_('Analytic Account')")],
  106. 'lines': [1, 0, 'text', _render("l['an_acc_code']")],
  107. 'totals': [1, 0, 'text', None]},
  108. 'partner_name': {
  109. 'header': [1, 36, 'text', _render("_('Partner')")],
  110. 'lines': [1, 0, 'text', _render("l['partner_name']")],
  111. 'totals': [1, 0, 'text', None]},
  112. 'partner_ref': {
  113. 'header': [1, 36, 'text', _render("_('Partner Reference')")],
  114. 'lines': [1, 0, 'text', _render("l['partner_ref']")],
  115. 'totals': [1, 0, 'text', None]},
  116. 'date_maturity': {
  117. 'header': [1, 13, 'text', _render("_('Maturity Date')")],
  118. 'lines': [1, 0, _render("l['date_maturity'] and 'date' or 'text'"),
  119. _render("l['date_maturity'] and datetime.strptime(l['date_maturity'],'%Y-%m-%d') or None"),
  120. None, self.aml_cell_style_date],
  121. 'totals': [1, 0, 'text', None]},
  122. 'debit': {
  123. 'header': [1, 18, 'text', _render("_('Debit')"), None, self.rh_cell_style_right],
  124. 'lines': [1, 0, 'number', _render("l['debit']"), None, self.aml_cell_style_decimal],
  125. 'totals': [1, 0, 'number', None, _render("debit_formula"), self.rt_cell_style_decimal]},
  126. 'credit': {
  127. 'header': [1, 18, 'text', _render("_('Credit')"), None, self.rh_cell_style_right],
  128. 'lines': [1, 0, 'number', _render("l['credit']"), None, self.aml_cell_style_decimal],
  129. 'totals': [1, 0, 'number', None, _render("credit_formula"), self.rt_cell_style_decimal]},
  130. 'balance': {
  131. 'header': [1, 18, 'text', _render("_('Balance')"), None, self.rh_cell_style_right],
  132. 'lines': [1, 0, 'number', None, _render("bal_formula"), self.aml_cell_style_decimal],
  133. 'totals': [1, 0, 'number', None, _render("bal_formula"), self.rt_cell_style_decimal]},
  134. 'reconcile': {
  135. 'header': [1, 12, 'text', _render("_('Rec.')"), None, self.rh_cell_style_center],
  136. 'lines': [1, 0, 'text', _render("l['reconcile']"), None, self.aml_cell_style_center],
  137. 'totals': [1, 0, 'text', None]},
  138. 'reconcile_partial': {
  139. 'header': [1, 12, 'text', _render("_('Part. Rec.')"), None, self.rh_cell_style_center],
  140. 'lines': [1, 0, 'text', _render("l['reconcile_partial']"), None, self.aml_cell_style_center],
  141. 'totals': [1, 0, 'text', None]},
  142. 'tax_code': {
  143. 'header': [1, 6, 'text', _render("_('VAT')"), None, self.rh_cell_style_center],
  144. 'lines': [1, 0, 'text', _render("l['tax_code']"), None, self.aml_cell_style_center],
  145. 'totals': [1, 0, 'text', None]},
  146. 'tax_amount': {
  147. 'header': [1, 18, 'text', _render("_('VAT Amount')"), None, self.rh_cell_style_right],
  148. 'lines': [1, 0, 'number', _render("l['tax_amount']"), None, self.aml_cell_style_decimal],
  149. 'totals': [1, 0, 'text', None]},
  150. 'amount_currency': {
  151. 'header': [1, 18, 'text', _render("_('Am. Currency')"), None, self.rh_cell_style_right],
  152. 'lines': [1, 0, _render("l['amount_currency'] and 'number' or 'text'"),
  153. _render("l['amount_currency'] or None"),
  154. None, self.aml_cell_style_decimal],
  155. 'totals': [1, 0, 'text', None]},
  156. 'currency_name': {
  157. 'header': [1, 6, 'text', _render("_('Curr.')"), None, self.rh_cell_style_center],
  158. 'lines': [1, 0, 'text', _render("l['currency_name']"), None, self.aml_cell_style_center],
  159. 'totals': [1, 0, 'text', None]},
  160. 'docname': {
  161. 'header': [1, 35, 'text', _render("_('Document')")],
  162. 'lines': [1, 0, 'text', _render("l['docname']")],
  163. 'totals': [1, 0, 'text', None]},
  164. 'move_ref': {
  165. 'header': [1, 25, 'text', _render("_('Entry Reference')")],
  166. 'lines': [1, 0, 'text', _render("l['move_ref']")],
  167. 'totals': [1, 0, 'text', None]},
  168. 'move_id': {
  169. 'header': [1, 10, 'text', _render("_('Entry Id')")],
  170. 'lines': [1, 0, 'text', _render("str(l['move_id'])")],
  171. 'totals': [1, 0, 'text', None]},
  172. }
  173. # XLS Template VAT Summary
  174. self.col_specs_vat_summary_template = {
  175. 'tax_case_name': {
  176. 'header': [1, 45, 'text', _render("_('Description')")],
  177. 'tax_totals': [1, 0, 'text', _render("t.name")]},
  178. 'tax_code': {
  179. 'header': [1, 6, 'text', _render("_('Case')")],
  180. 'tax_totals': [1, 0, 'text', _render("t.code")]},
  181. 'tax_amount': {
  182. 'header': [1, 18, 'text', _render("_('Amount')"), None, self.rh_cell_style_right],
  183. 'tax_totals': [1, 0, 'number', _render("sum_vat(o,t)"), None, self.aml_cell_style_decimal]},
  184. }
  185. def _journal_title(self, o, ws, _p, row_pos, xlwt, _xs):
  186. cell_style = xlwt.easyxf(_xs['xls_title'])
  187. report_name = (10 * ' ').join([
  188. _p.company.name,
  189. _p.title(o)[0],
  190. _p.title(o)[1],
  191. _p._("Journal Overview") + ' - ' + _p.company.currency_id.name,
  192. ])
  193. c_specs = [
  194. ('report_name', 1, 0, 'text', report_name),
  195. ]
  196. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  197. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  198. return row_pos + 1
  199. def _journal_lines(self, o, ws, _p, row_pos, xlwt, _xs):
  200. wanted_list = self.wanted_list
  201. debit_pos = self.debit_pos
  202. credit_pos = self.credit_pos
  203. # Column headers
  204. c_specs = map(lambda x: self.render(x, self.col_specs_lines_template, 'header', render_space={'_': _p._}), wanted_list)
  205. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  206. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  207. ws.set_horz_split_pos(row_pos)
  208. # account move lines
  209. aml_start_pos = row_pos
  210. aml_cnt = len(_p.lines(o))
  211. cnt = 0
  212. for l in _p.lines(o):
  213. cnt += 1
  214. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  215. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  216. bal_formula = debit_cell + '-' + credit_cell
  217. c_specs = map(lambda x: self.render(x, self.col_specs_lines_template, 'lines'), wanted_list)
  218. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  219. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.aml_cell_style)
  220. if l['draw_line'] and cnt != aml_cnt:
  221. row_pos += 1
  222. # Totals
  223. debit_start = rowcol_to_cell(aml_start_pos, debit_pos)
  224. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  225. debit_formula = 'SUM(%s:%s)' % (debit_start, debit_stop)
  226. credit_start = rowcol_to_cell(aml_start_pos, credit_pos)
  227. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  228. credit_formula = 'SUM(%s:%s)' % (credit_start, credit_stop)
  229. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  230. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  231. bal_formula = debit_cell + '-' + credit_cell
  232. c_specs = map(lambda x: self.render(x, self.col_specs_lines_template, 'totals'), wanted_list)
  233. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  234. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  235. return row_pos + 1
  236. def _journal_vat_summary(self, o, ws, _p, row_pos, xlwt, _xs):
  237. if not _p.tax_codes(o):
  238. return row_pos
  239. title_cell_style = xlwt.easyxf(_xs['bold'])
  240. c_specs = [('summary_title', 1, 0, 'text', _p._("VAT Declaration"))]
  241. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  242. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=title_cell_style) + 1
  243. wanted_list = self.wanted_list
  244. vat_summary_wanted_list = ['tax_case_name', 'tax_code', 'tax_amount']
  245. # calculate col_span
  246. cols_number = len(wanted_list)
  247. vat_summary_cols_number = len(vat_summary_wanted_list)
  248. if vat_summary_cols_number > cols_number:
  249. raise orm.except_orm(_('Programming Error!'),
  250. _("vat_summary_cols_number should be < cols_number !"))
  251. index = 0
  252. for i in range(vat_summary_cols_number):
  253. col = vat_summary_wanted_list[i]
  254. col_size = self.col_specs_lines_template[wanted_list[index]]['header'][1]
  255. templ_col_size = self.col_specs_vat_summary_template[col]['header'][1]
  256. #_logger.warn("col=%s, col_size=%s, templ_col_size=%s", col, col_size, templ_col_size)
  257. col_span = 1
  258. if templ_col_size > col_size:
  259. new_size = col_size
  260. while templ_col_size > new_size:
  261. col_span += 1
  262. index += 1
  263. new_size += self.col_specs_lines_template[wanted_list[index]]['header'][1]
  264. self.col_specs_vat_summary_template[col]['header'][0] = col_span
  265. self.col_specs_vat_summary_template[col]['tax_totals'][0] = col_span
  266. index += 1
  267. c_specs = map(lambda x: self.render(x, self.col_specs_vat_summary_template, 'header'), vat_summary_wanted_list)
  268. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  269. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style)
  270. for t in _p.tax_codes(o):
  271. c_specs = map(lambda x: self.render(x, self.col_specs_vat_summary_template, 'tax_totals'), vat_summary_wanted_list)
  272. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  273. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.aml_cell_style)
  274. return row_pos
  275. def generate_xls_report(self, _p, _xs, data, objects, wb):
  276. wanted_list = _p.wanted_list
  277. if _p.display_currency:
  278. wanted_list += ['amount_currency', 'currency_name']
  279. self.wanted_list = wanted_list
  280. self.col_specs_lines_template.update(_p.template_changes)
  281. self.debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  282. self.credit_pos = 'credit' in wanted_list and wanted_list.index('credit')
  283. if not (self.credit_pos and self.debit_pos) and 'balance' in wanted_list:
  284. raise orm.except_orm(_('Customisation Error!'),
  285. _("The 'Balance' field is a calculated XLS field requiring the presence of the 'Debit' and 'Credit' fields !"))
  286. for o in objects:
  287. sheet_name = ' - '.join([o[1].code, o[0].code])[:31].replace('/', '-')
  288. sheet_name = sheet_name[:31].replace('/', '-')
  289. ws = wb.add_sheet(sheet_name)
  290. ws.panes_frozen = True
  291. ws.remove_splits = True
  292. ws.portrait = 0 # Landscape
  293. ws.fit_width_to_pages = 1
  294. row_pos = 0
  295. # set print header/footer
  296. ws.header_str = self.xls_headers['standard']
  297. ws.footer_str = self.xls_footers['standard']
  298. # Data
  299. row_pos = self._journal_title(o, ws, _p, row_pos, xlwt, _xs)
  300. row_pos = self._journal_lines(o, ws, _p, row_pos, xlwt, _xs)
  301. row_pos = self._journal_vat_summary(o, ws, _p, row_pos, xlwt, _xs)
  302. account_journal_xls('report.nov.account.journal.xls', 'account.journal.period',
  303. parser=account_journal_xls_parser)
  304. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: