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.

373 lines
17 KiB

11 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2014 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.report import report_sxw
  26. from openerp.addons.report_xls.report_xls import report_xls
  27. from openerp.addons.report_xls.utils import rowcol_to_cell, _render
  28. from openerp.tools.translate import translate, _
  29. import logging
  30. _logger = logging.getLogger(__name__)
  31. _ir_translation_name = 'move.line.list.xls'
  32. class move_line_xls_parser(report_sxw.rml_parse):
  33. def __init__(self, cr, uid, name, context):
  34. super(move_line_xls_parser, self).__init__(
  35. cr, uid, name, context=context)
  36. move_obj = self.pool.get('account.move.line')
  37. self.context = context
  38. wanted_list = move_obj._report_xls_fields(cr, uid, context)
  39. template_changes = move_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. '_': self._,
  45. })
  46. def _(self, src):
  47. lang = self.context.get('lang', 'en_US')
  48. return translate(self.cr, _ir_translation_name, 'report', lang, src) \
  49. or src
  50. class move_line_xls(report_xls):
  51. def __init__(self, name, table, rml=False, parser=False, header=True,
  52. store=False):
  53. super(move_line_xls, self).__init__(
  54. name, table, rml, parser, header, store)
  55. # Cell Styles
  56. _xs = self.xls_styles
  57. # header
  58. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  59. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  60. self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
  61. self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
  62. # lines
  63. aml_cell_format = _xs['borders_all']
  64. self.aml_cell_style = xlwt.easyxf(aml_cell_format)
  65. self.aml_cell_style_center = xlwt.easyxf(
  66. aml_cell_format + _xs['center'])
  67. self.aml_cell_style_date = xlwt.easyxf(
  68. aml_cell_format + _xs['left'],
  69. num_format_str=report_xls.date_format)
  70. self.aml_cell_style_decimal = xlwt.easyxf(
  71. aml_cell_format + _xs['right'],
  72. num_format_str=report_xls.decimal_format)
  73. # totals
  74. rt_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  75. self.rt_cell_style = xlwt.easyxf(rt_cell_format)
  76. self.rt_cell_style_right = xlwt.easyxf(rt_cell_format + _xs['right'])
  77. self.rt_cell_style_decimal = xlwt.easyxf(
  78. rt_cell_format + _xs['right'],
  79. num_format_str=report_xls.decimal_format)
  80. # XLS Template
  81. self.col_specs_template = {
  82. 'move': {
  83. 'header': [1, 20, 'text', _render("_('Entry')")],
  84. 'lines': [1, 0, 'text', _render("line.move_id.name or ''")],
  85. 'totals': [1, 0, 'text', None]},
  86. 'name': {
  87. 'header': [1, 42, 'text', _render("_('Name')")],
  88. 'lines': [1, 0, 'text', _render("line.name or ''")],
  89. 'totals': [1, 0, 'text', None]},
  90. 'ref': {
  91. 'header': [1, 42, 'text', _render("_('Reference')")],
  92. 'lines': [1, 0, 'text', _render("line.ref or ''")],
  93. 'totals': [1, 0, 'text', None]},
  94. 'date': {
  95. 'header': [1, 13, 'text', _render("_('Effective Date')")],
  96. 'lines': [1, 0, 'date',
  97. _render("datetime.strptime(line.date,'%Y-%m-%d')"),
  98. None, self.aml_cell_style_date],
  99. 'totals': [1, 0, 'text', None]},
  100. 'period': {
  101. 'header': [1, 12, 'text', _render("_('Period')")],
  102. 'lines':
  103. [1, 0, 'text',
  104. _render("line.period_id.code or line.period_id.name")],
  105. 'totals': [1, 0, 'text', None]},
  106. 'partner': {
  107. 'header': [1, 36, 'text', _render("_('Partner')")],
  108. 'lines':
  109. [1, 0, 'text',
  110. _render("line.partner_id and line.partner_id.name or ''")],
  111. 'totals': [1, 0, 'text', None]},
  112. 'partner_ref': {
  113. 'header': [1, 36, 'text', _render("_('Partner Reference')")],
  114. 'lines':
  115. [1, 0, 'text',
  116. _render("line.partner_id and line.partner_id.ref or ''")],
  117. 'totals': [1, 0, 'text', None]},
  118. 'account': {
  119. 'header': [1, 12, 'text', _render("_('Account')")],
  120. 'lines': [1, 0, 'text', _render("line.account_id.code")],
  121. 'totals': [1, 0, 'text', None]},
  122. 'date_maturity': {
  123. 'header': [1, 13, 'text', _render("_('Maturity Date')")],
  124. 'lines':
  125. [1, 0,
  126. _render("line.date_maturity.val and 'date' or 'text'"),
  127. _render(
  128. "line.date_maturity.val \
  129. and datetime.strptime(line.date_maturity,'%Y-%m-%d') \
  130. or None"),
  131. None, self.aml_cell_style_date],
  132. 'totals': [1, 0, 'text', None]},
  133. 'debit': {
  134. 'header': [1, 18, 'text', _render("_('Debit')"), None,
  135. self.rh_cell_style_right],
  136. 'lines': [1, 0, 'number', _render("line.debit"), None,
  137. self.aml_cell_style_decimal],
  138. 'totals': [1, 0, 'number', None, _render("debit_formula"),
  139. self.rt_cell_style_decimal]},
  140. 'credit': {
  141. 'header': [1, 18, 'text', _render("_('Credit')"), None,
  142. self.rh_cell_style_right],
  143. 'lines': [1, 0, 'number', _render("line.credit"), None,
  144. self.aml_cell_style_decimal],
  145. 'totals': [1, 0, 'number', None, _render("credit_formula"),
  146. self.rt_cell_style_decimal]},
  147. 'balance': {
  148. 'header': [1, 18, 'text', _render("_('Balance')"), None,
  149. self.rh_cell_style_right],
  150. 'lines': [1, 0, 'number', None, _render("bal_formula"),
  151. self.aml_cell_style_decimal],
  152. 'totals': [1, 0, 'number', None, _render("bal_formula"),
  153. self.rt_cell_style_decimal]},
  154. 'reconcile': {
  155. 'header': [1, 12, 'text', _render("_('Rec.')"), None,
  156. self.rh_cell_style_center],
  157. 'lines': [1, 0, 'text',
  158. _render("line.reconcile_id.name or ''"), None,
  159. self.aml_cell_style_center],
  160. 'totals': [1, 0, 'text', None]},
  161. 'reconcile_partial': {
  162. 'header': [1, 12, 'text', _render("_('Part. Rec.')"), None,
  163. self.rh_cell_style_center],
  164. 'lines': [1, 0, 'text',
  165. _render("line.reconcile_partial_id.name or ''"),
  166. None, self.aml_cell_style_center],
  167. 'totals': [1, 0, 'text', None]},
  168. 'tax_code': {
  169. 'header': [1, 12, 'text', _render("_('Tax Code')"), None,
  170. self.rh_cell_style_center],
  171. 'lines': [1, 0, 'text', _render("line.tax_code_id.code or ''"),
  172. None, self.aml_cell_style_center],
  173. 'totals': [1, 0, 'text', None]},
  174. 'tax_amount': {
  175. 'header': [1, 18, 'text', _render("_('Tax/Base Amount')"),
  176. None, self.rh_cell_style_right],
  177. 'lines': [1, 0, 'number', _render("line.tax_amount"), None,
  178. self.aml_cell_style_decimal],
  179. 'totals': [1, 0, 'text', None]},
  180. 'amount_currency': {
  181. 'header': [1, 18, 'text', _render("_('Am. Currency')"), None,
  182. self.rh_cell_style_right],
  183. 'lines':
  184. [1, 0,
  185. _render("line.amount_currency and 'number' or 'text'"),
  186. _render("line.amount_currency or None"),
  187. None, self.aml_cell_style_decimal],
  188. 'totals': [1, 0, 'text', None]},
  189. 'currency_name': {
  190. 'header': [1, 6, 'text', _render("_('Curr.')"), None,
  191. self.rh_cell_style_center],
  192. 'lines':
  193. [1, 0, 'text',
  194. _render("line.currency_id and line.currency_id.name or ''"),
  195. None, self.aml_cell_style_center],
  196. 'totals': [1, 0, 'text', None]},
  197. 'journal': {
  198. 'header': [1, 12, 'text', _render("_('Journal')")],
  199. 'lines': [1, 0, 'text', _render("line.journal_id.code or ''")],
  200. 'totals': [1, 0, 'text', None]},
  201. 'company_currency': {
  202. 'header': [1, 10, 'text', _render("_('Comp. Curr.')")],
  203. 'lines': [1, 0, 'text',
  204. _render("line.company_id.currency_id.name or ''"),
  205. None, self.aml_cell_style_center],
  206. 'totals': [1, 0, 'text', None]},
  207. 'analytic_account': {
  208. 'header': [1, 36, 'text', _render("_('Analytic Account')")],
  209. 'lines': [1, 0, 'text',
  210. _render("line.analytic_account_id.code or ''")],
  211. 'totals': [1, 0, 'text', None]},
  212. 'product': {
  213. 'header': [1, 36, 'text', _render("_('Product')")],
  214. 'lines': [1, 0, 'text', _render("line.product_id.name or ''")],
  215. 'totals': [1, 0, 'text', None]},
  216. 'product_ref': {
  217. 'header': [1, 36, 'text', _render("_('Product Reference')")],
  218. 'lines': [1, 0, 'text',
  219. _render("line.product_id.default_code or ''")],
  220. 'totals': [1, 0, 'text', None]},
  221. 'product_uom': {
  222. 'header': [1, 20, 'text', _render("_('Unit of Measure')")],
  223. 'lines': [1, 0, 'text',
  224. _render("line.product_uom_id.name or ''")],
  225. 'totals': [1, 0, 'text', None]},
  226. 'quantity': {
  227. 'header': [1, 8, 'text', _render("_('Qty')"), None,
  228. self.rh_cell_style_right],
  229. 'lines': [1, 0,
  230. _render("line.quantity and 'number' or 'text'"),
  231. _render("line.quantity or None"), None,
  232. self.aml_cell_style_decimal],
  233. 'totals': [1, 0, 'text', None]},
  234. 'statement': {
  235. 'header': [1, 20, 'text', _render("_('Statement')")],
  236. 'lines':
  237. [1, 0, 'text',
  238. _render("line.statement_id and line.statement_id.name or ''")
  239. ],
  240. 'totals': [1, 0, 'text', None]},
  241. 'invoice': {
  242. 'header': [1, 20, 'text', _render("_('Invoice')")],
  243. 'lines':
  244. [1, 0, 'text',
  245. _render("line.invoice and line.invoice.number or ''")],
  246. 'totals': [1, 0, 'text', None]},
  247. 'amount_residual': {
  248. 'header': [1, 18, 'text', _render("_('Residual Amount')"),
  249. None, self.rh_cell_style_right],
  250. 'lines':
  251. [1, 0,
  252. _render("line.amount_residual and 'number' or 'text'"),
  253. _render("line.amount_residual or None"),
  254. None, self.aml_cell_style_decimal],
  255. 'totals': [1, 0, 'text', None]},
  256. 'amount_residual_currency': {
  257. 'header': [1, 18, 'text', _render("_('Res. Am. in Curr.')"),
  258. None, self.rh_cell_style_right],
  259. 'lines':
  260. [1, 0,
  261. _render(
  262. "line.amount_residual_currency and 'number' or 'text'"),
  263. _render("line.amount_residual_currency or None"),
  264. None, self.aml_cell_style_decimal],
  265. 'totals': [1, 0, 'text', None]},
  266. 'narration': {
  267. 'header': [1, 42, 'text', _render("_('Notes')")],
  268. 'lines': [1, 0, 'text',
  269. _render("line.move_id.narration or ''")],
  270. 'totals': [1, 0, 'text', None]},
  271. 'blocked': {
  272. 'header': [1, 4, 'text', _('Lit.'),
  273. None, self.rh_cell_style_right],
  274. 'lines': [1, 0, 'text', _render("line.blocked and 'x' or ''"),
  275. None, self.aml_cell_style_center],
  276. 'totals': [1, 0, 'text', None]},
  277. }
  278. def generate_xls_report(self, _p, _xs, data, objects, wb):
  279. wanted_list = _p.wanted_list
  280. self.col_specs_template.update(_p.template_changes)
  281. _ = _p._
  282. debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  283. credit_pos = 'credit' in wanted_list and wanted_list.index('credit')
  284. if not (credit_pos and debit_pos) and 'balance' in wanted_list:
  285. raise orm.except_orm(
  286. _('Customisation Error!'),
  287. _("The 'Balance' field is a calculated XLS field requiring \
  288. the presence of the 'Debit' and 'Credit' fields !"))
  289. # report_name = objects[0]._description or objects[0]._name
  290. report_name = _("Journal Items")
  291. ws = wb.add_sheet(report_name[:31])
  292. ws.panes_frozen = True
  293. ws.remove_splits = True
  294. ws.portrait = 0 # Landscape
  295. ws.fit_width_to_pages = 1
  296. row_pos = 0
  297. # set print header/footer
  298. ws.header_str = self.xls_headers['standard']
  299. ws.footer_str = self.xls_footers['standard']
  300. # Title
  301. cell_style = xlwt.easyxf(_xs['xls_title'])
  302. c_specs = [
  303. ('report_name', 1, 0, 'text', report_name),
  304. ]
  305. row_data = self.xls_row_template(c_specs, ['report_name'])
  306. row_pos = self.xls_write_row(
  307. ws, row_pos, row_data, row_style=cell_style)
  308. row_pos += 1
  309. # Column headers
  310. c_specs = map(lambda x: self.render(
  311. x, self.col_specs_template, 'header', render_space={'_': _p._}),
  312. wanted_list)
  313. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  314. row_pos = self.xls_write_row(
  315. ws, row_pos, row_data, row_style=self.rh_cell_style,
  316. set_column_size=True)
  317. ws.set_horz_split_pos(row_pos)
  318. # account move lines
  319. for line in objects:
  320. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  321. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  322. bal_formula = debit_cell + '-' + credit_cell
  323. _logger.debug('dummy call - %s', bal_formula)
  324. c_specs = map(
  325. lambda x: self.render(x, self.col_specs_template, 'lines'),
  326. wanted_list)
  327. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  328. row_pos = self.xls_write_row(
  329. ws, row_pos, row_data, row_style=self.aml_cell_style)
  330. # Totals
  331. aml_cnt = len(objects)
  332. debit_start = rowcol_to_cell(row_pos - aml_cnt, debit_pos)
  333. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  334. debit_formula = 'SUM(%s:%s)' % (debit_start, debit_stop)
  335. _logger.debug('dummy call - %s', debit_formula)
  336. credit_start = rowcol_to_cell(row_pos - aml_cnt, credit_pos)
  337. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  338. credit_formula = 'SUM(%s:%s)' % (credit_start, credit_stop)
  339. _logger.debug('dummy call - %s', credit_formula)
  340. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  341. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  342. bal_formula = debit_cell + '-' + credit_cell
  343. _logger.debug('dummy call - %s', bal_formula)
  344. c_specs = map(
  345. lambda x: self.render(x, self.col_specs_template, 'totals'),
  346. wanted_list)
  347. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  348. row_pos = self.xls_write_row(
  349. ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  350. move_line_xls('report.move.line.list.xls',
  351. 'account.move.line',
  352. parser=move_line_xls_parser)