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.

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