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.

391 lines
18 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2009-2016 Noviat
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import xlwt
  5. from datetime import datetime
  6. from openerp.osv import orm
  7. from openerp.addons.report_xls.report_xls import report_xls
  8. from openerp.addons.report_xls.utils import rowcol_to_cell, _render
  9. from .nov_account_journal import nov_journal_print
  10. from openerp.tools.translate import _
  11. import logging
  12. _logger = logging.getLogger(__name__)
  13. class account_journal_xls_parser(nov_journal_print):
  14. def __init__(self, cr, uid, name, context):
  15. super(account_journal_xls_parser, self).__init__(cr, uid, name,
  16. context=context)
  17. journal_obj = self.pool.get('account.journal')
  18. self.context = context
  19. wanted_list = journal_obj._report_xls_fields(cr, uid, context)
  20. template_changes = journal_obj._report_xls_template(cr, uid, context)
  21. self.localcontext.update({
  22. 'datetime': datetime,
  23. 'wanted_list': wanted_list,
  24. 'template_changes': template_changes,
  25. })
  26. class account_journal_xls(report_xls):
  27. def __init__(self, name, table, rml=False, parser=False, header=True,
  28. store=False):
  29. super(account_journal_xls, self).__init__(
  30. name, table, rml, parser, header, store)
  31. # Cell Styles
  32. _xs = self.xls_styles
  33. # header
  34. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  35. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  36. self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
  37. self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
  38. # lines
  39. aml_cell_format = _xs['borders_all']
  40. self.aml_cell_style = xlwt.easyxf(aml_cell_format)
  41. self.aml_cell_style_center = xlwt.easyxf(
  42. aml_cell_format + _xs['center'])
  43. self.aml_cell_style_date = xlwt.easyxf(
  44. aml_cell_format + _xs['left'],
  45. num_format_str=report_xls.date_format)
  46. self.aml_cell_style_decimal = xlwt.easyxf(
  47. aml_cell_format + _xs['right'],
  48. num_format_str=report_xls.decimal_format)
  49. # totals
  50. rt_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  51. self.rt_cell_style = xlwt.easyxf(rt_cell_format)
  52. self.rt_cell_style_right = xlwt.easyxf(rt_cell_format + _xs['right'])
  53. self.rt_cell_style_decimal = xlwt.easyxf(
  54. rt_cell_format + _xs['right'],
  55. num_format_str=report_xls.decimal_format)
  56. # XLS Template Journal Items
  57. self.col_specs_lines_template = {
  58. 'move_name': {
  59. 'header': [1, 20, 'text', _render("_('Entry')")],
  60. 'lines':
  61. [1, 0, 'text',
  62. _render("l['move_name'] != '/' and l['move_name'] \
  63. or ('*'+str(l['move_id']))")],
  64. 'totals': [1, 0, 'text', None]},
  65. 'move_date': {
  66. 'header': [1, 13, 'text', _render("_('Date')")],
  67. 'lines':
  68. [1, 0, 'date',
  69. _render("datetime.strptime(l['move_date'],'%Y-%m-%d')"),
  70. None, self.aml_cell_style_date],
  71. 'totals': [1, 0, 'text', None]},
  72. 'acc_code': {
  73. 'header': [1, 12, 'text', _render("_('Account')")],
  74. 'lines': [1, 0, 'text', _render("l['acc_code']")],
  75. 'totals': [1, 0, 'text', None]},
  76. 'acc_name': {
  77. 'header': [1, 36, 'text', _render("_('Account Name')")],
  78. 'lines': [1, 0, 'text', _render("l['acc_name']")],
  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. 'journal': {
  89. 'header': [1, 20, 'text', _render("_('Journal')")],
  90. 'lines': [1, 0, 'text', _render("l['journal']")],
  91. 'totals': [1, 0, 'text', None]},
  92. 'journal_code': {
  93. 'header': [1, 10, 'text', _render("_('Journal')")],
  94. 'lines': [1, 0, 'text', _render("l['journal_code']")],
  95. 'totals': [1, 0, 'text', None]},
  96. 'analytic_account': {
  97. 'header': [1, 20, 'text', _render("_('Analytic Account')")],
  98. 'lines': [1, 0, 'text', _render("l['an_acc_name']")],
  99. 'totals': [1, 0, 'text', None]},
  100. 'analytic_account_code': {
  101. 'header': [1, 20, 'text', _render("_('Analytic Account')")],
  102. 'lines': [1, 0, 'text', _render("l['an_acc_code']")],
  103. 'totals': [1, 0, 'text', None]},
  104. 'partner_name': {
  105. 'header': [1, 36, 'text', _render("_('Partner')")],
  106. 'lines': [1, 0, 'text', _render("l['partner_name']")],
  107. 'totals': [1, 0, 'text', None]},
  108. 'partner_ref': {
  109. 'header': [1, 36, 'text', _render("_('Partner Reference')")],
  110. 'lines': [1, 0, 'text', _render("l['partner_ref']")],
  111. 'totals': [1, 0, 'text', None]},
  112. 'date_maturity': {
  113. 'header': [1, 13, 'text', _render("_('Maturity Date')")],
  114. 'lines':
  115. [1, 0,
  116. _render("l['date_maturity'] and 'date' or 'text'"),
  117. _render(
  118. "l['date_maturity'] and datetime.\
  119. 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,
  124. self.rh_cell_style_right],
  125. 'lines': [1, 0, 'number', _render("l['debit']"), None,
  126. self.aml_cell_style_decimal],
  127. 'totals': [1, 0, 'number', None, _render("debit_formula"),
  128. self.rt_cell_style_decimal]},
  129. 'credit': {
  130. 'header': [1, 18, 'text', _render("_('Credit')"), None,
  131. self.rh_cell_style_right],
  132. 'lines': [1, 0, 'number', _render("l['credit']"), None,
  133. self.aml_cell_style_decimal],
  134. 'totals': [1, 0, 'number', None, _render("credit_formula"),
  135. self.rt_cell_style_decimal]},
  136. 'balance': {
  137. 'header': [1, 18, 'text', _render("_('Balance')"), None,
  138. self.rh_cell_style_right],
  139. 'lines': [1, 0, 'number', None, _render("bal_formula"),
  140. self.aml_cell_style_decimal],
  141. 'totals': [1, 0, 'number', None, _render("bal_formula"),
  142. self.rt_cell_style_decimal]},
  143. 'reconcile': {
  144. 'header': [1, 12, 'text', _render("_('Rec.')"), None,
  145. self.rh_cell_style_center],
  146. 'lines': [1, 0, 'text', _render("l['reconcile']"), None,
  147. self.aml_cell_style_center],
  148. 'totals': [1, 0, 'text', None]},
  149. 'reconcile_partial': {
  150. 'header': [1, 12, 'text', _render("_('Part. Rec.')"), None,
  151. self.rh_cell_style_center],
  152. 'lines': [1, 0, 'text', _render("l['reconcile_partial']"),
  153. None, self.aml_cell_style_center],
  154. 'totals': [1, 0, 'text', None]},
  155. 'tax_code': {
  156. 'header': [1, 6, 'text', _render("_('VAT')"), None,
  157. self.rh_cell_style_center],
  158. 'lines': [1, 0, 'text', _render("l['tax_code']"), None,
  159. self.aml_cell_style_center],
  160. 'totals': [1, 0, 'text', None]},
  161. 'tax_amount': {
  162. 'header': [1, 18, 'text', _render("_('VAT Amount')"), None,
  163. self.rh_cell_style_right],
  164. 'lines': [1, 0, 'number', _render("l['tax_amount']"), None,
  165. self.aml_cell_style_decimal],
  166. 'totals': [1, 0, 'text', None]},
  167. 'amount_currency': {
  168. 'header': [1, 18, 'text', _render("_('Am. Currency')"), None,
  169. self.rh_cell_style_right],
  170. 'lines':
  171. [1, 0,
  172. _render("l['amount_currency'] and 'number' or 'text'"),
  173. _render("l['amount_currency'] or None"),
  174. None, self.aml_cell_style_decimal],
  175. 'totals': [1, 0, 'text', None]},
  176. 'currency_name': {
  177. 'header': [1, 6, 'text', _render("_('Curr.')"), None,
  178. self.rh_cell_style_center],
  179. 'lines': [1, 0, 'text', _render("l['currency_name']"), None,
  180. self.aml_cell_style_center],
  181. 'totals': [1, 0, 'text', None]},
  182. 'docname': {
  183. 'header': [1, 35, 'text', _render("_('Document')")],
  184. 'lines': [1, 0, 'text', _render("l['docname']")],
  185. 'totals': [1, 0, 'text', None]},
  186. 'move_ref': {
  187. 'header': [1, 25, 'text', _render("_('Entry Reference')")],
  188. 'lines': [1, 0, 'text', _render("l['move_ref']")],
  189. 'totals': [1, 0, 'text', None]},
  190. 'move_id': {
  191. 'header': [1, 10, 'text', _render("_('Entry Id')")],
  192. 'lines': [1, 0, 'text', _render("str(l['move_id'])")],
  193. 'totals': [1, 0, 'text', None]},
  194. }
  195. # XLS Template VAT Summary
  196. self.col_specs_vat_summary_template = {
  197. 'tax_case_name': {
  198. 'header': [1, 45, 'text', _render("_('Description')")],
  199. 'tax_totals': [1, 0, 'text', _render("t.name")]},
  200. 'tax_code': {
  201. 'header': [1, 6, 'text', _render("_('Case')")],
  202. 'tax_totals': [1, 0, 'text', _render("t.code")]},
  203. 'tax_amount': {
  204. 'header': [1, 18, 'text', _render("_('Amount')"), None,
  205. self.rh_cell_style_right],
  206. 'tax_totals': [1, 0, 'number', _render("sum_vat(o,t)"), None,
  207. self.aml_cell_style_decimal]},
  208. }
  209. def _journal_title(self, o, ws, _p, row_pos, _xs):
  210. cell_style = xlwt.easyxf(_xs['xls_title'])
  211. report_name = (10 * ' ').join([
  212. _p.company.name,
  213. _p.title(o)[0],
  214. _p.title(o)[1],
  215. _p._("Journal Overview") + ' - ' + _p.company.currency_id.name,
  216. ])
  217. c_specs = [
  218. ('report_name', 1, 0, 'text', report_name),
  219. ]
  220. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  221. row_pos = self.xls_write_row(
  222. ws, row_pos, row_data, row_style=cell_style)
  223. return row_pos + 1
  224. def _journal_lines(self, o, ws, _p, row_pos, _xs):
  225. wanted_list = self.wanted_list
  226. debit_pos = self.debit_pos
  227. credit_pos = self.credit_pos
  228. # Column headers
  229. c_specs = map(lambda x: self.render(
  230. x, self.col_specs_lines_template, 'header',
  231. render_space={'_': _p._}), wanted_list)
  232. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  233. row_pos = self.xls_write_row(
  234. ws, row_pos, row_data, row_style=self.rh_cell_style,
  235. set_column_size=True)
  236. ws.set_horz_split_pos(row_pos)
  237. # account move lines
  238. aml_start_pos = row_pos
  239. aml_cnt = len(_p.lines(o))
  240. cnt = 0
  241. for l in _p.lines(o):
  242. cnt += 1
  243. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  244. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  245. bal_formula = debit_cell + '-' + credit_cell
  246. _logger.debug('dummy call - %s', bal_formula)
  247. c_specs = map(
  248. lambda x: self.render(x, self.col_specs_lines_template,
  249. 'lines'), wanted_list)
  250. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  251. row_pos = self.xls_write_row(
  252. ws, row_pos, row_data, row_style=self.aml_cell_style)
  253. if l['draw_line'] and cnt != aml_cnt:
  254. row_pos += 1
  255. # Totals
  256. debit_start = rowcol_to_cell(aml_start_pos, debit_pos)
  257. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  258. debit_formula = 'SUM(%s:%s)' % (debit_start, debit_stop)
  259. _logger.debug('dummy call - %s', debit_formula)
  260. credit_start = rowcol_to_cell(aml_start_pos, credit_pos)
  261. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  262. credit_formula = 'SUM(%s:%s)' % (credit_start, credit_stop)
  263. _logger.debug('dummy call - %s', credit_formula)
  264. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  265. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  266. bal_formula = debit_cell + '-' + credit_cell
  267. c_specs = map(lambda x: self.render(
  268. x, self.col_specs_lines_template, 'totals'), wanted_list)
  269. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  270. row_pos = self.xls_write_row(
  271. ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  272. return row_pos + 1
  273. def _journal_vat_summary(self, o, ws, _p, row_pos, _xs):
  274. if not _p.tax_codes(o):
  275. return row_pos
  276. title_cell_style = xlwt.easyxf(_xs['bold'])
  277. c_specs = [('summary_title', 1, 0, 'text', _p._("VAT Declaration"))]
  278. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  279. row_pos = self.xls_write_row(
  280. ws, row_pos, row_data, row_style=title_cell_style) + 1
  281. wanted_list = self.wanted_list
  282. vat_summary_wanted_list = ['tax_case_name', 'tax_code', 'tax_amount']
  283. # calculate col_span
  284. cols_number = len(wanted_list)
  285. vat_summary_cols_number = len(vat_summary_wanted_list)
  286. if vat_summary_cols_number > cols_number:
  287. raise orm.except_orm(
  288. _('Programming Error!'),
  289. _("vat_summary_cols_number should be < cols_number !"))
  290. index = 0
  291. for i in range(vat_summary_cols_number):
  292. col = vat_summary_wanted_list[i]
  293. col_size = self.col_specs_lines_template[
  294. wanted_list[index]]['header'][1]
  295. templ_col_size = self.col_specs_vat_summary_template[
  296. col]['header'][1]
  297. # _logger.warn("col=%s, col_size=%s, templ_col_size=%s",
  298. # col, col_size, templ_col_size)
  299. col_span = 1
  300. if templ_col_size > col_size:
  301. new_size = col_size
  302. while templ_col_size > new_size:
  303. col_span += 1
  304. index += 1
  305. new_size += self.col_specs_lines_template[
  306. wanted_list[index]]['header'][1]
  307. self.col_specs_vat_summary_template[col]['header'][0] = col_span
  308. self.col_specs_vat_summary_template[
  309. col]['tax_totals'][0] = col_span
  310. index += 1
  311. c_specs = map(lambda x: self.render(
  312. x, self.col_specs_vat_summary_template, 'header'),
  313. vat_summary_wanted_list)
  314. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  315. row_pos = self.xls_write_row(
  316. ws, row_pos, row_data, row_style=self.rh_cell_style)
  317. for t in _p.tax_codes(o):
  318. c_specs = map(lambda x: self.render(
  319. x, self.col_specs_vat_summary_template, 'tax_totals'),
  320. vat_summary_wanted_list)
  321. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  322. row_pos = self.xls_write_row(
  323. ws, row_pos, row_data, row_style=self.aml_cell_style)
  324. return row_pos
  325. def generate_xls_report(self, _p, _xs, data, objects, wb):
  326. wanted_list = _p.wanted_list
  327. if _p.display_currency:
  328. wanted_list += ['amount_currency', 'currency_name']
  329. self.wanted_list = wanted_list
  330. self.col_specs_lines_template.update(_p.template_changes)
  331. self.debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  332. self.credit_pos = 'credit' in wanted_list and wanted_list.index(
  333. 'credit')
  334. if not (self.credit_pos and self.debit_pos) and 'balance' \
  335. in wanted_list:
  336. raise orm.except_orm(_('Customisation Error!'),
  337. _("The 'Balance' field is a calculated XLS \
  338. field requiring the presence of the \
  339. 'Debit' and 'Credit' fields !"))
  340. for o in objects:
  341. sheet_name = ' - '.join([o[1].code, o[0].code]
  342. )[:31].replace('/', '-')
  343. sheet_name = sheet_name[:31].replace('/', '-')
  344. ws = wb.add_sheet(sheet_name)
  345. ws.panes_frozen = True
  346. ws.remove_splits = True
  347. ws.portrait = 0 # Landscape
  348. ws.fit_width_to_pages = 1
  349. row_pos = 0
  350. # set print header/footer
  351. ws.header_str = self.xls_headers['standard']
  352. ws.footer_str = self.xls_footers['standard']
  353. # Data
  354. row_pos = self._journal_title(o, ws, _p, row_pos, _xs)
  355. row_pos = self._journal_lines(o, ws, _p, row_pos, _xs)
  356. row_pos = self._journal_vat_summary(o, ws, _p, row_pos, _xs)
  357. account_journal_xls('report.nov.account.journal.xls', 'account.journal.period',
  358. parser=account_journal_xls_parser)